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