wxDisplay cleanup/rewrite:
[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 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Copyright: (c) 2002-2006 wxWidgets team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ===========================================================================
14 // declarations
15 // ===========================================================================
16
17 // ---------------------------------------------------------------------------
18 // headers
19 // ---------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_DISPLAY
29
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/dynarray.h"
33 #include "wx/frame.h"
34 #endif
35
36 #include "wx/dynload.h"
37 #include "wx/sysopt.h"
38
39 #include "wx/display.h"
40 #include "wx/display_impl.h"
41
42 // Mingw's w32api headers don't include ddraw.h, though the user may have
43 // installed it. If using configure, we actually probe for ddraw.h there
44 // and set wxUSE_DIRECTDRAW. Otherwise, assume we don't have it if using
45 // the w32api headers, and that we do otherwise.
46 #if !defined HAVE_W32API_H && !defined HAVE_DDRAW_H
47 #define HAVE_DDRAW_H
48 #endif
49
50 // user may disable compilation of DirectDraw code by setting
51 // wxUSE_DIRECTDRAW to 0 in the makefile/project settings
52 #if defined(HAVE_DDRAW_H) && !defined(wxUSE_DIRECTDRAW)
53 #define wxUSE_DIRECTDRAW 1
54 #endif
55
56 #ifndef __WXWINCE__
57 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
58 // can't directly test whether HMONITOR is defined or not in windef.h as
59 // it's not a macro but a typedef, so we test for an unrelated symbol which
60 // is only defined in winuser.h if WINVER >= 0x0500
61 #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK)
62 DECLARE_HANDLE(HMONITOR);
63 #define HMONITOR_DECLARED
64 #endif
65 #endif // !__WXWINCE__
66
67 #ifdef wxUSE_DIRECTDRAW
68 #include <ddraw.h>
69
70 // we don't want to link with ddraw.lib which contains the real
71 // IID_IDirectDraw2 definition
72 const GUID wxIID_IDirectDraw2 =
73 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
74 #endif // wxUSE_DIRECTDRAW
75
76 // ----------------------------------------------------------------------------
77 // typedefs for dynamically loaded Windows functions
78 // ----------------------------------------------------------------------------
79
80 typedef LONG (WINAPI *ChangeDisplaySettingsEx_t)(LPCTSTR lpszDeviceName,
81 LPDEVMODE lpDevMode,
82 HWND hwnd,
83 DWORD dwFlags,
84 LPVOID lParam);
85
86 #ifdef wxUSE_DIRECTDRAW
87 typedef BOOL (PASCAL *DDEnumExCallback_t)(GUID *pGuid,
88 LPTSTR driverDescription,
89 LPTSTR driverName,
90 LPVOID lpContext,
91 HMONITOR hmon);
92
93 typedef HRESULT (WINAPI *DirectDrawEnumerateEx_t)(DDEnumExCallback_t lpCallback,
94 LPVOID lpContext,
95 DWORD dwFlags);
96
97 typedef HRESULT (WINAPI *DirectDrawCreate_t)(GUID *lpGUID,
98 LPDIRECTDRAW *lplpDD,
99 IUnknown *pUnkOuter);
100 #endif // wxUSE_DIRECTDRAW
101
102 typedef BOOL (WINAPI *EnumDisplayMonitors_t)(HDC,LPCRECT,MONITORENUMPROC,LPARAM);
103 typedef HMONITOR (WINAPI *MonitorFromPoint_t)(POINT,DWORD);
104 typedef HMONITOR (WINAPI *MonitorFromWindow_t)(HWND,DWORD);
105 typedef BOOL (WINAPI *GetMonitorInfo_t)(HMONITOR,LPMONITORINFO);
106
107 #ifndef __WXWINCE__
108 // emulation of ChangeDisplaySettingsEx() for Win95
109 LONG WINAPI ChangeDisplaySettingsExForWin95(LPCTSTR WXUNUSED(lpszDeviceName),
110 LPDEVMODE lpDevMode,
111 HWND WXUNUSED(hwnd),
112 DWORD dwFlags,
113 LPVOID WXUNUSED(lParam))
114 {
115 return ::ChangeDisplaySettings(lpDevMode, dwFlags);
116 }
117 #endif // !__WXWINCE__
118
119 // ----------------------------------------------------------------------------
120 // display information classes
121 // ----------------------------------------------------------------------------
122
123 struct wxDisplayInfo
124 {
125 wxDisplayInfo(HMONITOR hmon = NULL)
126 {
127 m_hmon = hmon;
128 m_flags = (DWORD)-1;
129 }
130
131 virtual ~wxDisplayInfo() { }
132
133
134 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
135 // hadn't been done before)
136 void Initialize();
137
138
139 // handle of this monitor used by MonitorXXX() functions, never NULL
140 HMONITOR m_hmon;
141
142 // the entire area of this monitor in virtual screen coordinates
143 wxRect m_rect;
144
145 // the display device name for this monitor, empty initially and retrieved
146 // on demand by DoGetName()
147 wxString m_devName;
148
149 // the flags of this monitor, also used as initialization marker: if this
150 // is -1, GetMonitorInfo() hadn't been called yet
151 DWORD m_flags;
152 };
153
154 WX_DEFINE_ARRAY_PTR(wxDisplayInfo *, wxDisplayInfoArray);
155
156 // ----------------------------------------------------------------------------
157 // common base class for all Win32 wxDisplayImpl versions
158 // ----------------------------------------------------------------------------
159
160 class wxDisplayImplWin32Base : public wxDisplayImpl
161 {
162 public:
163 wxDisplayImplWin32Base(size_t n, wxDisplayInfo& info)
164 : wxDisplayImpl(n),
165 m_info(info)
166 {
167 }
168
169 virtual wxRect GetGeometry() const;
170 virtual wxString GetName() const;
171 virtual bool IsPrimary() const;
172
173 virtual wxVideoMode GetCurrentMode() const;
174
175 protected:
176 // convert a DEVMODE to our wxVideoMode
177 static wxVideoMode ConvertToVideoMode(const DEVMODE& dm)
178 {
179 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
180 // and although 0 is ok for us we don't want to return modes with 1hz
181 // refresh
182 return wxVideoMode(dm.dmPelsWidth,
183 dm.dmPelsHeight,
184 dm.dmBitsPerPel,
185 dm.dmDisplayFrequency > 1 ? dm.dmDisplayFrequency : 0);
186 }
187
188 wxDisplayInfo& m_info;
189 };
190
191 // ----------------------------------------------------------------------------
192 // common base class for all Win32 wxDisplayFactory versions
193 // ----------------------------------------------------------------------------
194
195 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
196 static MonitorFromPoint_t gs_MonitorFromPoint = NULL;
197 static MonitorFromWindow_t gs_MonitorFromWindow = NULL;
198 static GetMonitorInfo_t gs_GetMonitorInfo = NULL;
199
200 class wxDisplayFactoryWin32Base : public wxDisplayFactory
201 {
202 public:
203 virtual ~wxDisplayFactoryWin32Base();
204
205 bool IsOk() const { return !m_displays.empty(); }
206
207 virtual size_t GetCount() { return m_displays.size(); }
208 virtual int GetFromPoint(const wxPoint& pt);
209 virtual int GetFromWindow(wxWindow *window);
210
211 protected:
212 // ctor checks if the current system supports multimon API and dynamically
213 // bind the functions we need if this is the case and sets
214 // ms_supportsMultimon if they're available
215 wxDisplayFactoryWin32Base();
216
217 // delete all m_displays elements: can be called from the derived class
218 // dtor if it is important to do this before destroying it (as in
219 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
220 void Clear();
221
222 // find the monitor corresponding to the given handle, return wxNOT_FOUND
223 // if not found
224 int FindDisplayFromHMONITOR(HMONITOR hmon) const;
225
226
227 // flag indicating whether gs_MonitorXXX functions are available
228 static int ms_supportsMultimon;
229
230 // the array containing information about all available displays, should be
231 // filled by the derived class ctors
232 wxDisplayInfoArray m_displays;
233
234
235 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base)
236 };
237
238 // ----------------------------------------------------------------------------
239 // wxDisplay implementation using Windows multi-monitor support functions
240 // ----------------------------------------------------------------------------
241
242 class wxDisplayImplMultimon : public wxDisplayImplWin32Base
243 {
244 public:
245 wxDisplayImplMultimon(size_t n, wxDisplayInfo& info)
246 : wxDisplayImplWin32Base(n, info)
247 {
248 }
249
250 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
251 virtual bool ChangeMode(const wxVideoMode& mode);
252
253 private:
254 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon)
255 };
256
257 class WXDLLEXPORT wxDisplayFactoryMultimon : public wxDisplayFactoryWin32Base
258 {
259 public:
260 wxDisplayFactoryMultimon();
261
262 virtual wxDisplayImpl *CreateDisplay(size_t n);
263
264 private:
265 // EnumDisplayMonitors() callback
266 static BOOL CALLBACK MultimonEnumProc(HMONITOR hMonitor,
267 HDC hdcMonitor,
268 LPRECT lprcMonitor,
269 LPARAM dwData);
270
271
272 // add a monitor description to m_displays array
273 void AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor);
274 };
275
276 // ----------------------------------------------------------------------------
277 // wxDisplay implementation using DirectDraw
278 // ----------------------------------------------------------------------------
279
280 #ifdef wxUSE_DIRECTDRAW
281
282 struct wxDisplayInfoDirectDraw : wxDisplayInfo
283 {
284 wxDisplayInfoDirectDraw(const GUID& guid, HMONITOR hmon, LPTSTR name)
285 : wxDisplayInfo(hmon),
286 m_guid(guid)
287 {
288 m_pDD2 = NULL;
289 m_devName = name;
290 }
291
292 virtual ~wxDisplayInfoDirectDraw()
293 {
294 if ( m_pDD2 )
295 m_pDD2->Release();
296 }
297
298
299 // IDirectDraw object used to control this display, may be NULL
300 IDirectDraw2 *m_pDD2;
301
302 // DirectDraw GUID for this display, only valid when using DirectDraw
303 const GUID m_guid;
304
305
306 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw)
307 };
308
309 class wxDisplayImplDirectDraw : public wxDisplayImplWin32Base
310 {
311 public:
312 wxDisplayImplDirectDraw(size_t n, wxDisplayInfo& info, IDirectDraw2 *pDD2)
313 : wxDisplayImplWin32Base(n, info),
314 m_pDD2(pDD2)
315 {
316 m_pDD2->AddRef();
317 }
318
319 virtual ~wxDisplayImplDirectDraw()
320 {
321 m_pDD2->Release();
322 }
323
324 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
325 virtual bool ChangeMode(const wxVideoMode& mode);
326
327 private:
328 IDirectDraw2 *m_pDD2;
329
330 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw)
331 };
332
333 class WXDLLEXPORT wxDisplayFactoryDirectDraw : public wxDisplayFactoryWin32Base
334 {
335 public:
336 wxDisplayFactoryDirectDraw();
337 virtual ~wxDisplayFactoryDirectDraw();
338
339 virtual wxDisplayImpl *CreateDisplay(size_t n);
340
341 private:
342 // callback used with DirectDrawEnumerateEx()
343 static BOOL WINAPI DDEnumExCallback(GUID *pGuid,
344 LPTSTR driverDescription,
345 LPTSTR driverName,
346 LPVOID lpContext,
347 HMONITOR hmon);
348
349 // add a monitor description to m_displays array
350 void AddDisplay(const GUID& guid, HMONITOR hmon, LPTSTR name);
351
352
353 // ddraw.dll
354 wxDynamicLibrary m_dllDDraw;
355
356 // dynamically resolved DirectDrawCreate()
357 DirectDrawCreate_t m_pfnDirectDrawCreate;
358
359 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw)
360 };
361
362 #endif // wxUSE_DIRECTDRAW
363
364
365 // ============================================================================
366 // common classes implementation
367 // ============================================================================
368
369 // ----------------------------------------------------------------------------
370 // wxDisplay
371 // ----------------------------------------------------------------------------
372
373 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
374 {
375 // we have 2 implementations for modern Windows: one using standard Win32
376 // and another using DirectDraw, the choice between them is done using a
377 // system option
378 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
379 {
380 wxDisplayFactoryDirectDraw *factoryDD = new wxDisplayFactoryDirectDraw;
381 if ( factoryDD->IsOk() )
382 return factoryDD;
383
384 delete factoryDD;
385 }
386
387 wxDisplayFactoryMultimon *factoryMM = new wxDisplayFactoryMultimon;
388 if ( factoryMM->IsOk() )
389 return factoryMM;
390
391 delete factoryMM;
392
393
394 // finally fall back to a stub implementation if all else failed (Win95?)
395 return new wxDisplayFactorySingle;
396 }
397
398 // ----------------------------------------------------------------------------
399 // wxDisplayInfo
400 // ----------------------------------------------------------------------------
401
402 void wxDisplayInfo::Initialize()
403 {
404 if ( m_flags == (DWORD)-1 )
405 {
406 WinStruct<MONITORINFOEX> monInfo;
407 if ( !gs_GetMonitorInfo(m_hmon, (LPMONITORINFO)&monInfo) )
408 {
409 wxLogLastError(_T("GetMonitorInfo"));
410 m_flags = 0;
411 return;
412 }
413
414 wxCopyRECTToRect(monInfo.rcMonitor, m_rect);
415 m_devName = monInfo.szDevice;
416 m_flags = monInfo.dwFlags;
417 }
418 }
419
420 // ----------------------------------------------------------------------------
421 // wxDisplayImplWin32Base
422 // ----------------------------------------------------------------------------
423
424 wxRect wxDisplayImplWin32Base::GetGeometry() const
425 {
426 if ( m_info.m_rect.IsEmpty() )
427 m_info.Initialize();
428
429 return m_info.m_rect;
430 }
431
432 wxString wxDisplayImplWin32Base::GetName() const
433 {
434 if ( m_info.m_devName.IsEmpty() )
435 m_info.Initialize();
436
437 return m_info.m_devName;
438 }
439
440 bool wxDisplayImplWin32Base::IsPrimary() const
441 {
442 if ( m_info.m_flags == (DWORD)-1 )
443 m_info.Initialize();
444
445 return (m_info.m_flags & MONITORINFOF_PRIMARY) != 0;
446 }
447
448 wxVideoMode wxDisplayImplWin32Base::GetCurrentMode() const
449 {
450 wxVideoMode mode;
451
452 // The first parameter of EnumDisplaySettings() must be NULL under Win95
453 // according to MSDN. The version of GetName() we implement for Win95
454 // returns an empty string.
455 const wxString name = GetName();
456 const wxChar * const deviceName = name.empty() ? NULL : name.c_str();
457
458 DEVMODE dm;
459 dm.dmSize = sizeof(dm);
460 dm.dmDriverExtra = 0;
461 if ( !::EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, &dm) )
462 {
463 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
464 }
465 else
466 {
467 mode = ConvertToVideoMode(dm);
468 }
469
470 return mode;
471 }
472
473 // ----------------------------------------------------------------------------
474 // wxDisplayFactoryWin32Base
475 // ----------------------------------------------------------------------------
476
477 int wxDisplayFactoryWin32Base::ms_supportsMultimon = -1;
478
479 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
480 {
481 if ( ms_supportsMultimon == -1 )
482 {
483 ms_supportsMultimon = 0;
484
485 wxDynamicLibrary dllUser32(_T("user32.dll"));
486
487 wxLogNull noLog;
488
489 gs_MonitorFromPoint = (MonitorFromPoint_t)
490 dllUser32.GetSymbol(wxT("MonitorFromPoint"));
491 if ( !gs_MonitorFromPoint )
492 return;
493
494 gs_MonitorFromWindow = (MonitorFromWindow_t)
495 dllUser32.GetSymbol(wxT("MonitorFromWindow"));
496 if ( !gs_MonitorFromWindow )
497 return;
498
499 gs_GetMonitorInfo = (GetMonitorInfo_t)
500 dllUser32.GetSymbolAorW(wxT("GetMonitorInfo"));
501 if ( !gs_GetMonitorInfo )
502 return;
503
504 ms_supportsMultimon = 1;
505
506 // we can safely let dllUser32 go out of scope, the DLL itself will
507 // still remain loaded as all Win32 programs use it
508 }
509 }
510
511 void wxDisplayFactoryWin32Base::Clear()
512 {
513 WX_CLEAR_ARRAY(m_displays);
514 }
515
516 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
517 {
518 Clear();
519 }
520
521 // helper for GetFromPoint() and GetFromWindow()
522 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon) const
523 {
524 if ( hmon )
525 {
526 const size_t count = m_displays.size();
527 for ( size_t n = 0; n < count; n++ )
528 {
529 if ( hmon == m_displays[n]->m_hmon )
530 return n;
531 }
532 }
533
534 return wxNOT_FOUND;
535 }
536
537 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint& pt)
538 {
539 POINT pt2;
540 pt2.x = pt.x;
541 pt2.y = pt.y;
542
543 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2,
544 MONITOR_DEFAULTTONULL));
545 }
546
547 int wxDisplayFactoryWin32Base::GetFromWindow(wxWindow *window)
548 {
549 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window),
550 MONITOR_DEFAULTTONULL));
551 }
552
553 // ============================================================================
554 // wxDisplay implementation using Win32 multimon API
555 // ============================================================================
556
557 // ----------------------------------------------------------------------------
558 // wxDisplayFactoryMultimon initialization
559 // ----------------------------------------------------------------------------
560
561 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
562 {
563 if ( !ms_supportsMultimon )
564 return;
565
566 // look up EnumDisplayMonitors() which we don't need with DirectDraw
567 // implementation
568 EnumDisplayMonitors_t pfnEnumDisplayMonitors;
569 {
570 wxLogNull noLog;
571
572 wxDynamicLibrary dllUser32(_T("user32.dll"));
573 pfnEnumDisplayMonitors = (EnumDisplayMonitors_t)
574 dllUser32.GetSymbol(wxT("EnumDisplayMonitors"));
575 if ( !pfnEnumDisplayMonitors )
576 return;
577 }
578
579 // enumerate all displays
580 if ( !pfnEnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) )
581 {
582 wxLogLastError(wxT("EnumDisplayMonitors"));
583 }
584 }
585
586 /* static */
587 BOOL CALLBACK
588 wxDisplayFactoryMultimon::MultimonEnumProc(
589 HMONITOR hMonitor, // handle to display monitor
590 HDC WXUNUSED(hdcMonitor), // handle to monitor-appropriate device context
591 LPRECT lprcMonitor, // pointer to monitor intersection rectangle
592 LPARAM dwData // data passed from EnumDisplayMonitors (this)
593 )
594 {
595 wxDisplayFactoryMultimon *const self = (wxDisplayFactoryMultimon *)dwData;
596 self->AddDisplay(hMonitor, lprcMonitor);
597
598 // continue the enumeration
599 return TRUE;
600 }
601
602 // ----------------------------------------------------------------------------
603 // wxDisplayFactoryMultimon helper functions
604 // ----------------------------------------------------------------------------
605
606 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor)
607 {
608 wxDisplayInfo *info = new wxDisplayInfo(hMonitor);
609
610 // we also store the display geometry
611 info->m_rect = wxRect(lprcMonitor->left, lprcMonitor->top,
612 lprcMonitor->right - lprcMonitor->left,
613 lprcMonitor->bottom - lprcMonitor->top);
614
615 // now add this monitor to the array
616 m_displays.Add(info);
617 }
618
619 // ----------------------------------------------------------------------------
620 // wxDisplayFactoryMultimon inherited pure virtuals implementation
621 // ----------------------------------------------------------------------------
622
623 wxDisplayImpl *wxDisplayFactoryMultimon::CreateDisplay(size_t n)
624 {
625 wxCHECK_MSG( n < m_displays.size(), NULL, _T("invalid display index") );
626
627 return new wxDisplayImplMultimon(n, *(m_displays[n]));
628 }
629
630 // ----------------------------------------------------------------------------
631 // wxDisplayImplMultimon implementation
632 // ----------------------------------------------------------------------------
633
634 wxArrayVideoModes
635 wxDisplayImplMultimon::GetModes(const wxVideoMode& modeMatch) const
636 {
637 wxArrayVideoModes modes;
638
639 // The first parameter of EnumDisplaySettings() must be NULL under Win95
640 // according to MSDN. The version of GetName() we implement for Win95
641 // returns an empty string.
642 const wxString name = GetName();
643 const wxChar * const deviceName = name.empty() ? NULL : name.c_str();
644
645 DEVMODE dm;
646 dm.dmSize = sizeof(dm);
647 dm.dmDriverExtra = 0;
648 for ( int iModeNum = 0;
649 ::EnumDisplaySettings(deviceName, iModeNum, &dm);
650 iModeNum++ )
651 {
652 const wxVideoMode mode = ConvertToVideoMode(dm);
653 if ( mode.Matches(modeMatch) )
654 {
655 modes.Add(mode);
656 }
657 }
658
659 return modes;
660 }
661
662 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode& mode)
663 {
664 // prepare ChangeDisplaySettingsEx() parameters
665 DEVMODE dm;
666 DEVMODE *pDevMode;
667
668 int flags;
669
670 if ( mode == wxDefaultVideoMode )
671 {
672 // reset the video mode to default
673 pDevMode = NULL;
674 flags = 0;
675 }
676 else // change to the given mode
677 {
678 wxCHECK_MSG( mode.w && mode.h, false,
679 _T("at least the width and height must be specified") );
680
681 wxZeroMemory(dm);
682 dm.dmSize = sizeof(dm);
683 dm.dmDriverExtra = 0;
684 dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
685 dm.dmPelsWidth = mode.w;
686 dm.dmPelsHeight = mode.h;
687
688 if ( mode.bpp )
689 {
690 dm.dmFields |= DM_BITSPERPEL;
691 dm.dmBitsPerPel = mode.bpp;
692 }
693
694 if ( mode.refresh )
695 {
696 dm.dmFields |= DM_DISPLAYFREQUENCY;
697 dm.dmDisplayFrequency = mode.refresh;
698 }
699
700 pDevMode = &dm;
701
702 #ifdef __WXWINCE__
703 flags = 0;
704 #else // !__WXWINCE__
705 flags = CDS_FULLSCREEN;
706 #endif // __WXWINCE__/!__WXWINCE__
707 }
708
709
710 // get pointer to the function dynamically
711 //
712 // we're only called from the main thread, so it's ok to use static
713 // variable
714 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx = NULL;
715 if ( !pfnChangeDisplaySettingsEx )
716 {
717 wxDynamicLibrary dllUser32(_T("user32.dll"));
718 if ( dllUser32.IsLoaded() )
719 {
720 pfnChangeDisplaySettingsEx = (ChangeDisplaySettingsEx_t)
721 dllUser32.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
722 }
723 //else: huh, no user32.dll??
724
725 #ifndef __WXWINCE__
726 if ( !pfnChangeDisplaySettingsEx )
727 {
728 // we must be under Win95 and so there is no multiple monitors
729 // support anyhow
730 pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95;
731 }
732 #endif // !__WXWINCE__
733 }
734
735 // do change the mode
736 switch ( pfnChangeDisplaySettingsEx
737 (
738 GetName(), // display name
739 pDevMode, // dev mode or NULL to reset
740 NULL, // reserved
741 flags,
742 NULL // pointer to video parameters (not used)
743 ) )
744 {
745 case DISP_CHANGE_SUCCESSFUL:
746 // ok
747 {
748 // If we have a top-level, full-screen frame, emulate
749 // the DirectX behavior and resize it. This makes this
750 // API quite a bit easier to use.
751 wxWindow *winTop = wxTheApp->GetTopWindow();
752 wxFrame *frameTop = wxDynamicCast(winTop, wxFrame);
753 if (frameTop && frameTop->IsFullScreen())
754 {
755 wxVideoMode current = GetCurrentMode();
756 frameTop->SetClientSize(current.w, current.h);
757 }
758 }
759 return true;
760
761 case DISP_CHANGE_BADMODE:
762 // don't complain about this, this is the only "expected" error
763 break;
764
765 default:
766 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
767 }
768
769 return false;
770 }
771
772
773 // ============================================================================
774 // DirectDraw-based wxDisplay implementation
775 // ============================================================================
776
777 #if wxUSE_DIRECTDRAW
778
779 // ----------------------------------------------------------------------------
780 // wxDisplayFactoryDirectDraw initialization
781 // ----------------------------------------------------------------------------
782
783 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
784 {
785 if ( !ms_supportsMultimon )
786 return;
787
788 #if wxUSE_LOG
789 // suppress the errors if ddraw.dll is not found, we're prepared to handle
790 // this
791 wxLogNull noLog;
792 #endif
793
794 m_dllDDraw.Load(_T("ddraw.dll"));
795
796 if ( !m_dllDDraw.IsLoaded() )
797 return;
798
799 DirectDrawEnumerateEx_t pDDEnumEx = (DirectDrawEnumerateEx_t)
800 m_dllDDraw.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
801 if ( !pDDEnumEx )
802 return;
803
804 // we can't continue without DirectDrawCreate() later, so resolve it right
805 // now and fail the initialization if it's not available
806 m_pfnDirectDrawCreate = (DirectDrawCreate_t)
807 m_dllDDraw.GetSymbol(_T("DirectDrawCreate"));
808 if ( !m_pfnDirectDrawCreate )
809 return;
810
811 if ( (*pDDEnumEx)(DDEnumExCallback,
812 this,
813 DDENUM_ATTACHEDSECONDARYDEVICES) != DD_OK )
814 {
815 wxLogLastError(_T("DirectDrawEnumerateEx"));
816 }
817 }
818
819 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
820 {
821 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
822 // calling m_pDD2->Release() later would crash
823 Clear();
824 }
825
826 // ----------------------------------------------------------------------------
827 // callbacks for monitor/modes enumeration stuff
828 // ----------------------------------------------------------------------------
829
830 BOOL WINAPI
831 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID *pGuid,
832 LPTSTR WXUNUSED(driverDescription),
833 LPTSTR driverName,
834 LPVOID lpContext,
835 HMONITOR hmon)
836 {
837 if ( pGuid )
838 {
839 wxDisplayFactoryDirectDraw * self =
840 wx_static_cast(wxDisplayFactoryDirectDraw *, lpContext);
841 self->AddDisplay(*pGuid, hmon, driverName);
842 }
843 //else: we're called for the primary monitor, skip it
844
845 // continue the enumeration
846 return TRUE;
847 }
848
849 // ----------------------------------------------------------------------------
850 // wxDisplayFactoryDirectDraw helpers
851 // ----------------------------------------------------------------------------
852
853 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID& guid,
854 HMONITOR hmon,
855 LPTSTR name)
856 {
857 m_displays.Add(new wxDisplayInfoDirectDraw(guid, hmon, name));
858 }
859
860 // ----------------------------------------------------------------------------
861 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
862 // ----------------------------------------------------------------------------
863
864 wxDisplayImpl *wxDisplayFactoryDirectDraw::CreateDisplay(size_t n)
865 {
866 wxCHECK_MSG( n < m_displays.size(), NULL, _T("invalid display index") );
867
868 wxDisplayInfoDirectDraw *
869 info = wx_static_cast(wxDisplayInfoDirectDraw *, m_displays[n]);
870
871 if ( !info->m_pDD2 )
872 {
873 IDirectDraw *pDD;
874 GUID guid(info->m_guid);
875 HRESULT hr = (*m_pfnDirectDrawCreate)(&guid, &pDD, NULL);
876
877 if ( FAILED(hr) || !pDD )
878 {
879 // what to do??
880 wxLogApiError(_T("DirectDrawCreate"), hr);
881 return NULL;
882 }
883
884 // we got IDirectDraw, but we need IDirectDraw2
885 hr = pDD->QueryInterface(wxIID_IDirectDraw2, (void **)&info->m_pDD2);
886 pDD->Release();
887
888 if ( FAILED(hr) || !info->m_pDD2 )
889 {
890 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr);
891 return NULL;
892 }
893
894 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
895 // which is ok as we don't want to recreate DD objects all the time
896 }
897 //else: DirectDraw object corresponding to our display already exists
898
899 return new wxDisplayImplDirectDraw(n, *info, info->m_pDD2);
900 }
901
902 // ============================================================================
903 // wxDisplayImplDirectDraw
904 // ============================================================================
905
906 // ----------------------------------------------------------------------------
907 // video modes enumeration
908 // ----------------------------------------------------------------------------
909
910 // tiny helper class used to pass information from GetModes() to
911 // wxDDEnumModesCallback
912 class wxDDVideoModesAdder
913 {
914 public:
915 // our Add() method will add modes matching modeMatch to modes array
916 wxDDVideoModesAdder(wxArrayVideoModes& modes, const wxVideoMode& modeMatch)
917 : m_modes(modes),
918 m_modeMatch(modeMatch)
919 {
920 }
921
922 void Add(const wxVideoMode& mode)
923 {
924 if ( mode.Matches(m_modeMatch) )
925 m_modes.Add(mode);
926 }
927
928 private:
929 wxArrayVideoModes& m_modes;
930 const wxVideoMode& m_modeMatch;
931
932 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder)
933 };
934
935 HRESULT WINAPI wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc,
936 LPVOID lpContext)
937 {
938 // we need at least the mode size
939 static const DWORD FLAGS_REQUIRED = DDSD_HEIGHT | DDSD_WIDTH;
940 if ( (lpDDSurfaceDesc->dwFlags & FLAGS_REQUIRED) == FLAGS_REQUIRED )
941 {
942 wxDDVideoModesAdder * const vmodes =
943 wx_static_cast(wxDDVideoModesAdder *, lpContext);
944
945 vmodes->Add(wxVideoMode(lpDDSurfaceDesc->dwWidth,
946 lpDDSurfaceDesc->dwHeight,
947 lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount,
948 lpDDSurfaceDesc->dwRefreshRate));
949 }
950
951 // continue the enumeration
952 return DDENUMRET_OK;
953 }
954
955 wxArrayVideoModes
956 wxDisplayImplDirectDraw::GetModes(const wxVideoMode& modeMatch) const
957 {
958 wxArrayVideoModes modes;
959 wxDDVideoModesAdder modesAdder(modes, modeMatch);
960
961 HRESULT hr = m_pDD2->EnumDisplayModes
962 (
963 DDEDM_REFRESHRATES,
964 NULL, // all modes
965 &modesAdder, // callback parameter
966 wxDDEnumModesCallback
967 );
968
969 if ( FAILED(hr) )
970 {
971 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr);
972 }
973
974 return modes;
975 }
976
977 // ----------------------------------------------------------------------------
978 // video mode switching
979 // ----------------------------------------------------------------------------
980
981 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode& mode)
982 {
983 wxWindow *winTop = wxTheApp->GetTopWindow();
984 wxCHECK_MSG( winTop, false, _T("top level window required for DirectX") );
985
986 HRESULT hr = m_pDD2->SetCooperativeLevel
987 (
988 GetHwndOf(winTop),
989 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN
990 );
991 if ( FAILED(hr) )
992 {
993 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr);
994
995 return false;
996 }
997
998 hr = m_pDD2->SetDisplayMode(mode.w, mode.h, mode.bpp, mode.refresh, 0);
999 if ( FAILED(hr) )
1000 {
1001 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr);
1002
1003 return false;
1004 }
1005
1006 return true;
1007 }
1008
1009 #endif // wxUSE_DIRECTDRAW
1010
1011 #endif // wxUSE_DISPLAY