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 - (void)applicationDidFinishLaunching:(UIApplication *)application {
57 - (void)applicationWillTerminate:(UIApplication *)application {
59 wxTheApp->OnEndSession(event);
69 bool wxApp::CallOnInit()
74 bool wxApp::DoInitGui()
79 void wxApp::DoCleanUp()
90 // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
91 AudioServicesPlayAlertSound(0x00001000 );
94 // ----------------------------------------------------------------------------
95 // Launch default browser
96 // ----------------------------------------------------------------------------
98 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
100 return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
106 extern wxFont* CreateNormalFont()
108 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
111 extern wxFont* CreateSmallFont()
113 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
116 extern UIFont* CreateUIFont( const wxFont& font )
118 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
121 CFArrayRef CopyAvailableFontFamilyNames()
123 return (CFArrayRef) [[UIFont familyNames] retain];
126 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
128 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
129 if ( contextChanged )
130 UIGraphicsPushContext(context);
132 [text drawAtPoint:where withFont:font];
134 if ( contextChanged )
135 UIGraphicsPopContext();
138 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
140 return [text sizeWithFont:font];
143 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
146 CGRect r = [[UIScreen mainScreen] applicationFrame];
147 CGRect bounds = [[UIScreen mainScreen] bounds];
148 if ( bounds.size.height > r.size.height )
156 *width = r.size.width;
158 *height = r.size.height;
168 *width = r.size.height;
170 *height = r.size.width;
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
179 wxDisplaySize(width, height);
183 void wxGetMousePosition( int* x, int* y )
191 wxMouseState wxGetMouseState()
197 // Returns depth of screen
200 return 32; // TODO can we determine this ?
203 // Get size of display
204 void wxDisplaySize(int *width, int *height)
206 CGRect r = [[UIScreen mainScreen] applicationFrame];
207 CGRect bounds = [[UIScreen mainScreen] bounds];
209 if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
213 *width = (int)bounds.size.width ;
215 *height = (int)bounds.size.height;
221 *width = (int)bounds.size.height ;
223 *height = (int)bounds.size.width;
227 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
229 return new wxOSXTimerImpl(timer);
232 int gs_wxBusyCursorCount = 0;
233 extern wxCursor gMacCurrentCursor;
234 wxCursor gMacStoredActiveCursor;
236 // Set the cursor to the busy cursor for all windows
237 void wxBeginBusyCursor(const wxCursor *cursor)
239 if (gs_wxBusyCursorCount++ == 0)
241 gMacStoredActiveCursor = gMacCurrentCursor;
242 cursor->MacInstall();
244 wxSetCursor(*cursor);
246 //else: nothing to do, already set
249 // Restore cursor to normal
250 void wxEndBusyCursor()
252 wxCHECK_RET( gs_wxBusyCursorCount > 0,
253 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
255 if (--gs_wxBusyCursorCount == 0)
257 gMacStoredActiveCursor.MacInstall();
258 gMacStoredActiveCursor = wxNullCursor;
260 wxSetCursor(wxNullCursor);
264 // true if we're between the above two calls
267 return (gs_wxBusyCursorCount > 0);
270 bool wxGetKeyState (wxKeyCode key)
275 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
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.
282 wxSize sz = m_window->GetSize();
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 ;
289 wxBitmap bmp = wxBitmap(width, height, 32);
291 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
293 CGContextSaveGState(context);
296 CGContextTranslateCTM( context, 0, height );
297 CGContextScaleCTM( context, 1, -1 );
300 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
302 UIGraphicsPushContext(context);
303 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
304 UIGraphicsPopContext();
305 CGContextRestoreGState(context);
312 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
317 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
319 if ( release.empty() ||
320 // TODO use wx method
321 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
323 // failed to get version string or unrecognized format
333 return wxOS_MAC_OSX_DARWIN;
336 wxString wxGetOsDescription()
338 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
344 #endif // wxOSX_USE_IPHONE