+bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode& mode)
+{
+ // prepare ChangeDisplaySettingsEx() parameters
+ DEVMODE dm;
+ DEVMODE *pDevMode;
+
+ int flags;
+
+ if ( mode == wxDefaultVideoMode )
+ {
+ // reset the video mode to default
+ pDevMode = NULL;
+ flags = 0;
+ }
+ else // change to the given mode
+ {
+ wxCHECK_MSG( mode.w && mode.h, false,
+ _T("at least the width and height must be specified") );
+
+ wxZeroMemory(dm);
+ dm.dmSize = sizeof(dm);
+ dm.dmDriverExtra = 0;
+ dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
+ dm.dmPelsWidth = mode.w;
+ dm.dmPelsHeight = mode.h;
+
+ if ( mode.bpp )
+ {
+ dm.dmFields |= DM_BITSPERPEL;
+ dm.dmBitsPerPel = mode.bpp;
+ }
+
+ if ( mode.refresh )
+ {
+ dm.dmFields |= DM_DISPLAYFREQUENCY;
+ dm.dmDisplayFrequency = mode.refresh;
+ }
+
+ pDevMode = &dm;
+
+#ifdef __WXWINCE__
+ flags = 0;
+#else // !__WXWINCE__
+ flags = CDS_FULLSCREEN;
+#endif // __WXWINCE__/!__WXWINCE__
+ }
+
+
+ // get pointer to the function dynamically
+ //
+ // we're only called from the main thread, so it's ok to use static
+ // variable
+ static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx = NULL;
+ if ( !pfnChangeDisplaySettingsEx )
+ {
+ wxDynamicLibrary dllUser32(_T("user32.dll"));
+ if ( dllUser32.IsLoaded() )
+ {
+ pfnChangeDisplaySettingsEx = (ChangeDisplaySettingsEx_t)
+ dllUser32.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
+ }
+ //else: huh, no user32.dll??
+
+#ifndef __WXWINCE__
+ if ( !pfnChangeDisplaySettingsEx )
+ {
+ // we must be under Win95 and so there is no multiple monitors
+ // support anyhow
+ pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95;
+ }
+#endif // !__WXWINCE__
+ }
+
+ // do change the mode
+ switch ( pfnChangeDisplaySettingsEx
+ (
+ GetName(), // display name
+ pDevMode, // dev mode or NULL to reset
+ NULL, // reserved
+ flags,
+ NULL // pointer to video parameters (not used)
+ ) )
+ {
+ case DISP_CHANGE_SUCCESSFUL:
+ // ok
+ {
+ // If we have a top-level, full-screen frame, emulate
+ // the DirectX behavior and resize it. This makes this
+ // API quite a bit easier to use.
+ wxWindow *winTop = wxTheApp->GetTopWindow();
+ wxFrame *frameTop = wxDynamicCast(winTop, wxFrame);
+ if (frameTop && frameTop->IsFullScreen())
+ {
+ wxVideoMode current = GetCurrentMode();
+ frameTop->SetClientSize(current.w, current.h);
+ }
+ }
+ return true;
+
+ case DISP_CHANGE_BADMODE:
+ // don't complain about this, this is the only "expected" error
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
+ }
+
+ return false;
+}
+
+
+// ============================================================================
+// DirectDraw-based wxDisplay implementation
+// ============================================================================
+
+#if wxUSE_DIRECTDRAW
+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryDirectDraw initialization
+// ----------------------------------------------------------------------------
+
+wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
+{
+ if ( !ms_supportsMultimon )
+ return;
+
+#if wxUSE_LOG
+ // suppress the errors if ddraw.dll is not found, we're prepared to handle
+ // this
+ wxLogNull noLog;
+#endif
+
+ m_dllDDraw.Load(_T("ddraw.dll"));
+
+ if ( !m_dllDDraw.IsLoaded() )
+ return;
+
+ DirectDrawEnumerateEx_t pDDEnumEx = (DirectDrawEnumerateEx_t)
+ m_dllDDraw.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
+ if ( !pDDEnumEx )
+ return;
+
+ // we can't continue without DirectDrawCreate() later, so resolve it right
+ // now and fail the initialization if it's not available
+ m_pfnDirectDrawCreate = (DirectDrawCreate_t)
+ m_dllDDraw.GetSymbol(_T("DirectDrawCreate"));
+ if ( !m_pfnDirectDrawCreate )
+ return;
+
+ if ( (*pDDEnumEx)(DDEnumExCallback,
+ this,
+ DDENUM_ATTACHEDSECONDARYDEVICES) != DD_OK )
+ {
+ wxLogLastError(_T("DirectDrawEnumerateEx"));
+ }
+}
+
+wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
+{
+ // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
+ // calling m_pDD2->Release() later would crash
+ Clear();
+}
+
+// ----------------------------------------------------------------------------
+// callbacks for monitor/modes enumeration stuff
+// ----------------------------------------------------------------------------
+
+BOOL WINAPI
+wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID *pGuid,
+ LPTSTR WXUNUSED(driverDescription),
+ LPTSTR driverName,
+ LPVOID lpContext,
+ HMONITOR hmon)
+{
+ if ( pGuid )
+ {
+ wxDisplayFactoryDirectDraw * self =
+ wx_static_cast(wxDisplayFactoryDirectDraw *, lpContext);
+ self->AddDisplay(*pGuid, hmon, driverName);
+ }
+ //else: we're called for the primary monitor, skip it
+
+ // continue the enumeration
+ return TRUE;
+}
+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryDirectDraw helpers
+// ----------------------------------------------------------------------------
+
+void wxDisplayFactoryDirectDraw::AddDisplay(const GUID& guid,
+ HMONITOR hmon,
+ LPTSTR name)
+{
+ m_displays.Add(new wxDisplayInfoDirectDraw(guid, hmon, name));
+}
+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryDirectDraw inherited pure virtuals implementation
+// ----------------------------------------------------------------------------
+
+wxDisplayImpl *wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n)
+{
+ wxCHECK_MSG( n < m_displays.size(), NULL, _T("invalid display index") );
+
+ wxDisplayInfoDirectDraw *
+ info = wx_static_cast(wxDisplayInfoDirectDraw *, m_displays[n]);
+
+ if ( !info->m_pDD2 )
+ {
+ IDirectDraw *pDD;
+ GUID guid(info->m_guid);
+ HRESULT hr = (*m_pfnDirectDrawCreate)(&guid, &pDD, NULL);
+
+ if ( FAILED(hr) || !pDD )
+ {
+ // what to do??
+ wxLogApiError(_T("DirectDrawCreate"), hr);
+ return NULL;
+ }
+
+ // we got IDirectDraw, but we need IDirectDraw2
+ hr = pDD->QueryInterface(wxIID_IDirectDraw2, (void **)&info->m_pDD2);
+ pDD->Release();
+
+ if ( FAILED(hr) || !info->m_pDD2 )
+ {
+ wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr);
+ return NULL;
+ }
+
+ // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
+ // which is ok as we don't want to recreate DD objects all the time
+ }
+ //else: DirectDraw object corresponding to our display already exists
+
+ return new wxDisplayImplDirectDraw(n, *info, info->m_pDD2);
+}
+
+// ============================================================================
+// wxDisplayImplDirectDraw
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// video modes enumeration
+// ----------------------------------------------------------------------------
+
+// tiny helper class used to pass information from GetModes() to
+// wxDDEnumModesCallback
+class wxDDVideoModesAdder
+{
+public:
+ // our Add() method will add modes matching modeMatch to modes array
+ wxDDVideoModesAdder(wxArrayVideoModes& modes, const wxVideoMode& modeMatch)
+ : m_modes(modes),
+ m_modeMatch(modeMatch)
+ {
+ }
+
+ void Add(const wxVideoMode& mode)
+ {
+ if ( mode.Matches(m_modeMatch) )
+ m_modes.Add(mode);
+ }
+
+private:
+ wxArrayVideoModes& m_modes;
+ const wxVideoMode& m_modeMatch;
+
+ DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder)
+};
+
+HRESULT WINAPI wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc,
+ LPVOID lpContext)
+{
+ // we need at least the mode size
+ static const DWORD FLAGS_REQUIRED = DDSD_HEIGHT | DDSD_WIDTH;
+ if ( (lpDDSurfaceDesc->dwFlags & FLAGS_REQUIRED) == FLAGS_REQUIRED )
+ {
+ wxDDVideoModesAdder * const vmodes =
+ wx_static_cast(wxDDVideoModesAdder *, lpContext);
+
+ vmodes->Add(wxVideoMode(lpDDSurfaceDesc->dwWidth,
+ lpDDSurfaceDesc->dwHeight,
+ lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount,
+ lpDDSurfaceDesc->dwRefreshRate));
+ }
+
+ // continue the enumeration
+ return DDENUMRET_OK;
+}
+
+wxArrayVideoModes
+wxDisplayImplDirectDraw::GetModes(const wxVideoMode& modeMatch) const
+{
+ wxArrayVideoModes modes;
+ wxDDVideoModesAdder modesAdder(modes, modeMatch);
+
+ HRESULT hr = m_pDD2->EnumDisplayModes
+ (
+ DDEDM_REFRESHRATES,
+ NULL, // all modes
+ &modesAdder, // callback parameter
+ wxDDEnumModesCallback
+ );
+
+ if ( FAILED(hr) )
+ {
+ wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr);
+ }
+
+ return modes;
+}
+
+// ----------------------------------------------------------------------------
+// video mode switching
+// ----------------------------------------------------------------------------
+
+bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode& mode)
+{
+ wxWindow *winTop = wxTheApp->GetTopWindow();
+ wxCHECK_MSG( winTop, false, _T("top level window required for DirectX") );
+
+ HRESULT hr = m_pDD2->SetCooperativeLevel
+ (
+ GetHwndOf(winTop),
+ DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN
+ );
+ if ( FAILED(hr) )
+ {
+ wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr);
+
+ return false;
+ }
+
+ hr = m_pDD2->SetDisplayMode(mode.w, mode.h, mode.bpp, mode.refresh, 0);
+ if ( FAILED(hr) )
+ {
+ wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr);
+
+ return false;
+ }
+
+ return true;
+}
+
+#endif // wxUSE_DIRECTDRAW
+
+#endif // wxUSE_DISPLAY