non-PCH build fix (according to Tinderbox).
[wxWidgets.git] / src / msw / display.cpp
CommitLineData
a536e411 1/////////////////////////////////////////////////////////////////////////////
2ad495fb 2// Name: src/msw/display.cpp
a536e411 3// Purpose: MSW Implementation of wxDisplay class
fd921f35
VZ
4// Author: Royce Mitchell III, Vadim Zeitlin
5// Modified by: Ryan Norton (IsPrimary override)
a536e411
JS
6// Created: 06/21/02
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
fd921f35 9// Copyright: (c) 2002-2006 wxWidgets team
65571936 10// Licence: wxWindows licence
a536e411
JS
11/////////////////////////////////////////////////////////////////////////////
12
13// ===========================================================================
14// declarations
15// ===========================================================================
16
17// ---------------------------------------------------------------------------
18// headers
19// ---------------------------------------------------------------------------
20
a536e411
JS
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
28#if wxUSE_DISPLAY
29
7212d155
PC
30#include "wx/display.h"
31
a536e411 32#ifndef WX_PRECOMP
ad9835c9
WS
33 #include "wx/dynarray.h"
34 #include "wx/app.h"
35 #include "wx/frame.h"
a536e411
JS
36#endif
37
8585e2b5 38#include "wx/dynload.h"
ef1717a9 39#include "wx/sysopt.h"
8585e2b5 40
ef1717a9 41#include "wx/display_impl.h"
7212d155 42#include "wx/msw/missing.h"
a536e411 43
b3825734
VZ
44// define this to use DirectDraw for display mode switching: this is disabled
45// by default because ddraw.h is now always available and also it's not really
46// clear what are the benefits of using DirectDraw compared to the standard API
ef1717a9 47
b3825734
VZ
48#if !defined(wxUSE_DIRECTDRAW)
49 #define wxUSE_DIRECTDRAW 0
a536e411
JS
50#endif
51
2ad495fb 52#ifndef __WXWINCE__
01c54165 53 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
bcc1153a
VZ
54 // can't directly test whether HMONITOR is defined or not in windef.h as
55 // it's not a macro but a typedef, so we test for an unrelated symbol which
56 // is only defined in winuser.h if WINVER >= 0x0500
57 #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK)
58 DECLARE_HANDLE(HMONITOR);
e5570318
JS
59 typedef BOOL(CALLBACK * MONITORENUMPROC )(HMONITOR, HDC, LPRECT, LPARAM);
60 typedef struct tagMONITORINFO
61 {
62 DWORD cbSize;
63 RECT rcMonitor;
64 RECT rcWork;
65 DWORD dwFlags;
66 } MONITORINFO, *LPMONITORINFO;
67 typedef struct tagMONITORINFOEX : public tagMONITORINFO
68 {
69 TCHAR szDevice[CCHDEVICENAME];
70 } MONITORINFOEX, *LPMONITORINFOEX;
71 #define MONITOR_DEFAULTTONULL 0x00000000
72 #define MONITOR_DEFAULTTOPRIMARY 0x00000001
73 #define MONITOR_DEFAULTTONEAREST 0x00000002
ad9835c9 74 #define MONITORINFOF_PRIMARY 0x00000001
bcc1153a
VZ
75 #define HMONITOR_DECLARED
76 #endif
2ad495fb 77#endif // !__WXWINCE__
a536e411 78
b3825734
VZ
79#if wxUSE_DIRECTDRAW
80 #include <ddraw.h>
06efac1f 81
b3825734
VZ
82 // we don't want to link with ddraw.lib which contains the real
83 // IID_IDirectDraw2 definition
84 const GUID wxIID_IDirectDraw2 =
85 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
ef1717a9 86#endif // wxUSE_DIRECTDRAW
06efac1f 87
a536e411 88// ----------------------------------------------------------------------------
06efac1f 89// typedefs for dynamically loaded Windows functions
a536e411
JS
90// ----------------------------------------------------------------------------
91
8585e2b5
VZ
92typedef LONG (WINAPI *ChangeDisplaySettingsEx_t)(LPCTSTR lpszDeviceName,
93 LPDEVMODE lpDevMode,
94 HWND hwnd,
95 DWORD dwFlags,
96 LPVOID lParam);
a536e411 97
b3825734 98#if wxUSE_DIRECTDRAW
06efac1f
VZ
99typedef BOOL (PASCAL *DDEnumExCallback_t)(GUID *pGuid,
100 LPTSTR driverDescription,
101 LPTSTR driverName,
102 LPVOID lpContext,
103 HMONITOR hmon);
104
105typedef HRESULT (WINAPI *DirectDrawEnumerateEx_t)(DDEnumExCallback_t lpCallback,
106 LPVOID lpContext,
107 DWORD dwFlags);
108
109typedef HRESULT (WINAPI *DirectDrawCreate_t)(GUID *lpGUID,
110 LPDIRECTDRAW *lplpDD,
111 IUnknown *pUnkOuter);
ef1717a9 112#endif // wxUSE_DIRECTDRAW
01c54165
VZ
113
114typedef BOOL (WINAPI *EnumDisplayMonitors_t)(HDC,LPCRECT,MONITORENUMPROC,LPARAM);
115typedef HMONITOR (WINAPI *MonitorFromPoint_t)(POINT,DWORD);
116typedef HMONITOR (WINAPI *MonitorFromWindow_t)(HWND,DWORD);
117typedef BOOL (WINAPI *GetMonitorInfo_t)(HMONITOR,LPMONITORINFO);
118
ef1717a9
VZ
119#ifndef __WXWINCE__
120// emulation of ChangeDisplaySettingsEx() for Win95
121LONG WINAPI ChangeDisplaySettingsExForWin95(LPCTSTR WXUNUSED(lpszDeviceName),
122 LPDEVMODE lpDevMode,
123 HWND WXUNUSED(hwnd),
124 DWORD dwFlags,
125 LPVOID WXUNUSED(lParam))
126{
127 return ::ChangeDisplaySettings(lpDevMode, dwFlags);
128}
129#endif // !__WXWINCE__
06efac1f 130
8585e2b5 131// ----------------------------------------------------------------------------
ef1717a9 132// display information classes
8585e2b5 133// ----------------------------------------------------------------------------
a536e411 134
ef1717a9 135struct wxDisplayInfo
a536e411 136{
ef1717a9
VZ
137 wxDisplayInfo(HMONITOR hmon = NULL)
138 {
139 m_hmon = hmon;
140 m_flags = (DWORD)-1;
141 }
8585e2b5 142
ef1717a9 143 virtual ~wxDisplayInfo() { }
06efac1f 144
ef1717a9
VZ
145
146 // use GetMonitorInfo() to fill in all of our fields if needed (i.e. if it
147 // hadn't been done before)
148 void Initialize();
149
150
151 // handle of this monitor used by MonitorXXX() functions, never NULL
152 HMONITOR m_hmon;
06efac1f 153
8585e2b5 154 // the entire area of this monitor in virtual screen coordinates
a536e411 155 wxRect m_rect;
8585e2b5 156
6c5d6291
VZ
157 // the work or client area, i.e. the area available for the normal windows
158 wxRect m_rectClient;
159
8585e2b5
VZ
160 // the display device name for this monitor, empty initially and retrieved
161 // on demand by DoGetName()
162 wxString m_devName;
163
ef1717a9
VZ
164 // the flags of this monitor, also used as initialization marker: if this
165 // is -1, GetMonitorInfo() hadn't been called yet
166 DWORD m_flags;
8585e2b5
VZ
167};
168
ef1717a9 169WX_DEFINE_ARRAY_PTR(wxDisplayInfo *, wxDisplayInfoArray);
a536e411 170
06efac1f 171// ----------------------------------------------------------------------------
ef1717a9 172// common base class for all Win32 wxDisplayImpl versions
06efac1f
VZ
173// ----------------------------------------------------------------------------
174
ef1717a9 175class wxDisplayImplWin32Base : public wxDisplayImpl
01c54165 176{
ef1717a9 177public:
4e675101 178 wxDisplayImplWin32Base(unsigned n, wxDisplayInfo& info)
ef1717a9
VZ
179 : wxDisplayImpl(n),
180 m_info(info)
01c54165 181 {
01c54165 182 }
01c54165 183
ef1717a9 184 virtual wxRect GetGeometry() const;
6c5d6291 185 virtual wxRect GetClientArea() const;
ef1717a9
VZ
186 virtual wxString GetName() const;
187 virtual bool IsPrimary() const;
06efac1f 188
ef1717a9 189 virtual wxVideoMode GetCurrentMode() const;
06efac1f 190
ef1717a9
VZ
191protected:
192 // convert a DEVMODE to our wxVideoMode
193 static wxVideoMode ConvertToVideoMode(const DEVMODE& dm)
194 {
195 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
196 // and although 0 is ok for us we don't want to return modes with 1hz
197 // refresh
198 return wxVideoMode(dm.dmPelsWidth,
199 dm.dmPelsHeight,
200 dm.dmBitsPerPel,
201 dm.dmDisplayFrequency > 1 ? dm.dmDisplayFrequency : 0);
202 }
203
204 wxDisplayInfo& m_info;
205};
a536e411 206
8585e2b5 207// ----------------------------------------------------------------------------
ef1717a9 208// common base class for all Win32 wxDisplayFactory versions
8585e2b5
VZ
209// ----------------------------------------------------------------------------
210
ef1717a9
VZ
211// functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
212static MonitorFromPoint_t gs_MonitorFromPoint = NULL;
213static MonitorFromWindow_t gs_MonitorFromWindow = NULL;
214static GetMonitorInfo_t gs_GetMonitorInfo = NULL;
8585e2b5 215
ef1717a9
VZ
216class wxDisplayFactoryWin32Base : public wxDisplayFactory
217{
218public:
219 virtual ~wxDisplayFactoryWin32Base();
8585e2b5 220
ef1717a9 221 bool IsOk() const { return !m_displays.empty(); }
8585e2b5 222
4e675101 223 virtual unsigned GetCount() { return unsigned(m_displays.size()); }
ef1717a9
VZ
224 virtual int GetFromPoint(const wxPoint& pt);
225 virtual int GetFromWindow(wxWindow *window);
a536e411 226
ef1717a9
VZ
227protected:
228 // ctor checks if the current system supports multimon API and dynamically
229 // bind the functions we need if this is the case and sets
230 // ms_supportsMultimon if they're available
231 wxDisplayFactoryWin32Base();
a536e411 232
ef1717a9
VZ
233 // delete all m_displays elements: can be called from the derived class
234 // dtor if it is important to do this before destroying it (as in
235 // wxDisplayFactoryDirectDraw case), otherwise will be done by our dtor
236 void Clear();
06efac1f 237
ef1717a9
VZ
238 // find the monitor corresponding to the given handle, return wxNOT_FOUND
239 // if not found
240 int FindDisplayFromHMONITOR(HMONITOR hmon) const;
06efac1f 241
06efac1f 242
ef1717a9
VZ
243 // flag indicating whether gs_MonitorXXX functions are available
244 static int ms_supportsMultimon;
06efac1f 245
ef1717a9
VZ
246 // the array containing information about all available displays, should be
247 // filled by the derived class ctors
248 wxDisplayInfoArray m_displays;
06efac1f 249
06efac1f 250
ef1717a9
VZ
251 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base)
252};
06efac1f
VZ
253
254// ----------------------------------------------------------------------------
ef1717a9 255// wxDisplay implementation using Windows multi-monitor support functions
06efac1f
VZ
256// ----------------------------------------------------------------------------
257
ef1717a9 258class wxDisplayImplMultimon : public wxDisplayImplWin32Base
06efac1f 259{
ef1717a9 260public:
4e675101 261 wxDisplayImplMultimon(unsigned n, wxDisplayInfo& info)
ef1717a9
VZ
262 : wxDisplayImplWin32Base(n, info)
263 {
264 }
a536e411 265
ef1717a9
VZ
266 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
267 virtual bool ChangeMode(const wxVideoMode& mode);
a536e411 268
ef1717a9
VZ
269private:
270 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon)
271};
06efac1f 272
d14e03f5 273class wxDisplayFactoryMultimon : public wxDisplayFactoryWin32Base
ef1717a9
VZ
274{
275public:
276 wxDisplayFactoryMultimon();
06efac1f 277
4e675101 278 virtual wxDisplayImpl *CreateDisplay(unsigned n);
06efac1f 279
ef1717a9
VZ
280private:
281 // EnumDisplayMonitors() callback
282 static BOOL CALLBACK MultimonEnumProc(HMONITOR hMonitor,
283 HDC hdcMonitor,
284 LPRECT lprcMonitor,
285 LPARAM dwData);
06efac1f 286
06efac1f 287
ef1717a9
VZ
288 // add a monitor description to m_displays array
289 void AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor);
290};
06efac1f 291
ef1717a9
VZ
292// ----------------------------------------------------------------------------
293// wxDisplay implementation using DirectDraw
294// ----------------------------------------------------------------------------
06efac1f 295
b3825734 296#if wxUSE_DIRECTDRAW
ef1717a9
VZ
297
298struct wxDisplayInfoDirectDraw : wxDisplayInfo
06efac1f 299{
ef1717a9
VZ
300 wxDisplayInfoDirectDraw(const GUID& guid, HMONITOR hmon, LPTSTR name)
301 : wxDisplayInfo(hmon),
302 m_guid(guid)
a536e411 303 {
ef1717a9
VZ
304 m_pDD2 = NULL;
305 m_devName = name;
306 }
8585e2b5 307
ef1717a9
VZ
308 virtual ~wxDisplayInfoDirectDraw()
309 {
310 if ( m_pDD2 )
311 m_pDD2->Release();
a536e411
JS
312 }
313
06efac1f 314
ef1717a9
VZ
315 // IDirectDraw object used to control this display, may be NULL
316 IDirectDraw2 *m_pDD2;
317
318 // DirectDraw GUID for this display, only valid when using DirectDraw
319 const GUID m_guid;
320
321
322 DECLARE_NO_COPY_CLASS(wxDisplayInfoDirectDraw)
323};
06efac1f 324
ef1717a9
VZ
325class wxDisplayImplDirectDraw : public wxDisplayImplWin32Base
326{
327public:
4e675101 328 wxDisplayImplDirectDraw(unsigned n, wxDisplayInfo& info, IDirectDraw2 *pDD2)
ef1717a9
VZ
329 : wxDisplayImplWin32Base(n, info),
330 m_pDD2(pDD2)
06efac1f 331 {
ef1717a9
VZ
332 m_pDD2->AddRef();
333 }
06efac1f 334
ef1717a9
VZ
335 virtual ~wxDisplayImplDirectDraw()
336 {
337 m_pDD2->Release();
06efac1f 338 }
06efac1f 339
ef1717a9
VZ
340 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
341 virtual bool ChangeMode(const wxVideoMode& mode);
a536e411 342
ef1717a9
VZ
343private:
344 IDirectDraw2 *m_pDD2;
a536e411 345
ef1717a9
VZ
346 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw)
347};
a536e411 348
d14e03f5 349class wxDisplayFactoryDirectDraw : public wxDisplayFactoryWin32Base
8585e2b5 350{
ef1717a9
VZ
351public:
352 wxDisplayFactoryDirectDraw();
353 virtual ~wxDisplayFactoryDirectDraw();
a536e411 354
4e675101 355 virtual wxDisplayImpl *CreateDisplay(unsigned n);
a536e411 356
ef1717a9
VZ
357private:
358 // callback used with DirectDrawEnumerateEx()
359 static BOOL WINAPI DDEnumExCallback(GUID *pGuid,
360 LPTSTR driverDescription,
361 LPTSTR driverName,
362 LPVOID lpContext,
363 HMONITOR hmon);
06efac1f 364
ef1717a9
VZ
365 // add a monitor description to m_displays array
366 void AddDisplay(const GUID& guid, HMONITOR hmon, LPTSTR name);
06efac1f 367
a536e411 368
ef1717a9
VZ
369 // ddraw.dll
370 wxDynamicLibrary m_dllDDraw;
a536e411 371
ef1717a9
VZ
372 // dynamically resolved DirectDrawCreate()
373 DirectDrawCreate_t m_pfnDirectDrawCreate;
a536e411 374
ef1717a9
VZ
375 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw)
376};
01c54165 377
ef1717a9 378#endif // wxUSE_DIRECTDRAW
8585e2b5 379
8585e2b5 380
ef1717a9
VZ
381// ============================================================================
382// common classes implementation
383// ============================================================================
a536e411 384
ef1717a9
VZ
385// ----------------------------------------------------------------------------
386// wxDisplay
387// ----------------------------------------------------------------------------
388
389/* static */ wxDisplayFactory *wxDisplay::CreateFactory()
a536e411 390{
ef1717a9
VZ
391 // we have 2 implementations for modern Windows: one using standard Win32
392 // and another using DirectDraw, the choice between them is done using a
393 // system option
7487919e 394
b3825734 395#if wxUSE_DIRECTDRAW
ef1717a9 396 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
01c54165 397 {
ef1717a9
VZ
398 wxDisplayFactoryDirectDraw *factoryDD = new wxDisplayFactoryDirectDraw;
399 if ( factoryDD->IsOk() )
400 return factoryDD;
401
402 delete factoryDD;
01c54165 403 }
7487919e 404#endif // wxUSE_DIRECTDRAW
01c54165 405
ef1717a9
VZ
406 wxDisplayFactoryMultimon *factoryMM = new wxDisplayFactoryMultimon;
407 if ( factoryMM->IsOk() )
408 return factoryMM;
a536e411 409
ef1717a9
VZ
410 delete factoryMM;
411
412
413 // finally fall back to a stub implementation if all else failed (Win95?)
414 return new wxDisplayFactorySingle;
8585e2b5 415}
a536e411 416
ef1717a9
VZ
417// ----------------------------------------------------------------------------
418// wxDisplayInfo
419// ----------------------------------------------------------------------------
420
421void wxDisplayInfo::Initialize()
8585e2b5 422{
ef1717a9 423 if ( m_flags == (DWORD)-1 )
01c54165 424 {
ef1717a9
VZ
425 WinStruct<MONITORINFOEX> monInfo;
426 if ( !gs_GetMonitorInfo(m_hmon, (LPMONITORINFO)&monInfo) )
01c54165 427 {
ef1717a9
VZ
428 wxLogLastError(_T("GetMonitorInfo"));
429 m_flags = 0;
430 return;
01c54165 431 }
01c54165 432
ef1717a9 433 wxCopyRECTToRect(monInfo.rcMonitor, m_rect);
6c5d6291 434 wxCopyRECTToRect(monInfo.rcWork, m_rectClient);
ef1717a9
VZ
435 m_devName = monInfo.szDevice;
436 m_flags = monInfo.dwFlags;
437 }
8585e2b5
VZ
438}
439
06efac1f 440// ----------------------------------------------------------------------------
ef1717a9 441// wxDisplayImplWin32Base
06efac1f
VZ
442// ----------------------------------------------------------------------------
443
ef1717a9 444wxRect wxDisplayImplWin32Base::GetGeometry() const
8585e2b5 445{
ef1717a9
VZ
446 if ( m_info.m_rect.IsEmpty() )
447 m_info.Initialize();
06efac1f 448
ef1717a9
VZ
449 return m_info.m_rect;
450}
06efac1f 451
6c5d6291
VZ
452wxRect wxDisplayImplWin32Base::GetClientArea() const
453{
454 if ( m_info.m_rectClient.IsEmpty() )
455 m_info.Initialize();
456
457 return m_info.m_rectClient;
458}
459
ef1717a9
VZ
460wxString wxDisplayImplWin32Base::GetName() const
461{
57bd4c60 462 if ( m_info.m_devName.empty() )
ef1717a9 463 m_info.Initialize();
06efac1f 464
ef1717a9
VZ
465 return m_info.m_devName;
466}
06efac1f 467
ef1717a9
VZ
468bool wxDisplayImplWin32Base::IsPrimary() const
469{
470 if ( m_info.m_flags == (DWORD)-1 )
471 m_info.Initialize();
06efac1f 472
ef1717a9 473 return (m_info.m_flags & MONITORINFOF_PRIMARY) != 0;
06efac1f
VZ
474}
475
ef1717a9 476wxVideoMode wxDisplayImplWin32Base::GetCurrentMode() const
06efac1f 477{
ef1717a9 478 wxVideoMode mode;
01c54165 479
ef1717a9
VZ
480 // The first parameter of EnumDisplaySettings() must be NULL under Win95
481 // according to MSDN. The version of GetName() we implement for Win95
482 // returns an empty string.
483 const wxString name = GetName();
484 const wxChar * const deviceName = name.empty() ? NULL : name.c_str();
06efac1f 485
ef1717a9
VZ
486 DEVMODE dm;
487 dm.dmSize = sizeof(dm);
488 dm.dmDriverExtra = 0;
489 if ( !::EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, &dm) )
06efac1f 490 {
ef1717a9 491 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
06efac1f 492 }
ef1717a9
VZ
493 else
494 {
495 mode = ConvertToVideoMode(dm);
496 }
497
498 return mode;
06efac1f
VZ
499}
500
501// ----------------------------------------------------------------------------
ef1717a9 502// wxDisplayFactoryWin32Base
06efac1f
VZ
503// ----------------------------------------------------------------------------
504
ef1717a9 505int wxDisplayFactoryWin32Base::ms_supportsMultimon = -1;
8585e2b5 506
ef1717a9 507wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
8585e2b5 508{
ef1717a9 509 if ( ms_supportsMultimon == -1 )
01c54165 510 {
ef1717a9 511 ms_supportsMultimon = 0;
01c54165 512
ef1717a9 513 wxDynamicLibrary dllUser32(_T("user32.dll"));
06efac1f 514
ef1717a9
VZ
515 wxLogNull noLog;
516
517 gs_MonitorFromPoint = (MonitorFromPoint_t)
518 dllUser32.GetSymbol(wxT("MonitorFromPoint"));
519 if ( !gs_MonitorFromPoint )
520 return;
521
522 gs_MonitorFromWindow = (MonitorFromWindow_t)
523 dllUser32.GetSymbol(wxT("MonitorFromWindow"));
524 if ( !gs_MonitorFromWindow )
525 return;
526
527 gs_GetMonitorInfo = (GetMonitorInfo_t)
528 dllUser32.GetSymbolAorW(wxT("GetMonitorInfo"));
529 if ( !gs_GetMonitorInfo )
530 return;
531
532 ms_supportsMultimon = 1;
533
534 // we can safely let dllUser32 go out of scope, the DLL itself will
535 // still remain loaded as all Win32 programs use it
06efac1f 536 }
ef1717a9 537}
06efac1f 538
ef1717a9
VZ
539void wxDisplayFactoryWin32Base::Clear()
540{
541 WX_CLEAR_ARRAY(m_displays);
8585e2b5
VZ
542}
543
ef1717a9 544wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
8585e2b5 545{
ef1717a9
VZ
546 Clear();
547}
01c54165 548
ef1717a9
VZ
549// helper for GetFromPoint() and GetFromWindow()
550int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon) const
551{
552 if ( hmon )
a536e411 553 {
ef1717a9
VZ
554 const size_t count = m_displays.size();
555 for ( size_t n = 0; n < count; n++ )
8585e2b5 556 {
ef1717a9
VZ
557 if ( hmon == m_displays[n]->m_hmon )
558 return n;
8585e2b5 559 }
a536e411
JS
560 }
561
ef1717a9 562 return wxNOT_FOUND;
a536e411
JS
563}
564
ef1717a9
VZ
565int wxDisplayFactoryWin32Base::GetFromPoint(const wxPoint& pt)
566{
567 POINT pt2;
568 pt2.x = pt.x;
569 pt2.y = pt.y;
570
571 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2,
572 MONITOR_DEFAULTTONULL));
573}
574
575int wxDisplayFactoryWin32Base::GetFromWindow(wxWindow *window)
576{
577 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window),
578 MONITOR_DEFAULTTONULL));
579}
580
581// ============================================================================
582// wxDisplay implementation using Win32 multimon API
583// ============================================================================
584
24d2b4f5 585// ----------------------------------------------------------------------------
ef1717a9 586// wxDisplayFactoryMultimon initialization
24d2b4f5
RN
587// ----------------------------------------------------------------------------
588
ef1717a9 589wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
24d2b4f5 590{
ef1717a9
VZ
591 if ( !ms_supportsMultimon )
592 return;
01c54165 593
ef1717a9
VZ
594 // look up EnumDisplayMonitors() which we don't need with DirectDraw
595 // implementation
596 EnumDisplayMonitors_t pfnEnumDisplayMonitors;
597 {
598 wxLogNull noLog;
24d2b4f5 599
ef1717a9
VZ
600 wxDynamicLibrary dllUser32(_T("user32.dll"));
601 pfnEnumDisplayMonitors = (EnumDisplayMonitors_t)
602 dllUser32.GetSymbol(wxT("EnumDisplayMonitors"));
603 if ( !pfnEnumDisplayMonitors )
604 return;
605 }
24d2b4f5 606
ef1717a9
VZ
607 // enumerate all displays
608 if ( !pfnEnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) )
24d2b4f5 609 {
ef1717a9 610 wxLogLastError(wxT("EnumDisplayMonitors"));
24d2b4f5 611 }
ef1717a9
VZ
612}
613
614/* static */
615BOOL CALLBACK
616wxDisplayFactoryMultimon::MultimonEnumProc(
617 HMONITOR hMonitor, // handle to display monitor
618 HDC WXUNUSED(hdcMonitor), // handle to monitor-appropriate device context
619 LPRECT lprcMonitor, // pointer to monitor intersection rectangle
620 LPARAM dwData // data passed from EnumDisplayMonitors (this)
621)
622{
623 wxDisplayFactoryMultimon *const self = (wxDisplayFactoryMultimon *)dwData;
624 self->AddDisplay(hMonitor, lprcMonitor);
24d2b4f5 625
ef1717a9
VZ
626 // continue the enumeration
627 return TRUE;
24d2b4f5
RN
628}
629
06efac1f 630// ----------------------------------------------------------------------------
ef1717a9 631// wxDisplayFactoryMultimon helper functions
06efac1f
VZ
632// ----------------------------------------------------------------------------
633
ef1717a9 634void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor)
06efac1f 635{
ef1717a9 636 wxDisplayInfo *info = new wxDisplayInfo(hMonitor);
06efac1f 637
ef1717a9
VZ
638 // we also store the display geometry
639 info->m_rect = wxRect(lprcMonitor->left, lprcMonitor->top,
640 lprcMonitor->right - lprcMonitor->left,
641 lprcMonitor->bottom - lprcMonitor->top);
06efac1f 642
ef1717a9
VZ
643 // now add this monitor to the array
644 m_displays.Add(info);
645}
06efac1f 646
ef1717a9
VZ
647// ----------------------------------------------------------------------------
648// wxDisplayFactoryMultimon inherited pure virtuals implementation
649// ----------------------------------------------------------------------------
06efac1f 650
4e675101 651wxDisplayImpl *wxDisplayFactoryMultimon::CreateDisplay(unsigned n)
ef1717a9
VZ
652{
653 wxCHECK_MSG( n < m_displays.size(), NULL, _T("invalid display index") );
654
655 return new wxDisplayImplMultimon(n, *(m_displays[n]));
06efac1f 656}
ef1717a9
VZ
657
658// ----------------------------------------------------------------------------
659// wxDisplayImplMultimon implementation
660// ----------------------------------------------------------------------------
06efac1f
VZ
661
662wxArrayVideoModes
ef1717a9 663wxDisplayImplMultimon::GetModes(const wxVideoMode& modeMatch) const
a536e411 664{
8585e2b5
VZ
665 wxArrayVideoModes modes;
666
01c54165
VZ
667 // The first parameter of EnumDisplaySettings() must be NULL under Win95
668 // according to MSDN. The version of GetName() we implement for Win95
669 // returns an empty string.
670 const wxString name = GetName();
8585e2b5
VZ
671 const wxChar * const deviceName = name.empty() ? NULL : name.c_str();
672
673 DEVMODE dm;
01c54165
VZ
674 dm.dmSize = sizeof(dm);
675 dm.dmDriverExtra = 0;
8585e2b5
VZ
676 for ( int iModeNum = 0;
677 ::EnumDisplaySettings(deviceName, iModeNum, &dm);
678 iModeNum++ )
679 {
680 const wxVideoMode mode = ConvertToVideoMode(dm);
681 if ( mode.Matches(modeMatch) )
682 {
683 modes.Add(mode);
684 }
685 }
686
687 return modes;
a536e411
JS
688}
689
ef1717a9 690bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode& mode)
a536e411 691{
8585e2b5 692 // prepare ChangeDisplaySettingsEx() parameters
61373fa8
WS
693 DEVMODE dm;
694 DEVMODE *pDevMode;
695
8585e2b5
VZ
696 int flags;
697
698 if ( mode == wxDefaultVideoMode )
699 {
700 // reset the video mode to default
701 pDevMode = NULL;
702 flags = 0;
703 }
704 else // change to the given mode
705 {
06efac1f 706 wxCHECK_MSG( mode.w && mode.h, false,
8585e2b5
VZ
707 _T("at least the width and height must be specified") );
708
709 wxZeroMemory(dm);
710 dm.dmSize = sizeof(dm);
01c54165 711 dm.dmDriverExtra = 0;
8585e2b5
VZ
712 dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
713 dm.dmPelsWidth = mode.w;
714 dm.dmPelsHeight = mode.h;
715
716 if ( mode.bpp )
717 {
718 dm.dmFields |= DM_BITSPERPEL;
719 dm.dmBitsPerPel = mode.bpp;
720 }
721
722 if ( mode.refresh )
723 {
724 dm.dmFields |= DM_DISPLAYFREQUENCY;
725 dm.dmDisplayFrequency = mode.refresh;
726 }
727
728 pDevMode = &dm;
729
2ad495fb
WS
730#ifdef __WXWINCE__
731 flags = 0;
732#else // !__WXWINCE__
8585e2b5 733 flags = CDS_FULLSCREEN;
2ad495fb 734#endif // __WXWINCE__/!__WXWINCE__
8585e2b5
VZ
735 }
736
737
738 // get pointer to the function dynamically
739 //
740 // we're only called from the main thread, so it's ok to use static
741 // variable
742 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx = NULL;
743 if ( !pfnChangeDisplaySettingsEx )
744 {
745 wxDynamicLibrary dllUser32(_T("user32.dll"));
746 if ( dllUser32.IsLoaded() )
747 {
748 pfnChangeDisplaySettingsEx = (ChangeDisplaySettingsEx_t)
fd921f35 749 dllUser32.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
8585e2b5
VZ
750 }
751 //else: huh, no user32.dll??
752
2ad495fb 753#ifndef __WXWINCE__
8585e2b5
VZ
754 if ( !pfnChangeDisplaySettingsEx )
755 {
756 // we must be under Win95 and so there is no multiple monitors
757 // support anyhow
758 pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95;
759 }
2ad495fb 760#endif // !__WXWINCE__
8585e2b5
VZ
761 }
762
763 // do change the mode
764 switch ( pfnChangeDisplaySettingsEx
765 (
766 GetName(), // display name
767 pDevMode, // dev mode or NULL to reset
768 NULL, // reserved
769 flags,
770 NULL // pointer to video parameters (not used)
771 ) )
772 {
773 case DISP_CHANGE_SUCCESSFUL:
774 // ok
920b9675
JS
775 {
776 // If we have a top-level, full-screen frame, emulate
777 // the DirectX behavior and resize it. This makes this
778 // API quite a bit easier to use.
779 wxWindow *winTop = wxTheApp->GetTopWindow();
780 wxFrame *frameTop = wxDynamicCast(winTop, wxFrame);
781 if (frameTop && frameTop->IsFullScreen())
782 {
783 wxVideoMode current = GetCurrentMode();
784 frameTop->SetClientSize(current.w, current.h);
785 }
786 }
06efac1f 787 return true;
8585e2b5
VZ
788
789 case DISP_CHANGE_BADMODE:
790 // don't complain about this, this is the only "expected" error
791 break;
792
793 default:
794 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
795 }
796
06efac1f
VZ
797 return false;
798}
799
ef1717a9
VZ
800
801// ============================================================================
802// DirectDraw-based wxDisplay implementation
803// ============================================================================
804
805#if wxUSE_DIRECTDRAW
806
807// ----------------------------------------------------------------------------
808// wxDisplayFactoryDirectDraw initialization
809// ----------------------------------------------------------------------------
810
811wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
06efac1f 812{
ef1717a9
VZ
813 if ( !ms_supportsMultimon )
814 return;
815
816#if wxUSE_LOG
817 // suppress the errors if ddraw.dll is not found, we're prepared to handle
818 // this
819 wxLogNull noLog;
01c54165 820#endif
ef1717a9
VZ
821
822 m_dllDDraw.Load(_T("ddraw.dll"));
823
824 if ( !m_dllDDraw.IsLoaded() )
825 return;
826
827 DirectDrawEnumerateEx_t pDDEnumEx = (DirectDrawEnumerateEx_t)
828 m_dllDDraw.GetSymbolAorW(_T("DirectDrawEnumerateEx"));
829 if ( !pDDEnumEx )
830 return;
831
832 // we can't continue without DirectDrawCreate() later, so resolve it right
833 // now and fail the initialization if it's not available
834 m_pfnDirectDrawCreate = (DirectDrawCreate_t)
835 m_dllDDraw.GetSymbol(_T("DirectDrawCreate"));
836 if ( !m_pfnDirectDrawCreate )
837 return;
838
839 if ( (*pDDEnumEx)(DDEnumExCallback,
840 this,
841 DDENUM_ATTACHEDSECONDARYDEVICES) != DD_OK )
842 {
843 wxLogLastError(_T("DirectDrawEnumerateEx"));
844 }
845}
846
847wxDisplayFactoryDirectDraw::~wxDisplayFactoryDirectDraw()
848{
849 // we must clear m_displays now, before m_dllDDraw is unloaded as otherwise
850 // calling m_pDD2->Release() later would crash
851 Clear();
852}
853
854// ----------------------------------------------------------------------------
855// callbacks for monitor/modes enumeration stuff
856// ----------------------------------------------------------------------------
857
858BOOL WINAPI
859wxDisplayFactoryDirectDraw::DDEnumExCallback(GUID *pGuid,
860 LPTSTR WXUNUSED(driverDescription),
861 LPTSTR driverName,
862 LPVOID lpContext,
863 HMONITOR hmon)
864{
865 if ( pGuid )
866 {
867 wxDisplayFactoryDirectDraw * self =
868 wx_static_cast(wxDisplayFactoryDirectDraw *, lpContext);
869 self->AddDisplay(*pGuid, hmon, driverName);
870 }
871 //else: we're called for the primary monitor, skip it
872
873 // continue the enumeration
874 return TRUE;
875}
876
877// ----------------------------------------------------------------------------
878// wxDisplayFactoryDirectDraw helpers
879// ----------------------------------------------------------------------------
880
881void wxDisplayFactoryDirectDraw::AddDisplay(const GUID& guid,
882 HMONITOR hmon,
883 LPTSTR name)
884{
885 m_displays.Add(new wxDisplayInfoDirectDraw(guid, hmon, name));
886}
887
888// ----------------------------------------------------------------------------
889// wxDisplayFactoryDirectDraw inherited pure virtuals implementation
890// ----------------------------------------------------------------------------
891
4e675101 892wxDisplayImpl *wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n)
ef1717a9
VZ
893{
894 wxCHECK_MSG( n < m_displays.size(), NULL, _T("invalid display index") );
895
896 wxDisplayInfoDirectDraw *
897 info = wx_static_cast(wxDisplayInfoDirectDraw *, m_displays[n]);
898
899 if ( !info->m_pDD2 )
900 {
901 IDirectDraw *pDD;
902 GUID guid(info->m_guid);
903 HRESULT hr = (*m_pfnDirectDrawCreate)(&guid, &pDD, NULL);
904
905 if ( FAILED(hr) || !pDD )
906 {
907 // what to do??
908 wxLogApiError(_T("DirectDrawCreate"), hr);
909 return NULL;
910 }
911
912 // we got IDirectDraw, but we need IDirectDraw2
913 hr = pDD->QueryInterface(wxIID_IDirectDraw2, (void **)&info->m_pDD2);
914 pDD->Release();
915
916 if ( FAILED(hr) || !info->m_pDD2 )
917 {
918 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr);
919 return NULL;
920 }
921
922 // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
923 // which is ok as we don't want to recreate DD objects all the time
924 }
925 //else: DirectDraw object corresponding to our display already exists
926
927 return new wxDisplayImplDirectDraw(n, *info, info->m_pDD2);
928}
929
930// ============================================================================
931// wxDisplayImplDirectDraw
932// ============================================================================
933
934// ----------------------------------------------------------------------------
935// video modes enumeration
936// ----------------------------------------------------------------------------
937
938// tiny helper class used to pass information from GetModes() to
939// wxDDEnumModesCallback
940class wxDDVideoModesAdder
941{
942public:
943 // our Add() method will add modes matching modeMatch to modes array
944 wxDDVideoModesAdder(wxArrayVideoModes& modes, const wxVideoMode& modeMatch)
945 : m_modes(modes),
946 m_modeMatch(modeMatch)
947 {
948 }
949
950 void Add(const wxVideoMode& mode)
951 {
952 if ( mode.Matches(m_modeMatch) )
953 m_modes.Add(mode);
954 }
955
956private:
957 wxArrayVideoModes& m_modes;
958 const wxVideoMode& m_modeMatch;
959
960 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder)
961};
962
963HRESULT WINAPI wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc,
964 LPVOID lpContext)
965{
966 // we need at least the mode size
967 static const DWORD FLAGS_REQUIRED = DDSD_HEIGHT | DDSD_WIDTH;
968 if ( (lpDDSurfaceDesc->dwFlags & FLAGS_REQUIRED) == FLAGS_REQUIRED )
969 {
970 wxDDVideoModesAdder * const vmodes =
971 wx_static_cast(wxDDVideoModesAdder *, lpContext);
972
973 vmodes->Add(wxVideoMode(lpDDSurfaceDesc->dwWidth,
974 lpDDSurfaceDesc->dwHeight,
975 lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount,
976 lpDDSurfaceDesc->dwRefreshRate));
977 }
978
979 // continue the enumeration
980 return DDENUMRET_OK;
981}
982
983wxArrayVideoModes
984wxDisplayImplDirectDraw::GetModes(const wxVideoMode& modeMatch) const
985{
986 wxArrayVideoModes modes;
987 wxDDVideoModesAdder modesAdder(modes, modeMatch);
988
989 HRESULT hr = m_pDD2->EnumDisplayModes
990 (
991 DDEDM_REFRESHRATES,
992 NULL, // all modes
993 &modesAdder, // callback parameter
994 wxDDEnumModesCallback
995 );
996
997 if ( FAILED(hr) )
998 {
999 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr);
1000 }
1001
1002 return modes;
a536e411
JS
1003}
1004
ef1717a9
VZ
1005// ----------------------------------------------------------------------------
1006// video mode switching
1007// ----------------------------------------------------------------------------
1008
1009bool wxDisplayImplDirectDraw::ChangeMode(const wxVideoMode& mode)
1010{
1011 wxWindow *winTop = wxTheApp->GetTopWindow();
1012 wxCHECK_MSG( winTop, false, _T("top level window required for DirectX") );
1013
1014 HRESULT hr = m_pDD2->SetCooperativeLevel
1015 (
1016 GetHwndOf(winTop),
1017 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN
1018 );
1019 if ( FAILED(hr) )
1020 {
1021 wxLogApiError(_T("IDirectDraw2::SetCooperativeLevel"), hr);
1022
1023 return false;
1024 }
1025
1026 hr = m_pDD2->SetDisplayMode(mode.w, mode.h, mode.bpp, mode.refresh, 0);
1027 if ( FAILED(hr) )
1028 {
1029 wxLogApiError(_T("IDirectDraw2::SetDisplayMode"), hr);
1030
1031 return false;
1032 }
1033
1034 return true;
1035}
1036
1037#endif // wxUSE_DIRECTDRAW
1038
8585e2b5 1039#endif // wxUSE_DISPLAY