]> git.saurik.com Git - wxWidgets.git/blob - src/osx/utils_osx.cpp
new common code files
[wxWidgets.git] / src / osx / utils_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/utils_osx.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: utils.cpp 54886 2008-07-31 13:02:53Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 #include "wx/wxprec.h"
14
15 #include "wx/utils.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/app.h"
20 #if wxUSE_GUI
21 #include "wx/toplevel.h"
22 #include "wx/font.h"
23 #endif
24 #endif
25
26 #include "wx/apptrait.h"
27
28 #include <ctype.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34
35 // #include "MoreFilesX.h"
36
37 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
38 #include <AudioToolbox/AudioServices.h>
39 #endif
40
41 #include "wx/osx/private.h"
42
43 #ifdef wxOSX_USE_COCOA
44 // to get the themeing APIs
45 #include <Carbon/Carbon.h>
46 #endif
47
48 #if wxUSE_GUI
49 #include "wx/osx/private/timer.h"
50 #endif // wxUSE_GUI
51
52 #include "wx/evtloop.h"
53
54 #if defined(__MWERKS__) && wxUSE_UNICODE
55 #if __MWERKS__ < 0x4100
56 #include <wtime.h>
57 #endif
58 #endif
59
60 //
61 // TODO BEGIN move to utils_osx.cpp
62 //
63
64 #if wxUSE_BASE
65
66 extern bool WXDLLEXPORT wxIsDebuggerRunning()
67 {
68 // TODO : try to find out ...
69 return false;
70 }
71
72 #if wxOSX_USE_COCOA_OR_CARBON
73
74 // have a fast version for mac code that returns the version as a return value
75
76 long UMAGetSystemVersion()
77 {
78 static SInt32 sUMASystemVersion = 0 ;
79 if ( sUMASystemVersion == 0 )
80 {
81 verify_noerr(Gestalt(gestaltSystemVersion, &sUMASystemVersion));
82 }
83 return sUMASystemVersion ;
84 }
85
86 // our OS version is the same in non GUI and GUI cases
87 wxOperatingSystemId wxGetOsVersion(int *majorVsn, int *minorVsn)
88 {
89 SInt32 theSystem;
90 Gestalt(gestaltSystemVersion, &theSystem);
91
92 if ( majorVsn != NULL )
93 *majorVsn = (theSystem >> 8);
94
95 if ( minorVsn != NULL )
96 *minorVsn = (theSystem & 0xFF);
97
98 return wxOS_MAC_OSX_DARWIN;
99 }
100
101 #include <sys/utsname.h>
102
103 wxString wxGetOsDescription()
104 {
105 struct utsname name;
106 uname(&name);
107 return wxString::Format(_T("Mac OS X (%s %s %s)"),
108 wxString::FromAscii(name.sysname).c_str(),
109 wxString::FromAscii(name.release).c_str(),
110 wxString::FromAscii(name.machine).c_str());
111 }
112
113 #endif // wxOSX_USE_COCOA_OR_CARBON
114
115
116 //---------------------------------------------------------------------------
117 // wxMac Specific utility functions
118 //---------------------------------------------------------------------------
119
120 void wxMacStringToPascal( const wxString&from , StringPtr to )
121 {
122 wxCharBuffer buf = from.mb_str( wxConvLocal );
123 int len = strlen(buf);
124
125 if ( len > 255 )
126 len = 255;
127 to[0] = len;
128 memcpy( (char*) &to[1] , buf , len );
129 }
130
131 wxString wxMacMakeStringFromPascal( ConstStringPtr from )
132 {
133 return wxString( (char*) &from[1] , wxConvLocal , from[0] );
134 }
135
136 #endif // wxUSE_BASE
137
138 #if wxUSE_GUI
139
140 // Check whether this window wants to process messages, e.g. Stop button
141 // in long calculations.
142 bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
143 {
144 // TODO
145 return false;
146 }
147
148 // Return true if we have a colour display
149 bool wxColourDisplay()
150 {
151 return true;
152 }
153
154 #if wxOSX_USE_COCOA_OR_CARBON
155 // Returns depth of screen
156 int wxDisplayDepth()
157 {
158 int theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID());
159 return theDepth;
160 }
161
162 // Get size of display
163 void wxDisplaySize(int *width, int *height)
164 {
165 // TODO adapt for multi-displays
166 CGRect bounds = CGDisplayBounds(CGMainDisplayID());
167 if ( width )
168 *width = (int)bounds.size.width ;
169 if ( height )
170 *height = (int)bounds.size.height;
171 }
172 #endif
173
174 void wxDisplaySizeMM(int *width, int *height)
175 {
176 wxDisplaySize(width, height);
177 // on mac 72 is fixed (at least now;-)
178 double cvPt2Mm = 25.4 / 72;
179
180 if (width != NULL)
181 *width = int( *width * cvPt2Mm );
182
183 if (height != NULL)
184 *height = int( *height * cvPt2Mm );
185 }
186
187
188 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
189 {
190 // We suppose that toolkit version is the same as OS version under Mac
191 wxGetOsVersion(verMaj, verMin);
192
193 return wxPORT_OSX;
194 }
195
196 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
197 {
198 return new wxEventLoop;
199 }
200
201 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
202 {
203 return wxGenericFindWindowAtPoint(pt);
204 }
205
206 /*
207 Return the generic RGB color space. This is a 'get' function and the caller should
208 not release the returned value unless the caller retains it first. Usually callers
209 of this routine will immediately use the returned colorspace with CoreGraphics
210 so they typically do not need to retain it themselves.
211
212 This function creates the generic RGB color space once and hangs onto it so it can
213 return it whenever this function is called.
214 */
215
216 CGColorSpaceRef wxMacGetGenericRGBColorSpace()
217 {
218 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
219
220 if (genericRGBColorSpace == NULL)
221 {
222 #if wxOSX_USE_IPHONE
223 genericRGBColorSpace.reset( CGColorSpaceCreateDeviceRGB() );
224 #else
225 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
226 #endif
227 }
228
229 return genericRGBColorSpace;
230 }
231
232 #if wxOSX_USE_COCOA_OR_CARBON
233
234 CGColorRef wxMacCreateCGColorFromHITheme( ThemeBrush brush )
235 {
236 CGColorRef color ;
237 HIThemeBrushCreateCGColor( brush, &color );
238 return color;
239 }
240
241 #endif // wxOSX_USE_COCOA_OR_CARBON
242
243 IMPLEMENT_ABSTRACT_CLASS( wxWidgetImpl , wxObject )
244
245 wxWidgetImpl::wxWidgetImpl( wxWindowMac* peer , bool isRootControl )
246 {
247 Init();
248 m_isRootControl = isRootControl;
249 m_wxPeer = peer;
250 }
251
252 wxWidgetImpl::wxWidgetImpl()
253 {
254 Init();
255 }
256
257 wxWidgetImpl::~wxWidgetImpl()
258 {
259 }
260
261 void wxWidgetImpl::Init()
262 {
263 m_isRootControl = false;
264 m_wxPeer = NULL;
265 m_needsFocusRect = false;
266 }
267
268 void wxWidgetImpl::Destroy()
269 {
270 }
271
272 void wxWidgetImpl::SetNeedsFocusRect( bool needs )
273 {
274 m_needsFocusRect = needs;
275 }
276
277 bool wxWidgetImpl::NeedsFocusRect() const
278 {
279 return m_needsFocusRect;
280 }
281
282 #endif // wxUSE_GUI