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 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
92 - (wxNonOwnedWindowCocoaImpl*) implementation;
93 - (void)noResponderFor: (SEL) selector;
96 @implementation wxNSWindow
98 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
100 impl = theImplementation;
103 - (wxNonOwnedWindowCocoaImpl*) implementation
108 - (void)doCommandBySelector:(SEL)selector
110 if (shouldHandleSelector(selector) &&
111 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
112 [super doCommandBySelector:selector];
116 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
117 - (void)noResponderFor: (SEL) selector
119 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
121 [super noResponderFor:selector];
122 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
123 // superimpl(self, @selector(noResponderFor:), selector);
129 @interface wxNSPanel : NSPanel
132 wxNonOwnedWindowCocoaImpl* impl;
135 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
136 - (wxNonOwnedWindowCocoaImpl*) implementation;
137 - (void)noResponderFor: (SEL) selector;
140 @implementation wxNSPanel
142 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
144 impl = theImplementation;
147 - (BOOL)canBecomeKeyWindow
152 - (wxNonOwnedWindowCocoaImpl*) implementation
157 - (void)doCommandBySelector:(SEL)selector
159 if (shouldHandleSelector(selector))
160 [super doCommandBySelector:selector];
163 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
164 - (void)noResponderFor: (SEL) selector
166 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
168 [super noResponderFor:selector];
169 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
170 // superimpl(self, @selector(noResponderFor:), selector);
181 @interface wxNonOwnedWindowController : NSObject
185 - (void)windowDidResize:(NSNotification *)notification;
186 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
187 - (void)windowDidResignKey:(NSNotification *)notification;
188 - (void)windowDidBecomeKey:(NSNotification *)notification;
189 - (void)windowDidMove:(NSNotification *)notification;
190 - (BOOL)windowShouldClose:(id)window;
194 @implementation wxNonOwnedWindowController
202 - (BOOL)windowShouldClose:(id)nwindow
204 wxNSWindow* window = (wxNSWindow*) nwindow;
205 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
208 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
215 - (NSSize)windowWillResize:(NSWindow *)win
216 toSize:(NSSize)proposedFrameSize
218 NSRect frame = [win frame];
219 wxRect wxframe = wxFromNSRect( NULL, frame );
220 wxframe.SetWidth( (int)proposedFrameSize.width );
221 wxframe.SetHeight( (int)proposedFrameSize.height );
222 wxNSWindow* window = (wxNSWindow*) win;
223 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
226 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
229 wxpeer->HandleResizing( 0, &wxframe );
230 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
235 return proposedFrameSize;
238 - (void)windowDidResize:(NSNotification *)notification
240 wxNSWindow* window = (wxNSWindow*) [notification object];
241 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
244 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
246 wxpeer->HandleResized(0);
250 - (void)windowDidMove:(NSNotification *)notification
252 wxNSWindow* window = (wxNSWindow*) [notification object];
253 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
256 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
258 wxpeer->HandleMoved(0);
262 - (void)windowDidBecomeKey:(NSNotification *)notification
264 wxNSWindow* window = (wxNSWindow*) [notification object];
265 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
268 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
270 wxpeer->HandleActivated(0, true);
274 - (void)windowDidResignKey:(NSNotification *)notification
276 wxNSWindow* window = (wxNSWindow*) [notification object];
277 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
280 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
283 wxpeer->HandleActivated(0, false);
284 // Needed for popup window since the firstResponder
285 // (focus in wx) doesn't change when this
286 // TLW becomes inactive.
287 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
288 event.SetEventObject(wxpeer);
289 wxpeer->HandleWindowEvent(event);
294 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
298 if ([anObject isKindOfClass:[wxNSTextField class]])
300 wxNSTextField* tf = (wxNSTextField*) anObject;
301 wxNSTextFieldEditor* editor = [tf fieldEditor];
304 editor = [[wxNSTextFieldEditor alloc] init];
305 [editor setFieldEditor:YES];
306 [tf setFieldEditor:editor];
316 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
318 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
319 wxNonOwnedWindowImpl(nonownedwnd)
322 m_macFullScreenData = NULL;
325 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
328 m_macFullScreenData = NULL;
331 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
333 [m_macWindow setImplementation:nil];
334 [m_macWindow setDelegate:nil];
335 [m_macWindow release];
338 void wxNonOwnedWindowCocoaImpl::Destroy()
340 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
343 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
344 long style, long extraStyle, const wxString& WXUNUSED(name) )
346 static wxNonOwnedWindowController* controller = NULL;
349 controller =[[wxNonOwnedWindowController alloc] init];
352 int windowstyle = NSBorderlessWindowMask;
354 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
355 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
357 m_macWindow = [wxNSPanel alloc];
360 m_macWindow = [wxNSWindow alloc];
362 CGWindowLevel level = kCGNormalWindowLevel;
364 if ( style & wxFRAME_TOOL_WINDOW )
366 windowstyle |= NSUtilityWindowMask;
367 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
368 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
370 windowstyle |= NSTitledWindowMask ;
373 else if ( ( style & wxPOPUP_WINDOW ) )
375 level = kCGPopUpMenuWindowLevel;
377 if ( ( style & wxBORDER_NONE ) )
379 wclass = kHelpWindowClass ; // has no border
380 attr |= kWindowNoShadowAttribute;
384 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
388 else if ( ( style & wxCAPTION ) )
390 windowstyle |= NSTitledWindowMask ;
392 else if ( ( style & wxFRAME_DRAWER ) )
395 wclass = kDrawerWindowClass;
400 // set these even if we have no title, otherwise the controls won't be visible
401 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
402 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
404 windowstyle |= NSTitledWindowMask ;
407 else if ( ( style & wxNO_BORDER ) )
409 wclass = kSimpleWindowClass ;
413 wclass = kPlainWindowClass ;
418 if ( windowstyle & NSTitledWindowMask )
420 if ( ( style & wxMINIMIZE_BOX ) )
421 windowstyle |= NSMiniaturizableWindowMask ;
423 if ( ( style & wxMAXIMIZE_BOX ) )
424 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
426 if ( ( style & wxRESIZE_BORDER ) )
427 windowstyle |= NSResizableWindowMask ;
429 if ( ( style & wxCLOSE_BOX) )
430 windowstyle |= NSClosableWindowMask ;
432 if ( extraStyle & wxFRAME_EX_METAL)
433 windowstyle |= NSTexturedBackgroundWindowMask;
435 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
436 level = kCGFloatingWindowLevel;
438 if ( ( style & wxSTAY_ON_TOP ) )
439 level = kCGUtilityWindowLevel;
441 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
443 [m_macWindow setImplementation:this];
445 [m_macWindow initWithContentRect:r
446 styleMask:windowstyle
447 backing:NSBackingStoreBuffered
451 [m_macWindow setLevel:level];
453 [m_macWindow setDelegate:controller];
455 [m_macWindow setAcceptsMouseMovedEvents: YES];
459 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
464 void wxNonOwnedWindowCocoaImpl::Raise()
466 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
469 void wxNonOwnedWindowCocoaImpl::Lower()
471 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
474 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
476 [[m_macWindow contentView] setNeedsDisplay:YES];
479 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
483 wxNonOwnedWindow* wxpeer = GetWXPeer();
484 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
485 [m_macWindow makeKeyAndOrderFront:nil];
487 [m_macWindow orderFront:nil];
488 ShowWithoutActivating();
491 [m_macWindow orderOut:nil];
495 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
499 return wxWidgetCocoaImpl::
500 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
503 void wxNonOwnedWindowCocoaImpl::Update()
505 [m_macWindow displayIfNeeded];
508 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
510 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
514 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
519 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
523 bool metal = exStyle & wxFRAME_EX_METAL ;
524 int windowStyle = [ m_macWindow styleMask];
525 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
527 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
529 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
531 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
536 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
540 CGWindowLevel level = kCGNormalWindowLevel;
542 if (style & wxSTAY_ON_TOP)
543 level = kCGUtilityWindowLevel;
544 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
545 level = kCGFloatingWindowLevel;
547 [m_macWindow setLevel: level];
551 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle WXUNUSED(style))
556 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
561 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
563 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
564 // do not trigger refreshes upon invisible and possible partly created objects
565 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
568 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
570 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
575 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
577 NSRect rect = [m_macWindow frame];
578 width = (int)rect.size.width;
579 height = (int)rect.size.height;
582 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
584 NSRect outer = NSMakeRect(100,100,100,100);
585 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
586 NSRect rect = [[m_macWindow contentView] frame];
587 left = (int)rect.origin.x;
588 top = (int)rect.origin.y;
589 width = (int)rect.size.width;
590 height = (int)rect.size.height;
593 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
598 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
600 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
603 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
605 return [m_macWindow isZoomed];
608 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
610 return [m_macWindow isMiniaturized];
613 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
616 [m_macWindow miniaturize:nil];
618 [m_macWindow deminiaturize:nil];
621 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
623 [m_macWindow zoom:nil];
627 // http://cocoadevcentral.com/articles/000028.php
632 NSRect m_formerFrame;
635 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
637 return m_macFullScreenData != NULL ;
640 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
644 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
646 data = new FullScreenData();
648 m_macFullScreenData = data ;
649 data->m_formerLevel = [m_macWindow level];
650 data->m_formerFrame = [m_macWindow frame];
651 CGDisplayCapture( kCGDirectMainDisplay );
652 [m_macWindow setLevel:CGShieldingWindowLevel()];
653 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
655 else if ( m_macFullScreenData != NULL )
657 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
658 CGDisplayRelease( kCGDirectMainDisplay );
659 [m_macWindow setLevel:data->m_formerLevel];
660 [m_macWindow setFrame:data->m_formerFrame display:YES];
662 m_macFullScreenData = NULL ;
668 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
672 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
674 wxPoint p((x ? *x : 0), (y ? *y : 0) );
675 NSPoint nspt = wxToNSPoint( NULL, p );
676 nspt = [m_macWindow convertScreenToBase:nspt];
677 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
678 p = wxFromNSPoint([m_macWindow contentView], nspt);
685 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
687 wxPoint p((x ? *x : 0), (y ? *y : 0) );
688 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
689 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
690 nspt = [m_macWindow convertBaseToScreen:nspt];
691 p = wxFromNSPoint( NULL, nspt);
698 bool wxNonOwnedWindowCocoaImpl::IsActive()
700 return [m_macWindow isKeyWindow];
703 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
704 long style, long extraStyle, const wxString& name )
706 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
707 now->Create( parent, pos, size, style , extraStyle, name );