universal implementation for iphone
[wxWidgets.git] / src / osx / iphone / utils.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/mac/cocoa/utils.mm
3 // Purpose:     various cocoa utility functions
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     1998-01-01
7 // RCS-ID:      $Id: utils.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright:   (c) Stefan Csomor
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/wxprec.h"
15
16 #include "wx/utils.h"
17
18 #ifndef WX_PRECOMP
19     #include "wx/intl.h"
20     #include "wx/app.h"
21     #if wxUSE_GUI
22         #include "wx/toplevel.h"
23         #include "wx/font.h"
24     #endif
25 #endif
26
27 #include "wx/apptrait.h"
28
29 #include "wx/osx/private.h"
30
31 #if wxUSE_GUI
32     #include "wx/osx/private/timer.h"
33     #include "wx/osx/dcclient.h"
34 #endif // wxUSE_GUI
35
36 #if wxOSX_USE_IPHONE
37
38 #include <AudioToolbox/AudioServices.h>
39
40 #if wxUSE_BASE
41
42 // Emit a beeeeeep
43 void wxBell()
44 {
45     // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
46     AudioServicesPlayAlertSound(0x00001000 );
47 }
48
49 // ----------------------------------------------------------------------------
50 // Common Event Support
51 // ----------------------------------------------------------------------------
52
53 @interface wxAppDelegate : NSObject <UIApplicationDelegate> {
54 }
55
56 @end
57
58 @implementation wxAppDelegate
59
60 - (void)applicationDidFinishLaunching:(UIApplication *)application {    
61         wxTheApp->OnInit();
62 }
63
64
65 - (void)dealloc {
66         [super dealloc];
67 }
68
69
70 @end
71
72 bool wxApp::CallOnInit()
73 {
74     return true;
75 }
76
77 int wxApp::OnRun()
78 {
79     wxMacAutoreleasePool pool;
80     char* appname = "test";
81     UIApplicationMain( 1, &appname, nil, @"wxAppDelegate" );
82     return 1;
83 }
84
85 void wxMacWakeUp()
86 {
87     // TODO
88 }
89
90 #endif // wxUSE_BASE
91
92 #if wxUSE_GUI
93
94 // TODO : reorganize
95
96 extern wxFont* CreateNormalFont()
97 {
98     return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
99 }
100
101 extern wxFont* CreateSmallFont()
102 {
103     return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
104 }
105
106 extern UIFont* CreateUIFont( const wxFont& font )
107 {
108     return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
109 }
110
111 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
112 {
113     bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
114     if ( contextChanged )
115         UIGraphicsPushContext(context);
116
117     [text drawAtPoint:where withFont:font];
118
119     if ( contextChanged )
120         UIGraphicsPopContext();
121 }
122
123 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
124 {
125     return  [text sizeWithFont:font];
126 }
127
128 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
129 {
130     CGRect r = [[UIScreen mainScreen] applicationFrame];
131     if ( x )
132         *x = r.origin.x;
133     if ( y )
134         *y = r.origin.y;
135     if ( width )
136         *width = r.size.width;
137     if ( height )
138         *height = r.size.height;
139     
140 }
141
142 void wxGetMousePosition( int* x, int* y )
143 {
144 //    wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
145 };
146
147 // Returns depth of screen
148 int wxDisplayDepth()
149 {
150     return 32; // TODO can we determine this ?
151 }
152
153 // Get size of display
154 void wxDisplaySize(int *width, int *height)
155 {
156     CGRect bounds = [[UIScreen mainScreen] bounds];
157     
158     if ( width )
159         *width = (int)bounds.size.width ;
160     if ( height )
161         *height = (int)bounds.size.height;
162 }
163
164 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
165 {
166     return new wxOSXTimerImpl(timer);
167 }
168
169 int gs_wxBusyCursorCount = 0;
170 extern wxCursor    gMacCurrentCursor;
171 wxCursor        gMacStoredActiveCursor;
172
173 // Set the cursor to the busy cursor for all windows
174 void wxBeginBusyCursor(const wxCursor *cursor)
175 {
176     if (gs_wxBusyCursorCount++ == 0)
177     {
178         gMacStoredActiveCursor = gMacCurrentCursor;
179         cursor->MacInstall();
180
181         wxSetCursor(*cursor);
182     }
183     //else: nothing to do, already set
184 }
185
186 // Restore cursor to normal
187 void wxEndBusyCursor()
188 {
189     wxCHECK_RET( gs_wxBusyCursorCount > 0,
190         wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
191
192     if (--gs_wxBusyCursorCount == 0)
193     {
194         gMacStoredActiveCursor.MacInstall();
195         gMacStoredActiveCursor = wxNullCursor;
196
197         wxSetCursor(wxNullCursor);
198     }
199 }
200
201 // true if we're between the above two calls
202 bool wxIsBusy()
203 {
204     return (gs_wxBusyCursorCount > 0);
205 }
206
207 bool wxGetKeyState (wxKeyCode key)
208 {
209     return false;
210 }
211
212 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
213 {
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.
216     if (!m_window)
217         return wxNullBitmap;
218         
219     wxSize sz = m_window->GetSize();
220     
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 ;
225     
226     wxBitmap bmp = wxBitmap(width, height, 32);
227     
228     CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
229     
230     CGContextSaveGState(context);
231     
232     
233     CGContextTranslateCTM( context, 0,  height );
234     CGContextScaleCTM( context, 1, -1 );
235
236     if ( subrect )
237         CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
238
239     UIGraphicsPushContext(context);
240     [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
241     UIGraphicsPopContext();
242     CGContextRestoreGState(context);
243
244     return bmp;
245 }
246
247 #endif // wxUSE_GUI
248
249 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
250 {
251     // get OS version
252     int major, minor;
253
254     wxString release = wxCFStringRef( [ [UIDevice currentDevice] systemVersion] ).AsString() ;
255
256     if ( release.empty() ||
257          wxSscanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
258     {
259         // failed to get version string or unrecognized format
260         major =
261         minor = -1;
262     }
263
264     if ( verMaj )
265         *verMaj = major;
266     if ( verMin )
267         *verMin = minor;
268
269     return wxOS_MAC_OSX_DARWIN;
270 }
271
272 wxString wxGetOsDescription()
273 {
274     wxString release = wxCFStringRef( [ [UIDevice currentDevice] systemName] ).AsString() ;
275
276     return release;
277 }
278
279
280 #endif // wxOSX_USE_IPHONE