1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/utils.mm
3 // Purpose: various cocoa utility functions
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/wxprec.h"
21 #include "wx/toplevel.h"
26 #include "wx/apptrait.h"
28 #include "wx/osx/private.h"
31 #include "wx/osx/private/timer.h"
32 #include "wx/osx/dcclient.h"
37 #include <AudioToolbox/AudioServices.h>
41 // ----------------------------------------------------------------------------
42 // Common Event Support
43 // ----------------------------------------------------------------------------
45 @interface wxAppDelegate : NSObject <UIApplicationDelegate> {
50 @implementation wxAppDelegate
52 - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
54 wxUnusedVar(application);
55 wxUnusedVar(launchOptions);
56 wxTheApp->OSXOnWillFinishLaunching();
60 - (void)applicationDidFinishLaunching:(UIApplication *)application {
61 wxTheApp->OSXOnDidFinishLaunching();
64 - (void)applicationWillTerminate:(UIApplication *)application {
65 wxUnusedVar(application);
66 wxTheApp->OSXOnWillTerminate();
76 bool wxApp::CallOnInit()
81 bool wxApp::DoInitGui()
86 void wxApp::DoCleanUp()
97 // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
98 AudioServicesPlayAlertSound(0x00001000 );
101 // ----------------------------------------------------------------------------
102 // Launch default browser
103 // ----------------------------------------------------------------------------
105 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
107 return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
113 extern wxFont* CreateNormalFont()
115 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
118 extern wxFont* CreateSmallFont()
120 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
123 extern UIFont* CreateUIFont( const wxFont& font )
125 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
128 CFArrayRef CopyAvailableFontFamilyNames()
130 return (CFArrayRef) [[UIFont familyNames] retain];
133 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
135 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
136 if ( contextChanged )
137 UIGraphicsPushContext(context);
139 [text drawAtPoint:where withFont:font];
141 if ( contextChanged )
142 UIGraphicsPopContext();
145 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
147 return [text sizeWithFont:font];
150 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
153 CGRect r = [[UIScreen mainScreen] applicationFrame];
154 CGRect bounds = [[UIScreen mainScreen] bounds];
155 if ( bounds.size.height > r.size.height )
163 *width = r.size.width;
165 *height = r.size.height;
175 *width = r.size.height;
177 *height = r.size.width;
180 // it's easier to treat the status bar as an element of the toplevel window
181 // instead of the desktop in order to support easy rotation
186 wxDisplaySize(width, height);
190 void wxGetMousePosition( int* x, int* y )
198 wxMouseState wxGetMouseState()
204 // Returns depth of screen
207 return 32; // TODO can we determine this ?
210 // Get size of display
211 void wxDisplaySize(int *width, int *height)
213 CGRect r = [[UIScreen mainScreen] applicationFrame];
214 CGRect bounds = [[UIScreen mainScreen] bounds];
216 if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
220 *width = (int)bounds.size.width ;
222 *height = (int)bounds.size.height;
228 *width = (int)bounds.size.height ;
230 *height = (int)bounds.size.width;
234 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
236 return new wxOSXTimerImpl(timer);
239 int gs_wxBusyCursorCount = 0;
240 extern wxCursor gMacCurrentCursor;
241 wxCursor gMacStoredActiveCursor;
243 // Set the cursor to the busy cursor for all windows
244 void wxBeginBusyCursor(const wxCursor *cursor)
246 if (gs_wxBusyCursorCount++ == 0)
248 gMacStoredActiveCursor = gMacCurrentCursor;
249 cursor->MacInstall();
251 wxSetCursor(*cursor);
253 //else: nothing to do, already set
256 // Restore cursor to normal
257 void wxEndBusyCursor()
259 wxCHECK_RET( gs_wxBusyCursorCount > 0,
260 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
262 if (--gs_wxBusyCursorCount == 0)
264 gMacStoredActiveCursor.MacInstall();
265 gMacStoredActiveCursor = wxNullCursor;
267 wxSetCursor(wxNullCursor);
271 // true if we're between the above two calls
274 return (gs_wxBusyCursorCount > 0);
277 bool wxGetKeyState (wxKeyCode key)
282 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
284 // wxScreenDC is derived from wxWindowDC, so a screen dc will
285 // call this method when a Blit is performed with it as a source.
289 wxSize sz = m_window->GetSize();
291 int left = subrect != NULL ? subrect->x : 0 ;
292 int top = subrect != NULL ? subrect->y : 0 ;
293 int width = subrect != NULL ? subrect->width : sz.x;
294 int height = subrect != NULL ? subrect->height : sz.y ;
296 wxBitmap bmp = wxBitmap(width, height, 32);
298 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
300 CGContextSaveGState(context);
303 CGContextTranslateCTM( context, 0, height );
304 CGContextScaleCTM( context, 1, -1 );
307 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
309 UIGraphicsPushContext(context);
310 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
311 UIGraphicsPopContext();
312 CGContextRestoreGState(context);
319 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
324 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
326 if ( release.empty() ||
327 // TODO use wx method
328 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
330 // failed to get version string or unrecognized format
340 return wxOS_MAC_OSX_DARWIN;
343 wxString wxGetOsDescription()
345 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
351 #endif // wxOSX_USE_IPHONE