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