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