1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/nonownedwnd.mm
3 // Purpose: non owned window for iphone
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/osx/private.h"
16 #include "wx/nonownedwnd.h"
20 CGRect wxToNSRect(UIView* parent, const wxRect& r )
22 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
25 return CGRectMake(x, y, r.width , r.height);
28 wxRect wxFromNSRect( UIView* parent, const CGRect& rect )
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 );
36 CGPoint wxToNSPoint( UIView* parent, const wxPoint& p )
38 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
41 return CGPointMake(x, y);
44 wxPoint wxFromNSPoint( UIView* parent, const CGPoint& p )
46 CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
49 return wxPoint( x, y);
52 @interface wxUIContentViewController : UIViewController
58 @interface wxUIContentView : UIView
60 wxUIContentViewController* _controller;
63 - (void) setController: (wxUIContentViewController*) controller;
64 - (wxUIContentViewController*) controller;
73 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowIPhoneImpl , wxNonOwnedWindowImpl )
75 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl( wxNonOwnedWindow* nonownedwnd) :
76 wxNonOwnedWindowImpl(nonownedwnd)
79 m_macFullScreenData = NULL;
80 m_initialShowSent = false;
83 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl()
86 m_macFullScreenData = NULL;
87 m_initialShowSent = false;
90 wxNonOwnedWindowIPhoneImpl::~wxNonOwnedWindowIPhoneImpl()
92 [m_macWindow release];
95 void wxNonOwnedWindowIPhoneImpl::WillBeDestroyed()
99 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
100 long style, long extraStyle, const wxString& name )
102 m_macWindow = [UIWindow alloc];
104 UIWindowLevel level = UIWindowLevelNormal;
106 // most styles are not supported on the iphone
108 if ( style & wxFRAME_TOOL_WINDOW )
110 level = UIWindowLevelAlert; ;
112 else if ( ( style & wxPOPUP_WINDOW ) )
114 level = UIWindowLevelAlert;;
116 else if ( ( style & wxCAPTION ) )
119 else if ( ( style & wxFRAME_DRAWER ) )
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);
132 [m_macWindow initWithFrame:r ];
133 [m_macWindow setHidden:YES];
135 [m_macWindow setWindowLevel:level];
138 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
140 m_macWindow = nativeWindow;
144 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
149 void wxNonOwnedWindowIPhoneImpl::Raise()
153 void wxNonOwnedWindowIPhoneImpl::Lower()
157 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
159 [m_macWindow setHidden:(show ? NO : YES)];
162 if ( !m_initialShowSent )
164 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (GetWXPeer());
165 wxShowEvent eventShow(now->GetId(), true);
166 eventShow.SetEventObject(now);
168 now->HandleWindowEvent(eventShow);
170 m_initialShowSent = true;
172 //[m_macWindow orderFront: self];
173 [m_macWindow makeKeyWindow];
178 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
183 void wxNonOwnedWindowIPhoneImpl::Update()
185 // TODO [m_macWindow displayIfNeeded];
188 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
190 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
194 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
199 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
201 // no special styles supported
204 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
209 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
214 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
216 CGRect r = CGRectMake( x,y,width,height) ;
217 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
218 std::swap(r.size.width,r.size.height);
220 [m_macWindow setFrame:r];
223 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
225 CGRect r = [m_macWindow frame];
230 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
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;
239 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
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;
250 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
255 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
257 // TODO change title of app ?
260 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
265 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
270 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
274 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
278 CGRect r = [[UIScreen mainScreen] bounds];
279 [m_macWindow setFrame:r];
283 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
285 return m_macFullScreenData != NULL ;
288 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
293 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
297 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
299 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
300 p = [m_macWindow convertPoint:p fromWindow:nil];
307 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
309 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
310 p = [m_macWindow convertPoint:p toWindow:nil];
317 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
319 wxNonOwnedWindowIPhoneImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
320 now->Create( parent, nativeWindow );
325 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
326 long style, long extraStyle, const wxString& name )
328 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
329 now->Create( parent, pos, size, style , extraStyle, name );
333 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
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;
340 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:( fullscreen ? frame : appframe ) ];
341 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
342 wxUIContentViewController* controller = [[wxUIContentViewController alloc] initWithNibName:nil bundle:nil];
345 controller.wantsFullScreenLayout = fullscreen;
348 controller.view = contentview;
349 [contentview release];
350 [contentview setController:controller];
351 [contentview setHidden:YES];
353 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
354 impl->InstallEventHandler();
355 [toplevelwindow addSubview:contentview];
363 @implementation wxUIContentView
365 - (void) setController: (wxUIContentViewController*) controller
367 _controller = controller;
370 - (wxUIContentViewController*) controller
377 static BOOL initialized = NO;
381 wxOSXIPhoneClassAddWXMethods( self );
387 @implementation wxUIContentViewController
389 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
391 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
392 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
394 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
399 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
401 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
402 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
404 now->HandleResized(0);
407 -(void) viewWillAppear:(BOOL)animated
409 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
410 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
411 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
413 if ( nowimpl->InitialShowEventSent() )
415 wxShowEvent eventShow(now->GetId(), true);
416 eventShow.SetEventObject(now);
418 now->HandleWindowEvent(eventShow);
422 -(void) viewWillDisappear:(BOOL)animated
424 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
427 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
428 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
430 if ( nowimpl->InitialShowEventSent() )
432 wxShowEvent eventShow(now->GetId(), false);
433 eventShow.SetEventObject(now);
435 now->HandleWindowEvent(eventShow);
445 - (UIView*) rotatingFooterView
447 UIView* footerView = [super rotatingFooterView];
448 if ( footerView == nil )
450 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
451 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
452 if ( frame && frame->GetToolBar())
454 footerView = frame->GetToolBar()->GetHandle();