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