]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsgui.cpp
Add EnableHistory support to the OSX WebKit backend.
[wxWidgets.git] / src / msw / utilsgui.cpp
CommitLineData
e2478fde 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/utilsgui.cpp
e2478fde
VZ
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
526954c5 9// Licence: 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
e2478fde
VZ
107// ----------------------------------------------------------------------------
108// get display info
109// ----------------------------------------------------------------------------
110
111// See also the wxGetMousePosition in window.cpp
112// Deprecated: use wxPoint wxGetMousePosition() instead
113void wxGetMousePosition( int* x, int* y )
114{
115 POINT pt;
116 GetCursorPos( & pt );
117 if ( x ) *x = pt.x;
118 if ( y ) *y = pt.y;
259c43f6 119}
e2478fde 120
27d2dbbc 121// Return true if we have a colour display
e2478fde
VZ
122bool wxColourDisplay()
123{
124#ifdef __WXMICROWIN__
125 // MICROWIN_TODO
27d2dbbc 126 return true;
e2478fde
VZ
127#else
128 // this function is called from wxDC ctor so it is called a *lot* of times
3103e8a9 129 // hence we optimize it a bit but doing the check only once
e2478fde
VZ
130 //
131 // this should be MT safe as only the GUI thread (holding the GUI mutex)
132 // can call us
133 static int s_isColour = -1;
134
135 if ( s_isColour == -1 )
136 {
137 ScreenHDC dc;
138 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
139
140 s_isColour = (noCols == -1) || (noCols > 2);
141 }
142
143 return s_isColour != 0;
144#endif
145}
146
147// Returns depth of screen
148int wxDisplayDepth()
149{
150 ScreenHDC dc;
151 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
152}
153
154// Get size of display
155void wxDisplaySize(int *width, int *height)
156{
157#ifdef __WXMICROWIN__
158 RECT rect;
159 HWND hWnd = GetDesktopWindow();
160 ::GetWindowRect(hWnd, & rect);
161
162 if ( width )
163 *width = rect.right - rect.left;
164 if ( height )
165 *height = rect.bottom - rect.top;
166#else // !__WXMICROWIN__
167 ScreenHDC dc;
168
169 if ( width )
170 *width = ::GetDeviceCaps(dc, HORZRES);
171 if ( height )
172 *height = ::GetDeviceCaps(dc, VERTRES);
173#endif // __WXMICROWIN__/!__WXMICROWIN__
174}
175
176void wxDisplaySizeMM(int *width, int *height)
177{
178#ifdef __WXMICROWIN__
179 // MICROWIN_TODO
180 if ( width )
181 *width = 0;
182 if ( height )
183 *height = 0;
184#else
185 ScreenHDC dc;
186
187 if ( width )
188 *width = ::GetDeviceCaps(dc, HORZSIZE);
189 if ( height )
190 *height = ::GetDeviceCaps(dc, VERTSIZE);
191#endif
192}
193
194void wxClientDisplayRect(int *x, int *y, int *width, int *height)
195{
3a5bcc4d 196#if defined(__WXMICROWIN__)
e2478fde
VZ
197 *x = 0; *y = 0;
198 wxDisplaySize(width, height);
199#else
200 // Determine the desktop dimensions minus the taskbar and any other
201 // special decorations...
202 RECT r;
203
204 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
205 if (x) *x = r.left;
206 if (y) *y = r.top;
207 if (width) *width = r.right - r.left;
208 if (height) *height = r.bottom - r.top;
209#endif
210}
211
212// ---------------------------------------------------------------------------
213// window information functions
214// ---------------------------------------------------------------------------
215
216wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
217{
218 wxString str;
219
220 if ( hWnd )
221 {
222 int len = GetWindowTextLength((HWND)hWnd) + 1;
de564874 223 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
e2478fde
VZ
224 }
225
226 return str;
227}
228
229wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
230{
231 wxString str;
232
233 // MICROWIN_TODO
234#ifndef __WXMICROWIN__
235 if ( hWnd )
236 {
237 int len = 256; // some starting value
238
239 for ( ;; )
240 {
de564874 241 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
e2478fde 242
e2478fde
VZ
243 if ( count == len )
244 {
245 // the class name might have been truncated, retry with larger
246 // buffer
247 len *= 2;
248 }
249 else
250 {
251 break;
252 }
253 }
254 }
255#endif // !__WXMICROWIN__
256
257 return str;
258}
259
6756a5f8 260int WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
e2478fde 261{
6756a5f8 262 return ::GetWindowLong((HWND)hWnd, GWL_ID);
e2478fde
VZ
263}
264
265// ----------------------------------------------------------------------------
266// Metafile helpers
267// ----------------------------------------------------------------------------
268
cc344571 269void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef)
e2478fde 270{
e2478fde
VZ
271 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
272 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
273 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
274 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
275
276 *x *= (iWidthMM * 100);
277 *x /= iWidthPels;
278 *y *= (iHeightMM * 100);
279 *y /= iHeightPels;
280}
281
cc344571 282void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef)
e2478fde 283{
e2478fde
VZ
284 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
285 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
286 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
287 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
288
289 *x *= iWidthPels;
290 *x /= (iWidthMM * 100);
291 *y *= iHeightPels;
292 *y /= (iHeightMM * 100);
293}
294
cc344571
VS
295void HIMETRICToPixel(LONG *x, LONG *y)
296{
297 HIMETRICToPixel(x, y, ScreenHDC());
298}
299
300void PixelToHIMETRIC(LONG *x, LONG *y)
301{
302 PixelToHIMETRIC(x, y, ScreenHDC());
303}
304
4676948b
JS
305void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
306{
307#ifdef __WXWINCE__
308 POINT points[2];
309 points[0].x = x1;
310 points[0].y = y1;
311 points[1].x = x2;
312 points[1].y = y2;
313 Polyline(hdc, points, 2);
314#else
315 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
316#endif
317}
318
e2478fde 319
5f6475c1
VZ
320// ----------------------------------------------------------------------------
321// Shell API wrappers
322// ----------------------------------------------------------------------------
323
324extern bool wxEnableFileNameAutoComplete(HWND hwnd)
325{
40e0cf48 326#if wxUSE_DYNLIB_CLASS
5f6475c1
VZ
327 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
328
329 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
330 static bool s_initialized = false;
331
332 if ( !s_initialized )
333 {
334 s_initialized = true;
335
336 wxLogNull nolog;
9a83f860 337 wxDynamicLibrary dll(wxT("shlwapi.dll"));
5f6475c1
VZ
338 if ( dll.IsLoaded() )
339 {
340 s_pfnSHAutoComplete =
9a83f860 341 (SHAutoComplete_t)dll.GetSymbol(wxT("SHAutoComplete"));
5f6475c1
VZ
342 if ( s_pfnSHAutoComplete )
343 {
344 // won't be unloaded until the process termination, no big deal
345 dll.Detach();
346 }
347 }
348 }
349
350 if ( !s_pfnSHAutoComplete )
351 return false;
352
353 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
354 if ( FAILED(hr) )
355 {
9a83f860 356 wxLogApiError(wxT("SHAutoComplete"), hr);
5f6475c1
VZ
357 return false;
358 }
359
360 return true;
40e0cf48
VZ
361#else
362 wxUnusedVar(hwnd);
363 return false;
364#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
5f6475c1
VZ
365}
366
50a2e26f
FM
367// ----------------------------------------------------------------------------
368// Launch document with default app
369// ----------------------------------------------------------------------------
370
371bool wxLaunchDefaultApplication(const wxString& document, int flags)
372{
373 wxUnusedVar(flags);
374
375 WinStruct<SHELLEXECUTEINFO> sei;
376 sei.lpFile = document.wx_str();
50a2e26f
FM
377#ifdef __WXWINCE__
378 sei.nShow = SW_SHOWNORMAL; // SW_SHOWDEFAULT not defined under CE (#10216)
379#else
380 sei.nShow = SW_SHOWDEFAULT;
381#endif
382
383 // avoid Windows message box in case of error for consistency with
384 // wxLaunchDefaultBrowser() even if don't show the error ourselves in this
385 // function
386 sei.fMask = SEE_MASK_FLAG_NO_UI;
387
388 if ( ::ShellExecuteEx(&sei) )
389 return true;
390
391 return false;
392}
393
394// ----------------------------------------------------------------------------
395// Launch default browser
396// ----------------------------------------------------------------------------
397
398bool wxDoLaunchDefaultBrowser(const wxString& url, const wxString& scheme, int flags)
399{
400 wxUnusedVar(flags);
03647350 401
50a2e26f
FM
402#if wxUSE_IPC
403 if ( flags & wxBROWSER_NEW_WINDOW )
404 {
405 // ShellExecuteEx() opens the URL in an existing window by default so
406 // we can't use it if we need a new window
9a83f860 407 wxRegKey key(wxRegKey::HKCR, scheme + wxT("\\shell\\open"));
50a2e26f
FM
408 if ( !key.Exists() )
409 {
410 // try the default browser, it must be registered at least for http URLs
9a83f860 411 key.SetName(wxRegKey::HKCR, wxT("http\\shell\\open"));
50a2e26f
FM
412 }
413
414 if ( key.Exists() )
415 {
416 wxRegKey keyDDE(key, wxT("DDEExec"));
417 if ( keyDDE.Exists() )
418 {
419 // we only know the syntax of WWW_OpenURL DDE request for IE,
420 // optimistically assume that all other browsers are compatible
421 // with it
422 static const wxChar *TOPIC_OPEN_URL = wxT("WWW_OpenURL");
423 wxString ddeCmd;
424 wxRegKey keyTopic(keyDDE, wxT("topic"));
425 bool ok = keyTopic.Exists() &&
426 keyTopic.QueryDefaultValue() == TOPIC_OPEN_URL;
427 if ( ok )
428 {
429 ddeCmd = keyDDE.QueryDefaultValue();
430 ok = !ddeCmd.empty();
431 }
432
433 if ( ok )
434 {
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;
440 }
441
442 if ( ok )
443 {
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;
447 }
448
449 if ( ok )
450 {
451 // try to send it the DDE request now but ignore the errors
452 wxLogNull noLog;
453
454 const wxString ddeServer = wxRegKey(keyDDE, wxT("application"));
455 if ( wxExecuteDDE(ddeServer, TOPIC_OPEN_URL, ddeCmd) )
456 return true;
457
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
462 // browser anyhow
463 }
464 }
465 }
466 }
467#endif // wxUSE_IPC
468
469 WinStruct<SHELLEXECUTEINFO> sei;
470 sei.lpFile = url.c_str();
9a83f860 471 sei.lpVerb = wxT("open");
50a2e26f
FM
472 sei.nShow = SW_SHOWNORMAL;
473 sei.fMask = SEE_MASK_FLAG_NO_UI; // we give error message ourselves
474
475 if ( ::ShellExecuteEx(&sei) )
476 return true;
477
478 return false;
479}