1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/display.cpp
3 // Purpose: Mac implementation of wxDisplay class
4 // Author: Ryan Norton & Brian Victor
5 // Modified by: Royce Mitchell III, Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/display.h"
31 #include "wx/dynarray.h"
33 #include "wx/string.h"
34 #include "wx/gdicmn.h"
37 #include "wx/display_impl.h"
38 #include "wx/osx/private.h"
40 #if wxOSX_USE_COCOA_OR_CARBON
42 // ----------------------------------------------------------------------------
43 // display classes implementation
44 // ----------------------------------------------------------------------------
46 class wxDisplayImplMacOSX
: public wxDisplayImpl
49 wxDisplayImplMacOSX(unsigned n
, CGDirectDisplayID id
)
55 virtual wxRect
GetGeometry() const;
56 virtual wxRect
GetClientArea() const;
57 virtual wxString
GetName() const { return wxString(); }
59 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
60 virtual wxVideoMode
GetCurrentMode() const;
61 virtual bool ChangeMode(const wxVideoMode
& mode
);
64 CGDirectDisplayID m_id
;
66 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX
);
69 class wxDisplayFactoryMacOSX
: public wxDisplayFactory
72 wxDisplayFactoryMacOSX() {}
74 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
75 virtual unsigned GetCount();
76 virtual int GetFromPoint(const wxPoint
& pt
);
79 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX
);
82 // ============================================================================
83 // wxDisplayFactoryMacOSX implementation
84 // ============================================================================
86 unsigned wxDisplayFactoryMacOSX::GetCount()
92 CGGetActiveDisplayList(0, NULL
, &count
);
94 wxASSERT(err
== CGDisplayNoErr
);
99 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint
& p
)
101 CGPoint thePoint
= {(float)p
.x
, (float)p
.y
};
102 CGDirectDisplayID theID
;
103 CGDisplayCount theCount
;
104 CGDisplayErr err
= CGGetDisplaysWithPoint(thePoint
, 1, &theID
, &theCount
);
105 wxASSERT(err
== CGDisplayNoErr
);
107 int nWhich
= wxNOT_FOUND
;
111 theCount
= GetCount();
112 CGDirectDisplayID
* theIDs
= new CGDirectDisplayID
[theCount
];
113 err
= CGGetActiveDisplayList(theCount
, theIDs
, &theCount
);
114 wxASSERT(err
== CGDisplayNoErr
);
116 for (nWhich
= 0; nWhich
< (int) theCount
; ++nWhich
)
118 if (theIDs
[nWhich
] == theID
)
124 if (nWhich
== (int) theCount
)
126 wxFAIL_MSG(wxT("Failed to find display in display list"));
127 nWhich
= wxNOT_FOUND
;
134 wxDisplayImpl
*wxDisplayFactoryMacOSX::CreateDisplay(unsigned n
)
136 CGDisplayCount theCount
= GetCount();
137 CGDirectDisplayID
* theIDs
= new CGDirectDisplayID
[theCount
];
142 CGGetActiveDisplayList(theCount
, theIDs
, &theCount
);
144 wxASSERT( err
== CGDisplayNoErr
);
145 wxASSERT( n
< theCount
);
147 wxDisplayImplMacOSX
*display
= new wxDisplayImplMacOSX(n
, theIDs
[n
]);
154 // ============================================================================
155 // wxDisplayImplMacOSX implementation
156 // ============================================================================
158 wxRect
wxDisplayImplMacOSX::GetGeometry() const
160 CGRect theRect
= CGDisplayBounds(m_id
);
161 return wxRect( (int)theRect
.origin
.x
,
162 (int)theRect
.origin
.y
,
163 (int)theRect
.size
.width
,
164 (int)theRect
.size
.height
); //floats
167 wxRect
wxDisplayImplMacOSX::GetClientArea() const
169 // VZ: I don't know how to get client area for arbitrary display but
170 // wxGetClientDisplayRect() does work correctly for at least the main
171 // one (TODO: do it correctly for the other displays too)
173 return wxGetClientDisplayRect();
175 return wxDisplayImpl::GetClientArea();
178 static int wxCFDictKeyToInt( CFDictionaryRef desc
, CFStringRef key
)
180 CFNumberRef value
= (CFNumberRef
) CFDictionaryGetValue( desc
, key
);
185 CFNumberGetValue( value
, kCFNumberIntType
, &num
);
190 wxArrayVideoModes
wxDisplayImplMacOSX::GetModes(const wxVideoMode
& mode
) const
192 wxArrayVideoModes resultModes
;
194 CFArrayRef theArray
= CGDisplayAvailableModes( m_id
);
196 for (CFIndex i
= 0; i
< CFArrayGetCount(theArray
); ++i
)
198 CFDictionaryRef theValue
= (CFDictionaryRef
) CFArrayGetValueAtIndex( theArray
, i
);
201 wxCFDictKeyToInt( theValue
, kCGDisplayWidth
),
202 wxCFDictKeyToInt( theValue
, kCGDisplayHeight
),
203 wxCFDictKeyToInt( theValue
, kCGDisplayBitsPerPixel
),
204 wxCFDictKeyToInt( theValue
, kCGDisplayRefreshRate
));
206 if (theMode
.Matches( mode
))
207 resultModes
.Add( theMode
);
213 wxVideoMode
wxDisplayImplMacOSX::GetCurrentMode() const
215 CFDictionaryRef theValue
= CGDisplayCurrentMode( m_id
);
218 wxCFDictKeyToInt( theValue
, kCGDisplayWidth
),
219 wxCFDictKeyToInt( theValue
, kCGDisplayHeight
),
220 wxCFDictKeyToInt( theValue
, kCGDisplayBitsPerPixel
),
221 wxCFDictKeyToInt( theValue
, kCGDisplayRefreshRate
));
224 bool wxDisplayImplMacOSX::ChangeMode( const wxVideoMode
& mode
)
226 // Changing to default mode (wxDefaultVideoMode) doesn't
227 // work because we don't have access to the system's 'scrn'
228 // resource which holds the user's mode which the system
229 // will return to after this app is done
230 boolean_t bExactMatch
;
231 CFDictionaryRef theCGMode
= CGDisplayBestModeForParametersAndRefreshRate(
233 (size_t)mode
.GetDepth(),
234 (size_t)mode
.GetWidth(),
235 (size_t)mode
.GetHeight(),
236 (double)mode
.GetRefresh(),
239 bool bOK
= bExactMatch
;
242 bOK
= CGDisplaySwitchToMode( m_id
, theCGMode
) == CGDisplayNoErr
;
247 // ============================================================================
248 // wxDisplay::CreateFactory()
249 // ============================================================================
251 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
253 return new wxDisplayFactoryMacOSX
;
258 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
260 return new wxDisplayFactorySingle
;
265 #endif // wxUSE_DISPLAY