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