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/gdicmn.h"
27 #include "wx/string.h"
29 #import <Foundation/Foundation.h>
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 size_t wxDisplayBase::GetCount()
41 CGGetActiveDisplayList(0, NULL, &count);
43 wxASSERT(err == CGDisplayNoErr);
47 int wxDisplayBase::GetFromPoint(const wxPoint &p)
49 CGPoint thePoint = {(float)p.x, (float)p.y};
50 CGDirectDisplayID theID;
51 CGDisplayCount theCount;
52 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
53 wxASSERT(err == CGDisplayNoErr);
58 theCount = GetCount();
59 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
60 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
61 wxASSERT(err == CGDisplayNoErr);
63 for(nWhich = 0; nWhich < (int) theCount; ++nWhich)
65 if(theIDs[nWhich] == theID)
71 if(nWhich == (int) theCount)
73 wxFAIL_MSG(wxT("Failed to find display in display list"));
79 }//CFUserNotification[NSBundle bundleForClass:[self class]]
81 wxDisplay::wxDisplay(size_t index) : wxDisplayBase ( index )
83 CGDisplayCount theCount = GetCount();
84 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
88 CGGetActiveDisplayList(theCount, theIDs, &theCount);
90 wxASSERT(err == CGDisplayNoErr);
91 wxASSERT(index < theCount);
98 wxRect wxDisplay::GetGeometry() const
100 CGRect theRect = CGDisplayBounds(m_id);
101 return wxRect( (int)theRect.origin.x,
102 (int)theRect.origin.y,
103 (int)theRect.size.width,
104 (int)theRect.size.height ); //floats
107 int wxDisplay::GetDepth() const
109 return (int) CGDisplayBitsPerPixel(m_id); //size_t
112 wxString wxDisplay::GetName() const
114 // Macs don't name their displays...
115 return wxEmptyString;
118 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
123 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
125 CFNumberGetValue(value, kCFNumberIntType, &num);
130 wxDisplay::GetModes(const wxVideoMode& mode) const
132 wxArrayVideoModes Modes;
134 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
136 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
138 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
140 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
141 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
142 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
143 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
145 if (theMode.Matches(mode))
152 wxVideoMode wxDisplay::GetCurrentMode() const
154 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
156 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
157 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
158 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
159 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
162 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
164 //Changing to default mode (wxDefualtVideoMode) doesn't
165 //work because we don't have access to the system's 'scrn'
166 //resource which holds the user's mode which the system
167 //will return to after this app is done
168 boolean_t bExactMatch;
169 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
174 (double)mode.refresh,
177 bool bOK = bExactMatch;
180 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
185 wxDisplay::~wxDisplay()
189 #endif // wxUSE_DISPLAY