]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/display.mm
using proper cmd constant
[wxWidgets.git] / src / cocoa / display.mm
CommitLineData
51259762 1/////////////////////////////////////////////////////////////////////////////
1e151594 2// Name: src/cocoa/display.mm
51259762
RN
3// Purpose: Cocoa implementation of wxDisplay class
4// Author: Ryan Norton
ef1717a9 5// Modified by:
51259762
RN
6// Created: 2004-10-03
7// RCS-ID: $Id$
1e151594 8// Copyright: (c) Ryan Norton
51259762
RN
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
dcb68102 11
51259762
RN
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_DISPLAY
20
df91131c
WS
21#include "wx/display.h"
22
51259762 23#ifndef WX_PRECOMP
df91131c
WS
24 #include "wx/dynarray.h"
25 #include "wx/string.h"
dd05139a 26 #include "wx/gdicmn.h"
51259762
RN
27#endif
28
ef1717a9 29#include "wx/display_impl.h"
51259762
RN
30
31#import <Foundation/Foundation.h>
32
33// ----------------------------------------------------------------------------
ef1717a9 34// display classes implementation
51259762
RN
35// ----------------------------------------------------------------------------
36
ef1717a9
VZ
37class wxDisplayImplMacOSX : public wxDisplayImpl
38{
39public:
4e675101 40 wxDisplayImplMacOSX(unsigned n, CGDirectDisplayID id_)
fdd7a38b
VZ
41 : wxDisplayImpl(n),
42 m_id(id_)
43 {
44 }
ef1717a9
VZ
45
46 virtual wxRect GetGeometry() const;
47 virtual wxString GetName() const { return wxString(); }
48
49 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
50 virtual wxVideoMode GetCurrentMode() const;
51 virtual bool ChangeMode(const wxVideoMode& mode);
52
53private:
54 CGDirectDisplayID m_id;
55
c0c133e1 56 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX);
ef1717a9
VZ
57};
58
59class wxDisplayFactoryMacOSX : public wxDisplayFactory
60{
61public:
fdd7a38b 62 wxDisplayFactoryMacOSX() { }
ef1717a9 63
4e675101
PC
64 virtual wxDisplayImpl *CreateDisplay(unsigned n);
65 virtual unsigned GetCount();
ef1717a9
VZ
66 virtual int GetFromPoint(const wxPoint& pt);
67
68protected:
c0c133e1 69 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX);
ef1717a9
VZ
70};
71
72// ============================================================================
73// wxDisplayFactoryMacOSX implementation
74// ============================================================================
75
4e675101 76unsigned wxDisplayFactoryMacOSX::GetCount()
51259762
RN
77{
78 CGDisplayCount count;
4b6a582b
VZ
79 CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count);
80 wxCHECK_MSG( err != CGDisplayNoErr, 0, "CGGetActiveDisplayList() failed" );
ef1717a9 81
51259762
RN
82 return count;
83}
84
ef1717a9
VZ
85int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
86{
51259762
RN
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);
ef1717a9
VZ
92
93 int nWhich = wxNOT_FOUND;
94
51259762
RN
95 if (theCount)
96 {
97 theCount = GetCount();
98 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
99 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
100 wxASSERT(err == CGDisplayNoErr);
101
ef1717a9 102 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
51259762 103 {
ef1717a9 104 if (theIDs[nWhich] == theID)
51259762
RN
105 break;
106 }
ef1717a9
VZ
107
108 delete [] theIDs;
109
110 if (nWhich == (int) theCount)
51259762
RN
111 {
112 wxFAIL_MSG(wxT("Failed to find display in display list"));
ef1717a9 113 nWhich = wxNOT_FOUND;
51259762
RN
114 }
115 }
ef1717a9 116
51259762 117 return nWhich;
ef1717a9 118}
51259762 119
4e675101 120wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(unsigned n)
51259762
RN
121{
122 CGDisplayCount theCount = GetCount();
123 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
ef1717a9 124
4b6a582b
VZ
125 CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
126 wxCHECK_MSG( err != CGDisplayNoErr, NULL, "CGGetActiveDisplayList() failed" );
51259762 127
ef1717a9
VZ
128 wxASSERT( n < theCount );
129
fdd7a38b 130 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
ef1717a9
VZ
131
132 delete [] theIDs;
133
134 return display;
51259762
RN
135}
136
ef1717a9
VZ
137
138wxRect wxDisplayImplMacOSX::GetGeometry() const
51259762
RN
139{
140 CGRect theRect = CGDisplayBounds(m_id);
ef1717a9 141 return wxRect( (int)theRect.origin.x,
51259762
RN
142 (int)theRect.origin.y,
143 (int)theRect.size.width,
144 (int)theRect.size.height ); //floats
145}
146
51259762
RN
147static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
148{
149 CFNumberRef value;
150 int num = 0;
151
152 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
153 return 0;
154 CFNumberGetValue(value, kCFNumberIntType, &num);
155 return num;
156}
157
ef1717a9 158wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
51259762
RN
159{
160 wxArrayVideoModes Modes;
ef1717a9 161
51259762
RN
162 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
163
164 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
165 {
166 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
ef1717a9 167
51259762
RN
168 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
169 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
170 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
171 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
ef1717a9 172
51259762
RN
173 if (theMode.Matches(mode))
174 Modes.Add(theMode);
175 }
ef1717a9 176
51259762
RN
177 return Modes;
178}
179
ef1717a9 180wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
51259762
RN
181{
182 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
ef1717a9 183
51259762
RN
184 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
185 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
186 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
187 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
188}
189
ef1717a9 190bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
51259762
RN
191{
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 (
198 m_id,
199 (size_t)mode.bpp,
200 (size_t)mode.w,
201 (size_t)mode.h,
202 (double)mode.refresh,
203 &bExactMatch);
ef1717a9 204
51259762 205 bool bOK = bExactMatch;
ef1717a9 206
51259762
RN
207 if(bOK)
208 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
209
210 return bOK;
211}
212
ef1717a9
VZ
213// ============================================================================
214// wxDisplay::CreateFactory()
215// ============================================================================
216
217/* static */ wxDisplayFactory *wxDisplay::CreateFactory()
51259762 218{
fdd7a38b 219 return new wxDisplayFactoryMacOSX;
51259762
RN
220}
221
222#endif // wxUSE_DISPLAY