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 typedef BOOL(CALLBACK
* MONITORENUMPROC
)(HMONITOR
, HDC
, LPRECT
, LPARAM
);
64 typedef struct tagMONITORINFO
70 } MONITORINFO
, *LPMONITORINFO
;
71 typedef struct tagMONITORINFOEX
: public tagMONITORINFO
73 TCHAR szDevice
[CCHDEVICENAME
];
74 } MONITORINFOEX
, *LPMONITORINFOEX
;
75 #define MONITOR_DEFAULTTONULL 0x00000000
76 #define MONITOR_DEFAULTTOPRIMARY 0x00000001
77 #define MONITOR_DEFAULTTONEAREST 0x00000002
78 #define MONITORINFOF_PRIMARY 0x00000001
79 #define HMONITOR_DECLARED
81 #endif // !__WXWINCE__
83 #ifdef wxUSE_DIRECTDRAW
86 // we don't want to link with ddraw.lib which contains the real
87 // IID_IDirectDraw2 definition
88 const GUID wxIID_IDirectDraw2
=
89 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
90 #endif // wxUSE_DIRECTDRAW
92 // ----------------------------------------------------------------------------
93 // typedefs for dynamically loaded Windows functions
94 // ----------------------------------------------------------------------------
96 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
102 #ifdef wxUSE_DIRECTDRAW
103 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
104 LPTSTR driverDescription
,
109 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
113 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
114 LPDIRECTDRAW
*lplpDD
,
115 IUnknown
*pUnkOuter
);
116 #endif // wxUSE_DIRECTDRAW
118 typedef BOOL (WINAPI
*EnumDisplayMonitors_t
)(HDC
,LPCRECT
,MONITORENUMPROC
,LPARAM
);
119 typedef HMONITOR (WINAPI
*MonitorFromPoint_t
)(POINT
,DWORD
);
120 typedef HMONITOR (WINAPI
*MonitorFromWindow_t
)(HWND
,DWORD
);
121 typedef BOOL (WINAPI
*GetMonitorInfo_t
)(HMONITOR
,LPMONITORINFO
);
124 // emulation of ChangeDisplaySettingsEx() for Win95
125 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
129 LPVOID
WXUNUSED(lParam
))
131 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
133 #endif // !__WXWINCE__
135 // ----------------------------------------------------------------------------
136 // display information classes
137 // ----------------------------------------------------------------------------
141 wxDisplayInfo(HMONITOR hmon
= NULL
)
147 virtual ~wxDisplayInfo() { }
150 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
151 // hadn't been done before)
155 // handle of this monitor used by MonitorXXX() functions, never NULL
158 // the entire area of this monitor in virtual screen coordinates
161 // the work or client area, i.e. the area available for the normal windows
164 // the display device name for this monitor, empty initially and retrieved
165 // on demand by DoGetName()
168 // the flags of this monitor, also used as initialization marker: if this
169 // is -1, GetMonitorInfo() hadn't been called yet
173 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
175 // ----------------------------------------------------------------------------
176 // common base class for all Win32 wxDisplayImpl versions
177 // ----------------------------------------------------------------------------
179 class wxDisplayImplWin32Base
: public wxDisplayImpl
182 wxDisplayImplWin32Base(size_t n
, wxDisplayInfo
& info
)
188 virtual wxRect
GetGeometry() const;
189 virtual wxRect
GetClientArea() const;
190 virtual wxString
GetName() const;
191 virtual bool IsPrimary() const;
193 virtual wxVideoMode
GetCurrentMode() const;
196 // convert a DEVMODE to our wxVideoMode
197 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
199 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
200 // and although 0 is ok for us we don't want to return modes with 1hz
202 return wxVideoMode(dm
.dmPelsWidth
,
205 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
208 wxDisplayInfo
& m_info
;
211 // ----------------------------------------------------------------------------
212 // common base class for all Win32 wxDisplayFactory versions
213 // ----------------------------------------------------------------------------
215 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
216 static MonitorFromPoint_t gs_MonitorFromPoint
= NULL
;
217 static MonitorFromWindow_t gs_MonitorFromWindow
= NULL
;
218 static GetMonitorInfo_t gs_GetMonitorInfo
= NULL
;
220 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
223 virtual ~wxDisplayFactoryWin32Base();
225 bool IsOk() const { return !m_displays
.empty(); }
227 virtual size_t GetCount() { return m_displays
.size(); }
228 virtual int GetFromPoint(const wxPoint
& pt
);
229 virtual int GetFromWindow(wxWindow
*window
);
232 // ctor checks if the current system supports multimon API and dynamically
233 // bind the functions we need if this is the case and sets
234 // ms_supportsMultimon if they're available
235 wxDisplayFactoryWin32Base();
237 // delete all m_displays elements: can be called from the derived class
238 // dtor if it is important to do this before destroying it (as in
239 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
242 // find the monitor corresponding to the given handle, return wxNOT_FOUND
244 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
247 // flag indicating whether gs_MonitorXXX functions are available
248 static int ms_supportsMultimon
;
250 // the array containing information about all available displays, should be
251 // filled by the derived class ctors
252 wxDisplayInfoArray m_displays
;
255 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
)
258 // ----------------------------------------------------------------------------
259 // wxDisplay implementation using Windows multi-monitor support functions
260 // ----------------------------------------------------------------------------
262 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
265 wxDisplayImplMultimon(size_t n
, wxDisplayInfo
& info
)
266 : wxDisplayImplWin32Base(n
, info
)
270 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
271 virtual bool ChangeMode(const wxVideoMode
& mode
);
274 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
)
277 class wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
280 wxDisplayFactoryMultimon();
282 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
285 // EnumDisplayMonitors() callback
286 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
292 // add a monitor description to m_displays array
293 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
296 // ----------------------------------------------------------------------------
297 // wxDisplay implementation using DirectDraw
298 // ----------------------------------------------------------------------------
300 #ifdef wxUSE_DIRECTDRAW
302 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
304 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
305 : wxDisplayInfo(hmon
),
312 virtual ~wxDisplayInfoDirectDraw()
319 // IDirectDraw object used to control this display, may be NULL
320 IDirectDraw2
*m_pDD2
;
322 // DirectDraw GUID for this display, only valid when using DirectDraw
326 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
)
329 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
332 wxDisplayImplDirectDraw(size_t n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
333 : wxDisplayImplWin32Base(n
, info
),
339 virtual ~wxDisplayImplDirectDraw()
344 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
345 virtual bool ChangeMode(const wxVideoMode
& mode
);
348 IDirectDraw2
*m_pDD2
;
350 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
)
353 class wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
356 wxDisplayFactoryDirectDraw();
357 virtual ~wxDisplayFactoryDirectDraw();
359 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
362 // callback used with DirectDrawEnumerateEx()
363 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
364 LPTSTR driverDescription
,
369 // add a monitor description to m_displays array
370 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
374 wxDynamicLibrary m_dllDDraw
;
376 // dynamically resolved DirectDrawCreate()
377 DirectDrawCreate_t m_pfnDirectDrawCreate
;
379 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
)
382 #endif // wxUSE_DIRECTDRAW
385 // ============================================================================
386 // common classes implementation
387 // ============================================================================
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
395 // we have 2 implementations for modern Windows: one using standard Win32
396 // and another using DirectDraw, the choice between them is done using a
399 #ifdef wxUSE_DIRECTDRAW
400 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
402 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
403 if ( factoryDD
->IsOk() )
408 #endif // wxUSE_DIRECTDRAW
410 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
411 if ( factoryMM
->IsOk() )
417 // finally fall back to a stub implementation if all else failed (Win95?)
418 return new wxDisplayFactorySingle
;
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 void wxDisplayInfo::Initialize()
427 if ( m_flags
== (DWORD
)-1 )
429 WinStruct
<MONITORINFOEX
> monInfo
;
430 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
432 wxLogLastError(_T("GetMonitorInfo"));
437 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
438 wxCopyRECTToRect(monInfo
.rcWork
, m_rectClient
);
439 m_devName
= monInfo
.szDevice
;
440 m_flags
= monInfo
.dwFlags
;
444 // ----------------------------------------------------------------------------
445 // wxDisplayImplWin32Base
446 // ----------------------------------------------------------------------------
448 wxRect
wxDisplayImplWin32Base::GetGeometry() const
450 if ( m_info
.m_rect
.IsEmpty() )
453 return m_info
.m_rect
;
456 wxRect
wxDisplayImplWin32Base::GetClientArea() const
458 if ( m_info
.m_rectClient
.IsEmpty() )
461 return m_info
.m_rectClient
;
464 wxString
wxDisplayImplWin32Base::GetName() const
466 if ( m_info
.m_devName
.IsEmpty() )
469 return m_info
.m_devName
;
472 bool wxDisplayImplWin32Base::IsPrimary() const
474 if ( m_info
.m_flags
== (DWORD
)-1 )
477 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
480 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
484 // The first parameter of EnumDisplaySettings() must be NULL under Win95
485 // according to MSDN. The version of GetName() we implement for Win95
486 // returns an empty string.
487 const wxString name
= GetName();
488 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
491 dm
.dmSize
= sizeof(dm
);
492 dm
.dmDriverExtra
= 0;
493 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
495 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
499 mode
= ConvertToVideoMode(dm
);
505 // ----------------------------------------------------------------------------
506 // wxDisplayFactoryWin32Base
507 // ----------------------------------------------------------------------------
509 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
511 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
513 if ( ms_supportsMultimon
== -1 )
515 ms_supportsMultimon
= 0;
517 wxDynamicLibrary
dllUser32(_T("user32.dll"));
521 gs_MonitorFromPoint
= (MonitorFromPoint_t
)
522 dllUser32
.GetSymbol(wxT("MonitorFromPoint"));
523 if ( !gs_MonitorFromPoint
)
526 gs_MonitorFromWindow
= (MonitorFromWindow_t
)
527 dllUser32
.GetSymbol(wxT("MonitorFromWindow"));
528 if ( !gs_MonitorFromWindow
)
531 gs_GetMonitorInfo
= (GetMonitorInfo_t
)
532 dllUser32
.GetSymbolAorW(wxT("GetMonitorInfo"));
533 if ( !gs_GetMonitorInfo
)
536 ms_supportsMultimon
= 1;
538 // we can safely let dllUser32 go out of scope, the DLL itself will
539 // still remain loaded as all Win32 programs use it
543 void wxDisplayFactoryWin32Base::Clear()
545 WX_CLEAR_ARRAY(m_displays
);
548 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
553 // helper for GetFromPoint() and GetFromWindow()
554 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
558 const size_t count
= m_displays
.size();
559 for ( size_t n
= 0; n
< count
; n
++ )
561 if ( hmon
== m_displays
[n
]->m_hmon
)
569 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
575 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
576 MONITOR_DEFAULTTONULL
));
579 int wxDisplayFactoryWin32Base::GetFromWindow(wxWindow
*window
)
581 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
582 MONITOR_DEFAULTTONULL
));
585 // ============================================================================
586 // wxDisplay implementation using Win32 multimon API
587 // ============================================================================
589 // ----------------------------------------------------------------------------
590 // wxDisplayFactoryMultimon initialization
591 // ----------------------------------------------------------------------------
593 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
595 if ( !ms_supportsMultimon
)
598 // look up EnumDisplayMonitors() which we don't need with DirectDraw
600 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
604 wxDynamicLibrary
dllUser32(_T("user32.dll"));
605 pfnEnumDisplayMonitors
= (EnumDisplayMonitors_t
)
606 dllUser32
.GetSymbol(wxT("EnumDisplayMonitors"));
607 if ( !pfnEnumDisplayMonitors
)
611 // enumerate all displays
612 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
614 wxLogLastError(wxT("EnumDisplayMonitors"));
620 wxDisplayFactoryMultimon::MultimonEnumProc(
621 HMONITOR hMonitor
, // handle to display monitor
622 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
623 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
624 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
627 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
628 self
->AddDisplay(hMonitor
, lprcMonitor
);
630 // continue the enumeration
634 // ----------------------------------------------------------------------------
635 // wxDisplayFactoryMultimon helper functions
636 // ----------------------------------------------------------------------------
638 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
640 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
642 // we also store the display geometry
643 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
644 lprcMonitor
->right
- lprcMonitor
->left
,
645 lprcMonitor
->bottom
- lprcMonitor
->top
);
647 // now add this monitor to the array
648 m_displays
.Add(info
);
651 // ----------------------------------------------------------------------------
652 // wxDisplayFactoryMultimon inherited pure virtuals implementation
653 // ----------------------------------------------------------------------------
655 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(size_t n
)
657 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
659 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
662 // ----------------------------------------------------------------------------
663 // wxDisplayImplMultimon implementation
664 // ----------------------------------------------------------------------------
667 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
669 wxArrayVideoModes modes
;
671 // The first parameter of EnumDisplaySettings() must be NULL under Win95
672 // according to MSDN. The version of GetName() we implement for Win95
673 // returns an empty string.
674 const wxString name
= GetName();
675 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
678 dm
.dmSize
= sizeof(dm
);
679 dm
.dmDriverExtra
= 0;
680 for ( int iModeNum
= 0;
681 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
684 const wxVideoMode mode
= ConvertToVideoMode(dm
);
685 if ( mode
.Matches(modeMatch
) )
694 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
696 // prepare ChangeDisplaySettingsEx() parameters
702 if ( mode
== wxDefaultVideoMode
)
704 // reset the video mode to default
708 else // change to the given mode
710 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
711 _T("at least the width and height must be specified") );
714 dm
.dmSize
= sizeof(dm
);
715 dm
.dmDriverExtra
= 0;
716 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
717 dm
.dmPelsWidth
= mode
.w
;
718 dm
.dmPelsHeight
= mode
.h
;
722 dm
.dmFields
|= DM_BITSPERPEL
;
723 dm
.dmBitsPerPel
= mode
.bpp
;
728 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
729 dm
.dmDisplayFrequency
= mode
.refresh
;
736 #else // !__WXWINCE__
737 flags
= CDS_FULLSCREEN
;
738 #endif // __WXWINCE__/!__WXWINCE__
742 // get pointer to the function dynamically
744 // we're only called from the main thread, so it's ok to use static
746 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
747 if ( !pfnChangeDisplaySettingsEx
)
749 wxDynamicLibrary
dllUser32(_T("user32.dll"));
750 if ( dllUser32
.IsLoaded() )
752 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
753 dllUser32
.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
755 //else: huh, no user32.dll??
758 if ( !pfnChangeDisplaySettingsEx
)
760 // we must be under Win95 and so there is no multiple monitors
762 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
764 #endif // !__WXWINCE__
767 // do change the mode
768 switch ( pfnChangeDisplaySettingsEx
770 GetName(), // display name
771 pDevMode
, // dev mode or NULL to reset
774 NULL
// pointer to video parameters (not used)
777 case DISP_CHANGE_SUCCESSFUL
:
780 // If we have a top-level, full-screen frame, emulate
781 // the DirectX behavior and resize it. This makes this
782 // API quite a bit easier to use.
783 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
784 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
785 if (frameTop
&& frameTop
->IsFullScreen())
787 wxVideoMode current
= GetCurrentMode();
788 frameTop
->SetClientSize(current
.w
, current
.h
);
793 case DISP_CHANGE_BADMODE
:
794 // don't complain about this, this is the only "expected" error
798 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
805 // ============================================================================
806 // DirectDraw-based wxDisplay implementation
807 // ============================================================================
811 // ----------------------------------------------------------------------------
812 // wxDisplayFactoryDirectDraw initialization
813 // ----------------------------------------------------------------------------
815 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
817 if ( !ms_supportsMultimon
)
821 // suppress the errors if ddraw.dll is not found, we're prepared to handle
826 m_dllDDraw
.Load(_T("ddraw.dll"));
828 if ( !m_dllDDraw
.IsLoaded() )
831 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
832 m_dllDDraw
.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
836 // we can't continue without DirectDrawCreate() later, so resolve it right
837 // now and fail the initialization if it's not available
838 m_pfnDirectDrawCreate
= (DirectDrawCreate_t
)
839 m_dllDDraw
.GetSymbol(_T("DirectDrawCreate"));
840 if ( !m_pfnDirectDrawCreate
)
843 if ( (*pDDEnumEx
)(DDEnumExCallback
,
845 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
847 wxLogLastError(_T("DirectDrawEnumerateEx"));
851 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
853 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
854 // calling m_pDD2->Release() later would crash
858 // ----------------------------------------------------------------------------
859 // callbacks for monitor/modes enumeration stuff
860 // ----------------------------------------------------------------------------
863 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID
*pGuid
,
864 LPTSTR
WXUNUSED(driverDescription
),
871 wxDisplayFactoryDirectDraw
* self
=
872 wx_static_cast(wxDisplayFactoryDirectDraw
*, lpContext
);
873 self
->AddDisplay(*pGuid
, hmon
, driverName
);
875 //else: we're called for the primary monitor, skip it
877 // continue the enumeration
881 // ----------------------------------------------------------------------------
882 // wxDisplayFactoryDirectDraw helpers
883 // ----------------------------------------------------------------------------
885 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID
& guid
,
889 m_displays
.Add(new wxDisplayInfoDirectDraw(guid
, hmon
, name
));
892 // ----------------------------------------------------------------------------
893 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
894 // ----------------------------------------------------------------------------
896 wxDisplayImpl
*wxDisplayFactoryDirectDraw::CreateDisplay(size_t n
)
898 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
900 wxDisplayInfoDirectDraw
*
901 info
= wx_static_cast(wxDisplayInfoDirectDraw
*, m_displays
[n
]);
906 GUID
guid(info
->m_guid
);
907 HRESULT hr
= (*m_pfnDirectDrawCreate
)(&guid
, &pDD
, NULL
);
909 if ( FAILED(hr
) || !pDD
)
912 wxLogApiError(_T("DirectDrawCreate"), hr
);
916 // we got IDirectDraw, but we need IDirectDraw2
917 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&info
->m_pDD2
);
920 if ( FAILED(hr
) || !info
->m_pDD2
)
922 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
926 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
927 // which is ok as we don't want to recreate DD objects all the time
929 //else: DirectDraw object corresponding to our display already exists
931 return new wxDisplayImplDirectDraw(n
, *info
, info
->m_pDD2
);
934 // ============================================================================
935 // wxDisplayImplDirectDraw
936 // ============================================================================
938 // ----------------------------------------------------------------------------
939 // video modes enumeration
940 // ----------------------------------------------------------------------------
942 // tiny helper class used to pass information from GetModes() to
943 // wxDDEnumModesCallback
944 class wxDDVideoModesAdder
947 // our Add() method will add modes matching modeMatch to modes array
948 wxDDVideoModesAdder(wxArrayVideoModes
& modes
, const wxVideoMode
& modeMatch
)
950 m_modeMatch(modeMatch
)
954 void Add(const wxVideoMode
& mode
)
956 if ( mode
.Matches(m_modeMatch
) )
961 wxArrayVideoModes
& m_modes
;
962 const wxVideoMode
& m_modeMatch
;
964 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder
)
967 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
970 // we need at least the mode size
971 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
972 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
974 wxDDVideoModesAdder
* const vmodes
=
975 wx_static_cast(wxDDVideoModesAdder
*, lpContext
);
977 vmodes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
978 lpDDSurfaceDesc
->dwHeight
,
979 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
980 lpDDSurfaceDesc
->dwRefreshRate
));
983 // continue the enumeration
988 wxDisplayImplDirectDraw::GetModes(const wxVideoMode
& modeMatch
) const
990 wxArrayVideoModes modes
;
991 wxDDVideoModesAdder
modesAdder(modes
, modeMatch
);
993 HRESULT hr
= m_pDD2
->EnumDisplayModes
997 &modesAdder
, // callback parameter
998 wxDDEnumModesCallback
1003 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
1009 // ----------------------------------------------------------------------------
1010 // video mode switching
1011 // ----------------------------------------------------------------------------
1013 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode
& mode
)
1015 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
1016 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
1018 HRESULT hr
= m_pDD2
->SetCooperativeLevel
1021 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
1025 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr
);
1030 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1033 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr
);
1041 #endif // wxUSE_DIRECTDRAW
1043 #endif // wxUSE_DISPLAY