]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsgui.cpp
added Show/HideNativeCaret() (patch 759924)
[wxWidgets.git] / src / msw / utilsgui.cpp
CommitLineData
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
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"
42d54220 29 #include "wx/window.h"
e2478fde
VZ
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
45bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
46{
fda7962d 47 if (file != wxEmptyString)
e2478fde
VZ
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
59bool 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
67bool 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
75bool 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
83bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file)
84{
85 static const wxChar defunkt[] = wxT("$$default");
3e3be693
VZ
86
87 wxChar buf[1024];
fda7962d 88 if (file != wxEmptyString)
e2478fde 89 {
3e3be693
VZ
90 int n = GetPrivateProfileString(section, entry, defunkt,
91 buf, WXSIZEOF(buf), file);
92 if (n == 0 || wxStrcmp(buf, defunkt) == 0)
e2478fde
VZ
93 return FALSE;
94 }
95 else
96 {
3e3be693
VZ
97 int n = GetProfileString(section, entry, defunkt, buf, WXSIZEOF(buf));
98 if (n == 0 || wxStrcmp(buf, defunkt) == 0)
e2478fde
VZ
99 return FALSE;
100 }
101 if (*value) delete[] (*value);
3e3be693 102 *value = copystring(buf);
e2478fde
VZ
103 return TRUE;
104}
105
106bool 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
119bool 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
132bool 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
150static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
151static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
152static int gs_wxBusyCursorCount = 0;
153
154extern HCURSOR wxGetCurrentBusyCursor()
155{
156 return gs_wxBusyCursor;
157}
158
159// Set the cursor to the busy cursor for all windows
160void 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
173void 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
188bool 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.
195bool 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__
213wxChar *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
254void 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
263bool 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
289int wxDisplayDepth()
290{
291 ScreenHDC dc;
292 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
293}
294
295// Get size of display
296void 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
317void 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
335void wxClientDisplayRect(int *x, int *y, int *width, int *height)
336{
337#if defined(__WIN16__) || 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
357wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
358{
359 wxString str;
360
361 if ( hWnd )
362 {
363 int len = GetWindowTextLength((HWND)hWnd) + 1;
364 ::GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len);
365 str.UngetWriteBuf();
366 }
367
368 return str;
369}
370
371wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
372{
373 wxString str;
374
375 // MICROWIN_TODO
376#ifndef __WXMICROWIN__
377 if ( hWnd )
378 {
379 int len = 256; // some starting value
380
381 for ( ;; )
382 {
383 int count = ::GetClassName((HWND)hWnd, str.GetWriteBuf(len), len);
384
385 str.UngetWriteBuf();
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
403WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
404{
405#ifndef __WIN32__
406 return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID);
407#else // Win32
408 return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID);
409#endif // Win16/32
410}
411
412// ----------------------------------------------------------------------------
413// Metafile helpers
414// ----------------------------------------------------------------------------
415
416extern void PixelToHIMETRIC(LONG *x, LONG *y)
417{
418 ScreenHDC hdcRef;
419
420 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
421 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
422 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
423 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
424
425 *x *= (iWidthMM * 100);
426 *x /= iWidthPels;
427 *y *= (iHeightMM * 100);
428 *y /= iHeightPels;
429}
430
431extern void HIMETRICToPixel(LONG *x, LONG *y)
432{
433 ScreenHDC hdcRef;
434
435 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
436 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
437 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
438 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
439
440 *x *= iWidthPels;
441 *x /= (iWidthMM * 100);
442 *y *= iHeightPels;
443 *y /= (iHeightMM * 100);
444}
445
4676948b
JS
446void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2)
447{
448#ifdef __WXWINCE__
449 POINT points[2];
450 points[0].x = x1;
451 points[0].y = y1;
452 points[1].x = x2;
453 points[1].y = y2;
454 Polyline(hdc, points, 2);
455#else
456 MoveToEx(hdc, x1, y1, NULL); LineTo((HDC) hdc, x2, y2);
457#endif
458}
459
e2478fde 460