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()
105 extern wxFont* CreateNormalFont()
107 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
110 extern wxFont* CreateSmallFont()
112 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
115 extern UIFont* CreateUIFont( const wxFont& font )
117 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
120 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
122 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
123 if ( contextChanged )
124 UIGraphicsPushContext(context);
126 [text drawAtPoint:where withFont:font];
128 if ( contextChanged )
129 UIGraphicsPopContext();
132 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
134 return [text sizeWithFont:font];
137 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
139 CGRect r = [[UIScreen mainScreen] applicationFrame];
140 CGRect bounds = [[UIScreen mainScreen] bounds];
141 if ( bounds.size.height > r.size.height )
149 *width = r.size.width;
151 *height = r.size.height;
161 *width = r.size.height;
163 *height = r.size.width;
167 void wxGetMousePosition( int* x, int* y )
169 // wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
172 // Returns depth of screen
175 return 32; // TODO can we determine this ?
178 // Get size of display
179 void wxDisplaySize(int *width, int *height)
181 CGRect r = [[UIScreen mainScreen] applicationFrame];
182 CGRect bounds = [[UIScreen mainScreen] bounds];
184 if ( bounds.size.height > r.size.height )
188 *width = (int)bounds.size.width ;
190 *height = (int)bounds.size.height;
196 *width = (int)bounds.size.height ;
198 *height = (int)bounds.size.width;
202 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
204 return new wxOSXTimerImpl(timer);
207 int gs_wxBusyCursorCount = 0;
208 extern wxCursor gMacCurrentCursor;
209 wxCursor gMacStoredActiveCursor;
211 // Set the cursor to the busy cursor for all windows
212 void wxBeginBusyCursor(const wxCursor *cursor)
214 if (gs_wxBusyCursorCount++ == 0)
216 gMacStoredActiveCursor = gMacCurrentCursor;
217 cursor->MacInstall();
219 wxSetCursor(*cursor);
221 //else: nothing to do, already set
224 // Restore cursor to normal
225 void wxEndBusyCursor()
227 wxCHECK_RET( gs_wxBusyCursorCount > 0,
228 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
230 if (--gs_wxBusyCursorCount == 0)
232 gMacStoredActiveCursor.MacInstall();
233 gMacStoredActiveCursor = wxNullCursor;
235 wxSetCursor(wxNullCursor);
239 // true if we're between the above two calls
242 return (gs_wxBusyCursorCount > 0);
245 bool wxGetKeyState (wxKeyCode key)
250 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
252 // wxScreenDC is derived from wxWindowDC, so a screen dc will
253 // call this method when a Blit is performed with it as a source.
257 wxSize sz = m_window->GetSize();
259 int left = subrect != NULL ? subrect->x : 0 ;
260 int top = subrect != NULL ? subrect->y : 0 ;
261 int width = subrect != NULL ? subrect->width : sz.x;
262 int height = subrect != NULL ? subrect->height : sz.y ;
264 wxBitmap bmp = wxBitmap(width, height, 32);
266 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
268 CGContextSaveGState(context);
271 CGContextTranslateCTM( context, 0, height );
272 CGContextScaleCTM( context, 1, -1 );
275 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
277 UIGraphicsPushContext(context);
278 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
279 UIGraphicsPopContext();
280 CGContextRestoreGState(context);
287 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
292 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
294 if ( release.empty() ||
295 // TODO use wx method
296 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
298 // failed to get version string or unrecognized format
308 return wxOS_MAC_OSX_DARWIN;
311 wxString wxGetOsDescription()
313 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
319 #endif // wxOSX_USE_IPHONE