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