+ if ( !ms_supportsMultimon )
+ return;
+
+ m_dllDDraw.Load(wxT("ddraw.dll"), wxDL_VERBATIM | wxDL_QUIET);
+
+ if ( !m_dllDDraw.IsLoaded() )
+ return;
+
+ DirectDrawEnumerateEx_t
+ wxDL_INIT_FUNC_AW(pfn, DirectDrawEnumerateEx, m_dllDDraw);
+ if ( !pfnDirectDrawEnumerateEx )
+ return;
+
+ // we can't continue without DirectDrawCreate() later, so resolve it right
+ // now and fail the initialization if it's not available
+ if ( !wxDL_INIT_FUNC(m_pfn, DirectDrawCreate, m_dllDDraw) )
+ return;
+
+ if ( (*pfnDirectDrawEnumerateEx)(DDEnumExCallback,
+ this,
+ DDENUM_ATTACHEDSECONDARYDEVICES) != DD_OK )
+ {
+ wxLogLastError(wxT("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 =
+ 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, wxT("invalid display index") );
+
+ wxDisplayInfoDirectDraw *
+ info = 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(wxT("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(wxT("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;
+
+ wxDECLARE_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 =
+ static_cast<wxDDVideoModesAdder *>(lpContext);
+
+ vmodes->Add(wxVideoMode(lpDDSurfaceDesc->dwWidth,
+ lpDDSurfaceDesc->dwHeight,
+ lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount,
+ lpDDSurfaceDesc->dwRefreshRate));
+ }
+
+ // continue the enumeration
+ return DDENUMRET_OK;