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 = (int)(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 = (int)rect.origin.y;
34 int x = (int)rect.origin.x;
35 if ( parent == NULL || ![ parent isFlipped ] )
36 y = (int)(frame.size.height - (rect.origin.y + rect.size.height));
37 return wxRect( x, y, (int)rect.size.width, (int)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 = (int)(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 = (int)(frame.size.height - ( p.y ));
57 return wxPoint( x, y);
60 bool shouldHandleSelector(SEL selector)
62 if (selector == @selector(noop:)
63 || selector == @selector(complete:)
64 || selector == @selector(deleteBackward:)
65 || selector == @selector(deleteForward:)
66 || selector == @selector(insertNewline:)
67 || selector == @selector(insertTab:)
68 || selector == @selector(keyDown:)
69 || selector == @selector(keyUp:)
70 || selector == @selector(scrollPageUp:)
71 || selector == @selector(scrollPageDown:)
72 || selector == @selector(scrollToBeginningOfDocument:)
73 || selector == @selector(scrollToEndOfDocument:))
81 // wx native implementation classes
84 typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
86 @interface wxNSWindow : NSWindow
88 wxNonOwnedWindowCocoaImpl* impl;
91 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
92 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
93 - (wxNonOwnedWindowCocoaImpl*) implementation;
94 - (void)noResponderFor: (SEL) selector;
97 @implementation wxNSWindow
99 // The default implementation always moves the window back onto the screen,
100 // even when the programmer explicitly wants to hide it.
101 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
106 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
108 impl = theImplementation;
111 - (wxNonOwnedWindowCocoaImpl*) implementation
116 - (void)doCommandBySelector:(SEL)selector
118 if (shouldHandleSelector(selector) &&
119 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
120 [super doCommandBySelector:selector];
124 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
125 - (void)noResponderFor: (SEL) selector
127 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
129 [super noResponderFor:selector];
130 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
131 // superimpl(self, @selector(noResponderFor:), selector);
135 // We need this for borderless windows, i.e. shaped windows or windows without
136 // a title bar. For more info, see:
137 // http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
138 - (BOOL)canBecomeKeyWindow
145 @interface wxNSPanel : NSPanel
148 wxNonOwnedWindowCocoaImpl* impl;
151 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
152 - (wxNonOwnedWindowCocoaImpl*) implementation;
153 - (void)noResponderFor: (SEL) selector;
156 @implementation wxNSPanel
158 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
160 impl = theImplementation;
163 - (BOOL)canBecomeKeyWindow
168 - (wxNonOwnedWindowCocoaImpl*) implementation
173 - (void)doCommandBySelector:(SEL)selector
175 if (shouldHandleSelector(selector))
176 [super doCommandBySelector:selector];
179 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
180 - (void)noResponderFor: (SEL) selector
182 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
184 [super noResponderFor:selector];
185 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
186 // superimpl(self, @selector(noResponderFor:), selector);
197 @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
201 - (void)windowDidResize:(NSNotification *)notification;
202 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
203 - (void)windowDidResignKey:(NSNotification *)notification;
204 - (void)windowDidBecomeKey:(NSNotification *)notification;
205 - (void)windowDidMove:(NSNotification *)notification;
206 - (BOOL)windowShouldClose:(id)window;
207 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
211 @implementation wxNonOwnedWindowController
219 - (BOOL)windowShouldClose:(id)nwindow
221 wxNSWindow* window = (wxNSWindow*) nwindow;
222 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
225 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
232 - (NSSize)windowWillResize:(NSWindow *)win
233 toSize:(NSSize)proposedFrameSize
235 NSRect frame = [win frame];
236 wxRect wxframe = wxFromNSRect( NULL, frame );
237 wxframe.SetWidth( (int)proposedFrameSize.width );
238 wxframe.SetHeight( (int)proposedFrameSize.height );
239 wxNSWindow* window = (wxNSWindow*) win;
240 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
243 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
246 wxpeer->HandleResizing( 0, &wxframe );
247 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
252 return proposedFrameSize;
255 - (void)windowDidResize:(NSNotification *)notification
257 wxNSWindow* window = (wxNSWindow*) [notification object];
258 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
261 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
263 wxpeer->HandleResized(0);
267 - (void)windowDidMove:(NSNotification *)notification
269 wxNSWindow* window = (wxNSWindow*) [notification object];
270 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
273 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
275 wxpeer->HandleMoved(0);
279 - (void)windowDidBecomeKey:(NSNotification *)notification
281 wxNSWindow* window = (wxNSWindow*) [notification object];
282 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
285 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
287 wxpeer->HandleActivated(0, true);
291 - (void)windowDidResignKey:(NSNotification *)notification
293 wxNSWindow* window = (wxNSWindow*) [notification object];
294 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
297 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
300 wxpeer->HandleActivated(0, false);
301 // Needed for popup window since the firstResponder
302 // (focus in wx) doesn't change when this
303 // TLW becomes inactive.
304 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
305 event.SetEventObject(wxpeer);
306 wxpeer->HandleWindowEvent(event);
311 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
315 if ([anObject isKindOfClass:[wxNSTextField class]])
317 wxNSTextField* tf = (wxNSTextField*) anObject;
318 wxNSTextFieldEditor* editor = [tf fieldEditor];
321 editor = [[wxNSTextFieldEditor alloc] init];
322 [editor setFieldEditor:YES];
323 [tf setFieldEditor:editor];
331 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
333 wxNonOwnedWindowCocoaImpl* windowimpl = [(wxNSWindow*)window implementation];
336 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
337 wxMaximizeEvent event(wxpeer->GetId());
338 event.SetEventObject(wxpeer);
339 return !wxpeer->HandleWindowEvent(event);
346 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
348 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
349 wxNonOwnedWindowImpl(nonownedwnd)
352 m_macFullScreenData = NULL;
355 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
358 m_macFullScreenData = NULL;
361 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
363 [m_macWindow setImplementation:nil];
364 [m_macWindow setDelegate:nil];
365 [m_macWindow release];
368 void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
370 [m_macWindow setDelegate:nil];
373 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
374 long style, long extraStyle, const wxString& WXUNUSED(name) )
376 static wxNonOwnedWindowController* controller = NULL;
379 controller =[[wxNonOwnedWindowController alloc] init];
382 int windowstyle = NSBorderlessWindowMask;
384 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
385 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
387 m_macWindow = [wxNSPanel alloc];
390 m_macWindow = [wxNSWindow alloc];
392 CGWindowLevel level = kCGNormalWindowLevel;
394 if ( style & wxFRAME_TOOL_WINDOW )
396 windowstyle |= NSUtilityWindowMask;
397 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
398 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
400 windowstyle |= NSTitledWindowMask ;
403 else if ( ( style & wxPOPUP_WINDOW ) )
405 level = kCGPopUpMenuWindowLevel;
407 if ( ( style & wxBORDER_NONE ) )
409 wclass = kHelpWindowClass ; // has no border
410 attr |= kWindowNoShadowAttribute;
414 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
418 else if ( ( style & wxCAPTION ) )
420 windowstyle |= NSTitledWindowMask ;
422 else if ( ( style & wxFRAME_DRAWER ) )
425 wclass = kDrawerWindowClass;
430 // set these even if we have no title, otherwise the controls won't be visible
431 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
432 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
434 windowstyle |= NSTitledWindowMask ;
437 else if ( ( style & wxNO_BORDER ) )
439 wclass = kSimpleWindowClass ;
443 wclass = kPlainWindowClass ;
448 if ( windowstyle & NSTitledWindowMask )
450 if ( ( style & wxMINIMIZE_BOX ) )
451 windowstyle |= NSMiniaturizableWindowMask ;
453 if ( ( style & wxMAXIMIZE_BOX ) )
454 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
456 if ( ( style & wxRESIZE_BORDER ) )
457 windowstyle |= NSResizableWindowMask ;
459 if ( ( style & wxCLOSE_BOX) )
460 windowstyle |= NSClosableWindowMask ;
462 if ( extraStyle & wxFRAME_EX_METAL)
463 windowstyle |= NSTexturedBackgroundWindowMask;
465 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
466 level = kCGFloatingWindowLevel;
468 if ( ( style & wxSTAY_ON_TOP ) )
469 level = kCGUtilityWindowLevel;
471 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
473 [m_macWindow setImplementation:this];
474 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
476 [m_macWindow initWithContentRect:r
477 styleMask:windowstyle
478 backing:NSBackingStoreBuffered
482 [m_macWindow setLevel:level];
484 [m_macWindow setDelegate:controller];
486 [m_macWindow setAcceptsMouseMovedEvents: YES];
490 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
495 void wxNonOwnedWindowCocoaImpl::Raise()
497 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
500 void wxNonOwnedWindowCocoaImpl::Lower()
502 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
505 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
507 [m_macWindow orderFront:nil];
508 [[m_macWindow contentView] setNeedsDisplay: YES];
511 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
515 wxNonOwnedWindow* wxpeer = GetWXPeer();
516 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
517 [m_macWindow makeKeyAndOrderFront:nil];
519 [m_macWindow orderFront:nil];
520 [[m_macWindow contentView] setNeedsDisplay: YES];
523 [m_macWindow orderOut:nil];
527 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
531 return wxWidgetCocoaImpl::
532 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
535 void wxNonOwnedWindowCocoaImpl::Update()
537 [m_macWindow displayIfNeeded];
540 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
542 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
546 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
551 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
555 bool metal = exStyle & wxFRAME_EX_METAL ;
556 int windowStyle = [ m_macWindow styleMask];
557 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
559 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
561 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
563 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
568 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
572 CGWindowLevel level = kCGNormalWindowLevel;
574 if (style & wxSTAY_ON_TOP)
575 level = kCGUtilityWindowLevel;
576 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
577 level = kCGFloatingWindowLevel;
579 [m_macWindow setLevel: level];
583 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
585 if ( style == wxBG_STYLE_TRANSPARENT )
587 [m_macWindow setOpaque:NO];
588 [m_macWindow setBackgroundColor:[NSColor clearColor]];
594 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
599 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
601 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
602 // do not trigger refreshes upon invisible and possible partly created objects
603 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
606 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
608 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
613 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
615 NSRect rect = [m_macWindow frame];
616 width = (int)rect.size.width;
617 height = (int)rect.size.height;
620 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
622 NSRect outer = NSMakeRect(100,100,100,100);
623 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
624 NSRect rect = [[m_macWindow contentView] frame];
625 left = (int)rect.origin.x;
626 top = (int)rect.origin.y;
627 width = (int)rect.size.width;
628 height = (int)rect.size.height;
631 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
633 [m_macWindow setOpaque:NO];
634 [m_macWindow setBackgroundColor:[NSColor clearColor]];
639 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
641 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
644 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
646 return [m_macWindow isZoomed];
649 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
651 return [m_macWindow isMiniaturized];
654 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
657 [m_macWindow miniaturize:nil];
659 [m_macWindow deminiaturize:nil];
662 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
664 [m_macWindow zoom:nil];
668 // http://cocoadevcentral.com/articles/000028.php
673 NSRect m_formerFrame;
676 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
678 return m_macFullScreenData != NULL ;
681 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
685 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
687 data = new FullScreenData();
689 m_macFullScreenData = data ;
690 data->m_formerLevel = [m_macWindow level];
691 data->m_formerFrame = [m_macWindow frame];
692 CGDisplayCapture( kCGDirectMainDisplay );
693 [m_macWindow setLevel:CGShieldingWindowLevel()];
694 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
696 else if ( m_macFullScreenData != NULL )
698 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
699 CGDisplayRelease( kCGDirectMainDisplay );
700 [m_macWindow setLevel:data->m_formerLevel];
701 [m_macWindow setFrame:data->m_formerFrame display:YES];
703 m_macFullScreenData = NULL ;
709 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
711 NSRequestUserAttentionType flagsOSX;
714 case wxUSER_ATTENTION_INFO:
715 flagsOSX = NSInformationalRequest;
718 case wxUSER_ATTENTION_ERROR:
719 flagsOSX = NSCriticalRequest;
723 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
727 [NSApp requestUserAttention:flagsOSX];
730 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
732 wxPoint p((x ? *x : 0), (y ? *y : 0) );
733 NSPoint nspt = wxToNSPoint( NULL, p );
734 nspt = [m_macWindow convertScreenToBase:nspt];
735 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
736 p = wxFromNSPoint([m_macWindow contentView], nspt);
743 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
745 wxPoint p((x ? *x : 0), (y ? *y : 0) );
746 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
747 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
748 nspt = [m_macWindow convertBaseToScreen:nspt];
749 p = wxFromNSPoint( NULL, nspt);
756 bool wxNonOwnedWindowCocoaImpl::IsActive()
758 return [m_macWindow isKeyWindow];
761 void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
763 [m_macWindow setDocumentEdited:modified];
766 bool wxNonOwnedWindowCocoaImpl::IsModified() const
768 return [m_macWindow isDocumentEdited];
771 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
772 long style, long extraStyle, const wxString& name )
774 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
775 now->Create( parent, pos, size, style , extraStyle, name );