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