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 // display functions are found in different DLLs under WinCE and normal Win32
91 static const wxChar displayDllName
[] = _T("coredll.dll");
93 static const wxChar displayDllName
[] = _T("user32.dll");
96 // ----------------------------------------------------------------------------
97 // typedefs for dynamically loaded Windows functions
98 // ----------------------------------------------------------------------------
100 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
107 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
108 LPTSTR driverDescription
,
113 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
117 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
118 LPDIRECTDRAW
*lplpDD
,
119 IUnknown
*pUnkOuter
);
120 #endif // wxUSE_DIRECTDRAW
122 typedef BOOL (WINAPI
*EnumDisplayMonitors_t
)(HDC
,LPCRECT
,MONITORENUMPROC
,LPARAM
);
123 typedef HMONITOR (WINAPI
*MonitorFromPoint_t
)(POINT
,DWORD
);
124 typedef HMONITOR (WINAPI
*MonitorFromWindow_t
)(HWND
,DWORD
);
125 typedef BOOL (WINAPI
*GetMonitorInfo_t
)(HMONITOR
,LPMONITORINFO
);
128 // emulation of ChangeDisplaySettingsEx() for Win95
129 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
133 LPVOID
WXUNUSED(lParam
))
135 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
137 #endif // !__WXWINCE__
139 // ----------------------------------------------------------------------------
140 // display information classes
141 // ----------------------------------------------------------------------------
145 wxDisplayInfo(HMONITOR hmon
= NULL
)
151 virtual ~wxDisplayInfo() { }
154 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
155 // hadn't been done before)
159 // handle of this monitor used by MonitorXXX() functions, never NULL
162 // the entire area of this monitor in virtual screen coordinates
165 // the work or client area, i.e. the area available for the normal windows
168 // the display device name for this monitor, empty initially and retrieved
169 // on demand by DoGetName()
172 // the flags of this monitor, also used as initialization marker: if this
173 // is -1, GetMonitorInfo() hadn't been called yet
177 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
179 // ----------------------------------------------------------------------------
180 // common base class for all Win32 wxDisplayImpl versions
181 // ----------------------------------------------------------------------------
183 class wxDisplayImplWin32Base
: public wxDisplayImpl
186 wxDisplayImplWin32Base(unsigned n
, wxDisplayInfo
& info
)
192 virtual wxRect
GetGeometry() const;
193 virtual wxRect
GetClientArea() const;
194 virtual wxString
GetName() const;
195 virtual bool IsPrimary() const;
197 virtual wxVideoMode
GetCurrentMode() const;
200 // convert a DEVMODE to our wxVideoMode
201 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
203 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
204 // and although 0 is ok for us we don't want to return modes with 1hz
206 return wxVideoMode(dm
.dmPelsWidth
,
209 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
212 wxDisplayInfo
& m_info
;
215 // ----------------------------------------------------------------------------
216 // common base class for all Win32 wxDisplayFactory versions
217 // ----------------------------------------------------------------------------
219 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
220 static MonitorFromPoint_t gs_MonitorFromPoint
= NULL
;
221 static MonitorFromWindow_t gs_MonitorFromWindow
= NULL
;
222 static GetMonitorInfo_t gs_GetMonitorInfo
= NULL
;
224 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
227 virtual ~wxDisplayFactoryWin32Base();
229 bool IsOk() const { return !m_displays
.empty(); }
231 virtual unsigned GetCount() { return unsigned(m_displays
.size()); }
232 virtual int GetFromPoint(const wxPoint
& pt
);
233 virtual int GetFromWindow(const wxWindow
*window
);
236 // ctor checks if the current system supports multimon API and dynamically
237 // bind the functions we need if this is the case and sets
238 // ms_supportsMultimon if they're available
239 wxDisplayFactoryWin32Base();
241 // delete all m_displays elements: can be called from the derived class
242 // dtor if it is important to do this before destroying it (as in
243 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
246 // find the monitor corresponding to the given handle, return wxNOT_FOUND
248 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
251 // flag indicating whether gs_MonitorXXX functions are available
252 static int ms_supportsMultimon
;
254 // the array containing information about all available displays, should be
255 // filled by the derived class ctors
256 wxDisplayInfoArray m_displays
;
259 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
)
262 // ----------------------------------------------------------------------------
263 // wxDisplay implementation using Windows multi-monitor support functions
264 // ----------------------------------------------------------------------------
266 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
269 wxDisplayImplMultimon(unsigned n
, wxDisplayInfo
& info
)
270 : wxDisplayImplWin32Base(n
, info
)
274 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
275 virtual bool ChangeMode(const wxVideoMode
& mode
);
278 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
)
281 class wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
284 wxDisplayFactoryMultimon();
286 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
289 // EnumDisplayMonitors() callback
290 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
296 // add a monitor description to m_displays array
297 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
300 // ----------------------------------------------------------------------------
301 // wxDisplay implementation using DirectDraw
302 // ----------------------------------------------------------------------------
306 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
308 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
309 : wxDisplayInfo(hmon
),
316 virtual ~wxDisplayInfoDirectDraw()
323 // IDirectDraw object used to control this display, may be NULL
324 IDirectDraw2
*m_pDD2
;
326 // DirectDraw GUID for this display, only valid when using DirectDraw
330 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
)
333 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
336 wxDisplayImplDirectDraw(unsigned n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
337 : wxDisplayImplWin32Base(n
, info
),
343 virtual ~wxDisplayImplDirectDraw()
348 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
349 virtual bool ChangeMode(const wxVideoMode
& mode
);
352 IDirectDraw2
*m_pDD2
;
354 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
)
357 class wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
360 wxDisplayFactoryDirectDraw();
361 virtual ~wxDisplayFactoryDirectDraw();
363 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
366 // callback used with DirectDrawEnumerateEx()
367 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
368 LPTSTR driverDescription
,
373 // add a monitor description to m_displays array
374 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
378 wxDynamicLibrary m_dllDDraw
;
380 // dynamically resolved DirectDrawCreate()
381 DirectDrawCreate_t m_pfnDirectDrawCreate
;
383 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
)
386 #endif // wxUSE_DIRECTDRAW
389 // ============================================================================
390 // common classes implementation
391 // ============================================================================
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
399 // we have 2 implementations for modern Windows: one using standard Win32
400 // and another using DirectDraw, the choice between them is done using a
404 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
406 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
407 if ( factoryDD
->IsOk() )
412 #endif // wxUSE_DIRECTDRAW
414 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
415 if ( factoryMM
->IsOk() )
421 // finally fall back to a stub implementation if all else failed (Win95?)
422 return new wxDisplayFactorySingle
;
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 void wxDisplayInfo::Initialize()
431 if ( m_flags
== (DWORD
)-1 )
433 WinStruct
<MONITORINFOEX
> monInfo
;
434 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
436 wxLogLastError(_T("GetMonitorInfo"));
441 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
442 wxCopyRECTToRect(monInfo
.rcWork
, m_rectClient
);
443 m_devName
= monInfo
.szDevice
;
444 m_flags
= monInfo
.dwFlags
;
448 // ----------------------------------------------------------------------------
449 // wxDisplayImplWin32Base
450 // ----------------------------------------------------------------------------
452 wxRect
wxDisplayImplWin32Base::GetGeometry() const
454 if ( m_info
.m_rect
.IsEmpty() )
457 return m_info
.m_rect
;
460 wxRect
wxDisplayImplWin32Base::GetClientArea() const
462 if ( m_info
.m_rectClient
.IsEmpty() )
465 return m_info
.m_rectClient
;
468 wxString
wxDisplayImplWin32Base::GetName() const
470 if ( m_info
.m_devName
.empty() )
473 return m_info
.m_devName
;
476 bool wxDisplayImplWin32Base::IsPrimary() const
478 if ( m_info
.m_flags
== (DWORD
)-1 )
481 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
484 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
488 // The first parameter of EnumDisplaySettings() must be NULL under Win95
489 // according to MSDN. The version of GetName() we implement for Win95
490 // returns an empty string.
491 const wxString name
= GetName();
492 const wxChar
* const deviceName
= name
.empty()
493 ? (const wxChar
*)NULL
494 : (const wxChar
*)name
.c_str();
497 dm
.dmSize
= sizeof(dm
);
498 dm
.dmDriverExtra
= 0;
499 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
501 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
505 mode
= ConvertToVideoMode(dm
);
511 // ----------------------------------------------------------------------------
512 // wxDisplayFactoryWin32Base
513 // ----------------------------------------------------------------------------
515 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
517 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
519 if ( ms_supportsMultimon
== -1 )
521 ms_supportsMultimon
= 0;
523 wxDynamicLibrary
dllDisplay(displayDllName
, wxDL_VERBATIM
| wxDL_QUIET
);
525 if ( (wxDL_INIT_FUNC(gs_
, MonitorFromPoint
, dllDisplay
)) == NULL
||
526 (wxDL_INIT_FUNC(gs_
, MonitorFromWindow
, dllDisplay
)) == NULL
||
527 (wxDL_INIT_FUNC_AW(gs_
, GetMonitorInfo
, dllDisplay
)) == NULL
)
530 ms_supportsMultimon
= 1;
532 // we can safely let dllDisplay go out of scope, the DLL itself will
533 // still remain loaded as all programs link to it statically anyhow
537 void wxDisplayFactoryWin32Base::Clear()
539 WX_CLEAR_ARRAY(m_displays
);
542 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
547 // helper for GetFromPoint() and GetFromWindow()
548 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
552 const size_t count
= m_displays
.size();
553 for ( size_t n
= 0; n
< count
; n
++ )
555 if ( hmon
== m_displays
[n
]->m_hmon
)
563 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
569 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
570 MONITOR_DEFAULTTONULL
));
573 int wxDisplayFactoryWin32Base::GetFromWindow(const wxWindow
*window
)
575 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
576 MONITOR_DEFAULTTONULL
));
579 // ============================================================================
580 // wxDisplay implementation using Win32 multimon API
581 // ============================================================================
583 // ----------------------------------------------------------------------------
584 // wxDisplayFactoryMultimon initialization
585 // ----------------------------------------------------------------------------
587 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
589 if ( !ms_supportsMultimon
)
592 // look up EnumDisplayMonitors() which we don't need with DirectDraw
594 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
596 wxDynamicLibrary
dllDisplay(displayDllName
, wxDL_VERBATIM
| wxDL_QUIET
);
597 if ( (wxDL_INIT_FUNC(pfn
, EnumDisplayMonitors
, dllDisplay
)) == NULL
)
601 // enumerate all displays
602 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
604 wxLogLastError(wxT("EnumDisplayMonitors"));
610 wxDisplayFactoryMultimon::MultimonEnumProc(
611 HMONITOR hMonitor
, // handle to display monitor
612 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
613 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
614 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
617 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
618 self
->AddDisplay(hMonitor
, lprcMonitor
);
620 // continue the enumeration
624 // ----------------------------------------------------------------------------
625 // wxDisplayFactoryMultimon helper functions
626 // ----------------------------------------------------------------------------
628 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
630 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
632 // we also store the display geometry
633 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
634 lprcMonitor
->right
- lprcMonitor
->left
,
635 lprcMonitor
->bottom
- lprcMonitor
->top
);
637 // now add this monitor to the array
638 m_displays
.Add(info
);
641 // ----------------------------------------------------------------------------
642 // wxDisplayFactoryMultimon inherited pure virtuals implementation
643 // ----------------------------------------------------------------------------
645 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(unsigned n
)
647 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
649 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
652 // ----------------------------------------------------------------------------
653 // wxDisplayImplMultimon implementation
654 // ----------------------------------------------------------------------------
657 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
659 wxArrayVideoModes modes
;
661 // The first parameter of EnumDisplaySettings() must be NULL under Win95
662 // according to MSDN. The version of GetName() we implement for Win95
663 // returns an empty string.
664 const wxString name
= GetName();
665 const wxChar
* const deviceName
= name
.empty()
666 ? (const wxChar
*)NULL
667 : (const wxChar
*)name
.c_str();
670 dm
.dmSize
= sizeof(dm
);
671 dm
.dmDriverExtra
= 0;
672 for ( int iModeNum
= 0;
673 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
676 const wxVideoMode mode
= ConvertToVideoMode(dm
);
677 if ( mode
.Matches(modeMatch
) )
686 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
688 // prepare ChangeDisplaySettingsEx() parameters
694 if ( mode
== wxDefaultVideoMode
)
696 // reset the video mode to default
700 else // change to the given mode
702 wxCHECK_MSG( mode
.GetWidth() && mode
.GetHeight(), false,
703 _T("at least the width and height must be specified") );
706 dm
.dmSize
= sizeof(dm
);
707 dm
.dmDriverExtra
= 0;
708 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
709 dm
.dmPelsWidth
= mode
.GetWidth();
710 dm
.dmPelsHeight
= mode
.GetHeight();
712 if ( mode
.GetDepth() )
714 dm
.dmFields
|= DM_BITSPERPEL
;
715 dm
.dmBitsPerPel
= mode
.GetDepth();
718 if ( mode
.GetRefresh() )
720 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
721 dm
.dmDisplayFrequency
= mode
.GetRefresh();
728 #else // !__WXWINCE__
729 flags
= CDS_FULLSCREEN
;
730 #endif // __WXWINCE__/!__WXWINCE__
734 // get pointer to the function dynamically
736 // we're only called from the main thread, so it's ok to use static
738 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
739 if ( !pfnChangeDisplaySettingsEx
)
741 wxDynamicLibrary
dllDisplay(displayDllName
, wxDL_VERBATIM
| wxDL_QUIET
);
742 if ( dllDisplay
.IsLoaded() )
744 wxDL_INIT_FUNC_AW(pfn
, ChangeDisplaySettingsEx
, dllDisplay
);
746 //else: huh, no this DLL must always be present, what's going on??
749 if ( !pfnChangeDisplaySettingsEx
)
751 // we must be under Win95 and so there is no multiple monitors
753 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
755 #endif // !__WXWINCE__
758 // do change the mode
759 switch ( pfnChangeDisplaySettingsEx
761 GetName().wx_str(), // display name
762 pDevMode
, // dev mode or NULL to reset
765 NULL
// pointer to video parameters (not used)
768 case DISP_CHANGE_SUCCESSFUL
:
771 // If we have a top-level, full-screen frame, emulate
772 // the DirectX behavior and resize it. This makes this
773 // API quite a bit easier to use.
774 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
775 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
776 if (frameTop
&& frameTop
->IsFullScreen())
778 wxVideoMode current
= GetCurrentMode();
779 frameTop
->SetClientSize(current
.GetWidth(), current
.GetHeight());
784 case DISP_CHANGE_BADMODE
:
785 // don't complain about this, this is the only "expected" error
789 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
796 // ============================================================================
797 // DirectDraw-based wxDisplay implementation
798 // ============================================================================
802 // ----------------------------------------------------------------------------
803 // wxDisplayFactoryDirectDraw initialization
804 // ----------------------------------------------------------------------------
806 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
808 if ( !ms_supportsMultimon
)
811 m_dllDDraw
.Load(_T("ddraw.dll"), wxDL_VERBATIM
| wxDL_QUIET
);
813 if ( !m_dllDDraw
.IsLoaded() )
816 DirectDrawEnumerateEx_t
817 wxDL_INIT_FUNC_AW(pfn
, DirectDrawEnumerateEx
, m_dllDDraw
);
818 if ( !pfnDirectDrawEnumerateEx
)
821 // we can't continue without DirectDrawCreate() later, so resolve it right
822 // now and fail the initialization if it's not available
823 if ( !wxDL_INIT_FUNC(m_pfn
, DirectDrawCreate
, m_dllDDraw
) )
826 if ( (*pfnDirectDrawEnumerateEx
)(DDEnumExCallback
,
828 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
830 wxLogLastError(_T("DirectDrawEnumerateEx"));
834 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
836 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
837 // calling m_pDD2->Release() later would crash
841 // ----------------------------------------------------------------------------
842 // callbacks for monitor/modes enumeration stuff
843 // ----------------------------------------------------------------------------
846 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID
*pGuid
,
847 LPTSTR
WXUNUSED(driverDescription
),
854 wxDisplayFactoryDirectDraw
* self
=
855 wx_static_cast(wxDisplayFactoryDirectDraw
*, lpContext
);
856 self
->AddDisplay(*pGuid
, hmon
, driverName
);
858 //else: we're called for the primary monitor, skip it
860 // continue the enumeration
864 // ----------------------------------------------------------------------------
865 // wxDisplayFactoryDirectDraw helpers
866 // ----------------------------------------------------------------------------
868 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID
& guid
,
872 m_displays
.Add(new wxDisplayInfoDirectDraw(guid
, hmon
, name
));
875 // ----------------------------------------------------------------------------
876 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
877 // ----------------------------------------------------------------------------
879 wxDisplayImpl
*wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n
)
881 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
883 wxDisplayInfoDirectDraw
*
884 info
= wx_static_cast(wxDisplayInfoDirectDraw
*, m_displays
[n
]);
889 GUID
guid(info
->m_guid
);
890 HRESULT hr
= (*m_pfnDirectDrawCreate
)(&guid
, &pDD
, NULL
);
892 if ( FAILED(hr
) || !pDD
)
895 wxLogApiError(_T("DirectDrawCreate"), hr
);
899 // we got IDirectDraw, but we need IDirectDraw2
900 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&info
->m_pDD2
);
903 if ( FAILED(hr
) || !info
->m_pDD2
)
905 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
909 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
910 // which is ok as we don't want to recreate DD objects all the time
912 //else: DirectDraw object corresponding to our display already exists
914 return new wxDisplayImplDirectDraw(n
, *info
, info
->m_pDD2
);
917 // ============================================================================
918 // wxDisplayImplDirectDraw
919 // ============================================================================
921 // ----------------------------------------------------------------------------
922 // video modes enumeration
923 // ----------------------------------------------------------------------------
925 // tiny helper class used to pass information from GetModes() to
926 // wxDDEnumModesCallback
927 class wxDDVideoModesAdder
930 // our Add() method will add modes matching modeMatch to modes array
931 wxDDVideoModesAdder(wxArrayVideoModes
& modes
, const wxVideoMode
& modeMatch
)
933 m_modeMatch(modeMatch
)
937 void Add(const wxVideoMode
& mode
)
939 if ( mode
.Matches(m_modeMatch
) )
944 wxArrayVideoModes
& m_modes
;
945 const wxVideoMode
& m_modeMatch
;
947 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder
)
950 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
953 // we need at least the mode size
954 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
955 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
957 wxDDVideoModesAdder
* const vmodes
=
958 wx_static_cast(wxDDVideoModesAdder
*, lpContext
);
960 vmodes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
961 lpDDSurfaceDesc
->dwHeight
,
962 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
963 lpDDSurfaceDesc
->dwRefreshRate
));
966 // continue the enumeration
971 wxDisplayImplDirectDraw::GetModes(const wxVideoMode
& modeMatch
) const
973 wxArrayVideoModes modes
;
974 wxDDVideoModesAdder
modesAdder(modes
, modeMatch
);
976 HRESULT hr
= m_pDD2
->EnumDisplayModes
980 &modesAdder
, // callback parameter
981 wxDDEnumModesCallback
986 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
992 // ----------------------------------------------------------------------------
993 // video mode switching
994 // ----------------------------------------------------------------------------
996 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode
& mode
)
998 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
999 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
1001 HRESULT hr
= m_pDD2
->SetCooperativeLevel
1004 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
1008 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr
);
1013 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1016 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr
);
1024 #endif // wxUSE_DIRECTDRAW
1026 #endif // wxUSE_DISPLAY