1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for iphone
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: nonownedwnd.mm 48805 2007-09-19 14:52:25Z SC $
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 : wxUIView
60 wxUIContentViewController* _controller;
63 - (void) setController: (UIViewController*) controller;
64 - (UIViewController*) controller;
73 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowIPhoneImpl , wxNonOwnedWindowImpl )
75 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl( wxNonOwnedWindow* nonownedwnd) :
76 wxNonOwnedWindowImpl(nonownedwnd)
79 m_macFullScreenData = NULL;
82 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl()
85 m_macFullScreenData = NULL;
88 wxNonOwnedWindowIPhoneImpl::~wxNonOwnedWindowIPhoneImpl()
90 [m_macWindow release];
93 void wxNonOwnedWindowIPhoneImpl::WillBeDestroyed()
97 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
98 long style, long extraStyle, const wxString& name )
100 m_macWindow = [UIWindow alloc];
102 UIWindowLevel level = UIWindowLevelNormal;
104 // most styles are not supported on the iphone
106 if ( style & wxFRAME_TOOL_WINDOW )
108 level = UIWindowLevelAlert; ;
110 else if ( ( style & wxPOPUP_WINDOW ) )
112 level = UIWindowLevelAlert;;
114 else if ( ( style & wxCAPTION ) )
117 else if ( ( style & wxFRAME_DRAWER ) )
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);
130 [m_macWindow initWithFrame:r ];
132 [m_macWindow setWindowLevel:level];
133 // [m_macWindow makeKeyAndOrderFront:nil];
137 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
142 void wxNonOwnedWindowIPhoneImpl::Raise()
146 void wxNonOwnedWindowIPhoneImpl::Lower()
150 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
152 [m_macWindow setHidden:(show ? NO : YES)];
155 //[m_macWindow orderFront: self];
156 [m_macWindow makeKeyWindow];
161 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
166 void wxNonOwnedWindowIPhoneImpl::Update()
168 // TODO [m_macWindow displayIfNeeded];
171 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
173 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
177 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
182 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
184 // no special styles supported
187 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
192 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
197 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
199 CGRect r = CGRectMake( x,y,width,height) ;
200 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
201 std::swap(r.size.width,r.size.height);
203 [m_macWindow setFrame:r];
206 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
208 CGRect r = [m_macWindow frame];
213 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
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;
222 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
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;
233 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
238 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
240 // TODO change title of app ?
243 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
248 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
253 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
257 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
261 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
263 return m_macFullScreenData != NULL ;
266 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
271 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
275 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
277 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
278 p = [m_macWindow convertPoint:p fromWindow:nil];
285 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
287 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
288 p = [m_macWindow convertPoint:p toWindow:nil];
295 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
296 long style, long extraStyle, const wxString& name )
298 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
299 now->Create( parent, pos, size, style , extraStyle, name );
303 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
305 UIWindow* toplevelwindow = now->GetWXWindow();
306 CGRect frame = [toplevelwindow bounds];
307 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
309 if ( now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE )
311 double offset = appframe.origin.y;
312 frame.origin.y += offset;
313 frame.size.height -= offset;
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];
322 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
323 impl->InstallEventHandler();
324 [toplevelwindow addSubview:contentview];
332 @implementation wxUIContentView
334 - (void) setController: (UIViewController*) controller
336 _controller = controller;
339 - (UIViewController*) controller
346 @implementation wxUIContentViewController
348 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
350 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
351 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
353 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
358 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
360 CGRect fr = [self.view frame];
361 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
362 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
364 now->HandleResized(0);
372 - (UIView*) rotatingFooterView
374 UIView* footerView = [super rotatingFooterView];
375 if ( footerView == nil )
377 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
378 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
379 if ( frame && frame->GetToolBar())
381 footerView = frame->GetToolBar()->GetHandle();