]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/nonownedwnd.mm
We'll get an unhandled exception if the view is hidden, fortunately, lockFocusIfCanDr...
[wxWidgets.git] / src / osx / cocoa / nonownedwnd.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for cocoa
4 // Author: DavidStefan 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 #ifndef WX_PRECOMP
14 #include "wx/nonownedwnd.h"
15 #include "wx/frame.h"
16 #endif
17
18 #include "wx/osx/private.h"
19
20 NSRect wxToNSRect( NSView* parent, const wxRect& r )
21 {
22 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
23 int y = r.y;
24 int x = r.x ;
25 if ( parent == NULL || ![ parent isFlipped ] )
26 y = frame.size.height - ( r.y + r.height );
27 return NSMakeRect(x, y, r.width , r.height);
28 }
29
30 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
31 {
32 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
33 int y = rect.origin.y;
34 int x = rect.origin.x;
35 if ( parent == NULL || ![ parent isFlipped ] )
36 y = frame.size.height - (rect.origin.y + rect.size.height);
37 return wxRect( x, y, rect.size.width, rect.size.height );
38 }
39
40 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
41 {
42 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
43 int x = p.x ;
44 int y = p.y;
45 if ( parent == NULL || ![ parent isFlipped ] )
46 y = frame.size.height - ( p.y );
47 return NSMakePoint(x, y);
48 }
49
50 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
51 {
52 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
53 int x = p.x;
54 int y = p.y;
55 if ( parent == NULL || ![ parent isFlipped ] )
56 y = frame.size.height - ( p.y );
57 return wxPoint( x, y);
58 }
59
60 //
61 // wx native implementation classes
62 //
63
64 @interface wxNSWindow : NSWindow
65
66 {
67 wxNonOwnedWindowCocoaImpl* impl;
68 }
69
70 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
71 - (wxNonOwnedWindowCocoaImpl*) implementation;
72
73 @end
74
75 @implementation wxNSWindow
76
77 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
78 {
79 impl = theImplementation;
80 }
81
82 - (wxNonOwnedWindowCocoaImpl*) implementation
83 {
84 return impl;
85 }
86
87
88 @end
89
90 @interface wxNSPanel : wxNSWindow
91
92 {
93 }
94
95 @end
96
97 @implementation wxNSPanel
98
99 @end
100
101
102 //
103 // controller
104 //
105
106 @interface wxNonOwnedWindowController : NSObject
107 {
108 }
109
110 - (void)windowDidResize:(NSNotification *)notification;
111 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
112 - (void)windowDidResignMain:(NSNotification *)notification;
113 - (void)windowDidBecomeMain:(NSNotification *)notification;
114 - (void)windowDidMove:(NSNotification *)notification;
115 - (BOOL)windowShouldClose:(id)window;
116
117 @end
118
119 @implementation wxNonOwnedWindowController
120
121 - (id) init
122 {
123 [super init];
124 return self;
125 }
126
127 - (BOOL)windowShouldClose:(id)nwindow
128 {
129 wxNSWindow* window = (wxNSWindow*) nwindow;
130 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
131 if ( windowimpl )
132 {
133 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
134 if ( wxpeer )
135 wxpeer->Close();
136 }
137 return NO;
138 }
139
140 - (NSSize)windowWillResize:(NSWindow *)win
141 toSize:(NSSize)proposedFrameSize
142 {
143 NSRect frame = [win frame];
144 wxRect wxframe = wxFromNSRect( NULL, frame );
145 wxframe.SetWidth( proposedFrameSize.width );
146 wxframe.SetHeight( proposedFrameSize.height );
147 wxNSWindow* window = (wxNSWindow*) win;
148 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
149 if ( windowimpl )
150 {
151 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
152 if ( wxpeer )
153 {
154 wxpeer->HandleResizing( 0, &wxframe );
155 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
156 return newSize;
157 }
158 }
159
160 return proposedFrameSize;
161 }
162
163 - (void)windowDidResize:(NSNotification *)notification
164 {
165 wxNSWindow* window = (wxNSWindow*) [notification object];
166 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
167 if ( windowimpl )
168 {
169 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
170 if ( wxpeer )
171 wxpeer->HandleResized(0);
172 }
173 }
174
175 - (void)windowDidMove:(NSNotification *)notification
176 {
177 wxNSWindow* window = (wxNSWindow*) [notification object];
178 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
179 if ( windowimpl )
180 {
181 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
182 if ( wxpeer )
183 wxpeer->HandleMoved(0);
184 }
185 }
186
187 - (void)windowDidBecomeMain:(NSNotification *)notification
188 {
189 wxNSWindow* window = (wxNSWindow*) [notification object];
190 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
191 if ( windowimpl )
192 {
193 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
194 if ( wxpeer )
195 wxpeer->HandleActivated(0, true);
196 }
197 }
198
199 - (void)windowDidResignMain:(NSNotification *)notification
200 {
201 wxNSWindow* window = (wxNSWindow*) [notification object];
202 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
203 if ( windowimpl )
204 {
205 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
206 if ( wxpeer )
207 wxpeer->HandleActivated(0, false);
208 }
209 }
210
211 @end
212
213 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
214
215 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
216 wxNonOwnedWindowImpl(nonownedwnd)
217 {
218 m_macWindow = NULL;
219 m_macFullScreenData = NULL;
220 }
221
222 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
223 {
224 m_macWindow = NULL;
225 m_macFullScreenData = NULL;
226 }
227
228 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
229 {
230 [m_macWindow setImplementation:nil];
231 [m_macWindow setDelegate:nil];
232 [m_macWindow release];
233 }
234
235 void wxNonOwnedWindowCocoaImpl::Destroy()
236 {
237 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
238 }
239
240 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
241 long style, long extraStyle, const wxString& name )
242 {
243 static wxNonOwnedWindowController* controller = NULL;
244
245 if ( !controller )
246 controller =[[wxNonOwnedWindowController alloc] init];
247
248
249 int windowstyle = NSBorderlessWindowMask;
250
251 if ( style & wxFRAME_TOOL_WINDOW )
252 m_macWindow = [wxNSPanel alloc];
253 else
254 m_macWindow = [wxNSWindow alloc];
255
256 CGWindowLevel level = kCGNormalWindowLevel;
257
258 if ( style & wxFRAME_TOOL_WINDOW )
259 {
260 if ( ( style & wxSTAY_ON_TOP ) )
261 level = kCGUtilityWindowLevel;
262 else
263 level = kCGFloatingWindowLevel ;
264
265 }
266 else if ( ( style & wxPOPUP_WINDOW ) )
267 {
268 level = kCGPopUpMenuWindowLevel;
269 /*
270 if ( ( style & wxBORDER_NONE ) )
271 {
272 wclass = kHelpWindowClass ; // has no border
273 attr |= kWindowNoShadowAttribute;
274 }
275 else
276 {
277 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
278 }
279 */
280 }
281 else if ( ( style & wxCAPTION ) )
282 {
283 windowstyle |= NSTitledWindowMask ;
284 }
285 else if ( ( style & wxFRAME_DRAWER ) )
286 {
287 /*
288 wclass = kDrawerWindowClass;
289 */
290 }
291 else
292 {
293 // set these even if we have no title, otherwise the controls won't be visible
294 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
295 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
296 {
297 windowstyle |= NSTitledWindowMask ;
298 }
299 /*
300 else if ( ( style & wxNO_BORDER ) )
301 {
302 wclass = kSimpleWindowClass ;
303 }
304 else
305 {
306 wclass = kPlainWindowClass ;
307 }
308 */
309 }
310
311 if ( windowstyle & NSTitledWindowMask )
312 {
313 if ( ( style & wxMINIMIZE_BOX ) )
314 windowstyle |= NSMiniaturizableWindowMask ;
315
316 if ( ( style & wxMAXIMIZE_BOX ) )
317 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
318
319 if ( ( style & wxRESIZE_BORDER ) )
320 windowstyle |= NSResizableWindowMask ;
321
322 if ( ( style & wxCLOSE_BOX) )
323 windowstyle |= NSClosableWindowMask ;
324 }
325 if ( extraStyle & wxFRAME_EX_METAL)
326 windowstyle |= NSTexturedBackgroundWindowMask;
327
328 if ( ( style & wxSTAY_ON_TOP ) )
329 level = kCGUtilityWindowLevel;
330 /*
331 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) )
332 group = GetWindowGroupOfClass(kFloatingWindowClass);
333 */
334
335 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
336
337 [m_macWindow setImplementation:this];
338
339 [m_macWindow initWithContentRect:r
340 styleMask:windowstyle
341 backing:NSBackingStoreBuffered
342 defer:NO
343 ];
344
345 [m_macWindow setLevel:level];
346
347 [m_macWindow setDelegate:controller];
348
349 // [m_macWindow makeKeyAndOrderFront:nil];
350 }
351
352
353 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
354 {
355 return m_macWindow;
356 }
357
358 void wxNonOwnedWindowCocoaImpl::Raise()
359 {
360 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
361 }
362
363 void wxNonOwnedWindowCocoaImpl::Lower()
364 {
365 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
366 }
367
368 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
369 {
370 if ( show )
371 {
372 [m_macWindow orderFront:nil];
373 [[m_macWindow contentView] setNeedsDisplay:YES];
374 }
375 else
376 [m_macWindow orderOut:nil];
377 return true;
378 }
379
380 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
381 {
382 return Show(show);
383 }
384
385 void wxNonOwnedWindowCocoaImpl::Update()
386 {
387 [m_macWindow displayIfNeeded];
388 }
389
390 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
391 {
392 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
393 return true;
394 }
395
396 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
397 {
398 return true;
399 }
400
401 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
402 {
403 if ( m_macWindow )
404 {
405 bool metal = exStyle & wxFRAME_EX_METAL ;
406 int windowStyle = [ m_macWindow styleMask];
407 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
408 {
409 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
410 }
411 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
412 {
413 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
414 }
415 }
416 }
417
418 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
419 {
420 return true;
421 }
422
423 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
424 {
425 return true;
426 }
427
428 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
429 {
430 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
431 // do not trigger refreshes upon invisible and possible partly created objects
432 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
433 }
434
435 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
436 {
437 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
438 x = r.GetLeft();
439 y = r.GetTop();
440 }
441
442 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
443 {
444 NSRect rect = [m_macWindow frame];
445 width = rect.size.width;
446 height = rect.size.height;
447 }
448
449 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
450 {
451 NSRect outer = NSMakeRect(100,100,100,100);
452 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
453 NSRect rect = [[m_macWindow contentView] frame];
454 width = rect.size.width;
455 height = rect.size.height;
456 }
457
458 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
459 {
460 return false;
461 }
462
463 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
464 {
465 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
466 }
467
468 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
469 {
470 return [m_macWindow isZoomed];
471 }
472
473 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
474 {
475 return [m_macWindow isMiniaturized];
476 }
477
478 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
479 {
480 if ( iconize )
481 [m_macWindow miniaturize:nil];
482 else
483 [m_macWindow deminiaturize:nil];
484 }
485
486 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
487 {
488 [m_macWindow zoom:nil];
489 }
490
491
492 // http://cocoadevcentral.com/articles/000028.php
493
494 typedef struct
495 {
496 int m_formerLevel;
497 NSRect m_formerFrame;
498 } FullScreenData ;
499
500 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
501 {
502 return m_macFullScreenData != NULL ;
503 }
504
505 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
506 {
507 if ( show )
508 {
509 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
510 delete data ;
511 data = new FullScreenData();
512
513 m_macFullScreenData = data ;
514 data->m_formerLevel = [m_macWindow level];
515 data->m_formerFrame = [m_macWindow frame];
516 CGDisplayCapture( kCGDirectMainDisplay );
517 [m_macWindow setLevel:CGShieldingWindowLevel()];
518 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
519 }
520 else if ( m_macFullScreenData != NULL )
521 {
522 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
523 CGDisplayRelease( kCGDirectMainDisplay );
524 [m_macWindow setLevel:data->m_formerLevel];
525 [m_macWindow setFrame:data->m_formerFrame display:YES];
526 delete data ;
527 m_macFullScreenData = NULL ;
528 }
529
530 return true;
531 }
532
533 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
534 {
535 }
536
537 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
538 {
539 wxPoint p((x ? *x : 0), (y ? *y : 0) );
540 NSPoint nspt = wxToNSPoint( NULL, p );
541 nspt = [m_macWindow convertScreenToBase:nspt];
542 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
543 p = wxFromNSPoint([m_macWindow contentView], nspt);
544 if ( x )
545 *x = p.x;
546 if ( y )
547 *y = p.y;
548 }
549
550 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
551 {
552 wxPoint p((x ? *x : 0), (y ? *y : 0) );
553 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
554 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
555 nspt = [m_macWindow convertBaseToScreen:nspt];
556 p = wxFromNSPoint( NULL, nspt);
557 if ( x )
558 *x = p.x;
559 if ( y )
560 *y = p.y;
561 }
562
563 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
564 long style, long extraStyle, const wxString& name )
565 {
566 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
567 now->Create( parent, pos, size, style , extraStyle, name );
568 return now;
569 }