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"
21 #include "wx/display.h"
24 #include "wx/dynarray.h"
25 #include "wx/string.h"
26 #include "wx/gdicmn.h"
29 #include "wx/display_impl.h"
31 #import <Foundation/Foundation.h>
33 // ----------------------------------------------------------------------------
34 // display classes implementation
35 // ----------------------------------------------------------------------------
37 class wxDisplayImplMacOSX : public wxDisplayImpl
40 wxDisplayImplMacOSX(size_t n, CGDirectDisplayID id_)
46 virtual wxRect GetGeometry() const;
47 virtual wxString GetName() const { return wxString(); }
49 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
50 virtual wxVideoMode GetCurrentMode() const;
51 virtual bool ChangeMode(const wxVideoMode& mode);
54 CGDirectDisplayID m_id;
56 DECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX)
59 class wxDisplayFactoryMacOSX : public wxDisplayFactory
62 wxDisplayFactoryMacOSX() { }
64 virtual wxDisplayImpl *CreateDisplay(size_t n);
65 virtual size_t GetCount();
66 virtual int GetFromPoint(const wxPoint& pt);
69 DECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX)
72 // ============================================================================
73 // wxDisplayFactoryMacOSX implementation
74 // ============================================================================
76 size_t wxDisplayFactoryMacOSX::GetCount()
82 CGGetActiveDisplayList(0, NULL, &count);
84 wxASSERT(err == CGDisplayNoErr);
89 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
91 CGPoint thePoint = {(float)p.x, (float)p.y};
92 CGDirectDisplayID theID;
93 CGDisplayCount theCount;
94 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
95 wxASSERT(err == CGDisplayNoErr);
97 int nWhich = wxNOT_FOUND;
101 theCount = GetCount();
102 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
103 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
104 wxASSERT(err == CGDisplayNoErr);
106 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
108 if (theIDs[nWhich] == theID)
114 if (nWhich == (int) theCount)
116 wxFAIL_MSG(wxT("Failed to find display in display list"));
117 nWhich = wxNOT_FOUND;
124 wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(size_t n)
126 CGDisplayCount theCount = GetCount();
127 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
132 CGGetActiveDisplayList(theCount, theIDs, &theCount);
134 wxASSERT( err == CGDisplayNoErr );
135 wxASSERT( n < theCount );
137 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
145 wxRect wxDisplayImplMacOSX::GetGeometry() const
147 CGRect theRect = CGDisplayBounds(m_id);
148 return wxRect( (int)theRect.origin.x,
149 (int)theRect.origin.y,
150 (int)theRect.size.width,
151 (int)theRect.size.height ); //floats
154 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
159 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
161 CFNumberGetValue(value, kCFNumberIntType, &num);
165 wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
167 wxArrayVideoModes Modes;
169 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
171 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
173 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
175 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
176 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
177 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
178 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
180 if (theMode.Matches(mode))
187 wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
189 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
191 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
192 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
193 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
194 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
197 bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
199 //Changing to default mode (wxDefualtVideoMode) doesn't
200 //work because we don't have access to the system's 'scrn'
201 //resource which holds the user's mode which the system
202 //will return to after this app is done
203 boolean_t bExactMatch;
204 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
209 (double)mode.refresh,
212 bool bOK = bExactMatch;
215 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
220 // ============================================================================
221 // wxDisplay::CreateFactory()
222 // ============================================================================
224 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
226 return new wxDisplayFactoryMacOSX;
229 #endif // wxUSE_DISPLAY