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");
90 int n
= GetPrivateProfileString(section
, entry
, defunkt
,
91 buf
, WXSIZEOF(buf
), file
);
92 if (n
== 0 || wxStrcmp(buf
, defunkt
) == 0)
97 int n
= GetProfileString(section
, entry
, defunkt
, buf
, WXSIZEOF(buf
));
98 if (n
== 0 || wxStrcmp(buf
, defunkt
) == 0)
101 if (*value
) delete[] (*value
);
102 *value
= copystring(buf
);
106 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
109 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
112 *value
= (float)wxStrtod(s
, NULL
);
119 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
122 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
125 *value
= wxStrtol(s
, NULL
, 10);
132 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
135 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
138 *value
= (int)wxStrtol(s
, NULL
, 10);
144 #endif // wxUSE_RESOURCES
146 // ---------------------------------------------------------------------------
147 // helper functions for showing a "busy" cursor
148 // ---------------------------------------------------------------------------
150 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
151 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
152 static int gs_wxBusyCursorCount
= 0;
154 extern HCURSOR
wxGetCurrentBusyCursor()
156 return gs_wxBusyCursor
;
159 // Set the cursor to the busy cursor for all windows
160 void wxBeginBusyCursor(wxCursor
*cursor
)
162 if ( gs_wxBusyCursorCount
++ == 0 )
164 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
165 #ifndef __WXMICROWIN__
166 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
169 //else: nothing to do, already set
172 // Restore cursor to normal
173 void wxEndBusyCursor()
175 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
176 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
178 if ( --gs_wxBusyCursorCount
== 0 )
180 #ifndef __WXMICROWIN__
181 ::SetCursor(gs_wxBusyCursorOld
);
183 gs_wxBusyCursorOld
= 0;
187 // TRUE if we're between the above two calls
190 return gs_wxBusyCursorCount
> 0;
193 // Check whether this window wants to process messages, e.g. Stop button
194 // in long calculations.
195 bool wxCheckForInterrupt(wxWindow
*wnd
)
197 wxCHECK( wnd
, FALSE
);
200 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
202 ::TranslateMessage(&msg
);
203 ::DispatchMessage(&msg
);
209 // MSW only: get user-defined resource from the .res file.
210 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
212 #ifndef __WXMICROWIN__
213 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
215 HRSRC hResource
= ::FindResource(wxGetInstance(), resourceName
, resourceType
);
216 if ( hResource
== 0 )
219 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
223 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
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
);
234 // wxChar *s = copystring(theText);
238 UnlockResource(hData
);
242 // GlobalFree(hData);
246 #endif // __WXMICROWIN__
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 // See also the wxGetMousePosition in window.cpp
253 // Deprecated: use wxPoint wxGetMousePosition() instead
254 void wxGetMousePosition( int* x
, int* y
)
257 GetCursorPos( & pt
);
262 // Return TRUE if we have a colour display
263 bool wxColourDisplay()
265 #ifdef __WXMICROWIN__
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
272 // this should be MT safe as only the GUI thread (holding the GUI mutex)
274 static int s_isColour
= -1;
276 if ( s_isColour
== -1 )
279 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
281 s_isColour
= (noCols
== -1) || (noCols
> 2);
284 return s_isColour
!= 0;
288 // Returns depth of screen
292 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
295 // Get size of display
296 void wxDisplaySize(int *width
, int *height
)
298 #ifdef __WXMICROWIN__
300 HWND hWnd
= GetDesktopWindow();
301 ::GetWindowRect(hWnd
, & rect
);
304 *width
= rect
.right
- rect
.left
;
306 *height
= rect
.bottom
- rect
.top
;
307 #else // !__WXMICROWIN__
311 *width
= ::GetDeviceCaps(dc
, HORZRES
);
313 *height
= ::GetDeviceCaps(dc
, VERTRES
);
314 #endif // __WXMICROWIN__/!__WXMICROWIN__
317 void wxDisplaySizeMM(int *width
, int *height
)
319 #ifdef __WXMICROWIN__
329 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
331 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
335 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
337 #if defined(__WIN16__) || defined(__WXMICROWIN__)
339 wxDisplaySize(width
, height
);
341 // Determine the desktop dimensions minus the taskbar and any other
342 // special decorations...
345 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
348 if (width
) *width
= r
.right
- r
.left
;
349 if (height
) *height
= r
.bottom
- r
.top
;
353 // ---------------------------------------------------------------------------
354 // window information functions
355 // ---------------------------------------------------------------------------
357 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
363 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
364 ::GetWindowText((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
371 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
376 #ifndef __WXMICROWIN__
379 int len
= 256; // some starting value
383 int count
= ::GetClassName((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
388 // the class name might have been truncated, retry with larger
398 #endif // !__WXMICROWIN__
403 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
406 return (WXWORD
)GetWindowWord((HWND
)hWnd
, GWW_ID
);
408 return (WXWORD
)GetWindowLong((HWND
)hWnd
, GWL_ID
);
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
420 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
421 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
422 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
423 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
425 *x
*= (iWidthMM
* 100);
427 *y
*= (iHeightMM
* 100);
431 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
435 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
436 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
437 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
438 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
441 *x
/= (iWidthMM
* 100);
443 *y
/= (iHeightMM
* 100);