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