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);
137 @interface wxNSPanel : NSPanel
140 wxNonOwnedWindowCocoaImpl* impl;
143 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
144 - (wxNonOwnedWindowCocoaImpl*) implementation;
145 - (void)noResponderFor: (SEL) selector;
148 @implementation wxNSPanel
150 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
152 impl = theImplementation;
155 - (BOOL)canBecomeKeyWindow
160 - (wxNonOwnedWindowCocoaImpl*) implementation
165 - (void)doCommandBySelector:(SEL)selector
167 if (shouldHandleSelector(selector))
168 [super doCommandBySelector:selector];
171 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
172 - (void)noResponderFor: (SEL) selector
174 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
176 [super noResponderFor:selector];
177 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
178 // superimpl(self, @selector(noResponderFor:), selector);
189 @interface wxNonOwnedWindowController : NSObject
193 - (void)windowDidResize:(NSNotification *)notification;
194 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
195 - (void)windowDidResignKey:(NSNotification *)notification;
196 - (void)windowDidBecomeKey:(NSNotification *)notification;
197 - (void)windowDidMove:(NSNotification *)notification;
198 - (BOOL)windowShouldClose:(id)window;
202 @implementation wxNonOwnedWindowController
210 - (BOOL)windowShouldClose:(id)nwindow
212 wxNSWindow* window = (wxNSWindow*) nwindow;
213 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
216 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
223 - (NSSize)windowWillResize:(NSWindow *)win
224 toSize:(NSSize)proposedFrameSize
226 NSRect frame = [win frame];
227 wxRect wxframe = wxFromNSRect( NULL, frame );
228 wxframe.SetWidth( (int)proposedFrameSize.width );
229 wxframe.SetHeight( (int)proposedFrameSize.height );
230 wxNSWindow* window = (wxNSWindow*) win;
231 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
234 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
237 wxpeer->HandleResizing( 0, &wxframe );
238 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
243 return proposedFrameSize;
246 - (void)windowDidResize:(NSNotification *)notification
248 wxNSWindow* window = (wxNSWindow*) [notification object];
249 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
252 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
254 wxpeer->HandleResized(0);
258 - (void)windowDidMove:(NSNotification *)notification
260 wxNSWindow* window = (wxNSWindow*) [notification object];
261 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
264 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
266 wxpeer->HandleMoved(0);
270 - (void)windowDidBecomeKey:(NSNotification *)notification
272 wxNSWindow* window = (wxNSWindow*) [notification object];
273 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
276 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
278 wxpeer->HandleActivated(0, true);
282 - (void)windowDidResignKey:(NSNotification *)notification
284 wxNSWindow* window = (wxNSWindow*) [notification object];
285 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
288 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
291 wxpeer->HandleActivated(0, false);
292 // Needed for popup window since the firstResponder
293 // (focus in wx) doesn't change when this
294 // TLW becomes inactive.
295 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
296 event.SetEventObject(wxpeer);
297 wxpeer->HandleWindowEvent(event);
302 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
306 if ([anObject isKindOfClass:[wxNSTextField class]])
308 wxNSTextField* tf = (wxNSTextField*) anObject;
309 wxNSTextFieldEditor* editor = [tf fieldEditor];
312 editor = [[wxNSTextFieldEditor alloc] init];
313 [editor setFieldEditor:YES];
314 [tf setFieldEditor:editor];
324 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
326 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
327 wxNonOwnedWindowImpl(nonownedwnd)
330 m_macFullScreenData = NULL;
333 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
336 m_macFullScreenData = NULL;
339 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
341 [m_macWindow setImplementation:nil];
342 [m_macWindow setDelegate:nil];
343 [m_macWindow release];
346 void wxNonOwnedWindowCocoaImpl::Destroy()
348 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
351 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
352 long style, long extraStyle, const wxString& WXUNUSED(name) )
354 static wxNonOwnedWindowController* controller = NULL;
357 controller =[[wxNonOwnedWindowController alloc] init];
360 int windowstyle = NSBorderlessWindowMask;
362 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
363 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
365 m_macWindow = [wxNSPanel alloc];
368 m_macWindow = [wxNSWindow alloc];
370 CGWindowLevel level = kCGNormalWindowLevel;
372 if ( style & wxFRAME_TOOL_WINDOW )
374 windowstyle |= NSUtilityWindowMask;
375 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
376 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
378 windowstyle |= NSTitledWindowMask ;
381 else if ( ( style & wxPOPUP_WINDOW ) )
383 level = kCGPopUpMenuWindowLevel;
385 if ( ( style & wxBORDER_NONE ) )
387 wclass = kHelpWindowClass ; // has no border
388 attr |= kWindowNoShadowAttribute;
392 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
396 else if ( ( style & wxCAPTION ) )
398 windowstyle |= NSTitledWindowMask ;
400 else if ( ( style & wxFRAME_DRAWER ) )
403 wclass = kDrawerWindowClass;
408 // set these even if we have no title, otherwise the controls won't be visible
409 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
410 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
412 windowstyle |= NSTitledWindowMask ;
415 else if ( ( style & wxNO_BORDER ) )
417 wclass = kSimpleWindowClass ;
421 wclass = kPlainWindowClass ;
426 if ( windowstyle & NSTitledWindowMask )
428 if ( ( style & wxMINIMIZE_BOX ) )
429 windowstyle |= NSMiniaturizableWindowMask ;
431 if ( ( style & wxMAXIMIZE_BOX ) )
432 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
434 if ( ( style & wxRESIZE_BORDER ) )
435 windowstyle |= NSResizableWindowMask ;
437 if ( ( style & wxCLOSE_BOX) )
438 windowstyle |= NSClosableWindowMask ;
440 if ( extraStyle & wxFRAME_EX_METAL)
441 windowstyle |= NSTexturedBackgroundWindowMask;
443 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
444 level = kCGFloatingWindowLevel;
446 if ( ( style & wxSTAY_ON_TOP ) )
447 level = kCGUtilityWindowLevel;
449 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
451 [m_macWindow setImplementation:this];
452 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
454 [m_macWindow initWithContentRect:r
455 styleMask:windowstyle
456 backing:NSBackingStoreBuffered
460 [m_macWindow setLevel:level];
462 [m_macWindow setDelegate:controller];
464 [m_macWindow setAcceptsMouseMovedEvents: YES];
468 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
473 void wxNonOwnedWindowCocoaImpl::Raise()
475 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
478 void wxNonOwnedWindowCocoaImpl::Lower()
480 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
483 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
485 [m_macWindow orderBack:nil];
486 [[m_macWindow contentView] setNeedsDisplay: YES];
489 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
493 wxNonOwnedWindow* wxpeer = GetWXPeer();
494 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
495 [m_macWindow makeKeyAndOrderFront:nil];
497 [m_macWindow orderFront:nil];
498 [[m_macWindow contentView] setNeedsDisplay: YES];
501 [m_macWindow orderOut:nil];
505 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
509 return wxWidgetCocoaImpl::
510 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
513 void wxNonOwnedWindowCocoaImpl::Update()
515 [m_macWindow displayIfNeeded];
518 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
520 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
524 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
529 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
533 bool metal = exStyle & wxFRAME_EX_METAL ;
534 int windowStyle = [ m_macWindow styleMask];
535 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
537 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
539 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
541 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
546 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
550 CGWindowLevel level = kCGNormalWindowLevel;
552 if (style & wxSTAY_ON_TOP)
553 level = kCGUtilityWindowLevel;
554 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
555 level = kCGFloatingWindowLevel;
557 [m_macWindow setLevel: level];
561 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
563 if ( style == wxBG_STYLE_TRANSPARENT )
565 [m_macWindow setOpaque:NO];
566 [m_macWindow setBackgroundColor:[NSColor clearColor]];
572 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
577 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
579 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
580 // do not trigger refreshes upon invisible and possible partly created objects
581 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
584 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
586 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
591 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
593 NSRect rect = [m_macWindow frame];
594 width = (int)rect.size.width;
595 height = (int)rect.size.height;
598 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
600 NSRect outer = NSMakeRect(100,100,100,100);
601 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
602 NSRect rect = [[m_macWindow contentView] frame];
603 left = (int)rect.origin.x;
604 top = (int)rect.origin.y;
605 width = (int)rect.size.width;
606 height = (int)rect.size.height;
609 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
611 [m_macWindow setOpaque:NO];
612 [m_macWindow setBackgroundColor:[NSColor clearColor]];
617 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
619 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
622 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
624 return [m_macWindow isZoomed];
627 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
629 return [m_macWindow isMiniaturized];
632 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
635 [m_macWindow miniaturize:nil];
637 [m_macWindow deminiaturize:nil];
640 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
642 [m_macWindow zoom:nil];
646 // http://cocoadevcentral.com/articles/000028.php
651 NSRect m_formerFrame;
654 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
656 return m_macFullScreenData != NULL ;
659 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
663 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
665 data = new FullScreenData();
667 m_macFullScreenData = data ;
668 data->m_formerLevel = [m_macWindow level];
669 data->m_formerFrame = [m_macWindow frame];
670 CGDisplayCapture( kCGDirectMainDisplay );
671 [m_macWindow setLevel:CGShieldingWindowLevel()];
672 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
674 else if ( m_macFullScreenData != NULL )
676 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
677 CGDisplayRelease( kCGDirectMainDisplay );
678 [m_macWindow setLevel:data->m_formerLevel];
679 [m_macWindow setFrame:data->m_formerFrame display:YES];
681 m_macFullScreenData = NULL ;
687 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
691 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
693 wxPoint p((x ? *x : 0), (y ? *y : 0) );
694 NSPoint nspt = wxToNSPoint( NULL, p );
695 nspt = [m_macWindow convertScreenToBase:nspt];
696 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
697 p = wxFromNSPoint([m_macWindow contentView], nspt);
704 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
706 wxPoint p((x ? *x : 0), (y ? *y : 0) );
707 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
708 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
709 nspt = [m_macWindow convertBaseToScreen:nspt];
710 p = wxFromNSPoint( NULL, nspt);
717 bool wxNonOwnedWindowCocoaImpl::IsActive()
719 return [m_macWindow isKeyWindow];
722 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
723 long style, long extraStyle, const wxString& name )
725 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
726 now->Create( parent, pos, size, style , extraStyle, name );