committing current iphone state
[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     char* appname = "test";
81     UIApplicationMain( 1, &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     CGRect r = [[UIScreen mainScreen] applicationFrame];
140     if ( x )
141         *x = r.origin.x;
142     if ( y )
143         *y = r.origin.y;
144     if ( width )
145         *width = r.size.width;
146     if ( height )
147         *height = r.size.height;
148     
149 }
150
151 void wxGetMousePosition( int* x, int* y )
152 {
153 //    wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
154 };
155
156 // Returns depth of screen
157 int wxDisplayDepth()
158 {
159     return 32; // TODO can we determine this ?
160 }
161
162 // Get size of display
163 void wxDisplaySize(int *width, int *height)
164 {
165     CGRect bounds = [[UIScreen mainScreen] bounds];
166     
167     if ( width )
168         *width = (int)bounds.size.width ;
169     if ( height )
170         *height = (int)bounds.size.height;
171 }
172
173 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
174 {
175     return new wxOSXTimerImpl(timer);
176 }
177
178 int gs_wxBusyCursorCount = 0;
179 extern wxCursor    gMacCurrentCursor;
180 wxCursor        gMacStoredActiveCursor;
181
182 // Set the cursor to the busy cursor for all windows
183 void wxBeginBusyCursor(const wxCursor *cursor)
184 {
185     if (gs_wxBusyCursorCount++ == 0)
186     {
187         gMacStoredActiveCursor = gMacCurrentCursor;
188         cursor->MacInstall();
189
190         wxSetCursor(*cursor);
191     }
192     //else: nothing to do, already set
193 }
194
195 // Restore cursor to normal
196 void wxEndBusyCursor()
197 {
198     wxCHECK_RET( gs_wxBusyCursorCount > 0,
199         wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
200
201     if (--gs_wxBusyCursorCount == 0)
202     {
203         gMacStoredActiveCursor.MacInstall();
204         gMacStoredActiveCursor = wxNullCursor;
205
206         wxSetCursor(wxNullCursor);
207     }
208 }
209
210 // true if we're between the above two calls
211 bool wxIsBusy()
212 {
213     return (gs_wxBusyCursorCount > 0);
214 }
215
216 bool wxGetKeyState (wxKeyCode key)
217 {
218     return false;
219 }
220
221 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
222 {
223     // wxScreenDC is derived from wxWindowDC, so a screen dc will
224     // call this method when a Blit is performed with it as a source.
225     if (!m_window)
226         return wxNullBitmap;
227         
228     wxSize sz = m_window->GetSize();
229     
230     int left = subrect != NULL ? subrect->x : 0 ;
231     int top = subrect != NULL ? subrect->y : 0 ;
232     int width = subrect != NULL ? subrect->width : sz.x;
233     int height = subrect !=  NULL ? subrect->height : sz.y ;
234     
235     wxBitmap bmp = wxBitmap(width, height, 32);
236     
237     CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
238     
239     CGContextSaveGState(context);
240     
241     
242     CGContextTranslateCTM( context, 0,  height );
243     CGContextScaleCTM( context, 1, -1 );
244
245     if ( subrect )
246         CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
247
248     UIGraphicsPushContext(context);
249     [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
250     UIGraphicsPopContext();
251     CGContextRestoreGState(context);
252
253     return bmp;
254 }
255
256 #endif // wxUSE_GUI
257
258 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
259 {
260     // get OS version
261     int major, minor;
262
263     wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
264
265     if ( release.empty() ||
266         // TODO use wx method
267          scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
268     {
269         // failed to get version string or unrecognized format
270         major =
271         minor = -1;
272     }
273
274     if ( verMaj )
275         *verMaj = major;
276     if ( verMin )
277         *verMin = minor;
278
279     return wxOS_MAC_OSX_DARWIN;
280 }
281
282 wxString wxGetOsDescription()
283 {
284     wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
285
286     return release;
287 }
288
289
290 #endif // wxOSX_USE_IPHONE