]> git.saurik.com Git - wxWidgets.git/blob - src/msw/display.cpp
Don't document wxSortedArrayString as deriving from wxArrayString.
[wxWidgets.git] / src / msw / display.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/display.cpp
3 // Purpose: MSW Implementation of wxDisplay class
4 // Author: Royce Mitchell III, Vadim Zeitlin
5 // Modified by: Ryan Norton (IsPrimary override)
6 // Created: 06/21/02
7 // Copyright: (c) wxWidgets team
8 // Copyright: (c) 2002-2006 wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
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
29 #include "wx/display.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dynarray.h"
33 #include "wx/app.h"
34 #include "wx/frame.h"
35 #endif
36
37 #include "wx/dynlib.h"
38 #include "wx/sysopt.h"
39
40 #include "wx/display_impl.h"
41 #include "wx/msw/wrapwin.h"
42 #include "wx/msw/missing.h"
43 #include "wx/msw/private.h"
44 #include "wx/msw/private/hiddenwin.h"
45
46 #ifndef __WXWINCE__
47 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
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);
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
68 #define MONITORINFOF_PRIMARY 0x00000001
69 #define HMONITOR_DECLARED
70 #endif
71 #endif // !__WXWINCE__
72
73 // display functions are found in different DLLs under WinCE and normal Win32
74 #ifdef __WXWINCE__
75 static const wxChar displayDllName[] = wxT("coredll.dll");
76 #else
77 static const wxChar displayDllName[] = wxT("user32.dll");
78 #endif
79
80 // ----------------------------------------------------------------------------
81 // typedefs for dynamically loaded Windows functions
82 // ----------------------------------------------------------------------------
83
84 typedef LONG (WINAPI *ChangeDisplaySettingsEx_t)(LPCTSTR lpszDeviceName,
85 LPDEVMODE lpDevMode,
86 HWND hwnd,
87 DWORD dwFlags,
88 LPVOID lParam);
89
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
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__
106
107
108 // ----------------------------------------------------------------------------
109 // wxDisplayMSW declaration
110 // ----------------------------------------------------------------------------
111
112 class wxDisplayMSW : public wxDisplayImpl
113 {
114 public:
115 wxDisplayMSW(unsigned n, HMONITOR hmon)
116 : wxDisplayImpl(n),
117 m_hmon(hmon)
118 {
119 }
120
121 virtual wxRect GetGeometry() const;
122 virtual wxRect GetClientArea() const;
123 virtual wxString GetName() const;
124 virtual bool IsPrimary() const;
125
126 virtual wxVideoMode GetCurrentMode() const;
127 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
128 virtual bool ChangeMode(const wxVideoMode& mode);
129
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
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
147 HMONITOR m_hmon;
148
149 private:
150 wxDECLARE_NO_COPY_CLASS(wxDisplayMSW);
151 };
152
153
154 // ----------------------------------------------------------------------------
155 // wxDisplayFactoryMSW declaration
156 // ----------------------------------------------------------------------------
157
158 WX_DEFINE_ARRAY(HMONITOR, wxMonitorHandleArray);
159
160 // functions dynamically bound by wxDisplayFactoryMSW ctor.
161 static MonitorFromPoint_t gs_MonitorFromPoint = NULL;
162 static MonitorFromWindow_t gs_MonitorFromWindow = NULL;
163 static GetMonitorInfo_t gs_GetMonitorInfo = NULL;
164 static EnumDisplayMonitors_t gs_EnumDisplayMonitors = NULL;
165
166 class wxDisplayFactoryMSW : public wxDisplayFactory
167 {
168 public:
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();
173
174 // Dtor destroys the hidden window we use for getting WM_SETTINGCHANGE.
175 virtual ~wxDisplayFactoryMSW();
176
177 bool IsOk() const { return !m_displays.empty(); }
178
179 virtual wxDisplayImpl *CreateDisplay(unsigned n);
180 virtual unsigned GetCount() { return unsigned(m_displays.size()); }
181 virtual int GetFromPoint(const wxPoint& pt);
182 virtual int GetFromWindow(const wxWindow *window);
183
184 // Called when we receive WM_SETTINGCHANGE to refresh the list of monitor
185 // handles.
186 static void RefreshMonitors() { ms_factory->DoRefreshMonitors(); }
187
188
189 private:
190 // EnumDisplayMonitors() callback
191 static BOOL CALLBACK MultimonEnumProc(HMONITOR hMonitor,
192 HDC hdcMonitor,
193 LPRECT lprcMonitor,
194 LPARAM dwData);
195
196 // find the monitor corresponding to the given handle,
197 // return wxNOT_FOUND if not found
198 int FindDisplayFromHMONITOR(HMONITOR hmon) const;
199
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
210 // the array containing information about all available displays, filled by
211 // MultimonEnumProc()
212 wxMonitorHandleArray m_displays;
213
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
219 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMSW);
220 };
221
222 wxDisplayFactoryMSW* wxDisplayFactoryMSW::ms_factory = NULL;
223
224 // ----------------------------------------------------------------------------
225 // wxDisplay implementation
226 // ----------------------------------------------------------------------------
227
228 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
229 {
230 wxDisplayFactoryMSW *factoryMM = new wxDisplayFactoryMSW;
231
232 if ( factoryMM->IsOk() )
233 return factoryMM;
234
235 delete factoryMM;
236
237 // fall back to a stub implementation if no multimon support (Win95?)
238 return new wxDisplayFactorySingle;
239 }
240
241
242 // ----------------------------------------------------------------------------
243 // wxDisplayMSW implementation
244 // ----------------------------------------------------------------------------
245
246 bool wxDisplayMSW::GetMonInfo(MONITORINFOEX& monInfo) const
247 {
248 if ( !gs_GetMonitorInfo(m_hmon, &monInfo) )
249 {
250 wxLogLastError(wxT("GetMonitorInfo"));
251 return false;
252 }
253
254 return true;
255 }
256
257 wxRect wxDisplayMSW::GetGeometry() const
258 {
259 WinStruct<MONITORINFOEX> monInfo;
260
261 wxRect rect;
262 if ( GetMonInfo(monInfo) )
263 wxCopyRECTToRect(monInfo.rcMonitor, rect);
264
265 return rect;
266 }
267
268 wxRect wxDisplayMSW::GetClientArea() const
269 {
270 WinStruct<MONITORINFOEX> monInfo;
271
272 wxRect rectClient;
273 if ( GetMonInfo(monInfo) )
274 wxCopyRECTToRect(monInfo.rcWork, rectClient);
275
276 return rectClient;
277 }
278
279 wxString wxDisplayMSW::GetName() const
280 {
281 WinStruct<MONITORINFOEX> monInfo;
282
283 wxString name;
284 if ( GetMonInfo(monInfo) )
285 name = monInfo.szDevice;
286
287 return name;
288 }
289
290 bool wxDisplayMSW::IsPrimary() const
291 {
292 WinStruct<MONITORINFOEX> monInfo;
293
294 if ( !GetMonInfo(monInfo) )
295 return false;
296
297 return (monInfo.dwFlags & MONITORINFOF_PRIMARY) != 0;
298 }
299
300 wxVideoMode wxDisplayMSW::GetCurrentMode() const
301 {
302 wxVideoMode mode;
303
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();
308 const wxChar * const deviceName = name.empty()
309 ? (const wxChar*)NULL
310 : (const wxChar*)name.c_str();
311
312 DEVMODE dm;
313 dm.dmSize = sizeof(dm);
314 dm.dmDriverExtra = 0;
315
316 if ( !::EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, &dm) )
317 {
318 wxLogLastError(wxT("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
319 }
320 else
321 {
322 mode = ConvertToVideoMode(dm);
323 }
324
325 return mode;
326 }
327
328 wxArrayVideoModes wxDisplayMSW::GetModes(const wxVideoMode& modeMatch) const
329 {
330 wxArrayVideoModes modes;
331
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();
336 const wxChar * const deviceName = name.empty()
337 ? (const wxChar*)NULL
338 : (const wxChar*)name.c_str();
339
340 DEVMODE dm;
341 dm.dmSize = sizeof(dm);
342 dm.dmDriverExtra = 0;
343
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;
356 }
357
358 bool wxDisplayMSW::ChangeMode(const wxVideoMode& mode)
359 {
360 // prepare ChangeDisplaySettingsEx() parameters
361 DEVMODE dm;
362 DEVMODE *pDevMode;
363
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 {
374 wxCHECK_MSG( mode.GetWidth() && mode.GetHeight(), false,
375 wxT("at least the width and height must be specified") );
376
377 wxZeroMemory(dm);
378 dm.dmSize = sizeof(dm);
379 dm.dmDriverExtra = 0;
380 dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
381 dm.dmPelsWidth = mode.GetWidth();
382 dm.dmPelsHeight = mode.GetHeight();
383
384 if ( mode.GetDepth() )
385 {
386 dm.dmFields |= DM_BITSPERPEL;
387 dm.dmBitsPerPel = mode.GetDepth();
388 }
389
390 if ( mode.GetRefresh() )
391 {
392 dm.dmFields |= DM_DISPLAYFREQUENCY;
393 dm.dmDisplayFrequency = mode.GetRefresh();
394 }
395
396 pDevMode = &dm;
397
398 #ifdef __WXWINCE__
399 flags = 0;
400 #else // !__WXWINCE__
401 flags = CDS_FULLSCREEN;
402 #endif // __WXWINCE__/!__WXWINCE__
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 {
413 wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET);
414 if ( dllDisplay.IsLoaded() )
415 {
416 wxDL_INIT_FUNC_AW(pfn, ChangeDisplaySettingsEx, dllDisplay);
417 }
418 //else: huh, no this DLL must always be present, what's going on??
419
420 #ifndef __WXWINCE__
421 if ( !pfnChangeDisplaySettingsEx )
422 {
423 // we must be under Win95 and so there is no multiple monitors
424 // support anyhow
425 pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95;
426 }
427 #endif // !__WXWINCE__
428 }
429
430 // do change the mode
431 switch ( pfnChangeDisplaySettingsEx
432 (
433 GetName().t_str(), // display name
434 pDevMode, // dev mode or NULL to reset
435 NULL, // reserved
436 flags,
437 NULL // pointer to video parameters (not used)
438 ) )
439 {
440 case DISP_CHANGE_SUCCESSFUL:
441 // ok
442 {
443 // If we have a top-level, full-screen frame, emulate
444 // the DirectX behaviour and resize it. This makes this
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();
451 frameTop->SetClientSize(current.GetWidth(), current.GetHeight());
452 }
453 }
454 return true;
455
456 case DISP_CHANGE_BADMODE:
457 // don't complain about this, this is the only "expected" error
458 break;
459
460 default:
461 wxFAIL_MSG( wxT("unexpected ChangeDisplaySettingsEx() return value") );
462 }
463
464 return false;
465 }
466
467
468 // ----------------------------------------------------------------------------
469 // wxDisplayFactoryMSW implementation
470 // ----------------------------------------------------------------------------
471
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
485 wxDisplayFactoryMSW::wxDisplayFactoryMSW()
486 {
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
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
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
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 {
610 #ifdef __WXMSW__
611 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window),
612 MONITOR_DEFAULTTONULL));
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
620 }
621
622 #endif // wxUSE_DISPLAY
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 }