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 char* appname = "test";
81 UIApplicationMain( 1, &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];
145 *width = r.size.width;
147 *height = r.size.height;
151 void wxGetMousePosition( int* x, int* y )
153 // wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
156 // Returns depth of screen
159 return 32; // TODO can we determine this ?
162 // Get size of display
163 void wxDisplaySize(int *width, int *height)
165 CGRect bounds = [[UIScreen mainScreen] bounds];
168 *width = (int)bounds.size.width ;
170 *height = (int)bounds.size.height;
173 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
175 return new wxOSXTimerImpl(timer);
178 int gs_wxBusyCursorCount = 0;
179 extern wxCursor gMacCurrentCursor;
180 wxCursor gMacStoredActiveCursor;
182 // Set the cursor to the busy cursor for all windows
183 void wxBeginBusyCursor(const wxCursor *cursor)
185 if (gs_wxBusyCursorCount++ == 0)
187 gMacStoredActiveCursor = gMacCurrentCursor;
188 cursor->MacInstall();
190 wxSetCursor(*cursor);
192 //else: nothing to do, already set
195 // Restore cursor to normal
196 void wxEndBusyCursor()
198 wxCHECK_RET( gs_wxBusyCursorCount > 0,
199 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
201 if (--gs_wxBusyCursorCount == 0)
203 gMacStoredActiveCursor.MacInstall();
204 gMacStoredActiveCursor = wxNullCursor;
206 wxSetCursor(wxNullCursor);
210 // true if we're between the above two calls
213 return (gs_wxBusyCursorCount > 0);
216 bool wxGetKeyState (wxKeyCode key)
221 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
223 // wxScreenDC is derived from wxWindowDC, so a screen dc will
224 // call this method when a Blit is performed with it as a source.
228 wxSize sz = m_window->GetSize();
230 int left = subrect != NULL ? subrect->x : 0 ;
231 int top = subrect != NULL ? subrect->y : 0 ;
232 int width = subrect != NULL ? subrect->width : sz.x;
233 int height = subrect != NULL ? subrect->height : sz.y ;
235 wxBitmap bmp = wxBitmap(width, height, 32);
237 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
239 CGContextSaveGState(context);
242 CGContextTranslateCTM( context, 0, height );
243 CGContextScaleCTM( context, 1, -1 );
246 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
248 UIGraphicsPushContext(context);
249 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
250 UIGraphicsPopContext();
251 CGContextRestoreGState(context);
258 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
263 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
265 if ( release.empty() ||
266 // TODO use wx method
267 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
269 // failed to get version string or unrecognized format
279 return wxOS_MAC_OSX_DARWIN;
282 wxString wxGetOsDescription()
284 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
290 #endif // wxOSX_USE_IPHONE