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