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 licence
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/dynlib.h"
35 #include "wx/msw/private.h" // includes <windows.h>
37 // ============================================================================
39 // ============================================================================
41 // ---------------------------------------------------------------------------
42 // helper functions for showing a "busy" cursor
43 // ---------------------------------------------------------------------------
45 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
46 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
47 static int gs_wxBusyCursorCount
= 0;
49 extern HCURSOR
wxGetCurrentBusyCursor()
51 return gs_wxBusyCursor
;
54 // Set the cursor to the busy cursor for all windows
55 void wxBeginBusyCursor(const wxCursor
*cursor
)
57 if ( gs_wxBusyCursorCount
++ == 0 )
59 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
60 #ifndef __WXMICROWIN__
61 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
64 //else: nothing to do, already set
67 // Restore cursor to normal
68 void wxEndBusyCursor()
70 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
71 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
73 if ( --gs_wxBusyCursorCount
== 0 )
75 #ifndef __WXMICROWIN__
76 ::SetCursor(gs_wxBusyCursorOld
);
78 gs_wxBusyCursorOld
= 0;
82 // true if we're between the above two calls
85 return gs_wxBusyCursorCount
> 0;
88 // Check whether this window wants to process messages, e.g. Stop button
89 // in long calculations.
90 bool wxCheckForInterrupt(wxWindow
*wnd
)
92 wxCHECK( wnd
, false );
95 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
97 ::TranslateMessage(&msg
);
98 ::DispatchMessage(&msg
);
104 // MSW only: get user-defined resource from the .res file.
105 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
107 #ifndef __WXMICROWIN__
108 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
110 HRSRC hResource
= ::FindResource(wxGetInstance(),
111 resourceName
.wx_str(),
112 resourceType
.wx_str());
113 if ( hResource
== 0 )
116 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
120 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
124 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
125 // so we need to find the length of the resource.
126 int len
= ::SizeofResource(wxGetInstance(), hResource
) + 1;
127 wxChar
*s
= new wxChar
[len
];
128 wxStrlcpy(s
, theText
, len
);
132 UnlockResource(hData
);
136 // GlobalFree(hData);
140 #endif // __WXMICROWIN__
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // See also the wxGetMousePosition in window.cpp
147 // Deprecated: use wxPoint wxGetMousePosition() instead
148 void wxGetMousePosition( int* x
, int* y
)
151 GetCursorPos( & pt
);
156 // Return true if we have a colour display
157 bool wxColourDisplay()
159 #ifdef __WXMICROWIN__
163 // this function is called from wxDC ctor so it is called a *lot* of times
164 // hence we optimize it a bit but doing the check only once
166 // this should be MT safe as only the GUI thread (holding the GUI mutex)
168 static int s_isColour
= -1;
170 if ( s_isColour
== -1 )
173 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
175 s_isColour
= (noCols
== -1) || (noCols
> 2);
178 return s_isColour
!= 0;
182 // Returns depth of screen
186 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
189 // Get size of display
190 void wxDisplaySize(int *width
, int *height
)
192 #ifdef __WXMICROWIN__
194 HWND hWnd
= GetDesktopWindow();
195 ::GetWindowRect(hWnd
, & rect
);
198 *width
= rect
.right
- rect
.left
;
200 *height
= rect
.bottom
- rect
.top
;
201 #else // !__WXMICROWIN__
205 *width
= ::GetDeviceCaps(dc
, HORZRES
);
207 *height
= ::GetDeviceCaps(dc
, VERTRES
);
208 #endif // __WXMICROWIN__/!__WXMICROWIN__
211 void wxDisplaySizeMM(int *width
, int *height
)
213 #ifdef __WXMICROWIN__
223 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
225 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
229 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
231 #if defined(__WXMICROWIN__)
233 wxDisplaySize(width
, height
);
235 // Determine the desktop dimensions minus the taskbar and any other
236 // special decorations...
239 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
242 if (width
) *width
= r
.right
- r
.left
;
243 if (height
) *height
= r
.bottom
- r
.top
;
247 // ---------------------------------------------------------------------------
248 // window information functions
249 // ---------------------------------------------------------------------------
251 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
257 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
258 ::GetWindowText((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
264 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
269 #ifndef __WXMICROWIN__
272 int len
= 256; // some starting value
276 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
280 // the class name might have been truncated, retry with larger
290 #endif // !__WXMICROWIN__
295 int WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
297 return ::GetWindowLong((HWND
)hWnd
, GWL_ID
);
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
308 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
309 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
310 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
311 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
313 *x
*= (iWidthMM
* 100);
315 *y
*= (iHeightMM
* 100);
319 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
323 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
324 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
325 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
326 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
329 *x
/= (iWidthMM
* 100);
331 *y
/= (iHeightMM
* 100);
334 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
342 Polyline(hdc
, points
, 2);
344 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);
349 // ----------------------------------------------------------------------------
350 // Shell API wrappers
351 // ----------------------------------------------------------------------------
353 extern bool wxEnableFileNameAutoComplete(HWND hwnd
)
355 #if wxUSE_DYNLIB_CLASS
356 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
358 static SHAutoComplete_t s_pfnSHAutoComplete
= NULL
;
359 static bool s_initialized
= false;
361 if ( !s_initialized
)
363 s_initialized
= true;
366 wxDynamicLibrary
dll(_T("shlwapi.dll"));
367 if ( dll
.IsLoaded() )
369 s_pfnSHAutoComplete
=
370 (SHAutoComplete_t
)dll
.GetSymbol(_T("SHAutoComplete"));
371 if ( s_pfnSHAutoComplete
)
373 // won't be unloaded until the process termination, no big deal
379 if ( !s_pfnSHAutoComplete
)
382 HRESULT hr
= s_pfnSHAutoComplete(hwnd
, 0x10 /* SHACF_FILESYS_ONLY */);
385 wxLogApiError(_T("SHAutoComplete"), hr
);
393 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS