Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / osx / iphone / nonownedwnd.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/iphone/nonownedwnd.mm
3 // Purpose:     non owned window for iphone
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     2008-06-20
7 // Copyright:   (c) Stefan Csomor
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/osx/private.h"
14
15 #include "wx/nonownedwnd.h"
16 #include "wx/frame.h"
17 #include <algorithm>
18
19 CGRect wxToNSRect(UIView* parent, const wxRect& r )
20 {
21     CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
22     int y = r.y;
23     int x = r.x ;
24     return CGRectMake(x, y, r.width , r.height);
25 }
26
27 wxRect wxFromNSRect( UIView* parent, const CGRect& rect )
28 {
29     CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
30     int y = rect.origin.y;
31     int x = rect.origin.x;
32     return wxRect( x, y, rect.size.width, rect.size.height );
33 }
34
35 CGPoint wxToNSPoint( UIView* parent, const wxPoint& p )
36 {
37     CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
38     int x = p.x ;
39     int y = p.y;
40     return CGPointMake(x, y);
41 }
42
43 wxPoint wxFromNSPoint( UIView* parent, const CGPoint& p )
44 {
45     CGRect frame = parent ? [parent bounds] : [[UIScreen mainScreen] bounds];
46     int x = p.x;
47     int y = p.y;
48     return wxPoint( x, y);
49 }
50
51 @interface wxUIContentViewController : UIViewController
52 {
53 }
54
55 @end
56
57 @interface wxUIContentView : UIView
58 {
59     wxUIContentViewController* _controller;
60 }
61
62 - (void) setController: (wxUIContentViewController*) controller;
63 - (wxUIContentViewController*) controller;
64 @end
65
66
67
68 //
69 // c++ impl
70 //
71
72 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowIPhoneImpl , wxNonOwnedWindowImpl )
73
74 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl( wxNonOwnedWindow* nonownedwnd) :
75     wxNonOwnedWindowImpl(nonownedwnd)
76 {
77     m_macWindow = NULL;
78     m_macFullScreenData = NULL;
79     m_initialShowSent = false;
80 }
81
82 wxNonOwnedWindowIPhoneImpl::wxNonOwnedWindowIPhoneImpl()
83 {
84     m_macWindow = NULL;
85     m_macFullScreenData = NULL;
86     m_initialShowSent = false;
87 }
88
89 wxNonOwnedWindowIPhoneImpl::~wxNonOwnedWindowIPhoneImpl()
90 {
91     [m_macWindow release];
92 }
93
94 void wxNonOwnedWindowIPhoneImpl::WillBeDestroyed()
95 {
96 }
97
98 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
99 long style, long extraStyle, const wxString& name )
100 {
101     m_macWindow = [UIWindow alloc];
102
103     UIWindowLevel level = UIWindowLevelNormal;
104
105     // most styles are not supported on the iphone
106
107     if ( style & wxFRAME_TOOL_WINDOW )
108     {
109         level = UIWindowLevelAlert; ;
110     }
111     else if ( ( style & wxPOPUP_WINDOW ) )
112     {
113         level = UIWindowLevelAlert;;
114     }
115     else if ( ( style & wxCAPTION ) )
116     {
117     }
118     else if ( ( style & wxFRAME_DRAWER ) )
119     {
120     }
121     else
122     {
123     }
124
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);
130
131     [m_macWindow initWithFrame:r ];
132     [m_macWindow setHidden:YES];
133
134     [m_macWindow setWindowLevel:level];
135 }
136
137 void wxNonOwnedWindowIPhoneImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
138 {
139     m_macWindow = nativeWindow;
140 }
141
142
143 WXWindow wxNonOwnedWindowIPhoneImpl::GetWXWindow() const
144 {
145     return m_macWindow;
146 }
147
148 void wxNonOwnedWindowIPhoneImpl::Raise()
149 {
150 }
151
152 void wxNonOwnedWindowIPhoneImpl::Lower()
153 {
154 }
155
156 bool wxNonOwnedWindowIPhoneImpl::Show(bool show)
157 {
158     [m_macWindow setHidden:(show ? NO : YES)];
159     if ( show )
160     {
161         if ( !m_initialShowSent )
162         {
163             wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (GetWXPeer());
164             wxShowEvent eventShow(now->GetId(), true);
165             eventShow.SetEventObject(now);
166             
167             now->HandleWindowEvent(eventShow);
168
169             m_initialShowSent = true;
170         }
171         //[m_macWindow orderFront: self];
172         [m_macWindow makeKeyWindow];
173     }
174     return true;
175 }
176
177 bool wxNonOwnedWindowIPhoneImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
178 {
179     return Show(show);
180 }
181
182 void wxNonOwnedWindowIPhoneImpl::Update()
183 {
184 //  TODO  [m_macWindow displayIfNeeded];
185 }
186
187 bool wxNonOwnedWindowIPhoneImpl::SetTransparent(wxByte alpha)
188 {
189     [m_macWindow setAlpha:(CGFloat) alpha/255.0];
190     return true;
191 }
192
193 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundColour(const wxColour& col )
194 {
195     return true;
196 }
197
198 void wxNonOwnedWindowIPhoneImpl::SetExtraStyle( long exStyle )
199 {
200     // no special styles supported
201 }
202
203 bool wxNonOwnedWindowIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
204 {
205     return true;
206 }
207
208 bool wxNonOwnedWindowIPhoneImpl::CanSetTransparent()
209 {
210     return true;
211 }
212
213 void wxNonOwnedWindowIPhoneImpl::MoveWindow(int x, int y, int width, int height)
214 {
215     CGRect r = CGRectMake( x,y,width,height) ;
216     if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
217         std::swap(r.size.width,r.size.height);
218
219     [m_macWindow setFrame:r];
220 }
221
222 void wxNonOwnedWindowIPhoneImpl::GetPosition( int &x, int &y ) const
223 {
224     CGRect r = [m_macWindow frame];
225     x = r.origin.x;
226     y = r.origin.y;
227 }
228
229 void wxNonOwnedWindowIPhoneImpl::GetSize( int &width, int &height ) const
230 {
231     CGRect r = [m_macWindow frame];
232     if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
233         std::swap(r.size.width,r.size.height);
234     width = r.size.width;
235     height = r.size.height;
236 }
237
238 void wxNonOwnedWindowIPhoneImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
239 {
240     CGRect r = [m_macWindow bounds];
241     if ( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) )
242         std::swap(r.size.width,r.size.height);
243     width = r.size.width;
244     height = r.size.height;
245     left = r.origin.x;
246     top = r.origin.y;
247 }
248
249 bool wxNonOwnedWindowIPhoneImpl::SetShape(const wxRegion& region)
250 {
251     return false;
252 }
253
254 void wxNonOwnedWindowIPhoneImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
255 {
256 // TODO change title of app ?
257 }
258
259 bool wxNonOwnedWindowIPhoneImpl::IsMaximized() const
260 {
261     return false;
262 }
263
264 bool wxNonOwnedWindowIPhoneImpl::IsIconized() const
265 {
266     return false;
267 }
268
269 void wxNonOwnedWindowIPhoneImpl::Iconize( bool iconize )
270 {
271 }
272
273 void wxNonOwnedWindowIPhoneImpl::Maximize(bool maximize)
274 {
275     if ( maximize )
276     {
277         CGRect r = [[UIScreen mainScreen] bounds];
278         [m_macWindow setFrame:r];
279     }
280 }
281
282 bool wxNonOwnedWindowIPhoneImpl::IsFullScreen() const
283 {
284     return m_macFullScreenData != NULL ;
285 }
286
287 bool wxNonOwnedWindowIPhoneImpl::ShowFullScreen(bool show, long style)
288 {
289     return true;
290 }
291
292 void wxNonOwnedWindowIPhoneImpl::RequestUserAttention(int WXUNUSED(flags))
293 {
294 }
295
296 void wxNonOwnedWindowIPhoneImpl::ScreenToWindow( int *x, int *y )
297 {
298     CGPoint p = CGPointMake(  (x ? *x : 0), (y ? *y : 0) );
299     p = [m_macWindow convertPoint:p fromWindow:nil];
300     if ( x )
301         *x = p.x;
302     if ( y )
303         *y = p.y;
304 }
305
306 void wxNonOwnedWindowIPhoneImpl::WindowToScreen( int *x, int *y )
307 {
308     CGPoint p = CGPointMake(  (x ? *x : 0), (y ? *y : 0) );
309     p = [m_macWindow convertPoint:p toWindow:nil];
310     if ( x )
311         *x = p.x;
312     if ( y )
313         *y = p.y;
314 }
315
316 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
317 {
318     wxNonOwnedWindowIPhoneImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
319     now->Create( parent, nativeWindow );
320     return now;
321 }
322
323
324 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
325     long style, long extraStyle, const wxString& name )
326 {
327     wxNonOwnedWindowImpl* now = new wxNonOwnedWindowIPhoneImpl( wxpeer );
328     now->Create( parent, pos, size, style , extraStyle, name );
329     return now;
330 }
331
332 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
333 {
334     UIWindow* toplevelwindow = now->GetWXWindow();
335     CGRect frame = [toplevelwindow bounds];
336     CGRect appframe = [[UIScreen mainScreen] applicationFrame];
337     BOOL fullscreen = now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE && [[UIApplication sharedApplication] statusBarStyle] == UIStatusBarStyleBlackTranslucent;
338
339     wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:( fullscreen ? frame : appframe ) ];
340     contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
341     wxUIContentViewController* controller = [[wxUIContentViewController alloc] initWithNibName:nil bundle:nil];
342
343 #ifdef __IPHONE_3_0
344     controller.wantsFullScreenLayout = fullscreen;
345 #endif
346
347     controller.view = contentview;
348     [contentview release];
349     [contentview setController:controller];
350     [contentview setHidden:YES];
351     
352     wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
353     impl->InstallEventHandler();
354     
355     if ([toplevelwindow respondsToSelector:@selector(setRootViewController:)])
356     {
357         toplevelwindow.rootViewController = controller;
358     }
359     else
360     {
361         [toplevelwindow addSubview:contentview];
362     }
363     return impl;
364 }
365
366 //
367 // obj-c impl
368 //
369
370 @implementation wxUIContentView
371
372 - (void) setController: (wxUIContentViewController*) controller
373 {
374     _controller = controller;
375 }
376
377 - (wxUIContentViewController*) controller
378 {
379     return _controller;
380 }
381
382 + (void)initialize
383 {
384     static BOOL initialized = NO;
385     if (!initialized)
386     {
387         initialized = YES;
388         wxOSXIPhoneClassAddWXMethods( self );
389     }
390 }
391  
392 @end
393
394 @implementation wxUIContentViewController
395
396 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
397 {
398     wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
399     wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
400     
401     // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
402     
403     return YES;
404 }
405
406 // iOS 6 support, right now unconditionally supporting all orientations, TODO use a orientation mask
407
408 -(BOOL)shouldAutorotate
409 {
410     return YES;
411 }
412
413  - (NSUInteger)supportedInterfaceOrientations
414 {
415      return UIInterfaceOrientationMaskAll;
416 }
417  
418  
419
420
421 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
422 {
423     wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
424     wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
425     
426     now->HandleResized(0);
427 }
428
429 -(void) viewWillAppear:(BOOL)animated
430 {
431     wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
432     wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
433     wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
434     
435     if ( nowimpl->InitialShowEventSent() )
436     {
437         wxShowEvent eventShow(now->GetId(), true);
438         eventShow.SetEventObject(now);
439     
440         now->HandleWindowEvent(eventShow);
441     }
442 }
443
444 -(void) viewWillDisappear:(BOOL)animated
445 {
446     wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
447     if( impl )
448     {
449         wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
450         wxNonOwnedWindowIPhoneImpl* nowimpl = dynamic_cast<wxNonOwnedWindowIPhoneImpl*> (now->GetNonOwnedPeer());
451         
452         if ( nowimpl->InitialShowEventSent() )
453         {
454             wxShowEvent eventShow(now->GetId(), false);
455             eventShow.SetEventObject(now);
456         
457             now->HandleWindowEvent(eventShow);
458         }
459     }
460 }
461
462 -(void) dealloc
463 {
464     [super dealloc];
465 }
466
467 - (UIView*) rotatingFooterView
468 {
469     UIView* footerView = [super rotatingFooterView];
470     if ( footerView == nil )
471     {
472         wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
473         wxFrame* frame = dynamic_cast<wxFrame*> (impl->GetWXPeer());
474         if ( frame && frame->GetToolBar())
475         {
476             footerView = frame->GetToolBar()->GetHandle();
477         }
478     }
479     return footerView;
480 }
481
482 @end
483
484
485