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"
32 #include "wx/dynarray.h"
36 #include "wx/dynload.h"
37 #include "wx/sysopt.h"
39 #include "wx/display.h"
40 #include "wx/display_impl.h"
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
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
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
65 #endif // !__WXWINCE__
67 #ifdef wxUSE_DIRECTDRAW
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
76 // ----------------------------------------------------------------------------
77 // typedefs for dynamically loaded Windows functions
78 // ----------------------------------------------------------------------------
80 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
86 #ifdef wxUSE_DIRECTDRAW
87 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
88 LPTSTR driverDescription
,
93 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
97 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
100 #endif // wxUSE_DIRECTDRAW
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
);
108 // emulation of ChangeDisplaySettingsEx() for Win95
109 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
113 LPVOID
WXUNUSED(lParam
))
115 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
117 #endif // !__WXWINCE__
119 // ----------------------------------------------------------------------------
120 // display information classes
121 // ----------------------------------------------------------------------------
125 wxDisplayInfo(HMONITOR hmon
= NULL
)
131 virtual ~wxDisplayInfo() { }
134 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
135 // hadn't been done before)
139 // handle of this monitor used by MonitorXXX() functions, never NULL
142 // the entire area of this monitor in virtual screen coordinates
145 // the display device name for this monitor, empty initially and retrieved
146 // on demand by DoGetName()
149 // the flags of this monitor, also used as initialization marker: if this
150 // is -1, GetMonitorInfo() hadn't been called yet
154 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
156 // ----------------------------------------------------------------------------
157 // common base class for all Win32 wxDisplayImpl versions
158 // ----------------------------------------------------------------------------
160 class wxDisplayImplWin32Base
: public wxDisplayImpl
163 wxDisplayImplWin32Base(size_t n
, wxDisplayInfo
& info
)
169 virtual wxRect
GetGeometry() const;
170 virtual wxString
GetName() const;
171 virtual bool IsPrimary() const;
173 virtual wxVideoMode
GetCurrentMode() const;
176 // convert a DEVMODE to our wxVideoMode
177 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
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
182 return wxVideoMode(dm
.dmPelsWidth
,
185 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
188 wxDisplayInfo
& m_info
;
191 // ----------------------------------------------------------------------------
192 // common base class for all Win32 wxDisplayFactory versions
193 // ----------------------------------------------------------------------------
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
;
200 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
203 virtual ~wxDisplayFactoryWin32Base();
205 bool IsOk() const { return !m_displays
.empty(); }
207 virtual size_t GetCount() { return m_displays
.size(); }
208 virtual int GetFromPoint(const wxPoint
& pt
);
209 virtual int GetFromWindow(wxWindow
*window
);
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();
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
222 // find the monitor corresponding to the given handle, return wxNOT_FOUND
224 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
227 // flag indicating whether gs_MonitorXXX functions are available
228 static int ms_supportsMultimon
;
230 // the array containing information about all available displays, should be
231 // filled by the derived class ctors
232 wxDisplayInfoArray m_displays
;
235 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
)
238 // ----------------------------------------------------------------------------
239 // wxDisplay implementation using Windows multi-monitor support functions
240 // ----------------------------------------------------------------------------
242 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
245 wxDisplayImplMultimon(size_t n
, wxDisplayInfo
& info
)
246 : wxDisplayImplWin32Base(n
, info
)
250 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
251 virtual bool ChangeMode(const wxVideoMode
& mode
);
254 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
)
257 class WXDLLEXPORT wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
260 wxDisplayFactoryMultimon();
262 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
265 // EnumDisplayMonitors() callback
266 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
272 // add a monitor description to m_displays array
273 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
276 // ----------------------------------------------------------------------------
277 // wxDisplay implementation using DirectDraw
278 // ----------------------------------------------------------------------------
280 #ifdef wxUSE_DIRECTDRAW
282 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
284 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
285 : wxDisplayInfo(hmon
),
292 virtual ~wxDisplayInfoDirectDraw()
299 // IDirectDraw object used to control this display, may be NULL
300 IDirectDraw2
*m_pDD2
;
302 // DirectDraw GUID for this display, only valid when using DirectDraw
306 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
)
309 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
312 wxDisplayImplDirectDraw(size_t n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
313 : wxDisplayImplWin32Base(n
, info
),
319 virtual ~wxDisplayImplDirectDraw()
324 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
325 virtual bool ChangeMode(const wxVideoMode
& mode
);
328 IDirectDraw2
*m_pDD2
;
330 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
)
333 class WXDLLEXPORT wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
336 wxDisplayFactoryDirectDraw();
337 virtual ~wxDisplayFactoryDirectDraw();
339 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
342 // callback used with DirectDrawEnumerateEx()
343 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
344 LPTSTR driverDescription
,
349 // add a monitor description to m_displays array
350 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
354 wxDynamicLibrary m_dllDDraw
;
356 // dynamically resolved DirectDrawCreate()
357 DirectDrawCreate_t m_pfnDirectDrawCreate
;
359 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
)
362 #endif // wxUSE_DIRECTDRAW
365 // ============================================================================
366 // common classes implementation
367 // ============================================================================
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
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
379 #ifdef wxUSE_DIRECTDRAW
380 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
382 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
383 if ( factoryDD
->IsOk() )
388 #endif // wxUSE_DIRECTDRAW
390 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
391 if ( factoryMM
->IsOk() )
397 // finally fall back to a stub implementation if all else failed (Win95?)
398 return new wxDisplayFactorySingle
;
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 void wxDisplayInfo::Initialize()
407 if ( m_flags
== (DWORD
)-1 )
409 WinStruct
<MONITORINFOEX
> monInfo
;
410 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
412 wxLogLastError(_T("GetMonitorInfo"));
417 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
418 m_devName
= monInfo
.szDevice
;
419 m_flags
= monInfo
.dwFlags
;
423 // ----------------------------------------------------------------------------
424 // wxDisplayImplWin32Base
425 // ----------------------------------------------------------------------------
427 wxRect
wxDisplayImplWin32Base::GetGeometry() const
429 if ( m_info
.m_rect
.IsEmpty() )
432 return m_info
.m_rect
;
435 wxString
wxDisplayImplWin32Base::GetName() const
437 if ( m_info
.m_devName
.IsEmpty() )
440 return m_info
.m_devName
;
443 bool wxDisplayImplWin32Base::IsPrimary() const
445 if ( m_info
.m_flags
== (DWORD
)-1 )
448 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
451 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
455 // The first parameter of EnumDisplaySettings() must be NULL under Win95
456 // according to MSDN. The version of GetName() we implement for Win95
457 // returns an empty string.
458 const wxString name
= GetName();
459 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
462 dm
.dmSize
= sizeof(dm
);
463 dm
.dmDriverExtra
= 0;
464 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
466 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
470 mode
= ConvertToVideoMode(dm
);
476 // ----------------------------------------------------------------------------
477 // wxDisplayFactoryWin32Base
478 // ----------------------------------------------------------------------------
480 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
482 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
484 if ( ms_supportsMultimon
== -1 )
486 ms_supportsMultimon
= 0;
488 wxDynamicLibrary
dllUser32(_T("user32.dll"));
492 gs_MonitorFromPoint
= (MonitorFromPoint_t
)
493 dllUser32
.GetSymbol(wxT("MonitorFromPoint"));
494 if ( !gs_MonitorFromPoint
)
497 gs_MonitorFromWindow
= (MonitorFromWindow_t
)
498 dllUser32
.GetSymbol(wxT("MonitorFromWindow"));
499 if ( !gs_MonitorFromWindow
)
502 gs_GetMonitorInfo
= (GetMonitorInfo_t
)
503 dllUser32
.GetSymbolAorW(wxT("GetMonitorInfo"));
504 if ( !gs_GetMonitorInfo
)
507 ms_supportsMultimon
= 1;
509 // we can safely let dllUser32 go out of scope, the DLL itself will
510 // still remain loaded as all Win32 programs use it
514 void wxDisplayFactoryWin32Base::Clear()
516 WX_CLEAR_ARRAY(m_displays
);
519 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
524 // helper for GetFromPoint() and GetFromWindow()
525 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
529 const size_t count
= m_displays
.size();
530 for ( size_t n
= 0; n
< count
; n
++ )
532 if ( hmon
== m_displays
[n
]->m_hmon
)
540 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
546 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
547 MONITOR_DEFAULTTONULL
));
550 int wxDisplayFactoryWin32Base::GetFromWindow(wxWindow
*window
)
552 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
553 MONITOR_DEFAULTTONULL
));
556 // ============================================================================
557 // wxDisplay implementation using Win32 multimon API
558 // ============================================================================
560 // ----------------------------------------------------------------------------
561 // wxDisplayFactoryMultimon initialization
562 // ----------------------------------------------------------------------------
564 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
566 if ( !ms_supportsMultimon
)
569 // look up EnumDisplayMonitors() which we don't need with DirectDraw
571 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
575 wxDynamicLibrary
dllUser32(_T("user32.dll"));
576 pfnEnumDisplayMonitors
= (EnumDisplayMonitors_t
)
577 dllUser32
.GetSymbol(wxT("EnumDisplayMonitors"));
578 if ( !pfnEnumDisplayMonitors
)
582 // enumerate all displays
583 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
585 wxLogLastError(wxT("EnumDisplayMonitors"));
591 wxDisplayFactoryMultimon::MultimonEnumProc(
592 HMONITOR hMonitor
, // handle to display monitor
593 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
594 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
595 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
598 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
599 self
->AddDisplay(hMonitor
, lprcMonitor
);
601 // continue the enumeration
605 // ----------------------------------------------------------------------------
606 // wxDisplayFactoryMultimon helper functions
607 // ----------------------------------------------------------------------------
609 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
611 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
613 // we also store the display geometry
614 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
615 lprcMonitor
->right
- lprcMonitor
->left
,
616 lprcMonitor
->bottom
- lprcMonitor
->top
);
618 // now add this monitor to the array
619 m_displays
.Add(info
);
622 // ----------------------------------------------------------------------------
623 // wxDisplayFactoryMultimon inherited pure virtuals implementation
624 // ----------------------------------------------------------------------------
626 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(size_t n
)
628 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
630 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
633 // ----------------------------------------------------------------------------
634 // wxDisplayImplMultimon implementation
635 // ----------------------------------------------------------------------------
638 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
640 wxArrayVideoModes modes
;
642 // The first parameter of EnumDisplaySettings() must be NULL under Win95
643 // according to MSDN. The version of GetName() we implement for Win95
644 // returns an empty string.
645 const wxString name
= GetName();
646 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
649 dm
.dmSize
= sizeof(dm
);
650 dm
.dmDriverExtra
= 0;
651 for ( int iModeNum
= 0;
652 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
655 const wxVideoMode mode
= ConvertToVideoMode(dm
);
656 if ( mode
.Matches(modeMatch
) )
665 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
667 // prepare ChangeDisplaySettingsEx() parameters
673 if ( mode
== wxDefaultVideoMode
)
675 // reset the video mode to default
679 else // change to the given mode
681 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
682 _T("at least the width and height must be specified") );
685 dm
.dmSize
= sizeof(dm
);
686 dm
.dmDriverExtra
= 0;
687 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
688 dm
.dmPelsWidth
= mode
.w
;
689 dm
.dmPelsHeight
= mode
.h
;
693 dm
.dmFields
|= DM_BITSPERPEL
;
694 dm
.dmBitsPerPel
= mode
.bpp
;
699 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
700 dm
.dmDisplayFrequency
= mode
.refresh
;
707 #else // !__WXWINCE__
708 flags
= CDS_FULLSCREEN
;
709 #endif // __WXWINCE__/!__WXWINCE__
713 // get pointer to the function dynamically
715 // we're only called from the main thread, so it's ok to use static
717 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
718 if ( !pfnChangeDisplaySettingsEx
)
720 wxDynamicLibrary
dllUser32(_T("user32.dll"));
721 if ( dllUser32
.IsLoaded() )
723 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
724 dllUser32
.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
726 //else: huh, no user32.dll??
729 if ( !pfnChangeDisplaySettingsEx
)
731 // we must be under Win95 and so there is no multiple monitors
733 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
735 #endif // !__WXWINCE__
738 // do change the mode
739 switch ( pfnChangeDisplaySettingsEx
741 GetName(), // display name
742 pDevMode
, // dev mode or NULL to reset
745 NULL
// pointer to video parameters (not used)
748 case DISP_CHANGE_SUCCESSFUL
:
751 // If we have a top-level, full-screen frame, emulate
752 // the DirectX behavior and resize it. This makes this
753 // API quite a bit easier to use.
754 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
755 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
756 if (frameTop
&& frameTop
->IsFullScreen())
758 wxVideoMode current
= GetCurrentMode();
759 frameTop
->SetClientSize(current
.w
, current
.h
);
764 case DISP_CHANGE_BADMODE
:
765 // don't complain about this, this is the only "expected" error
769 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
776 // ============================================================================
777 // DirectDraw-based wxDisplay implementation
778 // ============================================================================
782 // ----------------------------------------------------------------------------
783 // wxDisplayFactoryDirectDraw initialization
784 // ----------------------------------------------------------------------------
786 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
788 if ( !ms_supportsMultimon
)
792 // suppress the errors if ddraw.dll is not found, we're prepared to handle
797 m_dllDDraw
.Load(_T("ddraw.dll"));
799 if ( !m_dllDDraw
.IsLoaded() )
802 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
803 m_dllDDraw
.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
807 // we can't continue without DirectDrawCreate() later, so resolve it right
808 // now and fail the initialization if it's not available
809 m_pfnDirectDrawCreate
= (DirectDrawCreate_t
)
810 m_dllDDraw
.GetSymbol(_T("DirectDrawCreate"));
811 if ( !m_pfnDirectDrawCreate
)
814 if ( (*pDDEnumEx
)(DDEnumExCallback
,
816 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
818 wxLogLastError(_T("DirectDrawEnumerateEx"));
822 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
824 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
825 // calling m_pDD2->Release() later would crash
829 // ----------------------------------------------------------------------------
830 // callbacks for monitor/modes enumeration stuff
831 // ----------------------------------------------------------------------------
834 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID
*pGuid
,
835 LPTSTR
WXUNUSED(driverDescription
),
842 wxDisplayFactoryDirectDraw
* self
=
843 wx_static_cast(wxDisplayFactoryDirectDraw
*, lpContext
);
844 self
->AddDisplay(*pGuid
, hmon
, driverName
);
846 //else: we're called for the primary monitor, skip it
848 // continue the enumeration
852 // ----------------------------------------------------------------------------
853 // wxDisplayFactoryDirectDraw helpers
854 // ----------------------------------------------------------------------------
856 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID
& guid
,
860 m_displays
.Add(new wxDisplayInfoDirectDraw(guid
, hmon
, name
));
863 // ----------------------------------------------------------------------------
864 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
865 // ----------------------------------------------------------------------------
867 wxDisplayImpl
*wxDisplayFactoryDirectDraw::CreateDisplay(size_t n
)
869 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
871 wxDisplayInfoDirectDraw
*
872 info
= wx_static_cast(wxDisplayInfoDirectDraw
*, m_displays
[n
]);
877 GUID
guid(info
->m_guid
);
878 HRESULT hr
= (*m_pfnDirectDrawCreate
)(&guid
, &pDD
, NULL
);
880 if ( FAILED(hr
) || !pDD
)
883 wxLogApiError(_T("DirectDrawCreate"), hr
);
887 // we got IDirectDraw, but we need IDirectDraw2
888 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&info
->m_pDD2
);
891 if ( FAILED(hr
) || !info
->m_pDD2
)
893 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
897 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
898 // which is ok as we don't want to recreate DD objects all the time
900 //else: DirectDraw object corresponding to our display already exists
902 return new wxDisplayImplDirectDraw(n
, *info
, info
->m_pDD2
);
905 // ============================================================================
906 // wxDisplayImplDirectDraw
907 // ============================================================================
909 // ----------------------------------------------------------------------------
910 // video modes enumeration
911 // ----------------------------------------------------------------------------
913 // tiny helper class used to pass information from GetModes() to
914 // wxDDEnumModesCallback
915 class wxDDVideoModesAdder
918 // our Add() method will add modes matching modeMatch to modes array
919 wxDDVideoModesAdder(wxArrayVideoModes
& modes
, const wxVideoMode
& modeMatch
)
921 m_modeMatch(modeMatch
)
925 void Add(const wxVideoMode
& mode
)
927 if ( mode
.Matches(m_modeMatch
) )
932 wxArrayVideoModes
& m_modes
;
933 const wxVideoMode
& m_modeMatch
;
935 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder
)
938 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
941 // we need at least the mode size
942 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
943 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
945 wxDDVideoModesAdder
* const vmodes
=
946 wx_static_cast(wxDDVideoModesAdder
*, lpContext
);
948 vmodes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
949 lpDDSurfaceDesc
->dwHeight
,
950 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
951 lpDDSurfaceDesc
->dwRefreshRate
));
954 // continue the enumeration
959 wxDisplayImplDirectDraw::GetModes(const wxVideoMode
& modeMatch
) const
961 wxArrayVideoModes modes
;
962 wxDDVideoModesAdder
modesAdder(modes
, modeMatch
);
964 HRESULT hr
= m_pDD2
->EnumDisplayModes
968 &modesAdder
, // callback parameter
969 wxDDEnumModesCallback
974 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
980 // ----------------------------------------------------------------------------
981 // video mode switching
982 // ----------------------------------------------------------------------------
984 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode
& mode
)
986 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
987 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
989 HRESULT hr
= m_pDD2
->SetCooperativeLevel
992 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
996 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr
);
1001 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1004 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr
);
1012 #endif // wxUSE_DIRECTDRAW
1014 #endif // wxUSE_DISPLAY