]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsgui.cpp
fixes to pass tests
[wxWidgets.git] / src / msw / utilsgui.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/utilsgui.cpp
3 // Purpose: Various utility functions only available in wxMSW GUI
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 21.06.2003 (extracted from msw/utils.cpp)
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/cursor.h"
29 #include "wx/window.h"
30 #include "wx/utils.h"
31 #endif //WX_PRECOMP
32
33 #include "wx/dynlib.h"
34
35 #include "wx/msw/private.h" // includes <windows.h>
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // Emit a beeeeeep
42 void wxBell()
43 {
44 ::MessageBeep((UINT)-1); // default sound
45 }
46
47 // ---------------------------------------------------------------------------
48 // helper functions for showing a "busy" cursor
49 // ---------------------------------------------------------------------------
50
51 static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
52 static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
53 static int gs_wxBusyCursorCount = 0;
54
55 extern HCURSOR wxGetCurrentBusyCursor()
56 {
57 return gs_wxBusyCursor;
58 }
59
60 // Set the cursor to the busy cursor for all windows
61 void wxBeginBusyCursor(const wxCursor *cursor)
62 {
63 if ( gs_wxBusyCursorCount++ == 0 )
64 {
65 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
66 #ifndef __WXMICROWIN__
67 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
68 #endif
69 }
70 //else: nothing to do, already set
71 }
72
73 // Restore cursor to normal
74 void wxEndBusyCursor()
75 {
76 wxCHECK_RET( gs_wxBusyCursorCount > 0,
77 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
78
79 if ( --gs_wxBusyCursorCount == 0 )
80 {
81 #ifndef __WXMICROWIN__
82 ::SetCursor(gs_wxBusyCursorOld);
83 #endif
84 gs_wxBusyCursorOld = 0;
85 }
86 }
87
88 // true if we're between the above two calls
89 bool wxIsBusy()
90 {
91 return gs_wxBusyCursorCount > 0;
92 }
93
94 // Check whether this window wants to process messages, e.g. Stop button
95 // in long calculations.
96 bool wxCheckForInterrupt(wxWindow *wnd)
97 {
98 wxCHECK( wnd, false );
99
100 MSG msg;
101 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
102 {
103 ::TranslateMessage(&msg);
104 ::DispatchMessage(&msg);
105 }
106
107 return true;
108 }
109
110 // ----------------------------------------------------------------------------
111 // get display info
112 // ----------------------------------------------------------------------------
113
114 // See also the wxGetMousePosition in window.cpp
115 // Deprecated: use wxPoint wxGetMousePosition() instead
116 void wxGetMousePosition( int* x, int* y )
117 {
118 POINT pt;
119 wxGetCursorPosMSW( & pt );
120 if ( x ) *x = pt.x;
121 if ( y ) *y = pt.y;
122 }
123
124 // Return true if we have a colour display
125 bool wxColourDisplay()
126 {
127 #ifdef __WXMICROWIN__
128 // MICROWIN_TODO
129 return true;
130 #else
131 // this function is called from wxDC ctor so it is called a *lot* of times
132 // hence we optimize it a bit but doing the check only once
133 //
134 // this should be MT safe as only the GUI thread (holding the GUI mutex)
135 // can call us
136 static int s_isColour = -1;
137
138 if ( s_isColour == -1 )
139 {
140 ScreenHDC dc;
141 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
142
143 s_isColour = (noCols == -1) || (noCols > 2);
144 }
145
146 return s_isColour != 0;
147 #endif
148 }
149
150 // Returns depth of screen
151 int wxDisplayDepth()
152 {
153 ScreenHDC dc;
154 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
155 }
156
157 // Get size of display
158 void wxDisplaySize(int *width, int *height)
159 {
160 #ifdef __WXMICROWIN__
161 RECT rect;
162 HWND hWnd = GetDesktopWindow();
163 ::GetWindowRect(hWnd, & rect);
164
165 if ( width )
166 *width = rect.right - rect.left;
167 if ( height )
168 *height = rect.bottom - rect.top;
169 #else // !__WXMICROWIN__
170 ScreenHDC dc;
171
172 if ( width )
173 *width = ::GetDeviceCaps(dc, HORZRES);
174 if ( height )
175 *height = ::GetDeviceCaps(dc, VERTRES);
176 #endif // __WXMICROWIN__/!__WXMICROWIN__
177 }
178
179 void wxDisplaySizeMM(int *width, int *height)
180 {
181 #ifdef __WXMICROWIN__
182 // MICROWIN_TODO
183 if ( width )
184 *width = 0;
185 if ( height )
186 *height = 0;
187 #else
188 ScreenHDC dc;
189
190 if ( width )
191 *width = ::GetDeviceCaps(dc, HORZSIZE);
192 if ( height )
193 *height = ::GetDeviceCaps(dc, VERTSIZE);
194 #endif
195 }
196
197 // ---------------------------------------------------------------------------
198 // window information functions
199 // ---------------------------------------------------------------------------
200
201 wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
202 {
203 wxString str;
204
205 if ( hWnd )
206 {
207 int len = GetWindowTextLength((HWND)hWnd) + 1;
208 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
209 }
210
211 return str;
212 }
213
214 wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
215 {
216 wxString str;
217
218 // MICROWIN_TODO
219 #ifndef __WXMICROWIN__
220 if ( hWnd )
221 {
222 int len = 256; // some starting value
223
224 for ( ;; )
225 {
226 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
227
228 if ( count == len )
229 {
230 // the class name might have been truncated, retry with larger
231 // buffer
232 len *= 2;
233 }
234 else
235 {
236 break;
237 }
238 }
239 }
240 #endif // !__WXMICROWIN__
241
242 return str;
243 }
244
245 int WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
246 {
247 return ::GetWindowLong((HWND)hWnd, GWL_ID);
248 }
249
250 // ----------------------------------------------------------------------------
251 // Metafile helpers
252 // ----------------------------------------------------------------------------
253
254 void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef)
255 {
256 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
257 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
258 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
259 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
260
261 *x *= (iWidthMM * 100);
262 *x /= iWidthPels;
263 *y *= (iHeightMM * 100);
264 *y /= iHeightPels;
265 }
266
267 void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef)
268 {
269 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
270 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
271 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
272 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
273
274 *x *= iWidthPels;
275 *x /= (iWidthMM * 100);
276 *y *= iHeightPels;
277 *y /= (iHeightMM * 100);
278 }
279
280 void HIMETRICToPixel(LONG *x, LONG *y)
281 {
282 HIMETRICToPixel(x, y, ScreenHDC());
283 }
284
285 void PixelToHIMETRIC(LONG *x, LONG *y)
286 {
287 PixelToHIMETRIC(x, y, ScreenHDC());
288 }
289
290 void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
291 {
292 #ifdef __WXWINCE__
293 POINT points[2];
294 points[0].x = x1;
295 points[0].y = y1;
296 points[1].x = x2;
297 points[1].y = y2;
298 Polyline(hdc, points, 2);
299 #else
300 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
301 #endif
302 }
303
304
305 // ----------------------------------------------------------------------------
306 // Shell API wrappers
307 // ----------------------------------------------------------------------------
308
309 extern bool wxEnableFileNameAutoComplete(HWND hwnd)
310 {
311 #if wxUSE_DYNLIB_CLASS
312 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
313
314 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
315 static bool s_initialized = false;
316
317 if ( !s_initialized )
318 {
319 s_initialized = true;
320
321 wxLogNull nolog;
322 wxDynamicLibrary dll(wxT("shlwapi.dll"));
323 if ( dll.IsLoaded() )
324 {
325 s_pfnSHAutoComplete =
326 (SHAutoComplete_t)dll.GetSymbol(wxT("SHAutoComplete"));
327 if ( s_pfnSHAutoComplete )
328 {
329 // won't be unloaded until the process termination, no big deal
330 dll.Detach();
331 }
332 }
333 }
334
335 if ( !s_pfnSHAutoComplete )
336 return false;
337
338 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
339 if ( FAILED(hr) )
340 {
341 wxLogApiError(wxT("SHAutoComplete"), hr);
342 return false;
343 }
344
345 return true;
346 #else
347 wxUnusedVar(hwnd);
348 return false;
349 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
350 }