1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/utilsgui.cpp
3 // Purpose: Various utility functions only available in GUI
4 // Author: Vadim Zeitlin
6 // Created: 21.06.2003 (extracted from msw/utils.cpp)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/cursor.h"
29 #include "wx/window.h"
33 #include "wx/dynlib.h"
35 #include "wx/msw/private.h" // includes <windows.h>
36 #include "wx/msw/registry.h"
37 #include <shellapi.h> // needed for SHELLEXECUTEINFO
40 // ============================================================================
42 // ============================================================================
47 ::MessageBeep((UINT
)-1); // default sound
50 // ---------------------------------------------------------------------------
51 // helper functions for showing a "busy" cursor
52 // ---------------------------------------------------------------------------
54 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
55 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
56 static int gs_wxBusyCursorCount
= 0;
58 extern HCURSOR
wxGetCurrentBusyCursor()
60 return gs_wxBusyCursor
;
63 // Set the cursor to the busy cursor for all windows
64 void wxBeginBusyCursor(const wxCursor
*cursor
)
66 if ( gs_wxBusyCursorCount
++ == 0 )
68 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
69 #ifndef __WXMICROWIN__
70 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
73 //else: nothing to do, already set
76 // Restore cursor to normal
77 void wxEndBusyCursor()
79 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
80 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
82 if ( --gs_wxBusyCursorCount
== 0 )
84 #ifndef __WXMICROWIN__
85 ::SetCursor(gs_wxBusyCursorOld
);
87 gs_wxBusyCursorOld
= 0;
91 // true if we're between the above two calls
94 return gs_wxBusyCursorCount
> 0;
97 // Check whether this window wants to process messages, e.g. Stop button
98 // in long calculations.
99 bool wxCheckForInterrupt(wxWindow
*wnd
)
101 wxCHECK( wnd
, false );
104 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
106 ::TranslateMessage(&msg
);
107 ::DispatchMessage(&msg
);
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 // See also the wxGetMousePosition in window.cpp
118 // Deprecated: use wxPoint wxGetMousePosition() instead
119 void wxGetMousePosition( int* x
, int* y
)
122 wxGetCursorPosMSW( & pt
);
127 // Return true if we have a colour display
128 bool wxColourDisplay()
130 #ifdef __WXMICROWIN__
134 // this function is called from wxDC ctor so it is called a *lot* of times
135 // hence we optimize it a bit but doing the check only once
137 // this should be MT safe as only the GUI thread (holding the GUI mutex)
139 static int s_isColour
= -1;
141 if ( s_isColour
== -1 )
144 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
146 s_isColour
= (noCols
== -1) || (noCols
> 2);
149 return s_isColour
!= 0;
153 // Returns depth of screen
157 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
160 // Get size of display
161 void wxDisplaySize(int *width
, int *height
)
163 #ifdef __WXMICROWIN__
165 HWND hWnd
= GetDesktopWindow();
166 ::GetWindowRect(hWnd
, & rect
);
169 *width
= rect
.right
- rect
.left
;
171 *height
= rect
.bottom
- rect
.top
;
172 #else // !__WXMICROWIN__
176 *width
= ::GetDeviceCaps(dc
, HORZRES
);
178 *height
= ::GetDeviceCaps(dc
, VERTRES
);
179 #endif // __WXMICROWIN__/!__WXMICROWIN__
182 void wxDisplaySizeMM(int *width
, int *height
)
184 #ifdef __WXMICROWIN__
194 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
196 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
200 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
202 #if defined(__WXMICROWIN__)
204 wxDisplaySize(width
, height
);
206 // Determine the desktop dimensions minus the taskbar and any other
207 // special decorations...
210 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
213 if (width
) *width
= r
.right
- r
.left
;
214 if (height
) *height
= r
.bottom
- r
.top
;
218 // ---------------------------------------------------------------------------
219 // window information functions
220 // ---------------------------------------------------------------------------
222 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
228 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
229 ::GetWindowText((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
235 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
240 #ifndef __WXMICROWIN__
243 int len
= 256; // some starting value
247 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
251 // the class name might have been truncated, retry with larger
261 #endif // !__WXMICROWIN__
266 int WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
268 return ::GetWindowLong((HWND
)hWnd
, GWL_ID
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 void PixelToHIMETRIC(LONG
*x
, LONG
*y
, HDC hdcRef
)
277 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
278 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
279 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
280 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
282 *x
*= (iWidthMM
* 100);
284 *y
*= (iHeightMM
* 100);
288 void HIMETRICToPixel(LONG
*x
, LONG
*y
, HDC hdcRef
)
290 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
291 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
292 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
293 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
296 *x
/= (iWidthMM
* 100);
298 *y
/= (iHeightMM
* 100);
301 void HIMETRICToPixel(LONG
*x
, LONG
*y
)
303 HIMETRICToPixel(x
, y
, ScreenHDC());
306 void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
308 PixelToHIMETRIC(x
, y
, ScreenHDC());
311 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
319 Polyline(hdc
, points
, 2);
321 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);
326 // ----------------------------------------------------------------------------
327 // Shell API wrappers
328 // ----------------------------------------------------------------------------
330 extern bool wxEnableFileNameAutoComplete(HWND hwnd
)
332 #if wxUSE_DYNLIB_CLASS
333 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
335 static SHAutoComplete_t s_pfnSHAutoComplete
= NULL
;
336 static bool s_initialized
= false;
338 if ( !s_initialized
)
340 s_initialized
= true;
343 wxDynamicLibrary
dll(wxT("shlwapi.dll"));
344 if ( dll
.IsLoaded() )
346 s_pfnSHAutoComplete
=
347 (SHAutoComplete_t
)dll
.GetSymbol(wxT("SHAutoComplete"));
348 if ( s_pfnSHAutoComplete
)
350 // won't be unloaded until the process termination, no big deal
356 if ( !s_pfnSHAutoComplete
)
359 HRESULT hr
= s_pfnSHAutoComplete(hwnd
, 0x10 /* SHACF_FILESYS_ONLY */);
362 wxLogApiError(wxT("SHAutoComplete"), hr
);
370 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
373 // ----------------------------------------------------------------------------
374 // Launch document with default app
375 // ----------------------------------------------------------------------------
377 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
381 WinStruct
<SHELLEXECUTEINFO
> sei
;
382 sei
.lpFile
= document
.t_str();
384 sei
.nShow
= SW_SHOWNORMAL
; // SW_SHOWDEFAULT not defined under CE (#10216)
386 sei
.nShow
= SW_SHOWDEFAULT
;
389 // avoid Windows message box in case of error for consistency with
390 // wxLaunchDefaultBrowser() even if don't show the error ourselves in this
392 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
394 if ( ::ShellExecuteEx(&sei
) )
400 // ----------------------------------------------------------------------------
401 // Launch default browser
402 // ----------------------------------------------------------------------------
404 bool wxDoLaunchDefaultBrowser(const wxString
& url
, const wxString
& scheme
, int flags
)
409 if ( flags
& wxBROWSER_NEW_WINDOW
)
411 // ShellExecuteEx() opens the URL in an existing window by default so
412 // we can't use it if we need a new window
413 wxRegKey
key(wxRegKey::HKCR
, scheme
+ wxT("\\shell\\open"));
416 // try the default browser, it must be registered at least for http URLs
417 key
.SetName(wxRegKey::HKCR
, wxT("http\\shell\\open"));
422 wxRegKey
keyDDE(key
, wxT("DDEExec"));
423 if ( keyDDE
.Exists() )
425 // we only know the syntax of WWW_OpenURL DDE request for IE,
426 // optimistically assume that all other browsers are compatible
428 static const wxChar
*TOPIC_OPEN_URL
= wxT("WWW_OpenURL");
430 wxRegKey
keyTopic(keyDDE
, wxT("topic"));
431 bool ok
= keyTopic
.Exists() &&
432 keyTopic
.QueryDefaultValue() == TOPIC_OPEN_URL
;
435 ddeCmd
= keyDDE
.QueryDefaultValue();
436 ok
= !ddeCmd
.empty();
441 // for WWW_OpenURL, the index of the window to open the URL
442 // in is -1 (meaning "current") by default, replace it with
443 // 0 which means "new" (see KB article 160957)
444 ok
= ddeCmd
.Replace(wxT("-1"), wxT("0"),
445 false /* only first occurrence */) == 1;
450 // and also replace the parameters: the topic should
451 // contain a placeholder for the URL
452 ok
= ddeCmd
.Replace(wxT("%1"), url
, false) == 1;
457 // try to send it the DDE request now but ignore the errors
460 const wxString ddeServer
= wxRegKey(keyDDE
, wxT("application"));
461 if ( wxExecuteDDE(ddeServer
, TOPIC_OPEN_URL
, ddeCmd
) )
464 // this is not necessarily an error: maybe browser is
465 // simply not running, but no matter, in any case we're
466 // going to launch it using ShellExecuteEx() below now and
467 // we shouldn't try to open a new window if we open a new
475 WinStruct
<SHELLEXECUTEINFO
> sei
;
476 sei
.lpFile
= url
.c_str();
477 sei
.lpVerb
= wxT("open");
478 sei
.nShow
= SW_SHOWNORMAL
;
479 sei
.fMask
= SEE_MASK_FLAG_NO_UI
; // we give error message ourselves
481 if ( ::ShellExecuteEx(&sei
) )