]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsgui.cpp
Applied #10639 (Not all previewed fonts are displayed with the correct nameface)
[wxWidgets.git] / src / msw / utilsgui.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/utilsgui.cpp
3 // Purpose: Various utility functions only available in 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 // License: 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 #include "wx/msw/registry.h"
37 #include <shellapi.h> // needed for SHELLEXECUTEINFO
38
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ---------------------------------------------------------------------------
45 // helper functions for showing a "busy" cursor
46 // ---------------------------------------------------------------------------
47
48 static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
49 static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
50 static int gs_wxBusyCursorCount = 0;
51
52 extern HCURSOR wxGetCurrentBusyCursor()
53 {
54 return gs_wxBusyCursor;
55 }
56
57 // Set the cursor to the busy cursor for all windows
58 void wxBeginBusyCursor(const wxCursor *cursor)
59 {
60 if ( gs_wxBusyCursorCount++ == 0 )
61 {
62 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
63 #ifndef __WXMICROWIN__
64 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
65 #endif
66 }
67 //else: nothing to do, already set
68 }
69
70 // Restore cursor to normal
71 void wxEndBusyCursor()
72 {
73 wxCHECK_RET( gs_wxBusyCursorCount > 0,
74 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
75
76 if ( --gs_wxBusyCursorCount == 0 )
77 {
78 #ifndef __WXMICROWIN__
79 ::SetCursor(gs_wxBusyCursorOld);
80 #endif
81 gs_wxBusyCursorOld = 0;
82 }
83 }
84
85 // true if we're between the above two calls
86 bool wxIsBusy()
87 {
88 return gs_wxBusyCursorCount > 0;
89 }
90
91 // Check whether this window wants to process messages, e.g. Stop button
92 // in long calculations.
93 bool wxCheckForInterrupt(wxWindow *wnd)
94 {
95 wxCHECK( wnd, false );
96
97 MSG msg;
98 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
99 {
100 ::TranslateMessage(&msg);
101 ::DispatchMessage(&msg);
102 }
103
104 return true;
105 }
106
107 // MSW only: get user-defined resource from the .res file.
108 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
109
110 #ifndef __WXMICROWIN__
111 char *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType, int* pLen /* = NULL */)
112 {
113 HRSRC hResource = ::FindResource(wxGetInstance(),
114 resourceName.wx_str(),
115 resourceType.wx_str());
116 if ( hResource == 0 )
117 return NULL;
118
119 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
120 if ( hData == 0 )
121 return NULL;
122
123 void *theText = ::LockResource(hData);
124 if ( !theText )
125 return NULL;
126
127 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
128 // so we need to find the length of the resource.
129 int len = ::SizeofResource(wxGetInstance(), hResource);
130 char *s = new char[len + 1];
131 memcpy(s, theText, len);
132 s[len] = '\0'; // NUL-terminate in case the resource itself wasn't
133
134 // Obsolete in WIN32
135 #ifndef __WIN32__
136 UnlockResource(hData);
137 #endif
138
139 // No need??
140 // GlobalFree(hData);
141
142 if (pLen)
143 *pLen = len;
144
145 return s;
146 }
147 #endif // __WXMICROWIN__
148
149 // ----------------------------------------------------------------------------
150 // get display info
151 // ----------------------------------------------------------------------------
152
153 // See also the wxGetMousePosition in window.cpp
154 // Deprecated: use wxPoint wxGetMousePosition() instead
155 void wxGetMousePosition( int* x, int* y )
156 {
157 POINT pt;
158 GetCursorPos( & pt );
159 if ( x ) *x = pt.x;
160 if ( y ) *y = pt.y;
161 }
162
163 // Return true if we have a colour display
164 bool wxColourDisplay()
165 {
166 #ifdef __WXMICROWIN__
167 // MICROWIN_TODO
168 return true;
169 #else
170 // this function is called from wxDC ctor so it is called a *lot* of times
171 // hence we optimize it a bit but doing the check only once
172 //
173 // this should be MT safe as only the GUI thread (holding the GUI mutex)
174 // can call us
175 static int s_isColour = -1;
176
177 if ( s_isColour == -1 )
178 {
179 ScreenHDC dc;
180 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
181
182 s_isColour = (noCols == -1) || (noCols > 2);
183 }
184
185 return s_isColour != 0;
186 #endif
187 }
188
189 // Returns depth of screen
190 int wxDisplayDepth()
191 {
192 ScreenHDC dc;
193 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
194 }
195
196 // Get size of display
197 void wxDisplaySize(int *width, int *height)
198 {
199 #ifdef __WXMICROWIN__
200 RECT rect;
201 HWND hWnd = GetDesktopWindow();
202 ::GetWindowRect(hWnd, & rect);
203
204 if ( width )
205 *width = rect.right - rect.left;
206 if ( height )
207 *height = rect.bottom - rect.top;
208 #else // !__WXMICROWIN__
209 ScreenHDC dc;
210
211 if ( width )
212 *width = ::GetDeviceCaps(dc, HORZRES);
213 if ( height )
214 *height = ::GetDeviceCaps(dc, VERTRES);
215 #endif // __WXMICROWIN__/!__WXMICROWIN__
216 }
217
218 void wxDisplaySizeMM(int *width, int *height)
219 {
220 #ifdef __WXMICROWIN__
221 // MICROWIN_TODO
222 if ( width )
223 *width = 0;
224 if ( height )
225 *height = 0;
226 #else
227 ScreenHDC dc;
228
229 if ( width )
230 *width = ::GetDeviceCaps(dc, HORZSIZE);
231 if ( height )
232 *height = ::GetDeviceCaps(dc, VERTSIZE);
233 #endif
234 }
235
236 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
237 {
238 #if defined(__WXMICROWIN__)
239 *x = 0; *y = 0;
240 wxDisplaySize(width, height);
241 #else
242 // Determine the desktop dimensions minus the taskbar and any other
243 // special decorations...
244 RECT r;
245
246 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
247 if (x) *x = r.left;
248 if (y) *y = r.top;
249 if (width) *width = r.right - r.left;
250 if (height) *height = r.bottom - r.top;
251 #endif
252 }
253
254 // ---------------------------------------------------------------------------
255 // window information functions
256 // ---------------------------------------------------------------------------
257
258 wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
259 {
260 wxString str;
261
262 if ( hWnd )
263 {
264 int len = GetWindowTextLength((HWND)hWnd) + 1;
265 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
266 }
267
268 return str;
269 }
270
271 wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
272 {
273 wxString str;
274
275 // MICROWIN_TODO
276 #ifndef __WXMICROWIN__
277 if ( hWnd )
278 {
279 int len = 256; // some starting value
280
281 for ( ;; )
282 {
283 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
284
285 if ( count == len )
286 {
287 // the class name might have been truncated, retry with larger
288 // buffer
289 len *= 2;
290 }
291 else
292 {
293 break;
294 }
295 }
296 }
297 #endif // !__WXMICROWIN__
298
299 return str;
300 }
301
302 int WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
303 {
304 return ::GetWindowLong((HWND)hWnd, GWL_ID);
305 }
306
307 // ----------------------------------------------------------------------------
308 // Metafile helpers
309 // ----------------------------------------------------------------------------
310
311 void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef)
312 {
313 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
314 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
315 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
316 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
317
318 *x *= (iWidthMM * 100);
319 *x /= iWidthPels;
320 *y *= (iHeightMM * 100);
321 *y /= iHeightPels;
322 }
323
324 void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef)
325 {
326 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
327 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
328 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
329 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
330
331 *x *= iWidthPels;
332 *x /= (iWidthMM * 100);
333 *y *= iHeightPels;
334 *y /= (iHeightMM * 100);
335 }
336
337 void HIMETRICToPixel(LONG *x, LONG *y)
338 {
339 HIMETRICToPixel(x, y, ScreenHDC());
340 }
341
342 void PixelToHIMETRIC(LONG *x, LONG *y)
343 {
344 PixelToHIMETRIC(x, y, ScreenHDC());
345 }
346
347 void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
348 {
349 #ifdef __WXWINCE__
350 POINT points[2];
351 points[0].x = x1;
352 points[0].y = y1;
353 points[1].x = x2;
354 points[1].y = y2;
355 Polyline(hdc, points, 2);
356 #else
357 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
358 #endif
359 }
360
361
362 // ----------------------------------------------------------------------------
363 // Shell API wrappers
364 // ----------------------------------------------------------------------------
365
366 extern bool wxEnableFileNameAutoComplete(HWND hwnd)
367 {
368 #if wxUSE_DYNLIB_CLASS
369 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
370
371 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
372 static bool s_initialized = false;
373
374 if ( !s_initialized )
375 {
376 s_initialized = true;
377
378 wxLogNull nolog;
379 wxDynamicLibrary dll(wxT("shlwapi.dll"));
380 if ( dll.IsLoaded() )
381 {
382 s_pfnSHAutoComplete =
383 (SHAutoComplete_t)dll.GetSymbol(wxT("SHAutoComplete"));
384 if ( s_pfnSHAutoComplete )
385 {
386 // won't be unloaded until the process termination, no big deal
387 dll.Detach();
388 }
389 }
390 }
391
392 if ( !s_pfnSHAutoComplete )
393 return false;
394
395 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
396 if ( FAILED(hr) )
397 {
398 wxLogApiError(wxT("SHAutoComplete"), hr);
399 return false;
400 }
401
402 return true;
403 #else
404 wxUnusedVar(hwnd);
405 return false;
406 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
407 }
408
409 // ----------------------------------------------------------------------------
410 // Launch document with default app
411 // ----------------------------------------------------------------------------
412
413 bool wxLaunchDefaultApplication(const wxString& document, int flags)
414 {
415 wxUnusedVar(flags);
416
417 WinStruct<SHELLEXECUTEINFO> sei;
418 sei.lpFile = document.wx_str();
419 sei.lpVerb = wxT("open");
420 #ifdef __WXWINCE__
421 sei.nShow = SW_SHOWNORMAL; // SW_SHOWDEFAULT not defined under CE (#10216)
422 #else
423 sei.nShow = SW_SHOWDEFAULT;
424 #endif
425
426 // avoid Windows message box in case of error for consistency with
427 // wxLaunchDefaultBrowser() even if don't show the error ourselves in this
428 // function
429 sei.fMask = SEE_MASK_FLAG_NO_UI;
430
431 if ( ::ShellExecuteEx(&sei) )
432 return true;
433
434 return false;
435 }
436
437 // ----------------------------------------------------------------------------
438 // Launch default browser
439 // ----------------------------------------------------------------------------
440
441 bool wxDoLaunchDefaultBrowser(const wxString& url, const wxString& scheme, int flags)
442 {
443 wxUnusedVar(flags);
444
445 #if wxUSE_IPC
446 if ( flags & wxBROWSER_NEW_WINDOW )
447 {
448 // ShellExecuteEx() opens the URL in an existing window by default so
449 // we can't use it if we need a new window
450 wxRegKey key(wxRegKey::HKCR, scheme + wxT("\\shell\\open"));
451 if ( !key.Exists() )
452 {
453 // try the default browser, it must be registered at least for http URLs
454 key.SetName(wxRegKey::HKCR, wxT("http\\shell\\open"));
455 }
456
457 if ( key.Exists() )
458 {
459 wxRegKey keyDDE(key, wxT("DDEExec"));
460 if ( keyDDE.Exists() )
461 {
462 // we only know the syntax of WWW_OpenURL DDE request for IE,
463 // optimistically assume that all other browsers are compatible
464 // with it
465 static const wxChar *TOPIC_OPEN_URL = wxT("WWW_OpenURL");
466 wxString ddeCmd;
467 wxRegKey keyTopic(keyDDE, wxT("topic"));
468 bool ok = keyTopic.Exists() &&
469 keyTopic.QueryDefaultValue() == TOPIC_OPEN_URL;
470 if ( ok )
471 {
472 ddeCmd = keyDDE.QueryDefaultValue();
473 ok = !ddeCmd.empty();
474 }
475
476 if ( ok )
477 {
478 // for WWW_OpenURL, the index of the window to open the URL
479 // in is -1 (meaning "current") by default, replace it with
480 // 0 which means "new" (see KB article 160957)
481 ok = ddeCmd.Replace(wxT("-1"), wxT("0"),
482 false /* only first occurrence */) == 1;
483 }
484
485 if ( ok )
486 {
487 // and also replace the parameters: the topic should
488 // contain a placeholder for the URL
489 ok = ddeCmd.Replace(wxT("%1"), url, false) == 1;
490 }
491
492 if ( ok )
493 {
494 // try to send it the DDE request now but ignore the errors
495 wxLogNull noLog;
496
497 const wxString ddeServer = wxRegKey(keyDDE, wxT("application"));
498 if ( wxExecuteDDE(ddeServer, TOPIC_OPEN_URL, ddeCmd) )
499 return true;
500
501 // this is not necessarily an error: maybe browser is
502 // simply not running, but no matter, in any case we're
503 // going to launch it using ShellExecuteEx() below now and
504 // we shouldn't try to open a new window if we open a new
505 // browser anyhow
506 }
507 }
508 }
509 }
510 #endif // wxUSE_IPC
511
512 WinStruct<SHELLEXECUTEINFO> sei;
513 sei.lpFile = url.c_str();
514 sei.lpVerb = wxT("open");
515 sei.nShow = SW_SHOWNORMAL;
516 sei.fMask = SEE_MASK_FLAG_NO_UI; // we give error message ourselves
517
518 if ( ::ShellExecuteEx(&sei) )
519 return true;
520
521 return false;
522 }