]>
Commit | Line | Data |
---|---|---|
a536e411 | 1 | ///////////////////////////////////////////////////////////////////////////// |
2ad495fb | 2 | // Name: src/msw/display.cpp |
a536e411 | 3 | // Purpose: MSW Implementation of wxDisplay class |
fd921f35 VZ |
4 | // Author: Royce Mitchell III, Vadim Zeitlin |
5 | // Modified by: Ryan Norton (IsPrimary override) | |
a536e411 | 6 | // Created: 06/21/02 |
77ffb593 | 7 | // Copyright: (c) wxWidgets team |
fd921f35 | 8 | // Copyright: (c) 2002-2006 wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
a536e411 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
a536e411 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_DISPLAY | |
28 | ||
7212d155 PC |
29 | #include "wx/display.h" |
30 | ||
a536e411 | 31 | #ifndef WX_PRECOMP |
ad9835c9 WS |
32 | #include "wx/dynarray.h" |
33 | #include "wx/app.h" | |
34 | #include "wx/frame.h" | |
a536e411 JS |
35 | #endif |
36 | ||
2d748a33 | 37 | #include "wx/dynlib.h" |
ef1717a9 | 38 | #include "wx/sysopt.h" |
8585e2b5 | 39 | |
ef1717a9 | 40 | #include "wx/display_impl.h" |
52778e00 | 41 | #include "wx/msw/wrapwin.h" |
7212d155 | 42 | #include "wx/msw/missing.h" |
142ae7b3 | 43 | #include "wx/msw/private.h" |
ad6f09f5 | 44 | #include "wx/msw/private/hiddenwin.h" |
a536e411 | 45 | |
2ad495fb | 46 | #ifndef __WXWINCE__ |
01c54165 | 47 | // Older versions of windef.h don't define HMONITOR. Unfortunately, we |
bcc1153a VZ |
48 | // can't directly test whether HMONITOR is defined or not in windef.h as |
49 | // it's not a macro but a typedef, so we test for an unrelated symbol which | |
50 | // is only defined in winuser.h if WINVER >= 0x0500 | |
51 | #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK) | |
52 | DECLARE_HANDLE(HMONITOR); | |
e5570318 JS |
53 | typedef BOOL(CALLBACK * MONITORENUMPROC )(HMONITOR, HDC, LPRECT, LPARAM); |
54 | typedef struct tagMONITORINFO | |
55 | { | |
56 | DWORD cbSize; | |
57 | RECT rcMonitor; | |
58 | RECT rcWork; | |
59 | DWORD dwFlags; | |
60 | } MONITORINFO, *LPMONITORINFO; | |
61 | typedef struct tagMONITORINFOEX : public tagMONITORINFO | |
62 | { | |
63 | TCHAR szDevice[CCHDEVICENAME]; | |
64 | } MONITORINFOEX, *LPMONITORINFOEX; | |
65 | #define MONITOR_DEFAULTTONULL 0x00000000 | |
66 | #define MONITOR_DEFAULTTOPRIMARY 0x00000001 | |
67 | #define MONITOR_DEFAULTTONEAREST 0x00000002 | |
ad9835c9 | 68 | #define MONITORINFOF_PRIMARY 0x00000001 |
bcc1153a VZ |
69 | #define HMONITOR_DECLARED |
70 | #endif | |
2ad495fb | 71 | #endif // !__WXWINCE__ |
a536e411 | 72 | |
d238d403 VZ |
73 | // display functions are found in different DLLs under WinCE and normal Win32 |
74 | #ifdef __WXWINCE__ | |
9a83f860 | 75 | static const wxChar displayDllName[] = wxT("coredll.dll"); |
d238d403 | 76 | #else |
9a83f860 | 77 | static const wxChar displayDllName[] = wxT("user32.dll"); |
d238d403 VZ |
78 | #endif |
79 | ||
a536e411 | 80 | // ---------------------------------------------------------------------------- |
06efac1f | 81 | // typedefs for dynamically loaded Windows functions |
a536e411 JS |
82 | // ---------------------------------------------------------------------------- |
83 | ||
8585e2b5 VZ |
84 | typedef LONG (WINAPI *ChangeDisplaySettingsEx_t)(LPCTSTR lpszDeviceName, |
85 | LPDEVMODE lpDevMode, | |
86 | HWND hwnd, | |
87 | DWORD dwFlags, | |
88 | LPVOID lParam); | |
a536e411 | 89 | |
01c54165 VZ |
90 | typedef BOOL (WINAPI *EnumDisplayMonitors_t)(HDC,LPCRECT,MONITORENUMPROC,LPARAM); |
91 | typedef HMONITOR (WINAPI *MonitorFromPoint_t)(POINT,DWORD); | |
92 | typedef HMONITOR (WINAPI *MonitorFromWindow_t)(HWND,DWORD); | |
93 | typedef BOOL (WINAPI *GetMonitorInfo_t)(HMONITOR,LPMONITORINFO); | |
94 | ||
ef1717a9 VZ |
95 | #ifndef __WXWINCE__ |
96 | // emulation of ChangeDisplaySettingsEx() for Win95 | |
97 | LONG WINAPI ChangeDisplaySettingsExForWin95(LPCTSTR WXUNUSED(lpszDeviceName), | |
98 | LPDEVMODE lpDevMode, | |
99 | HWND WXUNUSED(hwnd), | |
100 | DWORD dwFlags, | |
101 | LPVOID WXUNUSED(lParam)) | |
102 | { | |
103 | return ::ChangeDisplaySettings(lpDevMode, dwFlags); | |
104 | } | |
105 | #endif // !__WXWINCE__ | |
06efac1f | 106 | |
a536e411 | 107 | |
06efac1f | 108 | // ---------------------------------------------------------------------------- |
d5947fad | 109 | // wxDisplayMSW declaration |
06efac1f VZ |
110 | // ---------------------------------------------------------------------------- |
111 | ||
d5947fad | 112 | class wxDisplayMSW : public wxDisplayImpl |
01c54165 | 113 | { |
ef1717a9 | 114 | public: |
d5947fad | 115 | wxDisplayMSW(unsigned n, HMONITOR hmon) |
ef1717a9 | 116 | : wxDisplayImpl(n), |
d5947fad | 117 | m_hmon(hmon) |
01c54165 | 118 | { |
01c54165 | 119 | } |
01c54165 | 120 | |
ef1717a9 | 121 | virtual wxRect GetGeometry() const; |
6c5d6291 | 122 | virtual wxRect GetClientArea() const; |
ef1717a9 VZ |
123 | virtual wxString GetName() const; |
124 | virtual bool IsPrimary() const; | |
06efac1f | 125 | |
ef1717a9 | 126 | virtual wxVideoMode GetCurrentMode() const; |
d5947fad VZ |
127 | virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const; |
128 | virtual bool ChangeMode(const wxVideoMode& mode); | |
06efac1f | 129 | |
ef1717a9 VZ |
130 | protected: |
131 | // convert a DEVMODE to our wxVideoMode | |
132 | static wxVideoMode ConvertToVideoMode(const DEVMODE& dm) | |
133 | { | |
134 | // note that dmDisplayFrequency may be 0 or 1 meaning "standard one" | |
135 | // and although 0 is ok for us we don't want to return modes with 1hz | |
136 | // refresh | |
137 | return wxVideoMode(dm.dmPelsWidth, | |
138 | dm.dmPelsHeight, | |
139 | dm.dmBitsPerPel, | |
140 | dm.dmDisplayFrequency > 1 ? dm.dmDisplayFrequency : 0); | |
141 | } | |
142 | ||
473ca5f3 VZ |
143 | // Call GetMonitorInfo() and fill in the provided struct and return true if |
144 | // it succeeded, otherwise return false. | |
145 | bool GetMonInfo(MONITORINFOEX& monInfo) const; | |
146 | ||
d5947fad VZ |
147 | HMONITOR m_hmon; |
148 | ||
149 | private: | |
150 | wxDECLARE_NO_COPY_CLASS(wxDisplayMSW); | |
ef1717a9 | 151 | }; |
a536e411 | 152 | |
d5947fad | 153 | |
8585e2b5 | 154 | // ---------------------------------------------------------------------------- |
d5947fad | 155 | // wxDisplayFactoryMSW declaration |
8585e2b5 VZ |
156 | // ---------------------------------------------------------------------------- |
157 | ||
d5947fad VZ |
158 | WX_DEFINE_ARRAY(HMONITOR, wxMonitorHandleArray); |
159 | ||
160 | // functions dynamically bound by wxDisplayFactoryMSW ctor. | |
ef1717a9 VZ |
161 | static MonitorFromPoint_t gs_MonitorFromPoint = NULL; |
162 | static MonitorFromWindow_t gs_MonitorFromWindow = NULL; | |
163 | static GetMonitorInfo_t gs_GetMonitorInfo = NULL; | |
d5947fad | 164 | static EnumDisplayMonitors_t gs_EnumDisplayMonitors = NULL; |
8585e2b5 | 165 | |
d5947fad | 166 | class wxDisplayFactoryMSW : public wxDisplayFactory |
ef1717a9 VZ |
167 | { |
168 | public: | |
d5947fad VZ |
169 | // ctor checks if the current system supports multimon API and dynamically |
170 | // bind the functions we need if this is the case and fills the | |
171 | // m_displays array if they're available | |
172 | wxDisplayFactoryMSW(); | |
8585e2b5 | 173 | |
ad6f09f5 VZ |
174 | // Dtor destroys the hidden window we use for getting WM_SETTINGCHANGE. |
175 | virtual ~wxDisplayFactoryMSW(); | |
176 | ||
ef1717a9 | 177 | bool IsOk() const { return !m_displays.empty(); } |
8585e2b5 | 178 | |
d5947fad | 179 | virtual wxDisplayImpl *CreateDisplay(unsigned n); |
4e675101 | 180 | virtual unsigned GetCount() { return unsigned(m_displays.size()); } |
ef1717a9 | 181 | virtual int GetFromPoint(const wxPoint& pt); |
1e93d595 | 182 | virtual int GetFromWindow(const wxWindow *window); |
a536e411 | 183 | |
ad6f09f5 VZ |
184 | // Called when we receive WM_SETTINGCHANGE to refresh the list of monitor |
185 | // handles. | |
186 | static void RefreshMonitors() { ms_factory->DoRefreshMonitors(); } | |
187 | ||
188 | ||
ef1717a9 VZ |
189 | private: |
190 | // EnumDisplayMonitors() callback | |
191 | static BOOL CALLBACK MultimonEnumProc(HMONITOR hMonitor, | |
192 | HDC hdcMonitor, | |
193 | LPRECT lprcMonitor, | |
194 | LPARAM dwData); | |
06efac1f | 195 | |
d5947fad VZ |
196 | // find the monitor corresponding to the given handle, |
197 | // return wxNOT_FOUND if not found | |
198 | int FindDisplayFromHMONITOR(HMONITOR hmon) const; | |
06efac1f | 199 | |
ad6f09f5 VZ |
200 | // Update m_displays array, used by RefreshMonitors(). |
201 | void DoRefreshMonitors(); | |
202 | ||
203 | ||
204 | // The unique factory being used (as we don't have direct access to the | |
205 | // global factory pointer in the common code so we just duplicate this | |
206 | // variable (also making it of correct type for us) here). | |
207 | static wxDisplayFactoryMSW* ms_factory; | |
208 | ||
209 | ||
d5947fad VZ |
210 | // the array containing information about all available displays, filled by |
211 | // MultimonEnumProc() | |
212 | wxMonitorHandleArray m_displays; | |
213 | ||
ad6f09f5 VZ |
214 | // The hidden window we use for receiving WM_SETTINGCHANGE and its class |
215 | // name. | |
216 | HWND m_hiddenHwnd; | |
217 | const wxChar* m_hiddenClass; | |
218 | ||
d5947fad VZ |
219 | wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMSW); |
220 | }; | |
8585e2b5 | 221 | |
ad6f09f5 | 222 | wxDisplayFactoryMSW* wxDisplayFactoryMSW::ms_factory = NULL; |
a536e411 | 223 | |
ef1717a9 | 224 | // ---------------------------------------------------------------------------- |
d5947fad | 225 | // wxDisplay implementation |
ef1717a9 VZ |
226 | // ---------------------------------------------------------------------------- |
227 | ||
228 | /* static */ wxDisplayFactory *wxDisplay::CreateFactory() | |
a536e411 | 229 | { |
d5947fad | 230 | wxDisplayFactoryMSW *factoryMM = new wxDisplayFactoryMSW; |
16e45865 | 231 | |
ef1717a9 VZ |
232 | if ( factoryMM->IsOk() ) |
233 | return factoryMM; | |
a536e411 | 234 | |
ef1717a9 VZ |
235 | delete factoryMM; |
236 | ||
d5947fad | 237 | // fall back to a stub implementation if no multimon support (Win95?) |
ef1717a9 | 238 | return new wxDisplayFactorySingle; |
8585e2b5 | 239 | } |
a536e411 | 240 | |
16e45865 | 241 | |
ef1717a9 | 242 | // ---------------------------------------------------------------------------- |
d5947fad | 243 | // wxDisplayMSW implementation |
ef1717a9 VZ |
244 | // ---------------------------------------------------------------------------- |
245 | ||
473ca5f3 | 246 | bool wxDisplayMSW::GetMonInfo(MONITORINFOEX& monInfo) const |
8585e2b5 | 247 | { |
473ca5f3 | 248 | if ( !gs_GetMonitorInfo(m_hmon, &monInfo) ) |
d5947fad | 249 | { |
473ca5f3 VZ |
250 | wxLogLastError(wxT("GetMonitorInfo")); |
251 | return false; | |
ef1717a9 | 252 | } |
8585e2b5 | 253 | |
473ca5f3 VZ |
254 | return true; |
255 | } | |
256 | ||
257 | wxRect wxDisplayMSW::GetGeometry() const | |
258 | { | |
259 | WinStruct<MONITORINFOEX> monInfo; | |
260 | ||
d5947fad | 261 | wxRect rect; |
473ca5f3 VZ |
262 | if ( GetMonInfo(monInfo) ) |
263 | wxCopyRECTToRect(monInfo.rcMonitor, rect); | |
06efac1f | 264 | |
d5947fad | 265 | return rect; |
ef1717a9 | 266 | } |
06efac1f | 267 | |
d5947fad | 268 | wxRect wxDisplayMSW::GetClientArea() const |
6c5d6291 | 269 | { |
d5947fad VZ |
270 | WinStruct<MONITORINFOEX> monInfo; |
271 | ||
d5947fad | 272 | wxRect rectClient; |
473ca5f3 VZ |
273 | if ( GetMonInfo(monInfo) ) |
274 | wxCopyRECTToRect(monInfo.rcWork, rectClient); | |
6c5d6291 | 275 | |
d5947fad | 276 | return rectClient; |
6c5d6291 VZ |
277 | } |
278 | ||
d5947fad | 279 | wxString wxDisplayMSW::GetName() const |
ef1717a9 | 280 | { |
d5947fad | 281 | WinStruct<MONITORINFOEX> monInfo; |
06efac1f | 282 | |
473ca5f3 VZ |
283 | wxString name; |
284 | if ( GetMonInfo(monInfo) ) | |
285 | name = monInfo.szDevice; | |
d5947fad | 286 | |
473ca5f3 | 287 | return name; |
ef1717a9 | 288 | } |
06efac1f | 289 | |
d5947fad | 290 | bool wxDisplayMSW::IsPrimary() const |
ef1717a9 | 291 | { |
d5947fad VZ |
292 | WinStruct<MONITORINFOEX> monInfo; |
293 | ||
473ca5f3 | 294 | if ( !GetMonInfo(monInfo) ) |
d5947fad | 295 | return false; |
06efac1f | 296 | |
d5947fad | 297 | return (monInfo.dwFlags & MONITORINFOF_PRIMARY) != 0; |
06efac1f VZ |
298 | } |
299 | ||
d5947fad | 300 | wxVideoMode wxDisplayMSW::GetCurrentMode() const |
06efac1f | 301 | { |
ef1717a9 | 302 | wxVideoMode mode; |
01c54165 | 303 | |
ef1717a9 VZ |
304 | // The first parameter of EnumDisplaySettings() must be NULL under Win95 |
305 | // according to MSDN. The version of GetName() we implement for Win95 | |
306 | // returns an empty string. | |
307 | const wxString name = GetName(); | |
c9f78968 VS |
308 | const wxChar * const deviceName = name.empty() |
309 | ? (const wxChar*)NULL | |
310 | : (const wxChar*)name.c_str(); | |
06efac1f | 311 | |
ef1717a9 VZ |
312 | DEVMODE dm; |
313 | dm.dmSize = sizeof(dm); | |
314 | dm.dmDriverExtra = 0; | |
d5947fad | 315 | |
ef1717a9 | 316 | if ( !::EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, &dm) ) |
06efac1f | 317 | { |
9a83f860 | 318 | wxLogLastError(wxT("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)")); |
06efac1f | 319 | } |
ef1717a9 VZ |
320 | else |
321 | { | |
322 | mode = ConvertToVideoMode(dm); | |
323 | } | |
324 | ||
325 | return mode; | |
06efac1f VZ |
326 | } |
327 | ||
d5947fad | 328 | wxArrayVideoModes wxDisplayMSW::GetModes(const wxVideoMode& modeMatch) const |
a536e411 | 329 | { |
8585e2b5 VZ |
330 | wxArrayVideoModes modes; |
331 | ||
01c54165 VZ |
332 | // The first parameter of EnumDisplaySettings() must be NULL under Win95 |
333 | // according to MSDN. The version of GetName() we implement for Win95 | |
334 | // returns an empty string. | |
335 | const wxString name = GetName(); | |
c9f78968 VS |
336 | const wxChar * const deviceName = name.empty() |
337 | ? (const wxChar*)NULL | |
338 | : (const wxChar*)name.c_str(); | |
8585e2b5 VZ |
339 | |
340 | DEVMODE dm; | |
01c54165 VZ |
341 | dm.dmSize = sizeof(dm); |
342 | dm.dmDriverExtra = 0; | |
d5947fad | 343 | |
8585e2b5 VZ |
344 | for ( int iModeNum = 0; |
345 | ::EnumDisplaySettings(deviceName, iModeNum, &dm); | |
346 | iModeNum++ ) | |
347 | { | |
348 | const wxVideoMode mode = ConvertToVideoMode(dm); | |
349 | if ( mode.Matches(modeMatch) ) | |
350 | { | |
351 | modes.Add(mode); | |
352 | } | |
353 | } | |
354 | ||
355 | return modes; | |
a536e411 JS |
356 | } |
357 | ||
d5947fad | 358 | bool wxDisplayMSW::ChangeMode(const wxVideoMode& mode) |
a536e411 | 359 | { |
8585e2b5 | 360 | // prepare ChangeDisplaySettingsEx() parameters |
61373fa8 WS |
361 | DEVMODE dm; |
362 | DEVMODE *pDevMode; | |
363 | ||
8585e2b5 VZ |
364 | int flags; |
365 | ||
366 | if ( mode == wxDefaultVideoMode ) | |
367 | { | |
368 | // reset the video mode to default | |
369 | pDevMode = NULL; | |
370 | flags = 0; | |
371 | } | |
372 | else // change to the given mode | |
373 | { | |
2edac25b | 374 | wxCHECK_MSG( mode.GetWidth() && mode.GetHeight(), false, |
9a83f860 | 375 | wxT("at least the width and height must be specified") ); |
8585e2b5 VZ |
376 | |
377 | wxZeroMemory(dm); | |
378 | dm.dmSize = sizeof(dm); | |
01c54165 | 379 | dm.dmDriverExtra = 0; |
8585e2b5 | 380 | dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; |
2edac25b RR |
381 | dm.dmPelsWidth = mode.GetWidth(); |
382 | dm.dmPelsHeight = mode.GetHeight(); | |
8585e2b5 | 383 | |
2edac25b | 384 | if ( mode.GetDepth() ) |
8585e2b5 VZ |
385 | { |
386 | dm.dmFields |= DM_BITSPERPEL; | |
2edac25b | 387 | dm.dmBitsPerPel = mode.GetDepth(); |
8585e2b5 VZ |
388 | } |
389 | ||
2edac25b | 390 | if ( mode.GetRefresh() ) |
8585e2b5 VZ |
391 | { |
392 | dm.dmFields |= DM_DISPLAYFREQUENCY; | |
2edac25b | 393 | dm.dmDisplayFrequency = mode.GetRefresh(); |
8585e2b5 VZ |
394 | } |
395 | ||
396 | pDevMode = &dm; | |
397 | ||
2ad495fb WS |
398 | #ifdef __WXWINCE__ |
399 | flags = 0; | |
400 | #else // !__WXWINCE__ | |
8585e2b5 | 401 | flags = CDS_FULLSCREEN; |
2ad495fb | 402 | #endif // __WXWINCE__/!__WXWINCE__ |
8585e2b5 VZ |
403 | } |
404 | ||
405 | ||
406 | // get pointer to the function dynamically | |
407 | // | |
408 | // we're only called from the main thread, so it's ok to use static | |
409 | // variable | |
410 | static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx = NULL; | |
411 | if ( !pfnChangeDisplaySettingsEx ) | |
412 | { | |
d238d403 VZ |
413 | wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET); |
414 | if ( dllDisplay.IsLoaded() ) | |
8585e2b5 | 415 | { |
d238d403 | 416 | wxDL_INIT_FUNC_AW(pfn, ChangeDisplaySettingsEx, dllDisplay); |
8585e2b5 | 417 | } |
d238d403 | 418 | //else: huh, no this DLL must always be present, what's going on?? |
8585e2b5 | 419 | |
2ad495fb | 420 | #ifndef __WXWINCE__ |
8585e2b5 VZ |
421 | if ( !pfnChangeDisplaySettingsEx ) |
422 | { | |
423 | // we must be under Win95 and so there is no multiple monitors | |
424 | // support anyhow | |
425 | pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95; | |
426 | } | |
2ad495fb | 427 | #endif // !__WXWINCE__ |
8585e2b5 VZ |
428 | } |
429 | ||
430 | // do change the mode | |
431 | switch ( pfnChangeDisplaySettingsEx | |
432 | ( | |
017dc06b | 433 | GetName().t_str(), // display name |
e0a050e3 VS |
434 | pDevMode, // dev mode or NULL to reset |
435 | NULL, // reserved | |
8585e2b5 | 436 | flags, |
e0a050e3 | 437 | NULL // pointer to video parameters (not used) |
8585e2b5 VZ |
438 | ) ) |
439 | { | |
440 | case DISP_CHANGE_SUCCESSFUL: | |
441 | // ok | |
920b9675 JS |
442 | { |
443 | // If we have a top-level, full-screen frame, emulate | |
4c51a665 | 444 | // the DirectX behaviour and resize it. This makes this |
920b9675 JS |
445 | // API quite a bit easier to use. |
446 | wxWindow *winTop = wxTheApp->GetTopWindow(); | |
447 | wxFrame *frameTop = wxDynamicCast(winTop, wxFrame); | |
448 | if (frameTop && frameTop->IsFullScreen()) | |
449 | { | |
450 | wxVideoMode current = GetCurrentMode(); | |
2edac25b | 451 | frameTop->SetClientSize(current.GetWidth(), current.GetHeight()); |
920b9675 JS |
452 | } |
453 | } | |
06efac1f | 454 | return true; |
8585e2b5 VZ |
455 | |
456 | case DISP_CHANGE_BADMODE: | |
457 | // don't complain about this, this is the only "expected" error | |
458 | break; | |
459 | ||
460 | default: | |
9a83f860 | 461 | wxFAIL_MSG( wxT("unexpected ChangeDisplaySettingsEx() return value") ); |
8585e2b5 VZ |
462 | } |
463 | ||
06efac1f VZ |
464 | return false; |
465 | } | |
466 | ||
d5947fad VZ |
467 | |
468 | // ---------------------------------------------------------------------------- | |
469 | // wxDisplayFactoryMSW implementation | |
470 | // ---------------------------------------------------------------------------- | |
471 | ||
ad6f09f5 VZ |
472 | LRESULT APIENTRY |
473 | wxDisplayWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) | |
474 | { | |
475 | if ( msg == WM_SETTINGCHANGE ) | |
476 | { | |
477 | wxDisplayFactoryMSW::RefreshMonitors(); | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
482 | return ::DefWindowProc(hwnd, msg, wParam, lParam); | |
483 | } | |
484 | ||
d5947fad VZ |
485 | wxDisplayFactoryMSW::wxDisplayFactoryMSW() |
486 | { | |
ad6f09f5 VZ |
487 | // This is not supposed to happen with the current code, the factory is |
488 | // implicitly a singleton. | |
489 | wxASSERT_MSG( !ms_factory, wxS("Using more than one factory?") ); | |
490 | ||
491 | ms_factory = this; | |
492 | ||
493 | m_hiddenHwnd = NULL; | |
494 | m_hiddenClass = NULL; | |
495 | ||
d5947fad VZ |
496 | if ( gs_MonitorFromPoint==NULL || gs_MonitorFromWindow==NULL |
497 | || gs_GetMonitorInfo==NULL || gs_EnumDisplayMonitors==NULL ) | |
498 | { | |
499 | // First initialization, or last initialization failed. | |
500 | wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET); | |
501 | ||
502 | wxDL_INIT_FUNC(gs_, MonitorFromPoint, dllDisplay); | |
503 | wxDL_INIT_FUNC(gs_, MonitorFromWindow, dllDisplay); | |
504 | wxDL_INIT_FUNC_AW(gs_, GetMonitorInfo, dllDisplay); | |
505 | wxDL_INIT_FUNC(gs_, EnumDisplayMonitors, dllDisplay); | |
506 | ||
507 | // we can safely let dllDisplay go out of scope, the DLL itself will | |
508 | // still remain loaded as all programs link to it statically anyhow | |
509 | } | |
510 | ||
511 | if ( gs_MonitorFromPoint==NULL || gs_MonitorFromWindow==NULL | |
512 | || gs_GetMonitorInfo==NULL || gs_EnumDisplayMonitors==NULL ) | |
513 | return; | |
514 | ||
ad6f09f5 VZ |
515 | DoRefreshMonitors(); |
516 | ||
517 | // Also create a hidden window to listen for WM_SETTINGCHANGE that we | |
518 | // receive when a monitor is added to or removed from the system as we must | |
519 | // refresh our monitor handles information then. | |
520 | m_hiddenHwnd = wxCreateHiddenWindow | |
521 | ( | |
522 | &m_hiddenClass, | |
523 | wxT("wxDisplayHiddenWindow"), | |
524 | wxDisplayWndProc | |
525 | ); | |
526 | } | |
527 | ||
528 | wxDisplayFactoryMSW::~wxDisplayFactoryMSW() | |
529 | { | |
530 | if ( m_hiddenHwnd ) | |
531 | { | |
532 | if ( !::DestroyWindow(m_hiddenHwnd) ) | |
533 | { | |
534 | wxLogLastError(wxT("DestroyWindow(wxDisplayHiddenWindow)")); | |
535 | } | |
536 | ||
537 | if ( m_hiddenClass ) | |
538 | { | |
539 | if ( !::UnregisterClass(m_hiddenClass, wxGetInstance()) ) | |
540 | { | |
541 | wxLogLastError(wxT("UnregisterClass(wxDisplayHiddenWindow)")); | |
542 | } | |
543 | } | |
544 | } | |
545 | ||
546 | ms_factory = NULL; | |
547 | } | |
548 | ||
549 | void wxDisplayFactoryMSW::DoRefreshMonitors() | |
550 | { | |
551 | m_displays.Clear(); | |
552 | ||
d5947fad VZ |
553 | if ( !gs_EnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) ) |
554 | { | |
555 | wxLogLastError(wxT("EnumDisplayMonitors")); | |
556 | } | |
557 | } | |
558 | ||
559 | /* static */ | |
560 | BOOL CALLBACK | |
561 | wxDisplayFactoryMSW::MultimonEnumProc( | |
562 | HMONITOR hMonitor, // handle to display monitor | |
563 | HDC WXUNUSED(hdcMonitor), // handle to monitor-appropriate device context | |
564 | LPRECT WXUNUSED(lprcMonitor), // pointer to monitor intersection rectangle | |
565 | LPARAM dwData) // data passed from EnumDisplayMonitors (this) | |
566 | { | |
567 | wxDisplayFactoryMSW *const self = (wxDisplayFactoryMSW *)dwData; | |
568 | ||
569 | self->m_displays.Add(hMonitor); | |
570 | ||
571 | // continue the enumeration | |
572 | return TRUE; | |
573 | } | |
574 | ||
575 | wxDisplayImpl *wxDisplayFactoryMSW::CreateDisplay(unsigned n) | |
576 | { | |
577 | wxCHECK_MSG( n < m_displays.size(), NULL, wxT("An invalid index was passed to wxDisplay") ); | |
578 | ||
579 | return new wxDisplayMSW(n, m_displays[n]); | |
580 | } | |
581 | ||
582 | // helper for GetFromPoint() and GetFromWindow() | |
583 | int wxDisplayFactoryMSW::FindDisplayFromHMONITOR(HMONITOR hmon) const | |
584 | { | |
585 | if ( hmon ) | |
586 | { | |
587 | const size_t count = m_displays.size(); | |
588 | for ( size_t n = 0; n < count; n++ ) | |
589 | { | |
590 | if ( hmon == m_displays[n] ) | |
591 | return n; | |
592 | } | |
593 | } | |
594 | ||
595 | return wxNOT_FOUND; | |
596 | } | |
597 | ||
598 | int wxDisplayFactoryMSW::GetFromPoint(const wxPoint& pt) | |
599 | { | |
600 | POINT pt2; | |
601 | pt2.x = pt.x; | |
602 | pt2.y = pt.y; | |
603 | ||
604 | return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2, | |
605 | MONITOR_DEFAULTTONULL)); | |
606 | } | |
607 | ||
608 | int wxDisplayFactoryMSW::GetFromWindow(const wxWindow *window) | |
609 | { | |
fa88bebe | 610 | #ifdef __WXMSW__ |
d5947fad VZ |
611 | return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window), |
612 | MONITOR_DEFAULTTONULL)); | |
fa88bebe VZ |
613 | #else |
614 | const wxSize halfsize = window->GetSize() / 2; | |
615 | wxPoint pt = window->GetScreenPosition(); | |
616 | pt.x += halfsize.x; | |
617 | pt.y += halfsize.y; | |
618 | return GetFromPoint(pt); | |
619 | #endif | |
d5947fad VZ |
620 | } |
621 | ||
8585e2b5 | 622 | #endif // wxUSE_DISPLAY |
5146904d VZ |
623 | |
624 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) | |
625 | { | |
626 | #if defined(__WXMICROWIN__) | |
627 | *x = 0; *y = 0; | |
628 | wxDisplaySize(width, height); | |
629 | #else | |
630 | // Determine the desktop dimensions minus the taskbar and any other | |
631 | // special decorations... | |
632 | RECT r; | |
633 | ||
634 | SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); | |
635 | if (x) *x = r.left; | |
636 | if (y) *y = r.top; | |
637 | if (width) *width = r.right - r.left; | |
638 | if (height) *height = r.bottom - r.top; | |
639 | #endif | |
640 | } |