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(unsigned 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 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX);
59 class wxDisplayFactoryMacOSX : public wxDisplayFactory
62 wxDisplayFactoryMacOSX() { }
64 virtual wxDisplayImpl *CreateDisplay(unsigned n);
65 virtual unsigned GetCount();
66 virtual int GetFromPoint(const wxPoint& pt);
69 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX);
72 // ============================================================================
73 // wxDisplayFactoryMacOSX implementation
74 // ============================================================================
76 unsigned wxDisplayFactoryMacOSX::GetCount()
79 CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count);
80 wxCHECK_MSG( err != CGDisplayNoErr, 0, "CGGetActiveDisplayList() failed" );
85 int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
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);
93 int nWhich = wxNOT_FOUND;
97 theCount = GetCount();
98 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
99 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
100 wxASSERT(err == CGDisplayNoErr);
102 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
104 if (theIDs[nWhich] == theID)
110 if (nWhich == (int) theCount)
112 wxFAIL_MSG(wxT("Failed to find display in display list"));
113 nWhich = wxNOT_FOUND;
120 wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(unsigned n)
122 CGDisplayCount theCount = GetCount();
123 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
125 CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
126 wxCHECK_MSG( err != CGDisplayNoErr, NULL, "CGGetActiveDisplayList() failed" );
128 wxASSERT( n < theCount );
130 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
138 wxRect wxDisplayImplMacOSX::GetGeometry() const
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
147 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
152 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
154 CFNumberGetValue(value, kCFNumberIntType, &num);
158 wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
160 wxArrayVideoModes Modes;
162 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
164 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
166 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
168 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
169 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
170 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
171 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
173 if (theMode.Matches(mode))
180 wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
182 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
184 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
185 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
186 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
187 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
190 bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
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 (
202 (double)mode.refresh,
205 bool bOK = bExactMatch;
208 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
213 // ============================================================================
214 // wxDisplay::CreateFactory()
215 // ============================================================================
217 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
219 return new wxDisplayFactoryMacOSX;
222 #endif // wxUSE_DISPLAY