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"
44 #include "wx/msw/private.h"
46 // define this to use DirectDraw for display mode switching: this is disabled
47 // by default because ddraw.h is now always available and also it's not really
48 // clear what are the benefits of using DirectDraw compared to the standard API
50 #if !defined(wxUSE_DIRECTDRAW)
51 #define wxUSE_DIRECTDRAW 0
55 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
56 // can't directly test whether HMONITOR is defined or not in windef.h as
57 // it's not a macro but a typedef, so we test for an unrelated symbol which
58 // is only defined in winuser.h if WINVER >= 0x0500
59 #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK)
60 DECLARE_HANDLE(HMONITOR
);
61 typedef BOOL(CALLBACK
* MONITORENUMPROC
)(HMONITOR
, HDC
, LPRECT
, LPARAM
);
62 typedef struct tagMONITORINFO
68 } MONITORINFO
, *LPMONITORINFO
;
69 typedef struct tagMONITORINFOEX
: public tagMONITORINFO
71 TCHAR szDevice
[CCHDEVICENAME
];
72 } MONITORINFOEX
, *LPMONITORINFOEX
;
73 #define MONITOR_DEFAULTTONULL 0x00000000
74 #define MONITOR_DEFAULTTOPRIMARY 0x00000001
75 #define MONITOR_DEFAULTTONEAREST 0x00000002
76 #define MONITORINFOF_PRIMARY 0x00000001
77 #define HMONITOR_DECLARED
79 #endif // !__WXWINCE__
84 // we don't want to link with ddraw.lib which contains the real
85 // IID_IDirectDraw2 definition
86 const GUID wxIID_IDirectDraw2
=
87 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
88 #endif // wxUSE_DIRECTDRAW
90 // display functions are found in different DLLs under WinCE and normal Win32
92 static const wxChar displayDllName
[] = wxT("coredll.dll");
94 static const wxChar displayDllName
[] = wxT("user32.dll");
97 // ----------------------------------------------------------------------------
98 // typedefs for dynamically loaded Windows functions
99 // ----------------------------------------------------------------------------
101 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
108 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
109 LPTSTR driverDescription
,
114 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
118 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
119 LPDIRECTDRAW
*lplpDD
,
120 IUnknown
*pUnkOuter
);
121 #endif // wxUSE_DIRECTDRAW
123 typedef BOOL (WINAPI
*EnumDisplayMonitors_t
)(HDC
,LPCRECT
,MONITORENUMPROC
,LPARAM
);
124 typedef HMONITOR (WINAPI
*MonitorFromPoint_t
)(POINT
,DWORD
);
125 typedef HMONITOR (WINAPI
*MonitorFromWindow_t
)(HWND
,DWORD
);
126 typedef BOOL (WINAPI
*GetMonitorInfo_t
)(HMONITOR
,LPMONITORINFO
);
129 // emulation of ChangeDisplaySettingsEx() for Win95
130 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
134 LPVOID
WXUNUSED(lParam
))
136 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
138 #endif // !__WXWINCE__
140 // ----------------------------------------------------------------------------
141 // display information classes
142 // ----------------------------------------------------------------------------
146 wxDisplayInfo(HMONITOR hmon
= NULL
)
152 virtual ~wxDisplayInfo() { }
155 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
156 // hadn't been done before)
160 // handle of this monitor used by MonitorXXX() functions, never NULL
163 // the entire area of this monitor in virtual screen coordinates
166 // the work or client area, i.e. the area available for the normal windows
169 // the display device name for this monitor, empty initially and retrieved
170 // on demand by DoGetName()
173 // the flags of this monitor, also used as initialization marker: if this
174 // is -1, GetMonitorInfo() hadn't been called yet
178 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
180 // ----------------------------------------------------------------------------
181 // common base class for all Win32 wxDisplayImpl versions
182 // ----------------------------------------------------------------------------
184 class wxDisplayImplWin32Base
: public wxDisplayImpl
187 wxDisplayImplWin32Base(unsigned n
, wxDisplayInfo
& info
)
193 virtual wxRect
GetGeometry() const;
194 virtual wxRect
GetClientArea() const;
195 virtual wxString
GetName() const;
196 virtual bool IsPrimary() const;
198 virtual wxVideoMode
GetCurrentMode() const;
201 // convert a DEVMODE to our wxVideoMode
202 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
204 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
205 // and although 0 is ok for us we don't want to return modes with 1hz
207 return wxVideoMode(dm
.dmPelsWidth
,
210 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
213 wxDisplayInfo
& m_info
;
216 // ----------------------------------------------------------------------------
217 // common base class for all Win32 wxDisplayFactory versions
218 // ----------------------------------------------------------------------------
220 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
221 static MonitorFromPoint_t gs_MonitorFromPoint
= NULL
;
222 static MonitorFromWindow_t gs_MonitorFromWindow
= NULL
;
223 static GetMonitorInfo_t gs_GetMonitorInfo
= NULL
;
225 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
228 virtual ~wxDisplayFactoryWin32Base();
230 bool IsOk() const { return !m_displays
.empty(); }
232 virtual unsigned GetCount() { return unsigned(m_displays
.size()); }
233 virtual int GetFromPoint(const wxPoint
& pt
);
234 virtual int GetFromWindow(const wxWindow
*window
);
237 // ctor checks if the current system supports multimon API and dynamically
238 // bind the functions we need if this is the case and sets
239 // ms_supportsMultimon if they're available
240 wxDisplayFactoryWin32Base();
242 // delete all m_displays elements: can be called from the derived class
243 // dtor if it is important to do this before destroying it (as in
244 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
247 // find the monitor corresponding to the given handle, return wxNOT_FOUND
249 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
252 // flag indicating whether gs_MonitorXXX functions are available
253 static int ms_supportsMultimon
;
255 // the array containing information about all available displays, should be
256 // filled by the derived class ctors
257 wxDisplayInfoArray m_displays
;
260 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
);
263 // ----------------------------------------------------------------------------
264 // wxDisplay implementation using Windows multi-monitor support functions
265 // ----------------------------------------------------------------------------
267 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
270 wxDisplayImplMultimon(unsigned n
, wxDisplayInfo
& info
)
271 : wxDisplayImplWin32Base(n
, info
)
275 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
276 virtual bool ChangeMode(const wxVideoMode
& mode
);
279 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
);
282 class wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
285 wxDisplayFactoryMultimon();
287 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
290 // EnumDisplayMonitors() callback
291 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
297 // add a monitor description to m_displays array
298 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
301 // ----------------------------------------------------------------------------
302 // wxDisplay implementation using DirectDraw
303 // ----------------------------------------------------------------------------
307 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
309 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
310 : wxDisplayInfo(hmon
),
317 virtual ~wxDisplayInfoDirectDraw()
324 // IDirectDraw object used to control this display, may be NULL
325 IDirectDraw2
*m_pDD2
;
327 // DirectDraw GUID for this display, only valid when using DirectDraw
331 wxDECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
);
334 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
337 wxDisplayImplDirectDraw(unsigned n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
338 : wxDisplayImplWin32Base(n
, info
),
344 virtual ~wxDisplayImplDirectDraw()
349 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
350 virtual bool ChangeMode(const wxVideoMode
& mode
);
353 IDirectDraw2
*m_pDD2
;
355 wxDECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
);
358 class wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
361 wxDisplayFactoryDirectDraw();
362 virtual ~wxDisplayFactoryDirectDraw();
364 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
367 // callback used with DirectDrawEnumerateEx()
368 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
369 LPTSTR driverDescription
,
374 // add a monitor description to m_displays array
375 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
379 wxDynamicLibrary m_dllDDraw
;
381 // dynamically resolved DirectDrawCreate()
382 DirectDrawCreate_t m_pfnDirectDrawCreate
;
384 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
);
387 #endif // wxUSE_DIRECTDRAW
390 // ============================================================================
391 // common classes implementation
392 // ============================================================================
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
400 // we have 2 implementations for modern Windows: one using standard Win32
401 // and another using DirectDraw, the choice between them is done using a
405 if ( wxSystemOptions::GetOptionInt(wxT("msw.display.directdraw")) )
407 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
408 if ( factoryDD
->IsOk() )
413 #endif // wxUSE_DIRECTDRAW
415 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
416 if ( factoryMM
->IsOk() )
422 // finally fall back to a stub implementation if all else failed (Win95?)
423 return new wxDisplayFactorySingle
;
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 void wxDisplayInfo::Initialize()
432 if ( m_flags
== (DWORD
)-1 )
434 WinStruct
<MONITORINFOEX
> monInfo
;
435 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
437 wxLogLastError(wxT("GetMonitorInfo"));
442 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
443 wxCopyRECTToRect(monInfo
.rcWork
, m_rectClient
);
444 m_devName
= monInfo
.szDevice
;
445 m_flags
= monInfo
.dwFlags
;
449 // ----------------------------------------------------------------------------
450 // wxDisplayImplWin32Base
451 // ----------------------------------------------------------------------------
453 wxRect
wxDisplayImplWin32Base::GetGeometry() const
455 if ( m_info
.m_rect
.IsEmpty() )
458 return m_info
.m_rect
;
461 wxRect
wxDisplayImplWin32Base::GetClientArea() const
463 if ( m_info
.m_rectClient
.IsEmpty() )
466 return m_info
.m_rectClient
;
469 wxString
wxDisplayImplWin32Base::GetName() const
471 if ( m_info
.m_devName
.empty() )
474 return m_info
.m_devName
;
477 bool wxDisplayImplWin32Base::IsPrimary() const
479 if ( m_info
.m_flags
== (DWORD
)-1 )
482 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
485 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
489 // The first parameter of EnumDisplaySettings() must be NULL under Win95
490 // according to MSDN. The version of GetName() we implement for Win95
491 // returns an empty string.
492 const wxString name
= GetName();
493 const wxChar
* const deviceName
= name
.empty()
494 ? (const wxChar
*)NULL
495 : (const wxChar
*)name
.c_str();
498 dm
.dmSize
= sizeof(dm
);
499 dm
.dmDriverExtra
= 0;
500 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
502 wxLogLastError(wxT("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
506 mode
= ConvertToVideoMode(dm
);
512 // ----------------------------------------------------------------------------
513 // wxDisplayFactoryWin32Base
514 // ----------------------------------------------------------------------------
516 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
518 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
520 if ( ms_supportsMultimon
== -1 )
522 ms_supportsMultimon
= 0;
524 wxDynamicLibrary
dllDisplay(displayDllName
, wxDL_VERBATIM
| wxDL_QUIET
);
526 if ( (wxDL_INIT_FUNC(gs_
, MonitorFromPoint
, dllDisplay
)) == NULL
||
527 (wxDL_INIT_FUNC(gs_
, MonitorFromWindow
, dllDisplay
)) == NULL
||
528 (wxDL_INIT_FUNC_AW(gs_
, GetMonitorInfo
, dllDisplay
)) == NULL
)
531 ms_supportsMultimon
= 1;
533 // we can safely let dllDisplay go out of scope, the DLL itself will
534 // still remain loaded as all programs link to it statically anyhow
538 void wxDisplayFactoryWin32Base::Clear()
540 WX_CLEAR_ARRAY(m_displays
);
543 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
548 // helper for GetFromPoint() and GetFromWindow()
549 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
553 const size_t count
= m_displays
.size();
554 for ( size_t n
= 0; n
< count
; n
++ )
556 if ( hmon
== m_displays
[n
]->m_hmon
)
564 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
570 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
571 MONITOR_DEFAULTTONULL
));
574 int wxDisplayFactoryWin32Base::GetFromWindow(const wxWindow
*window
)
576 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
577 MONITOR_DEFAULTTONULL
));
580 // ============================================================================
581 // wxDisplay implementation using Win32 multimon API
582 // ============================================================================
584 // ----------------------------------------------------------------------------
585 // wxDisplayFactoryMultimon initialization
586 // ----------------------------------------------------------------------------
588 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
590 if ( !ms_supportsMultimon
)
593 // look up EnumDisplayMonitors() which we don't need with DirectDraw
595 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
597 wxDynamicLibrary
dllDisplay(displayDllName
, wxDL_VERBATIM
| wxDL_QUIET
);
598 if ( (wxDL_INIT_FUNC(pfn
, EnumDisplayMonitors
, dllDisplay
)) == NULL
)
602 // enumerate all displays
603 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
605 wxLogLastError(wxT("EnumDisplayMonitors"));
611 wxDisplayFactoryMultimon::MultimonEnumProc(
612 HMONITOR hMonitor
, // handle to display monitor
613 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
614 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
615 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
618 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
619 self
->AddDisplay(hMonitor
, lprcMonitor
);
621 // continue the enumeration
625 // ----------------------------------------------------------------------------
626 // wxDisplayFactoryMultimon helper functions
627 // ----------------------------------------------------------------------------
629 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
631 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
633 // we also store the display geometry
634 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
635 lprcMonitor
->right
- lprcMonitor
->left
,
636 lprcMonitor
->bottom
- lprcMonitor
->top
);
638 // now add this monitor to the array
639 m_displays
.Add(info
);
642 // ----------------------------------------------------------------------------
643 // wxDisplayFactoryMultimon inherited pure virtuals implementation
644 // ----------------------------------------------------------------------------
646 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(unsigned n
)
648 wxCHECK_MSG( n
< m_displays
.size(), NULL
, wxT("invalid display index") );
650 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
653 // ----------------------------------------------------------------------------
654 // wxDisplayImplMultimon implementation
655 // ----------------------------------------------------------------------------
658 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
660 wxArrayVideoModes modes
;
662 // The first parameter of EnumDisplaySettings() must be NULL under Win95
663 // according to MSDN. The version of GetName() we implement for Win95
664 // returns an empty string.
665 const wxString name
= GetName();
666 const wxChar
* const deviceName
= name
.empty()
667 ? (const wxChar
*)NULL
668 : (const wxChar
*)name
.c_str();
671 dm
.dmSize
= sizeof(dm
);
672 dm
.dmDriverExtra
= 0;
673 for ( int iModeNum
= 0;
674 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
677 const wxVideoMode mode
= ConvertToVideoMode(dm
);
678 if ( mode
.Matches(modeMatch
) )
687 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
689 // prepare ChangeDisplaySettingsEx() parameters
695 if ( mode
== wxDefaultVideoMode
)
697 // reset the video mode to default
701 else // change to the given mode
703 wxCHECK_MSG( mode
.GetWidth() && mode
.GetHeight(), false,
704 wxT("at least the width and height must be specified") );
707 dm
.dmSize
= sizeof(dm
);
708 dm
.dmDriverExtra
= 0;
709 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
710 dm
.dmPelsWidth
= mode
.GetWidth();
711 dm
.dmPelsHeight
= mode
.GetHeight();
713 if ( mode
.GetDepth() )
715 dm
.dmFields
|= DM_BITSPERPEL
;
716 dm
.dmBitsPerPel
= mode
.GetDepth();
719 if ( mode
.GetRefresh() )
721 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
722 dm
.dmDisplayFrequency
= mode
.GetRefresh();
729 #else // !__WXWINCE__
730 flags
= CDS_FULLSCREEN
;
731 #endif // __WXWINCE__/!__WXWINCE__
735 // get pointer to the function dynamically
737 // we're only called from the main thread, so it's ok to use static
739 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
740 if ( !pfnChangeDisplaySettingsEx
)
742 wxDynamicLibrary
dllDisplay(displayDllName
, wxDL_VERBATIM
| wxDL_QUIET
);
743 if ( dllDisplay
.IsLoaded() )
745 wxDL_INIT_FUNC_AW(pfn
, ChangeDisplaySettingsEx
, dllDisplay
);
747 //else: huh, no this DLL must always be present, what's going on??
750 if ( !pfnChangeDisplaySettingsEx
)
752 // we must be under Win95 and so there is no multiple monitors
754 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
756 #endif // !__WXWINCE__
759 // do change the mode
760 switch ( pfnChangeDisplaySettingsEx
762 GetName().wx_str(), // display name
763 pDevMode
, // dev mode or NULL to reset
766 NULL
// pointer to video parameters (not used)
769 case DISP_CHANGE_SUCCESSFUL
:
772 // If we have a top-level, full-screen frame, emulate
773 // the DirectX behavior and resize it. This makes this
774 // API quite a bit easier to use.
775 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
776 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
777 if (frameTop
&& frameTop
->IsFullScreen())
779 wxVideoMode current
= GetCurrentMode();
780 frameTop
->SetClientSize(current
.GetWidth(), current
.GetHeight());
785 case DISP_CHANGE_BADMODE
:
786 // don't complain about this, this is the only "expected" error
790 wxFAIL_MSG( wxT("unexpected ChangeDisplaySettingsEx() return value") );
797 // ============================================================================
798 // DirectDraw-based wxDisplay implementation
799 // ============================================================================
803 // ----------------------------------------------------------------------------
804 // wxDisplayFactoryDirectDraw initialization
805 // ----------------------------------------------------------------------------
807 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
809 if ( !ms_supportsMultimon
)
812 m_dllDDraw
.Load(wxT("ddraw.dll"), wxDL_VERBATIM
| wxDL_QUIET
);
814 if ( !m_dllDDraw
.IsLoaded() )
817 DirectDrawEnumerateEx_t
818 wxDL_INIT_FUNC_AW(pfn
, DirectDrawEnumerateEx
, m_dllDDraw
);
819 if ( !pfnDirectDrawEnumerateEx
)
822 // we can't continue without DirectDrawCreate() later, so resolve it right
823 // now and fail the initialization if it's not available
824 if ( !wxDL_INIT_FUNC(m_pfn
, DirectDrawCreate
, m_dllDDraw
) )
827 if ( (*pfnDirectDrawEnumerateEx
)(DDEnumExCallback
,
829 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
831 wxLogLastError(wxT("DirectDrawEnumerateEx"));
835 wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
837 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
838 // calling m_pDD2->Release() later would crash
842 // ----------------------------------------------------------------------------
843 // callbacks for monitor/modes enumeration stuff
844 // ----------------------------------------------------------------------------
847 wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID
*pGuid
,
848 LPTSTR
WXUNUSED(driverDescription
),
855 wxDisplayFactoryDirectDraw
* self
=
856 static_cast<wxDisplayFactoryDirectDraw
*>(lpContext
);
857 self
->AddDisplay(*pGuid
, hmon
, driverName
);
859 //else: we're called for the primary monitor, skip it
861 // continue the enumeration
865 // ----------------------------------------------------------------------------
866 // wxDisplayFactoryDirectDraw helpers
867 // ----------------------------------------------------------------------------
869 void wxDisplayFactoryDirectDraw::AddDisplay(const GUID
& guid
,
873 m_displays
.Add(new wxDisplayInfoDirectDraw(guid
, hmon
, name
));
876 // ----------------------------------------------------------------------------
877 // wxDisplayFactoryDirectDraw inherited pure virtuals implementation
878 // ----------------------------------------------------------------------------
880 wxDisplayImpl
*wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n
)
882 wxCHECK_MSG( n
< m_displays
.size(), NULL
, wxT("invalid display index") );
884 wxDisplayInfoDirectDraw
*
885 info
= static_cast<wxDisplayInfoDirectDraw
*>(m_displays
[n
]);
890 GUID
guid(info
->m_guid
);
891 HRESULT hr
= (*m_pfnDirectDrawCreate
)(&guid
, &pDD
, NULL
);
893 if ( FAILED(hr
) || !pDD
)
896 wxLogApiError(wxT("DirectDrawCreate"), hr
);
900 // we got IDirectDraw, but we need IDirectDraw2
901 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&info
->m_pDD2
);
904 if ( FAILED(hr
) || !info
->m_pDD2
)
906 wxLogApiError(wxT("IDirectDraw::QueryInterface(IDD2)"), hr
);
910 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
911 // which is ok as we don't want to recreate DD objects all the time
913 //else: DirectDraw object corresponding to our display already exists
915 return new wxDisplayImplDirectDraw(n
, *info
, info
->m_pDD2
);
918 // ============================================================================
919 // wxDisplayImplDirectDraw
920 // ============================================================================
922 // ----------------------------------------------------------------------------
923 // video modes enumeration
924 // ----------------------------------------------------------------------------
926 // tiny helper class used to pass information from GetModes() to
927 // wxDDEnumModesCallback
928 class wxDDVideoModesAdder
931 // our Add() method will add modes matching modeMatch to modes array
932 wxDDVideoModesAdder(wxArrayVideoModes
& modes
, const wxVideoMode
& modeMatch
)
934 m_modeMatch(modeMatch
)
938 void Add(const wxVideoMode
& mode
)
940 if ( mode
.Matches(m_modeMatch
) )
945 wxArrayVideoModes
& m_modes
;
946 const wxVideoMode
& m_modeMatch
;
948 wxDECLARE_NO_COPY_CLASS(wxDDVideoModesAdder
);
951 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
954 // we need at least the mode size
955 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
956 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
958 wxDDVideoModesAdder
* const vmodes
=
959 static_cast<wxDDVideoModesAdder
*>(lpContext
);
961 vmodes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
962 lpDDSurfaceDesc
->dwHeight
,
963 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
964 lpDDSurfaceDesc
->dwRefreshRate
));
967 // continue the enumeration
972 wxDisplayImplDirectDraw::GetModes(const wxVideoMode
& modeMatch
) const
974 wxArrayVideoModes modes
;
975 wxDDVideoModesAdder
modesAdder(modes
, modeMatch
);
977 HRESULT hr
= m_pDD2
->EnumDisplayModes
981 &modesAdder
, // callback parameter
982 wxDDEnumModesCallback
987 wxLogApiError(wxT("IDirectDraw::EnumDisplayModes"), hr
);
993 // ----------------------------------------------------------------------------
994 // video mode switching
995 // ----------------------------------------------------------------------------
997 bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode
& mode
)
999 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
1000 wxCHECK_MSG( winTop
, false, wxT("top level window required for DirectX") );
1002 HRESULT hr
= m_pDD2
->SetCooperativeLevel
1005 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
1009 wxLogApiError(wxT("IDirectDraw2::SetCooperativeLevel"), hr
);
1014 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1017 wxLogApiError(wxT("IDirectDraw2::SetDisplayMode"), hr
);
1025 #endif // wxUSE_DIRECTDRAW
1027 #endif // wxUSE_DISPLAY