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