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" );
96 extern wxFont* CreateNormalFont()
98 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
101 extern wxFont* CreateSmallFont()
103 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
106 extern UIFont* CreateUIFont( const wxFont& font )
108 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
111 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
113 bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
114 if ( contextChanged )
115 UIGraphicsPushContext(context);
117 [text drawAtPoint:where withFont:font];
119 if ( contextChanged )
120 UIGraphicsPopContext();
123 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
125 return [text sizeWithFont:font];
128 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
130 CGRect r = [[UIScreen mainScreen] applicationFrame];
136 *width = r.size.width;
138 *height = r.size.height;
142 void wxGetMousePosition( int* x, int* y )
144 // wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
147 // Returns depth of screen
150 return 32; // TODO can we determine this ?
153 // Get size of display
154 void wxDisplaySize(int *width, int *height)
156 CGRect bounds = [[UIScreen mainScreen] bounds];
159 *width = (int)bounds.size.width ;
161 *height = (int)bounds.size.height;
164 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
166 return new wxOSXTimerImpl(timer);
169 int gs_wxBusyCursorCount = 0;
170 extern wxCursor gMacCurrentCursor;
171 wxCursor gMacStoredActiveCursor;
173 // Set the cursor to the busy cursor for all windows
174 void wxBeginBusyCursor(const wxCursor *cursor)
176 if (gs_wxBusyCursorCount++ == 0)
178 gMacStoredActiveCursor = gMacCurrentCursor;
179 cursor->MacInstall();
181 wxSetCursor(*cursor);
183 //else: nothing to do, already set
186 // Restore cursor to normal
187 void wxEndBusyCursor()
189 wxCHECK_RET( gs_wxBusyCursorCount > 0,
190 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
192 if (--gs_wxBusyCursorCount == 0)
194 gMacStoredActiveCursor.MacInstall();
195 gMacStoredActiveCursor = wxNullCursor;
197 wxSetCursor(wxNullCursor);
201 // true if we're between the above two calls
204 return (gs_wxBusyCursorCount > 0);
207 bool wxGetKeyState (wxKeyCode key)
212 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
214 // wxScreenDC is derived from wxWindowDC, so a screen dc will
215 // call this method when a Blit is performed with it as a source.
219 wxSize sz = m_window->GetSize();
221 int left = subrect != NULL ? subrect->x : 0 ;
222 int top = subrect != NULL ? subrect->y : 0 ;
223 int width = subrect != NULL ? subrect->width : sz.x;
224 int height = subrect != NULL ? subrect->height : sz.y ;
226 wxBitmap bmp = wxBitmap(width, height, 32);
228 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
230 CGContextSaveGState(context);
233 CGContextTranslateCTM( context, 0, height );
234 CGContextScaleCTM( context, 1, -1 );
237 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
239 UIGraphicsPushContext(context);
240 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
241 UIGraphicsPopContext();
242 CGContextRestoreGState(context);
249 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
254 wxString release = wxCFStringRef( [ [UIDevice currentDevice] systemVersion] ).AsString() ;
256 if ( release.empty() ||
257 wxSscanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
259 // failed to get version string or unrecognized format
269 return wxOS_MAC_OSX_DARWIN;
272 wxString wxGetOsDescription()
274 wxString release = wxCFStringRef( [ [UIDevice currentDevice] systemName] ).AsString() ;
280 #endif // wxOSX_USE_IPHONE