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