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