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(), resourceName
, resourceType
);
111 if ( hResource
== 0 )
114 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
118 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
122 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
123 // so we need to find the length of the resource.
124 int len
= ::SizeofResource(wxGetInstance(), hResource
);
125 wxChar
*s
= new wxChar
[len
+1];
126 wxStrncpy(s
,theText
,len
);
131 UnlockResource(hData
);
135 // GlobalFree(hData);
139 #endif // __WXMICROWIN__
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 // See also the wxGetMousePosition in window.cpp
146 // Deprecated: use wxPoint wxGetMousePosition() instead
147 void wxGetMousePosition( int* x
, int* y
)
150 GetCursorPos( & pt
);
155 // Return true if we have a colour display
156 bool wxColourDisplay()
158 #ifdef __WXMICROWIN__
162 // this function is called from wxDC ctor so it is called a *lot* of times
163 // hence we optimize it a bit but doing the check only once
165 // this should be MT safe as only the GUI thread (holding the GUI mutex)
167 static int s_isColour
= -1;
169 if ( s_isColour
== -1 )
172 int noCols
= ::GetDeviceCaps(dc
, NUMCOLORS
);
174 s_isColour
= (noCols
== -1) || (noCols
> 2);
177 return s_isColour
!= 0;
181 // Returns depth of screen
185 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
188 // Get size of display
189 void wxDisplaySize(int *width
, int *height
)
191 #ifdef __WXMICROWIN__
193 HWND hWnd
= GetDesktopWindow();
194 ::GetWindowRect(hWnd
, & rect
);
197 *width
= rect
.right
- rect
.left
;
199 *height
= rect
.bottom
- rect
.top
;
200 #else // !__WXMICROWIN__
204 *width
= ::GetDeviceCaps(dc
, HORZRES
);
206 *height
= ::GetDeviceCaps(dc
, VERTRES
);
207 #endif // __WXMICROWIN__/!__WXMICROWIN__
210 void wxDisplaySizeMM(int *width
, int *height
)
212 #ifdef __WXMICROWIN__
222 *width
= ::GetDeviceCaps(dc
, HORZSIZE
);
224 *height
= ::GetDeviceCaps(dc
, VERTSIZE
);
228 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
230 #if defined(__WXMICROWIN__)
232 wxDisplaySize(width
, height
);
234 // Determine the desktop dimensions minus the taskbar and any other
235 // special decorations...
238 SystemParametersInfo(SPI_GETWORKAREA
, 0, &r
, 0);
241 if (width
) *width
= r
.right
- r
.left
;
242 if (height
) *height
= r
.bottom
- r
.top
;
246 // ---------------------------------------------------------------------------
247 // window information functions
248 // ---------------------------------------------------------------------------
250 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
256 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
257 ::GetWindowText((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
263 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
268 #ifndef __WXMICROWIN__
271 int len
= 256; // some starting value
275 int count
= ::GetClassName((HWND
)hWnd
, wxStringBuffer(str
, len
), len
);
279 // the class name might have been truncated, retry with larger
289 #endif // !__WXMICROWIN__
294 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
296 return (WXWORD
)GetWindowLong((HWND
)hWnd
, GWL_ID
);
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 extern void PixelToHIMETRIC(LONG
*x
, LONG
*y
)
307 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
308 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
309 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
310 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
312 *x
*= (iWidthMM
* 100);
314 *y
*= (iHeightMM
* 100);
318 extern void HIMETRICToPixel(LONG
*x
, LONG
*y
)
322 int iWidthMM
= GetDeviceCaps(hdcRef
, HORZSIZE
),
323 iHeightMM
= GetDeviceCaps(hdcRef
, VERTSIZE
),
324 iWidthPels
= GetDeviceCaps(hdcRef
, HORZRES
),
325 iHeightPels
= GetDeviceCaps(hdcRef
, VERTRES
);
328 *x
/= (iWidthMM
* 100);
330 *y
/= (iHeightMM
* 100);
333 void wxDrawLine(HDC hdc
, int x1
, int y1
, int x2
, int y2
)
341 Polyline(hdc
, points
, 2);
343 MoveToEx(hdc
, x1
, y1
, NULL
); LineTo((HDC
) hdc
, x2
, y2
);
348 // ----------------------------------------------------------------------------
349 // Shell API wrappers
350 // ----------------------------------------------------------------------------
352 extern bool wxEnableFileNameAutoComplete(HWND hwnd
)
354 #if wxUSE_DYNLIB_CLASS
355 typedef HRESULT (WINAPI
*SHAutoComplete_t
)(HWND
, DWORD
);
357 static SHAutoComplete_t s_pfnSHAutoComplete
= NULL
;
358 static bool s_initialized
= false;
360 if ( !s_initialized
)
362 s_initialized
= true;
365 wxDynamicLibrary
dll(_T("shlwapi.dll"));
366 if ( dll
.IsLoaded() )
368 s_pfnSHAutoComplete
=
369 (SHAutoComplete_t
)dll
.GetSymbol(_T("SHAutoComplete"));
370 if ( s_pfnSHAutoComplete
)
372 // won't be unloaded until the process termination, no big deal
378 if ( !s_pfnSHAutoComplete
)
381 HRESULT hr
= s_pfnSHAutoComplete(hwnd
, 0x10 /* SHACF_FILESYS_ONLY */);
384 wxLogApiError(_T("SHAutoComplete"), hr
);
392 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS