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