]> git.saurik.com Git - wxWidgets.git/blob - src/osx/iphone/utils.mm
Add wxTimer::StartOnce().
[wxWidgets.git] / src / osx / iphone / utils.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/utils.mm
3 // Purpose: various cocoa utility functions
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 #include "wx/wxprec.h"
13
14 #include "wx/wxprec.h"
15
16 #include "wx/utils.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.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 "wx/osx/private.h"
30
31 #if wxUSE_GUI
32 #include "wx/osx/private/timer.h"
33 #include "wx/osx/dcclient.h"
34 #endif // wxUSE_GUI
35
36 #if wxOSX_USE_IPHONE
37
38 #include <AudioToolbox/AudioServices.h>
39
40 #if 1 // wxUSE_BASE
41
42 // ----------------------------------------------------------------------------
43 // Common Event Support
44 // ----------------------------------------------------------------------------
45
46 @interface wxAppDelegate : NSObject <UIApplicationDelegate> {
47 }
48
49 @end
50
51 @implementation wxAppDelegate
52
53 - (void)applicationDidFinishLaunching:(UIApplication *)application {
54 wxTheApp->OnInit();
55 }
56
57 - (void)applicationWillTerminate:(UIApplication *)application {
58 wxCloseEvent event;
59 wxTheApp->OnEndSession(event);
60 }
61
62 - (void)dealloc {
63 [super dealloc];
64 }
65
66
67 @end
68
69 bool wxApp::CallOnInit()
70 {
71 return true;
72 }
73
74 bool wxApp::DoInitGui()
75 {
76 return true;
77 }
78
79 void wxApp::DoCleanUp()
80 {
81 }
82
83 #endif // wxUSE_BASE
84
85 #if wxUSE_GUI
86
87 // Emit a beeeeeep
88 void wxBell()
89 {
90 // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
91 AudioServicesPlayAlertSound(0x00001000 );
92 }
93
94 // ----------------------------------------------------------------------------
95 // Launch default browser
96 // ----------------------------------------------------------------------------
97
98 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
99 {
100 return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
101 == YES;
102 }
103
104 // TODO : reorganize
105
106 extern wxFont* CreateNormalFont()
107 {
108 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
109 }
110
111 extern wxFont* CreateSmallFont()
112 {
113 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
114 }
115
116 extern UIFont* CreateUIFont( const wxFont& font )
117 {
118 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
119 }
120
121 CFArrayRef CopyAvailableFontFamilyNames()
122 {
123 return (CFArrayRef) [[UIFont familyNames] retain];
124 }
125
126 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
127 {
128 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
129 if ( contextChanged )
130 UIGraphicsPushContext(context);
131
132 [text drawAtPoint:where withFont:font];
133
134 if ( contextChanged )
135 UIGraphicsPopContext();
136 }
137
138 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
139 {
140 return [text sizeWithFont:font];
141 }
142
143 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
144 {
145 #if 0
146 CGRect r = [[UIScreen mainScreen] applicationFrame];
147 CGRect bounds = [[UIScreen mainScreen] bounds];
148 if ( bounds.size.height > r.size.height )
149 {
150 // portrait
151 if ( x )
152 *x = r.origin.x;
153 if ( y )
154 *y = r.origin.y;
155 if ( width )
156 *width = r.size.width;
157 if ( height )
158 *height = r.size.height;
159 }
160 else
161 {
162 // landscape
163 if ( x )
164 *x = r.origin.y;
165 if ( y )
166 *y = r.origin.x;
167 if ( width )
168 *width = r.size.height;
169 if ( height )
170 *height = r.size.width;
171 }
172 #else
173 // it's easier to treat the status bar as an element of the toplevel window
174 // instead of the desktop in order to support easy rotation
175 if ( x )
176 *x = 0;
177 if ( y )
178 *y = 0;
179 wxDisplaySize(width, height);
180 #endif
181 }
182
183 void wxGetMousePosition( int* x, int* y )
184 {
185 if ( x )
186 *x = 0;
187 if ( y )
188 *y = 0;
189 };
190
191 wxMouseState wxGetMouseState()
192 {
193 wxMouseState ms;
194 return ms;
195 }
196
197 // Returns depth of screen
198 int wxDisplayDepth()
199 {
200 return 32; // TODO can we determine this ?
201 }
202
203 // Get size of display
204 void wxDisplaySize(int *width, int *height)
205 {
206 CGRect r = [[UIScreen mainScreen] applicationFrame];
207 CGRect bounds = [[UIScreen mainScreen] bounds];
208
209 if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
210 {
211 // portrait
212 if ( width )
213 *width = (int)bounds.size.width ;
214 if ( height )
215 *height = (int)bounds.size.height;
216 }
217 else
218 {
219 // landscape
220 if ( width )
221 *width = (int)bounds.size.height ;
222 if ( height )
223 *height = (int)bounds.size.width;
224 }
225 }
226
227 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
228 {
229 return new wxOSXTimerImpl(timer);
230 }
231
232 int gs_wxBusyCursorCount = 0;
233 extern wxCursor gMacCurrentCursor;
234 wxCursor gMacStoredActiveCursor;
235
236 // Set the cursor to the busy cursor for all windows
237 void wxBeginBusyCursor(const wxCursor *cursor)
238 {
239 if (gs_wxBusyCursorCount++ == 0)
240 {
241 gMacStoredActiveCursor = gMacCurrentCursor;
242 cursor->MacInstall();
243
244 wxSetCursor(*cursor);
245 }
246 //else: nothing to do, already set
247 }
248
249 // Restore cursor to normal
250 void wxEndBusyCursor()
251 {
252 wxCHECK_RET( gs_wxBusyCursorCount > 0,
253 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
254
255 if (--gs_wxBusyCursorCount == 0)
256 {
257 gMacStoredActiveCursor.MacInstall();
258 gMacStoredActiveCursor = wxNullCursor;
259
260 wxSetCursor(wxNullCursor);
261 }
262 }
263
264 // true if we're between the above two calls
265 bool wxIsBusy()
266 {
267 return (gs_wxBusyCursorCount > 0);
268 }
269
270 bool wxGetKeyState (wxKeyCode key)
271 {
272 return false;
273 }
274
275 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
276 {
277 // wxScreenDC is derived from wxWindowDC, so a screen dc will
278 // call this method when a Blit is performed with it as a source.
279 if (!m_window)
280 return wxNullBitmap;
281
282 wxSize sz = m_window->GetSize();
283
284 int left = subrect != NULL ? subrect->x : 0 ;
285 int top = subrect != NULL ? subrect->y : 0 ;
286 int width = subrect != NULL ? subrect->width : sz.x;
287 int height = subrect != NULL ? subrect->height : sz.y ;
288
289 wxBitmap bmp = wxBitmap(width, height, 32);
290
291 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
292
293 CGContextSaveGState(context);
294
295
296 CGContextTranslateCTM( context, 0, height );
297 CGContextScaleCTM( context, 1, -1 );
298
299 if ( subrect )
300 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
301
302 UIGraphicsPushContext(context);
303 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
304 UIGraphicsPopContext();
305 CGContextRestoreGState(context);
306
307 return bmp;
308 }
309
310 #endif // wxUSE_GUI
311
312 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
313 {
314 // get OS version
315 int major, minor;
316
317 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
318
319 if ( release.empty() ||
320 // TODO use wx method
321 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
322 {
323 // failed to get version string or unrecognized format
324 major =
325 minor = -1;
326 }
327
328 if ( verMaj )
329 *verMaj = major;
330 if ( verMin )
331 *verMin = minor;
332
333 return wxOS_MAC_OSX_DARWIN;
334 }
335
336 wxString wxGetOsDescription()
337 {
338 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
339
340 return release;
341 }
342
343
344 #endif // wxOSX_USE_IPHONE