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