]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsgui.cpp
Add wxAnyScrollHelperBase to reduce code duplication in wxVarScrollHelperBase.
[wxWidgets.git] / src / msw / utilsgui.cpp
CommitLineData
e2478fde 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/utilsgui.cpp
5146904d 3// Purpose: Various utility functions only available in wxMSW GUI
e2478fde
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 21.06.2003 (extracted from msw/utils.cpp)
e2478fde 7// Copyright: (c) Julian Smart
526954c5 8// Licence: wxWindows licence
e2478fde
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27 #include "wx/cursor.h"
42d54220 28 #include "wx/window.h"
e2478fde
VZ
29 #include "wx/utils.h"
30#endif //WX_PRECOMP
31
5f6475c1
VZ
32#include "wx/dynlib.h"
33
e2478fde
VZ
34#include "wx/msw/private.h" // includes <windows.h>
35
36// ============================================================================
37// implementation
38// ============================================================================
39
598fe99d
VZ
40// Emit a beeeeeep
41void wxBell()
42{
43 ::MessageBeep((UINT)-1); // default sound
44}
45
e2478fde
VZ
46// ---------------------------------------------------------------------------
47// helper functions for showing a "busy" cursor
48// ---------------------------------------------------------------------------
49
50static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
51static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
52static int gs_wxBusyCursorCount = 0;
53
54extern HCURSOR wxGetCurrentBusyCursor()
55{
56 return gs_wxBusyCursor;
57}
58
59// Set the cursor to the busy cursor for all windows
f516d986 60void wxBeginBusyCursor(const wxCursor *cursor)
e2478fde
VZ
61{
62 if ( gs_wxBusyCursorCount++ == 0 )
63 {
64 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
65#ifndef __WXMICROWIN__
66 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
67#endif
68 }
69 //else: nothing to do, already set
70}
71
72// Restore cursor to normal
73void wxEndBusyCursor()
74{
75 wxCHECK_RET( gs_wxBusyCursorCount > 0,
76 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
77
78 if ( --gs_wxBusyCursorCount == 0 )
79 {
80#ifndef __WXMICROWIN__
81 ::SetCursor(gs_wxBusyCursorOld);
82#endif
83 gs_wxBusyCursorOld = 0;
84 }
85}
86
27d2dbbc 87// true if we're between the above two calls
e2478fde
VZ
88bool wxIsBusy()
89{
90 return gs_wxBusyCursorCount > 0;
91}
92
93// Check whether this window wants to process messages, e.g. Stop button
94// in long calculations.
95bool wxCheckForInterrupt(wxWindow *wnd)
96{
27d2dbbc 97 wxCHECK( wnd, false );
e2478fde
VZ
98
99 MSG msg;
100 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
101 {
102 ::TranslateMessage(&msg);
103 ::DispatchMessage(&msg);
104 }
105
27d2dbbc 106 return true;
e2478fde
VZ
107}
108
e2478fde
VZ
109// ----------------------------------------------------------------------------
110// get display info
111// ----------------------------------------------------------------------------
112
113// See also the wxGetMousePosition in window.cpp
114// Deprecated: use wxPoint wxGetMousePosition() instead
115void wxGetMousePosition( int* x, int* y )
116{
117 POINT pt;
d6c37f5b 118 wxGetCursorPosMSW( & pt );
e2478fde
VZ
119 if ( x ) *x = pt.x;
120 if ( y ) *y = pt.y;
259c43f6 121}
e2478fde 122
27d2dbbc 123// Return true if we have a colour display
e2478fde
VZ
124bool wxColourDisplay()
125{
126#ifdef __WXMICROWIN__
127 // MICROWIN_TODO
27d2dbbc 128 return true;
e2478fde
VZ
129#else
130 // this function is called from wxDC ctor so it is called a *lot* of times
3103e8a9 131 // hence we optimize it a bit but doing the check only once
e2478fde
VZ
132 //
133 // this should be MT safe as only the GUI thread (holding the GUI mutex)
134 // can call us
135 static int s_isColour = -1;
136
137 if ( s_isColour == -1 )
138 {
139 ScreenHDC dc;
140 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
141
142 s_isColour = (noCols == -1) || (noCols > 2);
143 }
144
145 return s_isColour != 0;
146#endif
147}
148
149// Returns depth of screen
150int wxDisplayDepth()
151{
152 ScreenHDC dc;
153 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
154}
155
156// Get size of display
157void wxDisplaySize(int *width, int *height)
158{
159#ifdef __WXMICROWIN__
160 RECT rect;
161 HWND hWnd = GetDesktopWindow();
162 ::GetWindowRect(hWnd, & rect);
163
164 if ( width )
165 *width = rect.right - rect.left;
166 if ( height )
167 *height = rect.bottom - rect.top;
168#else // !__WXMICROWIN__
169 ScreenHDC dc;
170
171 if ( width )
172 *width = ::GetDeviceCaps(dc, HORZRES);
173 if ( height )
174 *height = ::GetDeviceCaps(dc, VERTRES);
175#endif // __WXMICROWIN__/!__WXMICROWIN__
176}
177
178void wxDisplaySizeMM(int *width, int *height)
179{
180#ifdef __WXMICROWIN__
181 // MICROWIN_TODO
182 if ( width )
183 *width = 0;
184 if ( height )
185 *height = 0;
186#else
187 ScreenHDC dc;
188
189 if ( width )
190 *width = ::GetDeviceCaps(dc, HORZSIZE);
191 if ( height )
192 *height = ::GetDeviceCaps(dc, VERTSIZE);
193#endif
194}
195
e2478fde
VZ
196// ---------------------------------------------------------------------------
197// window information functions
198// ---------------------------------------------------------------------------
199
200wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
201{
202 wxString str;
203
204 if ( hWnd )
205 {
206 int len = GetWindowTextLength((HWND)hWnd) + 1;
de564874 207 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
e2478fde
VZ
208 }
209
210 return str;
211}
212
213wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
214{
215 wxString str;
216
217 // MICROWIN_TODO
218#ifndef __WXMICROWIN__
219 if ( hWnd )
220 {
221 int len = 256; // some starting value
222
223 for ( ;; )
224 {
de564874 225 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
e2478fde 226
e2478fde
VZ
227 if ( count == len )
228 {
229 // the class name might have been truncated, retry with larger
230 // buffer
231 len *= 2;
232 }
233 else
234 {
235 break;
236 }
237 }
238 }
239#endif // !__WXMICROWIN__
240
241 return str;
242}
243
6756a5f8 244int WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
e2478fde 245{
6756a5f8 246 return ::GetWindowLong((HWND)hWnd, GWL_ID);
e2478fde
VZ
247}
248
249// ----------------------------------------------------------------------------
250// Metafile helpers
251// ----------------------------------------------------------------------------
252
cc344571 253void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef)
e2478fde 254{
e2478fde
VZ
255 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
256 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
257 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
258 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
259
260 *x *= (iWidthMM * 100);
261 *x /= iWidthPels;
262 *y *= (iHeightMM * 100);
263 *y /= iHeightPels;
264}
265
cc344571 266void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef)
e2478fde 267{
e2478fde
VZ
268 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
269 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
270 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
271 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
272
273 *x *= iWidthPels;
274 *x /= (iWidthMM * 100);
275 *y *= iHeightPels;
276 *y /= (iHeightMM * 100);
277}
278
cc344571
VS
279void HIMETRICToPixel(LONG *x, LONG *y)
280{
281 HIMETRICToPixel(x, y, ScreenHDC());
282}
283
284void PixelToHIMETRIC(LONG *x, LONG *y)
285{
286 PixelToHIMETRIC(x, y, ScreenHDC());
287}
288
4676948b
JS
289void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
290{
291#ifdef __WXWINCE__
292 POINT points[2];
293 points[0].x = x1;
294 points[0].y = y1;
295 points[1].x = x2;
296 points[1].y = y2;
297 Polyline(hdc, points, 2);
298#else
299 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
300#endif
301}
302
e2478fde 303
5f6475c1
VZ
304// ----------------------------------------------------------------------------
305// Shell API wrappers
306// ----------------------------------------------------------------------------
307
308extern bool wxEnableFileNameAutoComplete(HWND hwnd)
309{
40e0cf48 310#if wxUSE_DYNLIB_CLASS
5f6475c1
VZ
311 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
312
313 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
314 static bool s_initialized = false;
315
316 if ( !s_initialized )
317 {
318 s_initialized = true;
319
320 wxLogNull nolog;
9a83f860 321 wxDynamicLibrary dll(wxT("shlwapi.dll"));
5f6475c1
VZ
322 if ( dll.IsLoaded() )
323 {
324 s_pfnSHAutoComplete =
9a83f860 325 (SHAutoComplete_t)dll.GetSymbol(wxT("SHAutoComplete"));
5f6475c1
VZ
326 if ( s_pfnSHAutoComplete )
327 {
328 // won't be unloaded until the process termination, no big deal
329 dll.Detach();
330 }
331 }
332 }
333
334 if ( !s_pfnSHAutoComplete )
335 return false;
336
337 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
338 if ( FAILED(hr) )
339 {
9a83f860 340 wxLogApiError(wxT("SHAutoComplete"), hr);
5f6475c1
VZ
341 return false;
342 }
343
344 return true;
40e0cf48
VZ
345#else
346 wxUnusedVar(hwnd);
347 return false;
348#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
5f6475c1 349}