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