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"
29 #include "wx/window.h"
33 #include "wx/msw/private.h" // includes <windows.h>
35 // ============================================================================
37 // ============================================================================
39 // ----------------------------------------------------------------------------
40 // functions to work with .INI files
41 // ----------------------------------------------------------------------------
43 // Reading and writing resources (eg WIN.INI, .Xdefaults)
45 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
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);
53 return (WritePrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)value
, (LPCTSTR
)WXSTRINGCAST file
) != 0);
56 return (WriteProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)WXSTRINGCAST value
) != 0);
59 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
62 buf
.Printf(wxT("%.4f"), value
);
64 return wxWriteResource(section
, entry
, buf
, file
);
67 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
70 buf
.Printf(wxT("%ld"), value
);
72 return wxWriteResource(section
, entry
, buf
, file
);
75 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
78 buf
.Printf(wxT("%d"), value
);
80 return wxWriteResource(section
, entry
, buf
, file
);
83 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
85 static const wxChar defunkt
[] = wxT("$$default");
88 int n
= GetPrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
89 (LPTSTR
)wxBuffer
, 1000, (LPCTSTR
)WXSTRINGCAST file
);
90 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
95 int n
= GetProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
96 (LPTSTR
)wxBuffer
, 1000);
97 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
100 if (*value
) delete[] (*value
);
101 *value
= copystring(wxBuffer
);
105 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
108 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
111 *value
= (float)wxStrtod(s
, NULL
);
118 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
121 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
124 *value
= wxStrtol(s
, NULL
, 10);
131 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
134 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
137 *value
= (int)wxStrtol(s
, NULL
, 10);
143 #endif // wxUSE_RESOURCES
145 // ---------------------------------------------------------------------------
146 // helper functions for showing a "busy" cursor
147 // ---------------------------------------------------------------------------
149 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
150 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
151 static int gs_wxBusyCursorCount
= 0;
153 extern HCURSOR
wxGetCurrentBusyCursor()
155 return gs_wxBusyCursor
;
158 // Set the cursor to the busy cursor for all windows
159 void wxBeginBusyCursor(wxCursor
*cursor
)
161 if ( gs_wxBusyCursorCount
++ == 0 )
163 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
164 #ifndef __WXMICROWIN__
165 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
168 //else: nothing to do, already set
171 // Restore cursor to normal
172 void wxEndBusyCursor()
174 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
175 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
177 if ( --gs_wxBusyCursorCount
== 0 )
179 #ifndef __WXMICROWIN__
180 ::SetCursor(gs_wxBusyCursorOld
);
182 gs_wxBusyCursorOld
= 0;
186 // TRUE if we're between the above two calls
189 return gs_wxBusyCursorCount
> 0;
192 // Check whether this window wants to process messages, e.g. Stop button
193 // in long calculations.
194 bool wxCheckForInterrupt(wxWindow
*wnd
)
196 wxCHECK( wnd
, FALSE
);
199 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
201 ::TranslateMessage(&msg
);
202 ::DispatchMessage(&msg
);
208 // MSW only: get user-defined resource from the .res file.
209 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
211 #ifndef __WXMICROWIN__
212 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
214 HRSRC hResource
= ::FindResource(wxGetInstance(), resourceName
, resourceType
);
215 if ( hResource
== 0 )
218 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
222 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
226 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
227 // so we need to find the length of the resource.
228 int len
= ::SizeofResource(wxGetInstance(), hResource
);
229 wxChar
*s
= new wxChar
[len
+1];
230 wxStrncpy(s
,theText
,len
);
233 // wxChar *s = copystring(theText);
237 UnlockResource(hData
);
241 // GlobalFree(hData);
245 #endif // __WXMICROWIN__
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // See also the wxGetMousePosition in window.cpp
252 // Deprecated: use wxPoint wxGetMousePosition() instead
253 void wxGetMousePosition( int* x
, int* y
)
256 GetCursorPos( & pt
);
261 // Return TRUE if we have a colour display
262 bool wxColourDisplay()
264 #ifdef __WXMICROWIN__
268 // this function is called from wxDC ctor so it is called a *lot* of times
269 // hence we optimize it a bit but doign the check only once
271 // this should be MT safe as only the GUI thread (holding the GUI mutex)
273 static int s_isColour
= -1;
275 if ( s_isColour
== -1 )
278 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
280 s_isColour
= (noCols
== -1) || (noCols
> 2);
283 return s_isColour
!= 0;
287 // Returns depth of screen
291 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
294 // Get size of display
295 void wxDisplaySize(int *width
, int *height
)
297 #ifdef __WXMICROWIN__
299 HWND hWnd
= GetDesktopWindow();
300 ::GetWindowRect(hWnd
, & rect
);
303 *width
= rect
.right
- rect
.left
;
305 *height
= rect
.bottom
- rect
.top
;
306 #else // !__WXMICROWIN__
310 *width
= ::GetDeviceCaps(dc
, HORZRES
);
312 *height
= ::GetDeviceCaps(dc
, VERTRES
);
313 #endif // __WXMICROWIN__/!__WXMICROWIN__
316 void wxDisplaySizeMM(int *width
, int *height
)
318 #ifdef __WXMICROWIN__
328 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
330 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
334 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
336 #if defined(__WIN16__) || defined(__WXMICROWIN__)
338 wxDisplaySize(width
, height
);
340 // Determine the desktop dimensions minus the taskbar and any other
341 // special decorations...
344 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
347 if (width
) *width
= r
.right
- r
.left
;
348 if (height
) *height
= r
.bottom
- r
.top
;
352 // ---------------------------------------------------------------------------
353 // window information functions
354 // ---------------------------------------------------------------------------
356 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
362 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
363 ::GetWindowText((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
370 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
375 #ifndef __WXMICROWIN__
378 int len
= 256; // some starting value
382 int count
= ::GetClassName((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
387 // the class name might have been truncated, retry with larger
397 #endif // !__WXMICROWIN__
402 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
405 return (WXWORD
)GetWindowWord((HWND
)hWnd
, GWW_ID
);
407 return (WXWORD
)GetWindowLong((HWND
)hWnd
, GWL_ID
);
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
419 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
420 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
421 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
422 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
424 *x
*= (iWidthMM
* 100);
426 *y
*= (iHeightMM
* 100);
430 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
434 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
435 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
436 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
437 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
440 *x
/= (iWidthMM
* 100);
442 *y
/= (iHeightMM
* 100);