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