]> git.saurik.com Git - wxWidgets.git/blob - src/osx/utils_osx.cpp
Make wxTaskBarIcon's ctor have the same API on all platforms even though setting...
[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 }
94 else
95 #endif
96 {
97 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
98 theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID());
99 #endif
100 }
101 return theDepth;
102 }
103
104 // Get size of display
105 void wxDisplaySize(int *width, int *height)
106 {
107 // TODO adapt for multi-displays
108 CGRect bounds = CGDisplayBounds(CGMainDisplayID());
109 if ( width )
110 *width = (int)bounds.size.width ;
111 if ( height )
112 *height = (int)bounds.size.height;
113 }
114
115 #if wxUSE_GUI
116
117 // ----------------------------------------------------------------------------
118 // Launch document with default app
119 // ----------------------------------------------------------------------------
120
121 bool wxLaunchDefaultApplication(const wxString& document, int flags)
122 {
123 wxUnusedVar(flags);
124
125 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(document)));
126 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
127 wxCFRef<CFURLRef> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
128 OSStatus err = LSOpenCFURLRef( curl , NULL );
129
130 if (err == noErr)
131 {
132 return true;
133 }
134 else
135 {
136 wxLogDebug(wxT("Default Application Launch error %d"), (int) err);
137 return false;
138 }
139 }
140
141 // ----------------------------------------------------------------------------
142 // Launch default browser
143 // ----------------------------------------------------------------------------
144
145 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
146 {
147 wxUnusedVar(flags);
148 wxCFRef< CFURLRef > curl( CFURLCreateWithString( kCFAllocatorDefault,
149 wxCFStringRef( url ), NULL ) );
150 OSStatus err = LSOpenCFURLRef( curl , NULL );
151
152 if (err == noErr)
153 {
154 return true;
155 }
156 else
157 {
158 wxLogDebug(wxT("Browser Launch error %d"), (int) err);
159 return false;
160 }
161 }
162
163 #endif // wxUSE_GUI
164
165 #endif
166
167 void wxDisplaySizeMM(int *width, int *height)
168 {
169 wxDisplaySize(width, height);
170 // on mac 72 is fixed (at least now;-)
171 double cvPt2Mm = 25.4 / 72;
172
173 if (width != NULL)
174 *width = int( *width * cvPt2Mm );
175
176 if (height != NULL)
177 *height = int( *height * cvPt2Mm );
178 }
179
180
181 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
182 {
183 // We suppose that toolkit version is the same as OS version under Mac
184 wxGetOsVersion(verMaj, verMin);
185
186 return wxPORT_OSX;
187 }
188
189 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
190 {
191 return new wxEventLoop;
192 }
193
194 wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt);
195
196 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
197 {
198 #if wxOSX_USE_CARBON
199
200 Point screenPoint = { pt.y , pt.x };
201 WindowRef windowRef;
202
203 if ( FindWindow( screenPoint , &windowRef ) )
204 {
205 wxNonOwnedWindow *nonOwned = wxNonOwnedWindow::GetFromWXWindow( windowRef );
206
207 if ( nonOwned )
208 return wxFindWindowAtPoint( nonOwned , pt );
209 }
210
211 return NULL;
212
213 #else
214
215 return wxGenericFindWindowAtPoint( pt );
216
217 #endif
218 }
219
220 /*
221 Return the generic RGB color space. This is a 'get' function and the caller should
222 not release the returned value unless the caller retains it first. Usually callers
223 of this routine will immediately use the returned colorspace with CoreGraphics
224 so they typically do not need to retain it themselves.
225
226 This function creates the generic RGB color space once and hangs onto it so it can
227 return it whenever this function is called.
228 */
229
230 CGColorSpaceRef wxMacGetGenericRGBColorSpace()
231 {
232 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
233
234 if (genericRGBColorSpace == NULL)
235 {
236 #if wxOSX_USE_IPHONE
237 genericRGBColorSpace.reset( CGColorSpaceCreateDeviceRGB() );
238 #else
239 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
240 #endif
241 }
242
243 return genericRGBColorSpace;
244 }
245
246 #if wxOSX_USE_COCOA_OR_CARBON
247
248 CGColorRef wxMacCreateCGColorFromHITheme( ThemeBrush brush )
249 {
250 CGColorRef color ;
251 HIThemeBrushCreateCGColor( brush, &color );
252 return color;
253 }
254
255 //---------------------------------------------------------------------------
256 // Mac Specific string utility functions
257 //---------------------------------------------------------------------------
258
259 void wxMacStringToPascal( const wxString&from , unsigned char * to )
260 {
261 wxCharBuffer buf = from.mb_str( wxConvLocal );
262 int len = strlen(buf);
263
264 if ( len > 255 )
265 len = 255;
266 to[0] = len;
267 memcpy( (char*) &to[1] , buf , len );
268 }
269
270 wxString wxMacMakeStringFromPascal( const unsigned char * from )
271 {
272 return wxString( (char*) &from[1] , wxConvLocal , from[0] );
273 }
274
275 #endif // wxOSX_USE_COCOA_OR_CARBON