1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/utilsgui.cpp
3 // Purpose: Various utility functions only available in GUI
4 // Author: Vadim Zeitlin
6 // Created: 21.06.2003 (extracted from msw/utils.cpp)
8 // Copyright: (c) Julian Smart
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/cursor.h"
32 #include "wx/msw/private.h" // includes <windows.h>
34 // ============================================================================
36 // ============================================================================
38 // ----------------------------------------------------------------------------
39 // functions to work with .INI files
40 // ----------------------------------------------------------------------------
42 // Reading and writing resources (eg WIN.INI, .Xdefaults)
44 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
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);
52 return (WritePrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)value
, (LPCTSTR
)WXSTRINGCAST file
) != 0);
55 return (WriteProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)WXSTRINGCAST value
) != 0);
58 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
61 buf
.Printf(wxT("%.4f"), value
);
63 return wxWriteResource(section
, entry
, buf
, file
);
66 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
69 buf
.Printf(wxT("%ld"), value
);
71 return wxWriteResource(section
, entry
, buf
, file
);
74 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
77 buf
.Printf(wxT("%d"), value
);
79 return wxWriteResource(section
, entry
, buf
, file
);
82 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
84 static const wxChar defunkt
[] = wxT("$$default");
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)
94 int n
= GetProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
95 (LPTSTR
)wxBuffer
, 1000);
96 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
99 if (*value
) delete[] (*value
);
100 *value
= copystring(wxBuffer
);
104 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
107 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
110 *value
= (float)wxStrtod(s
, NULL
);
117 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
120 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
123 *value
= wxStrtol(s
, NULL
, 10);
130 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
133 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
136 *value
= (int)wxStrtol(s
, NULL
, 10);
142 #endif // wxUSE_RESOURCES
144 // ---------------------------------------------------------------------------
145 // helper functions for showing a "busy" cursor
146 // ---------------------------------------------------------------------------
148 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
149 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
150 static int gs_wxBusyCursorCount
= 0;
152 extern HCURSOR
wxGetCurrentBusyCursor()
154 return gs_wxBusyCursor
;
157 // Set the cursor to the busy cursor for all windows
158 void wxBeginBusyCursor(wxCursor
*cursor
)
160 if ( gs_wxBusyCursorCount
++ == 0 )
162 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
163 #ifndef __WXMICROWIN__
164 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
167 //else: nothing to do, already set
170 // Restore cursor to normal
171 void wxEndBusyCursor()
173 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
174 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
176 if ( --gs_wxBusyCursorCount
== 0 )
178 #ifndef __WXMICROWIN__
179 ::SetCursor(gs_wxBusyCursorOld
);
181 gs_wxBusyCursorOld
= 0;
185 // TRUE if we're between the above two calls
188 return gs_wxBusyCursorCount
> 0;
191 // Check whether this window wants to process messages, e.g. Stop button
192 // in long calculations.
193 bool wxCheckForInterrupt(wxWindow
*wnd
)
195 wxCHECK( wnd
, FALSE
);
198 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
200 ::TranslateMessage(&msg
);
201 ::DispatchMessage(&msg
);
207 // MSW only: get user-defined resource from the .res file.
208 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
210 #ifndef __WXMICROWIN__
211 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
213 HRSRC hResource
= ::FindResource(wxGetInstance(), resourceName
, resourceType
);
214 if ( hResource
== 0 )
217 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
221 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
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
);
232 // wxChar *s = copystring(theText);
236 UnlockResource(hData
);
240 // GlobalFree(hData);
244 #endif // __WXMICROWIN__
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 // See also the wxGetMousePosition in window.cpp
251 // Deprecated: use wxPoint wxGetMousePosition() instead
252 void wxGetMousePosition( int* x
, int* y
)
255 GetCursorPos( & pt
);
260 // Return TRUE if we have a colour display
261 bool wxColourDisplay()
263 #ifdef __WXMICROWIN__
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
270 // this should be MT safe as only the GUI thread (holding the GUI mutex)
272 static int s_isColour
= -1;
274 if ( s_isColour
== -1 )
277 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
279 s_isColour
= (noCols
== -1) || (noCols
> 2);
282 return s_isColour
!= 0;
286 // Returns depth of screen
290 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
293 // Get size of display
294 void wxDisplaySize(int *width
, int *height
)
296 #ifdef __WXMICROWIN__
298 HWND hWnd
= GetDesktopWindow();
299 ::GetWindowRect(hWnd
, & rect
);
302 *width
= rect
.right
- rect
.left
;
304 *height
= rect
.bottom
- rect
.top
;
305 #else // !__WXMICROWIN__
309 *width
= ::GetDeviceCaps(dc
, HORZRES
);
311 *height
= ::GetDeviceCaps(dc
, VERTRES
);
312 #endif // __WXMICROWIN__/!__WXMICROWIN__
315 void wxDisplaySizeMM(int *width
, int *height
)
317 #ifdef __WXMICROWIN__
327 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
329 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
333 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
335 #if defined(__WIN16__) || defined(__WXMICROWIN__)
337 wxDisplaySize(width
, height
);
339 // Determine the desktop dimensions minus the taskbar and any other
340 // special decorations...
343 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
346 if (width
) *width
= r
.right
- r
.left
;
347 if (height
) *height
= r
.bottom
- r
.top
;
351 // ---------------------------------------------------------------------------
352 // window information functions
353 // ---------------------------------------------------------------------------
355 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
361 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
362 ::GetWindowText((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
369 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
374 #ifndef __WXMICROWIN__
377 int len
= 256; // some starting value
381 int count
= ::GetClassName((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
386 // the class name might have been truncated, retry with larger
396 #endif // !__WXMICROWIN__
401 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
404 return (WXWORD
)GetWindowWord((HWND
)hWnd
, GWW_ID
);
406 return (WXWORD
)GetWindowLong((HWND
)hWnd
, GWL_ID
);
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
418 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
419 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
420 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
421 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
423 *x
*= (iWidthMM
* 100);
425 *y
*= (iHeightMM
* 100);
429 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
433 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
434 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
435 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
436 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
439 *x
/= (iWidthMM
* 100);
441 *y
/= (iHeightMM
* 100);