1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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 <Carbon/Carbon.h>
39 #include "wx/display_impl.h"
41 // ----------------------------------------------------------------------------
42 // display classes implementation
43 // ----------------------------------------------------------------------------
45 class wxDisplayImplMacOSX
: public wxDisplayImpl
48 wxDisplayImplMacOSX(unsigned n
, CGDirectDisplayID id
)
54 virtual wxRect
GetGeometry() const;
55 virtual wxRect
GetClientArea() const;
56 virtual wxString
GetName() const { return wxString(); }
58 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
59 virtual wxVideoMode
GetCurrentMode() const;
60 virtual bool ChangeMode(const wxVideoMode
& mode
);
63 CGDirectDisplayID m_id
;
65 DECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX
)
68 class wxDisplayFactoryMacOSX
: public wxDisplayFactory
71 wxDisplayFactoryMacOSX() {}
73 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
74 virtual unsigned GetCount();
75 virtual int GetFromPoint(const wxPoint
& pt
);
78 DECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX
)
81 // ============================================================================
82 // wxDisplayFactoryMacOSX implementation
83 // ============================================================================
85 unsigned wxDisplayFactoryMacOSX::GetCount()
91 CGGetActiveDisplayList(0, NULL
, &count
);
93 wxASSERT(err
== CGDisplayNoErr
);
98 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint
& p
)
100 CGPoint thePoint
= {(float)p
.x
, (float)p
.y
};
101 CGDirectDisplayID theID
;
102 CGDisplayCount theCount
;
103 CGDisplayErr err
= CGGetDisplaysWithPoint(thePoint
, 1, &theID
, &theCount
);
104 wxASSERT(err
== CGDisplayNoErr
);
106 int nWhich
= wxNOT_FOUND
;
110 theCount
= GetCount();
111 CGDirectDisplayID
* theIDs
= new CGDirectDisplayID
[theCount
];
112 err
= CGGetActiveDisplayList(theCount
, theIDs
, &theCount
);
113 wxASSERT(err
== CGDisplayNoErr
);
115 for (nWhich
= 0; nWhich
< (int) theCount
; ++nWhich
)
117 if (theIDs
[nWhich
] == theID
)
123 if (nWhich
== (int) theCount
)
125 wxFAIL_MSG(wxT("Failed to find display in display list"));
126 nWhich
= wxNOT_FOUND
;
133 wxDisplayImpl
*wxDisplayFactoryMacOSX::CreateDisplay(unsigned n
)
135 CGDisplayCount theCount
= GetCount();
136 CGDirectDisplayID
* theIDs
= new CGDirectDisplayID
[theCount
];
141 CGGetActiveDisplayList(theCount
, theIDs
, &theCount
);
143 wxASSERT( err
== CGDisplayNoErr
);
144 wxASSERT( n
< theCount
);
146 wxDisplayImplMacOSX
*display
= new wxDisplayImplMacOSX(n
, theIDs
[n
]);
153 // ============================================================================
154 // wxDisplayImplMacOSX implementation
155 // ============================================================================
157 wxRect
wxDisplayImplMacOSX::GetGeometry() const
159 CGRect theRect
= CGDisplayBounds(m_id
);
160 return wxRect( (int)theRect
.origin
.x
,
161 (int)theRect
.origin
.y
,
162 (int)theRect
.size
.width
,
163 (int)theRect
.size
.height
); //floats
166 wxRect
wxDisplayImplMacOSX::GetClientArea() const
168 // VZ: I don't know how to get client area for arbitrary display but
169 // wxGetClientDisplayRect() does work correctly for at least the main
170 // one (TODO: do it correctly for the other displays too)
172 return wxGetClientDisplayRect();
174 return wxDisplayImpl::GetClientArea();
177 static int wxCFDictKeyToInt( CFDictionaryRef desc
, CFStringRef key
)
179 CFNumberRef value
= (CFNumberRef
) CFDictionaryGetValue( desc
, key
);
184 CFNumberGetValue( value
, kCFNumberIntType
, &num
);
189 wxArrayVideoModes
wxDisplayImplMacOSX::GetModes(const wxVideoMode
& mode
) const
191 wxArrayVideoModes resultModes
;
193 CFArrayRef theArray
= CGDisplayAvailableModes( m_id
);
195 for (CFIndex i
= 0; i
< CFArrayGetCount(theArray
); ++i
)
197 CFDictionaryRef theValue
= (CFDictionaryRef
) CFArrayGetValueAtIndex( theArray
, i
);
200 wxCFDictKeyToInt( theValue
, kCGDisplayWidth
),
201 wxCFDictKeyToInt( theValue
, kCGDisplayHeight
),
202 wxCFDictKeyToInt( theValue
, kCGDisplayBitsPerPixel
),
203 wxCFDictKeyToInt( theValue
, kCGDisplayRefreshRate
));
205 if (theMode
.Matches( mode
))
206 resultModes
.Add( theMode
);
212 wxVideoMode
wxDisplayImplMacOSX::GetCurrentMode() const
214 CFDictionaryRef theValue
= CGDisplayCurrentMode( m_id
);
217 wxCFDictKeyToInt( theValue
, kCGDisplayWidth
),
218 wxCFDictKeyToInt( theValue
, kCGDisplayHeight
),
219 wxCFDictKeyToInt( theValue
, kCGDisplayBitsPerPixel
),
220 wxCFDictKeyToInt( theValue
, kCGDisplayRefreshRate
));
223 bool wxDisplayImplMacOSX::ChangeMode( const wxVideoMode
& mode
)
225 // Changing to default mode (wxDefaultVideoMode) doesn't
226 // work because we don't have access to the system's 'scrn'
227 // resource which holds the user's mode which the system
228 // will return to after this app is done
229 boolean_t bExactMatch
;
230 CFDictionaryRef theCGMode
= CGDisplayBestModeForParametersAndRefreshRate(
232 (size_t)mode
.GetDepth(),
233 (size_t)mode
.GetWidth(),
234 (size_t)mode
.GetHeight(),
235 (double)mode
.GetRefresh(),
238 bool bOK
= bExactMatch
;
241 bOK
= CGDisplaySwitchToMode( m_id
, theCGMode
) == CGDisplayNoErr
;
246 // ============================================================================
247 // wxDisplay::CreateFactory()
248 // ============================================================================
250 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
252 return new wxDisplayFactoryMacOSX
;
255 #endif // wxUSE_DISPLAY