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