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)
8 // Copyright: (c) wxWidgets team
9 // Copyright: (c) 2002-2006 wxWidgets team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/display.h"
33 #include "wx/dynarray.h"
38 #include "wx/dynload.h"
39 #include "wx/sysopt.h"
41 #include "wx/display_impl.h"
42 #include "wx/msw/missing.h"
44 // define this to use DirectDraw for display mode switching: this is disabled
45 // by default because ddraw.h is now always available and also it's not really
46 // clear what are the benefits of using DirectDraw compared to the standard API
48 #if !defined(wxUSE_DIRECTDRAW)
49 #define wxUSE_DIRECTDRAW 0
53 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
54 // can't directly test whether HMONITOR is defined or not in windef.h as
55 // it's not a macro but a typedef, so we test for an unrelated symbol which
56 // is only defined in winuser.h if WINVER >= 0x0500
57 #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK)
58 DECLARE_HANDLE(HMONITOR
);
59 typedef BOOL(CALLBACK
* MONITORENUMPROC
)(HMONITOR
, HDC
, LPRECT
, LPARAM
);
60 typedef struct tagMONITORINFO
66 } MONITORINFO
, *LPMONITORINFO
;
67 typedef struct tagMONITORINFOEX
: public tagMONITORINFO
69 TCHAR szDevice
[CCHDEVICENAME
];
70 } MONITORINFOEX
, *LPMONITORINFOEX
;
71 #define MONITOR_DEFAULTTONULL 0x00000000
72 #define MONITOR_DEFAULTTOPRIMARY 0x00000001
73 #define MONITOR_DEFAULTTONEAREST 0x00000002
74 #define MONITORINFOF_PRIMARY 0x00000001
75 #define HMONITOR_DECLARED
77 #endif // !__WXWINCE__
82 // we don't want to link with ddraw.lib which contains the real
83 // IID_IDirectDraw2 definition
84 const GUID wxIID_IDirectDraw2
=
85 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
86 #endif // wxUSE_DIRECTDRAW
88 // ----------------------------------------------------------------------------
89 // typedefs for dynamically loaded Windows functions
90 // ----------------------------------------------------------------------------
92 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
99 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
100 LPTSTR driverDescription
,
105 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
109 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
110 LPDIRECTDRAW
*lplpDD
,
111 IUnknown
*pUnkOuter
);
112 #endif // wxUSE_DIRECTDRAW
114 typedef BOOL (WINAPI
*EnumDisplayMonitors_t
)(HDC
,LPCRECT
,MONITORENUMPROC
,LPARAM
);
115 typedef HMONITOR (WINAPI
*MonitorFromPoint_t
)(POINT
,DWORD
);
116 typedef HMONITOR (WINAPI
*MonitorFromWindow_t
)(HWND
,DWORD
);
117 typedef BOOL (WINAPI
*GetMonitorInfo_t
)(HMONITOR
,LPMONITORINFO
);
120 // emulation of ChangeDisplaySettingsEx() for Win95
121 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
125 LPVOID
WXUNUSED(lParam
))
127 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
129 #endif // !__WXWINCE__
131 // ----------------------------------------------------------------------------
132 // display information classes
133 // ----------------------------------------------------------------------------
137 wxDisplayInfo(HMONITOR hmon
= NULL
)
143 virtual ~wxDisplayInfo() { }
146 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
147 // hadn't been done before)
151 // handle of this monitor used by MonitorXXX() functions, never NULL
154 // the entire area of this monitor in virtual screen coordinates
157 // the work or client area, i.e. the area available for the normal windows
160 // the display device name for this monitor, empty initially and retrieved
161 // on demand by DoGetName()
164 // the flags of this monitor, also used as initialization marker: if this
165 // is -1, GetMonitorInfo() hadn't been called yet
169 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
171 // ----------------------------------------------------------------------------
172 // common base class for all Win32 wxDisplayImpl versions
173 // ----------------------------------------------------------------------------
175 class wxDisplayImplWin32Base
: public wxDisplayImpl
178 wxDisplayImplWin32Base(unsigned n
, wxDisplayInfo
& info
)
184 virtual wxRect
GetGeometry() const;
185 virtual wxRect
GetClientArea() const;
186 virtual wxString
GetName() const;
187 virtual bool IsPrimary() const;
189 virtual wxVideoMode
GetCurrentMode() const;
192 // convert a DEVMODE to our wxVideoMode
193 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
195 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
196 // and although 0 is ok for us we don't want to return modes with 1hz
198 return wxVideoMode(dm
.dmPelsWidth
,
201 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
204 wxDisplayInfo
& m_info
;
207 // ----------------------------------------------------------------------------
208 // common base class for all Win32 wxDisplayFactory versions
209 // ----------------------------------------------------------------------------
211 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
212 static MonitorFromPoint_t gs_MonitorFromPoint
= NULL
;
213 static MonitorFromWindow_t gs_MonitorFromWindow
= NULL
;
214 static GetMonitorInfo_t gs_GetMonitorInfo
= NULL
;
216 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
219 virtual ~wxDisplayFactoryWin32Base();
221 bool IsOk() const { return !m_displays
.empty(); }
223 virtual unsigned GetCount() { return unsigned(m_displays
.size()); }
224 virtual int GetFromPoint(const wxPoint
& pt
);
225 virtual int GetFromWindow(wxWindow
*window
);
228 // ctor checks if the current system supports multimon API and dynamically
229 // bind the functions we need if this is the case and sets
230 // ms_supportsMultimon if they're available
231 wxDisplayFactoryWin32Base();
233 // delete all m_displays elements: can be called from the derived class
234 // dtor if it is important to do this before destroying it (as in
235 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
238 // find the monitor corresponding to the given handle, return wxNOT_FOUND
240 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
243 // flag indicating whether gs_MonitorXXX functions are available
244 static int ms_supportsMultimon
;
246 // the array containing information about all available displays, should be
247 // filled by the derived class ctors
248 wxDisplayInfoArray m_displays
;
251 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
)
254 // ----------------------------------------------------------------------------
255 // wxDisplay implementation using Windows multi-monitor support functions
256 // ----------------------------------------------------------------------------
258 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
261 wxDisplayImplMultimon(unsigned n
, wxDisplayInfo
& info
)
262 : wxDisplayImplWin32Base(n
, info
)
266 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
267 virtual bool ChangeMode(const wxVideoMode
& mode
);
270 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
)
273 class wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
276 wxDisplayFactoryMultimon();
278 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
281 // EnumDisplayMonitors() callback
282 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
288 // add a monitor description to m_displays array
289 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
292 // ----------------------------------------------------------------------------
293 // wxDisplay implementation using DirectDraw
294 // ----------------------------------------------------------------------------
298 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
300 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
301 : wxDisplayInfo(hmon
),
308 virtual ~wxDisplayInfoDirectDraw()
315 // IDirectDraw object used to control this display, may be NULL
316 IDirectDraw2
*m_pDD2
;
318 // DirectDraw GUID for this display, only valid when using DirectDraw
322 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
)
325 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
328 wxDisplayImplDirectDraw(unsigned n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
329 : wxDisplayImplWin32Base(n
, info
),
335 virtual ~wxDisplayImplDirectDraw()
340 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
341 virtual bool ChangeMode(const wxVideoMode
& mode
);
344 IDirectDraw2
*m_pDD2
;
346 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
)
349 class wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
352 wxDisplayFactoryDirectDraw();
353 virtual ~wxDisplayFactoryDirectDraw();
355 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
358 // callback used with DirectDrawEnumerateEx()
359 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
360 LPTSTR driverDescription
,
365 // add a monitor description to m_displays array
366 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
370 wxDynamicLibrary m_dllDDraw
;
372 // dynamically resolved DirectDrawCreate()
373 DirectDrawCreate_t m_pfnDirectDrawCreate
;
375 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
)
378 #endif // wxUSE_DIRECTDRAW
381 // ============================================================================
382 // common classes implementation
383 // ============================================================================
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
391 // we have 2 implementations for modern Windows: one using standard Win32
392 // and another using DirectDraw, the choice between them is done using a
396 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
398 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
399 if ( factoryDD
->IsOk() )
404 #endif // wxUSE_DIRECTDRAW
406 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
407 if ( factoryMM
->IsOk() )
413 // finally fall back to a stub implementation if all else failed (Win95?)
414 return new wxDisplayFactorySingle
;
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void wxDisplayInfo::Initialize()
423 if ( m_flags
== (DWORD
)-1 )
425 WinStruct
<MONITORINFOEX
> monInfo
;
426 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
428 wxLogLastError(_T("GetMonitorInfo"));
433 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
434 wxCopyRECTToRect(monInfo
.rcWork
, m_rectClient
);
435 m_devName
= monInfo
.szDevice
;
436 m_flags
= monInfo
.dwFlags
;
440 // ----------------------------------------------------------------------------
441 // wxDisplayImplWin32Base
442 // ----------------------------------------------------------------------------
444 wxRect
wxDisplayImplWin32Base::GetGeometry() const
446 if ( m_info
.m_rect
.IsEmpty() )
449 return m_info
.m_rect
;
452 wxRect
wxDisplayImplWin32Base::GetClientArea() const
454 if ( m_info
.m_rectClient
.IsEmpty() )
457 return m_info
.m_rectClient
;
460 wxString
wxDisplayImplWin32Base::GetName() const
462 if ( m_info
.m_devName
.empty() )
465 return m_info
.m_devName
;
468 bool wxDisplayImplWin32Base::IsPrimary() const
470 if ( m_info
.m_flags
== (DWORD
)-1 )
473 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
476 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
480 // The first parameter of EnumDisplaySettings() must be NULL under Win95
481 // according to MSDN. The version of GetName() we implement for Win95
482 // returns an empty string.
483 const wxString name
= GetName();
484 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
487 dm
.dmSize
= sizeof(dm
);
488 dm
.dmDriverExtra
= 0;
489 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
491 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
495 mode
= ConvertToVideoMode(dm
);
501 // ----------------------------------------------------------------------------
502 // wxDisplayFactoryWin32Base
503 // ----------------------------------------------------------------------------
505 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
507 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
509 if ( ms_supportsMultimon
== -1 )
511 ms_supportsMultimon
= 0;
513 wxDynamicLibrary
dllUser32(_T("user32.dll"));
517 gs_MonitorFromPoint
= (MonitorFromPoint_t
)
518 dllUser32
.GetSymbol(wxT("MonitorFromPoint"));
519 if ( !gs_MonitorFromPoint
)
522 gs_MonitorFromWindow
= (MonitorFromWindow_t
)
523 dllUser32
.GetSymbol(wxT("MonitorFromWindow"));
524 if ( !gs_MonitorFromWindow
)
527 gs_GetMonitorInfo
= (GetMonitorInfo_t
)
528 dllUser32
.GetSymbolAorW(wxT("GetMonitorInfo"));
529 if ( !gs_GetMonitorInfo
)
532 ms_supportsMultimon
= 1;
534 // we can safely let dllUser32 go out of scope, the DLL itself will
535 // still remain loaded as all Win32 programs use it
539 void wxDisplayFactoryWin32Base::Clear()
541 WX_CLEAR_ARRAY(m_displays
);
544 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
549 // helper for GetFromPoint() and GetFromWindow()
550 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
554 const size_t count
= m_displays
.size();
555 for ( size_t n
= 0; n
< count
; n
++ )
557 if ( hmon
== m_displays
[n
]->m_hmon
)
565 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
571 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
572 MONITOR_DEFAULTTONULL
));
575 int wxDisplayFactoryWin32Base::GetFromWindow(wxWindow
*window
)
577 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
578 MONITOR_DEFAULTTONULL
));
581 // ============================================================================
582 // wxDisplay implementation using Win32 multimon API
583 // ============================================================================
585 // ----------------------------------------------------------------------------
586 // wxDisplayFactoryMultimon initialization
587 // ----------------------------------------------------------------------------
589 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
591 if ( !ms_supportsMultimon
)
594 // look up EnumDisplayMonitors() which we don't need with DirectDraw
596 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
600 wxDynamicLibrary
dllUser32(_T("user32.dll"));
601 pfnEnumDisplayMonitors
= (EnumDisplayMonitors_t
)
602 dllUser32
.GetSymbol(wxT("EnumDisplayMonitors"));
603 if ( !pfnEnumDisplayMonitors
)
607 // enumerate all displays
608 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
610 wxLogLastError(wxT("EnumDisplayMonitors"));
616 wxDisplayFactoryMultimon::MultimonEnumProc(
617 HMONITOR hMonitor
, // handle to display monitor
618 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
619 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
620 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
623 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
624 self
->AddDisplay(hMonitor
, lprcMonitor
);
626 // continue the enumeration
630 // ----------------------------------------------------------------------------
631 // wxDisplayFactoryMultimon helper functions
632 // ----------------------------------------------------------------------------
634 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
636 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
638 // we also store the display geometry
639 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
640 lprcMonitor
->right
- lprcMonitor
->left
,
641 lprcMonitor
->bottom
- lprcMonitor
->top
);
643 // now add this monitor to the array
644 m_displays
.Add(info
);
647 // ----------------------------------------------------------------------------
648 // wxDisplayFactoryMultimon inherited pure virtuals implementation
649 // ----------------------------------------------------------------------------
651 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(unsigned n
)
653 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
655 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
658 // ----------------------------------------------------------------------------
659 // wxDisplayImplMultimon implementation
660 // ----------------------------------------------------------------------------
663 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
665 wxArrayVideoModes modes
;
667 // The first parameter of EnumDisplaySettings() must be NULL under Win95
668 // according to MSDN. The version of GetName() we implement for Win95
669 // returns an empty string.
670 const wxString name
= GetName();
671 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
674 dm
.dmSize
= sizeof(dm
);
675 dm
.dmDriverExtra
= 0;
676 for ( int iModeNum
= 0;
677 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
680 const wxVideoMode mode
= ConvertToVideoMode(dm
);
681 if ( mode
.Matches(modeMatch
) )
690 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
692 // prepare ChangeDisplaySettingsEx() parameters
698 if ( mode
== wxDefaultVideoMode
)
700 // reset the video mode to default
704 else // change to the given mode
706 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
707 _T("at least the width and height must be specified") );
710 dm
.dmSize
= sizeof(dm
);
711 dm
.dmDriverExtra
= 0;
712 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
713 dm
.dmPelsWidth
= mode
.w
;
714 dm
.dmPelsHeight
= mode
.h
;
718 dm
.dmFields
|= DM_BITSPERPEL
;
719 dm
.dmBitsPerPel
= mode
.bpp
;
724 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
725 dm
.dmDisplayFrequency
= mode
.refresh
;
732 #else // !__WXWINCE__
733 flags
= CDS_FULLSCREEN
;
734 #endif // __WXWINCE__/!__WXWINCE__
738 // get pointer to the function dynamically
740 // we're only called from the main thread, so it's ok to use static
742 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
743 if ( !pfnChangeDisplaySettingsEx
)
745 wxDynamicLibrary
dllUser32(_T("user32.dll"));
746 if ( dllUser32
.IsLoaded() )
748 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
749 dllUser32
.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
751 //else: huh, no user32.dll??
754 if ( !pfnChangeDisplaySettingsEx
)
756 // we must be under Win95 and so there is no multiple monitors
758 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
760 #endif // !__WXWINCE__
763 // do change the mode
764 switch ( pfnChangeDisplaySettingsEx
766 GetName(), // display name
767 pDevMode
, // dev mode or NULL to reset
770 NULL
// pointer to video parameters (not used)
773 case DISP_CHANGE_SUCCESSFUL
:
776 // If we have a top-level, full-screen frame, emulate
777 // the DirectX behavior and resize it. This makes this
778 // API quite a bit easier to use.
779 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
780 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
781 if (frameTop
&& frameTop
->IsFullScreen())
783 wxVideoMode current
= GetCurrentMode();
784 frameTop
->SetClientSize(current
.w
, current
.h
);
789 case DISP_CHANGE_BADMODE
:
790 // don't complain about this, this is the only "expected" error
794 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
801 // ============================================================================
802 // DirectDraw-based wxDisplay implementation
803 // ============================================================================
807 // ----------------------------------------------------------------------------
808 // wxDisplayFactoryDirectDraw initialization
809 // ----------------------------------------------------------------------------
811 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
813 if ( !ms_supportsMultimon
)
817 // suppress the errors if ddraw.dll is not found, we're prepared to handle
822 m_dllDDraw
.Load(_T("ddraw.dll"));
824 if ( !m_dllDDraw
.IsLoaded() )
827 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
828 m_dllDDraw
.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
832 // we can't continue without DirectDrawCreate() later, so resolve it right
833 // now and fail the initialization if it's not available
834 m_pfnDirectDrawCreate
= (DirectDrawCreate_t
)
835 m_dllDDraw
.GetSymbol(_T("DirectDrawCreate"));
836 if ( !m_pfnDirectDrawCreate
)
839 if ( (*pDDEnumEx
)(DDEnumExCallback
,
841 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
843 wxLogLastError(_T("DirectDrawEnumerateEx"));
847 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
849 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
850 // calling m_pDD2->Release() later would crash
854 // ----------------------------------------------------------------------------
855 // callbacks for monitor/modes enumeration stuff
856 // ----------------------------------------------------------------------------
859 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID
*pGuid
,
860 LPTSTR
WXUNUSED(driverDescription
),
867 wxDisplayFactoryDirectDraw
* self
=
868 wx_static_cast(wxDisplayFactoryDirectDraw
*, lpContext
);
869 self
->AddDisplay(*pGuid
, hmon
, driverName
);
871 //else: we're called for the primary monitor, skip it
873 // continue the enumeration
877 // ----------------------------------------------------------------------------
878 // wxDisplayFactoryDirectDraw helpers
879 // ----------------------------------------------------------------------------
881 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID
& guid
,
885 m_displays
.Add(new wxDisplayInfoDirectDraw(guid
, hmon
, name
));
888 // ----------------------------------------------------------------------------
889 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
890 // ----------------------------------------------------------------------------
892 wxDisplayImpl
*wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n
)
894 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
896 wxDisplayInfoDirectDraw
*
897 info
= wx_static_cast(wxDisplayInfoDirectDraw
*, m_displays
[n
]);
902 GUID
guid(info
->m_guid
);
903 HRESULT hr
= (*m_pfnDirectDrawCreate
)(&guid
, &pDD
, NULL
);
905 if ( FAILED(hr
) || !pDD
)
908 wxLogApiError(_T("DirectDrawCreate"), hr
);
912 // we got IDirectDraw, but we need IDirectDraw2
913 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&info
->m_pDD2
);
916 if ( FAILED(hr
) || !info
->m_pDD2
)
918 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
922 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
923 // which is ok as we don't want to recreate DD objects all the time
925 //else: DirectDraw object corresponding to our display already exists
927 return new wxDisplayImplDirectDraw(n
, *info
, info
->m_pDD2
);
930 // ============================================================================
931 // wxDisplayImplDirectDraw
932 // ============================================================================
934 // ----------------------------------------------------------------------------
935 // video modes enumeration
936 // ----------------------------------------------------------------------------
938 // tiny helper class used to pass information from GetModes() to
939 // wxDDEnumModesCallback
940 class wxDDVideoModesAdder
943 // our Add() method will add modes matching modeMatch to modes array
944 wxDDVideoModesAdder(wxArrayVideoModes
& modes
, const wxVideoMode
& modeMatch
)
946 m_modeMatch(modeMatch
)
950 void Add(const wxVideoMode
& mode
)
952 if ( mode
.Matches(m_modeMatch
) )
957 wxArrayVideoModes
& m_modes
;
958 const wxVideoMode
& m_modeMatch
;
960 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder
)
963 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
966 // we need at least the mode size
967 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
968 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
970 wxDDVideoModesAdder
* const vmodes
=
971 wx_static_cast(wxDDVideoModesAdder
*, lpContext
);
973 vmodes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
974 lpDDSurfaceDesc
->dwHeight
,
975 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
976 lpDDSurfaceDesc
->dwRefreshRate
));
979 // continue the enumeration
984 wxDisplayImplDirectDraw::GetModes(const wxVideoMode
& modeMatch
) const
986 wxArrayVideoModes modes
;
987 wxDDVideoModesAdder
modesAdder(modes
, modeMatch
);
989 HRESULT hr
= m_pDD2
->EnumDisplayModes
993 &modesAdder
, // callback parameter
994 wxDDEnumModesCallback
999 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
1005 // ----------------------------------------------------------------------------
1006 // video mode switching
1007 // ----------------------------------------------------------------------------
1009 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode
& mode
)
1011 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
1012 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
1014 HRESULT hr
= m_pDD2
->SetCooperativeLevel
1017 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
1021 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr
);
1026 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1029 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr
);
1037 #endif // wxUSE_DIRECTDRAW
1039 #endif // wxUSE_DISPLAY