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