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 category for NSWindow (our own and wrapped instances)
84 @interface NSWindow (wxNSWindowSupport)
86 - (wxNonOwnedWindowCocoaImpl*) WX_implementation;
88 - (bool) WX_filterSendEvent:(NSEvent *) event;
92 @implementation NSWindow (wxNSWindowSupport)
94 - (wxNonOwnedWindowCocoaImpl*) WX_implementation
96 return (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
99 // TODO in cocoa everything during a drag is sent to the NSWindow the mouse down occured,
100 // this does not conform to the wx behaviour if the window is not captured, so try to resend
101 // or capture all wx mouse event handling at the tlw as we did for carbon
103 - (bool) WX_filterSendEvent:(NSEvent *) event
105 bool handled = false;
106 if ( ([event type] >= NSLeftMouseDown) && ([event type] <= NSMouseExited) )
108 wxWindow* cw = wxWindow::GetCapture();
111 ((wxWidgetCocoaImpl*)cw->GetPeer())->DoHandleMouseEvent( event);
120 // wx native implementation
123 @interface wxNSWindow : NSWindow
127 - (void) sendEvent:(NSEvent *)event;
128 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
129 - (void)noResponderFor: (SEL) selector;
132 @implementation wxNSWindow
134 - (void)sendEvent:(NSEvent *) event
136 if ( ![self WX_filterSendEvent: event] )
137 [super sendEvent: event];
140 // The default implementation always moves the window back onto the screen,
141 // even when the programmer explicitly wants to hide it.
142 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
148 - (void)doCommandBySelector:(SEL)selector
150 if (shouldHandleSelector(selector) &&
151 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
152 [super doCommandBySelector:selector];
156 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
157 - (void)noResponderFor: (SEL) selector
159 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
161 [super noResponderFor:selector];
165 // We need this for borderless windows, i.e. shaped windows or windows without
166 // a title bar. For more info, see:
167 // http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
168 - (BOOL)canBecomeKeyWindow
175 @interface wxNSPanel : NSPanel
179 - (void)noResponderFor: (SEL) selector;
180 - (void)sendEvent:(NSEvent *)event;
183 @implementation wxNSPanel
185 - (BOOL)canBecomeKeyWindow
190 - (void)doCommandBySelector:(SEL)selector
192 if (shouldHandleSelector(selector))
193 [super doCommandBySelector:selector];
196 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
197 - (void)noResponderFor: (SEL) selector
199 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
201 [super noResponderFor:selector];
205 - (void)sendEvent:(NSEvent *) event
207 if ( ![self WX_filterSendEvent: event] )
208 [super sendEvent: event];
218 @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
222 - (void)windowDidResize:(NSNotification *)notification;
223 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
224 - (void)windowDidResignKey:(NSNotification *)notification;
225 - (void)windowDidBecomeKey:(NSNotification *)notification;
226 - (void)windowDidMove:(NSNotification *)notification;
227 - (BOOL)windowShouldClose:(id)window;
228 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
232 @implementation wxNonOwnedWindowController
240 - (BOOL)windowShouldClose:(id)nwindow
242 wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
245 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
252 - (NSSize)windowWillResize:(NSWindow *)window
253 toSize:(NSSize)proposedFrameSize
255 NSRect frame = [window frame];
256 wxRect wxframe = wxFromNSRect( NULL, frame );
257 wxframe.SetWidth( (int)proposedFrameSize.width );
258 wxframe.SetHeight( (int)proposedFrameSize.height );
260 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
263 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
266 wxpeer->HandleResizing( 0, &wxframe );
267 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
272 return proposedFrameSize;
275 - (void)windowDidResize:(NSNotification *)notification
277 NSWindow* window = (NSWindow*) [notification object];
278 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
281 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
283 wxpeer->HandleResized(0);
287 - (void)windowDidMove:(NSNotification *)notification
289 wxNSWindow* window = (wxNSWindow*) [notification object];
290 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
293 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
295 wxpeer->HandleMoved(0);
299 - (void)windowDidBecomeKey:(NSNotification *)notification
301 NSWindow* window = (NSWindow*) [notification object];
302 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
305 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
307 wxpeer->HandleActivated(0, true);
311 - (void)windowDidResignKey:(NSNotification *)notification
313 NSWindow* window = (NSWindow*) [notification object];
314 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
317 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
320 wxpeer->HandleActivated(0, false);
321 // Needed for popup window since the firstResponder
322 // (focus in wx) doesn't change when this
323 // TLW becomes inactive.
324 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
325 event.SetEventObject(wxpeer);
326 wxpeer->HandleWindowEvent(event);
331 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
335 if ([anObject isKindOfClass:[wxNSTextField class]])
337 wxNSTextField* tf = (wxNSTextField*) anObject;
338 wxNSTextFieldEditor* editor = [tf fieldEditor];
341 editor = [[wxNSTextFieldEditor alloc] init];
342 [editor setFieldEditor:YES];
343 [tf setFieldEditor:editor];
351 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
353 wxUnusedVar(newFrame);
354 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
357 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
358 wxMaximizeEvent event(wxpeer->GetId());
359 event.SetEventObject(wxpeer);
360 return !wxpeer->HandleWindowEvent(event);
367 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
369 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
370 wxNonOwnedWindowImpl(nonownedwnd)
373 m_macFullScreenData = NULL;
376 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
379 m_macFullScreenData = NULL;
382 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
384 if ( !m_wxPeer->IsNativeWindowWrapper() )
386 [m_macWindow setDelegate:nil];
387 [m_macWindow release];
391 void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
393 if ( !m_wxPeer->IsNativeWindowWrapper() )
395 [m_macWindow setDelegate:nil];
399 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
400 long style, long extraStyle, const wxString& WXUNUSED(name) )
402 static wxNonOwnedWindowController* controller = NULL;
405 controller =[[wxNonOwnedWindowController alloc] init];
408 int windowstyle = NSBorderlessWindowMask;
410 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
411 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
413 m_macWindow = [wxNSPanel alloc];
416 m_macWindow = [wxNSWindow alloc];
418 CGWindowLevel level = kCGNormalWindowLevel;
420 if ( style & wxFRAME_TOOL_WINDOW )
422 windowstyle |= NSUtilityWindowMask;
423 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
424 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
426 windowstyle |= NSTitledWindowMask ;
429 else if ( ( style & wxPOPUP_WINDOW ) )
431 level = kCGPopUpMenuWindowLevel;
433 if ( ( style & wxBORDER_NONE ) )
435 wclass = kHelpWindowClass ; // has no border
436 attr |= kWindowNoShadowAttribute;
440 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
444 else if ( ( style & wxCAPTION ) )
446 windowstyle |= NSTitledWindowMask ;
448 else if ( ( style & wxFRAME_DRAWER ) )
451 wclass = kDrawerWindowClass;
456 // set these even if we have no title, otherwise the controls won't be visible
457 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
458 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
460 windowstyle |= NSTitledWindowMask ;
463 else if ( ( style & wxNO_BORDER ) )
465 wclass = kSimpleWindowClass ;
469 wclass = kPlainWindowClass ;
474 if ( windowstyle & NSTitledWindowMask )
476 if ( ( style & wxMINIMIZE_BOX ) )
477 windowstyle |= NSMiniaturizableWindowMask ;
479 if ( ( style & wxMAXIMIZE_BOX ) )
480 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
482 if ( ( style & wxRESIZE_BORDER ) )
483 windowstyle |= NSResizableWindowMask ;
485 if ( ( style & wxCLOSE_BOX) )
486 windowstyle |= NSClosableWindowMask ;
488 if ( extraStyle & wxFRAME_EX_METAL)
489 windowstyle |= NSTexturedBackgroundWindowMask;
491 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
492 level = kCGFloatingWindowLevel;
494 if ( ( style & wxSTAY_ON_TOP ) )
495 level = kCGUtilityWindowLevel;
497 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
499 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
501 [m_macWindow initWithContentRect:r
502 styleMask:windowstyle
503 backing:NSBackingStoreBuffered
507 [m_macWindow setLevel:level];
509 [m_macWindow setDelegate:controller];
511 [m_macWindow setAcceptsMouseMovedEvents: YES];
513 if ( ( style & wxFRAME_SHAPED) )
515 [m_macWindow setOpaque:NO];
516 [m_macWindow setAlphaValue:1.0];
520 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
522 m_macWindow = nativeWindow;
525 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
530 void wxNonOwnedWindowCocoaImpl::Raise()
532 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
535 void wxNonOwnedWindowCocoaImpl::Lower()
537 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
540 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
542 [m_macWindow orderFront:nil];
543 [[m_macWindow contentView] setNeedsDisplay: YES];
546 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
550 wxNonOwnedWindow* wxpeer = GetWXPeer();
551 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
552 [m_macWindow makeKeyAndOrderFront:nil];
554 [m_macWindow orderFront:nil];
555 [[m_macWindow contentView] setNeedsDisplay: YES];
558 [m_macWindow orderOut:nil];
562 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
566 return wxWidgetCocoaImpl::
567 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
570 void wxNonOwnedWindowCocoaImpl::Update()
572 [m_macWindow displayIfNeeded];
575 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
577 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
581 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
586 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
590 bool metal = exStyle & wxFRAME_EX_METAL ;
591 int windowStyle = [ m_macWindow styleMask];
592 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
594 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
596 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
598 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
603 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
607 CGWindowLevel level = kCGNormalWindowLevel;
609 if (style & wxSTAY_ON_TOP)
610 level = kCGUtilityWindowLevel;
611 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
612 level = kCGFloatingWindowLevel;
614 [m_macWindow setLevel: level];
618 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
620 if ( style == wxBG_STYLE_TRANSPARENT )
622 [m_macWindow setOpaque:NO];
623 [m_macWindow setBackgroundColor:[NSColor clearColor]];
629 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
634 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
636 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
637 // do not trigger refreshes upon invisible and possible partly created objects
638 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
641 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
643 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
648 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
650 NSRect rect = [m_macWindow frame];
651 width = (int)rect.size.width;
652 height = (int)rect.size.height;
655 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
657 NSRect outer = NSMakeRect(100,100,100,100);
658 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
659 NSRect rect = [[m_macWindow contentView] frame];
660 left = (int)rect.origin.x;
661 top = (int)rect.origin.y;
662 width = (int)rect.size.width;
663 height = (int)rect.size.height;
666 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
668 [m_macWindow setOpaque:NO];
669 [m_macWindow setBackgroundColor:[NSColor clearColor]];
674 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
676 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
679 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
681 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
683 return [m_macWindow isZoomed];
687 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
688 NSRect rectWindow = [m_macWindow frame];
689 return (rectScreen.origin.x == rectWindow.origin.x &&
690 rectScreen.origin.y == rectWindow.origin.y &&
691 rectScreen.size.width == rectWindow.size.width &&
692 rectScreen.size.height == rectWindow.size.height);
696 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
698 return [m_macWindow isMiniaturized];
701 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
704 [m_macWindow miniaturize:nil];
706 [m_macWindow deminiaturize:nil];
709 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
711 [m_macWindow zoom:nil];
715 // http://cocoadevcentral.com/articles/000028.php
720 NSRect m_formerFrame;
723 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
725 return m_macFullScreenData != NULL ;
728 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
732 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
734 data = new FullScreenData();
736 m_macFullScreenData = data ;
737 data->m_formerLevel = [m_macWindow level];
738 data->m_formerFrame = [m_macWindow frame];
739 CGDisplayCapture( kCGDirectMainDisplay );
740 [m_macWindow setLevel:CGShieldingWindowLevel()];
741 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
743 else if ( m_macFullScreenData != NULL )
745 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
746 CGDisplayRelease( kCGDirectMainDisplay );
747 [m_macWindow setLevel:data->m_formerLevel];
748 [m_macWindow setFrame:data->m_formerFrame display:YES];
750 m_macFullScreenData = NULL ;
756 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
758 NSRequestUserAttentionType flagsOSX;
761 case wxUSER_ATTENTION_INFO:
762 flagsOSX = NSInformationalRequest;
765 case wxUSER_ATTENTION_ERROR:
766 flagsOSX = NSCriticalRequest;
770 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
774 [NSApp requestUserAttention:flagsOSX];
777 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
779 wxPoint p((x ? *x : 0), (y ? *y : 0) );
780 NSPoint nspt = wxToNSPoint( NULL, p );
781 nspt = [m_macWindow convertScreenToBase:nspt];
782 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
783 p = wxFromNSPoint([m_macWindow contentView], nspt);
790 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
792 wxPoint p((x ? *x : 0), (y ? *y : 0) );
793 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
794 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
795 nspt = [m_macWindow convertBaseToScreen:nspt];
796 p = wxFromNSPoint( NULL, nspt);
803 bool wxNonOwnedWindowCocoaImpl::IsActive()
805 return [m_macWindow isKeyWindow];
808 void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
810 [m_macWindow setDocumentEdited:modified];
813 bool wxNonOwnedWindowCocoaImpl::IsModified() const
815 return [m_macWindow isDocumentEdited];
818 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
820 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
821 now->Create( parent, nativeWindow );
825 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
826 long style, long extraStyle, const wxString& name )
828 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
829 now->Create( parent, pos, size, style , extraStyle, name );