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();
377 sei
.lpVerb
= wxT("open");
379 sei
.nShow
= SW_SHOWNORMAL
; // SW_SHOWDEFAULT not defined under CE (#10216)
381 sei
.nShow
= SW_SHOWDEFAULT
;
384 // avoid Windows message box in case of error for consistency with
385 // wxLaunchDefaultBrowser() even if don't show the error ourselves in this
387 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
389 if ( ::ShellExecuteEx(&sei
) )
395 // ----------------------------------------------------------------------------
396 // Launch default browser
397 // ----------------------------------------------------------------------------
399 bool wxDoLaunchDefaultBrowser(const wxString
& url
, const wxString
& scheme
, int flags
)
404 if ( flags
& wxBROWSER_NEW_WINDOW
)
406 // ShellExecuteEx() opens the URL in an existing window by default so
407 // we can't use it if we need a new window
408 wxRegKey
key(wxRegKey::HKCR
, scheme
+ wxT("\\shell\\open"));
411 // try the default browser, it must be registered at least for http URLs
412 key
.SetName(wxRegKey::HKCR
, wxT("http\\shell\\open"));
417 wxRegKey
keyDDE(key
, wxT("DDEExec"));
418 if ( keyDDE
.Exists() )
420 // we only know the syntax of WWW_OpenURL DDE request for IE,
421 // optimistically assume that all other browsers are compatible
423 static const wxChar
*TOPIC_OPEN_URL
= wxT("WWW_OpenURL");
425 wxRegKey
keyTopic(keyDDE
, wxT("topic"));
426 bool ok
= keyTopic
.Exists() &&
427 keyTopic
.QueryDefaultValue() == TOPIC_OPEN_URL
;
430 ddeCmd
= keyDDE
.QueryDefaultValue();
431 ok
= !ddeCmd
.empty();
436 // for WWW_OpenURL, the index of the window to open the URL
437 // in is -1 (meaning "current") by default, replace it with
438 // 0 which means "new" (see KB article 160957)
439 ok
= ddeCmd
.Replace(wxT("-1"), wxT("0"),
440 false /* only first occurrence */) == 1;
445 // and also replace the parameters: the topic should
446 // contain a placeholder for the URL
447 ok
= ddeCmd
.Replace(wxT("%1"), url
, false) == 1;
452 // try to send it the DDE request now but ignore the errors
455 const wxString ddeServer
= wxRegKey(keyDDE
, wxT("application"));
456 if ( wxExecuteDDE(ddeServer
, TOPIC_OPEN_URL
, ddeCmd
) )
459 // this is not necessarily an error: maybe browser is
460 // simply not running, but no matter, in any case we're
461 // going to launch it using ShellExecuteEx() below now and
462 // we shouldn't try to open a new window if we open a new
470 WinStruct
<SHELLEXECUTEINFO
> sei
;
471 sei
.lpFile
= url
.c_str();
472 sei
.lpVerb
= wxT("open");
473 sei
.nShow
= SW_SHOWNORMAL
;
474 sei
.fMask
= SEE_MASK_FLAG_NO_UI
; // we give error message ourselves
476 if ( ::ShellExecuteEx(&sei
) )