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 work or client area, i.e. the area available for the normal windows
148 // the display device name for this monitor, empty initially and retrieved
149 // on demand by DoGetName()
152 // the flags of this monitor, also used as initialization marker: if this
153 // is -1, GetMonitorInfo() hadn't been called yet
157 WX_DEFINE_ARRAY_PTR(wxDisplayInfo
*, wxDisplayInfoArray
);
159 // ----------------------------------------------------------------------------
160 // common base class for all Win32 wxDisplayImpl versions
161 // ----------------------------------------------------------------------------
163 class wxDisplayImplWin32Base
: public wxDisplayImpl
166 wxDisplayImplWin32Base(size_t n
, wxDisplayInfo
& info
)
172 virtual wxRect
GetGeometry() const;
173 virtual wxRect
GetClientArea() const;
174 virtual wxString
GetName() const;
175 virtual bool IsPrimary() const;
177 virtual wxVideoMode
GetCurrentMode() const;
180 // convert a DEVMODE to our wxVideoMode
181 static wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
183 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
184 // and although 0 is ok for us we don't want to return modes with 1hz
186 return wxVideoMode(dm
.dmPelsWidth
,
189 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
192 wxDisplayInfo
& m_info
;
195 // ----------------------------------------------------------------------------
196 // common base class for all Win32 wxDisplayFactory versions
197 // ----------------------------------------------------------------------------
199 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
200 static MonitorFromPoint_t gs_MonitorFromPoint
= NULL
;
201 static MonitorFromWindow_t gs_MonitorFromWindow
= NULL
;
202 static GetMonitorInfo_t gs_GetMonitorInfo
= NULL
;
204 class wxDisplayFactoryWin32Base
: public wxDisplayFactory
207 virtual ~wxDisplayFactoryWin32Base();
209 bool IsOk() const { return !m_displays
.empty(); }
211 virtual size_t GetCount() { return m_displays
.size(); }
212 virtual int GetFromPoint(const wxPoint
& pt
);
213 virtual int GetFromWindow(wxWindow
*window
);
216 // ctor checks if the current system supports multimon API and dynamically
217 // bind the functions we need if this is the case and sets
218 // ms_supportsMultimon if they're available
219 wxDisplayFactoryWin32Base();
221 // delete all m_displays elements: can be called from the derived class
222 // dtor if it is important to do this before destroying it (as in
223 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
226 // find the monitor corresponding to the given handle, return wxNOT_FOUND
228 int FindDisplayFromHMONITOR(HMONITOR hmon
) const;
231 // flag indicating whether gs_MonitorXXX functions are available
232 static int ms_supportsMultimon
;
234 // the array containing information about all available displays, should be
235 // filled by the derived class ctors
236 wxDisplayInfoArray m_displays
;
239 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base
)
242 // ----------------------------------------------------------------------------
243 // wxDisplay implementation using Windows multi-monitor support functions
244 // ----------------------------------------------------------------------------
246 class wxDisplayImplMultimon
: public wxDisplayImplWin32Base
249 wxDisplayImplMultimon(size_t n
, wxDisplayInfo
& info
)
250 : wxDisplayImplWin32Base(n
, info
)
254 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
255 virtual bool ChangeMode(const wxVideoMode
& mode
);
258 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon
)
261 class WXDLLEXPORT wxDisplayFactoryMultimon
: public wxDisplayFactoryWin32Base
264 wxDisplayFactoryMultimon();
266 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
269 // EnumDisplayMonitors() callback
270 static BOOL CALLBACK
MultimonEnumProc(HMONITOR hMonitor
,
276 // add a monitor description to m_displays array
277 void AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
);
280 // ----------------------------------------------------------------------------
281 // wxDisplay implementation using DirectDraw
282 // ----------------------------------------------------------------------------
284 #ifdef wxUSE_DIRECTDRAW
286 struct wxDisplayInfoDirectDraw
: wxDisplayInfo
288 wxDisplayInfoDirectDraw(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
)
289 : wxDisplayInfo(hmon
),
296 virtual ~wxDisplayInfoDirectDraw()
303 // IDirectDraw object used to control this display, may be NULL
304 IDirectDraw2
*m_pDD2
;
306 // DirectDraw GUID for this display, only valid when using DirectDraw
310 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw
)
313 class wxDisplayImplDirectDraw
: public wxDisplayImplWin32Base
316 wxDisplayImplDirectDraw(size_t n
, wxDisplayInfo
& info
, IDirectDraw2
*pDD2
)
317 : wxDisplayImplWin32Base(n
, info
),
323 virtual ~wxDisplayImplDirectDraw()
328 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
329 virtual bool ChangeMode(const wxVideoMode
& mode
);
332 IDirectDraw2
*m_pDD2
;
334 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw
)
337 class WXDLLEXPORT wxDisplayFactoryDirectDraw
: public wxDisplayFactoryWin32Base
340 wxDisplayFactoryDirectDraw();
341 virtual ~wxDisplayFactoryDirectDraw();
343 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
346 // callback used with DirectDrawEnumerateEx()
347 static BOOL WINAPI
DDEnumExCallback(GUID
*pGuid
,
348 LPTSTR driverDescription
,
353 // add a monitor description to m_displays array
354 void AddDisplay(const GUID
& guid
, HMONITOR hmon
, LPTSTR name
);
358 wxDynamicLibrary m_dllDDraw
;
360 // dynamically resolved DirectDrawCreate()
361 DirectDrawCreate_t m_pfnDirectDrawCreate
;
363 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw
)
366 #endif // wxUSE_DIRECTDRAW
369 // ============================================================================
370 // common classes implementation
371 // ============================================================================
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
379 // we have 2 implementations for modern Windows: one using standard Win32
380 // and another using DirectDraw, the choice between them is done using a
383 #ifdef wxUSE_DIRECTDRAW
384 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
386 wxDisplayFactoryDirectDraw
*factoryDD
= new wxDisplayFactoryDirectDraw
;
387 if ( factoryDD
->IsOk() )
392 #endif // wxUSE_DIRECTDRAW
394 wxDisplayFactoryMultimon
*factoryMM
= new wxDisplayFactoryMultimon
;
395 if ( factoryMM
->IsOk() )
401 // finally fall back to a stub implementation if all else failed (Win95?)
402 return new wxDisplayFactorySingle
;
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 void wxDisplayInfo::Initialize()
411 if ( m_flags
== (DWORD
)-1 )
413 WinStruct
<MONITORINFOEX
> monInfo
;
414 if ( !gs_GetMonitorInfo(m_hmon
, (LPMONITORINFO
)&monInfo
) )
416 wxLogLastError(_T("GetMonitorInfo"));
421 wxCopyRECTToRect(monInfo
.rcMonitor
, m_rect
);
422 wxCopyRECTToRect(monInfo
.rcWork
, m_rectClient
);
423 m_devName
= monInfo
.szDevice
;
424 m_flags
= monInfo
.dwFlags
;
428 // ----------------------------------------------------------------------------
429 // wxDisplayImplWin32Base
430 // ----------------------------------------------------------------------------
432 wxRect
wxDisplayImplWin32Base::GetGeometry() const
434 if ( m_info
.m_rect
.IsEmpty() )
437 return m_info
.m_rect
;
440 wxRect
wxDisplayImplWin32Base::GetClientArea() const
442 if ( m_info
.m_rectClient
.IsEmpty() )
445 return m_info
.m_rectClient
;
448 wxString
wxDisplayImplWin32Base::GetName() const
450 if ( m_info
.m_devName
.IsEmpty() )
453 return m_info
.m_devName
;
456 bool wxDisplayImplWin32Base::IsPrimary() const
458 if ( m_info
.m_flags
== (DWORD
)-1 )
461 return (m_info
.m_flags
& MONITORINFOF_PRIMARY
) != 0;
464 wxVideoMode
wxDisplayImplWin32Base::GetCurrentMode() const
468 // The first parameter of EnumDisplaySettings() must be NULL under Win95
469 // according to MSDN. The version of GetName() we implement for Win95
470 // returns an empty string.
471 const wxString name
= GetName();
472 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
475 dm
.dmSize
= sizeof(dm
);
476 dm
.dmDriverExtra
= 0;
477 if ( !::EnumDisplaySettings(deviceName
, ENUM_CURRENT_SETTINGS
, &dm
) )
479 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
483 mode
= ConvertToVideoMode(dm
);
489 // ----------------------------------------------------------------------------
490 // wxDisplayFactoryWin32Base
491 // ----------------------------------------------------------------------------
493 int wxDisplayFactoryWin32Base::ms_supportsMultimon
= -1;
495 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
497 if ( ms_supportsMultimon
== -1 )
499 ms_supportsMultimon
= 0;
501 wxDynamicLibrary
dllUser32(_T("user32.dll"));
505 gs_MonitorFromPoint
= (MonitorFromPoint_t
)
506 dllUser32
.GetSymbol(wxT("MonitorFromPoint"));
507 if ( !gs_MonitorFromPoint
)
510 gs_MonitorFromWindow
= (MonitorFromWindow_t
)
511 dllUser32
.GetSymbol(wxT("MonitorFromWindow"));
512 if ( !gs_MonitorFromWindow
)
515 gs_GetMonitorInfo
= (GetMonitorInfo_t
)
516 dllUser32
.GetSymbolAorW(wxT("GetMonitorInfo"));
517 if ( !gs_GetMonitorInfo
)
520 ms_supportsMultimon
= 1;
522 // we can safely let dllUser32 go out of scope, the DLL itself will
523 // still remain loaded as all Win32 programs use it
527 void wxDisplayFactoryWin32Base::Clear()
529 WX_CLEAR_ARRAY(m_displays
);
532 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
537 // helper for GetFromPoint() and GetFromWindow()
538 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon
) const
542 const size_t count
= m_displays
.size();
543 for ( size_t n
= 0; n
< count
; n
++ )
545 if ( hmon
== m_displays
[n
]->m_hmon
)
553 int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint
& pt
)
559 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2
,
560 MONITOR_DEFAULTTONULL
));
563 int wxDisplayFactoryWin32Base::GetFromWindow(wxWindow
*window
)
565 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window
),
566 MONITOR_DEFAULTTONULL
));
569 // ============================================================================
570 // wxDisplay implementation using Win32 multimon API
571 // ============================================================================
573 // ----------------------------------------------------------------------------
574 // wxDisplayFactoryMultimon initialization
575 // ----------------------------------------------------------------------------
577 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
579 if ( !ms_supportsMultimon
)
582 // look up EnumDisplayMonitors() which we don't need with DirectDraw
584 EnumDisplayMonitors_t pfnEnumDisplayMonitors
;
588 wxDynamicLibrary
dllUser32(_T("user32.dll"));
589 pfnEnumDisplayMonitors
= (EnumDisplayMonitors_t
)
590 dllUser32
.GetSymbol(wxT("EnumDisplayMonitors"));
591 if ( !pfnEnumDisplayMonitors
)
595 // enumerate all displays
596 if ( !pfnEnumDisplayMonitors(NULL
, NULL
, MultimonEnumProc
, (LPARAM
)this) )
598 wxLogLastError(wxT("EnumDisplayMonitors"));
604 wxDisplayFactoryMultimon::MultimonEnumProc(
605 HMONITOR hMonitor
, // handle to display monitor
606 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
607 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
608 LPARAM dwData
// data passed from EnumDisplayMonitors (this)
611 wxDisplayFactoryMultimon
*const self
= (wxDisplayFactoryMultimon
*)dwData
;
612 self
->AddDisplay(hMonitor
, lprcMonitor
);
614 // continue the enumeration
618 // ----------------------------------------------------------------------------
619 // wxDisplayFactoryMultimon helper functions
620 // ----------------------------------------------------------------------------
622 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor
, LPRECT lprcMonitor
)
624 wxDisplayInfo
*info
= new wxDisplayInfo(hMonitor
);
626 // we also store the display geometry
627 info
->m_rect
= wxRect(lprcMonitor
->left
, lprcMonitor
->top
,
628 lprcMonitor
->right
- lprcMonitor
->left
,
629 lprcMonitor
->bottom
- lprcMonitor
->top
);
631 // now add this monitor to the array
632 m_displays
.Add(info
);
635 // ----------------------------------------------------------------------------
636 // wxDisplayFactoryMultimon inherited pure virtuals implementation
637 // ----------------------------------------------------------------------------
639 wxDisplayImpl
*wxDisplayFactoryMultimon::CreateDisplay(size_t n
)
641 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
643 return new wxDisplayImplMultimon(n
, *(m_displays
[n
]));
646 // ----------------------------------------------------------------------------
647 // wxDisplayImplMultimon implementation
648 // ----------------------------------------------------------------------------
651 wxDisplayImplMultimon::GetModes(const wxVideoMode
& modeMatch
) const
653 wxArrayVideoModes modes
;
655 // The first parameter of EnumDisplaySettings() must be NULL under Win95
656 // according to MSDN. The version of GetName() we implement for Win95
657 // returns an empty string.
658 const wxString name
= GetName();
659 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
662 dm
.dmSize
= sizeof(dm
);
663 dm
.dmDriverExtra
= 0;
664 for ( int iModeNum
= 0;
665 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
668 const wxVideoMode mode
= ConvertToVideoMode(dm
);
669 if ( mode
.Matches(modeMatch
) )
678 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode
& mode
)
680 // prepare ChangeDisplaySettingsEx() parameters
686 if ( mode
== wxDefaultVideoMode
)
688 // reset the video mode to default
692 else // change to the given mode
694 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
695 _T("at least the width and height must be specified") );
698 dm
.dmSize
= sizeof(dm
);
699 dm
.dmDriverExtra
= 0;
700 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
701 dm
.dmPelsWidth
= mode
.w
;
702 dm
.dmPelsHeight
= mode
.h
;
706 dm
.dmFields
|= DM_BITSPERPEL
;
707 dm
.dmBitsPerPel
= mode
.bpp
;
712 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
713 dm
.dmDisplayFrequency
= mode
.refresh
;
720 #else // !__WXWINCE__
721 flags
= CDS_FULLSCREEN
;
722 #endif // __WXWINCE__/!__WXWINCE__
726 // get pointer to the function dynamically
728 // we're only called from the main thread, so it's ok to use static
730 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
731 if ( !pfnChangeDisplaySettingsEx
)
733 wxDynamicLibrary
dllUser32(_T("user32.dll"));
734 if ( dllUser32
.IsLoaded() )
736 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
737 dllUser32
.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
739 //else: huh, no user32.dll??
742 if ( !pfnChangeDisplaySettingsEx
)
744 // we must be under Win95 and so there is no multiple monitors
746 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
748 #endif // !__WXWINCE__
751 // do change the mode
752 switch ( pfnChangeDisplaySettingsEx
754 GetName(), // display name
755 pDevMode
, // dev mode or NULL to reset
758 NULL
// pointer to video parameters (not used)
761 case DISP_CHANGE_SUCCESSFUL
:
764 // If we have a top-level, full-screen frame, emulate
765 // the DirectX behavior and resize it. This makes this
766 // API quite a bit easier to use.
767 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
768 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
769 if (frameTop
&& frameTop
->IsFullScreen())
771 wxVideoMode current
= GetCurrentMode();
772 frameTop
->SetClientSize(current
.w
, current
.h
);
777 case DISP_CHANGE_BADMODE
:
778 // don't complain about this, this is the only "expected" error
782 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
789 // ============================================================================
790 // DirectDraw-based wxDisplay implementation
791 // ============================================================================
795 // ----------------------------------------------------------------------------
796 // wxDisplayFactoryDirectDraw initialization
797 // ----------------------------------------------------------------------------
799 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
801 if ( !ms_supportsMultimon
)
805 // suppress the errors if ddraw.dll is not found, we're prepared to handle
810 m_dllDDraw
.Load(_T("ddraw.dll"));
812 if ( !m_dllDDraw
.IsLoaded() )
815 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
816 m_dllDDraw
.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
820 // we can't continue without DirectDrawCreate() later, so resolve it right
821 // now and fail the initialization if it's not available
822 m_pfnDirectDrawCreate
= (DirectDrawCreate_t
)
823 m_dllDDraw
.GetSymbol(_T("DirectDrawCreate"));
824 if ( !m_pfnDirectDrawCreate
)
827 if ( (*pDDEnumEx
)(DDEnumExCallback
,
829 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
831 wxLogLastError(_T("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 wx_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(size_t n
)
882 wxCHECK_MSG( n
< m_displays
.size(), NULL
, _T("invalid display index") );
884 wxDisplayInfoDirectDraw
*
885 info
= wx_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(_T("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(_T("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 DECLARE_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 wx_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(_T("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, _T("top level window required for DirectX") );
1002 HRESULT hr
= m_pDD2
->SetCooperativeLevel
1005 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
1009 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr
);
1014 hr
= m_pDD2
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
1017 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr
);
1025 #endif // wxUSE_DIRECTDRAW
1027 #endif // wxUSE_DISPLAY