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
)
47 if (file
!= wxEmptyString
)
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 if (file
!= wxEmptyString
)
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
= wxStrcpy(new wxChar
[wxStrlen(buf
) + 1], 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(__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
, wxStringBuffer(str
, len
), len
);
370 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
375 #ifndef __WXMICROWIN__
378 int len
= 256; // some starting value
382 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
386 // the class name might have been truncated, retry with larger
396 #endif // !__WXMICROWIN__
401 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
403 return (WXWORD
)GetWindowLong((HWND
)hWnd
, GWL_ID
);
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
414 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
415 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
416 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
417 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
419 *x
*= (iWidthMM
* 100);
421 *y
*= (iHeightMM
* 100);
425 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
429 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
430 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
431 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
432 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
435 *x
/= (iWidthMM
* 100);
437 *y
/= (iHeightMM
* 100);
440 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
448 Polyline(hdc
, points
, 2);
450 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);