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(size_t n, CGDirectDisplayID id_)
45 virtual wxRect GetGeometry() const;
46 virtual wxString GetName() const { return wxString(); }
48 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
49 virtual wxVideoMode GetCurrentMode() const;
50 virtual bool ChangeMode(const wxVideoMode& mode);
53 CGDirectDisplayID m_id;
55 DECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX)
58 class wxDisplayFactoryMacOSX : public wxDisplayFactory
61 wxDisplayFactoryMacOSX() { }
63 virtual wxDisplayImpl *CreateDisplay(size_t n);
64 virtual size_t GetCount();
65 virtual int GetFromPoint(const wxPoint& pt);
68 DECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX)
71 // ============================================================================
72 // wxDisplayFactoryMacOSX implementation
73 // ============================================================================
75 size_t wxDisplayFactoryMacOSX::GetCount()
81 CGGetActiveDisplayList(0, NULL, &count);
83 wxASSERT(err == CGDisplayNoErr);
88 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
90 CGPoint thePoint = {(float)p.x, (float)p.y};
91 CGDirectDisplayID theID;
92 CGDisplayCount theCount;
93 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
94 wxASSERT(err == CGDisplayNoErr);
96 int nWhich = wxNOT_FOUND;
100 theCount = GetCount();
101 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
102 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
103 wxASSERT(err == CGDisplayNoErr);
105 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
107 if (theIDs[nWhich] == theID)
113 if (nWhich == (int) theCount)
115 wxFAIL_MSG(wxT("Failed to find display in display list"));
116 nWhich = wxNOT_FOUND;
123 wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(size_t n)
125 CGDisplayCount theCount = GetCount();
126 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
131 CGGetActiveDisplayList(theCount, theIDs, &theCount);
133 wxASSERT( err == CGDisplayNoErr );
134 wxASSERT( n < theCount );
136 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
144 wxRect wxDisplayImplMacOSX::GetGeometry() const
146 CGRect theRect = CGDisplayBounds(m_id);
147 return wxRect( (int)theRect.origin.x,
148 (int)theRect.origin.y,
149 (int)theRect.size.width,
150 (int)theRect.size.height ); //floats
153 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
158 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
160 CFNumberGetValue(value, kCFNumberIntType, &num);
164 wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
166 wxArrayVideoModes Modes;
168 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
170 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
172 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
174 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
175 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
176 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
177 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
179 if (theMode.Matches(mode))
186 wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
188 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
190 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
191 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
192 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
193 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
196 bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
198 //Changing to default mode (wxDefualtVideoMode) doesn't
199 //work because we don't have access to the system's 'scrn'
200 //resource which holds the user's mode which the system
201 //will return to after this app is done
202 boolean_t bExactMatch;
203 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
208 (double)mode.refresh,
211 bool bOK = bExactMatch;
214 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
219 // ============================================================================
220 // wxDisplay::CreateFactory()
221 // ============================================================================
223 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
225 return new wxDisplayFactoryMacOSX;
228 #endif // wxUSE_DISPLAY