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: (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];
139 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
144 void wxNonOwnedWindowIPhoneImpl::Raise()
148 void wxNonOwnedWindowIPhoneImpl::Lower()
152 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
154 [m_macWindow setHidden:(show ? NO : YES)];
157 if ( !m_initialShowSent )
159 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (GetWXPeer());
160 wxShowEvent eventShow(now->GetId(), true);
161 eventShow.SetEventObject(now);
163 now->HandleWindowEvent(eventShow);
165 m_initialShowSent = true;
167 //[m_macWindow orderFront: self];
168 [m_macWindow makeKeyWindow];
173 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
178 void wxNonOwnedWindowIPhoneImpl::Update()
180 // TODO [m_macWindow displayIfNeeded];
183 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
185 [m_macWindow setAlpha:(CGFloat) alpha/255.0];
189 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
194 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
196 // no special styles supported
199 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
204 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
209 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
211 CGRect r = CGRectMake( x,y,width,height) ;
212 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
213 std::swap(r.size.width,r.size.height);
215 [m_macWindow setFrame:r];
218 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
220 CGRect r = [m_macWindow frame];
225 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
227 CGRect r = [m_macWindow frame];
228 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
229 std::swap(r.size.width,r.size.height);
230 width = r.size.width;
231 height = r.size.height;
234 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
236 CGRect r = [m_macWindow bounds];
237 if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
238 std::swap(r.size.width,r.size.height);
239 width = r.size.width;
240 height = r.size.height;
245 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
250 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
252 // TODO change title of app ?
255 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
260 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
265 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
269 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
273 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
275 return m_macFullScreenData != NULL ;
278 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
283 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
287 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
289 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
290 p = [m_macWindow convertPoint:p fromWindow:nil];
297 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
299 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
300 p = [m_macWindow convertPoint:p toWindow:nil];
307 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
308 long style, long extraStyle, const wxString& name )
310 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
311 now->Create( parent, pos, size, style , extraStyle, name );
315 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
317 UIWindow* toplevelwindow = now->GetWXWindow();
318 CGRect frame = [toplevelwindow bounds];
319 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
320 BOOL fullscreen = now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] == UIStatusBarStyleBlackTranslucent;
322 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:( fullscreen ? frame : appframe ) ];
323 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
324 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
325 controller.wantsFullScreenLayout = fullscreen;
326 controller.view = contentview;
327 [contentview release];
328 [contentview setController:controller];
329 [contentview setHidden:YES];
331 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
332 impl->InstallEventHandler();
333 [toplevelwindow addSubview:contentview];
341 @implementation wxUIContentView
343 - (void) setController: (wxUIContentViewController*) controller
345 _controller = controller;
348 - (wxUIContentViewController*) controller
355 @implementation wxUIContentViewController
357 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
359 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
360 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
362 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
367 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
369 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
370 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
372 now->HandleResized(0);
375 -(void) viewWillAppear:(BOOL)animated
377 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
378 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
379 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
381 if ( nowimpl->InitialShowEventSent() )
383 wxShowEvent eventShow(now->GetId(), true);
384 eventShow.SetEventObject(now);
386 now->HandleWindowEvent(eventShow);
390 -(void) viewWillDisappear:(BOOL)animated
392 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
393 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
394 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
396 if ( nowimpl->InitialShowEventSent() )
398 wxShowEvent eventShow(now->GetId(), false);
399 eventShow.SetEventObject(now);
401 now->HandleWindowEvent(eventShow);
410 - (UIView*) rotatingFooterView
412 UIView* footerView = [super rotatingFooterView];
413 if ( footerView == nil )
415 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
416 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
417 if ( frame && frame->GetToolBar())
419 footerView = frame->GetToolBar()->GetHandle();