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