1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/display.mm
3 // Purpose: Cocoa implementation of wxDisplay class
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/dynarray.h"
25 #include "wx/display.h"
26 #include "wx/display_impl.h"
27 #include "wx/gdicmn.h"
28 #include "wx/string.h"
30 #import <Foundation/Foundation.h>
32 // ----------------------------------------------------------------------------
33 // display classes implementation
34 // ----------------------------------------------------------------------------
36 class wxDisplayImplMacOSX : public wxDisplayImpl
39 wxDisplayImplMacOSX(CGDirectDisplayID id_) : m_id(id_) { }
41 virtual wxRect GetGeometry() const;
42 virtual wxString GetName() const { return wxString(); }
44 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
45 virtual wxVideoMode GetCurrentMode() const;
46 virtual bool ChangeMode(const wxVideoMode& mode);
49 CGDirectDisplayID m_id;
51 DECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX)
54 class wxDisplayFactoryMacOSX : public wxDisplayFactory
57 wxDisplayFactoryMacOSX();
59 virtual wxDisplayImpl *CreateDisplay(size_t n);
60 virtual size_t GetCount();
61 virtual int GetFromPoint(const wxPoint& pt);
64 DECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX)
67 // ============================================================================
68 // wxDisplayFactoryMacOSX implementation
69 // ============================================================================
71 size_t wxDisplayFactoryMacOSX::GetCount()
77 CGGetActiveDisplayList(0, NULL, &count);
79 wxASSERT(err == CGDisplayNoErr);
84 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
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);
92 int nWhich = wxNOT_FOUND;
96 theCount = GetCount();
97 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
98 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
99 wxASSERT(err == CGDisplayNoErr);
101 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
103 if (theIDs[nWhich] == theID)
109 if (nWhich == (int) theCount)
111 wxFAIL_MSG(wxT("Failed to find display in display list"));
112 nWhich = wxNOT_FOUND;
119 wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(size_t n)
121 CGDisplayCount theCount = GetCount();
122 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
127 CGGetActiveDisplayList(theCount, theIDs, &theCount);
129 wxASSERT( err == CGDisplayNoErr );
130 wxASSERT( n < theCount );
132 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(theIDs[n]);
140 wxRect wxDisplayImplMacOSX::GetGeometry() const
142 CGRect theRect = CGDisplayBounds(m_id);
143 return wxRect( (int)theRect.origin.x,
144 (int)theRect.origin.y,
145 (int)theRect.size.width,
146 (int)theRect.size.height ); //floats
149 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
154 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
156 CFNumberGetValue(value, kCFNumberIntType, &num);
160 wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
162 wxArrayVideoModes Modes;
164 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
166 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
168 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
170 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
171 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
172 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
173 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
175 if (theMode.Matches(mode))
182 wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
184 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
186 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
187 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
188 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
189 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
192 bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
194 //Changing to default mode (wxDefualtVideoMode) doesn't
195 //work because we don't have access to the system's 'scrn'
196 //resource which holds the user's mode which the system
197 //will return to after this app is done
198 boolean_t bExactMatch;
199 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
204 (double)mode.refresh,
207 bool bOK = bExactMatch;
210 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
215 // ============================================================================
216 // wxDisplay::CreateFactory()
217 // ============================================================================
219 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
221 return new wxDisplayFactoryMac;
224 #endif // wxUSE_DISPLAY