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