]> git.saurik.com Git - wxWidgets.git/blob - src/osx/utils_osx.cpp
Fix a very annoying autorelease pool memory leak.
[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 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #include "wx/wxprec.h"
13
14 #include "wx/utils.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/app.h"
19 #include "wx/log.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 #include <AudioToolbox/AudioServices.h>
38
39 #include "wx/osx/private.h"
40 #include "wx/osx/private/timer.h"
41
42 #include "wx/evtloop.h"
43
44 // Check whether this window wants to process messages, e.g. Stop button
45 // in long calculations.
46 bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
47 {
48 // TODO
49 return false;
50 }
51
52 // Return true if we have a colour display
53 bool wxColourDisplay()
54 {
55 // always the case on OS X
56 return true;
57 }
58
59
60 #if wxOSX_USE_COCOA_OR_CARBON
61
62 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
63 // bring back declaration so that we can support deployment targets < 10_6
64 CG_EXTERN size_t CGDisplayBitsPerPixel(CGDirectDisplayID display)
65 CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_6,
66 __IPHONE_NA, __IPHONE_NA);
67 #endif
68
69 // Returns depth of screen
70 int wxDisplayDepth()
71 {
72 int theDepth = 0;
73
74 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
75 if ( UMAGetSystemVersion() >= 0x1060 )
76 {
77 CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
78 CFStringRef encoding = CGDisplayModeCopyPixelEncoding(currentMode);
79
80 if(CFStringCompare(encoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
81 theDepth = 32;
82 else if(CFStringCompare(encoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
83 theDepth = 16;
84 else if(CFStringCompare(encoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
85 theDepth = 8;
86 else
87 theDepth = 32; // some reasonable default
88
89 CFRelease(encoding);
90 CGDisplayModeRelease(currentMode);
91 }
92 else
93 #endif
94 {
95 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
96 theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID());
97 #endif
98 }
99 return theDepth;
100 }
101
102 // Get size of display
103 void wxDisplaySize(int *width, int *height)
104 {
105 // TODO adapt for multi-displays
106 CGRect bounds = CGDisplayBounds(CGMainDisplayID());
107 if ( width )
108 *width = (int)bounds.size.width ;
109 if ( height )
110 *height = (int)bounds.size.height;
111 }
112
113 #if wxUSE_GUI
114
115 // ----------------------------------------------------------------------------
116 // Launch document with default app
117 // ----------------------------------------------------------------------------
118
119 bool wxLaunchDefaultApplication(const wxString& document, int flags)
120 {
121 wxUnusedVar(flags);
122
123 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(document)));
124 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
125 wxCFRef<CFURLRef> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
126 OSStatus err = LSOpenCFURLRef( curl , NULL );
127
128 if (err == noErr)
129 {
130 return true;
131 }
132 else
133 {
134 wxLogDebug(wxT("Default Application Launch error %d"), (int) err);
135 return false;
136 }
137 }
138
139 // ----------------------------------------------------------------------------
140 // Launch default browser
141 // ----------------------------------------------------------------------------
142
143 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
144 {
145 wxUnusedVar(flags);
146 wxCFRef< CFURLRef > curl( CFURLCreateWithString( kCFAllocatorDefault,
147 wxCFStringRef( url ), NULL ) );
148 OSStatus err = LSOpenCFURLRef( curl , NULL );
149
150 if (err == noErr)
151 {
152 return true;
153 }
154 else
155 {
156 wxLogDebug(wxT("Browser Launch error %d"), (int) err);
157 return false;
158 }
159 }
160
161 #endif // wxUSE_GUI
162
163 #endif
164
165 void wxDisplaySizeMM(int *width, int *height)
166 {
167 wxDisplaySize(width, height);
168 // on mac 72 is fixed (at least now;-)
169 double cvPt2Mm = 25.4 / 72;
170
171 if (width != NULL)
172 *width = int( *width * cvPt2Mm );
173
174 if (height != NULL)
175 *height = int( *height * cvPt2Mm );
176 }
177
178
179 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
180 {
181 // We suppose that toolkit version is the same as OS version under Mac
182 wxGetOsVersion(verMaj, verMin);
183
184 return wxPORT_OSX;
185 }
186
187 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
188 {
189 return new wxEventLoop;
190 }
191
192 wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt);
193
194 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
195 {
196 #if wxOSX_USE_CARBON
197
198 Point screenPoint = { pt.y , pt.x };
199 WindowRef windowRef;
200
201 if ( FindWindow( screenPoint , &windowRef ) )
202 {
203 wxNonOwnedWindow *nonOwned = wxNonOwnedWindow::GetFromWXWindow( windowRef );
204
205 if ( nonOwned )
206 return wxFindWindowAtPoint( nonOwned , pt );
207 }
208
209 return NULL;
210
211 #else
212
213 return wxGenericFindWindowAtPoint( pt );
214
215 #endif
216 }
217
218 /*
219 Return the generic RGB color space. This is a 'get' function and the caller should
220 not release the returned value unless the caller retains it first. Usually callers
221 of this routine will immediately use the returned colorspace with CoreGraphics
222 so they typically do not need to retain it themselves.
223
224 This function creates the generic RGB color space once and hangs onto it so it can
225 return it whenever this function is called.
226 */
227
228 CGColorSpaceRef wxMacGetGenericRGBColorSpace()
229 {
230 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
231
232 if (genericRGBColorSpace == NULL)
233 {
234 #if wxOSX_USE_IPHONE
235 genericRGBColorSpace.reset( CGColorSpaceCreateDeviceRGB() );
236 #else
237 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
238 #endif
239 }
240
241 return genericRGBColorSpace;
242 }
243
244 #if wxOSX_USE_COCOA_OR_CARBON
245
246 CGColorRef wxMacCreateCGColorFromHITheme( ThemeBrush brush )
247 {
248 const int maxcachedbrush = 58+5; // negative indices are for metabrushes, cache down to -5)
249 int brushindex = brush+5;
250 if ( brushindex < 0 || brushindex > maxcachedbrush )
251 {
252 CGColorRef color ;
253 HIThemeBrushCreateCGColor( brush, &color );
254 return color;
255 }
256 else
257 {
258 static bool inited = false;
259 static CGColorRef themecolors[maxcachedbrush+1];
260 if ( !inited )
261 {
262 for ( int i = 0 ; i <= maxcachedbrush ; ++i )
263 HIThemeBrushCreateCGColor( i-5, &themecolors[i] );
264 inited = true;
265 }
266 return CGColorRetain(themecolors[brushindex ]);
267 }
268 }
269
270 //---------------------------------------------------------------------------
271 // Mac Specific string utility functions
272 //---------------------------------------------------------------------------
273
274 void wxMacStringToPascal( const wxString&from , unsigned char * to )
275 {
276 wxCharBuffer buf = from.mb_str( wxConvLocal );
277 int len = strlen(buf);
278
279 if ( len > 255 )
280 len = 255;
281 to[0] = len;
282 memcpy( (char*) &to[1] , buf , len );
283 }
284
285 wxString wxMacMakeStringFromPascal( const unsigned char * from )
286 {
287 return wxString( (char*) &from[1] , wxConvLocal , from[0] );
288 }
289
290 #endif // wxOSX_USE_COCOA_OR_CARBON