1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // ============================================================================
44 // ---------------------------------------------------------------------------
45 // helper functions for showing a "busy" cursor
46 // ---------------------------------------------------------------------------
48 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
49 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
50 static int gs_wxBusyCursorCount
= 0;
52 extern HCURSOR
wxGetCurrentBusyCursor()
54 return gs_wxBusyCursor
;
57 // Set the cursor to the busy cursor for all windows
58 void wxBeginBusyCursor(const wxCursor
*cursor
)
60 if ( gs_wxBusyCursorCount
++ == 0 )
62 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
63 #ifndef __WXMICROWIN__
64 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
67 //else: nothing to do, already set
70 // Restore cursor to normal
71 void wxEndBusyCursor()
73 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
74 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
76 if ( --gs_wxBusyCursorCount
== 0 )
78 #ifndef __WXMICROWIN__
79 ::SetCursor(gs_wxBusyCursorOld
);
81 gs_wxBusyCursorOld
= 0;
85 // true if we're between the above two calls
88 return gs_wxBusyCursorCount
> 0;
91 // Check whether this window wants to process messages, e.g. Stop button
92 // in long calculations.
93 bool wxCheckForInterrupt(wxWindow
*wnd
)
95 wxCHECK( wnd
, false );
98 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
100 ::TranslateMessage(&msg
);
101 ::DispatchMessage(&msg
);
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 // See also the wxGetMousePosition in window.cpp
112 // Deprecated: use wxPoint wxGetMousePosition() instead
113 void wxGetMousePosition( int* x
, int* y
)
116 GetCursorPos( & pt
);
121 // Return true if we have a colour display
122 bool wxColourDisplay()
124 #ifdef __WXMICROWIN__
128 // this function is called from wxDC ctor so it is called a *lot* of times
129 // hence we optimize it a bit but doing the check only once
131 // this should be MT safe as only the GUI thread (holding the GUI mutex)
133 static int s_isColour
= -1;
135 if ( s_isColour
== -1 )
138 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
140 s_isColour
= (noCols
== -1) || (noCols
> 2);
143 return s_isColour
!= 0;
147 // Returns depth of screen
151 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
154 // Get size of display
155 void wxDisplaySize(int *width
, int *height
)
157 #ifdef __WXMICROWIN__
159 HWND hWnd
= GetDesktopWindow();
160 ::GetWindowRect(hWnd
, & rect
);
163 *width
= rect
.right
- rect
.left
;
165 *height
= rect
.bottom
- rect
.top
;
166 #else // !__WXMICROWIN__
170 *width
= ::GetDeviceCaps(dc
, HORZRES
);
172 *height
= ::GetDeviceCaps(dc
, VERTRES
);
173 #endif // __WXMICROWIN__/!__WXMICROWIN__
176 void wxDisplaySizeMM(int *width
, int *height
)
178 #ifdef __WXMICROWIN__
188 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
190 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
194 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
196 #if defined(__WXMICROWIN__)
198 wxDisplaySize(width
, height
);
200 // Determine the desktop dimensions minus the taskbar and any other
201 // special decorations...
204 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
207 if (width
) *width
= r
.right
- r
.left
;
208 if (height
) *height
= r
.bottom
- r
.top
;
212 // ---------------------------------------------------------------------------
213 // window information functions
214 // ---------------------------------------------------------------------------
216 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
222 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
223 ::GetWindowText((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
229 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
234 #ifndef __WXMICROWIN__
237 int len
= 256; // some starting value
241 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
245 // the class name might have been truncated, retry with larger
255 #endif // !__WXMICROWIN__
260 int WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
262 return ::GetWindowLong((HWND
)hWnd
, GWL_ID
);
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 void PixelToHIMETRIC(LONG
*x
, LONG
*y
, HDC hdcRef
)
271 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
272 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
273 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
274 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
276 *x
*= (iWidthMM
* 100);
278 *y
*= (iHeightMM
* 100);
282 void HIMETRICToPixel(LONG
*x
, LONG
*y
, HDC hdcRef
)
284 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
285 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
286 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
287 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
290 *x
/= (iWidthMM
* 100);
292 *y
/= (iHeightMM
* 100);
295 void HIMETRICToPixel(LONG
*x
, LONG
*y
)
297 HIMETRICToPixel(x
, y
, ScreenHDC());
300 void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
302 PixelToHIMETRIC(x
, y
, ScreenHDC());
305 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
313 Polyline(hdc
, points
, 2);
315 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);
320 // ----------------------------------------------------------------------------
321 // Shell API wrappers
322 // ----------------------------------------------------------------------------
324 extern bool wxEnableFileNameAutoComplete(HWND hwnd
)
326 #if wxUSE_DYNLIB_CLASS
327 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
329 static SHAutoComplete_t s_pfnSHAutoComplete
= NULL
;
330 static bool s_initialized
= false;
332 if ( !s_initialized
)
334 s_initialized
= true;
337 wxDynamicLibrary
dll(wxT("shlwapi.dll"));
338 if ( dll
.IsLoaded() )
340 s_pfnSHAutoComplete
=
341 (SHAutoComplete_t
)dll
.GetSymbol(wxT("SHAutoComplete"));
342 if ( s_pfnSHAutoComplete
)
344 // won't be unloaded until the process termination, no big deal
350 if ( !s_pfnSHAutoComplete
)
353 HRESULT hr
= s_pfnSHAutoComplete(hwnd
, 0x10 /* SHACF_FILESYS_ONLY */);
356 wxLogApiError(wxT("SHAutoComplete"), hr
);
364 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
367 // ----------------------------------------------------------------------------
368 // Launch document with default app
369 // ----------------------------------------------------------------------------
371 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
375 WinStruct
<SHELLEXECUTEINFO
> sei
;
376 sei
.lpFile
= document
.wx_str();
378 sei
.nShow
= SW_SHOWNORMAL
; // SW_SHOWDEFAULT not defined under CE (#10216)
380 sei
.nShow
= SW_SHOWDEFAULT
;
383 // avoid Windows message box in case of error for consistency with
384 // wxLaunchDefaultBrowser() even if don't show the error ourselves in this
386 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
388 if ( ::ShellExecuteEx(&sei
) )
394 // ----------------------------------------------------------------------------
395 // Launch default browser
396 // ----------------------------------------------------------------------------
398 bool wxDoLaunchDefaultBrowser(const wxString
& url
, const wxString
& scheme
, int flags
)
403 if ( flags
& wxBROWSER_NEW_WINDOW
)
405 // ShellExecuteEx() opens the URL in an existing window by default so
406 // we can't use it if we need a new window
407 wxRegKey
key(wxRegKey::HKCR
, scheme
+ wxT("\\shell\\open"));
410 // try the default browser, it must be registered at least for http URLs
411 key
.SetName(wxRegKey::HKCR
, wxT("http\\shell\\open"));
416 wxRegKey
keyDDE(key
, wxT("DDEExec"));
417 if ( keyDDE
.Exists() )
419 // we only know the syntax of WWW_OpenURL DDE request for IE,
420 // optimistically assume that all other browsers are compatible
422 static const wxChar
*TOPIC_OPEN_URL
= wxT("WWW_OpenURL");
424 wxRegKey
keyTopic(keyDDE
, wxT("topic"));
425 bool ok
= keyTopic
.Exists() &&
426 keyTopic
.QueryDefaultValue() == TOPIC_OPEN_URL
;
429 ddeCmd
= keyDDE
.QueryDefaultValue();
430 ok
= !ddeCmd
.empty();
435 // for WWW_OpenURL, the index of the window to open the URL
436 // in is -1 (meaning "current") by default, replace it with
437 // 0 which means "new" (see KB article 160957)
438 ok
= ddeCmd
.Replace(wxT("-1"), wxT("0"),
439 false /* only first occurrence */) == 1;
444 // and also replace the parameters: the topic should
445 // contain a placeholder for the URL
446 ok
= ddeCmd
.Replace(wxT("%1"), url
, false) == 1;
451 // try to send it the DDE request now but ignore the errors
454 const wxString ddeServer
= wxRegKey(keyDDE
, wxT("application"));
455 if ( wxExecuteDDE(ddeServer
, TOPIC_OPEN_URL
, ddeCmd
) )
458 // this is not necessarily an error: maybe browser is
459 // simply not running, but no matter, in any case we're
460 // going to launch it using ShellExecuteEx() below now and
461 // we shouldn't try to open a new window if we open a new
469 WinStruct
<SHELLEXECUTEINFO
> sei
;
470 sei
.lpFile
= url
.c_str();
471 sei
.lpVerb
= wxT("open");
472 sei
.nShow
= SW_SHOWNORMAL
;
473 sei
.fMask
= SEE_MASK_FLAG_NO_UI
; // we give error message ourselves
475 if ( ::ShellExecuteEx(&sei
) )