1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/utils.mm
3 // Purpose: various cocoa utility functions
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: utils.mm 48805 2007-09-19 14:52:25Z SC $
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>
45 // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
46 AudioServicesPlayAlertSound(0x00001000 );
49 // ----------------------------------------------------------------------------
50 // Common Event Support
51 // ----------------------------------------------------------------------------
53 @interface wxAppDelegate : NSObject <UIApplicationDelegate> {
58 @implementation wxAppDelegate
60 - (void)applicationDidFinishLaunching:(UIApplication *)application {
72 bool wxApp::CallOnInit()
79 wxMacAutoreleasePool pool;
80 const char* appname = "app";
81 UIApplicationMain( 1, (char**) &appname, nil, @"wxAppDelegate" );
85 bool wxApp::DoInitGui()
90 void wxApp::DoCleanUp()
103 // ----------------------------------------------------------------------------
104 // Launch default browser
105 // ----------------------------------------------------------------------------
107 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
109 return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
115 extern wxFont* CreateNormalFont()
117 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
120 extern wxFont* CreateSmallFont()
122 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
125 extern UIFont* CreateUIFont( const wxFont& font )
127 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
130 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
132 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
133 if ( contextChanged )
134 UIGraphicsPushContext(context);
136 [text drawAtPoint:where withFont:font];
138 if ( contextChanged )
139 UIGraphicsPopContext();
142 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
144 return [text sizeWithFont:font];
147 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
150 CGRect r = [[UIScreen mainScreen] applicationFrame];
151 CGRect bounds = [[UIScreen mainScreen] bounds];
152 if ( bounds.size.height > r.size.height )
160 *width = r.size.width;
162 *height = r.size.height;
172 *width = r.size.height;
174 *height = r.size.width;
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
183 wxDisplaySize(width, height);
187 void wxGetMousePosition( int* x, int* y )
195 wxMouseState wxGetMouseState()
201 // Returns depth of screen
204 return 32; // TODO can we determine this ?
207 // Get size of display
208 void wxDisplaySize(int *width, int *height)
210 CGRect r = [[UIScreen mainScreen] applicationFrame];
211 CGRect bounds = [[UIScreen mainScreen] bounds];
213 if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
217 *width = (int)bounds.size.width ;
219 *height = (int)bounds.size.height;
225 *width = (int)bounds.size.height ;
227 *height = (int)bounds.size.width;
231 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
233 return new wxOSXTimerImpl(timer);
236 int gs_wxBusyCursorCount = 0;
237 extern wxCursor gMacCurrentCursor;
238 wxCursor gMacStoredActiveCursor;
240 // Set the cursor to the busy cursor for all windows
241 void wxBeginBusyCursor(const wxCursor *cursor)
243 if (gs_wxBusyCursorCount++ == 0)
245 gMacStoredActiveCursor = gMacCurrentCursor;
246 cursor->MacInstall();
248 wxSetCursor(*cursor);
250 //else: nothing to do, already set
253 // Restore cursor to normal
254 void wxEndBusyCursor()
256 wxCHECK_RET( gs_wxBusyCursorCount > 0,
257 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
259 if (--gs_wxBusyCursorCount == 0)
261 gMacStoredActiveCursor.MacInstall();
262 gMacStoredActiveCursor = wxNullCursor;
264 wxSetCursor(wxNullCursor);
268 // true if we're between the above two calls
271 return (gs_wxBusyCursorCount > 0);
274 bool wxGetKeyState (wxKeyCode key)
279 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
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.
286 wxSize sz = m_window->GetSize();
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 ;
293 wxBitmap bmp = wxBitmap(width, height, 32);
295 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
297 CGContextSaveGState(context);
300 CGContextTranslateCTM( context, 0, height );
301 CGContextScaleCTM( context, 1, -1 );
304 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
306 UIGraphicsPushContext(context);
307 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
308 UIGraphicsPopContext();
309 CGContextRestoreGState(context);
316 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
321 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
323 if ( release.empty() ||
324 // TODO use wx method
325 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
327 // failed to get version string or unrecognized format
337 return wxOS_MAC_OSX_DARWIN;
340 wxString wxGetOsDescription()
342 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
348 #endif // wxOSX_USE_IPHONE