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