1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/utils.mm
3 // Purpose: various cocoa utility functions
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/wxprec.h"
22 #include "wx/toplevel.h"
27 #include "wx/apptrait.h"
29 #include "wx/osx/private.h"
32 #include "wx/osx/private/timer.h"
33 #include "wx/osx/dcclient.h"
38 #include <AudioToolbox/AudioServices.h>
42 // ----------------------------------------------------------------------------
43 // Common Event Support
44 // ----------------------------------------------------------------------------
46 @interface wxAppDelegate : NSObject <UIApplicationDelegate> {
51 @implementation wxAppDelegate
53 - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
55 wxUnusedVar(application);
56 wxUnusedVar(launchOptions);
57 wxTheApp->OSXOnWillFinishLaunching();
61 - (void)applicationDidFinishLaunching:(UIApplication *)application {
62 wxTheApp->OSXOnDidFinishLaunching();
65 - (void)applicationWillTerminate:(UIApplication *)application {
66 wxUnusedVar(application);
67 wxTheApp->OSXOnWillTerminate();
77 bool wxApp::CallOnInit()
82 bool wxApp::DoInitGui()
87 void wxApp::DoCleanUp()
98 // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
99 AudioServicesPlayAlertSound(0x00001000 );
102 // ----------------------------------------------------------------------------
103 // Launch default browser
104 // ----------------------------------------------------------------------------
106 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
108 return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
114 extern wxFont* CreateNormalFont()
116 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
119 extern wxFont* CreateSmallFont()
121 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
124 extern UIFont* CreateUIFont( const wxFont& font )
126 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
129 CFArrayRef CopyAvailableFontFamilyNames()
131 return (CFArrayRef) [[UIFont familyNames] retain];
134 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
136 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
137 if ( contextChanged )
138 UIGraphicsPushContext(context);
140 [text drawAtPoint:where withFont:font];
142 if ( contextChanged )
143 UIGraphicsPopContext();
146 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
148 return [text sizeWithFont:font];
151 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
154 CGRect r = [[UIScreen mainScreen] applicationFrame];
155 CGRect bounds = [[UIScreen mainScreen] bounds];
156 if ( bounds.size.height > r.size.height )
164 *width = r.size.width;
166 *height = r.size.height;
176 *width = r.size.height;
178 *height = r.size.width;
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
187 wxDisplaySize(width, height);
191 void wxGetMousePosition( int* x, int* y )
199 wxMouseState wxGetMouseState()
205 // Returns depth of screen
208 return 32; // TODO can we determine this ?
211 // Get size of display
212 void wxDisplaySize(int *width, int *height)
214 CGRect r = [[UIScreen mainScreen] applicationFrame];
215 CGRect bounds = [[UIScreen mainScreen] bounds];
217 if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
221 *width = (int)bounds.size.width ;
223 *height = (int)bounds.size.height;
229 *width = (int)bounds.size.height ;
231 *height = (int)bounds.size.width;
235 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
237 return new wxOSXTimerImpl(timer);
240 int gs_wxBusyCursorCount = 0;
241 extern wxCursor gMacCurrentCursor;
242 wxCursor gMacStoredActiveCursor;
244 // Set the cursor to the busy cursor for all windows
245 void wxBeginBusyCursor(const wxCursor *cursor)
247 if (gs_wxBusyCursorCount++ == 0)
249 gMacStoredActiveCursor = gMacCurrentCursor;
250 cursor->MacInstall();
252 wxSetCursor(*cursor);
254 //else: nothing to do, already set
257 // Restore cursor to normal
258 void wxEndBusyCursor()
260 wxCHECK_RET( gs_wxBusyCursorCount > 0,
261 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
263 if (--gs_wxBusyCursorCount == 0)
265 gMacStoredActiveCursor.MacInstall();
266 gMacStoredActiveCursor = wxNullCursor;
268 wxSetCursor(wxNullCursor);
272 // true if we're between the above two calls
275 return (gs_wxBusyCursorCount > 0);
278 bool wxGetKeyState (wxKeyCode key)
283 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
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.
290 wxSize sz = m_window->GetSize();
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 ;
297 wxBitmap bmp = wxBitmap(width, height, 32);
299 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
301 CGContextSaveGState(context);
304 CGContextTranslateCTM( context, 0, height );
305 CGContextScaleCTM( context, 1, -1 );
308 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
310 UIGraphicsPushContext(context);
311 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
312 UIGraphicsPopContext();
313 CGContextRestoreGState(context);
320 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
325 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
327 if ( release.empty() ||
328 // TODO use wx method
329 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
331 // failed to get version string or unrecognized format
341 return wxOS_MAC_OSX_DARWIN;
344 wxString wxGetOsDescription()
346 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
352 #endif // wxOSX_USE_IPHONE