]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsgui.cpp
split AddCatalog docs for the two overloads
[wxWidgets.git] / src / msw / utilsgui.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/utilsgui.cpp
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
9 // License: wxWindows licence
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"
29 #include "wx/window.h"
30 #include "wx/utils.h"
31 #endif //WX_PRECOMP
32
33 #include "wx/dynlib.h"
34
35 #include "wx/msw/private.h" // includes <windows.h>
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // ---------------------------------------------------------------------------
42 // helper functions for showing a "busy" cursor
43 // ---------------------------------------------------------------------------
44
45 static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
46 static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
47 static int gs_wxBusyCursorCount = 0;
48
49 extern HCURSOR wxGetCurrentBusyCursor()
50 {
51 return gs_wxBusyCursor;
52 }
53
54 // Set the cursor to the busy cursor for all windows
55 void wxBeginBusyCursor(const wxCursor *cursor)
56 {
57 if ( gs_wxBusyCursorCount++ == 0 )
58 {
59 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
60 #ifndef __WXMICROWIN__
61 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
62 #endif
63 }
64 //else: nothing to do, already set
65 }
66
67 // Restore cursor to normal
68 void wxEndBusyCursor()
69 {
70 wxCHECK_RET( gs_wxBusyCursorCount > 0,
71 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
72
73 if ( --gs_wxBusyCursorCount == 0 )
74 {
75 #ifndef __WXMICROWIN__
76 ::SetCursor(gs_wxBusyCursorOld);
77 #endif
78 gs_wxBusyCursorOld = 0;
79 }
80 }
81
82 // true if we're between the above two calls
83 bool wxIsBusy()
84 {
85 return gs_wxBusyCursorCount > 0;
86 }
87
88 // Check whether this window wants to process messages, e.g. Stop button
89 // in long calculations.
90 bool wxCheckForInterrupt(wxWindow *wnd)
91 {
92 wxCHECK( wnd, false );
93
94 MSG msg;
95 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
96 {
97 ::TranslateMessage(&msg);
98 ::DispatchMessage(&msg);
99 }
100
101 return true;
102 }
103
104 // MSW only: get user-defined resource from the .res file.
105 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
106
107 #ifndef __WXMICROWIN__
108 wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
109 {
110 HRSRC hResource = ::FindResource(wxGetInstance(),
111 resourceName.wx_str(),
112 resourceType.wx_str());
113 if ( hResource == 0 )
114 return NULL;
115
116 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
117 if ( hData == 0 )
118 return NULL;
119
120 wxChar *theText = (wxChar *)::LockResource(hData);
121 if ( !theText )
122 return NULL;
123
124 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
125 // so we need to find the length of the resource.
126 int len = ::SizeofResource(wxGetInstance(), hResource) + 1;
127 wxChar *s = new wxChar[len];
128 wxStrlcpy(s, theText, len);
129
130 // Obsolete in WIN32
131 #ifndef __WIN32__
132 UnlockResource(hData);
133 #endif
134
135 // No need??
136 // GlobalFree(hData);
137
138 return s;
139 }
140 #endif // __WXMICROWIN__
141
142 // ----------------------------------------------------------------------------
143 // get display info
144 // ----------------------------------------------------------------------------
145
146 // See also the wxGetMousePosition in window.cpp
147 // Deprecated: use wxPoint wxGetMousePosition() instead
148 void wxGetMousePosition( int* x, int* y )
149 {
150 POINT pt;
151 GetCursorPos( & pt );
152 if ( x ) *x = pt.x;
153 if ( y ) *y = pt.y;
154 }
155
156 // Return true if we have a colour display
157 bool wxColourDisplay()
158 {
159 #ifdef __WXMICROWIN__
160 // MICROWIN_TODO
161 return true;
162 #else
163 // this function is called from wxDC ctor so it is called a *lot* of times
164 // hence we optimize it a bit but doing the check only once
165 //
166 // this should be MT safe as only the GUI thread (holding the GUI mutex)
167 // can call us
168 static int s_isColour = -1;
169
170 if ( s_isColour == -1 )
171 {
172 ScreenHDC dc;
173 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
174
175 s_isColour = (noCols == -1) || (noCols > 2);
176 }
177
178 return s_isColour != 0;
179 #endif
180 }
181
182 // Returns depth of screen
183 int wxDisplayDepth()
184 {
185 ScreenHDC dc;
186 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
187 }
188
189 // Get size of display
190 void wxDisplaySize(int *width, int *height)
191 {
192 #ifdef __WXMICROWIN__
193 RECT rect;
194 HWND hWnd = GetDesktopWindow();
195 ::GetWindowRect(hWnd, & rect);
196
197 if ( width )
198 *width = rect.right - rect.left;
199 if ( height )
200 *height = rect.bottom - rect.top;
201 #else // !__WXMICROWIN__
202 ScreenHDC dc;
203
204 if ( width )
205 *width = ::GetDeviceCaps(dc, HORZRES);
206 if ( height )
207 *height = ::GetDeviceCaps(dc, VERTRES);
208 #endif // __WXMICROWIN__/!__WXMICROWIN__
209 }
210
211 void wxDisplaySizeMM(int *width, int *height)
212 {
213 #ifdef __WXMICROWIN__
214 // MICROWIN_TODO
215 if ( width )
216 *width = 0;
217 if ( height )
218 *height = 0;
219 #else
220 ScreenHDC dc;
221
222 if ( width )
223 *width = ::GetDeviceCaps(dc, HORZSIZE);
224 if ( height )
225 *height = ::GetDeviceCaps(dc, VERTSIZE);
226 #endif
227 }
228
229 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
230 {
231 #if defined(__WXMICROWIN__)
232 *x = 0; *y = 0;
233 wxDisplaySize(width, height);
234 #else
235 // Determine the desktop dimensions minus the taskbar and any other
236 // special decorations...
237 RECT r;
238
239 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
240 if (x) *x = r.left;
241 if (y) *y = r.top;
242 if (width) *width = r.right - r.left;
243 if (height) *height = r.bottom - r.top;
244 #endif
245 }
246
247 // ---------------------------------------------------------------------------
248 // window information functions
249 // ---------------------------------------------------------------------------
250
251 wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
252 {
253 wxString str;
254
255 if ( hWnd )
256 {
257 int len = GetWindowTextLength((HWND)hWnd) + 1;
258 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
259 }
260
261 return str;
262 }
263
264 wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
265 {
266 wxString str;
267
268 // MICROWIN_TODO
269 #ifndef __WXMICROWIN__
270 if ( hWnd )
271 {
272 int len = 256; // some starting value
273
274 for ( ;; )
275 {
276 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
277
278 if ( count == len )
279 {
280 // the class name might have been truncated, retry with larger
281 // buffer
282 len *= 2;
283 }
284 else
285 {
286 break;
287 }
288 }
289 }
290 #endif // !__WXMICROWIN__
291
292 return str;
293 }
294
295 int WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
296 {
297 return ::GetWindowLong((HWND)hWnd, GWL_ID);
298 }
299
300 // ----------------------------------------------------------------------------
301 // Metafile helpers
302 // ----------------------------------------------------------------------------
303
304 extern void PixelToHIMETRIC(LONG *x, LONG *y)
305 {
306 ScreenHDC hdcRef;
307
308 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
309 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
310 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
311 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
312
313 *x *= (iWidthMM * 100);
314 *x /= iWidthPels;
315 *y *= (iHeightMM * 100);
316 *y /= iHeightPels;
317 }
318
319 extern void HIMETRICToPixel(LONG *x, LONG *y)
320 {
321 ScreenHDC hdcRef;
322
323 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
324 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
325 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
326 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
327
328 *x *= iWidthPels;
329 *x /= (iWidthMM * 100);
330 *y *= iHeightPels;
331 *y /= (iHeightMM * 100);
332 }
333
334 void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
335 {
336 #ifdef __WXWINCE__
337 POINT points[2];
338 points[0].x = x1;
339 points[0].y = y1;
340 points[1].x = x2;
341 points[1].y = y2;
342 Polyline(hdc, points, 2);
343 #else
344 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
345 #endif
346 }
347
348
349 // ----------------------------------------------------------------------------
350 // Shell API wrappers
351 // ----------------------------------------------------------------------------
352
353 extern bool wxEnableFileNameAutoComplete(HWND hwnd)
354 {
355 #if wxUSE_DYNLIB_CLASS
356 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
357
358 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
359 static bool s_initialized = false;
360
361 if ( !s_initialized )
362 {
363 s_initialized = true;
364
365 wxLogNull nolog;
366 wxDynamicLibrary dll(_T("shlwapi.dll"));
367 if ( dll.IsLoaded() )
368 {
369 s_pfnSHAutoComplete =
370 (SHAutoComplete_t)dll.GetSymbol(_T("SHAutoComplete"));
371 if ( s_pfnSHAutoComplete )
372 {
373 // won't be unloaded until the process termination, no big deal
374 dll.Detach();
375 }
376 }
377 }
378
379 if ( !s_pfnSHAutoComplete )
380 return false;
381
382 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
383 if ( FAILED(hr) )
384 {
385 wxLogApiError(_T("SHAutoComplete"), hr);
386 return false;
387 }
388
389 return true;
390 #else
391 wxUnusedVar(hwnd);
392 return false;
393 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
394 }
395