updating implementations of wxGetMouseState for osx_cocoa and dummy impls for iphone
[wxWidgets.git] / src / osx / iphone / utils.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/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     const char* appname = "app";
81     UIApplicationMain( 1, (char**) &appname, nil, @"wxAppDelegate" );
82     return 1;
83 }
84
85 bool wxApp::DoInitGui()
86 {
87     return true;
88 }
89
90 void wxApp::DoCleanUp()
91 {
92 }
93
94 void wxMacWakeUp()
95 {
96     // TODO
97 }
98
99 #endif // wxUSE_BASE
100
101 #if wxUSE_GUI
102
103 // ----------------------------------------------------------------------------
104 // Launch default browser
105 // ----------------------------------------------------------------------------
106
107 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
108 {
109     return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
110         == YES;
111 }
112
113 // TODO : reorganize
114
115 extern wxFont* CreateNormalFont()
116 {
117     return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
118 }
119
120 extern wxFont* CreateSmallFont()
121 {
122     return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
123 }
124
125 extern UIFont* CreateUIFont( const wxFont& font )
126 {
127     return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
128 }
129
130 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
131 {
132     bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
133     if ( contextChanged )
134         UIGraphicsPushContext(context);
135
136     [text drawAtPoint:where withFont:font];
137
138     if ( contextChanged )
139         UIGraphicsPopContext();
140 }
141
142 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
143 {
144     return  [text sizeWithFont:font];
145 }
146
147 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
148 {
149 #if 0
150     CGRect r = [[UIScreen mainScreen] applicationFrame];
151     CGRect bounds = [[UIScreen mainScreen] bounds];
152     if ( bounds.size.height > r.size.height )
153     {
154         // portrait
155         if ( x )
156             *x = r.origin.x;
157         if ( y )
158             *y = r.origin.y;
159         if ( width )
160             *width = r.size.width;
161         if ( height )
162             *height = r.size.height;
163     }
164     else
165     {
166         // landscape
167         if ( x )
168             *x = r.origin.y;
169         if ( y )
170             *y = r.origin.x;
171         if ( width )
172             *width = r.size.height;
173         if ( height )
174             *height = r.size.width;
175     }
176 #else
177     // it's easier to treat the status bar as an element of the toplevel window 
178     // instead of the desktop in order to support easy rotation
179     if ( x )
180         *x = 0;
181     if ( y )
182         *y = 0;
183     wxDisplaySize(width, height);
184 #endif
185 }
186
187 void wxGetMousePosition( int* x, int* y )
188 {
189     if ( x )
190         *x = 0;
191     if ( y )
192         *y = 0;
193 };
194
195 wxMouseState wxGetMouseState()
196 {
197     wxMouseState ms;
198     return ms;
199 }    
200
201 // Returns depth of screen
202 int wxDisplayDepth()
203 {
204     return 32; // TODO can we determine this ?
205 }
206
207 // Get size of display
208 void wxDisplaySize(int *width, int *height)
209 {
210     CGRect r = [[UIScreen mainScreen] applicationFrame];
211     CGRect bounds = [[UIScreen mainScreen] bounds];
212
213     if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
214     {
215         // portrait
216         if ( width )
217             *width = (int)bounds.size.width ;
218         if ( height )
219             *height = (int)bounds.size.height;
220     }
221     else
222     {
223         // landscape
224         if ( width )
225             *width = (int)bounds.size.height ;
226         if ( height )
227             *height = (int)bounds.size.width;
228     }
229 }
230
231 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
232 {
233     return new wxOSXTimerImpl(timer);
234 }
235
236 int gs_wxBusyCursorCount = 0;
237 extern wxCursor    gMacCurrentCursor;
238 wxCursor        gMacStoredActiveCursor;
239
240 // Set the cursor to the busy cursor for all windows
241 void wxBeginBusyCursor(const wxCursor *cursor)
242 {
243     if (gs_wxBusyCursorCount++ == 0)
244     {
245         gMacStoredActiveCursor = gMacCurrentCursor;
246         cursor->MacInstall();
247
248         wxSetCursor(*cursor);
249     }
250     //else: nothing to do, already set
251 }
252
253 // Restore cursor to normal
254 void wxEndBusyCursor()
255 {
256     wxCHECK_RET( gs_wxBusyCursorCount > 0,
257         wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
258
259     if (--gs_wxBusyCursorCount == 0)
260     {
261         gMacStoredActiveCursor.MacInstall();
262         gMacStoredActiveCursor = wxNullCursor;
263
264         wxSetCursor(wxNullCursor);
265     }
266 }
267
268 // true if we're between the above two calls
269 bool wxIsBusy()
270 {
271     return (gs_wxBusyCursorCount > 0);
272 }
273
274 bool wxGetKeyState (wxKeyCode key)
275 {
276     return false;
277 }
278
279 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
280 {
281     // wxScreenDC is derived from wxWindowDC, so a screen dc will
282     // call this method when a Blit is performed with it as a source.
283     if (!m_window)
284         return wxNullBitmap;
285
286     wxSize sz = m_window->GetSize();
287
288     int left = subrect != NULL ? subrect->x : 0 ;
289     int top = subrect != NULL ? subrect->y : 0 ;
290     int width = subrect != NULL ? subrect->width : sz.x;
291     int height = subrect !=  NULL ? subrect->height : sz.y ;
292
293     wxBitmap bmp = wxBitmap(width, height, 32);
294
295     CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
296
297     CGContextSaveGState(context);
298
299
300     CGContextTranslateCTM( context, 0,  height );
301     CGContextScaleCTM( context, 1, -1 );
302
303     if ( subrect )
304         CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
305
306     UIGraphicsPushContext(context);
307     [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
308     UIGraphicsPopContext();
309     CGContextRestoreGState(context);
310
311     return bmp;
312 }
313
314 #endif // wxUSE_GUI
315
316 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
317 {
318     // get OS version
319     int major, minor;
320
321     wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
322
323     if ( release.empty() ||
324         // TODO use wx method
325          scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
326     {
327         // failed to get version string or unrecognized format
328         major =
329         minor = -1;
330     }
331
332     if ( verMaj )
333         *verMaj = major;
334     if ( verMin )
335         *verMin = minor;
336
337     return wxOS_MAC_OSX_DARWIN;
338 }
339
340 wxString wxGetOsDescription()
341 {
342     wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
343
344     return release;
345 }
346
347
348 #endif // wxOSX_USE_IPHONE