The rounded corners look really dumb at this size.
[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 // Copyright:   (c) Stefan Csomor
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/wxprec.h"
14
15 #include "wx/utils.h"
16
17 #ifndef WX_PRECOMP
18     #include "wx/intl.h"
19     #include "wx/app.h"
20     #if wxUSE_GUI
21         #include "wx/toplevel.h"
22         #include "wx/font.h"
23     #endif
24 #endif
25
26 #include "wx/apptrait.h"
27
28 #include "wx/osx/private.h"
29
30 #if wxUSE_GUI
31     #include "wx/osx/private/timer.h"
32     #include "wx/osx/dcclient.h"
33 #endif // wxUSE_GUI
34
35 #if wxOSX_USE_IPHONE
36
37 #include <AudioToolbox/AudioServices.h>
38
39 #if 1 // wxUSE_BASE
40
41 // ----------------------------------------------------------------------------
42 // Common Event Support
43 // ----------------------------------------------------------------------------
44
45 @interface wxAppDelegate : NSObject <UIApplicationDelegate> {
46 }
47
48 @end
49
50 @implementation wxAppDelegate
51
52 - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
53 {
54     wxUnusedVar(application);
55     wxUnusedVar(launchOptions);
56     wxTheApp->OSXOnWillFinishLaunching();
57     return YES;
58 }
59
60 - (void)applicationDidFinishLaunching:(UIApplication *)application {    
61     wxTheApp->OSXOnDidFinishLaunching();
62 }
63
64 - (void)applicationWillTerminate:(UIApplication *)application { 
65     wxUnusedVar(application);
66     wxTheApp->OSXOnWillTerminate();
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 // Emit a beeeeeep
95 void wxBell()
96 {
97     // would be kSystemSoundID_UserPreferredAlert but since the headers aren't correct, add it manually
98     AudioServicesPlayAlertSound(0x00001000 );
99 }
100
101 // ----------------------------------------------------------------------------
102 // Launch default browser
103 // ----------------------------------------------------------------------------
104
105 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
106 {
107     return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:wxCFStringRef(url).AsNSString()]]
108         == YES;
109 }
110
111 // TODO : reorganize
112
113 extern wxFont* CreateNormalFont()
114 {
115     return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
116 }
117
118 extern wxFont* CreateSmallFont()
119 {
120     return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
121 }
122
123 extern UIFont* CreateUIFont( const wxFont& font )
124 {
125     return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
126 }
127
128 CFArrayRef CopyAvailableFontFamilyNames()
129 {
130     return (CFArrayRef) [[UIFont familyNames] retain];
131 }
132
133 extern void DrawTextInContext( CGContextRef context, CGPoint where, UIFont *font, NSString* text )
134 {
135     bool contextChanged = ( UIGraphicsGetCurrentContext() != context );
136     if ( contextChanged )
137         UIGraphicsPushContext(context);
138
139     [text drawAtPoint:where withFont:font];
140
141     if ( contextChanged )
142         UIGraphicsPopContext();
143 }
144
145 extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
146 {
147     return  [text sizeWithFont:font];
148 }
149
150 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
151 {
152 #if 0
153     CGRect r = [[UIScreen mainScreen] applicationFrame];
154     CGRect bounds = [[UIScreen mainScreen] bounds];
155     if ( bounds.size.height > r.size.height )
156     {
157         // portrait
158         if ( x )
159             *x = r.origin.x;
160         if ( y )
161             *y = r.origin.y;
162         if ( width )
163             *width = r.size.width;
164         if ( height )
165             *height = r.size.height;
166     }
167     else
168     {
169         // landscape
170         if ( x )
171             *x = r.origin.y;
172         if ( y )
173             *y = r.origin.x;
174         if ( width )
175             *width = r.size.height;
176         if ( height )
177             *height = r.size.width;
178     }
179 #else
180     // it's easier to treat the status bar as an element of the toplevel window 
181     // instead of the desktop in order to support easy rotation
182     if ( x )
183         *x = 0;
184     if ( y )
185         *y = 0;
186     wxDisplaySize(width, height);
187 #endif
188 }
189
190 void wxGetMousePosition( int* x, int* y )
191 {
192     if ( x )
193         *x = 0;
194     if ( y )
195         *y = 0;
196 };
197
198 wxMouseState wxGetMouseState()
199 {
200     wxMouseState ms;
201     return ms;
202 }    
203
204 // Returns depth of screen
205 int wxDisplayDepth()
206 {
207     return 32; // TODO can we determine this ?
208 }
209
210 // Get size of display
211 void wxDisplaySize(int *width, int *height)
212 {
213     CGRect r = [[UIScreen mainScreen] applicationFrame];
214     CGRect bounds = [[UIScreen mainScreen] bounds];
215
216     if ( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) )
217     {
218         // portrait
219         if ( width )
220             *width = (int)bounds.size.width ;
221         if ( height )
222             *height = (int)bounds.size.height;
223     }
224     else
225     {
226         // landscape
227         if ( width )
228             *width = (int)bounds.size.height ;
229         if ( height )
230             *height = (int)bounds.size.width;
231     }
232 }
233
234 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
235 {
236     return new wxOSXTimerImpl(timer);
237 }
238
239 int gs_wxBusyCursorCount = 0;
240 extern wxCursor    gMacCurrentCursor;
241 wxCursor        gMacStoredActiveCursor;
242
243 // Set the cursor to the busy cursor for all windows
244 void wxBeginBusyCursor(const wxCursor *cursor)
245 {
246     if (gs_wxBusyCursorCount++ == 0)
247     {
248         gMacStoredActiveCursor = gMacCurrentCursor;
249         cursor->MacInstall();
250
251         wxSetCursor(*cursor);
252     }
253     //else: nothing to do, already set
254 }
255
256 // Restore cursor to normal
257 void wxEndBusyCursor()
258 {
259     wxCHECK_RET( gs_wxBusyCursorCount > 0,
260         wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
261
262     if (--gs_wxBusyCursorCount == 0)
263     {
264         gMacStoredActiveCursor.MacInstall();
265         gMacStoredActiveCursor = wxNullCursor;
266
267         wxSetCursor(wxNullCursor);
268     }
269 }
270
271 // true if we're between the above two calls
272 bool wxIsBusy()
273 {
274     return (gs_wxBusyCursorCount > 0);
275 }
276
277 bool wxGetKeyState (wxKeyCode key)
278 {
279     return false;
280 }
281
282 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
283 {
284     // wxScreenDC is derived from wxWindowDC, so a screen dc will
285     // call this method when a Blit is performed with it as a source.
286     if (!m_window)
287         return wxNullBitmap;
288
289     wxSize sz = m_window->GetSize();
290
291     int left = subrect != NULL ? subrect->x : 0 ;
292     int top = subrect != NULL ? subrect->y : 0 ;
293     int width = subrect != NULL ? subrect->width : sz.x;
294     int height = subrect !=  NULL ? subrect->height : sz.y ;
295
296     wxBitmap bmp = wxBitmap(width, height, 32);
297
298     CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
299
300     CGContextSaveGState(context);
301
302
303     CGContextTranslateCTM( context, 0,  height );
304     CGContextScaleCTM( context, 1, -1 );
305
306     if ( subrect )
307         CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
308
309     UIGraphicsPushContext(context);
310     [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
311     UIGraphicsPopContext();
312     CGContextRestoreGState(context);
313
314     return bmp;
315 }
316
317 #endif // wxUSE_GUI
318
319 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
320 {
321     // get OS version
322     int major, minor;
323
324     wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
325
326     if ( release.empty() ||
327         // TODO use wx method
328          scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
329     {
330         // failed to get version string or unrecognized format
331         major =
332         minor = -1;
333     }
334
335     if ( verMaj )
336         *verMaj = major;
337     if ( verMin )
338         *verMin = minor;
339
340     return wxOS_MAC_OSX_DARWIN;
341 }
342
343 wxString wxGetOsDescription()
344 {
345     wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
346
347     return release;
348 }
349
350
351 #endif // wxOSX_USE_IPHONE