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 {
64 - (void)applicationWillTerminate:(UIApplication *)application {
66 wxTheApp->OnEndSession(event);
76 bool wxApp::CallOnInit()
81 bool wxApp::DoInitGui()
86 void wxApp::DoCleanUp()
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 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
123 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
124 if ( contextChanged )
125 UIGraphicsPushContext(context);
127 [text drawAtPoint:where withFont:font];
129 if ( contextChanged )
130 UIGraphicsPopContext();
133 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
135 return [text sizeWithFont:font];
138 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
141 CGRect r = [[UIScreen mainScreen] applicationFrame];
142 CGRect bounds = [[UIScreen mainScreen] bounds];
143 if ( bounds.size.height > r.size.height )
151 *width = r.size.width;
153 *height = r.size.height;
163 *width = r.size.height;
165 *height = r.size.width;
168 // it's easier to treat the status bar as an element of the toplevel window
169 // instead of the desktop in order to support easy rotation
174 wxDisplaySize(width, height);
178 void wxGetMousePosition( int* x, int* y )
186 wxMouseState wxGetMouseState()
192 // Returns depth of screen
195 return 32; // TODO can we determine this ?
198 // Get size of display
199 void wxDisplaySize(int *width, int *height)
201 CGRect r = [[UIScreen mainScreen] applicationFrame];
202 CGRect bounds = [[UIScreen mainScreen] bounds];
204 if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
208 *width = (int)bounds.size.width ;
210 *height = (int)bounds.size.height;
216 *width = (int)bounds.size.height ;
218 *height = (int)bounds.size.width;
222 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
224 return new wxOSXTimerImpl(timer);
227 int gs_wxBusyCursorCount = 0;
228 extern wxCursor gMacCurrentCursor;
229 wxCursor gMacStoredActiveCursor;
231 // Set the cursor to the busy cursor for all windows
232 void wxBeginBusyCursor(const wxCursor *cursor)
234 if (gs_wxBusyCursorCount++ == 0)
236 gMacStoredActiveCursor = gMacCurrentCursor;
237 cursor->MacInstall();
239 wxSetCursor(*cursor);
241 //else: nothing to do, already set
244 // Restore cursor to normal
245 void wxEndBusyCursor()
247 wxCHECK_RET( gs_wxBusyCursorCount > 0,
248 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
250 if (--gs_wxBusyCursorCount == 0)
252 gMacStoredActiveCursor.MacInstall();
253 gMacStoredActiveCursor = wxNullCursor;
255 wxSetCursor(wxNullCursor);
259 // true if we're between the above two calls
262 return (gs_wxBusyCursorCount > 0);
265 bool wxGetKeyState (wxKeyCode key)
270 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
272 // wxScreenDC is derived from wxWindowDC, so a screen dc will
273 // call this method when a Blit is performed with it as a source.
277 wxSize sz = m_window->GetSize();
279 int left = subrect != NULL ? subrect->x : 0 ;
280 int top = subrect != NULL ? subrect->y : 0 ;
281 int width = subrect != NULL ? subrect->width : sz.x;
282 int height = subrect != NULL ? subrect->height : sz.y ;
284 wxBitmap bmp = wxBitmap(width, height, 32);
286 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
288 CGContextSaveGState(context);
291 CGContextTranslateCTM( context, 0, height );
292 CGContextScaleCTM( context, 1, -1 );
295 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
297 UIGraphicsPushContext(context);
298 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
299 UIGraphicsPopContext();
300 CGContextRestoreGState(context);
307 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
312 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
314 if ( release.empty() ||
315 // TODO use wx method
316 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
318 // failed to get version string or unrecognized format
328 return wxOS_MAC_OSX_DARWIN;
331 wxString wxGetOsDescription()
333 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
339 #endif // wxOSX_USE_IPHONE