+ case NSScrollWheel :
+ {
+ float deltaX = 0.0;
+ float deltaY = 0.0;
+
+ wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
+
+ // see http://developer.apple.com/qa/qa2005/qa1453.html
+ // for more details on why we have to look for the exact type
+
+ const EventRef cEvent = (EventRef) [nsEvent eventRef];
+ bool isMouseScrollEvent = false;
+ if ( cEvent )
+ isMouseScrollEvent = ::GetEventKind(cEvent) == kEventMouseScroll;
+
+ if ( isMouseScrollEvent )
+ {
+ deltaX = [nsEvent deviceDeltaX];
+ deltaY = [nsEvent deviceDeltaY];
+ }
+ else
+ {
+ deltaX = ([nsEvent deltaX] * 10);
+ deltaY = ([nsEvent deltaY] * 10);
+ }
+
+ wxevent.m_wheelDelta = 10;
+ wxevent.m_linesPerAction = 1;
+
+ if ( fabs(deltaX) > fabs(deltaY) )
+ {
+ wxevent.m_wheelAxis = 1;
+ wxevent.m_wheelRotation = (int)deltaX;
+ }
+ else
+ {
+ wxevent.m_wheelRotation = (int)deltaY;
+ }
+
+ }
+ break ;
+
+ case NSMouseEntered :
+ wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
+ break;
+ case NSMouseExited :
+ wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
+ break;
+ case NSLeftMouseDragged :
+ case NSRightMouseDragged :
+ case NSOtherMouseDragged :
+ case NSMouseMoved :
+ wxevent.SetEventType( wxEVT_MOTION ) ;
+ break;
+ default :
+ break ;
+ }
+
+ wxevent.m_clickCount = clickCount;
+ wxWindowMac* peer = GetWXPeer();
+ if ( peer )
+ {
+ wxevent.SetEventObject(peer);
+ wxevent.SetId(peer->GetId()) ;
+ }
+}
+
+@implementation wxNSView
+
++ (void)initialize
+{
+ static BOOL initialized = NO;
+ if (!initialized)
+ {
+ initialized = YES;
+ wxOSXCocoaClassAddWXMethods( self );
+ }
+}
+
+- (void) setTrackingTag: (NSTrackingRectTag)tag
+{
+ rectTag = tag;
+}
+
+- (NSTrackingRectTag) trackingTag
+{
+ return rectTag;
+}
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
+- (void) updateTrackingArea
+{
+ if (_trackingArea)
+ {
+ [self removeTrackingArea: _trackingArea];
+ [_trackingArea release];
+ }
+
+ NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveAlways;
+
+ NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect: [self bounds] options: options owner: self userInfo: nil];
+ [self addTrackingArea: area];
+
+ _trackingArea = area;
+}
+
+- (NSTrackingArea*) trackingArea
+{
+ return _trackingArea;
+}
+#endif
+@end // wxNSView
+
+//
+// event handlers
+//
+
+#if wxUSE_DRAG_AND_DROP
+
+// see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
+// for details on the NSPasteboard -> PasteboardRef conversion
+
+NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NSDragOperationNone;
+
+ return impl->draggingEntered(sender, self, _cmd);
+}
+
+void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return ;
+
+ return impl->draggingExited(sender, self, _cmd);
+}
+
+NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NSDragOperationNone;
+
+ return impl->draggingUpdated(sender, self, _cmd);
+}
+
+BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NSDragOperationNone;
+
+ return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
+}
+
+#endif
+
+void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ impl->mouseEvent(event, self, _cmd);
+}
+
+BOOL wxOSX_acceptsFirstMouse(NSView* WXUNUSED(self), SEL WXUNUSED(_cmd), NSEvent *WXUNUSED(event))
+{
+ // This is needed to support click through, otherwise the first click on a window
+ // will not do anything unless it is the active window already.
+ return YES;
+}
+
+void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ impl->keyEvent(event, self, _cmd);
+}
+
+void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ impl->insertText(text, self, _cmd);
+}
+
+BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NO;
+
+ return impl->performKeyEquivalent(event, self, _cmd);
+}
+
+BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NO;
+
+ return impl->acceptsFirstResponder(self, _cmd);
+}
+
+BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NO;
+
+ return impl->becomeFirstResponder(self, _cmd);
+}
+
+BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NO;
+
+ return impl->resignFirstResponder(self, _cmd);
+}
+
+void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ impl->resetCursorRects(self, _cmd);
+}
+
+BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return NO;
+
+ return impl->isFlipped(self, _cmd) ? YES:NO;
+}
+
+void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ return impl->drawRect(&rect, self, _cmd);
+}
+
+void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ impl->controlAction(self, _cmd, sender);
+}
+
+void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
+{
+ wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
+ if (impl == NULL)
+ return;
+
+ impl->controlDoubleAction(self, _cmd, sender);
+}
+
+unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
+{
+ id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
+ NSPasteboard *pboard = [sender draggingPasteboard];
+ NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
+
+ wxWindow* wxpeer = GetWXPeer();
+ if ( wxpeer == NULL )
+ return NSDragOperationNone;
+
+ wxDropTarget* target = wxpeer->GetDropTarget();
+ if ( target == NULL )
+ return NSDragOperationNone;
+
+ wxDragResult result = wxDragNone;
+ NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
+ wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
+
+ if ( sourceDragMask & NSDragOperationLink )
+ result = wxDragLink;
+ else if ( sourceDragMask & NSDragOperationCopy )
+ result = wxDragCopy;
+ else if ( sourceDragMask & NSDragOperationMove )
+ result = wxDragMove;
+
+ PasteboardRef pboardRef;
+ PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
+ target->SetCurrentDragPasteboard(pboardRef);
+ result = target->OnEnter(pt.x, pt.y, result);
+ CFRelease(pboardRef);
+
+ NSDragOperation nsresult = NSDragOperationNone;
+ switch (result )
+ {
+ case wxDragLink:
+ nsresult = NSDragOperationLink;
+ case wxDragMove:
+ nsresult = NSDragOperationMove;
+ case wxDragCopy:
+ nsresult = NSDragOperationCopy;
+ default :
+ break;
+ }
+ return nsresult;
+}
+
+void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
+{
+ id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
+ NSPasteboard *pboard = [sender draggingPasteboard];
+
+ wxWindow* wxpeer = GetWXPeer();
+ if ( wxpeer == NULL )
+ return;
+
+ wxDropTarget* target = wxpeer->GetDropTarget();
+ if ( target == NULL )
+ return;
+
+ PasteboardRef pboardRef;
+ PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
+ target->SetCurrentDragPasteboard(pboardRef);
+ target->OnLeave();
+ CFRelease(pboardRef);
+ }
+
+unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
+{
+ id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
+ NSPasteboard *pboard = [sender draggingPasteboard];
+ NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
+
+ wxWindow* wxpeer = GetWXPeer();
+ if ( wxpeer == NULL )
+ return NSDragOperationNone;
+
+ wxDropTarget* target = wxpeer->GetDropTarget();
+ if ( target == NULL )
+ return NSDragOperationNone;
+
+ wxDragResult result = wxDragNone;
+ NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
+ wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
+
+ if ( sourceDragMask & NSDragOperationLink )
+ result = wxDragLink;
+ else if ( sourceDragMask & NSDragOperationCopy )
+ result = wxDragCopy;
+ else if ( sourceDragMask & NSDragOperationMove )
+ result = wxDragMove;
+
+ PasteboardRef pboardRef;
+ PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
+ target->SetCurrentDragPasteboard(pboardRef);
+ result = target->OnDragOver(pt.x, pt.y, result);
+ CFRelease(pboardRef);
+
+ NSDragOperation nsresult = NSDragOperationNone;
+ switch (result )
+ {
+ case wxDragLink:
+ nsresult = NSDragOperationLink;
+ case wxDragMove:
+ nsresult = NSDragOperationMove;
+ case wxDragCopy:
+ nsresult = NSDragOperationCopy;
+ default :
+ break;
+ }
+ return nsresult;
+}
+
+bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
+{
+ id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
+
+ NSPasteboard *pboard = [sender draggingPasteboard];
+ NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
+
+ wxWindow* wxpeer = GetWXPeer();
+ wxDropTarget* target = wxpeer->GetDropTarget();
+ wxDragResult result = wxDragNone;
+ NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
+ wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
+
+ if ( sourceDragMask & NSDragOperationLink )
+ result = wxDragLink;
+ else if ( sourceDragMask & NSDragOperationCopy )
+ result = wxDragCopy;
+ else if ( sourceDragMask & NSDragOperationMove )
+ result = wxDragMove;
+
+ PasteboardRef pboardRef;
+ PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
+ target->SetCurrentDragPasteboard(pboardRef);
+
+ if (target->OnDrop(pt.x, pt.y))
+ result = target->OnData(pt.x, pt.y, result);
+
+ CFRelease(pboardRef);
+
+ return result != wxDragNone;
+}
+
+typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
+typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
+typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
+typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
+typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
+typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
+
+void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
+{
+ if ( !DoHandleMouseEvent(event) )
+ {
+ // for plain NSView mouse events would propagate to parents otherwise
+ if (!m_wxPeer->MacIsUserPane())
+ {
+ wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ superimpl(slf, (SEL)_cmd, event);
+ }
+ }
+}
+
+void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
+{
+ if ( [event type] == NSKeyDown )
+ m_lastKeyDownEvent = event;
+ if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
+ {
+ wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ superimpl(slf, (SEL)_cmd, event);
+ }
+ m_lastKeyDownEvent = NULL;
+}
+
+void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
+{
+ if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
+ {
+ wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ superimpl(slf, (SEL)_cmd, text);
+ }
+}
+
+
+bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
+{
+ wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ return superimpl(slf, (SEL)_cmd, event);
+}
+
+bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
+{
+ if ( m_wxPeer->MacIsUserPane() )
+ return m_wxPeer->AcceptsFocus();
+ else
+ {
+ wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ return superimpl(slf, (SEL)_cmd);
+ }
+}
+
+bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
+{
+ wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ // get the current focus before running becomeFirstResponder
+ NSView* otherView = FindFocus();
+
+ wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
+ BOOL r = superimpl(slf, (SEL)_cmd);
+ if ( r )
+ {
+ DoNotifyFocusEvent( true, otherWindow );
+ }
+
+ return r;
+}
+
+bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
+{
+ wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ BOOL r = superimpl(slf, (SEL)_cmd);
+ // get the current focus after running resignFirstResponder
+ // note that this value isn't reliable, it might return the same view that
+ // is resigning
+ NSView* otherView = FindFocus();
+ wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
+ // NSTextViews have an editor as true responder, therefore the might get the
+ // resign notification if their editor takes over, don't trigger any event then
+ if ( r && !m_hasEditor)
+ {
+ DoNotifyFocusEvent( false, otherWindow );
+ }
+ return r;
+}
+
+void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
+{
+ wxWindow* wxpeer = GetWXPeer();
+ if ( wxpeer )
+ {
+ NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
+ if (cursor == NULL)
+ {
+ wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
+ superimpl(slf, (SEL)_cmd);
+ }
+ else
+ {
+ [slf addCursorRect: [slf bounds]
+ cursor: cursor];
+ }
+ }
+}
+
+bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
+{
+ return m_isFlipped;
+}
+
+
+#define OSX_DEBUG_DRAWING 0
+
+void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
+{
+ CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
+ CGContextSaveGState( context );
+
+#if OSX_DEBUG_DRAWING
+ CGContextBeginPath( context );
+ CGContextMoveToPoint(context, 0, 0);
+ NSRect bounds = [self bounds];
+ CGContextAddLineToPoint(context, 10, 0);
+ CGContextMoveToPoint(context, 0, 0);
+ CGContextAddLineToPoint(context, 0, 10);
+ CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
+ CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
+ CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
+ CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
+ CGContextClosePath( context );
+ CGContextStrokePath(context);
+#endif
+
+ if ( !m_isFlipped )
+ {
+ CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
+ CGContextScaleCTM( context, 1, -1 );
+ }
+
+ wxRegion updateRgn;
+ const NSRect *rects;
+ NSInteger count;
+
+ [slf getRectsBeingDrawn:&rects count:&count];
+ for ( int i = 0 ; i < count ; ++i )
+ {
+ updateRgn.Union(wxFromNSRect(slf, rects[i]));
+ }
+
+ wxWindow* wxpeer = GetWXPeer();
+ if ( wxpeer->MacGetTopLevelWindow()->GetWindowStyle() & wxFRAME_SHAPED )
+ {
+ int xoffset = 0, yoffset = 0;
+ wxRegion rgn = wxpeer->MacGetTopLevelWindow()->GetShape();
+ wxpeer->MacRootWindowToWindow( &xoffset, &yoffset );
+ rgn.Offset( xoffset, yoffset );
+ updateRgn.Intersect(rgn);
+ }
+
+ wxpeer->GetUpdateRegion() = updateRgn;
+ wxpeer->MacSetCGContextRef( context );
+
+ bool handled = wxpeer->MacDoRedraw( 0 );
+ CGContextRestoreGState( context );
+
+ CGContextSaveGState( context );
+ if ( !handled )
+ {
+ // call super
+ SEL _cmd = @selector(drawRect:);
+ wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
+ superimpl(slf, _cmd, *(NSRect*)rect);
+ CGContextRestoreGState( context );
+ CGContextSaveGState( context );
+ }
+ wxpeer->MacPaintChildrenBorders();
+ wxpeer->MacSetCGContextRef( NULL );
+ CGContextRestoreGState( context );
+}
+
+void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
+{
+ wxWindow* wxpeer = (wxWindow*) GetWXPeer();
+ if ( wxpeer )
+ wxpeer->OSXHandleClicked(0);
+}