1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/display.mm
3 // Purpose: Cocoa implementation of wxDisplay class
7 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/display.h"
23 #include "wx/dynarray.h"
24 #include "wx/string.h"
25 #include "wx/gdicmn.h"
28 #include "wx/display_impl.h"
30 #import <Foundation/Foundation.h>
32 // ----------------------------------------------------------------------------
33 // display classes implementation
34 // ----------------------------------------------------------------------------
36 class wxDisplayImplMacOSX : public wxDisplayImpl
39 wxDisplayImplMacOSX(unsigned 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 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX);
58 class wxDisplayFactoryMacOSX : public wxDisplayFactory
61 wxDisplayFactoryMacOSX() { }
63 virtual wxDisplayImpl *CreateDisplay(unsigned n);
64 virtual unsigned GetCount();
65 virtual int GetFromPoint(const wxPoint& pt);
68 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX);
71 // ============================================================================
72 // wxDisplayFactoryMacOSX implementation
73 // ============================================================================
75 unsigned wxDisplayFactoryMacOSX::GetCount()
78 CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count);
79 wxCHECK_MSG( err != CGDisplayNoErr, 0, "CGGetActiveDisplayList() failed" );
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(unsigned n)
121 CGDisplayCount theCount = GetCount();
122 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
124 CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
125 wxCHECK_MSG( err != CGDisplayNoErr, NULL, "CGGetActiveDisplayList() failed" );
127 wxASSERT( n < theCount );
129 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
137 wxRect wxDisplayImplMacOSX::GetGeometry() const
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
146 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
151 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
153 CFNumberGetValue(value, kCFNumberIntType, &num);
157 wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
159 wxArrayVideoModes Modes;
161 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
163 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
165 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
167 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
168 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
169 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
170 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
172 if (theMode.Matches(mode))
179 wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
181 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
183 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
184 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
185 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
186 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
189 bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
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 (
201 (double)mode.refresh,
204 bool bOK = bExactMatch;
207 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
212 // ============================================================================
213 // wxDisplay::CreateFactory()
214 // ============================================================================
216 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
218 return new wxDisplayFactoryMacOSX;
221 #endif // wxUSE_DISPLAY