1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/nonownedwnd.mm
3 // Purpose: non owned window for iphone
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/osx/private.h"
15 #include "wx/nonownedwnd.h"
19 CGRect wxToNSRect(UIView* parent, const wxRect& r )
21 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
24 return CGRectMake(x, y, r.width , r.height);
27 wxRect wxFromNSRect( UIView* parent, const CGRect& rect )
29 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
30 int y = rect.origin.y;
31 int x = rect.origin.x;
32 return wxRect( x, y, rect.size.width, rect.size.height );
35 CGPoint wxToNSPoint( UIView* parent, const wxPoint& p )
37 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
40 return CGPointMake(x, y);
43 wxPoint wxFromNSPoint( UIView* parent, const CGPoint& p )
45 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
48 return wxPoint( x, y);
51 @interface wxUIContentViewController : UIViewController
57 @interface wxUIContentView : UIView
59 wxUIContentViewController* _controller;
62 - (void) setController: (wxUIContentViewController*) controller;
63 - (wxUIContentViewController*) controller;
72 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowIPhoneImpl , wxNonOwnedWindowImpl )
74 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl( wxNonOwnedWindow* nonownedwnd) :
75 wxNonOwnedWindowImpl(nonownedwnd)
78 m_macFullScreenData = NULL;
79 m_initialShowSent = false;
82 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl()
85 m_macFullScreenData = NULL;
86 m_initialShowSent = false;
89 wxNonOwnedWindowIPhoneImpl::~wxNonOwnedWindowIPhoneImpl()
91 [m_macWindow release];
94 void wxNonOwnedWindowIPhoneImpl::WillBeDestroyed()
98 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
99 long style, long extraStyle, const wxString& name )
101 m_macWindow = [UIWindow alloc];
103 UIWindowLevel level = UIWindowLevelNormal;
105 // most styles are not supported on the iphone
107 if ( style & wxFRAME_TOOL_WINDOW )
109 level = UIWindowLevelAlert; ;
111 else if ( ( style & wxPOPUP_WINDOW ) )
113 level = UIWindowLevelAlert;;
115 else if ( ( style & wxCAPTION ) )
118 else if ( ( style & wxFRAME_DRAWER ) )
125 if ( ( style & wxSTAY_ON_TOP ) )
126 level = UIWindowLevelAlert;
127 CGRect r = CGRectMake( 0, 0, size.x, size.y) ;
128 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
129 std::swap(r.size.width,r.size.height);
131 [m_macWindow initWithFrame:r ];
132 [m_macWindow setHidden:YES];
134 [m_macWindow setWindowLevel:level];
137 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
139 m_macWindow = nativeWindow;
143 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
148 void wxNonOwnedWindowIPhoneImpl::Raise()
152 void wxNonOwnedWindowIPhoneImpl::Lower()
156 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
158 [m_macWindow setHidden:(show ? NO : YES)];
161 if ( !m_initialShowSent )
163 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (GetWXPeer());
164 wxShowEvent eventShow(now->GetId(), true);
165 eventShow.SetEventObject(now);
167 now->HandleWindowEvent(eventShow);
169 m_initialShowSent = true;
171 //[m_macWindow orderFront: self];
172 [m_macWindow makeKeyWindow];
177 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
182 void wxNonOwnedWindowIPhoneImpl::Update()
184 // TODO [m_macWindow displayIfNeeded];
187 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
189 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
193 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
198 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
200 // no special styles supported
203 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
208 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
213 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
215 CGRect r = CGRectMake( x,y,width,height) ;
216 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
217 std::swap(r.size.width,r.size.height);
219 [m_macWindow setFrame:r];
222 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
224 CGRect r = [m_macWindow frame];
229 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
231 CGRect r = [m_macWindow frame];
232 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
233 std::swap(r.size.width,r.size.height);
234 width = r.size.width;
235 height = r.size.height;
238 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
240 CGRect r = [m_macWindow bounds];
241 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
242 std::swap(r.size.width,r.size.height);
243 width = r.size.width;
244 height = r.size.height;
249 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
254 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
256 // TODO change title of app ?
259 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
264 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
269 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
273 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
277 CGRect r = [[UIScreen mainScreen] bounds];
278 [m_macWindow setFrame:r];
282 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
284 return m_macFullScreenData != NULL ;
287 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
292 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
296 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
298 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
299 p = [m_macWindow convertPoint:p fromWindow:nil];
306 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
308 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
309 p = [m_macWindow convertPoint:p toWindow:nil];
316 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
318 wxNonOwnedWindowIPhoneImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
319 now->Create( parent, nativeWindow );
324 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
325 long style, long extraStyle, const wxString& name )
327 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
328 now->Create( parent, pos, size, style , extraStyle, name );
332 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
334 UIWindow* toplevelwindow = now->GetWXWindow();
335 CGRect frame = [toplevelwindow bounds];
336 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
337 BOOL fullscreen = now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] == UIStatusBarStyleBlackTranslucent;
339 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:( fullscreen ? frame : appframe ) ];
340 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
341 wxUIContentViewController* controller = [[wxUIContentViewController alloc] initWithNibName:nil bundle:nil];
344 controller.wantsFullScreenLayout = fullscreen;
347 controller.view = contentview;
348 [contentview release];
349 [contentview setController:controller];
350 [contentview setHidden:YES];
352 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
353 impl->InstallEventHandler();
355 if ([toplevelwindow respondsToSelector:@selector(setRootViewController:)])
357 toplevelwindow.rootViewController = controller;
361 [toplevelwindow addSubview:contentview];
370 @implementation wxUIContentView
372 - (void) setController: (wxUIContentViewController*) controller
374 _controller = controller;
377 - (wxUIContentViewController*) controller
384 static BOOL initialized = NO;
388 wxOSXIPhoneClassAddWXMethods( self );
394 @implementation wxUIContentViewController
396 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
398 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
399 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
401 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
406 // iOS 6 support, right now unconditionally supporting all orientations, TODO use a orientation mask
408 -(BOOL)shouldAutorotate
413 - (NSUInteger)supportedInterfaceOrientations
415 return UIInterfaceOrientationMaskAll;
421 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
423 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
424 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
426 now->HandleResized(0);
429 -(void) viewWillAppear:(BOOL)animated
431 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
432 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
433 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
435 if ( nowimpl->InitialShowEventSent() )
437 wxShowEvent eventShow(now->GetId(), true);
438 eventShow.SetEventObject(now);
440 now->HandleWindowEvent(eventShow);
444 -(void) viewWillDisappear:(BOOL)animated
446 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
449 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
450 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
452 if ( nowimpl->InitialShowEventSent() )
454 wxShowEvent eventShow(now->GetId(), false);
455 eventShow.SetEventObject(now);
457 now->HandleWindowEvent(eventShow);
467 - (UIView*) rotatingFooterView
469 UIView* footerView = [super rotatingFooterView];
470 if ( footerView == nil )
472 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
473 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
474 if ( frame && frame->GetToolBar())
476 footerView = frame->GetToolBar()->GetHandle();