]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/display.mm
Fix horizontal mouse wheel scrolling in wxGTK.
[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 6// Created: 2004-10-03
1e151594 7// Copyright: (c) Ryan Norton
51259762
RN
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
dcb68102 10
51259762
RN
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#if wxUSE_DISPLAY
19
df91131c
WS
20#include "wx/display.h"
21
51259762 22#ifndef WX_PRECOMP
df91131c
WS
23 #include "wx/dynarray.h"
24 #include "wx/string.h"
dd05139a 25 #include "wx/gdicmn.h"
51259762
RN
26#endif
27
ef1717a9 28#include "wx/display_impl.h"
51259762
RN
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:
4e675101 39 wxDisplayImplMacOSX(unsigned n, CGDirectDisplayID id_)
fdd7a38b
VZ
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
c0c133e1 55 wxDECLARE_NO_COPY_CLASS(wxDisplayImplMacOSX);
ef1717a9
VZ
56};
57
58class wxDisplayFactoryMacOSX : public wxDisplayFactory
59{
60public:
fdd7a38b 61 wxDisplayFactoryMacOSX() { }
ef1717a9 62
4e675101
PC
63 virtual wxDisplayImpl *CreateDisplay(unsigned n);
64 virtual unsigned GetCount();
ef1717a9
VZ
65 virtual int GetFromPoint(const wxPoint& pt);
66
67protected:
c0c133e1 68 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryMacOSX);
ef1717a9
VZ
69};
70
71// ============================================================================
72// wxDisplayFactoryMacOSX implementation
73// ============================================================================
74
4e675101 75unsigned wxDisplayFactoryMacOSX::GetCount()
51259762
RN
76{
77 CGDisplayCount count;
4b6a582b
VZ
78 CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count);
79 wxCHECK_MSG( err != CGDisplayNoErr, 0, "CGGetActiveDisplayList() failed" );
ef1717a9 80
51259762
RN
81 return count;
82}
83
ef1717a9
VZ
84int wxDisplayFactoryMacOSX::GetFromPoint(const wxPoint& p)
85{
51259762
RN
86 CGPoint thePoint = {(float)p.x, (float)p.y};
87 CGDirectDisplayID theID;
88 CGDisplayCount theCount;
89 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
90 wxASSERT(err == CGDisplayNoErr);
ef1717a9
VZ
91
92 int nWhich = wxNOT_FOUND;
93
51259762
RN
94 if (theCount)
95 {
96 theCount = GetCount();
97 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
98 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
99 wxASSERT(err == CGDisplayNoErr);
100
ef1717a9 101 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
51259762 102 {
ef1717a9 103 if (theIDs[nWhich] == theID)
51259762
RN
104 break;
105 }
ef1717a9
VZ
106
107 delete [] theIDs;
108
109 if (nWhich == (int) theCount)
51259762
RN
110 {
111 wxFAIL_MSG(wxT("Failed to find display in display list"));
ef1717a9 112 nWhich = wxNOT_FOUND;
51259762
RN
113 }
114 }
ef1717a9 115
51259762 116 return nWhich;
ef1717a9 117}
51259762 118
4e675101 119wxDisplayImpl *wxDisplayFactoryMacOSX::CreateDisplay(unsigned n)
51259762
RN
120{
121 CGDisplayCount theCount = GetCount();
122 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
ef1717a9 123
4b6a582b
VZ
124 CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
125 wxCHECK_MSG( err != CGDisplayNoErr, NULL, "CGGetActiveDisplayList() failed" );
51259762 126
ef1717a9
VZ
127 wxASSERT( n < theCount );
128
fdd7a38b 129 wxDisplayImplMacOSX *display = new wxDisplayImplMacOSX(n, theIDs[n]);
ef1717a9
VZ
130
131 delete [] theIDs;
132
133 return display;
51259762
RN
134}
135
ef1717a9
VZ
136
137wxRect wxDisplayImplMacOSX::GetGeometry() const
51259762
RN
138{
139 CGRect theRect = CGDisplayBounds(m_id);
ef1717a9 140 return wxRect( (int)theRect.origin.x,
51259762
RN
141 (int)theRect.origin.y,
142 (int)theRect.size.width,
143 (int)theRect.size.height ); //floats
144}
145
51259762
RN
146static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
147{
148 CFNumberRef value;
149 int num = 0;
150
151 if ( (value = (CFNumberRef) CFDictionaryGetValue(desc, key)) == NULL )
152 return 0;
153 CFNumberGetValue(value, kCFNumberIntType, &num);
154 return num;
155}
156
ef1717a9 157wxArrayVideoModes wxDisplayImplMacOSX::GetModes(const wxVideoMode& mode) const
51259762
RN
158{
159 wxArrayVideoModes Modes;
ef1717a9 160
51259762
RN
161 CFArrayRef theArray = CGDisplayAvailableModes(m_id);
162
163 for(CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
164 {
165 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex(theArray, i);
ef1717a9 166
51259762
RN
167 wxVideoMode theMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
168 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
169 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
170 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
ef1717a9 171
51259762
RN
172 if (theMode.Matches(mode))
173 Modes.Add(theMode);
174 }
ef1717a9 175
51259762
RN
176 return Modes;
177}
178
ef1717a9 179wxVideoMode wxDisplayImplMacOSX::GetCurrentMode() const
51259762
RN
180{
181 CFDictionaryRef theValue = CGDisplayCurrentMode (m_id);
ef1717a9 182
51259762
RN
183 return wxVideoMode(wxCFDictKeyToInt(theValue, kCGDisplayWidth),
184 wxCFDictKeyToInt(theValue, kCGDisplayHeight),
185 wxCFDictKeyToInt(theValue, kCGDisplayBitsPerPixel),
186 wxCFDictKeyToInt(theValue, kCGDisplayRefreshRate));
187}
188
ef1717a9 189bool wxDisplayImplMacOSX::ChangeMode(const wxVideoMode& mode)
51259762
RN
190{
191 //Changing to default mode (wxDefualtVideoMode) doesn't
192 //work because we don't have access to the system's 'scrn'
193 //resource which holds the user's mode which the system
194 //will return to after this app is done
195 boolean_t bExactMatch;
196 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate (
197 m_id,
198 (size_t)mode.bpp,
199 (size_t)mode.w,
200 (size_t)mode.h,
201 (double)mode.refresh,
202 &bExactMatch);
ef1717a9 203
51259762 204 bool bOK = bExactMatch;
ef1717a9 205
51259762
RN
206 if(bOK)
207 bOK = CGDisplaySwitchToMode(m_id, theCGMode) == CGDisplayNoErr;
208
209 return bOK;
210}
211
ef1717a9
VZ
212// ============================================================================
213// wxDisplay::CreateFactory()
214// ============================================================================
215
216/* static */ wxDisplayFactory *wxDisplay::CreateFactory()
51259762 217{
fdd7a38b 218 return new wxDisplayFactoryMacOSX;
51259762
RN
219}
220
221#endif // wxUSE_DISPLAY