]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/cocoa/display.mm | |
3 | // Purpose: Cocoa implementation of wxDisplay class | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 2004-10-03 | |
7 | // Copyright: (c) Ryan Norton | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_DISPLAY | |
19 | ||
20 | #include "wx/display.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/dynarray.h" | |
24 | #include "wx/string.h" | |
25 | #include "wx/gdicmn.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/display_impl.h" | |
29 | ||
30 | #import <Foundation/Foundation.h> | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // display classes implementation | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | class wxDisplayImplMacOSX : public wxDisplayImpl | |
37 | { | |
38 | public: | |
39 | wxDisplayImplMacOSX(unsigned n, CGDirectDisplayID id_) | |
40 | : wxDisplayImpl(n), | |
41 | m_id(id_) | |
42 | { | |
43 | } | |
44 | ||
45 | virtual wxRect GetGeometry() const; | |
46 | virtual wxString GetName() const { return wxString(); } | |
47 | ||
48 | virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const; | |
49 | virtual wxVideoMode GetCurrentMode() const; | |
50 | virtual bool ChangeMode(const wxVideoMode& mode); | |
51 | ||
52 | private: | |
53 | CGDirectDisplayID m_id; | |
54 | ||
55 | wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX); | |
56 | }; | |
57 | ||
58 | class wxDisplayFactoryMacOSX : public wxDisplayFactory | |
59 | { | |
60 | public: | |
61 | wxDisplayFactoryMacOSX() { } | |
62 | ||
63 | virtual wxDisplayImpl *CreateDisplay(unsigned n); | |
64 | virtual unsigned GetCount(); | |
65 | virtual int GetFromPoint(const wxPoint& pt); | |
66 | ||
67 | protected: | |
68 | wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX); | |
69 | }; | |
70 | ||
71 | // ============================================================================ | |
72 | // wxDisplayFactoryMacOSX implementation | |
73 | // ============================================================================ | |
74 | ||
75 | unsigned wxDisplayFactoryMacOSX::GetCount() | |
76 | { | |
77 | CGDisplayCount count; | |
78 | CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count); | |
79 | wxCHECK_MSG( err != CGDisplayNoErr, 0, "CGGetActiveDisplayList() failed" ); | |
80 | ||
81 | return count; | |
82 | } | |
83 | ||
84 | int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p) | |
85 | { | |
86 | CGPoint thePoint = {(float)p.x, (float)p.y}; | |
87 | CGDirectDisplayID theID; | |
88 | CGDisplayCount theCount; | |
89 | CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount); | |
90 | wxASSERT(err == CGDisplayNoErr); | |
91 | ||
92 | int nWhich = wxNOT_FOUND; | |
93 | ||
94 | if (theCount) | |
95 | { | |
96 | theCount = GetCount(); | |
97 | CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount]; | |
98 | err = CGGetActiveDisplayList(theCount, theIDs, &theCount); | |
99 | wxASSERT(err == CGDisplayNoErr); | |
100 | ||
101 | for (nWhich = 0; nWhich < (int) theCount; ++nWhich) | |
102 | { | |
103 | if (theIDs[nWhich] == theID) | |
104 | break; | |
105 | } | |
106 | ||
107 | delete [] theIDs; | |
108 | ||
109 | if (nWhich == (int) theCount) | |
110 | { | |
111 | wxFAIL_MSG(wxT("Failed to find display in display list")); | |
112 | nWhich = wxNOT_FOUND; | |
113 | } | |
114 | } | |
115 | ||
116 | return nWhich; | |
117 | } | |
118 | ||
119 | wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(unsigned n) | |
120 | { | |
121 | CGDisplayCount theCount = GetCount(); | |
122 | CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount]; | |
123 | ||
124 | CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount); | |
125 | wxCHECK_MSG( err != CGDisplayNoErr, NULL, "CGGetActiveDisplayList() failed" ); | |
126 | ||
127 | wxASSERT( n < theCount ); | |
128 | ||
129 | wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]); | |
130 | ||
131 | delete [] theIDs; | |
132 | ||
133 | return display; | |
134 | } | |
135 | ||
136 | ||
137 | wxRect wxDisplayImplMacOSX::GetGeometry() const | |
138 | { | |
139 | CGRect theRect = CGDisplayBounds(m_id); | |
140 | return wxRect( (int)theRect.origin.x, | |
141 | (int)theRect.origin.y, | |
142 | (int)theRect.size.width, | |
143 | (int)theRect.size.height ); //floats | |
144 | } | |
145 | ||
146 | static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key ) | |
147 | { | |
148 | CFNumberRef value; | |
149 | int num = 0; | |
150 | ||
151 | if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL ) | |
152 | return 0; | |
153 | CFNumberGetValue(value, kCFNumberIntType, &num); | |
154 | return num; | |
155 | } | |
156 | ||
157 | wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const | |
158 | { | |
159 | wxArrayVideoModes Modes; | |
160 | ||
161 | CFArrayRef theArray = CGDisplayAvailableModes(m_id); | |
162 | ||
163 | for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i) | |
164 | { | |
165 | CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i); | |
166 | ||
167 | wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth), | |
168 | wxCFDictKeyToInt(theValue, kCGDisplayHeight), | |
169 | wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel), | |
170 | wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate)); | |
171 | ||
172 | if (theMode.Matches(mode)) | |
173 | Modes.Add(theMode); | |
174 | } | |
175 | ||
176 | return Modes; | |
177 | } | |
178 | ||
179 | wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const | |
180 | { | |
181 | CFDictionaryRef theValue = CGDisplayCurrentMode (m_id); | |
182 | ||
183 | return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth), | |
184 | wxCFDictKeyToInt(theValue, kCGDisplayHeight), | |
185 | wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel), | |
186 | wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate)); | |
187 | } | |
188 | ||
189 | bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode) | |
190 | { | |
191 | //Changing to default mode (wxDefualtVideoMode) doesn't | |
192 | //work because we don't have access to the system's 'scrn' | |
193 | //resource which holds the user's mode which the system | |
194 | //will return to after this app is done | |
195 | boolean_t bExactMatch; | |
196 | CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate ( | |
197 | m_id, | |
198 | (size_t)mode.bpp, | |
199 | (size_t)mode.w, | |
200 | (size_t)mode.h, | |
201 | (double)mode.refresh, | |
202 | &bExactMatch); | |
203 | ||
204 | bool bOK = bExactMatch; | |
205 | ||
206 | if(bOK) | |
207 | bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr; | |
208 | ||
209 | return bOK; | |
210 | } | |
211 | ||
212 | // ============================================================================ | |
213 | // wxDisplay::CreateFactory() | |
214 | // ============================================================================ | |
215 | ||
216 | /* static */ wxDisplayFactory *wxDisplay::CreateFactory() | |
217 | { | |
218 | return new wxDisplayFactoryMacOSX; | |
219 | } | |
220 | ||
221 | #endif // wxUSE_DISPLAY |