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
);
127 wxChar
*s
= new wxChar
[len
+1];
128 wxStrncpy(s
,theText
,len
);
133 UnlockResource(hData
);
137 // GlobalFree(hData);
141 #endif // __WXMICROWIN__
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 // See also the wxGetMousePosition in window.cpp
148 // Deprecated: use wxPoint wxGetMousePosition() instead
149 void wxGetMousePosition( int* x
, int* y
)
152 GetCursorPos( & pt
);
157 // Return true if we have a colour display
158 bool wxColourDisplay()
160 #ifdef __WXMICROWIN__
164 // this function is called from wxDC ctor so it is called a *lot* of times
165 // hence we optimize it a bit but doing the check only once
167 // this should be MT safe as only the GUI thread (holding the GUI mutex)
169 static int s_isColour
= -1;
171 if ( s_isColour
== -1 )
174 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
176 s_isColour
= (noCols
== -1) || (noCols
> 2);
179 return s_isColour
!= 0;
183 // Returns depth of screen
187 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
190 // Get size of display
191 void wxDisplaySize(int *width
, int *height
)
193 #ifdef __WXMICROWIN__
195 HWND hWnd
= GetDesktopWindow();
196 ::GetWindowRect(hWnd
, & rect
);
199 *width
= rect
.right
- rect
.left
;
201 *height
= rect
.bottom
- rect
.top
;
202 #else // !__WXMICROWIN__
206 *width
= ::GetDeviceCaps(dc
, HORZRES
);
208 *height
= ::GetDeviceCaps(dc
, VERTRES
);
209 #endif // __WXMICROWIN__/!__WXMICROWIN__
212 void wxDisplaySizeMM(int *width
, int *height
)
214 #ifdef __WXMICROWIN__
224 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
226 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
230 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
232 #if defined(__WXMICROWIN__)
234 wxDisplaySize(width
, height
);
236 // Determine the desktop dimensions minus the taskbar and any other
237 // special decorations...
240 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
243 if (width
) *width
= r
.right
- r
.left
;
244 if (height
) *height
= r
.bottom
- r
.top
;
248 // ---------------------------------------------------------------------------
249 // window information functions
250 // ---------------------------------------------------------------------------
252 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
258 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
259 ::GetWindowText((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
265 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
270 #ifndef __WXMICROWIN__
273 int len
= 256; // some starting value
277 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
281 // the class name might have been truncated, retry with larger
291 #endif // !__WXMICROWIN__
296 int WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
298 return ::GetWindowLong((HWND
)hWnd
, GWL_ID
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
309 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
310 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
311 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
312 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
314 *x
*= (iWidthMM
* 100);
316 *y
*= (iHeightMM
* 100);
320 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
324 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
325 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
326 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
327 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
330 *x
/= (iWidthMM
* 100);
332 *y
/= (iHeightMM
* 100);
335 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
343 Polyline(hdc
, points
, 2);
345 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);
350 // ----------------------------------------------------------------------------
351 // Shell API wrappers
352 // ----------------------------------------------------------------------------
354 extern bool wxEnableFileNameAutoComplete(HWND hwnd
)
356 #if wxUSE_DYNLIB_CLASS
357 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
359 static SHAutoComplete_t s_pfnSHAutoComplete
= NULL
;
360 static bool s_initialized
= false;
362 if ( !s_initialized
)
364 s_initialized
= true;
367 wxDynamicLibrary
dll(_T("shlwapi.dll"));
368 if ( dll
.IsLoaded() )
370 s_pfnSHAutoComplete
=
371 (SHAutoComplete_t
)dll
.GetSymbol(_T("SHAutoComplete"));
372 if ( s_pfnSHAutoComplete
)
374 // won't be unloaded until the process termination, no big deal
380 if ( !s_pfnSHAutoComplete
)
383 HRESULT hr
= s_pfnSHAutoComplete(hwnd
, 0x10 /* SHACF_FILESYS_ONLY */);
386 wxLogApiError(_T("SHAutoComplete"), hr
);
394 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS