]>
Commit | Line | Data |
---|---|---|
e2478fde VZ |
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 | |
65571936 | 9 | // License: 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 VZ |
35 | #include "wx/msw/private.h" // includes <windows.h> |
36 | ||
37 | // ============================================================================ | |
38 | // implementation | |
39 | // ============================================================================ | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // functions to work with .INI files | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // Reading and writing resources (eg WIN.INI, .Xdefaults) | |
46 | #if wxUSE_RESOURCES | |
47 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) | |
48 | { | |
fda7962d | 49 | if (file != wxEmptyString) |
e2478fde VZ |
50 | // Don't know what the correct cast should be, but it doesn't |
51 | // compile in BC++/16-bit without this cast. | |
52 | #if !defined(__WIN32__) | |
53 | return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0); | |
54 | #else | |
55 | return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0); | |
56 | #endif | |
57 | else | |
58 | return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0); | |
59 | } | |
60 | ||
61 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
62 | { | |
63 | wxString buf; | |
64 | buf.Printf(wxT("%.4f"), value); | |
65 | ||
66 | return wxWriteResource(section, entry, buf, file); | |
67 | } | |
68 | ||
69 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
70 | { | |
71 | wxString buf; | |
72 | buf.Printf(wxT("%ld"), value); | |
73 | ||
74 | return wxWriteResource(section, entry, buf, file); | |
75 | } | |
76 | ||
77 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
78 | { | |
79 | wxString buf; | |
80 | buf.Printf(wxT("%d"), value); | |
81 | ||
82 | return wxWriteResource(section, entry, buf, file); | |
83 | } | |
84 | ||
85 | bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file) | |
86 | { | |
87 | static const wxChar defunkt[] = wxT("$$default"); | |
3e3be693 VZ |
88 | |
89 | wxChar buf[1024]; | |
fda7962d | 90 | if (file != wxEmptyString) |
e2478fde | 91 | { |
3e3be693 VZ |
92 | int n = GetPrivateProfileString(section, entry, defunkt, |
93 | buf, WXSIZEOF(buf), file); | |
94 | if (n == 0 || wxStrcmp(buf, defunkt) == 0) | |
27d2dbbc | 95 | return false; |
e2478fde VZ |
96 | } |
97 | else | |
98 | { | |
3e3be693 VZ |
99 | int n = GetProfileString(section, entry, defunkt, buf, WXSIZEOF(buf)); |
100 | if (n == 0 || wxStrcmp(buf, defunkt) == 0) | |
27d2dbbc | 101 | return false; |
e2478fde VZ |
102 | } |
103 | if (*value) delete[] (*value); | |
de564874 | 104 | *value = wxStrcpy(new wxChar[wxStrlen(buf) + 1], buf); |
27d2dbbc | 105 | return true; |
e2478fde VZ |
106 | } |
107 | ||
108 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
109 | { | |
110 | wxChar *s = NULL; | |
111 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
112 | if (succ) | |
113 | { | |
114 | *value = (float)wxStrtod(s, NULL); | |
115 | delete[] s; | |
27d2dbbc | 116 | return true; |
e2478fde | 117 | } |
27d2dbbc | 118 | else return false; |
e2478fde VZ |
119 | } |
120 | ||
121 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
122 | { | |
123 | wxChar *s = NULL; | |
124 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
125 | if (succ) | |
126 | { | |
127 | *value = wxStrtol(s, NULL, 10); | |
128 | delete[] s; | |
27d2dbbc | 129 | return true; |
e2478fde | 130 | } |
27d2dbbc | 131 | else return false; |
e2478fde VZ |
132 | } |
133 | ||
134 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
135 | { | |
136 | wxChar *s = NULL; | |
137 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
138 | if (succ) | |
139 | { | |
140 | *value = (int)wxStrtol(s, NULL, 10); | |
141 | delete[] s; | |
27d2dbbc | 142 | return true; |
e2478fde | 143 | } |
27d2dbbc | 144 | else return false; |
e2478fde VZ |
145 | } |
146 | #endif // wxUSE_RESOURCES | |
147 | ||
148 | // --------------------------------------------------------------------------- | |
149 | // helper functions for showing a "busy" cursor | |
150 | // --------------------------------------------------------------------------- | |
151 | ||
152 | static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor | |
153 | static HCURSOR gs_wxBusyCursorOld = 0; // old cursor | |
154 | static int gs_wxBusyCursorCount = 0; | |
155 | ||
156 | extern HCURSOR wxGetCurrentBusyCursor() | |
157 | { | |
158 | return gs_wxBusyCursor; | |
159 | } | |
160 | ||
161 | // Set the cursor to the busy cursor for all windows | |
f516d986 | 162 | void wxBeginBusyCursor(const wxCursor *cursor) |
e2478fde VZ |
163 | { |
164 | if ( gs_wxBusyCursorCount++ == 0 ) | |
165 | { | |
166 | gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR(); | |
167 | #ifndef __WXMICROWIN__ | |
168 | gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor); | |
169 | #endif | |
170 | } | |
171 | //else: nothing to do, already set | |
172 | } | |
173 | ||
174 | // Restore cursor to normal | |
175 | void wxEndBusyCursor() | |
176 | { | |
177 | wxCHECK_RET( gs_wxBusyCursorCount > 0, | |
178 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); | |
179 | ||
180 | if ( --gs_wxBusyCursorCount == 0 ) | |
181 | { | |
182 | #ifndef __WXMICROWIN__ | |
183 | ::SetCursor(gs_wxBusyCursorOld); | |
184 | #endif | |
185 | gs_wxBusyCursorOld = 0; | |
186 | } | |
187 | } | |
188 | ||
27d2dbbc | 189 | // true if we're between the above two calls |
e2478fde VZ |
190 | bool wxIsBusy() |
191 | { | |
192 | return gs_wxBusyCursorCount > 0; | |
193 | } | |
194 | ||
195 | // Check whether this window wants to process messages, e.g. Stop button | |
196 | // in long calculations. | |
197 | bool wxCheckForInterrupt(wxWindow *wnd) | |
198 | { | |
27d2dbbc | 199 | wxCHECK( wnd, false ); |
e2478fde VZ |
200 | |
201 | MSG msg; | |
202 | while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) ) | |
203 | { | |
204 | ::TranslateMessage(&msg); | |
205 | ::DispatchMessage(&msg); | |
206 | } | |
207 | ||
27d2dbbc | 208 | return true; |
e2478fde VZ |
209 | } |
210 | ||
211 | // MSW only: get user-defined resource from the .res file. | |
212 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
213 | ||
214 | #ifndef __WXMICROWIN__ | |
215 | wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType) | |
216 | { | |
217 | HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType); | |
218 | if ( hResource == 0 ) | |
219 | return NULL; | |
220 | ||
221 | HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); | |
222 | if ( hData == 0 ) | |
223 | return NULL; | |
224 | ||
225 | wxChar *theText = (wxChar *)::LockResource(hData); | |
226 | if ( !theText ) | |
227 | return NULL; | |
228 | ||
229 | // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). | |
230 | // so we need to find the length of the resource. | |
231 | int len = ::SizeofResource(wxGetInstance(), hResource); | |
232 | wxChar *s = new wxChar[len+1]; | |
233 | wxStrncpy(s,theText,len); | |
234 | s[len]=0; | |
235 | ||
236 | // wxChar *s = copystring(theText); | |
237 | ||
238 | // Obsolete in WIN32 | |
239 | #ifndef __WIN32__ | |
240 | UnlockResource(hData); | |
241 | #endif | |
242 | ||
243 | // No need?? | |
244 | // GlobalFree(hData); | |
245 | ||
246 | return s; | |
247 | } | |
248 | #endif // __WXMICROWIN__ | |
249 | ||
250 | // ---------------------------------------------------------------------------- | |
251 | // get display info | |
252 | // ---------------------------------------------------------------------------- | |
253 | ||
254 | // See also the wxGetMousePosition in window.cpp | |
255 | // Deprecated: use wxPoint wxGetMousePosition() instead | |
256 | void wxGetMousePosition( int* x, int* y ) | |
257 | { | |
258 | POINT pt; | |
259 | GetCursorPos( & pt ); | |
260 | if ( x ) *x = pt.x; | |
261 | if ( y ) *y = pt.y; | |
259c43f6 | 262 | } |
e2478fde | 263 | |
27d2dbbc | 264 | // Return true if we have a colour display |
e2478fde VZ |
265 | bool wxColourDisplay() |
266 | { | |
267 | #ifdef __WXMICROWIN__ | |
268 | // MICROWIN_TODO | |
27d2dbbc | 269 | return true; |
e2478fde VZ |
270 | #else |
271 | // this function is called from wxDC ctor so it is called a *lot* of times | |
3103e8a9 | 272 | // hence we optimize it a bit but doing the check only once |
e2478fde VZ |
273 | // |
274 | // this should be MT safe as only the GUI thread (holding the GUI mutex) | |
275 | // can call us | |
276 | static int s_isColour = -1; | |
277 | ||
278 | if ( s_isColour == -1 ) | |
279 | { | |
280 | ScreenHDC dc; | |
281 | int noCols = ::GetDeviceCaps(dc, NUMCOLORS); | |
282 | ||
283 | s_isColour = (noCols == -1) || (noCols > 2); | |
284 | } | |
285 | ||
286 | return s_isColour != 0; | |
287 | #endif | |
288 | } | |
289 | ||
290 | // Returns depth of screen | |
291 | int wxDisplayDepth() | |
292 | { | |
293 | ScreenHDC dc; | |
294 | return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL); | |
295 | } | |
296 | ||
297 | // Get size of display | |
298 | void wxDisplaySize(int *width, int *height) | |
299 | { | |
300 | #ifdef __WXMICROWIN__ | |
301 | RECT rect; | |
302 | HWND hWnd = GetDesktopWindow(); | |
303 | ::GetWindowRect(hWnd, & rect); | |
304 | ||
305 | if ( width ) | |
306 | *width = rect.right - rect.left; | |
307 | if ( height ) | |
308 | *height = rect.bottom - rect.top; | |
309 | #else // !__WXMICROWIN__ | |
310 | ScreenHDC dc; | |
311 | ||
312 | if ( width ) | |
313 | *width = ::GetDeviceCaps(dc, HORZRES); | |
314 | if ( height ) | |
315 | *height = ::GetDeviceCaps(dc, VERTRES); | |
316 | #endif // __WXMICROWIN__/!__WXMICROWIN__ | |
317 | } | |
318 | ||
319 | void wxDisplaySizeMM(int *width, int *height) | |
320 | { | |
321 | #ifdef __WXMICROWIN__ | |
322 | // MICROWIN_TODO | |
323 | if ( width ) | |
324 | *width = 0; | |
325 | if ( height ) | |
326 | *height = 0; | |
327 | #else | |
328 | ScreenHDC dc; | |
329 | ||
330 | if ( width ) | |
331 | *width = ::GetDeviceCaps(dc, HORZSIZE); | |
332 | if ( height ) | |
333 | *height = ::GetDeviceCaps(dc, VERTSIZE); | |
334 | #endif | |
335 | } | |
336 | ||
337 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) | |
338 | { | |
3a5bcc4d | 339 | #if defined(__WXMICROWIN__) |
e2478fde VZ |
340 | *x = 0; *y = 0; |
341 | wxDisplaySize(width, height); | |
342 | #else | |
343 | // Determine the desktop dimensions minus the taskbar and any other | |
344 | // special decorations... | |
345 | RECT r; | |
346 | ||
347 | SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); | |
348 | if (x) *x = r.left; | |
349 | if (y) *y = r.top; | |
350 | if (width) *width = r.right - r.left; | |
351 | if (height) *height = r.bottom - r.top; | |
352 | #endif | |
353 | } | |
354 | ||
355 | // --------------------------------------------------------------------------- | |
356 | // window information functions | |
357 | // --------------------------------------------------------------------------- | |
358 | ||
359 | wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd) | |
360 | { | |
361 | wxString str; | |
362 | ||
363 | if ( hWnd ) | |
364 | { | |
365 | int len = GetWindowTextLength((HWND)hWnd) + 1; | |
de564874 | 366 | ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len); |
e2478fde VZ |
367 | } |
368 | ||
369 | return str; | |
370 | } | |
371 | ||
372 | wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd) | |
373 | { | |
374 | wxString str; | |
375 | ||
376 | // MICROWIN_TODO | |
377 | #ifndef __WXMICROWIN__ | |
378 | if ( hWnd ) | |
379 | { | |
380 | int len = 256; // some starting value | |
381 | ||
382 | for ( ;; ) | |
383 | { | |
de564874 | 384 | int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len); |
e2478fde | 385 | |
e2478fde VZ |
386 | if ( count == len ) |
387 | { | |
388 | // the class name might have been truncated, retry with larger | |
389 | // buffer | |
390 | len *= 2; | |
391 | } | |
392 | else | |
393 | { | |
394 | break; | |
395 | } | |
396 | } | |
397 | } | |
398 | #endif // !__WXMICROWIN__ | |
399 | ||
400 | return str; | |
401 | } | |
402 | ||
403 | WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd) | |
404 | { | |
e2478fde | 405 | return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID); |
e2478fde VZ |
406 | } |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // Metafile helpers | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
412 | extern void PixelToHIMETRIC(LONG *x, LONG *y) | |
413 | { | |
414 | ScreenHDC hdcRef; | |
415 | ||
416 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
417 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
418 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
419 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
420 | ||
421 | *x *= (iWidthMM * 100); | |
422 | *x /= iWidthPels; | |
423 | *y *= (iHeightMM * 100); | |
424 | *y /= iHeightPels; | |
425 | } | |
426 | ||
427 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
428 | { | |
429 | ScreenHDC hdcRef; | |
430 | ||
431 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
432 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
433 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
434 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
435 | ||
436 | *x *= iWidthPels; | |
437 | *x /= (iWidthMM * 100); | |
438 | *y *= iHeightPels; | |
439 | *y /= (iHeightMM * 100); | |
440 | } | |
441 | ||
4676948b JS |
442 | void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2) |
443 | { | |
444 | #ifdef __WXWINCE__ | |
445 | POINT points[2]; | |
446 | points[0].x = x1; | |
447 | points[0].y = y1; | |
448 | points[1].x = x2; | |
449 | points[1].y = y2; | |
450 | Polyline(hdc, points, 2); | |
451 | #else | |
452 | MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2); | |
453 | #endif | |
454 | } | |
455 | ||
e2478fde | 456 | |
5f6475c1 VZ |
457 | // ---------------------------------------------------------------------------- |
458 | // Shell API wrappers | |
459 | // ---------------------------------------------------------------------------- | |
460 | ||
461 | extern bool wxEnableFileNameAutoComplete(HWND hwnd) | |
462 | { | |
40e0cf48 | 463 | #if wxUSE_DYNLIB_CLASS |
5f6475c1 VZ |
464 | typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD); |
465 | ||
466 | static SHAutoComplete_t s_pfnSHAutoComplete = NULL; | |
467 | static bool s_initialized = false; | |
468 | ||
469 | if ( !s_initialized ) | |
470 | { | |
471 | s_initialized = true; | |
472 | ||
473 | wxLogNull nolog; | |
474 | wxDynamicLibrary dll(_T("shlwapi.dll")); | |
475 | if ( dll.IsLoaded() ) | |
476 | { | |
477 | s_pfnSHAutoComplete = | |
478 | (SHAutoComplete_t)dll.GetSymbol(_T("SHAutoComplete")); | |
479 | if ( s_pfnSHAutoComplete ) | |
480 | { | |
481 | // won't be unloaded until the process termination, no big deal | |
482 | dll.Detach(); | |
483 | } | |
484 | } | |
485 | } | |
486 | ||
487 | if ( !s_pfnSHAutoComplete ) | |
488 | return false; | |
489 | ||
490 | HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */); | |
491 | if ( FAILED(hr) ) | |
492 | { | |
493 | wxLogApiError(_T("SHAutoComplete"), hr); | |
494 | return false; | |
495 | } | |
496 | ||
497 | return true; | |
40e0cf48 VZ |
498 | #else |
499 | wxUnusedVar(hwnd); | |
500 | return false; | |
501 | #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS | |
5f6475c1 VZ |
502 | } |
503 |