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
107 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
109 impl = theImplementation;
112 - (wxNonOwnedWindowCocoaImpl*) implementation
117 - (void)doCommandBySelector:(SEL)selector
119 if (shouldHandleSelector(selector) &&
120 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
121 [super doCommandBySelector:selector];
125 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
126 - (void)noResponderFor: (SEL) selector
128 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
130 [super noResponderFor:selector];
131 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
132 // superimpl(self, @selector(noResponderFor:), selector);
136 // We need this for borderless windows, i.e. shaped windows or windows without
137 // a title bar. For more info, see:
138 // http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
139 - (BOOL)canBecomeKeyWindow
146 @interface wxNSPanel : NSPanel
149 wxNonOwnedWindowCocoaImpl* impl;
152 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
153 - (wxNonOwnedWindowCocoaImpl*) implementation;
154 - (void)noResponderFor: (SEL) selector;
157 @implementation wxNSPanel
159 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
161 impl = theImplementation;
164 - (BOOL)canBecomeKeyWindow
169 - (wxNonOwnedWindowCocoaImpl*) implementation
174 - (void)doCommandBySelector:(SEL)selector
176 if (shouldHandleSelector(selector))
177 [super doCommandBySelector:selector];
180 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
181 - (void)noResponderFor: (SEL) selector
183 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
185 [super noResponderFor:selector];
186 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
187 // superimpl(self, @selector(noResponderFor:), selector);
198 @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
202 - (void)windowDidResize:(NSNotification *)notification;
203 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
204 - (void)windowDidResignKey:(NSNotification *)notification;
205 - (void)windowDidBecomeKey:(NSNotification *)notification;
206 - (void)windowDidMove:(NSNotification *)notification;
207 - (BOOL)windowShouldClose:(id)window;
208 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
212 @implementation wxNonOwnedWindowController
220 - (BOOL)windowShouldClose:(id)nwindow
222 wxNSWindow* window = (wxNSWindow*) nwindow;
223 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
226 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
233 - (NSSize)windowWillResize:(NSWindow *)win
234 toSize:(NSSize)proposedFrameSize
236 NSRect frame = [win frame];
237 wxRect wxframe = wxFromNSRect( NULL, frame );
238 wxframe.SetWidth( (int)proposedFrameSize.width );
239 wxframe.SetHeight( (int)proposedFrameSize.height );
240 wxNSWindow* window = (wxNSWindow*) win;
241 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
244 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
247 wxpeer->HandleResizing( 0, &wxframe );
248 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
253 return proposedFrameSize;
256 - (void)windowDidResize:(NSNotification *)notification
258 wxNSWindow* window = (wxNSWindow*) [notification object];
259 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
262 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
264 wxpeer->HandleResized(0);
268 - (void)windowDidMove:(NSNotification *)notification
270 wxNSWindow* window = (wxNSWindow*) [notification object];
271 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
274 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
276 wxpeer->HandleMoved(0);
280 - (void)windowDidBecomeKey:(NSNotification *)notification
282 wxNSWindow* window = (wxNSWindow*) [notification object];
283 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
286 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
288 wxpeer->HandleActivated(0, true);
292 - (void)windowDidResignKey:(NSNotification *)notification
294 wxNSWindow* window = (wxNSWindow*) [notification object];
295 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
298 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
301 wxpeer->HandleActivated(0, false);
302 // Needed for popup window since the firstResponder
303 // (focus in wx) doesn't change when this
304 // TLW becomes inactive.
305 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
306 event.SetEventObject(wxpeer);
307 wxpeer->HandleWindowEvent(event);
312 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
316 if ([anObject isKindOfClass:[wxNSTextField class]])
318 wxNSTextField* tf = (wxNSTextField*) anObject;
319 wxNSTextFieldEditor* editor = [tf fieldEditor];
322 editor = [[wxNSTextFieldEditor alloc] init];
323 [editor setFieldEditor:YES];
324 [tf setFieldEditor:editor];
332 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
334 wxNonOwnedWindowCocoaImpl* windowimpl = [(wxNSWindow*)window implementation];
337 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
338 wxMaximizeEvent event(wxpeer->GetId());
339 event.SetEventObject(wxpeer);
340 return !wxpeer->HandleWindowEvent(event);
347 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
349 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
350 wxNonOwnedWindowImpl(nonownedwnd)
353 m_macFullScreenData = NULL;
356 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
359 m_macFullScreenData = NULL;
362 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
364 if ( !m_wxPeer->IsNativeWindowWrapper() )
366 [m_macWindow setImplementation:nil];
367 [m_macWindow setDelegate:nil];
368 [m_macWindow release];
372 void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
374 if ( !m_wxPeer->IsNativeWindowWrapper() )
376 [m_macWindow setDelegate:nil];
380 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
381 long style, long extraStyle, const wxString& WXUNUSED(name) )
383 static wxNonOwnedWindowController* controller = NULL;
386 controller =[[wxNonOwnedWindowController alloc] init];
389 int windowstyle = NSBorderlessWindowMask;
391 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
392 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
394 m_macWindow = [wxNSPanel alloc];
397 m_macWindow = [wxNSWindow alloc];
399 CGWindowLevel level = kCGNormalWindowLevel;
401 if ( style & wxFRAME_TOOL_WINDOW )
403 windowstyle |= NSUtilityWindowMask;
404 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
405 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
407 windowstyle |= NSTitledWindowMask ;
410 else if ( ( style & wxPOPUP_WINDOW ) )
412 level = kCGPopUpMenuWindowLevel;
414 if ( ( style & wxBORDER_NONE ) )
416 wclass = kHelpWindowClass ; // has no border
417 attr |= kWindowNoShadowAttribute;
421 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
425 else if ( ( style & wxCAPTION ) )
427 windowstyle |= NSTitledWindowMask ;
429 else if ( ( style & wxFRAME_DRAWER ) )
432 wclass = kDrawerWindowClass;
437 // set these even if we have no title, otherwise the controls won't be visible
438 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
439 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
441 windowstyle |= NSTitledWindowMask ;
444 else if ( ( style & wxNO_BORDER ) )
446 wclass = kSimpleWindowClass ;
450 wclass = kPlainWindowClass ;
455 if ( windowstyle & NSTitledWindowMask )
457 if ( ( style & wxMINIMIZE_BOX ) )
458 windowstyle |= NSMiniaturizableWindowMask ;
460 if ( ( style & wxMAXIMIZE_BOX ) )
461 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
463 if ( ( style & wxRESIZE_BORDER ) )
464 windowstyle |= NSResizableWindowMask ;
466 if ( ( style & wxCLOSE_BOX) )
467 windowstyle |= NSClosableWindowMask ;
469 if ( extraStyle & wxFRAME_EX_METAL)
470 windowstyle |= NSTexturedBackgroundWindowMask;
472 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
473 level = kCGFloatingWindowLevel;
475 if ( ( style & wxSTAY_ON_TOP ) )
476 level = kCGUtilityWindowLevel;
478 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
480 [m_macWindow setImplementation:this];
481 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
483 [m_macWindow initWithContentRect:r
484 styleMask:windowstyle
485 backing:NSBackingStoreBuffered
489 [m_macWindow setLevel:level];
491 [m_macWindow setDelegate:controller];
493 [m_macWindow setAcceptsMouseMovedEvents: YES];
495 if ( ( style & wxFRAME_SHAPED) )
497 [m_macWindow setOpaque:NO];
498 [m_macWindow setAlphaValue:1.0];
502 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
504 m_macWindow = nativeWindow;
507 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
512 void wxNonOwnedWindowCocoaImpl::Raise()
514 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
517 void wxNonOwnedWindowCocoaImpl::Lower()
519 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
522 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
524 [m_macWindow orderFront:nil];
525 [[m_macWindow contentView] setNeedsDisplay: YES];
528 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
532 wxNonOwnedWindow* wxpeer = GetWXPeer();
533 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
534 [m_macWindow makeKeyAndOrderFront:nil];
536 [m_macWindow orderFront:nil];
537 [[m_macWindow contentView] setNeedsDisplay: YES];
540 [m_macWindow orderOut:nil];
544 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
548 return wxWidgetCocoaImpl::
549 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
552 void wxNonOwnedWindowCocoaImpl::Update()
554 [m_macWindow displayIfNeeded];
557 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
559 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
563 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
568 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
572 bool metal = exStyle & wxFRAME_EX_METAL ;
573 int windowStyle = [ m_macWindow styleMask];
574 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
576 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
578 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
580 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
585 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
589 CGWindowLevel level = kCGNormalWindowLevel;
591 if (style & wxSTAY_ON_TOP)
592 level = kCGUtilityWindowLevel;
593 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
594 level = kCGFloatingWindowLevel;
596 [m_macWindow setLevel: level];
600 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
602 if ( style == wxBG_STYLE_TRANSPARENT )
604 [m_macWindow setOpaque:NO];
605 [m_macWindow setBackgroundColor:[NSColor clearColor]];
611 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
616 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
618 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
619 // do not trigger refreshes upon invisible and possible partly created objects
620 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
623 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
625 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
630 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
632 NSRect rect = [m_macWindow frame];
633 width = (int)rect.size.width;
634 height = (int)rect.size.height;
637 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
639 NSRect outer = NSMakeRect(100,100,100,100);
640 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
641 NSRect rect = [[m_macWindow contentView] frame];
642 left = (int)rect.origin.x;
643 top = (int)rect.origin.y;
644 width = (int)rect.size.width;
645 height = (int)rect.size.height;
648 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
650 [m_macWindow setOpaque:NO];
651 [m_macWindow setBackgroundColor:[NSColor clearColor]];
656 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
658 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
661 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
663 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
665 return [m_macWindow isZoomed];
669 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
670 NSRect rectWindow = [m_macWindow frame];
671 return (rectScreen.origin.x == rectWindow.origin.x &&
672 rectScreen.origin.y == rectWindow.origin.y &&
673 rectScreen.size.width == rectWindow.size.width &&
674 rectScreen.size.height == rectWindow.size.height);
678 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
680 return [m_macWindow isMiniaturized];
683 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
686 [m_macWindow miniaturize:nil];
688 [m_macWindow deminiaturize:nil];
691 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
693 [m_macWindow zoom:nil];
697 // http://cocoadevcentral.com/articles/000028.php
702 NSRect m_formerFrame;
705 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
707 return m_macFullScreenData != NULL ;
710 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
714 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
716 data = new FullScreenData();
718 m_macFullScreenData = data ;
719 data->m_formerLevel = [m_macWindow level];
720 data->m_formerFrame = [m_macWindow frame];
721 CGDisplayCapture( kCGDirectMainDisplay );
722 [m_macWindow setLevel:CGShieldingWindowLevel()];
723 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
725 else if ( m_macFullScreenData != NULL )
727 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
728 CGDisplayRelease( kCGDirectMainDisplay );
729 [m_macWindow setLevel:data->m_formerLevel];
730 [m_macWindow setFrame:data->m_formerFrame display:YES];
732 m_macFullScreenData = NULL ;
738 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
740 NSRequestUserAttentionType flagsOSX;
743 case wxUSER_ATTENTION_INFO:
744 flagsOSX = NSInformationalRequest;
747 case wxUSER_ATTENTION_ERROR:
748 flagsOSX = NSCriticalRequest;
752 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
756 [NSApp requestUserAttention:flagsOSX];
759 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
761 wxPoint p((x ? *x : 0), (y ? *y : 0) );
762 NSPoint nspt = wxToNSPoint( NULL, p );
763 nspt = [m_macWindow convertScreenToBase:nspt];
764 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
765 p = wxFromNSPoint([m_macWindow contentView], nspt);
772 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
774 wxPoint p((x ? *x : 0), (y ? *y : 0) );
775 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
776 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
777 nspt = [m_macWindow convertBaseToScreen:nspt];
778 p = wxFromNSPoint( NULL, nspt);
785 bool wxNonOwnedWindowCocoaImpl::IsActive()
787 return [m_macWindow isKeyWindow];
790 void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
792 [m_macWindow setDocumentEdited:modified];
795 bool wxNonOwnedWindowCocoaImpl::IsModified() const
797 return [m_macWindow isDocumentEdited];
800 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
802 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
803 now->Create( parent, nativeWindow );
807 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
808 long style, long extraStyle, const wxString& name )
810 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
811 now->Create( parent, pos, size, style , extraStyle, name );