]> git.saurik.com Git - wxWidgets.git/blob - src/msw/display.cpp
fix unused variable warning when wxUSE_PALETTE==0
[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/msw/missing.h"
32 #include "wx/dynarray.h"
33 #include "wx/app.h"
34 #include "wx/frame.h"
35 #endif
36
37 #include "wx/dynload.h"
38 #include "wx/sysopt.h"
39
40 #include "wx/display.h"
41 #include "wx/display_impl.h"
42
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
46
47 #if !defined(wxUSE_DIRECTDRAW)
48 #define wxUSE_DIRECTDRAW 0
49 #endif
50
51 #ifndef __WXWINCE__
52 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
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);
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
73 #define MONITORINFOF_PRIMARY 0x00000001
74 #define HMONITOR_DECLARED
75 #endif
76 #endif // !__WXWINCE__
77
78 #if wxUSE_DIRECTDRAW
79 #include <ddraw.h>
80
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 } };
85 #endif // wxUSE_DIRECTDRAW
86
87 // ----------------------------------------------------------------------------
88 // typedefs for dynamically loaded Windows functions
89 // ----------------------------------------------------------------------------
90
91 typedef LONG (WINAPI *ChangeDisplaySettingsEx_t)(LPCTSTR lpszDeviceName,
92 LPDEVMODE lpDevMode,
93 HWND hwnd,
94 DWORD dwFlags,
95 LPVOID lParam);
96
97 #if wxUSE_DIRECTDRAW
98 typedef BOOL (PASCAL *DDEnumExCallback_t)(GUID *pGuid,
99 LPTSTR driverDescription,
100 LPTSTR driverName,
101 LPVOID lpContext,
102 HMONITOR hmon);
103
104 typedef HRESULT (WINAPI *DirectDrawEnumerateEx_t)(DDEnumExCallback_t lpCallback,
105 LPVOID lpContext,
106 DWORD dwFlags);
107
108 typedef HRESULT (WINAPI *DirectDrawCreate_t)(GUID *lpGUID,
109 LPDIRECTDRAW *lplpDD,
110 IUnknown *pUnkOuter);
111 #endif // wxUSE_DIRECTDRAW
112
113 typedef BOOL (WINAPI *EnumDisplayMonitors_t)(HDC,LPCRECT,MONITORENUMPROC,LPARAM);
114 typedef HMONITOR (WINAPI *MonitorFromPoint_t)(POINT,DWORD);
115 typedef HMONITOR (WINAPI *MonitorFromWindow_t)(HWND,DWORD);
116 typedef BOOL (WINAPI *GetMonitorInfo_t)(HMONITOR,LPMONITORINFO);
117
118 #ifndef __WXWINCE__
119 // emulation of ChangeDisplaySettingsEx() for Win95
120 LONG 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__
129
130 // ----------------------------------------------------------------------------
131 // display information classes
132 // ----------------------------------------------------------------------------
133
134 struct wxDisplayInfo
135 {
136 wxDisplayInfo(HMONITOR hmon = NULL)
137 {
138 m_hmon = hmon;
139 m_flags = (DWORD)-1;
140 }
141
142 virtual ~wxDisplayInfo() { }
143
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;
152
153 // the entire area of this monitor in virtual screen coordinates
154 wxRect m_rect;
155
156 // the work or client area, i.e. the area available for the normal windows
157 wxRect m_rectClient;
158
159 // the display device name for this monitor, empty initially and retrieved
160 // on demand by DoGetName()
161 wxString m_devName;
162
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;
166 };
167
168 WX_DEFINE_ARRAY_PTR(wxDisplayInfo *, wxDisplayInfoArray);
169
170 // ----------------------------------------------------------------------------
171 // common base class for all Win32 wxDisplayImpl versions
172 // ----------------------------------------------------------------------------
173
174 class wxDisplayImplWin32Base : public wxDisplayImpl
175 {
176 public:
177 wxDisplayImplWin32Base(size_t n, wxDisplayInfo& info)
178 : wxDisplayImpl(n),
179 m_info(info)
180 {
181 }
182
183 virtual wxRect GetGeometry() const;
184 virtual wxRect GetClientArea() const;
185 virtual wxString GetName() const;
186 virtual bool IsPrimary() const;
187
188 virtual wxVideoMode GetCurrentMode() const;
189
190 protected:
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 };
205
206 // ----------------------------------------------------------------------------
207 // common base class for all Win32 wxDisplayFactory versions
208 // ----------------------------------------------------------------------------
209
210 // functions dynamically bound by wxDisplayFactoryWin32Base::Initialize()
211 static MonitorFromPoint_t gs_MonitorFromPoint = NULL;
212 static MonitorFromWindow_t gs_MonitorFromWindow = NULL;
213 static GetMonitorInfo_t gs_GetMonitorInfo = NULL;
214
215 class wxDisplayFactoryWin32Base : public wxDisplayFactory
216 {
217 public:
218 virtual ~wxDisplayFactoryWin32Base();
219
220 bool IsOk() const { return !m_displays.empty(); }
221
222 virtual size_t GetCount() { return m_displays.size(); }
223 virtual int GetFromPoint(const wxPoint& pt);
224 virtual int GetFromWindow(wxWindow *window);
225
226 protected:
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();
231
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();
236
237 // find the monitor corresponding to the given handle, return wxNOT_FOUND
238 // if not found
239 int FindDisplayFromHMONITOR(HMONITOR hmon) const;
240
241
242 // flag indicating whether gs_MonitorXXX functions are available
243 static int ms_supportsMultimon;
244
245 // the array containing information about all available displays, should be
246 // filled by the derived class ctors
247 wxDisplayInfoArray m_displays;
248
249
250 DECLARE_NO_COPY_CLASS(wxDisplayFactoryWin32Base)
251 };
252
253 // ----------------------------------------------------------------------------
254 // wxDisplay implementation using Windows multi-monitor support functions
255 // ----------------------------------------------------------------------------
256
257 class wxDisplayImplMultimon : public wxDisplayImplWin32Base
258 {
259 public:
260 wxDisplayImplMultimon(size_t n, wxDisplayInfo& info)
261 : wxDisplayImplWin32Base(n, info)
262 {
263 }
264
265 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
266 virtual bool ChangeMode(const wxVideoMode& mode);
267
268 private:
269 DECLARE_NO_COPY_CLASS(wxDisplayImplMultimon)
270 };
271
272 class wxDisplayFactoryMultimon : public wxDisplayFactoryWin32Base
273 {
274 public:
275 wxDisplayFactoryMultimon();
276
277 virtual wxDisplayImpl *CreateDisplay(size_t n);
278
279 private:
280 // EnumDisplayMonitors() callback
281 static BOOL CALLBACK MultimonEnumProc(HMONITOR hMonitor,
282 HDC hdcMonitor,
283 LPRECT lprcMonitor,
284 LPARAM dwData);
285
286
287 // add a monitor description to m_displays array
288 void AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor);
289 };
290
291 // ----------------------------------------------------------------------------
292 // wxDisplay implementation using DirectDraw
293 // ----------------------------------------------------------------------------
294
295 #if wxUSE_DIRECTDRAW
296
297 struct wxDisplayInfoDirectDraw : wxDisplayInfo
298 {
299 wxDisplayInfoDirectDraw(const GUID& guid, HMONITOR hmon, LPTSTR name)
300 : wxDisplayInfo(hmon),
301 m_guid(guid)
302 {
303 m_pDD2 = NULL;
304 m_devName = name;
305 }
306
307 virtual ~wxDisplayInfoDirectDraw()
308 {
309 if ( m_pDD2 )
310 m_pDD2->Release();
311 }
312
313
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 };
323
324 class wxDisplayImplDirectDraw : public wxDisplayImplWin32Base
325 {
326 public:
327 wxDisplayImplDirectDraw(size_t n, wxDisplayInfo& info, IDirectDraw2 *pDD2)
328 : wxDisplayImplWin32Base(n, info),
329 m_pDD2(pDD2)
330 {
331 m_pDD2->AddRef();
332 }
333
334 virtual ~wxDisplayImplDirectDraw()
335 {
336 m_pDD2->Release();
337 }
338
339 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
340 virtual bool ChangeMode(const wxVideoMode& mode);
341
342 private:
343 IDirectDraw2 *m_pDD2;
344
345 DECLARE_NO_COPY_CLASS(wxDisplayImplDirectDraw)
346 };
347
348 class wxDisplayFactoryDirectDraw : public wxDisplayFactoryWin32Base
349 {
350 public:
351 wxDisplayFactoryDirectDraw();
352 virtual ~wxDisplayFactoryDirectDraw();
353
354 virtual wxDisplayImpl *CreateDisplay(size_t n);
355
356 private:
357 // callback used with DirectDrawEnumerateEx()
358 static BOOL WINAPI DDEnumExCallback(GUID *pGuid,
359 LPTSTR driverDescription,
360 LPTSTR driverName,
361 LPVOID lpContext,
362 HMONITOR hmon);
363
364 // add a monitor description to m_displays array
365 void AddDisplay(const GUID& guid, HMONITOR hmon, LPTSTR name);
366
367
368 // ddraw.dll
369 wxDynamicLibrary m_dllDDraw;
370
371 // dynamically resolved DirectDrawCreate()
372 DirectDrawCreate_t m_pfnDirectDrawCreate;
373
374 DECLARE_NO_COPY_CLASS(wxDisplayFactoryDirectDraw)
375 };
376
377 #endif // wxUSE_DIRECTDRAW
378
379
380 // ============================================================================
381 // common classes implementation
382 // ============================================================================
383
384 // ----------------------------------------------------------------------------
385 // wxDisplay
386 // ----------------------------------------------------------------------------
387
388 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
389 {
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
393
394 #if wxUSE_DIRECTDRAW
395 if ( wxSystemOptions::GetOptionInt(_T("msw.display.directdraw")) )
396 {
397 wxDisplayFactoryDirectDraw *factoryDD = new wxDisplayFactoryDirectDraw;
398 if ( factoryDD->IsOk() )
399 return factoryDD;
400
401 delete factoryDD;
402 }
403 #endif // wxUSE_DIRECTDRAW
404
405 wxDisplayFactoryMultimon *factoryMM = new wxDisplayFactoryMultimon;
406 if ( factoryMM->IsOk() )
407 return factoryMM;
408
409 delete factoryMM;
410
411
412 // finally fall back to a stub implementation if all else failed (Win95?)
413 return new wxDisplayFactorySingle;
414 }
415
416 // ----------------------------------------------------------------------------
417 // wxDisplayInfo
418 // ----------------------------------------------------------------------------
419
420 void wxDisplayInfo::Initialize()
421 {
422 if ( m_flags == (DWORD)-1 )
423 {
424 WinStruct<MONITORINFOEX> monInfo;
425 if ( !gs_GetMonitorInfo(m_hmon, (LPMONITORINFO)&monInfo) )
426 {
427 wxLogLastError(_T("GetMonitorInfo"));
428 m_flags = 0;
429 return;
430 }
431
432 wxCopyRECTToRect(monInfo.rcMonitor, m_rect);
433 wxCopyRECTToRect(monInfo.rcWork, m_rectClient);
434 m_devName = monInfo.szDevice;
435 m_flags = monInfo.dwFlags;
436 }
437 }
438
439 // ----------------------------------------------------------------------------
440 // wxDisplayImplWin32Base
441 // ----------------------------------------------------------------------------
442
443 wxRect wxDisplayImplWin32Base::GetGeometry() const
444 {
445 if ( m_info.m_rect.IsEmpty() )
446 m_info.Initialize();
447
448 return m_info.m_rect;
449 }
450
451 wxRect wxDisplayImplWin32Base::GetClientArea() const
452 {
453 if ( m_info.m_rectClient.IsEmpty() )
454 m_info.Initialize();
455
456 return m_info.m_rectClient;
457 }
458
459 wxString wxDisplayImplWin32Base::GetName() const
460 {
461 if ( m_info.m_devName.empty() )
462 m_info.Initialize();
463
464 return m_info.m_devName;
465 }
466
467 bool wxDisplayImplWin32Base::IsPrimary() const
468 {
469 if ( m_info.m_flags == (DWORD)-1 )
470 m_info.Initialize();
471
472 return (m_info.m_flags & MONITORINFOF_PRIMARY) != 0;
473 }
474
475 wxVideoMode wxDisplayImplWin32Base::GetCurrentMode() const
476 {
477 wxVideoMode mode;
478
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();
484
485 DEVMODE dm;
486 dm.dmSize = sizeof(dm);
487 dm.dmDriverExtra = 0;
488 if ( !::EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, &dm) )
489 {
490 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
491 }
492 else
493 {
494 mode = ConvertToVideoMode(dm);
495 }
496
497 return mode;
498 }
499
500 // ----------------------------------------------------------------------------
501 // wxDisplayFactoryWin32Base
502 // ----------------------------------------------------------------------------
503
504 int wxDisplayFactoryWin32Base::ms_supportsMultimon = -1;
505
506 wxDisplayFactoryWin32Base::wxDisplayFactoryWin32Base()
507 {
508 if ( ms_supportsMultimon == -1 )
509 {
510 ms_supportsMultimon = 0;
511
512 wxDynamicLibrary dllUser32(_T("user32.dll"));
513
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
535 }
536 }
537
538 void wxDisplayFactoryWin32Base::Clear()
539 {
540 WX_CLEAR_ARRAY(m_displays);
541 }
542
543 wxDisplayFactoryWin32Base::~wxDisplayFactoryWin32Base()
544 {
545 Clear();
546 }
547
548 // helper for GetFromPoint() and GetFromWindow()
549 int wxDisplayFactoryWin32Base::FindDisplayFromHMONITOR(HMONITOR hmon) const
550 {
551 if ( hmon )
552 {
553 const size_t count = m_displays.size();
554 for ( size_t n = 0; n < count; n++ )
555 {
556 if ( hmon == m_displays[n]->m_hmon )
557 return n;
558 }
559 }
560
561 return wxNOT_FOUND;
562 }
563
564 int 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
574 int 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
584 // ----------------------------------------------------------------------------
585 // wxDisplayFactoryMultimon initialization
586 // ----------------------------------------------------------------------------
587
588 wxDisplayFactoryMultimon::wxDisplayFactoryMultimon()
589 {
590 if ( !ms_supportsMultimon )
591 return;
592
593 // look up EnumDisplayMonitors() which we don't need with DirectDraw
594 // implementation
595 EnumDisplayMonitors_t pfnEnumDisplayMonitors;
596 {
597 wxLogNull noLog;
598
599 wxDynamicLibrary dllUser32(_T("user32.dll"));
600 pfnEnumDisplayMonitors = (EnumDisplayMonitors_t)
601 dllUser32.GetSymbol(wxT("EnumDisplayMonitors"));
602 if ( !pfnEnumDisplayMonitors )
603 return;
604 }
605
606 // enumerate all displays
607 if ( !pfnEnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) )
608 {
609 wxLogLastError(wxT("EnumDisplayMonitors"));
610 }
611 }
612
613 /* static */
614 BOOL CALLBACK
615 wxDisplayFactoryMultimon::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);
624
625 // continue the enumeration
626 return TRUE;
627 }
628
629 // ----------------------------------------------------------------------------
630 // wxDisplayFactoryMultimon helper functions
631 // ----------------------------------------------------------------------------
632
633 void wxDisplayFactoryMultimon::AddDisplay(HMONITOR hMonitor, LPRECT lprcMonitor)
634 {
635 wxDisplayInfo *info = new wxDisplayInfo(hMonitor);
636
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);
641
642 // now add this monitor to the array
643 m_displays.Add(info);
644 }
645
646 // ----------------------------------------------------------------------------
647 // wxDisplayFactoryMultimon inherited pure virtuals implementation
648 // ----------------------------------------------------------------------------
649
650 wxDisplayImpl *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]));
655 }
656
657 // ----------------------------------------------------------------------------
658 // wxDisplayImplMultimon implementation
659 // ----------------------------------------------------------------------------
660
661 wxArrayVideoModes
662 wxDisplayImplMultimon::GetModes(const wxVideoMode& modeMatch) const
663 {
664 wxArrayVideoModes modes;
665
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();
670 const wxChar * const deviceName = name.empty() ? NULL : name.c_str();
671
672 DEVMODE dm;
673 dm.dmSize = sizeof(dm);
674 dm.dmDriverExtra = 0;
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;
687 }
688
689 bool wxDisplayImplMultimon::ChangeMode(const wxVideoMode& mode)
690 {
691 // prepare ChangeDisplaySettingsEx() parameters
692 DEVMODE dm;
693 DEVMODE *pDevMode;
694
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 {
705 wxCHECK_MSG( mode.w && mode.h, false,
706 _T("at least the width and height must be specified") );
707
708 wxZeroMemory(dm);
709 dm.dmSize = sizeof(dm);
710 dm.dmDriverExtra = 0;
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
729 #ifdef __WXWINCE__
730 flags = 0;
731 #else // !__WXWINCE__
732 flags = CDS_FULLSCREEN;
733 #endif // __WXWINCE__/!__WXWINCE__
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)
748 dllUser32.GetSymbolAorW(_T("ChangeDisplaySettingsEx"));
749 }
750 //else: huh, no user32.dll??
751
752 #ifndef __WXWINCE__
753 if ( !pfnChangeDisplaySettingsEx )
754 {
755 // we must be under Win95 and so there is no multiple monitors
756 // support anyhow
757 pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95;
758 }
759 #endif // !__WXWINCE__
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
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 }
786 return true;
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
796 return false;
797 }
798
799
800 // ============================================================================
801 // DirectDraw-based wxDisplay implementation
802 // ============================================================================
803
804 #if wxUSE_DIRECTDRAW
805
806 // ----------------------------------------------------------------------------
807 // wxDisplayFactoryDirectDraw initialization
808 // ----------------------------------------------------------------------------
809
810 wxDisplayFactoryDirectDraw::wxDisplayFactoryDirectDraw()
811 {
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;
819 #endif
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
846 wxDisplayFactoryDirectDraw::~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
857 BOOL WINAPI
858 wxDisplayFactoryDirectDraw::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
880 void 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
891 wxDisplayImpl *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
939 class wxDDVideoModesAdder
940 {
941 public:
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
955 private:
956 wxArrayVideoModes& m_modes;
957 const wxVideoMode& m_modeMatch;
958
959 DECLARE_NO_COPY_CLASS(wxDDVideoModesAdder)
960 };
961
962 HRESULT 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
982 wxArrayVideoModes
983 wxDisplayImplDirectDraw::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;
1002 }
1003
1004 // ----------------------------------------------------------------------------
1005 // video mode switching
1006 // ----------------------------------------------------------------------------
1007
1008 bool 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
1038 #endif // wxUSE_DISPLAY