]> git.saurik.com Git - wxWidgets.git/blame - src/msw/display.cpp
Respect alignment flags for owner-drawn buttons in wxMSW.
[wxWidgets.git] / src / msw / display.cpp
CommitLineData
a536e411 1/////////////////////////////////////////////////////////////////////////////
2ad495fb 2// Name: src/msw/display.cpp
a536e411 3// Purpose: MSW Implementation of wxDisplay class
fd921f35
VZ
4// Author: Royce Mitchell III, Vadim Zeitlin
5// Modified by: Ryan Norton (IsPrimary override)
a536e411
JS
6// Created: 06/21/02
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
fd921f35 9// Copyright: (c) 2002-2006 wxWidgets team
65571936 10// Licence: wxWindows licence
a536e411
JS
11/////////////////////////////////////////////////////////////////////////////
12
13// ===========================================================================
14// declarations
15// ===========================================================================
16
17// ---------------------------------------------------------------------------
18// headers
19// ---------------------------------------------------------------------------
20
a536e411
JS
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
7212d155
PC
30#include "wx/display.h"
31
a536e411 32#ifndef WX_PRECOMP
ad9835c9
WS
33 #include "wx/dynarray.h"
34 #include "wx/app.h"
35 #include "wx/frame.h"
a536e411
JS
36#endif
37
8585e2b5 38#include "wx/dynload.h"
ef1717a9 39#include "wx/sysopt.h"
8585e2b5 40
ef1717a9 41#include "wx/display_impl.h"
52778e00 42#include "wx/msw/wrapwin.h"
7212d155 43#include "wx/msw/missing.h"
142ae7b3 44#include "wx/msw/private.h"
a536e411 45
2ad495fb 46#ifndef __WXWINCE__
01c54165 47 // Older versions of windef.h don't define HMONITOR. Unfortunately, we
bcc1153a
VZ
48 // can't directly test whether HMONITOR is defined or not in windef.h as
49 // it's not a macro but a typedef, so we test for an unrelated symbol which
50 // is only defined in winuser.h if WINVER >= 0x0500
51 #if !defined(HMONITOR_DECLARED) && !defined(MNS_NOCHECK)
52 DECLARE_HANDLE(HMONITOR);
e5570318
JS
53 typedef BOOL(CALLBACK * MONITORENUMPROC )(HMONITOR, HDC, LPRECT, LPARAM);
54 typedef struct tagMONITORINFO
55 {
56 DWORD cbSize;
57 RECT rcMonitor;
58 RECT rcWork;
59 DWORD dwFlags;
60 } MONITORINFO, *LPMONITORINFO;
61 typedef struct tagMONITORINFOEX : public tagMONITORINFO
62 {
63 TCHAR szDevice[CCHDEVICENAME];
64 } MONITORINFOEX, *LPMONITORINFOEX;
65 #define MONITOR_DEFAULTTONULL 0x00000000
66 #define MONITOR_DEFAULTTOPRIMARY 0x00000001
67 #define MONITOR_DEFAULTTONEAREST 0x00000002
ad9835c9 68 #define MONITORINFOF_PRIMARY 0x00000001
bcc1153a
VZ
69 #define HMONITOR_DECLARED
70 #endif
2ad495fb 71#endif // !__WXWINCE__
a536e411 72
d238d403
VZ
73// display functions are found in different DLLs under WinCE and normal Win32
74#ifdef __WXWINCE__
9a83f860 75static const wxChar displayDllName[] = wxT("coredll.dll");
d238d403 76#else
9a83f860 77static const wxChar displayDllName[] = wxT("user32.dll");
d238d403
VZ
78#endif
79
a536e411 80// ----------------------------------------------------------------------------
06efac1f 81// typedefs for dynamically loaded Windows functions
a536e411
JS
82// ----------------------------------------------------------------------------
83
8585e2b5
VZ
84typedef LONG (WINAPI *ChangeDisplaySettingsEx_t)(LPCTSTR lpszDeviceName,
85 LPDEVMODE lpDevMode,
86 HWND hwnd,
87 DWORD dwFlags,
88 LPVOID lParam);
a536e411 89
01c54165
VZ
90typedef BOOL (WINAPI *EnumDisplayMonitors_t)(HDC,LPCRECT,MONITORENUMPROC,LPARAM);
91typedef HMONITOR (WINAPI *MonitorFromPoint_t)(POINT,DWORD);
92typedef HMONITOR (WINAPI *MonitorFromWindow_t)(HWND,DWORD);
93typedef BOOL (WINAPI *GetMonitorInfo_t)(HMONITOR,LPMONITORINFO);
94
ef1717a9
VZ
95#ifndef __WXWINCE__
96// emulation of ChangeDisplaySettingsEx() for Win95
97LONG WINAPI ChangeDisplaySettingsExForWin95(LPCTSTR WXUNUSED(lpszDeviceName),
98 LPDEVMODE lpDevMode,
99 HWND WXUNUSED(hwnd),
100 DWORD dwFlags,
101 LPVOID WXUNUSED(lParam))
102{
103 return ::ChangeDisplaySettings(lpDevMode, dwFlags);
104}
105#endif // !__WXWINCE__
06efac1f 106
a536e411 107
06efac1f 108// ----------------------------------------------------------------------------
d5947fad 109// wxDisplayMSW declaration
06efac1f
VZ
110// ----------------------------------------------------------------------------
111
d5947fad 112class wxDisplayMSW : public wxDisplayImpl
01c54165 113{
ef1717a9 114public:
d5947fad 115 wxDisplayMSW(unsigned n, HMONITOR hmon)
ef1717a9 116 : wxDisplayImpl(n),
d5947fad 117 m_hmon(hmon)
01c54165 118 {
01c54165 119 }
01c54165 120
ef1717a9 121 virtual wxRect GetGeometry() const;
6c5d6291 122 virtual wxRect GetClientArea() const;
ef1717a9
VZ
123 virtual wxString GetName() const;
124 virtual bool IsPrimary() const;
06efac1f 125
ef1717a9 126 virtual wxVideoMode GetCurrentMode() const;
d5947fad
VZ
127 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
128 virtual bool ChangeMode(const wxVideoMode& mode);
06efac1f 129
ef1717a9
VZ
130protected:
131 // convert a DEVMODE to our wxVideoMode
132 static wxVideoMode ConvertToVideoMode(const DEVMODE& dm)
133 {
134 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one"
135 // and although 0 is ok for us we don't want to return modes with 1hz
136 // refresh
137 return wxVideoMode(dm.dmPelsWidth,
138 dm.dmPelsHeight,
139 dm.dmBitsPerPel,
140 dm.dmDisplayFrequency > 1 ? dm.dmDisplayFrequency : 0);
141 }
142
473ca5f3
VZ
143 // Call GetMonitorInfo() and fill in the provided struct and return true if
144 // it succeeded, otherwise return false.
145 bool GetMonInfo(MONITORINFOEX& monInfo) const;
146
d5947fad
VZ
147 HMONITOR m_hmon;
148
149private:
150 wxDECLARE_NO_COPY_CLASS(wxDisplayMSW);
ef1717a9 151};
a536e411 152
d5947fad 153
8585e2b5 154// ----------------------------------------------------------------------------
d5947fad 155// wxDisplayFactoryMSW declaration
8585e2b5
VZ
156// ----------------------------------------------------------------------------
157
d5947fad
VZ
158WX_DEFINE_ARRAY(HMONITOR, wxMonitorHandleArray);
159
160// functions dynamically bound by wxDisplayFactoryMSW ctor.
ef1717a9
VZ
161static MonitorFromPoint_t gs_MonitorFromPoint = NULL;
162static MonitorFromWindow_t gs_MonitorFromWindow = NULL;
163static GetMonitorInfo_t gs_GetMonitorInfo = NULL;
d5947fad 164static EnumDisplayMonitors_t gs_EnumDisplayMonitors = NULL;
8585e2b5 165
d5947fad 166class wxDisplayFactoryMSW : public wxDisplayFactory
ef1717a9
VZ
167{
168public:
d5947fad
VZ
169 // ctor checks if the current system supports multimon API and dynamically
170 // bind the functions we need if this is the case and fills the
171 // m_displays array if they're available
172 wxDisplayFactoryMSW();
8585e2b5 173
ef1717a9 174 bool IsOk() const { return !m_displays.empty(); }
8585e2b5 175
d5947fad 176 virtual wxDisplayImpl *CreateDisplay(unsigned n);
4e675101 177 virtual unsigned GetCount() { return unsigned(m_displays.size()); }
ef1717a9 178 virtual int GetFromPoint(const wxPoint& pt);
1e93d595 179 virtual int GetFromWindow(const wxWindow *window);
a536e411 180
ef1717a9
VZ
181private:
182 // EnumDisplayMonitors() callback
183 static BOOL CALLBACK MultimonEnumProc(HMONITOR hMonitor,
184 HDC hdcMonitor,
185 LPRECT lprcMonitor,
186 LPARAM dwData);
06efac1f 187
d5947fad
VZ
188 // find the monitor corresponding to the given handle,
189 // return wxNOT_FOUND if not found
190 int FindDisplayFromHMONITOR(HMONITOR hmon) const;
06efac1f 191
d5947fad
VZ
192 // the array containing information about all available displays, filled by
193 // MultimonEnumProc()
194 wxMonitorHandleArray m_displays;
195
196 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMSW);
197};
8585e2b5 198
a536e411 199
ef1717a9 200// ----------------------------------------------------------------------------
d5947fad 201// wxDisplay implementation
ef1717a9
VZ
202// ----------------------------------------------------------------------------
203
204/* static */ wxDisplayFactory *wxDisplay::CreateFactory()
a536e411 205{
d5947fad 206 wxDisplayFactoryMSW *factoryMM = new wxDisplayFactoryMSW;
16e45865 207
ef1717a9
VZ
208 if ( factoryMM->IsOk() )
209 return factoryMM;
a536e411 210
ef1717a9
VZ
211 delete factoryMM;
212
d5947fad 213 // fall back to a stub implementation if no multimon support (Win95?)
ef1717a9 214 return new wxDisplayFactorySingle;
8585e2b5 215}
a536e411 216
16e45865 217
ef1717a9 218// ----------------------------------------------------------------------------
d5947fad 219// wxDisplayMSW implementation
ef1717a9
VZ
220// ----------------------------------------------------------------------------
221
473ca5f3 222bool wxDisplayMSW::GetMonInfo(MONITORINFOEX& monInfo) const
8585e2b5 223{
473ca5f3 224 if ( !gs_GetMonitorInfo(m_hmon, &monInfo) )
d5947fad 225 {
473ca5f3
VZ
226 wxLogLastError(wxT("GetMonitorInfo"));
227 return false;
ef1717a9 228 }
8585e2b5 229
473ca5f3
VZ
230 return true;
231}
232
233wxRect wxDisplayMSW::GetGeometry() const
234{
235 WinStruct<MONITORINFOEX> monInfo;
236
d5947fad 237 wxRect rect;
473ca5f3
VZ
238 if ( GetMonInfo(monInfo) )
239 wxCopyRECTToRect(monInfo.rcMonitor, rect);
06efac1f 240
d5947fad 241 return rect;
ef1717a9 242}
06efac1f 243
d5947fad 244wxRect wxDisplayMSW::GetClientArea() const
6c5d6291 245{
d5947fad
VZ
246 WinStruct<MONITORINFOEX> monInfo;
247
d5947fad 248 wxRect rectClient;
473ca5f3
VZ
249 if ( GetMonInfo(monInfo) )
250 wxCopyRECTToRect(monInfo.rcWork, rectClient);
6c5d6291 251
d5947fad 252 return rectClient;
6c5d6291
VZ
253}
254
d5947fad 255wxString wxDisplayMSW::GetName() const
ef1717a9 256{
d5947fad 257 WinStruct<MONITORINFOEX> monInfo;
06efac1f 258
473ca5f3
VZ
259 wxString name;
260 if ( GetMonInfo(monInfo) )
261 name = monInfo.szDevice;
d5947fad 262
473ca5f3 263 return name;
ef1717a9 264}
06efac1f 265
d5947fad 266bool wxDisplayMSW::IsPrimary() const
ef1717a9 267{
d5947fad
VZ
268 WinStruct<MONITORINFOEX> monInfo;
269
473ca5f3 270 if ( !GetMonInfo(monInfo) )
d5947fad 271 return false;
06efac1f 272
d5947fad 273 return (monInfo.dwFlags & MONITORINFOF_PRIMARY) != 0;
06efac1f
VZ
274}
275
d5947fad 276wxVideoMode wxDisplayMSW::GetCurrentMode() const
06efac1f 277{
ef1717a9 278 wxVideoMode mode;
01c54165 279
ef1717a9
VZ
280 // The first parameter of EnumDisplaySettings() must be NULL under Win95
281 // according to MSDN. The version of GetName() we implement for Win95
282 // returns an empty string.
283 const wxString name = GetName();
c9f78968
VS
284 const wxChar * const deviceName = name.empty()
285 ? (const wxChar*)NULL
286 : (const wxChar*)name.c_str();
06efac1f 287
ef1717a9
VZ
288 DEVMODE dm;
289 dm.dmSize = sizeof(dm);
290 dm.dmDriverExtra = 0;
d5947fad 291
ef1717a9 292 if ( !::EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, &dm) )
06efac1f 293 {
9a83f860 294 wxLogLastError(wxT("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
06efac1f 295 }
ef1717a9
VZ
296 else
297 {
298 mode = ConvertToVideoMode(dm);
299 }
300
301 return mode;
06efac1f
VZ
302}
303
d5947fad 304wxArrayVideoModes wxDisplayMSW::GetModes(const wxVideoMode& modeMatch) const
a536e411 305{
8585e2b5
VZ
306 wxArrayVideoModes modes;
307
01c54165
VZ
308 // The first parameter of EnumDisplaySettings() must be NULL under Win95
309 // according to MSDN. The version of GetName() we implement for Win95
310 // returns an empty string.
311 const wxString name = GetName();
c9f78968
VS
312 const wxChar * const deviceName = name.empty()
313 ? (const wxChar*)NULL
314 : (const wxChar*)name.c_str();
8585e2b5
VZ
315
316 DEVMODE dm;
01c54165
VZ
317 dm.dmSize = sizeof(dm);
318 dm.dmDriverExtra = 0;
d5947fad 319
8585e2b5
VZ
320 for ( int iModeNum = 0;
321 ::EnumDisplaySettings(deviceName, iModeNum, &dm);
322 iModeNum++ )
323 {
324 const wxVideoMode mode = ConvertToVideoMode(dm);
325 if ( mode.Matches(modeMatch) )
326 {
327 modes.Add(mode);
328 }
329 }
330
331 return modes;
a536e411
JS
332}
333
d5947fad 334bool wxDisplayMSW::ChangeMode(const wxVideoMode& mode)
a536e411 335{
8585e2b5 336 // prepare ChangeDisplaySettingsEx() parameters
61373fa8
WS
337 DEVMODE dm;
338 DEVMODE *pDevMode;
339
8585e2b5
VZ
340 int flags;
341
342 if ( mode == wxDefaultVideoMode )
343 {
344 // reset the video mode to default
345 pDevMode = NULL;
346 flags = 0;
347 }
348 else // change to the given mode
349 {
2edac25b 350 wxCHECK_MSG( mode.GetWidth() && mode.GetHeight(), false,
9a83f860 351 wxT("at least the width and height must be specified") );
8585e2b5
VZ
352
353 wxZeroMemory(dm);
354 dm.dmSize = sizeof(dm);
01c54165 355 dm.dmDriverExtra = 0;
8585e2b5 356 dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2edac25b
RR
357 dm.dmPelsWidth = mode.GetWidth();
358 dm.dmPelsHeight = mode.GetHeight();
8585e2b5 359
2edac25b 360 if ( mode.GetDepth() )
8585e2b5
VZ
361 {
362 dm.dmFields |= DM_BITSPERPEL;
2edac25b 363 dm.dmBitsPerPel = mode.GetDepth();
8585e2b5
VZ
364 }
365
2edac25b 366 if ( mode.GetRefresh() )
8585e2b5
VZ
367 {
368 dm.dmFields |= DM_DISPLAYFREQUENCY;
2edac25b 369 dm.dmDisplayFrequency = mode.GetRefresh();
8585e2b5
VZ
370 }
371
372 pDevMode = &dm;
373
2ad495fb
WS
374#ifdef __WXWINCE__
375 flags = 0;
376#else // !__WXWINCE__
8585e2b5 377 flags = CDS_FULLSCREEN;
2ad495fb 378#endif // __WXWINCE__/!__WXWINCE__
8585e2b5
VZ
379 }
380
381
382 // get pointer to the function dynamically
383 //
384 // we're only called from the main thread, so it's ok to use static
385 // variable
386 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx = NULL;
387 if ( !pfnChangeDisplaySettingsEx )
388 {
d238d403
VZ
389 wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET);
390 if ( dllDisplay.IsLoaded() )
8585e2b5 391 {
d238d403 392 wxDL_INIT_FUNC_AW(pfn, ChangeDisplaySettingsEx, dllDisplay);
8585e2b5 393 }
d238d403 394 //else: huh, no this DLL must always be present, what's going on??
8585e2b5 395
2ad495fb 396#ifndef __WXWINCE__
8585e2b5
VZ
397 if ( !pfnChangeDisplaySettingsEx )
398 {
399 // we must be under Win95 and so there is no multiple monitors
400 // support anyhow
401 pfnChangeDisplaySettingsEx = ChangeDisplaySettingsExForWin95;
402 }
2ad495fb 403#endif // !__WXWINCE__
8585e2b5
VZ
404 }
405
406 // do change the mode
407 switch ( pfnChangeDisplaySettingsEx
408 (
e0a050e3
VS
409 GetName().wx_str(), // display name
410 pDevMode, // dev mode or NULL to reset
411 NULL, // reserved
8585e2b5 412 flags,
e0a050e3 413 NULL // pointer to video parameters (not used)
8585e2b5
VZ
414 ) )
415 {
416 case DISP_CHANGE_SUCCESSFUL:
417 // ok
920b9675
JS
418 {
419 // If we have a top-level, full-screen frame, emulate
420 // the DirectX behavior and resize it. This makes this
421 // API quite a bit easier to use.
422 wxWindow *winTop = wxTheApp->GetTopWindow();
423 wxFrame *frameTop = wxDynamicCast(winTop, wxFrame);
424 if (frameTop && frameTop->IsFullScreen())
425 {
426 wxVideoMode current = GetCurrentMode();
2edac25b 427 frameTop->SetClientSize(current.GetWidth(), current.GetHeight());
920b9675
JS
428 }
429 }
06efac1f 430 return true;
8585e2b5
VZ
431
432 case DISP_CHANGE_BADMODE:
433 // don't complain about this, this is the only "expected" error
434 break;
435
436 default:
9a83f860 437 wxFAIL_MSG( wxT("unexpected ChangeDisplaySettingsEx() return value") );
8585e2b5
VZ
438 }
439
06efac1f
VZ
440 return false;
441}
442
d5947fad
VZ
443
444// ----------------------------------------------------------------------------
445// wxDisplayFactoryMSW implementation
446// ----------------------------------------------------------------------------
447
448wxDisplayFactoryMSW::wxDisplayFactoryMSW()
449{
450 if ( gs_MonitorFromPoint==NULL || gs_MonitorFromWindow==NULL
451 || gs_GetMonitorInfo==NULL || gs_EnumDisplayMonitors==NULL )
452 {
453 // First initialization, or last initialization failed.
454 wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET);
455
456 wxDL_INIT_FUNC(gs_, MonitorFromPoint, dllDisplay);
457 wxDL_INIT_FUNC(gs_, MonitorFromWindow, dllDisplay);
458 wxDL_INIT_FUNC_AW(gs_, GetMonitorInfo, dllDisplay);
459 wxDL_INIT_FUNC(gs_, EnumDisplayMonitors, dllDisplay);
460
461 // we can safely let dllDisplay go out of scope, the DLL itself will
462 // still remain loaded as all programs link to it statically anyhow
463 }
464
465 if ( gs_MonitorFromPoint==NULL || gs_MonitorFromWindow==NULL
466 || gs_GetMonitorInfo==NULL || gs_EnumDisplayMonitors==NULL )
467 return;
468
469 // enumerate all displays
470 if ( !gs_EnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) )
471 {
472 wxLogLastError(wxT("EnumDisplayMonitors"));
473 }
474}
475
476/* static */
477BOOL CALLBACK
478wxDisplayFactoryMSW::MultimonEnumProc(
479 HMONITOR hMonitor, // handle to display monitor
480 HDC WXUNUSED(hdcMonitor), // handle to monitor-appropriate device context
481 LPRECT WXUNUSED(lprcMonitor), // pointer to monitor intersection rectangle
482 LPARAM dwData) // data passed from EnumDisplayMonitors (this)
483{
484 wxDisplayFactoryMSW *const self = (wxDisplayFactoryMSW *)dwData;
485
486 self->m_displays.Add(hMonitor);
487
488 // continue the enumeration
489 return TRUE;
490}
491
492wxDisplayImpl *wxDisplayFactoryMSW::CreateDisplay(unsigned n)
493{
494 wxCHECK_MSG( n < m_displays.size(), NULL, wxT("An invalid index was passed to wxDisplay") );
495
496 return new wxDisplayMSW(n, m_displays[n]);
497}
498
499// helper for GetFromPoint() and GetFromWindow()
500int wxDisplayFactoryMSW::FindDisplayFromHMONITOR(HMONITOR hmon) const
501{
502 if ( hmon )
503 {
504 const size_t count = m_displays.size();
505 for ( size_t n = 0; n < count; n++ )
506 {
507 if ( hmon == m_displays[n] )
508 return n;
509 }
510 }
511
512 return wxNOT_FOUND;
513}
514
515int wxDisplayFactoryMSW::GetFromPoint(const wxPoint& pt)
516{
517 POINT pt2;
518 pt2.x = pt.x;
519 pt2.y = pt.y;
520
521 return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2,
522 MONITOR_DEFAULTTONULL));
523}
524
525int wxDisplayFactoryMSW::GetFromWindow(const wxWindow *window)
526{
527 return FindDisplayFromHMONITOR(gs_MonitorFromWindow(GetHwndOf(window),
528 MONITOR_DEFAULTTONULL));
529}
530
8585e2b5 531#endif // wxUSE_DISPLAY