]> git.saurik.com Git - wxWidgets.git/blob - src/osx/iphone/nonownedwnd.mm
Use the best instead of current page size in OS X preferences dialog.
[wxWidgets.git] / src / osx / iphone / nonownedwnd.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/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$
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 : UIView
59 {
60 wxUIContentViewController* _controller;
61 }
62
63 - (void) setController: (wxUIContentViewController*) controller;
64 - (wxUIContentViewController*) 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 m_initialShowSent = false;
81 }
82
83 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl()
84 {
85 m_macWindow = NULL;
86 m_macFullScreenData = NULL;
87 m_initialShowSent = false;
88 }
89
90 wxNonOwnedWindowIPhoneImpl::~wxNonOwnedWindowIPhoneImpl()
91 {
92 [m_macWindow release];
93 }
94
95 void wxNonOwnedWindowIPhoneImpl::WillBeDestroyed()
96 {
97 }
98
99 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
100 long style, long extraStyle, const wxString& name )
101 {
102 m_macWindow = [UIWindow alloc];
103
104 UIWindowLevel level = UIWindowLevelNormal;
105
106 // most styles are not supported on the iphone
107
108 if ( style & wxFRAME_TOOL_WINDOW )
109 {
110 level = UIWindowLevelAlert; ;
111 }
112 else if ( ( style & wxPOPUP_WINDOW ) )
113 {
114 level = UIWindowLevelAlert;;
115 }
116 else if ( ( style & wxCAPTION ) )
117 {
118 }
119 else if ( ( style & wxFRAME_DRAWER ) )
120 {
121 }
122 else
123 {
124 }
125
126 if ( ( style & wxSTAY_ON_TOP ) )
127 level = UIWindowLevelAlert;
128 CGRect r = CGRectMake( 0, 0, size.x, size.y) ;
129 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
130 std::swap(r.size.width,r.size.height);
131
132 [m_macWindow initWithFrame:r ];
133 [m_macWindow setHidden:YES];
134
135 [m_macWindow setWindowLevel:level];
136 }
137
138 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
139 {
140 m_macWindow = nativeWindow;
141 }
142
143
144 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
145 {
146 return m_macWindow;
147 }
148
149 void wxNonOwnedWindowIPhoneImpl::Raise()
150 {
151 }
152
153 void wxNonOwnedWindowIPhoneImpl::Lower()
154 {
155 }
156
157 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
158 {
159 [m_macWindow setHidden:(show ? NO : YES)];
160 if ( show )
161 {
162 if ( !m_initialShowSent )
163 {
164 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (GetWXPeer());
165 wxShowEvent eventShow(now->GetId(), true);
166 eventShow.SetEventObject(now);
167
168 now->HandleWindowEvent(eventShow);
169
170 m_initialShowSent = true;
171 }
172 //[m_macWindow orderFront: self];
173 [m_macWindow makeKeyWindow];
174 }
175 return true;
176 }
177
178 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
179 {
180 return Show(show);
181 }
182
183 void wxNonOwnedWindowIPhoneImpl::Update()
184 {
185 // TODO [m_macWindow displayIfNeeded];
186 }
187
188 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
189 {
190 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
191 return true;
192 }
193
194 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
195 {
196 return true;
197 }
198
199 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
200 {
201 // no special styles supported
202 }
203
204 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
205 {
206 return true;
207 }
208
209 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
210 {
211 return true;
212 }
213
214 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
215 {
216 CGRect r = CGRectMake( x,y,width,height) ;
217 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
218 std::swap(r.size.width,r.size.height);
219
220 [m_macWindow setFrame:r];
221 }
222
223 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
224 {
225 CGRect r = [m_macWindow frame];
226 x = r.origin.x;
227 y = r.origin.y;
228 }
229
230 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
231 {
232 CGRect r = [m_macWindow frame];
233 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
234 std::swap(r.size.width,r.size.height);
235 width = r.size.width;
236 height = r.size.height;
237 }
238
239 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
240 {
241 CGRect r = [m_macWindow bounds];
242 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
243 std::swap(r.size.width,r.size.height);
244 width = r.size.width;
245 height = r.size.height;
246 left = r.origin.x;
247 top = r.origin.y;
248 }
249
250 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
251 {
252 return false;
253 }
254
255 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
256 {
257 // TODO change title of app ?
258 }
259
260 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
261 {
262 return false;
263 }
264
265 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
266 {
267 return false;
268 }
269
270 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
271 {
272 }
273
274 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
275 {
276 if ( maximize )
277 {
278 CGRect r = [[UIScreen mainScreen] bounds];
279 [m_macWindow setFrame:r];
280 }
281 }
282
283 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
284 {
285 return m_macFullScreenData != NULL ;
286 }
287
288 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
289 {
290 return true;
291 }
292
293 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
294 {
295 }
296
297 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
298 {
299 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
300 p = [m_macWindow convertPoint:p fromWindow:nil];
301 if ( x )
302 *x = p.x;
303 if ( y )
304 *y = p.y;
305 }
306
307 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
308 {
309 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
310 p = [m_macWindow convertPoint:p toWindow:nil];
311 if ( x )
312 *x = p.x;
313 if ( y )
314 *y = p.y;
315 }
316
317 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
318 {
319 wxNonOwnedWindowIPhoneImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
320 now->Create( parent, nativeWindow );
321 return now;
322 }
323
324
325 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
326 long style, long extraStyle, const wxString& name )
327 {
328 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
329 now->Create( parent, pos, size, style , extraStyle, name );
330 return now;
331 }
332
333 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
334 {
335 UIWindow* toplevelwindow = now->GetWXWindow();
336 CGRect frame = [toplevelwindow bounds];
337 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
338 BOOL fullscreen = now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] == UIStatusBarStyleBlackTranslucent;
339
340 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:( fullscreen ? frame : appframe ) ];
341 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
342 wxUIContentViewController* controller = [[wxUIContentViewController alloc] initWithNibName:nil bundle:nil];
343
344 #ifdef __IPHONE_3_0
345 controller.wantsFullScreenLayout = fullscreen;
346 #endif
347
348 controller.view = contentview;
349 [contentview release];
350 [contentview setController:controller];
351 [contentview setHidden:YES];
352
353 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
354 impl->InstallEventHandler();
355
356 if ([toplevelwindow respondsToSelector:@selector(setRootViewController:)])
357 {
358 toplevelwindow.rootViewController = controller;
359 }
360 else
361 {
362 [toplevelwindow addSubview:contentview];
363 }
364 return impl;
365 }
366
367 //
368 // obj-c impl
369 //
370
371 @implementation wxUIContentView
372
373 - (void) setController: (wxUIContentViewController*) controller
374 {
375 _controller = controller;
376 }
377
378 - (wxUIContentViewController*) controller
379 {
380 return _controller;
381 }
382
383 + (void)initialize
384 {
385 static BOOL initialized = NO;
386 if (!initialized)
387 {
388 initialized = YES;
389 wxOSXIPhoneClassAddWXMethods( self );
390 }
391 }
392
393 @end
394
395 @implementation wxUIContentViewController
396
397 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
398 {
399 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
400 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
401
402 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
403
404 return YES;
405 }
406
407 // iOS 6 support, right now unconditionally supporting all orientations, TODO use a orientation mask
408
409 -(BOOL)shouldAutorotate
410 {
411 return YES;
412 }
413
414 - (NSUInteger)supportedInterfaceOrientations
415 {
416 return UIInterfaceOrientationMaskAll;
417 }
418
419
420
421
422 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
423 {
424 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
425 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
426
427 now->HandleResized(0);
428 }
429
430 -(void) viewWillAppear:(BOOL)animated
431 {
432 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
433 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
434 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
435
436 if ( nowimpl->InitialShowEventSent() )
437 {
438 wxShowEvent eventShow(now->GetId(), true);
439 eventShow.SetEventObject(now);
440
441 now->HandleWindowEvent(eventShow);
442 }
443 }
444
445 -(void) viewWillDisappear:(BOOL)animated
446 {
447 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
448 if( impl )
449 {
450 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
451 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
452
453 if ( nowimpl->InitialShowEventSent() )
454 {
455 wxShowEvent eventShow(now->GetId(), false);
456 eventShow.SetEventObject(now);
457
458 now->HandleWindowEvent(eventShow);
459 }
460 }
461 }
462
463 -(void) dealloc
464 {
465 [super dealloc];
466 }
467
468 - (UIView*) rotatingFooterView
469 {
470 UIView* footerView = [super rotatingFooterView];
471 if ( footerView == nil )
472 {
473 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
474 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
475 if ( frame && frame->GetToolBar())
476 {
477 footerView = frame->GetToolBar()->GetHandle();
478 }
479 }
480 return footerView;
481 }
482
483 @end
484
485
486