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/wrapwin.h"
43 #include "wx/msw/missing.h"
45 // define this to use DirectDraw for display mode switching: this is disabled
46 // by default because ddraw.h is now always available and also it's not really
47 // clear what are the benefits of using DirectDraw compared to the standard API
49 #if !defined(wxUSE_DIRECTDRAW)
50 #define wxUSE_DIRECTDRAW 0
54 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
55 // can't directly test whether HMONITOR is defined or not in windef.h as
56 // it's not a macro but a typedef, so we test for an unrelated symbol which
57 // is only defined in winuser.h if WINVER >= 0x0500
58 #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK)
59 DECLARE_HANDLE(HMONITOR
);
60 typedef BOOL(CALLBACK
* MONITORENUMPROC
)(HMONITOR
, HDC
, LPRECT
, LPARAM
);
61 typedef struct tagMONITORINFO
67 } MONITORINFO
, *LPMONITORINFO
;
68 typedef struct tagMONITORINFOEX
: public tagMONITORINFO
70 TCHAR szDevice
[CCHDEVICENAME
];
71 } MONITORINFOEX
, *LPMONITORINFOEX
;
72 #define MONITOR_DEFAULTTONULL 0x00000000
73 #define MONITOR_DEFAULTTOPRIMARY 0x00000001
74 #define MONITOR_DEFAULTTONEAREST 0x00000002
75 #define MONITORINFOF_PRIMARY 0x00000001
76 #define HMONITOR_DECLARED
78 #endif // !__WXWINCE__
83 // we don't want to link with ddraw.lib which contains the real
84 // IID_IDirectDraw2 definition
85 const GUID wxIID_IDirectDraw2
=
86 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
87 #endif // wxUSE_DIRECTDRAW
89 // ----------------------------------------------------------------------------
90 // typedefs for dynamically loaded Windows functions
91 // ----------------------------------------------------------------------------
93 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
100 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
101 LPTSTR driverDescription
,
106 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
110 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
111 LPDIRECTDRAW
*lplpDD
,
112 IUnknown
*pUnkOuter
);
113 #endif // wxUSE_DIRECTDRAW
115 typedef BOOL (WINAPI
*EnumDisplayMonitors_t
)(HDC
,LPCRECT
,MONITORENUMPROC
,LPARAM
);
116 typedef HMONITOR (WINAPI
*MonitorFromPoint_t
)(POINT
,DWORD
);
117 typedef HMONITOR (WINAPI
*MonitorFromWindow_t
)(HWND
,DWORD
);
118 typedef BOOL (WINAPI
*GetMonitorInfo_t
)(HMONITOR
,LPMONITORINFO
);
121 // emulation of ChangeDisplaySettingsEx() for Win95
122 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
126 LPVOID
WXUNUSED(lParam
))
128 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
130 #endif // !__WXWINCE__
132 // ----------------------------------------------------------------------------
133 // display information classes
134 // ----------------------------------------------------------------------------
138 wxDisplayInfo(HMONITOR hmon
= NULL
)
144 virtual ~wxDisplayInfo() { }
147 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
148 // hadn't been done before)
152 // handle of this monitor used by MonitorXXX() functions, never NULL
155 // the entire area of this monitor in virtual screen coordinates
158 // the work or client area, i.e. the area available for the normal windows
161 // the display device name for this monitor, empty initially and retrieved
162 // on demand by DoGetName()
165 // the flags of this monitor, also used as initialization marker: if this
166 // is -1, GetMonitorInfo() hadn't been called yet
170 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
172 // ----------------------------------------------------------------------------
173 // common base class for all Win32 wxDisplayImpl versions
174 // ----------------------------------------------------------------------------
176 class wxDisplayImplWin32Base
: public wxDisplayImpl
179 wxDisplayImplWin32Base(unsigned n
, wxDisplayInfo
& info
)
185 virtual wxRect
GetGeometry() const;
186 virtual wxRect
GetClientArea() const;
187 virtual wxString
GetName() const;
188 virtual bool IsPrimary() const;
190 virtual wxVideoMode
GetCurrentMode() const;
193 // convert a DEVMODE to our wxVideoMode
194 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
196 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
197 // and although 0 is ok for us we don't want to return modes with 1hz
199 return wxVideoMode(dm
.dmPelsWidth
,
202 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
205 wxDisplayInfo
& m_info
;
208 // ----------------------------------------------------------------------------
209 // common base class for all Win32 wxDisplayFactory versions
210 // ----------------------------------------------------------------------------
212 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
213 static MonitorFromPoint_t gs_MonitorFromPoint
= NULL
;
214 static MonitorFromWindow_t gs_MonitorFromWindow
= NULL
;
215 static GetMonitorInfo_t gs_GetMonitorInfo
= NULL
;
217 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
220 virtual ~wxDisplayFactoryWin32Base();
222 bool IsOk() const { return !m_displays
.empty(); }
224 virtual unsigned GetCount() { return unsigned(m_displays
.size()); }
225 virtual int GetFromPoint(const wxPoint
& pt
);
226 virtual int GetFromWindow(const wxWindow
*window
);
229 // ctor checks if the current system supports multimon API and dynamically
230 // bind the functions we need if this is the case and sets
231 // ms_supportsMultimon if they're available
232 wxDisplayFactoryWin32Base();
234 // delete all m_displays elements: can be called from the derived class
235 // dtor if it is important to do this before destroying it (as in
236 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
239 // find the monitor corresponding to the given handle, return wxNOT_FOUND
241 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
244 // flag indicating whether gs_MonitorXXX functions are available
245 static int ms_supportsMultimon
;
247 // the array containing information about all available displays, should be
248 // filled by the derived class ctors
249 wxDisplayInfoArray m_displays
;
252 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
)
255 // ----------------------------------------------------------------------------
256 // wxDisplay implementation using Windows multi-monitor support functions
257 // ----------------------------------------------------------------------------
259 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
262 wxDisplayImplMultimon(unsigned n
, wxDisplayInfo
& info
)
263 : wxDisplayImplWin32Base(n
, info
)
267 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
268 virtual bool ChangeMode(const wxVideoMode
& mode
);
271 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
)
274 class wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
277 wxDisplayFactoryMultimon();
279 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
282 // EnumDisplayMonitors() callback
283 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
289 // add a monitor description to m_displays array
290 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
293 // ----------------------------------------------------------------------------
294 // wxDisplay implementation using DirectDraw
295 // ----------------------------------------------------------------------------
299 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
301 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
302 : wxDisplayInfo(hmon
),
309 virtual ~wxDisplayInfoDirectDraw()
316 // IDirectDraw object used to control this display, may be NULL
317 IDirectDraw2
*m_pDD2
;
319 // DirectDraw GUID for this display, only valid when using DirectDraw
323 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
)
326 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
329 wxDisplayImplDirectDraw(unsigned n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
330 : wxDisplayImplWin32Base(n
, info
),
336 virtual ~wxDisplayImplDirectDraw()
341 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
342 virtual bool ChangeMode(const wxVideoMode
& mode
);
345 IDirectDraw2
*m_pDD2
;
347 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
)
350 class wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
353 wxDisplayFactoryDirectDraw();
354 virtual ~wxDisplayFactoryDirectDraw();
356 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
359 // callback used with DirectDrawEnumerateEx()
360 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
361 LPTSTR driverDescription
,
366 // add a monitor description to m_displays array
367 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
371 wxDynamicLibrary m_dllDDraw
;
373 // dynamically resolved DirectDrawCreate()
374 DirectDrawCreate_t m_pfnDirectDrawCreate
;
376 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
)
379 #endif // wxUSE_DIRECTDRAW
382 // ============================================================================
383 // common classes implementation
384 // ============================================================================
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
392 // we have 2 implementations for modern Windows: one using standard Win32
393 // and another using DirectDraw, the choice between them is done using a
397 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
399 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
400 if ( factoryDD
->IsOk() )
405 #endif // wxUSE_DIRECTDRAW
407 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
408 if ( factoryMM
->IsOk() )
414 // finally fall back to a stub implementation if all else failed (Win95?)
415 return new wxDisplayFactorySingle
;
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 void wxDisplayInfo::Initialize()
424 if ( m_flags
== (DWORD
)-1 )
426 WinStruct
<MONITORINFOEX
> monInfo
;
427 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
429 wxLogLastError(_T("GetMonitorInfo"));
434 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
435 wxCopyRECTToRect(monInfo
.rcWork
, m_rectClient
);
436 m_devName
= monInfo
.szDevice
;
437 m_flags
= monInfo
.dwFlags
;
441 // ----------------------------------------------------------------------------
442 // wxDisplayImplWin32Base
443 // ----------------------------------------------------------------------------
445 wxRect
wxDisplayImplWin32Base::GetGeometry() const
447 if ( m_info
.m_rect
.IsEmpty() )
450 return m_info
.m_rect
;
453 wxRect
wxDisplayImplWin32Base::GetClientArea() const
455 if ( m_info
.m_rectClient
.IsEmpty() )
458 return m_info
.m_rectClient
;
461 wxString
wxDisplayImplWin32Base::GetName() const
463 if ( m_info
.m_devName
.empty() )
466 return m_info
.m_devName
;
469 bool wxDisplayImplWin32Base::IsPrimary() const
471 if ( m_info
.m_flags
== (DWORD
)-1 )
474 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
477 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
481 // The first parameter of EnumDisplaySettings() must be NULL under Win95
482 // according to MSDN. The version of GetName() we implement for Win95
483 // returns an empty string.
484 const wxString name
= GetName();
485 const wxChar
* const deviceName
= name
.empty()
486 ? (const wxChar
*)NULL
487 : (const wxChar
*)name
.c_str();
490 dm
.dmSize
= sizeof(dm
);
491 dm
.dmDriverExtra
= 0;
492 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
494 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
498 mode
= ConvertToVideoMode(dm
);
504 // ----------------------------------------------------------------------------
505 // wxDisplayFactoryWin32Base
506 // ----------------------------------------------------------------------------
508 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
510 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
512 if ( ms_supportsMultimon
== -1 )
514 ms_supportsMultimon
= 0;
516 wxDynamicLibrary
dllUser32(_T("user32.dll"));
520 gs_MonitorFromPoint
= (MonitorFromPoint_t
)
521 dllUser32
.GetSymbol(wxT("MonitorFromPoint"));
522 if ( !gs_MonitorFromPoint
)
525 gs_MonitorFromWindow
= (MonitorFromWindow_t
)
526 dllUser32
.GetSymbol(wxT("MonitorFromWindow"));
527 if ( !gs_MonitorFromWindow
)
530 gs_GetMonitorInfo
= (GetMonitorInfo_t
)
531 dllUser32
.GetSymbolAorW(wxT("GetMonitorInfo"));
532 if ( !gs_GetMonitorInfo
)
535 ms_supportsMultimon
= 1;
537 // we can safely let dllUser32 go out of scope, the DLL itself will
538 // still remain loaded as all Win32 programs use it
542 void wxDisplayFactoryWin32Base::Clear()
544 WX_CLEAR_ARRAY(m_displays
);
547 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
552 // helper for GetFromPoint() and GetFromWindow()
553 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
557 const size_t count
= m_displays
.size();
558 for ( size_t n
= 0; n
< count
; n
++ )
560 if ( hmon
== m_displays
[n
]->m_hmon
)
568 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
574 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
575 MONITOR_DEFAULTTONULL
));
578 int wxDisplayFactoryWin32Base::GetFromWindow(const wxWindow
*window
)
580 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
581 MONITOR_DEFAULTTONULL
));
584 // ============================================================================
585 // wxDisplay implementation using Win32 multimon API
586 // ============================================================================
588 // ----------------------------------------------------------------------------
589 // wxDisplayFactoryMultimon initialization
590 // ----------------------------------------------------------------------------
592 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
594 if ( !ms_supportsMultimon
)
597 // look up EnumDisplayMonitors() which we don't need with DirectDraw
599 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
603 wxDynamicLibrary
dllUser32(_T("user32.dll"));
604 pfnEnumDisplayMonitors
= (EnumDisplayMonitors_t
)
605 dllUser32
.GetSymbol(wxT("EnumDisplayMonitors"));
606 if ( !pfnEnumDisplayMonitors
)
610 // enumerate all displays
611 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
613 wxLogLastError(wxT("EnumDisplayMonitors"));
619 wxDisplayFactoryMultimon::MultimonEnumProc(
620 HMONITOR hMonitor
, // handle to display monitor
621 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
622 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
623 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
626 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
627 self
->AddDisplay(hMonitor
, lprcMonitor
);
629 // continue the enumeration
633 // ----------------------------------------------------------------------------
634 // wxDisplayFactoryMultimon helper functions
635 // ----------------------------------------------------------------------------
637 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
639 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
641 // we also store the display geometry
642 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
643 lprcMonitor
->right
- lprcMonitor
->left
,
644 lprcMonitor
->bottom
- lprcMonitor
->top
);
646 // now add this monitor to the array
647 m_displays
.Add(info
);
650 // ----------------------------------------------------------------------------
651 // wxDisplayFactoryMultimon inherited pure virtuals implementation
652 // ----------------------------------------------------------------------------
654 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(unsigned n
)
656 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
658 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
661 // ----------------------------------------------------------------------------
662 // wxDisplayImplMultimon implementation
663 // ----------------------------------------------------------------------------
666 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
668 wxArrayVideoModes modes
;
670 // The first parameter of EnumDisplaySettings() must be NULL under Win95
671 // according to MSDN. The version of GetName() we implement for Win95
672 // returns an empty string.
673 const wxString name
= GetName();
674 const wxChar
* const deviceName
= name
.empty()
675 ? (const wxChar
*)NULL
676 : (const wxChar
*)name
.c_str();
679 dm
.dmSize
= sizeof(dm
);
680 dm
.dmDriverExtra
= 0;
681 for ( int iModeNum
= 0;
682 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
685 const wxVideoMode mode
= ConvertToVideoMode(dm
);
686 if ( mode
.Matches(modeMatch
) )
695 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
697 // prepare ChangeDisplaySettingsEx() parameters
703 if ( mode
== wxDefaultVideoMode
)
705 // reset the video mode to default
709 else // change to the given mode
711 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
712 _T("at least the width and height must be specified") );
715 dm
.dmSize
= sizeof(dm
);
716 dm
.dmDriverExtra
= 0;
717 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
718 dm
.dmPelsWidth
= mode
.w
;
719 dm
.dmPelsHeight
= mode
.h
;
723 dm
.dmFields
|= DM_BITSPERPEL
;
724 dm
.dmBitsPerPel
= mode
.bpp
;
729 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
730 dm
.dmDisplayFrequency
= mode
.refresh
;
737 #else // !__WXWINCE__
738 flags
= CDS_FULLSCREEN
;
739 #endif // __WXWINCE__/!__WXWINCE__
743 // get pointer to the function dynamically
745 // we're only called from the main thread, so it's ok to use static
747 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
748 if ( !pfnChangeDisplaySettingsEx
)
750 wxDynamicLibrary
dllUser32(_T("user32.dll"));
751 if ( dllUser32
.IsLoaded() )
753 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
754 dllUser32
.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
756 //else: huh, no user32.dll??
759 if ( !pfnChangeDisplaySettingsEx
)
761 // we must be under Win95 and so there is no multiple monitors
763 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
765 #endif // !__WXWINCE__
768 // do change the mode
769 switch ( pfnChangeDisplaySettingsEx
771 GetName().wx_str(), // display name
772 pDevMode
, // dev mode or NULL to reset
775 NULL
// pointer to video parameters (not used)
778 case DISP_CHANGE_SUCCESSFUL
:
781 // If we have a top-level, full-screen frame, emulate
782 // the DirectX behavior and resize it. This makes this
783 // API quite a bit easier to use.
784 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
785 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
786 if (frameTop
&& frameTop
->IsFullScreen())
788 wxVideoMode current
= GetCurrentMode();
789 frameTop
->SetClientSize(current
.w
, current
.h
);
794 case DISP_CHANGE_BADMODE
:
795 // don't complain about this, this is the only "expected" error
799 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
806 // ============================================================================
807 // DirectDraw-based wxDisplay implementation
808 // ============================================================================
812 // ----------------------------------------------------------------------------
813 // wxDisplayFactoryDirectDraw initialization
814 // ----------------------------------------------------------------------------
816 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
818 if ( !ms_supportsMultimon
)
822 // suppress the errors if ddraw.dll is not found, we're prepared to handle
827 m_dllDDraw
.Load(_T("ddraw.dll"));
829 if ( !m_dllDDraw
.IsLoaded() )
832 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
833 m_dllDDraw
.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
837 // we can't continue without DirectDrawCreate() later, so resolve it right
838 // now and fail the initialization if it's not available
839 m_pfnDirectDrawCreate
= (DirectDrawCreate_t
)
840 m_dllDDraw
.GetSymbol(_T("DirectDrawCreate"));
841 if ( !m_pfnDirectDrawCreate
)
844 if ( (*pDDEnumEx
)(DDEnumExCallback
,
846 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
848 wxLogLastError(_T("DirectDrawEnumerateEx"));
852 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
854 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
855 // calling m_pDD2->Release() later would crash
859 // ----------------------------------------------------------------------------
860 // callbacks for monitor/modes enumeration stuff
861 // ----------------------------------------------------------------------------
864 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID
*pGuid
,
865 LPTSTR
WXUNUSED(driverDescription
),
872 wxDisplayFactoryDirectDraw
* self
=
873 wx_static_cast(wxDisplayFactoryDirectDraw
*, lpContext
);
874 self
->AddDisplay(*pGuid
, hmon
, driverName
);
876 //else: we're called for the primary monitor, skip it
878 // continue the enumeration
882 // ----------------------------------------------------------------------------
883 // wxDisplayFactoryDirectDraw helpers
884 // ----------------------------------------------------------------------------
886 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID
& guid
,
890 m_displays
.Add(new wxDisplayInfoDirectDraw(guid
, hmon
, name
));
893 // ----------------------------------------------------------------------------
894 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
895 // ----------------------------------------------------------------------------
897 wxDisplayImpl
*wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n
)
899 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
901 wxDisplayInfoDirectDraw
*
902 info
= wx_static_cast(wxDisplayInfoDirectDraw
*, m_displays
[n
]);
907 GUID
guid(info
->m_guid
);
908 HRESULT hr
= (*m_pfnDirectDrawCreate
)(&guid
, &pDD
, NULL
);
910 if ( FAILED(hr
) || !pDD
)
913 wxLogApiError(_T("DirectDrawCreate"), hr
);
917 // we got IDirectDraw, but we need IDirectDraw2
918 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&info
->m_pDD2
);
921 if ( FAILED(hr
) || !info
->m_pDD2
)
923 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
927 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
928 // which is ok as we don't want to recreate DD objects all the time
930 //else: DirectDraw object corresponding to our display already exists
932 return new wxDisplayImplDirectDraw(n
, *info
, info
->m_pDD2
);
935 // ============================================================================
936 // wxDisplayImplDirectDraw
937 // ============================================================================
939 // ----------------------------------------------------------------------------
940 // video modes enumeration
941 // ----------------------------------------------------------------------------
943 // tiny helper class used to pass information from GetModes() to
944 // wxDDEnumModesCallback
945 class wxDDVideoModesAdder
948 // our Add() method will add modes matching modeMatch to modes array
949 wxDDVideoModesAdder(wxArrayVideoModes
& modes
, const wxVideoMode
& modeMatch
)
951 m_modeMatch(modeMatch
)
955 void Add(const wxVideoMode
& mode
)
957 if ( mode
.Matches(m_modeMatch
) )
962 wxArrayVideoModes
& m_modes
;
963 const wxVideoMode
& m_modeMatch
;
965 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder
)
968 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
971 // we need at least the mode size
972 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
973 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
975 wxDDVideoModesAdder
* const vmodes
=
976 wx_static_cast(wxDDVideoModesAdder
*, lpContext
);
978 vmodes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
979 lpDDSurfaceDesc
->dwHeight
,
980 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
981 lpDDSurfaceDesc
->dwRefreshRate
));
984 // continue the enumeration
989 wxDisplayImplDirectDraw::GetModes(const wxVideoMode
& modeMatch
) const
991 wxArrayVideoModes modes
;
992 wxDDVideoModesAdder
modesAdder(modes
, modeMatch
);
994 HRESULT hr
= m_pDD2
->EnumDisplayModes
998 &modesAdder
, // callback parameter
999 wxDDEnumModesCallback
1004 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
1010 // ----------------------------------------------------------------------------
1011 // video mode switching
1012 // ----------------------------------------------------------------------------
1014 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode
& mode
)
1016 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
1017 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
1019 HRESULT hr
= m_pDD2
->SetCooperativeLevel
1022 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
1026 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr
);
1031 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1034 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr
);
1042 #endif // wxUSE_DIRECTDRAW
1044 #endif // wxUSE_DISPLAY