don't use obsolete functions (mostly copystring() and Count()), remove their document...
[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(), resourceName, resourceType);
111 if ( hResource == 0 )
112 return NULL;
113
114 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
115 if ( hData == 0 )
116 return NULL;
117
118 wxChar *theText = (wxChar *)::LockResource(hData);
119 if ( !theText )
120 return NULL;
121
122 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
123 // so we need to find the length of the resource.
124 int len = ::SizeofResource(wxGetInstance(), hResource);
125 wxChar *s = new wxChar[len+1];
126 wxStrncpy(s,theText,len);
127 s[len]=0;
128
129 // Obsolete in WIN32
130 #ifndef __WIN32__
131 UnlockResource(hData);
132 #endif
133
134 // No need??
135 // GlobalFree(hData);
136
137 return s;
138 }
139 #endif // __WXMICROWIN__
140
141 // ----------------------------------------------------------------------------
142 // get display info
143 // ----------------------------------------------------------------------------
144
145 // See also the wxGetMousePosition in window.cpp
146 // Deprecated: use wxPoint wxGetMousePosition() instead
147 void wxGetMousePosition( int* x, int* y )
148 {
149 POINT pt;
150 GetCursorPos( & pt );
151 if ( x ) *x = pt.x;
152 if ( y ) *y = pt.y;
153 }
154
155 // Return true if we have a colour display
156 bool wxColourDisplay()
157 {
158 #ifdef __WXMICROWIN__
159 // MICROWIN_TODO
160 return true;
161 #else
162 // this function is called from wxDC ctor so it is called a *lot* of times
163 // hence we optimize it a bit but doing the check only once
164 //
165 // this should be MT safe as only the GUI thread (holding the GUI mutex)
166 // can call us
167 static int s_isColour = -1;
168
169 if ( s_isColour == -1 )
170 {
171 ScreenHDC dc;
172 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
173
174 s_isColour = (noCols == -1) || (noCols > 2);
175 }
176
177 return s_isColour != 0;
178 #endif
179 }
180
181 // Returns depth of screen
182 int wxDisplayDepth()
183 {
184 ScreenHDC dc;
185 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
186 }
187
188 // Get size of display
189 void wxDisplaySize(int *width, int *height)
190 {
191 #ifdef __WXMICROWIN__
192 RECT rect;
193 HWND hWnd = GetDesktopWindow();
194 ::GetWindowRect(hWnd, & rect);
195
196 if ( width )
197 *width = rect.right - rect.left;
198 if ( height )
199 *height = rect.bottom - rect.top;
200 #else // !__WXMICROWIN__
201 ScreenHDC dc;
202
203 if ( width )
204 *width = ::GetDeviceCaps(dc, HORZRES);
205 if ( height )
206 *height = ::GetDeviceCaps(dc, VERTRES);
207 #endif // __WXMICROWIN__/!__WXMICROWIN__
208 }
209
210 void wxDisplaySizeMM(int *width, int *height)
211 {
212 #ifdef __WXMICROWIN__
213 // MICROWIN_TODO
214 if ( width )
215 *width = 0;
216 if ( height )
217 *height = 0;
218 #else
219 ScreenHDC dc;
220
221 if ( width )
222 *width = ::GetDeviceCaps(dc, HORZSIZE);
223 if ( height )
224 *height = ::GetDeviceCaps(dc, VERTSIZE);
225 #endif
226 }
227
228 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
229 {
230 #if defined(__WXMICROWIN__)
231 *x = 0; *y = 0;
232 wxDisplaySize(width, height);
233 #else
234 // Determine the desktop dimensions minus the taskbar and any other
235 // special decorations...
236 RECT r;
237
238 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
239 if (x) *x = r.left;
240 if (y) *y = r.top;
241 if (width) *width = r.right - r.left;
242 if (height) *height = r.bottom - r.top;
243 #endif
244 }
245
246 // ---------------------------------------------------------------------------
247 // window information functions
248 // ---------------------------------------------------------------------------
249
250 wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
251 {
252 wxString str;
253
254 if ( hWnd )
255 {
256 int len = GetWindowTextLength((HWND)hWnd) + 1;
257 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
258 }
259
260 return str;
261 }
262
263 wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
264 {
265 wxString str;
266
267 // MICROWIN_TODO
268 #ifndef __WXMICROWIN__
269 if ( hWnd )
270 {
271 int len = 256; // some starting value
272
273 for ( ;; )
274 {
275 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
276
277 if ( count == len )
278 {
279 // the class name might have been truncated, retry with larger
280 // buffer
281 len *= 2;
282 }
283 else
284 {
285 break;
286 }
287 }
288 }
289 #endif // !__WXMICROWIN__
290
291 return str;
292 }
293
294 WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
295 {
296 return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID);
297 }
298
299 // ----------------------------------------------------------------------------
300 // Metafile helpers
301 // ----------------------------------------------------------------------------
302
303 extern void PixelToHIMETRIC(LONG *x, LONG *y)
304 {
305 ScreenHDC hdcRef;
306
307 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
308 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
309 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
310 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
311
312 *x *= (iWidthMM * 100);
313 *x /= iWidthPels;
314 *y *= (iHeightMM * 100);
315 *y /= iHeightPels;
316 }
317
318 extern void HIMETRICToPixel(LONG *x, LONG *y)
319 {
320 ScreenHDC hdcRef;
321
322 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
323 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
324 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
325 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
326
327 *x *= iWidthPels;
328 *x /= (iWidthMM * 100);
329 *y *= iHeightPels;
330 *y /= (iHeightMM * 100);
331 }
332
333 void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
334 {
335 #ifdef __WXWINCE__
336 POINT points[2];
337 points[0].x = x1;
338 points[0].y = y1;
339 points[1].x = x2;
340 points[1].y = y2;
341 Polyline(hdc, points, 2);
342 #else
343 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
344 #endif
345 }
346
347
348 // ----------------------------------------------------------------------------
349 // Shell API wrappers
350 // ----------------------------------------------------------------------------
351
352 extern bool wxEnableFileNameAutoComplete(HWND hwnd)
353 {
354 #if wxUSE_DYNLIB_CLASS
355 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
356
357 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
358 static bool s_initialized = false;
359
360 if ( !s_initialized )
361 {
362 s_initialized = true;
363
364 wxLogNull nolog;
365 wxDynamicLibrary dll(_T("shlwapi.dll"));
366 if ( dll.IsLoaded() )
367 {
368 s_pfnSHAutoComplete =
369 (SHAutoComplete_t)dll.GetSymbol(_T("SHAutoComplete"));
370 if ( s_pfnSHAutoComplete )
371 {
372 // won't be unloaded until the process termination, no big deal
373 dll.Detach();
374 }
375 }
376 }
377
378 if ( !s_pfnSHAutoComplete )
379 return false;
380
381 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
382 if ( FAILED(hr) )
383 {
384 wxLogApiError(_T("SHAutoComplete"), hr);
385 return false;
386 }
387
388 return true;
389 #else
390 wxUnusedVar(hwnd);
391 return false;
392 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
393 }
394