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