]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/display.cpp
use wxOSX_USE_COCOA instead of __WXOSX_COCOA__ (which was checked incorrectly)
[wxWidgets.git] / src / osx / core / display.cpp
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
6 // Created: 06/21/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_DISPLAY
27
28 #include "wx/display.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/dynarray.h"
32 #include "wx/log.h"
33 #include "wx/string.h"
34 #include "wx/gdicmn.h"
35 #endif
36
37 #include "wx/display_impl.h"
38 #include "wx/osx/private.h"
39
40 #if wxOSX_USE_COCOA_OR_CARBON
41
42 // ----------------------------------------------------------------------------
43 // display classes implementation
44 // ----------------------------------------------------------------------------
45
46 class wxDisplayImplMacOSX : public wxDisplayImpl
47 {
48 public:
49 wxDisplayImplMacOSX(unsigned n, CGDirectDisplayID id)
50 : wxDisplayImpl(n),
51 m_id(id)
52 {
53 }
54
55 virtual wxRect GetGeometry() const;
56 virtual wxRect GetClientArea() const;
57 virtual wxString GetName() const { return wxString(); }
58
59 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
60 virtual wxVideoMode GetCurrentMode() const;
61 virtual bool ChangeMode(const wxVideoMode& mode);
62
63 private:
64 CGDirectDisplayID m_id;
65
66 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX);
67 };
68
69 class wxDisplayFactoryMacOSX : public wxDisplayFactory
70 {
71 public:
72 wxDisplayFactoryMacOSX() {}
73
74 virtual wxDisplayImpl *CreateDisplay(unsigned n);
75 virtual unsigned GetCount();
76 virtual int GetFromPoint(const wxPoint& pt);
77
78 protected:
79 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX);
80 };
81
82 // ============================================================================
83 // wxDisplayFactoryMacOSX implementation
84 // ============================================================================
85
86 unsigned wxDisplayFactoryMacOSX::GetCount()
87 {
88 CGDisplayCount count;
89 CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count);
90
91 wxCHECK_MSG( err == CGDisplayNoErr, 0, "CGGetActiveDisplayList() failed" );
92
93 return count;
94 }
95
96 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
97 {
98 CGPoint thePoint = {(float)p.x, (float)p.y};
99 CGDirectDisplayID theID;
100 CGDisplayCount theCount;
101 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
102 wxASSERT(err == CGDisplayNoErr);
103
104 int nWhich = wxNOT_FOUND;
105
106 if (theCount)
107 {
108 theCount = GetCount();
109 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
110 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
111 wxASSERT(err == CGDisplayNoErr);
112
113 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
114 {
115 if (theIDs[nWhich] == theID)
116 break;
117 }
118
119 delete [] theIDs;
120
121 if (nWhich == (int) theCount)
122 {
123 wxFAIL_MSG(wxT("Failed to find display in display list"));
124 nWhich = wxNOT_FOUND;
125 }
126 }
127
128 return nWhich;
129 }
130
131 wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(unsigned n)
132 {
133 CGDisplayCount theCount = GetCount();
134 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
135
136 CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
137 wxCHECK_MSG( err == CGDisplayNoErr, NULL, "CGGetActiveDisplayList() failed" );
138
139 wxASSERT( n < theCount );
140
141 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
142
143 delete [] theIDs;
144
145 return display;
146 }
147
148 // ============================================================================
149 // wxDisplayImplMacOSX implementation
150 // ============================================================================
151
152 wxRect wxDisplayImplMacOSX::GetGeometry() const
153 {
154 CGRect theRect = CGDisplayBounds(m_id);
155 return wxRect( (int)theRect.origin.x,
156 (int)theRect.origin.y,
157 (int)theRect.size.width,
158 (int)theRect.size.height ); //floats
159 }
160
161 wxRect wxDisplayImplMacOSX::GetClientArea() const
162 {
163 // VZ: I don't know how to get client area for arbitrary display but
164 // wxGetClientDisplayRect() does work correctly for at least the main
165 // one (TODO: do it correctly for the other displays too)
166 if ( IsPrimary() )
167 return wxGetClientDisplayRect();
168
169 return wxDisplayImpl::GetClientArea();
170 }
171
172 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
173 {
174 CFNumberRef value = (CFNumberRef) CFDictionaryGetValue( desc, key );
175 if (value == NULL)
176 return 0;
177
178 int num = 0;
179 CFNumberGetValue( value, kCFNumberIntType, &num );
180
181 return num;
182 }
183
184 wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
185 {
186 wxArrayVideoModes resultModes;
187
188 CFArrayRef theArray = CGDisplayAvailableModes( m_id );
189
190 for (CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
191 {
192 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex( theArray, i );
193
194 wxVideoMode theMode(
195 wxCFDictKeyToInt( theValue, kCGDisplayWidth ),
196 wxCFDictKeyToInt( theValue, kCGDisplayHeight ),
197 wxCFDictKeyToInt( theValue, kCGDisplayBitsPerPixel ),
198 wxCFDictKeyToInt( theValue, kCGDisplayRefreshRate ));
199
200 if (theMode.Matches( mode ))
201 resultModes.Add( theMode );
202 }
203
204 return resultModes;
205 }
206
207 wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
208 {
209 CFDictionaryRef theValue = CGDisplayCurrentMode( m_id );
210
211 return wxVideoMode(
212 wxCFDictKeyToInt( theValue, kCGDisplayWidth ),
213 wxCFDictKeyToInt( theValue, kCGDisplayHeight ),
214 wxCFDictKeyToInt( theValue, kCGDisplayBitsPerPixel ),
215 wxCFDictKeyToInt( theValue, kCGDisplayRefreshRate ));
216 }
217
218 bool wxDisplayImplMacOSX::ChangeMode( const wxVideoMode& mode )
219 {
220 // Changing to default mode (wxDefaultVideoMode) doesn't
221 // work because we don't have access to the system's 'scrn'
222 // resource which holds the user's mode which the system
223 // will return to after this app is done
224 boolean_t bExactMatch;
225 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate(
226 m_id,
227 (size_t)mode.GetDepth(),
228 (size_t)mode.GetWidth(),
229 (size_t)mode.GetHeight(),
230 (double)mode.GetRefresh(),
231 &bExactMatch );
232
233 bool bOK = bExactMatch;
234
235 if (bOK)
236 bOK = CGDisplaySwitchToMode( m_id, theCGMode ) == CGDisplayNoErr;
237
238 return bOK;
239 }
240
241 // ============================================================================
242 // wxDisplay::CreateFactory()
243 // ============================================================================
244
245 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
246 {
247 return new wxDisplayFactoryMacOSX;
248 }
249
250 #else
251
252 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
253 {
254 return new wxDisplayFactorySingle;
255 }
256
257 #endif
258
259 #endif // wxUSE_DISPLAY