moved combobox styles from defs.to to combobox.h; added wxTE/wxCB_FILENAME styles...
[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 // 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 {
49 if (file != wxEmptyString)
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");
88
89 wxChar buf[1024];
90 if (file != wxEmptyString)
91 {
92 int n = GetPrivateProfileString(section, entry, defunkt,
93 buf, WXSIZEOF(buf), file);
94 if (n == 0 || wxStrcmp(buf, defunkt) == 0)
95 return false;
96 }
97 else
98 {
99 int n = GetProfileString(section, entry, defunkt, buf, WXSIZEOF(buf));
100 if (n == 0 || wxStrcmp(buf, defunkt) == 0)
101 return false;
102 }
103 if (*value) delete[] (*value);
104 *value = wxStrcpy(new wxChar[wxStrlen(buf) + 1], buf);
105 return true;
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;
116 return true;
117 }
118 else return false;
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;
129 return true;
130 }
131 else return false;
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;
142 return true;
143 }
144 else return false;
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
162 void wxBeginBusyCursor(const wxCursor *cursor)
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
189 // true if we're between the above two calls
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 {
199 wxCHECK( wnd, false );
200
201 MSG msg;
202 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
203 {
204 ::TranslateMessage(&msg);
205 ::DispatchMessage(&msg);
206 }
207
208 return true;
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;
262 }
263
264 // Return true if we have a colour display
265 bool wxColourDisplay()
266 {
267 #ifdef __WXMICROWIN__
268 // MICROWIN_TODO
269 return true;
270 #else
271 // this function is called from wxDC ctor so it is called a *lot* of times
272 // hence we optimize it a bit but doing the check only once
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 {
339 #if defined(__WXMICROWIN__)
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;
366 ::GetWindowText((HWND)hWnd, wxStringBuffer(str, len), len);
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 {
384 int count = ::GetClassName((HWND)hWnd, wxStringBuffer(str, len), len);
385
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 {
405 return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID);
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
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
456
457 // ----------------------------------------------------------------------------
458 // Shell API wrappers
459 // ----------------------------------------------------------------------------
460
461 extern bool wxEnableFileNameAutoComplete(HWND hwnd)
462 {
463 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
464
465 static SHAutoComplete_t s_pfnSHAutoComplete = NULL;
466 static bool s_initialized = false;
467
468 if ( !s_initialized )
469 {
470 s_initialized = true;
471
472 wxLogNull nolog;
473 wxDynamicLibrary dll(_T("shlwapi.dll"));
474 if ( dll.IsLoaded() )
475 {
476 s_pfnSHAutoComplete =
477 (SHAutoComplete_t)dll.GetSymbol(_T("SHAutoComplete"));
478 if ( s_pfnSHAutoComplete )
479 {
480 // won't be unloaded until the process termination, no big deal
481 dll.Detach();
482 }
483 }
484 }
485
486 if ( !s_pfnSHAutoComplete )
487 return false;
488
489 HRESULT hr = s_pfnSHAutoComplete(hwnd, 0x10 /* SHACF_FILESYS_ONLY */);
490 if ( FAILED(hr) )
491 {
492 wxLogApiError(_T("SHAutoComplete"), hr);
493 return false;
494 }
495
496 return true;
497 }
498