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];
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 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
280 return m_macFullScreenData != NULL ;
283 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
288 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
292 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
294 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
295 p = [m_macWindow convertPoint:p fromWindow:nil];
302 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
304 CGPoint p = CGPointMake( (x ? *x : 0), (y ? *y : 0) );
305 p = [m_macWindow convertPoint:p toWindow:nil];
312 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
314 wxNonOwnedWindowIPhoneImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
315 now->Create( parent, nativeWindow );
320 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
321 long style, long extraStyle, const wxString& name )
323 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
324 now->Create( parent, pos, size, style , extraStyle, name );
328 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
330 UIWindow* toplevelwindow = now->GetWXWindow();
331 CGRect frame = [toplevelwindow bounds];
332 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
333 BOOL fullscreen = now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] == UIStatusBarStyleBlackTranslucent;
335 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:( fullscreen ? frame : appframe ) ];
336 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
337 wxUIContentViewController* controller = [[wxUIContentViewController alloc] initWithNibName:nil bundle:nil];
340 controller.wantsFullScreenLayout = fullscreen;
343 controller.view = contentview;
344 [contentview release];
345 [contentview setController:controller];
346 [contentview setHidden:YES];
348 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
349 impl->InstallEventHandler();
350 [toplevelwindow addSubview:contentview];
358 @implementation wxUIContentView
360 - (void) setController: (wxUIContentViewController*) controller
362 _controller = controller;
365 - (wxUIContentViewController*) controller
372 @implementation wxUIContentViewController
374 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
376 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
377 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
379 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
384 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
386 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
387 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
389 now->HandleResized(0);
392 -(void) viewWillAppear:(BOOL)animated
394 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
395 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
396 wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
398 if ( nowimpl->InitialShowEventSent() )
400 wxShowEvent eventShow(now->GetId(), true);
401 eventShow.SetEventObject(now);
403 now->HandleWindowEvent(eventShow);
407 -(void) viewWillDisappear:(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(), false);
416 eventShow.SetEventObject(now);
418 now->HandleWindowEvent(eventShow);
427 - (UIView*) rotatingFooterView
429 UIView* footerView = [super rotatingFooterView];
430 if ( footerView == nil )
432 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
433 wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
434 if ( frame && frame->GetToolBar())
436 footerView = frame->GetToolBar()->GetHandle();