1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for cocoa
4 // Author: DavidStefan 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/nonownedwnd.h"
18 #include "wx/osx/private.h"
20 NSRect wxToNSRect( NSView* parent, const wxRect& r )
22 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
25 if ( parent == NULL || ![ parent isFlipped ] )
26 y = frame.size.height - ( r.y + r.height );
27 return NSMakeRect(x, y, r.width , r.height);
30 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
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 );
40 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
42 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
45 if ( parent == NULL || ![ parent isFlipped ] )
46 y = frame.size.height - ( p.y );
47 return NSMakePoint(x, y);
50 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
52 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
55 if ( parent == NULL || ![ parent isFlipped ] )
56 y = frame.size.height - ( p.y );
57 return wxPoint( x, y);
61 // wx native implementation classes
64 typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
66 @interface wxNSWindow : NSWindow
69 wxNonOwnedWindowCocoaImpl* impl;
72 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
73 - (wxNonOwnedWindowCocoaImpl*) implementation;
74 - (void)noResponderFor: (SEL) selector;
77 @implementation wxNSWindow
79 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
81 impl = theImplementation;
84 - (wxNonOwnedWindowCocoaImpl*) implementation
89 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
90 - (void)noResponderFor: (SEL) selector
92 if (selector != @selector(keyDown:))
94 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
95 superimpl(self, @selector(noResponderFor:), selector);
101 @interface wxNSPanel : NSPanel
104 wxNonOwnedWindowCocoaImpl* impl;
107 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
108 - (wxNonOwnedWindowCocoaImpl*) implementation;
109 - (void)noResponderFor: (SEL) selector;
112 @implementation wxNSPanel
114 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
116 impl = theImplementation;
119 - (BOOL)canBecomeKeyWindow
124 - (wxNonOwnedWindowCocoaImpl*) implementation
129 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
130 - (void)noResponderFor: (SEL) selector
132 if (selector != @selector(keyDown:))
134 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
135 superimpl(self, @selector(noResponderFor:), selector);
146 @interface wxNonOwnedWindowController : NSObject
150 - (void)windowDidResize:(NSNotification *)notification;
151 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
152 - (void)windowDidResignKey:(NSNotification *)notification;
153 - (void)windowDidBecomeKey:(NSNotification *)notification;
154 - (void)windowDidMove:(NSNotification *)notification;
155 - (BOOL)windowShouldClose:(id)window;
159 @implementation wxNonOwnedWindowController
167 - (BOOL)windowShouldClose:(id)nwindow
169 wxNSWindow* window = (wxNSWindow*) nwindow;
170 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
173 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
180 - (NSSize)windowWillResize:(NSWindow *)win
181 toSize:(NSSize)proposedFrameSize
183 NSRect frame = [win frame];
184 wxRect wxframe = wxFromNSRect( NULL, frame );
185 wxframe.SetWidth( proposedFrameSize.width );
186 wxframe.SetHeight( proposedFrameSize.height );
187 wxNSWindow* window = (wxNSWindow*) win;
188 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
191 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
194 wxpeer->HandleResizing( 0, &wxframe );
195 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
200 return proposedFrameSize;
203 - (void)windowDidResize:(NSNotification *)notification
205 wxNSWindow* window = (wxNSWindow*) [notification object];
206 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
209 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
211 wxpeer->HandleResized(0);
215 - (void)windowDidMove:(NSNotification *)notification
217 wxNSWindow* window = (wxNSWindow*) [notification object];
218 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
221 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
223 wxpeer->HandleMoved(0);
227 - (void)windowDidBecomeKey:(NSNotification *)notification
229 wxNSWindow* window = (wxNSWindow*) [notification object];
230 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
233 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
235 wxpeer->HandleActivated(0, true);
239 - (void)windowDidResignKey:(NSNotification *)notification
241 wxNSWindow* window = (wxNSWindow*) [notification object];
242 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
245 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
248 wxpeer->HandleActivated(0, false);
249 // Needed for popup window since the firstResponder
250 // (focus in wx) doesn't change when this
251 // TLW becomes inactive.
252 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
253 event.SetEventObject(wxpeer);
254 wxpeer->HandleWindowEvent(event);
261 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
263 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
264 wxNonOwnedWindowImpl(nonownedwnd)
267 m_macFullScreenData = NULL;
270 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
273 m_macFullScreenData = NULL;
276 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
278 [m_macWindow setImplementation:nil];
279 [m_macWindow setDelegate:nil];
280 [m_macWindow release];
283 void wxNonOwnedWindowCocoaImpl::Destroy()
285 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
288 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
289 long style, long extraStyle, const wxString& name )
291 static wxNonOwnedWindowController* controller = NULL;
294 controller =[[wxNonOwnedWindowController alloc] init];
297 int windowstyle = NSBorderlessWindowMask;
299 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
300 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
302 m_macWindow = [wxNSPanel alloc];
305 m_macWindow = [wxNSWindow alloc];
307 CGWindowLevel level = kCGNormalWindowLevel;
309 if ( style & wxFRAME_TOOL_WINDOW )
311 windowstyle |= NSUtilityWindowMask;
312 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
313 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
315 windowstyle |= NSTitledWindowMask ;
318 else if ( ( style & wxPOPUP_WINDOW ) )
320 level = kCGPopUpMenuWindowLevel;
322 if ( ( style & wxBORDER_NONE ) )
324 wclass = kHelpWindowClass ; // has no border
325 attr |= kWindowNoShadowAttribute;
329 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
333 else if ( ( style & wxCAPTION ) )
335 windowstyle |= NSTitledWindowMask ;
337 else if ( ( style & wxFRAME_DRAWER ) )
340 wclass = kDrawerWindowClass;
345 // set these even if we have no title, otherwise the controls won't be visible
346 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
347 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
349 windowstyle |= NSTitledWindowMask ;
352 else if ( ( style & wxNO_BORDER ) )
354 wclass = kSimpleWindowClass ;
358 wclass = kPlainWindowClass ;
363 if ( windowstyle & NSTitledWindowMask )
365 if ( ( style & wxMINIMIZE_BOX ) )
366 windowstyle |= NSMiniaturizableWindowMask ;
368 if ( ( style & wxMAXIMIZE_BOX ) )
369 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
371 if ( ( style & wxRESIZE_BORDER ) )
372 windowstyle |= NSResizableWindowMask ;
374 if ( ( style & wxCLOSE_BOX) )
375 windowstyle |= NSClosableWindowMask ;
377 if ( extraStyle & wxFRAME_EX_METAL)
378 windowstyle |= NSTexturedBackgroundWindowMask;
380 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
381 level = kCGFloatingWindowLevel;
383 if ( ( style & wxSTAY_ON_TOP ) )
384 level = kCGUtilityWindowLevel;
386 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
388 [m_macWindow setImplementation:this];
390 [m_macWindow initWithContentRect:r
391 styleMask:windowstyle
392 backing:NSBackingStoreBuffered
396 [m_macWindow setLevel:level];
398 [m_macWindow setDelegate:controller];
400 [m_macWindow setAcceptsMouseMovedEvents: YES];
402 if ( ( style & wxPOPUP_WINDOW ) )
403 [m_macWindow makeKeyAndOrderFront:nil];
407 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
412 void wxNonOwnedWindowCocoaImpl::Raise()
414 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
417 void wxNonOwnedWindowCocoaImpl::Lower()
419 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
422 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
426 [m_macWindow orderFront:nil];
427 [[m_macWindow contentView] setNeedsDisplay:YES];
430 [m_macWindow orderOut:nil];
434 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
439 void wxNonOwnedWindowCocoaImpl::Update()
441 [m_macWindow displayIfNeeded];
444 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
446 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
450 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
455 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
459 bool metal = exStyle & wxFRAME_EX_METAL ;
460 int windowStyle = [ m_macWindow styleMask];
461 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
463 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
465 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
467 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
472 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
477 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
482 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
484 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
485 // do not trigger refreshes upon invisible and possible partly created objects
486 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
489 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
491 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
496 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
498 NSRect rect = [m_macWindow frame];
499 width = rect.size.width;
500 height = rect.size.height;
503 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
505 NSRect outer = NSMakeRect(100,100,100,100);
506 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
507 NSRect rect = [[m_macWindow contentView] frame];
508 left = rect.origin.x;
510 width = rect.size.width;
511 height = rect.size.height;
514 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
519 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
521 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
524 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
526 return [m_macWindow isZoomed];
529 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
531 return [m_macWindow isMiniaturized];
534 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
537 [m_macWindow miniaturize:nil];
539 [m_macWindow deminiaturize:nil];
542 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
544 [m_macWindow zoom:nil];
548 // http://cocoadevcentral.com/articles/000028.php
553 NSRect m_formerFrame;
556 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
558 return m_macFullScreenData != NULL ;
561 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
565 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
567 data = new FullScreenData();
569 m_macFullScreenData = data ;
570 data->m_formerLevel = [m_macWindow level];
571 data->m_formerFrame = [m_macWindow frame];
572 CGDisplayCapture( kCGDirectMainDisplay );
573 [m_macWindow setLevel:CGShieldingWindowLevel()];
574 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
576 else if ( m_macFullScreenData != NULL )
578 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
579 CGDisplayRelease( kCGDirectMainDisplay );
580 [m_macWindow setLevel:data->m_formerLevel];
581 [m_macWindow setFrame:data->m_formerFrame display:YES];
583 m_macFullScreenData = NULL ;
589 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
593 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
595 wxPoint p((x ? *x : 0), (y ? *y : 0) );
596 NSPoint nspt = wxToNSPoint( NULL, p );
597 nspt = [m_macWindow convertScreenToBase:nspt];
598 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
599 p = wxFromNSPoint([m_macWindow contentView], nspt);
606 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
608 wxPoint p((x ? *x : 0), (y ? *y : 0) );
609 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
610 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
611 nspt = [m_macWindow convertBaseToScreen:nspt];
612 p = wxFromNSPoint( NULL, nspt);
619 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
620 long style, long extraStyle, const wxString& name )
622 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
623 now->Create( parent, pos, size, style , extraStyle, name );