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 // License: 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 // MSW only: get user-defined resource from the .res file.
108 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
110 #ifndef __WXMICROWIN__
111 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
113 HRSRC hResource
= ::FindResource(wxGetInstance(),
114 resourceName
.wx_str(),
115 resourceType
.wx_str());
116 if ( hResource
== 0 )
119 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
123 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
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
) + 1;
130 wxChar
*s
= new wxChar
[len
];
131 wxStrlcpy(s
, theText
, len
);
135 UnlockResource(hData
);
139 // GlobalFree(hData);
143 #endif // __WXMICROWIN__
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 // See also the wxGetMousePosition in window.cpp
150 // Deprecated: use wxPoint wxGetMousePosition() instead
151 void wxGetMousePosition( int* x
, int* y
)
154 GetCursorPos( & pt
);
159 // Return true if we have a colour display
160 bool wxColourDisplay()
162 #ifdef __WXMICROWIN__
166 // this function is called from wxDC ctor so it is called a *lot* of times
167 // hence we optimize it a bit but doing the check only once
169 // this should be MT safe as only the GUI thread (holding the GUI mutex)
171 static int s_isColour
= -1;
173 if ( s_isColour
== -1 )
176 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
178 s_isColour
= (noCols
== -1) || (noCols
> 2);
181 return s_isColour
!= 0;
185 // Returns depth of screen
189 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
192 // Get size of display
193 void wxDisplaySize(int *width
, int *height
)
195 #ifdef __WXMICROWIN__
197 HWND hWnd
= GetDesktopWindow();
198 ::GetWindowRect(hWnd
, & rect
);
201 *width
= rect
.right
- rect
.left
;
203 *height
= rect
.bottom
- rect
.top
;
204 #else // !__WXMICROWIN__
208 *width
= ::GetDeviceCaps(dc
, HORZRES
);
210 *height
= ::GetDeviceCaps(dc
, VERTRES
);
211 #endif // __WXMICROWIN__/!__WXMICROWIN__
214 void wxDisplaySizeMM(int *width
, int *height
)
216 #ifdef __WXMICROWIN__
226 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
228 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
232 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
234 #if defined(__WXMICROWIN__)
236 wxDisplaySize(width
, height
);
238 // Determine the desktop dimensions minus the taskbar and any other
239 // special decorations...
242 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
245 if (width
) *width
= r
.right
- r
.left
;
246 if (height
) *height
= r
.bottom
- r
.top
;
250 // ---------------------------------------------------------------------------
251 // window information functions
252 // ---------------------------------------------------------------------------
254 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
260 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
261 ::GetWindowText((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
267 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
272 #ifndef __WXMICROWIN__
275 int len
= 256; // some starting value
279 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
283 // the class name might have been truncated, retry with larger
293 #endif // !__WXMICROWIN__
298 int WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
300 return ::GetWindowLong((HWND
)hWnd
, GWL_ID
);
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 void PixelToHIMETRIC(LONG
*x
, LONG
*y
, HDC hdcRef
)
309 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
310 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
311 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
312 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
314 *x
*= (iWidthMM
* 100);
316 *y
*= (iHeightMM
* 100);
320 void HIMETRICToPixel(LONG
*x
, LONG
*y
, HDC hdcRef
)
322 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
323 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
324 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
325 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
328 *x
/= (iWidthMM
* 100);
330 *y
/= (iHeightMM
* 100);
333 void HIMETRICToPixel(LONG
*x
, LONG
*y
)
335 HIMETRICToPixel(x
, y
, ScreenHDC());
338 void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
340 PixelToHIMETRIC(x
, y
, ScreenHDC());
343 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
351 Polyline(hdc
, points
, 2);
353 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);
358 // ----------------------------------------------------------------------------
359 // Shell API wrappers
360 // ----------------------------------------------------------------------------
362 extern bool wxEnableFileNameAutoComplete(HWND hwnd
)
364 #if wxUSE_DYNLIB_CLASS
365 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
367 static SHAutoComplete_t s_pfnSHAutoComplete
= NULL
;
368 static bool s_initialized
= false;
370 if ( !s_initialized
)
372 s_initialized
= true;
375 wxDynamicLibrary
dll(wxT("shlwapi.dll"));
376 if ( dll
.IsLoaded() )
378 s_pfnSHAutoComplete
=
379 (SHAutoComplete_t
)dll
.GetSymbol(wxT("SHAutoComplete"));
380 if ( s_pfnSHAutoComplete
)
382 // won't be unloaded until the process termination, no big deal
388 if ( !s_pfnSHAutoComplete
)
391 HRESULT hr
= s_pfnSHAutoComplete(hwnd
, 0x10 /* SHACF_FILESYS_ONLY */);
394 wxLogApiError(wxT("SHAutoComplete"), hr
);
402 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
405 // ----------------------------------------------------------------------------
406 // Launch document with default app
407 // ----------------------------------------------------------------------------
409 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
413 WinStruct
<SHELLEXECUTEINFO
> sei
;
414 sei
.lpFile
= document
.wx_str();
415 sei
.lpVerb
= wxT("open");
417 sei
.nShow
= SW_SHOWNORMAL
; // SW_SHOWDEFAULT not defined under CE (#10216)
419 sei
.nShow
= SW_SHOWDEFAULT
;
422 // avoid Windows message box in case of error for consistency with
423 // wxLaunchDefaultBrowser() even if don't show the error ourselves in this
425 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
427 if ( ::ShellExecuteEx(&sei
) )
433 // ----------------------------------------------------------------------------
434 // Launch default browser
435 // ----------------------------------------------------------------------------
437 bool wxDoLaunchDefaultBrowser(const wxString
& url
, const wxString
& scheme
, int flags
)
442 if ( flags
& wxBROWSER_NEW_WINDOW
)
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
+ wxT("\\shell\\open"));
449 // try the default browser, it must be registered at least for http URLs
450 key
.SetName(wxRegKey::HKCR
, wxT("http\\shell\\open"));
455 wxRegKey
keyDDE(key
, wxT("DDEExec"));
456 if ( keyDDE
.Exists() )
458 // we only know the syntax of WWW_OpenURL DDE request for IE,
459 // optimistically assume that all other browsers are compatible
461 static const wxChar
*TOPIC_OPEN_URL
= wxT("WWW_OpenURL");
463 wxRegKey
keyTopic(keyDDE
, wxT("topic"));
464 bool ok
= keyTopic
.Exists() &&
465 keyTopic
.QueryDefaultValue() == TOPIC_OPEN_URL
;
468 ddeCmd
= keyDDE
.QueryDefaultValue();
469 ok
= !ddeCmd
.empty();
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;
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;
490 // try to send it the DDE request now but ignore the errors
493 const wxString ddeServer
= wxRegKey(keyDDE
, wxT("application"));
494 if ( wxExecuteDDE(ddeServer
, TOPIC_OPEN_URL
, ddeCmd
) )
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
508 WinStruct
<SHELLEXECUTEINFO
> sei
;
509 sei
.lpFile
= url
.c_str();
510 sei
.lpVerb
= wxT("open");
511 sei
.nShow
= SW_SHOWNORMAL
;
512 sei
.fMask
= SEE_MASK_FLAG_NO_UI
; // we give error message ourselves
514 if ( ::ShellExecuteEx(&sei
) )