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