+ return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2,
+ MONITOR_DEFAULTTONULL));
+}
+
+int wxDisplayFactoryWin32Base::GetFromWindow(const wxWindow *window)
+{
+ return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window),
+ MONITOR_DEFAULTTONULL));
+}
+
+// ============================================================================
+// wxDisplay implementation using Win32 multimon API
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryMultimon initialization
+// ----------------------------------------------------------------------------
+
+wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
+{
+ if ( !ms_supportsMultimon )
+ return;
+
+ // look up EnumDisplayMonitors() which we don't need with DirectDraw
+ // implementation
+ EnumDisplayMonitors_t pfnEnumDisplayMonitors;
+ {
+ wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET);
+ if ( (wxDL_INIT_FUNC(pfn, EnumDisplayMonitors, dllDisplay)) == NULL )
+ return;
+ }
+
+ // enumerate all displays
+ if ( !pfnEnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) )
+ {
+ wxLogLastError(wxT("EnumDisplayMonitors"));
+ }
+}
+
+/* static */
+BOOL CALLBACK
+wxDisplayFactoryMultimon::MultimonEnumProc(
+ HMONITOR hMonitor, // handle to display monitor
+ HDC WXUNUSED(hdcMonitor), // handle to monitor-appropriate device context
+ LPRECT lprcMonitor, // pointer to monitor intersection rectangle
+ LPARAM dwData // data passed from EnumDisplayMonitors (this)
+)
+{
+ wxDisplayFactoryMultimon *const self = (wxDisplayFactoryMultimon *)dwData;
+ self->AddDisplay(hMonitor, lprcMonitor);
+
+ // continue the enumeration
+ return TRUE;
+}
+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryMultimon helper functions
+// ----------------------------------------------------------------------------
+
+void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor)
+{
+ wxDisplayInfo *info = new wxDisplayInfo(hMonitor);
+
+ // we also store the display geometry
+ info->m_rect = wxRect(lprcMonitor->left, lprcMonitor->top,
+ lprcMonitor->right - lprcMonitor->left,
+ lprcMonitor->bottom - lprcMonitor->top);
+
+ // now add this monitor to the array
+ m_displays.Add(info);
+}
+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryMultimon inherited pure virtuals implementation
+// ----------------------------------------------------------------------------
+
+wxDisplayImpl *wxDisplayFactoryMultimon::CreateDisplay(unsigned n)
+{
+ wxCHECK_MSG( n < m_displays.size(), NULL, _T("invalid display index") );
+
+ return new wxDisplayImplMultimon(n, *(m_displays[n]));
+}
+
+// ----------------------------------------------------------------------------
+// wxDisplayImplMultimon implementation
+// ----------------------------------------------------------------------------
+
+wxArrayVideoModes
+wxDisplayImplMultimon::GetModes(const wxVideoMode& modeMatch) const
+{
+ wxArrayVideoModes modes;
+
+ // The first parameter of EnumDisplaySettings() must be NULL under Win95
+ // according to MSDN. The version of GetName() we implement for Win95
+ // returns an empty string.
+ const wxString name = GetName();
+ const wxChar * const deviceName = name.empty()
+ ? (const wxChar*)NULL
+ : (const wxChar*)name.c_str();
+
+ DEVMODE dm;
+ dm.dmSize = sizeof(dm);
+ dm.dmDriverExtra = 0;
+ for ( int iModeNum = 0;
+ ::EnumDisplaySettings(deviceName, iModeNum, &dm);
+ iModeNum++ )
+ {
+ const wxVideoMode mode = ConvertToVideoMode(dm);
+ if ( mode.Matches(modeMatch) )
+ {
+ modes.Add(mode);
+ }
+ }
+
+ return modes;
+}
+
+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.GetWidth() && mode.GetHeight(), 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.GetWidth();
+ dm.dmPelsHeight = mode.GetHeight();
+
+ if ( mode.GetDepth() )
+ {
+ dm.dmFields |= DM_BITSPERPEL;
+ dm.dmBitsPerPel = mode.GetDepth();
+ }
+
+ if ( mode.GetRefresh() )
+ {
+ dm.dmFields |= DM_DISPLAYFREQUENCY;
+ dm.dmDisplayFrequency = mode.GetRefresh();
+ }
+
+ 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 dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET);
+ if ( dllDisplay.IsLoaded() )
+ {
+ wxDL_INIT_FUNC_AW(pfn, ChangeDisplaySettingsEx, dllDisplay);
+ }
+ //else: huh, no this DLL must always be present, what's going on??
+
+#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().wx_str(), // 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.GetWidth(), current.GetHeight());
+ }
+ }
+ 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;
+
+ m_dllDDraw.Load(_T("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(_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
+// ----------------------------------------------------------------------------