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
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;
210 @implementation wxNonOwnedWindowController
218 - (BOOL)windowShouldClose:(id)nwindow
220 wxNSWindow* window = (wxNSWindow*) nwindow;
221 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
224 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
231 - (NSSize)windowWillResize:(NSWindow *)win
232 toSize:(NSSize)proposedFrameSize
234 NSRect frame = [win frame];
235 wxRect wxframe = wxFromNSRect( NULL, frame );
236 wxframe.SetWidth( (int)proposedFrameSize.width );
237 wxframe.SetHeight( (int)proposedFrameSize.height );
238 wxNSWindow* window = (wxNSWindow*) win;
239 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
242 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
245 wxpeer->HandleResizing( 0, &wxframe );
246 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
251 return proposedFrameSize;
254 - (void)windowDidResize:(NSNotification *)notification
256 wxNSWindow* window = (wxNSWindow*) [notification object];
257 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
260 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
262 wxpeer->HandleResized(0);
266 - (void)windowDidMove:(NSNotification *)notification
268 wxNSWindow* window = (wxNSWindow*) [notification object];
269 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
272 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
274 wxpeer->HandleMoved(0);
278 - (void)windowDidBecomeKey:(NSNotification *)notification
280 wxNSWindow* window = (wxNSWindow*) [notification object];
281 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
284 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
286 wxpeer->HandleActivated(0, true);
290 - (void)windowDidResignKey:(NSNotification *)notification
292 wxNSWindow* window = (wxNSWindow*) [notification object];
293 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
296 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
299 wxpeer->HandleActivated(0, false);
300 // Needed for popup window since the firstResponder
301 // (focus in wx) doesn't change when this
302 // TLW becomes inactive.
303 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
304 event.SetEventObject(wxpeer);
305 wxpeer->HandleWindowEvent(event);
310 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
314 if ([anObject isKindOfClass:[wxNSTextField class]])
316 wxNSTextField* tf = (wxNSTextField*) anObject;
317 wxNSTextFieldEditor* editor = [tf fieldEditor];
320 editor = [[wxNSTextFieldEditor alloc] init];
321 [editor setFieldEditor:YES];
322 [tf setFieldEditor:editor];
332 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
334 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
335 wxNonOwnedWindowImpl(nonownedwnd)
338 m_macFullScreenData = NULL;
341 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
344 m_macFullScreenData = NULL;
347 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
349 [m_macWindow setImplementation:nil];
350 [m_macWindow setDelegate:nil];
351 [m_macWindow release];
354 void wxNonOwnedWindowCocoaImpl::Destroy()
356 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
359 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
360 long style, long extraStyle, const wxString& WXUNUSED(name) )
362 static wxNonOwnedWindowController* controller = NULL;
365 controller =[[wxNonOwnedWindowController alloc] init];
368 int windowstyle = NSBorderlessWindowMask;
370 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
371 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
373 m_macWindow = [wxNSPanel alloc];
376 m_macWindow = [wxNSWindow alloc];
378 CGWindowLevel level = kCGNormalWindowLevel;
380 if ( style & wxFRAME_TOOL_WINDOW )
382 windowstyle |= NSUtilityWindowMask;
383 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
384 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
386 windowstyle |= NSTitledWindowMask ;
389 else if ( ( style & wxPOPUP_WINDOW ) )
391 level = kCGPopUpMenuWindowLevel;
393 if ( ( style & wxBORDER_NONE ) )
395 wclass = kHelpWindowClass ; // has no border
396 attr |= kWindowNoShadowAttribute;
400 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
404 else if ( ( style & wxCAPTION ) )
406 windowstyle |= NSTitledWindowMask ;
408 else if ( ( style & wxFRAME_DRAWER ) )
411 wclass = kDrawerWindowClass;
416 // set these even if we have no title, otherwise the controls won't be visible
417 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
418 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
420 windowstyle |= NSTitledWindowMask ;
423 else if ( ( style & wxNO_BORDER ) )
425 wclass = kSimpleWindowClass ;
429 wclass = kPlainWindowClass ;
434 if ( windowstyle & NSTitledWindowMask )
436 if ( ( style & wxMINIMIZE_BOX ) )
437 windowstyle |= NSMiniaturizableWindowMask ;
439 if ( ( style & wxMAXIMIZE_BOX ) )
440 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
442 if ( ( style & wxRESIZE_BORDER ) )
443 windowstyle |= NSResizableWindowMask ;
445 if ( ( style & wxCLOSE_BOX) )
446 windowstyle |= NSClosableWindowMask ;
448 if ( extraStyle & wxFRAME_EX_METAL)
449 windowstyle |= NSTexturedBackgroundWindowMask;
451 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
452 level = kCGFloatingWindowLevel;
454 if ( ( style & wxSTAY_ON_TOP ) )
455 level = kCGUtilityWindowLevel;
457 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
459 [m_macWindow setImplementation:this];
460 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
462 [m_macWindow initWithContentRect:r
463 styleMask:windowstyle
464 backing:NSBackingStoreBuffered
468 [m_macWindow setLevel:level];
470 [m_macWindow setDelegate:controller];
472 [m_macWindow setAcceptsMouseMovedEvents: YES];
476 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
481 void wxNonOwnedWindowCocoaImpl::Raise()
483 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
486 void wxNonOwnedWindowCocoaImpl::Lower()
488 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
491 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
493 [m_macWindow orderFront:nil];
494 [[m_macWindow contentView] setNeedsDisplay: YES];
497 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
501 wxNonOwnedWindow* wxpeer = GetWXPeer();
502 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
503 [m_macWindow makeKeyAndOrderFront:nil];
505 [m_macWindow orderFront:nil];
506 [[m_macWindow contentView] setNeedsDisplay: YES];
509 [m_macWindow orderOut:nil];
513 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
517 return wxWidgetCocoaImpl::
518 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
521 void wxNonOwnedWindowCocoaImpl::Update()
523 [m_macWindow displayIfNeeded];
526 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
528 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
532 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
537 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
541 bool metal = exStyle & wxFRAME_EX_METAL ;
542 int windowStyle = [ m_macWindow styleMask];
543 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
545 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
547 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
549 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
554 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
558 CGWindowLevel level = kCGNormalWindowLevel;
560 if (style & wxSTAY_ON_TOP)
561 level = kCGUtilityWindowLevel;
562 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
563 level = kCGFloatingWindowLevel;
565 [m_macWindow setLevel: level];
569 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
571 if ( style == wxBG_STYLE_TRANSPARENT )
573 [m_macWindow setOpaque:NO];
574 [m_macWindow setBackgroundColor:[NSColor clearColor]];
580 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
585 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
587 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
588 // do not trigger refreshes upon invisible and possible partly created objects
589 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
592 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
594 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
599 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
601 NSRect rect = [m_macWindow frame];
602 width = (int)rect.size.width;
603 height = (int)rect.size.height;
606 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
608 NSRect outer = NSMakeRect(100,100,100,100);
609 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
610 NSRect rect = [[m_macWindow contentView] frame];
611 left = (int)rect.origin.x;
612 top = (int)rect.origin.y;
613 width = (int)rect.size.width;
614 height = (int)rect.size.height;
617 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
619 [m_macWindow setOpaque:NO];
620 [m_macWindow setBackgroundColor:[NSColor clearColor]];
625 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
627 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
630 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
632 return [m_macWindow isZoomed];
635 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
637 return [m_macWindow isMiniaturized];
640 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
643 [m_macWindow miniaturize:nil];
645 [m_macWindow deminiaturize:nil];
648 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
650 [m_macWindow zoom:nil];
654 // http://cocoadevcentral.com/articles/000028.php
659 NSRect m_formerFrame;
662 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
664 return m_macFullScreenData != NULL ;
667 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
671 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
673 data = new FullScreenData();
675 m_macFullScreenData = data ;
676 data->m_formerLevel = [m_macWindow level];
677 data->m_formerFrame = [m_macWindow frame];
678 CGDisplayCapture( kCGDirectMainDisplay );
679 [m_macWindow setLevel:CGShieldingWindowLevel()];
680 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
682 else if ( m_macFullScreenData != NULL )
684 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
685 CGDisplayRelease( kCGDirectMainDisplay );
686 [m_macWindow setLevel:data->m_formerLevel];
687 [m_macWindow setFrame:data->m_formerFrame display:YES];
689 m_macFullScreenData = NULL ;
695 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
699 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
701 wxPoint p((x ? *x : 0), (y ? *y : 0) );
702 NSPoint nspt = wxToNSPoint( NULL, p );
703 nspt = [m_macWindow convertScreenToBase:nspt];
704 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
705 p = wxFromNSPoint([m_macWindow contentView], nspt);
712 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
714 wxPoint p((x ? *x : 0), (y ? *y : 0) );
715 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
716 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
717 nspt = [m_macWindow convertBaseToScreen:nspt];
718 p = wxFromNSPoint( NULL, nspt);
725 bool wxNonOwnedWindowCocoaImpl::IsActive()
727 return [m_macWindow isKeyWindow];
730 void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
732 [m_macWindow setDocumentEdited:modified];
735 bool wxNonOwnedWindowCocoaImpl::IsModified() const
737 return [m_macWindow isDocumentEdited];
740 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
741 long style, long extraStyle, const wxString& name )
743 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
744 now->Create( parent, pos, size, style , extraStyle, name );