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