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