+bool shouldHandleSelector(SEL selector)
+{
+ if (selector == @selector(noop:)
+ || selector == @selector(complete:)
+ || selector == @selector(deleteBackward:)
+ || selector == @selector(deleteForward:)
+ || selector == @selector(insertNewline:)
+ || selector == @selector(insertTab:)
+ || selector == @selector(keyDown:)
+ || selector == @selector(keyUp:)
+ || selector == @selector(scrollPageUp:)
+ || selector == @selector(scrollPageDown:)
+ || selector == @selector(scrollToBeginningOfDocument:)
+ || selector == @selector(scrollToEndOfDocument:))
+ return false;
+
+ return true;
+
+}
+
+//
+// wx category for NSWindow (our own and wrapped instances)
+//
+
+@interface NSWindow (wxNSWindowSupport)
+
+- (wxNonOwnedWindowCocoaImpl*) WX_implementation;
+
+- (bool) WX_filterSendEvent:(NSEvent *) event;
+
+@end
+
+@implementation NSWindow (wxNSWindowSupport)
+
+- (wxNonOwnedWindowCocoaImpl*) WX_implementation
+{
+ return (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
+}
+
+// TODO in cocoa everything during a drag is sent to the NSWindow the mouse down occured,
+// this does not conform to the wx behaviour if the window is not captured, so try to resend
+// or capture all wx mouse event handling at the tlw as we did for carbon
+
+- (bool) WX_filterSendEvent:(NSEvent *) event
+{
+ bool handled = false;
+ if ( ([event type] >= NSLeftMouseDown) && ([event type] <= NSMouseExited) )
+ {
+ wxWindow* cw = wxWindow::GetCapture();
+ if ( cw != NULL )
+ {
+ ((wxWidgetCocoaImpl*)cw->GetPeer())->DoHandleMouseEvent( event);
+ handled = true;
+ }
+ }
+ return handled;
+}
+@end
+
+//
+// wx native implementation
+//
+
+@interface wxNSWindow : NSWindow
+{
+}
+
+- (void) sendEvent:(NSEvent *)event;
+- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
+- (void)noResponderFor: (SEL) selector;
+@end
+
+@implementation wxNSWindow
+
+- (void)sendEvent:(NSEvent *) event
+{
+ if ( ![self WX_filterSendEvent: event] )
+ {
+ WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
+ WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
+
+ if (wxTheApp)
+ wxTheApp->MacSetCurrentEvent(event, NULL);
+
+ [super sendEvent: event];
+
+ if (wxTheApp)
+ wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
+ }
+}
+
+// The default implementation always moves the window back onto the screen,
+// even when the programmer explicitly wants to hide it.
+- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
+{
+ wxUnusedVar(screen);
+ return frameRect;
+}
+
+- (void)doCommandBySelector:(SEL)selector
+{
+ if (shouldHandleSelector(selector) &&
+ !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
+ [super doCommandBySelector:selector];
+}
+
+
+// NB: if we don't do this, all key downs that get handled lead to a NSBeep
+- (void)noResponderFor: (SEL) selector
+{
+ if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
+ {
+ [super noResponderFor:selector];
+ }
+}
+
+// We need this for borderless windows, i.e. shaped windows or windows without
+// a title bar. For more info, see:
+// http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
+- (BOOL)canBecomeKeyWindow
+{
+ return YES;
+}
+
+@end
+
+@interface wxNSPanel : NSPanel
+{
+}
+
+- (void)noResponderFor: (SEL) selector;
+- (void)sendEvent:(NSEvent *)event;
+@end
+
+@implementation wxNSPanel
+
+- (BOOL)canBecomeKeyWindow
+{
+ return YES;
+}
+
+- (void)doCommandBySelector:(SEL)selector
+{
+ if (shouldHandleSelector(selector))
+ [super doCommandBySelector:selector];
+}
+
+// NB: if we don't do this, it seems that all events that end here lead to a NSBeep
+- (void)noResponderFor: (SEL) selector
+{
+ if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
+ {
+ [super noResponderFor:selector];
+ }
+}
+
+- (void)sendEvent:(NSEvent *) event
+{
+ if ( ![self WX_filterSendEvent: event] )
+ [super sendEvent: event];
+}
+
+@end
+
+
+//
+// controller
+//
+
+@interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
+{
+}
+
+- (void)windowDidResize:(NSNotification *)notification;
+- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
+- (void)windowDidResignKey:(NSNotification *)notification;
+- (void)windowDidBecomeKey:(NSNotification *)notification;
+- (void)windowDidMove:(NSNotification *)notification;
+- (BOOL)windowShouldClose:(id)window;
+- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
+
+@end
+
+@implementation wxNonOwnedWindowController
+
+- (id) init
+{
+ [super init];
+ return self;
+}
+
+- (BOOL)windowShouldClose:(id)nwindow
+{
+ wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ if ( wxpeer )
+ wxpeer->Close();
+ }
+ return NO;
+}
+
+- (NSSize)windowWillResize:(NSWindow *)window
+ toSize:(NSSize)proposedFrameSize
+{
+ NSRect frame = [window frame];
+ wxRect wxframe = wxFromNSRect( NULL, frame );
+ wxframe.SetWidth( (int)proposedFrameSize.width );
+ wxframe.SetHeight( (int)proposedFrameSize.height );
+
+ wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ if ( wxpeer )
+ {
+ wxpeer->HandleResizing( 0, &wxframe );
+ NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
+ return newSize;
+ }
+ }
+
+ return proposedFrameSize;
+}
+
+- (void)windowDidResize:(NSNotification *)notification
+{
+ NSWindow* window = (NSWindow*) [notification object];
+ wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ if ( wxpeer )
+ wxpeer->HandleResized(0);
+ }
+}
+
+- (void)windowDidMove:(NSNotification *)notification
+{
+ wxNSWindow* window = (wxNSWindow*) [notification object];
+ wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ if ( wxpeer )
+ wxpeer->HandleMoved(0);
+ }
+}
+
+- (void)windowDidBecomeKey:(NSNotification *)notification
+{
+ NSWindow* window = (NSWindow*) [notification object];
+ wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ if ( wxpeer )
+ wxpeer->HandleActivated(0, true);
+ }
+}
+
+- (void)windowDidResignKey:(NSNotification *)notification
+{
+ NSWindow* window = (NSWindow*) [notification object];
+ wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ if ( wxpeer )
+ {
+ wxpeer->HandleActivated(0, false);
+ // Needed for popup window since the firstResponder
+ // (focus in wx) doesn't change when this
+ // TLW becomes inactive.
+ wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
+ event.SetEventObject(wxpeer);
+ wxpeer->HandleWindowEvent(event);
+ }
+ }
+}
+
+- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
+{
+ wxUnusedVar(sender);
+
+ if ([anObject isKindOfClass:[wxNSTextField class]])
+ {
+ wxNSTextField* tf = (wxNSTextField*) anObject;
+ wxNSTextFieldEditor* editor = [tf fieldEditor];
+ if ( editor == nil )
+ {
+ editor = [[wxNSTextFieldEditor alloc] init];
+ [editor setFieldEditor:YES];
+ [tf setFieldEditor:editor];
+ }
+ return editor;
+ }
+
+ return nil;
+}
+
+- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
+{
+ wxUnusedVar(newFrame);
+ wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
+ if ( windowimpl )
+ {
+ wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
+ wxMaximizeEvent event(wxpeer->GetId());
+ event.SetEventObject(wxpeer);
+ return !wxpeer->HandleWindowEvent(event);
+ }
+ return true;
+}
+
+@end
+