1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/cocoa/display.mm
 
   3 // Purpose:     Cocoa implementation of wxDisplay class
 
   8 // Copyright:   (c) Ryan Norton
 
   9 // Licence:     wxWindows licence
 
  10 /////////////////////////////////////////////////////////////////////////////
 
  13     #pragma implementation "display.h"
 
  16 // For compilers that support precompilation, includes "wx.h".
 
  17 #include "wx/wxprec.h"
 
  26    #include "wx/dynarray.h"
 
  29 #include "wx/display.h"
 
  30 #include "wx/gdicmn.h"
 
  31 #include "wx/string.h"
 
  33 #import <Foundation/Foundation.h>
 
  35 // ----------------------------------------------------------------------------
 
  37 // ----------------------------------------------------------------------------
 
  39 size_t wxDisplayBase::GetCount()
 
  45     CGGetActiveDisplayList(0, NULL, &count);
 
  47     wxASSERT(err == CGDisplayNoErr);
 
  51 int wxDisplayBase::GetFromPoint(const wxPoint &p)
 
  53     CGPoint thePoint = {(float)p.x, (float)p.y};
 
  54     CGDirectDisplayID theID;
 
  55     CGDisplayCount theCount;
 
  56     CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
 
  57     wxASSERT(err == CGDisplayNoErr);
 
  62         theCount = GetCount();
 
  63         CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
 
  64         err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
 
  65         wxASSERT(err == CGDisplayNoErr);
 
  67         for(nWhich = 0; nWhich < (int) theCount; ++nWhich)
 
  69             if(theIDs[nWhich] == theID)
 
  75         if(nWhich == (int) theCount)
 
  77             wxFAIL_MSG(wxT("Failed to find display in display list"));
 
  83 }//CFUserNotification[NSBundle bundleForClass:[self class]]
 
  85 wxDisplay::wxDisplay(size_t index) : wxDisplayBase ( index )
 
  87     CGDisplayCount theCount = GetCount();
 
  88     CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
 
  92     CGGetActiveDisplayList(theCount, theIDs, &theCount);
 
  94     wxASSERT(err == CGDisplayNoErr);
 
  95     wxASSERT(index < theCount);
 
 102 wxRect wxDisplay::GetGeometry() const
 
 104     CGRect theRect = CGDisplayBounds(m_id);
 
 105     return wxRect(      (int)theRect.origin.x,
 
 106                     (int)theRect.origin.y,
 
 107                     (int)theRect.size.width,
 
 108                     (int)theRect.size.height  ); //floats
 
 111 int wxDisplay::GetDepth() const
 
 113     return (int) CGDisplayBitsPerPixel(m_id); //size_t
 
 116 wxString wxDisplay::GetName() const
 
 118     // Macs don't name their displays...
 
 119     return wxEmptyString;
 
 122 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
 
 127     if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
 
 129     CFNumberGetValue(value, kCFNumberIntType, &num);
 
 134     wxDisplay::GetModes(const wxVideoMode& mode) const
 
 136     wxArrayVideoModes Modes;
 
 138     CFArrayRef theArray = CGDisplayAvailableModes(m_id);
 
 140     for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
 
 142         CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
 
 144         wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
 
 145                             wxCFDictKeyToInt(theValue, kCGDisplayHeight),
 
 146                             wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
 
 147                             wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
 
 149         if (theMode.Matches(mode))
 
 156 wxVideoMode wxDisplay::GetCurrentMode() const
 
 158     CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
 
 160     return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
 
 161                             wxCFDictKeyToInt(theValue, kCGDisplayHeight),
 
 162                             wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
 
 163                             wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
 
 166 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
 
 168     //Changing to default mode (wxDefualtVideoMode) doesn't
 
 169     //work because we don't have access to the system's 'scrn'
 
 170     //resource which holds the user's mode which the system
 
 171     //will return to after this app is done
 
 172     boolean_t bExactMatch;
 
 173     CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
 
 178                                         (double)mode.refresh,
 
 181     bool bOK = bExactMatch;
 
 184         bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
 
 189 wxDisplay::~wxDisplay()
 
 193 #endif // wxUSE_DISPLAY