]> git.saurik.com Git - wxWidgets.git/blob - src/osx/iphone/nonownedwnd.mm
supporting auto-rotate for translucent statusbar
[wxWidgets.git] / src / osx / iphone / nonownedwnd.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for iphone
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2008-06-20
7 // RCS-ID: $Id: nonownedwnd.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/osx/private.h"
15
16 #include "wx/nonownedwnd.h"
17 #include "wx/frame.h"
18 #include <algorithm>
19
20 CGRect wxToNSRect(UIView* parent, const wxRect& r )
21 {
22 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
23 int y = r.y;
24 int x = r.x ;
25 return CGRectMake(x, y, r.width , r.height);
26 }
27
28 wxRect wxFromNSRect( UIView* parent, const CGRect& rect )
29 {
30 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
31 int y = rect.origin.y;
32 int x = rect.origin.x;
33 return wxRect( x, y, rect.size.width, rect.size.height );
34 }
35
36 CGPoint wxToNSPoint( UIView* parent, const wxPoint& p )
37 {
38 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
39 int x = p.x ;
40 int y = p.y;
41 return CGPointMake(x, y);
42 }
43
44 wxPoint wxFromNSPoint( UIView* parent, const CGPoint& p )
45 {
46 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
47 int x = p.x;
48 int y = p.y;
49 return wxPoint( x, y);
50 }
51
52 @interface wxUIContentViewController : UIViewController
53 {
54 }
55
56 @end
57
58 @interface wxUIContentView : wxUIView
59 {
60 wxUIContentViewController* _controller;
61 }
62
63 - (void) setController: (UIViewController*) controller;
64 - (UIViewController*) controller;
65 @end
66
67
68
69 //
70 // c++ impl
71 //
72
73 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowIPhoneImpl , wxNonOwnedWindowImpl )
74
75 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl( wxNonOwnedWindow* nonownedwnd) :
76 wxNonOwnedWindowImpl(nonownedwnd)
77 {
78 m_macWindow = NULL;
79 m_macFullScreenData = NULL;
80 }
81
82 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl()
83 {
84 m_macWindow = NULL;
85 m_macFullScreenData = NULL;
86 }
87
88 wxNonOwnedWindowIPhoneImpl::~wxNonOwnedWindowIPhoneImpl()
89 {
90 [m_macWindow release];
91 }
92
93 void wxNonOwnedWindowIPhoneImpl::WillBeDestroyed()
94 {
95 }
96
97 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
98 long style, long extraStyle, const wxString& name )
99 {
100 m_macWindow = [UIWindow alloc];
101
102 UIWindowLevel level = UIWindowLevelNormal;
103
104 // most styles are not supported on the iphone
105
106 if ( style & wxFRAME_TOOL_WINDOW )
107 {
108 level = UIWindowLevelAlert; ;
109 }
110 else if ( ( style & wxPOPUP_WINDOW ) )
111 {
112 level = UIWindowLevelAlert;;
113 }
114 else if ( ( style & wxCAPTION ) )
115 {
116 }
117 else if ( ( style & wxFRAME_DRAWER ) )
118 {
119 }
120 else
121 {
122 }
123
124 if ( ( style & wxSTAY_ON_TOP ) )
125 level = UIWindowLevelAlert;
126 CGRect r = CGRectMake( 0, 0, size.x, size.y) ;
127 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
128 std::swap(r.size.width,r.size.height);
129
130 [m_macWindow initWithFrame:r ];
131
132 [m_macWindow setWindowLevel:level];
133 // [m_macWindow makeKeyAndOrderFront:nil];
134 }
135
136
137 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
138 {
139 return m_macWindow;
140 }
141
142 void wxNonOwnedWindowIPhoneImpl::Raise()
143 {
144 }
145
146 void wxNonOwnedWindowIPhoneImpl::Lower()
147 {
148 }
149
150 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
151 {
152 [m_macWindow setHidden:(show ? NO : YES)];
153 if ( show )
154 {
155 //[m_macWindow orderFront: self];
156 [m_macWindow makeKeyWindow];
157 }
158 return true;
159 }
160
161 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
162 {
163 return Show(show);
164 }
165
166 void wxNonOwnedWindowIPhoneImpl::Update()
167 {
168 // TODO [m_macWindow displayIfNeeded];
169 }
170
171 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
172 {
173 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
174 return true;
175 }
176
177 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
178 {
179 return true;
180 }
181
182 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
183 {
184 // no special styles supported
185 }
186
187 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
188 {
189 return true;
190 }
191
192 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
193 {
194 return true;
195 }
196
197 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
198 {
199 CGRect r = CGRectMake( x,y,width,height) ;
200 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
201 std::swap(r.size.width,r.size.height);
202
203 [m_macWindow setFrame:r];
204 }
205
206 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
207 {
208 CGRect r = [m_macWindow frame];
209 x = r.origin.x;
210 y = r.origin.y;
211 }
212
213 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
214 {
215 CGRect r = [m_macWindow frame];
216 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
217 std::swap(r.size.width,r.size.height);
218 width = r.size.width;
219 height = r.size.height;
220 }
221
222 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
223 {
224 CGRect r = [m_macWindow bounds];
225 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
226 std::swap(r.size.width,r.size.height);
227 width = r.size.width;
228 height = r.size.height;
229 left = r.origin.x;
230 top = r.origin.y;
231 }
232
233 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
234 {
235 return false;
236 }
237
238 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
239 {
240 // TODO change title of app ?
241 }
242
243 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
244 {
245 return false;
246 }
247
248 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
249 {
250 return false;
251 }
252
253 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
254 {
255 }
256
257 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
258 {
259 }
260
261 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
262 {
263 return m_macFullScreenData != NULL ;
264 }
265
266 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
267 {
268 return true;
269 }
270
271 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
272 {
273 }
274
275 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
276 {
277 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
278 p = [m_macWindow convertPoint:p fromWindow:nil];
279 if ( x )
280 *x = p.x;
281 if ( y )
282 *y = p.y;
283 }
284
285 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
286 {
287 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
288 p = [m_macWindow convertPoint:p toWindow:nil];
289 if ( x )
290 *x = p.x;
291 if ( y )
292 *y = p.y;
293 }
294
295 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
296 long style, long extraStyle, const wxString& name )
297 {
298 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
299 now->Create( parent, pos, size, style , extraStyle, name );
300 return now;
301 }
302
303 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
304 {
305 UIWindow* toplevelwindow = now->GetWXWindow();
306 CGRect frame = [toplevelwindow bounds];
307 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
308
309 if ( now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] != UIStatusBarStyleBlackTranslucent)
310 {
311 double offset = appframe.origin.y;
312 frame.origin.y += offset;
313 frame.size.height -= offset;
314 }
315
316 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:frame];
317 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
318 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
319 controller.view = contentview;
320 [contentview setController:controller];
321
322 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
323 impl->InstallEventHandler();
324 [toplevelwindow addSubview:contentview];
325 return impl;
326 }
327
328 //
329 // obj-c impl
330 //
331
332 @implementation wxUIContentView
333
334 - (void) setController: (UIViewController*) controller
335 {
336 _controller = controller;
337 }
338
339 - (UIViewController*) controller
340 {
341 return _controller;
342 }
343
344 @end
345
346 @implementation wxUIContentViewController
347
348 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
349 {
350 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
351 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
352
353 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
354
355 return YES;
356 }
357
358 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
359 {
360 CGRect frame = [self.view frame];
361 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
362 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
363
364 if ( now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] == UIStatusBarStyleBlackTranslucent)
365 {
366 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
367 if ( CGRectEqualToRect(appframe, frame) )
368 {
369 if ( appframe.origin.y != 0 )
370 {
371 double offset = appframe.origin.y;
372 frame.origin.y -= offset;
373 frame.size.height += offset;
374 }
375 else
376 {
377 double offset = appframe.origin.x;
378 frame.origin.x -= offset;
379 frame.size.width += offset;
380 }
381
382 [self.view setFrame:frame];
383 }
384 }
385
386 now->HandleResized(0);
387 }
388
389 -(void) dealloc
390 {
391 [super dealloc];
392 }
393
394 - (UIView*) rotatingFooterView
395 {
396 UIView* footerView = [super rotatingFooterView];
397 if ( footerView == nil )
398 {
399 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
400 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
401 if ( frame && frame->GetToolBar())
402 {
403 footerView = frame->GetToolBar()->GetHandle();
404 }
405 }
406 }
407
408 @end
409
410
411