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::Destroy()
95 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
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 ];
133 [m_macWindow setWindowLevel:level];
134 // [m_macWindow makeKeyAndOrderFront:nil];
138 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
143 void wxNonOwnedWindowIPhoneImpl::Raise()
147 void wxNonOwnedWindowIPhoneImpl::Lower()
151 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
153 [m_macWindow setHidden:(show ? NO : YES)];
156 //[m_macWindow orderFront: self];
157 [m_macWindow makeKeyWindow];
162 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
167 void wxNonOwnedWindowIPhoneImpl::Update()
169 // TODO [m_macWindow displayIfNeeded];
172 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
174 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
178 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
183 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
185 // no special styles supported
188 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
193 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
198 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
200 CGRect r = CGRectMake( x,y,width,height) ;
201 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
202 std::swap(r.size.width,r.size.height);
204 [m_macWindow setFrame:r];
207 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
209 CGRect r = [m_macWindow frame];
214 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
216 CGRect r = [m_macWindow frame];
217 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
218 std::swap(r.size.width,r.size.height);
219 width = r.size.width;
220 height = r.size.height;
223 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
225 CGRect r = [m_macWindow bounds];
226 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
227 std::swap(r.size.width,r.size.height);
228 width = r.size.width;
229 height = r.size.height;
234 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
239 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
241 // TODO change title of app ?
244 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
249 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
254 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
258 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
262 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
264 return m_macFullScreenData != NULL ;
267 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
272 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
276 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
278 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
279 p = [m_macWindow convertPoint:p fromWindow:nil];
286 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
288 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
289 p = [m_macWindow convertPoint:p toWindow:nil];
296 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
297 long style, long extraStyle, const wxString& name )
299 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
300 now->Create( parent, pos, size, style , extraStyle, name );
304 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
306 UIWindow* toplevelwindow = now->GetWXWindow();
307 CGRect frame = [toplevelwindow bounds];
308 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
310 if ( now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE )
312 double offset = appframe.origin.y;
313 frame.origin.y += offset;
314 frame.size.height -= offset;
317 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:frame];
318 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
319 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
320 controller.view = contentview;
321 [contentview setController:controller];
323 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
324 impl->InstallEventHandler();
325 [toplevelwindow addSubview:contentview];
333 @implementation wxUIContentView
335 - (void) setController: (UIViewController*) controller
337 _controller = controller;
340 - (UIViewController*) controller
347 @implementation wxUIContentViewController
349 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
351 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
352 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
354 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
359 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
361 CGRect fr = [self.view frame];
362 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
363 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
365 now->HandleResized(0);
373 - (UIView*) rotatingFooterView
375 UIView* footerView = [super rotatingFooterView];
376 if ( footerView == nil )
378 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
379 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
380 if ( frame && frame->GetToolBar())
382 footerView = frame->GetToolBar()->GetHandle();