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