]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/iphone/utils.mm
switching to explicit flush mode, otherwise clientdc destruction lead to contention...
[wxWidgets.git] / src / osx / iphone / utils.mm
... / ...
CommitLineData
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
43void 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
72bool wxApp::CallOnInit()
73{
74 return true;
75}
76
77int wxApp::OnRun()
78{
79 wxMacAutoreleasePool pool;
80 const char* appname = "app";
81 UIApplicationMain( 1, (char**) &appname, nil, @"wxAppDelegate" );
82 return 1;
83}
84
85bool wxApp::DoInitGui()
86{
87 return true;
88}
89
90void wxApp::DoCleanUp()
91{
92}
93
94void wxMacWakeUp()
95{
96 // TODO
97}
98
99#endif // wxUSE_BASE
100
101#if wxUSE_GUI
102
103// TODO : reorganize
104
105extern wxFont* CreateNormalFont()
106{
107 return new wxFont([UIFont systemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
108}
109
110extern wxFont* CreateSmallFont()
111{
112 return new wxFont([UIFont smallSystemFontSize] , wxSWISS, wxNORMAL, wxNORMAL, FALSE, "Helvetica" );
113}
114
115extern UIFont* CreateUIFont( const wxFont& font )
116{
117 return [UIFont fontWithName:wxCFStringRef(font.GetFaceName() ).AsNSString() size:font.GetPointSize()];
118}
119
120extern 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
132extern CGSize MeasureTextInContext( UIFont *font, NSString* text )
133{
134 return [text sizeWithFont:font];
135}
136
137void wxClientDisplayRect(int *x, int *y, int *width, int *height)
138{
139 CGRect r = [[UIScreen mainScreen] applicationFrame];
140 CGRect bounds = [[UIScreen mainScreen] bounds];
141 if ( bounds.size.height > r.size.height )
142 {
143 // portrait
144 if ( x )
145 *x = r.origin.x;
146 if ( y )
147 *y = r.origin.y;
148 if ( width )
149 *width = r.size.width;
150 if ( height )
151 *height = r.size.height;
152 }
153 else
154 {
155 // landscape
156 if ( x )
157 *x = r.origin.y;
158 if ( y )
159 *y = r.origin.x;
160 if ( width )
161 *width = r.size.height;
162 if ( height )
163 *height = r.size.width;
164 }
165}
166
167void wxGetMousePosition( int* x, int* y )
168{
169// wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
170};
171
172// Returns depth of screen
173int wxDisplayDepth()
174{
175 return 32; // TODO can we determine this ?
176}
177
178// Get size of display
179void wxDisplaySize(int *width, int *height)
180{
181 CGRect r = [[UIScreen mainScreen] applicationFrame];
182 CGRect bounds = [[UIScreen mainScreen] bounds];
183
184 if ( bounds.size.height > r.size.height )
185 {
186 // portrait
187 if ( width )
188 *width = (int)bounds.size.width ;
189 if ( height )
190 *height = (int)bounds.size.height;
191 }
192 else
193 {
194 // landscape
195 if ( width )
196 *width = (int)bounds.size.height ;
197 if ( height )
198 *height = (int)bounds.size.width;
199 }
200}
201
202wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
203{
204 return new wxOSXTimerImpl(timer);
205}
206
207int gs_wxBusyCursorCount = 0;
208extern wxCursor gMacCurrentCursor;
209wxCursor gMacStoredActiveCursor;
210
211// Set the cursor to the busy cursor for all windows
212void wxBeginBusyCursor(const wxCursor *cursor)
213{
214 if (gs_wxBusyCursorCount++ == 0)
215 {
216 gMacStoredActiveCursor = gMacCurrentCursor;
217 cursor->MacInstall();
218
219 wxSetCursor(*cursor);
220 }
221 //else: nothing to do, already set
222}
223
224// Restore cursor to normal
225void wxEndBusyCursor()
226{
227 wxCHECK_RET( gs_wxBusyCursorCount > 0,
228 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
229
230 if (--gs_wxBusyCursorCount == 0)
231 {
232 gMacStoredActiveCursor.MacInstall();
233 gMacStoredActiveCursor = wxNullCursor;
234
235 wxSetCursor(wxNullCursor);
236 }
237}
238
239// true if we're between the above two calls
240bool wxIsBusy()
241{
242 return (gs_wxBusyCursorCount > 0);
243}
244
245bool wxGetKeyState (wxKeyCode key)
246{
247 return false;
248}
249
250wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
251{
252 // wxScreenDC is derived from wxWindowDC, so a screen dc will
253 // call this method when a Blit is performed with it as a source.
254 if (!m_window)
255 return wxNullBitmap;
256
257 wxSize sz = m_window->GetSize();
258
259 int left = subrect != NULL ? subrect->x : 0 ;
260 int top = subrect != NULL ? subrect->y : 0 ;
261 int width = subrect != NULL ? subrect->width : sz.x;
262 int height = subrect != NULL ? subrect->height : sz.y ;
263
264 wxBitmap bmp = wxBitmap(width, height, 32);
265
266 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
267
268 CGContextSaveGState(context);
269
270
271 CGContextTranslateCTM( context, 0, height );
272 CGContextScaleCTM( context, 1, -1 );
273
274 if ( subrect )
275 CGContextTranslateCTM( context, -subrect->x, -subrect->y ) ;
276
277 UIGraphicsPushContext(context);
278 [ (NSView*) m_window->GetHandle() drawRect:CGRectMake(left, top, width, height ) ];
279 UIGraphicsPopContext();
280 CGContextRestoreGState(context);
281
282 return bmp;
283}
284
285#endif // wxUSE_GUI
286
287wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
288{
289 // get OS version
290 int major, minor;
291
292 wxString release = wxCFStringRef( wxCFRetain( [ [UIDevice currentDevice] systemVersion] ) ).AsString() ;
293
294 if ( release.empty() ||
295 // TODO use wx method
296 scanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 )
297 {
298 // failed to get version string or unrecognized format
299 major =
300 minor = -1;
301 }
302
303 if ( verMaj )
304 *verMaj = major;
305 if ( verMin )
306 *verMin = minor;
307
308 return wxOS_MAC_OSX_DARWIN;
309}
310
311wxString wxGetOsDescription()
312{
313 wxString release = wxCFStringRef( wxCFRetain([ [UIDevice currentDevice] systemName] )).AsString() ;
314
315 return release;
316}
317
318
319#endif // wxOSX_USE_IPHONE