]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/display.mm
implementation streamlined
[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
21#ifndef WX_PRECOMP
22 #include "wx/dynarray.h"
23#endif
24
25#include "wx/display.h"
ef1717a9 26#include "wx/display_impl.h"
51259762
RN
27#include "wx/gdicmn.h"
28#include "wx/string.h"
29
30#import <Foundation/Foundation.h>
31
32// ----------------------------------------------------------------------------
ef1717a9 33// display classes implementation
51259762
RN
34// ----------------------------------------------------------------------------
35
ef1717a9
VZ
36class wxDisplayImplMacOSX : public wxDisplayImpl
37{
38public:
fdd7a38b
VZ
39 wxDisplayImplMacOSX(size_t n, CGDirectDisplayID id_)
40 : wxDisplayImpl(n),
41 m_id(id_)
42 {
43 }
ef1717a9
VZ
44
45 virtual wxRect GetGeometry() const;
46 virtual wxString GetName() const { return wxString(); }
47
48 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
49 virtual wxVideoMode GetCurrentMode() const;
50 virtual bool ChangeMode(const wxVideoMode& mode);
51
52private:
53 CGDirectDisplayID m_id;
54
55 DECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX)
56};
57
58class wxDisplayFactoryMacOSX : public wxDisplayFactory
59{
60public:
fdd7a38b 61 wxDisplayFactoryMacOSX() { }
ef1717a9
VZ
62
63 virtual wxDisplayImpl *CreateDisplay(size_t n);
64 virtual size_t GetCount();
65 virtual int GetFromPoint(const wxPoint& pt);
66
67protected:
68 DECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX)
69};
70
71// ============================================================================
72// wxDisplayFactoryMacOSX implementation
73// ============================================================================
74
75size_t wxDisplayFactoryMacOSX::GetCount()
51259762
RN
76{
77 CGDisplayCount count;
78#ifdef __WXDEBUG__
ef1717a9 79 CGDisplayErr err =
51259762
RN
80#endif
81 CGGetActiveDisplayList(0, NULL, &count);
82
83 wxASSERT(err == CGDisplayNoErr);
ef1717a9 84
51259762
RN
85 return count;
86}
87
ef1717a9
VZ
88int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
89{
51259762
RN
90 CGPoint thePoint = {(float)p.x, (float)p.y};
91 CGDirectDisplayID theID;
92 CGDisplayCount theCount;
93 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
94 wxASSERT(err == CGDisplayNoErr);
ef1717a9
VZ
95
96 int nWhich = wxNOT_FOUND;
97
51259762
RN
98 if (theCount)
99 {
100 theCount = GetCount();
101 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
102 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
103 wxASSERT(err == CGDisplayNoErr);
104
ef1717a9 105 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
51259762 106 {
ef1717a9 107 if (theIDs[nWhich] == theID)
51259762
RN
108 break;
109 }
ef1717a9
VZ
110
111 delete [] theIDs;
112
113 if (nWhich == (int) theCount)
51259762
RN
114 {
115 wxFAIL_MSG(wxT("Failed to find display in display list"));
ef1717a9 116 nWhich = wxNOT_FOUND;
51259762
RN
117 }
118 }
ef1717a9 119
51259762 120 return nWhich;
ef1717a9 121}
51259762 122
ef1717a9 123wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(size_t n)
51259762
RN
124{
125 CGDisplayCount theCount = GetCount();
126 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
ef1717a9 127
51259762 128#ifdef __WXDEBUG__
ef1717a9 129 CGDisplayErr err =
51259762
RN
130#endif
131 CGGetActiveDisplayList(theCount, theIDs, &theCount);
132
ef1717a9
VZ
133 wxASSERT( err == CGDisplayNoErr );
134 wxASSERT( n < theCount );
135
fdd7a38b 136 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
ef1717a9
VZ
137
138 delete [] theIDs;
139
140 return display;
51259762
RN
141}
142
ef1717a9
VZ
143
144wxRect wxDisplayImplMacOSX::GetGeometry() const
51259762
RN
145{
146 CGRect theRect = CGDisplayBounds(m_id);
ef1717a9 147 return wxRect( (int)theRect.origin.x,
51259762
RN
148 (int)theRect.origin.y,
149 (int)theRect.size.width,
150 (int)theRect.size.height ); //floats
151}
152
51259762
RN
153static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
154{
155 CFNumberRef value;
156 int num = 0;
157
158 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
159 return 0;
160 CFNumberGetValue(value, kCFNumberIntType, &num);
161 return num;
162}
163
ef1717a9 164wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
51259762
RN
165{
166 wxArrayVideoModes Modes;
ef1717a9 167
51259762
RN
168 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
169
170 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
171 {
172 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
ef1717a9 173
51259762
RN
174 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
175 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
176 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
177 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
ef1717a9 178
51259762
RN
179 if (theMode.Matches(mode))
180 Modes.Add(theMode);
181 }
ef1717a9 182
51259762
RN
183 return Modes;
184}
185
ef1717a9 186wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
51259762
RN
187{
188 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
ef1717a9 189
51259762
RN
190 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
191 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
192 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
193 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
194}
195
ef1717a9 196bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
51259762
RN
197{
198 //Changing to default mode (wxDefualtVideoMode) doesn't
199 //work because we don't have access to the system's 'scrn'
200 //resource which holds the user's mode which the system
201 //will return to after this app is done
202 boolean_t bExactMatch;
203 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
204 m_id,
205 (size_t)mode.bpp,
206 (size_t)mode.w,
207 (size_t)mode.h,
208 (double)mode.refresh,
209 &bExactMatch);
ef1717a9 210
51259762 211 bool bOK = bExactMatch;
ef1717a9 212
51259762
RN
213 if(bOK)
214 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
215
216 return bOK;
217}
218
ef1717a9
VZ
219// ============================================================================
220// wxDisplay::CreateFactory()
221// ============================================================================
222
223/* static */ wxDisplayFactory *wxDisplay::CreateFactory()
51259762 224{
fdd7a38b 225 return new wxDisplayFactoryMacOSX;
51259762
RN
226}
227
228#endif // wxUSE_DISPLAY
ef1717a9 229