]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsgui.cpp
Whitespaces, tabs and fix to the lack of empty line at end.
[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/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 != wxEmptyString)
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
87 wxChar buf[1024];
88 if (file != wxEmptyString)
89 {
90 int n = GetPrivateProfileString(section, entry, defunkt,
91 buf, WXSIZEOF(buf), file);
92 if (n == 0 || wxStrcmp(buf, defunkt) == 0)
93 return false;
94 }
95 else
96 {
97 int n = GetProfileString(section, entry, defunkt, buf, WXSIZEOF(buf));
98 if (n == 0 || wxStrcmp(buf, defunkt) == 0)
99 return false;
100 }
101 if (*value) delete[] (*value);
102 *value = wxStrcpy(new wxChar[wxStrlen(buf) + 1], buf);
103 return true;
104 }
105
106 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
107 {
108 wxChar *s = NULL;
109 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
110 if (succ)
111 {
112 *value = (float)wxStrtod(s, NULL);
113 delete[] s;
114 return true;
115 }
116 else return false;
117 }
118
119 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
120 {
121 wxChar *s = NULL;
122 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
123 if (succ)
124 {
125 *value = wxStrtol(s, NULL, 10);
126 delete[] s;
127 return true;
128 }
129 else return false;
130 }
131
132 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
133 {
134 wxChar *s = NULL;
135 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
136 if (succ)
137 {
138 *value = (int)wxStrtol(s, NULL, 10);
139 delete[] s;
140 return true;
141 }
142 else return false;
143 }
144 #endif // wxUSE_RESOURCES
145
146 // ---------------------------------------------------------------------------
147 // helper functions for showing a "busy" cursor
148 // ---------------------------------------------------------------------------
149
150 static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
151 static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
152 static int gs_wxBusyCursorCount = 0;
153
154 extern HCURSOR wxGetCurrentBusyCursor()
155 {
156 return gs_wxBusyCursor;
157 }
158
159 // Set the cursor to the busy cursor for all windows
160 void wxBeginBusyCursor(wxCursor *cursor)
161 {
162 if ( gs_wxBusyCursorCount++ == 0 )
163 {
164 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
165 #ifndef __WXMICROWIN__
166 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
167 #endif
168 }
169 //else: nothing to do, already set
170 }
171
172 // Restore cursor to normal
173 void wxEndBusyCursor()
174 {
175 wxCHECK_RET( gs_wxBusyCursorCount > 0,
176 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
177
178 if ( --gs_wxBusyCursorCount == 0 )
179 {
180 #ifndef __WXMICROWIN__
181 ::SetCursor(gs_wxBusyCursorOld);
182 #endif
183 gs_wxBusyCursorOld = 0;
184 }
185 }
186
187 // true if we're between the above two calls
188 bool wxIsBusy()
189 {
190 return gs_wxBusyCursorCount > 0;
191 }
192
193 // Check whether this window wants to process messages, e.g. Stop button
194 // in long calculations.
195 bool wxCheckForInterrupt(wxWindow *wnd)
196 {
197 wxCHECK( wnd, false );
198
199 MSG msg;
200 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
201 {
202 ::TranslateMessage(&msg);
203 ::DispatchMessage(&msg);
204 }
205
206 return true;
207 }
208
209 // MSW only: get user-defined resource from the .res file.
210 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
211
212 #ifndef __WXMICROWIN__
213 wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
214 {
215 HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType);
216 if ( hResource == 0 )
217 return NULL;
218
219 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
220 if ( hData == 0 )
221 return NULL;
222
223 wxChar *theText = (wxChar *)::LockResource(hData);
224 if ( !theText )
225 return NULL;
226
227 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
228 // so we need to find the length of the resource.
229 int len = ::SizeofResource(wxGetInstance(), hResource);
230 wxChar *s = new wxChar[len+1];
231 wxStrncpy(s,theText,len);
232 s[len]=0;
233
234 // wxChar *s = copystring(theText);
235
236 // Obsolete in WIN32
237 #ifndef __WIN32__
238 UnlockResource(hData);
239 #endif
240
241 // No need??
242 // GlobalFree(hData);
243
244 return s;
245 }
246 #endif // __WXMICROWIN__
247
248 // ----------------------------------------------------------------------------
249 // get display info
250 // ----------------------------------------------------------------------------
251
252 // See also the wxGetMousePosition in window.cpp
253 // Deprecated: use wxPoint wxGetMousePosition() instead
254 void wxGetMousePosition( int* x, int* y )
255 {
256 POINT pt;
257 GetCursorPos( & pt );
258 if ( x ) *x = pt.x;
259 if ( y ) *y = pt.y;
260 };
261
262 // Return true if we have a colour display
263 bool wxColourDisplay()
264 {
265 #ifdef __WXMICROWIN__
266 // MICROWIN_TODO
267 return true;
268 #else
269 // this function is called from wxDC ctor so it is called a *lot* of times
270 // hence we optimize it a bit but doign the check only once
271 //
272 // this should be MT safe as only the GUI thread (holding the GUI mutex)
273 // can call us
274 static int s_isColour = -1;
275
276 if ( s_isColour == -1 )
277 {
278 ScreenHDC dc;
279 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
280
281 s_isColour = (noCols == -1) || (noCols > 2);
282 }
283
284 return s_isColour != 0;
285 #endif
286 }
287
288 // Returns depth of screen
289 int wxDisplayDepth()
290 {
291 ScreenHDC dc;
292 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
293 }
294
295 // Get size of display
296 void wxDisplaySize(int *width, int *height)
297 {
298 #ifdef __WXMICROWIN__
299 RECT rect;
300 HWND hWnd = GetDesktopWindow();
301 ::GetWindowRect(hWnd, & rect);
302
303 if ( width )
304 *width = rect.right - rect.left;
305 if ( height )
306 *height = rect.bottom - rect.top;
307 #else // !__WXMICROWIN__
308 ScreenHDC dc;
309
310 if ( width )
311 *width = ::GetDeviceCaps(dc, HORZRES);
312 if ( height )
313 *height = ::GetDeviceCaps(dc, VERTRES);
314 #endif // __WXMICROWIN__/!__WXMICROWIN__
315 }
316
317 void wxDisplaySizeMM(int *width, int *height)
318 {
319 #ifdef __WXMICROWIN__
320 // MICROWIN_TODO
321 if ( width )
322 *width = 0;
323 if ( height )
324 *height = 0;
325 #else
326 ScreenHDC dc;
327
328 if ( width )
329 *width = ::GetDeviceCaps(dc, HORZSIZE);
330 if ( height )
331 *height = ::GetDeviceCaps(dc, VERTSIZE);
332 #endif
333 }
334
335 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
336 {
337 #if defined(__WXMICROWIN__)
338 *x = 0; *y = 0;
339 wxDisplaySize(width, height);
340 #else
341 // Determine the desktop dimensions minus the taskbar and any other
342 // special decorations...
343 RECT r;
344
345 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
346 if (x) *x = r.left;
347 if (y) *y = r.top;
348 if (width) *width = r.right - r.left;
349 if (height) *height = r.bottom - r.top;
350 #endif
351 }
352
353 // ---------------------------------------------------------------------------
354 // window information functions
355 // ---------------------------------------------------------------------------
356
357 wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
358 {
359 wxString str;
360
361 if ( hWnd )
362 {
363 int len = GetWindowTextLength((HWND)hWnd) + 1;
364 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
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, wxStringBuffer(str, len), len);
383
384 if ( count == len )
385 {
386 // the class name might have been truncated, retry with larger
387 // buffer
388 len *= 2;
389 }
390 else
391 {
392 break;
393 }
394 }
395 }
396 #endif // !__WXMICROWIN__
397
398 return str;
399 }
400
401 WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
402 {
403 return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID);
404 }
405
406 // ----------------------------------------------------------------------------
407 // Metafile helpers
408 // ----------------------------------------------------------------------------
409
410 extern void PixelToHIMETRIC(LONG *x, LONG *y)
411 {
412 ScreenHDC hdcRef;
413
414 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
415 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
416 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
417 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
418
419 *x *= (iWidthMM * 100);
420 *x /= iWidthPels;
421 *y *= (iHeightMM * 100);
422 *y /= iHeightPels;
423 }
424
425 extern void HIMETRICToPixel(LONG *x, LONG *y)
426 {
427 ScreenHDC hdcRef;
428
429 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
430 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
431 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
432 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
433
434 *x *= iWidthPels;
435 *x /= (iWidthMM * 100);
436 *y *= iHeightPels;
437 *y /= (iHeightMM * 100);
438 }
439
440 void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
441 {
442 #ifdef __WXWINCE__
443 POINT points[2];
444 points[0].x = x1;
445 points[0].y = y1;
446 points[1].x = x2;
447 points[1].y = y2;
448 Polyline(hdc, points, 2);
449 #else
450 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
451 #endif
452 }
453
454