1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/window.mm
3 // Purpose: widgets (non tlw) for cocoa
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/dcclient.h"
16 #include "wx/nonownedwnd.h"
21 #include "wx/osx/private.h"
28 #if wxUSE_DRAG_AND_DROP
32 #include <objc/objc-runtime.h>
34 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
38 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
39 wxRect bounds(x,y,w,h);
40 NSView* sv = (window->GetParent()->GetHandle() );
42 return wxToNSRect( sv, bounds );
45 @interface wxNSView : NSView
47 NSTrackingRectTag rectTag;
50 - (BOOL) canBecomeKeyView;
51 // the tracking tag is needed to track mouse enter / exit events
52 - (void) setTrackingTag: (NSTrackingRectTag)tag;
53 - (NSTrackingRectTag) trackingTag;
56 @interface NSView(PossibleMethods)
57 - (void)setTitle:(NSString *)aString;
58 - (void)setStringValue:(NSString *)aString;
59 - (void)setIntValue:(int)anInt;
60 - (void)setFloatValue:(float)aFloat;
61 - (void)setDoubleValue:(double)aDouble;
65 - (void)setMinValue:(double)aDouble;
66 - (void)setMaxValue:(double)aDouble;
71 - (void)setEnabled:(BOOL)flag;
73 - (void)setImage:(NSImage *)image;
74 - (void)setControlSize:(NSControlSize)size;
76 - (void)setFont:(NSFont *)fontObject;
80 - (void)setTarget:(id)anObject;
81 - (void)setAction:(SEL)aSelector;
82 - (void)setDoubleAction:(SEL)aSelector;
85 long wxOSXTranslateCocoaKey( NSEvent* event )
89 if ([event type] != NSFlagsChanged)
91 NSString* s = [event charactersIgnoringModifiers];
92 // backspace char reports as delete w/modifiers for some reason
95 switch ( [s characterAtIndex:0] )
102 case NSUpArrowFunctionKey :
105 case NSDownArrowFunctionKey :
108 case NSLeftArrowFunctionKey :
111 case NSRightArrowFunctionKey :
114 case NSInsertFunctionKey :
117 case NSDeleteFunctionKey :
120 case NSHomeFunctionKey :
123 // case NSBeginFunctionKey :
124 // retval = WXK_BEGIN;
126 case NSEndFunctionKey :
129 case NSPageUpFunctionKey :
132 case NSPageDownFunctionKey :
133 retval = WXK_PAGEDOWN;
135 case NSHelpFunctionKey :
139 int intchar = [s characterAtIndex: 0];
140 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
141 retval = WXK_F1 + (intchar - NSF1FunctionKey );
147 // Some keys don't seem to have constants. The code mimics the approach
148 // taken by WebKit. See:
149 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
150 switch( [event keyCode] )
155 retval = WXK_COMMAND;
159 retval = WXK_CAPITAL;
162 case 56: // Left Shift
163 case 60: // Right Shift
168 case 61: // Right Alt
172 case 59: // Left Ctrl
173 case 62: // Right Ctrl
174 retval = WXK_CONTROL;
191 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString = NULL )
193 UInt32 modifiers = [nsEvent modifierFlags] ;
194 int eventType = [nsEvent type];
196 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
197 wxevent.m_controlDown = modifiers & NSControlKeyMask;
198 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
199 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
201 wxevent.m_rawCode = [nsEvent keyCode];
202 wxevent.m_rawFlags = modifiers;
204 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
208 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
211 wxevent.SetEventType( wxEVT_KEY_UP ) ;
213 case NSFlagsChanged :
214 // setup common code here
221 if ( eventType != NSFlagsChanged )
223 NSString* nschars = [nsEvent characters];
226 // if charString is set, it did not come from key up / key down
227 wxevent.SetEventType( wxEVT_CHAR );
228 wxCFStringRef cfchars((CFStringRef)[charString retain]);
229 chars = cfchars.AsString();
233 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
234 chars = cfchars.AsString();
238 int aunichar = chars.Length() > 0 ? chars[0] : 0;
241 if (wxevent.GetEventType() != wxEVT_CHAR)
242 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
246 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
247 keyval = wxToupper( aunichar ) ;
253 wxevent.m_uniChar = aunichar;
255 wxevent.m_keyCode = keyval;
258 UInt32 g_lastButton = 0 ;
259 bool g_lastButtonWasFakeRight = false ;
261 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
263 int eventType = [nsEvent type];
264 UInt32 modifiers = [nsEvent modifierFlags] ;
265 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
267 // these parameters are not given for all events
268 UInt32 button = [nsEvent buttonNumber];
269 UInt32 clickCount = 0;
271 wxevent.m_x = screenMouseLocation.x;
272 wxevent.m_y = screenMouseLocation.y;
273 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
274 wxevent.m_controlDown = modifiers & NSControlKeyMask;
275 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
276 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
277 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
279 UInt32 mouseChord = 0;
283 case NSLeftMouseDown :
284 case NSLeftMouseDragged :
287 case NSRightMouseDown :
288 case NSRightMouseDragged :
291 case NSOtherMouseDown :
292 case NSOtherMouseDragged :
297 // a control click is interpreted as a right click
298 bool thisButtonIsFakeRight = false ;
299 if ( button == 0 && (modifiers & NSControlKeyMask) )
302 thisButtonIsFakeRight = true ;
305 // otherwise we report double clicks by connecting a left click with a ctrl-left click
306 if ( clickCount > 1 && button != g_lastButton )
309 // we must make sure that our synthetic 'right' button corresponds in
310 // mouse down, moved and mouse up, and does not deliver a right down and left up
313 case NSLeftMouseDown :
314 case NSRightMouseDown :
315 case NSOtherMouseDown :
316 g_lastButton = button ;
317 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
324 g_lastButtonWasFakeRight = false ;
326 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
327 button = g_lastButton ;
329 // Adjust the chord mask to remove the primary button and add the
330 // secondary button. It is possible that the secondary button is
331 // already pressed, e.g. on a mouse connected to a laptop, but this
332 // possibility is ignored here:
333 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
334 mouseChord = ((mouseChord & ~1U) | 2U);
337 wxevent.m_leftDown = true ;
339 wxevent.m_rightDown = true ;
341 wxevent.m_middleDown = true ;
343 // translate into wx types
346 case NSLeftMouseDown :
347 case NSRightMouseDown :
348 case NSOtherMouseDown :
349 clickCount = [nsEvent clickCount];
353 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
357 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
361 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
370 case NSRightMouseUp :
371 case NSOtherMouseUp :
372 clickCount = [nsEvent clickCount];
376 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
380 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
384 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
394 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
395 wxevent.m_wheelDelta = 10;
396 wxevent.m_linesPerAction = 1;
397 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
398 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
400 wxevent.m_wheelAxis = 1;
401 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
405 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
410 case NSMouseEntered :
411 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
414 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
416 case NSLeftMouseDragged :
417 case NSRightMouseDragged :
418 case NSOtherMouseDragged :
420 wxevent.SetEventType( wxEVT_MOTION ) ;
426 wxevent.m_clickCount = clickCount;
430 @implementation wxNSView
434 static BOOL initialized = NO;
438 wxOSXCocoaClassAddWXMethods( self );
442 - (BOOL) canBecomeKeyView
447 - (void) setTrackingTag: (NSTrackingRectTag)tag
452 - (NSTrackingRectTag) trackingTag
463 #if wxUSE_DRAG_AND_DROP
465 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
466 // for details on the NSPasteboard -> PasteboardRef conversion
468 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
470 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
472 return NSDragOperationNone;
474 return impl->draggingEntered(sender, self, _cmd);
477 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
479 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
483 return impl->draggingExited(sender, self, _cmd);
486 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
488 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
490 return NSDragOperationNone;
492 return impl->draggingUpdated(sender, self, _cmd);
495 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
497 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
499 return NSDragOperationNone;
501 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
504 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
506 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
510 impl->mouseEvent(event, self, _cmd);
513 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
515 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
519 impl->keyEvent(event, self, _cmd);
522 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
524 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
528 impl->insertText(text, self, _cmd);
531 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
533 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
537 return impl->performKeyEquivalent(event, self, _cmd);
540 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
542 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
546 return impl->acceptsFirstResponder(self, _cmd);
549 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
551 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
555 return impl->becomeFirstResponder(self, _cmd);
558 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
560 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
564 return impl->resignFirstResponder(self, _cmd);
567 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
569 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
573 impl->resetCursorRects(self, _cmd);
576 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
578 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
582 return impl->isFlipped(self, _cmd) ? YES:NO;
585 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
587 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
591 return impl->drawRect(&rect, self, _cmd);
594 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
596 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
600 impl->controlAction(self, _cmd, sender);
603 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
605 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
609 impl->controlDoubleAction(self, _cmd, sender);
612 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
614 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
615 NSPasteboard *pboard = [sender draggingPasteboard];
616 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
618 wxWindow* wxpeer = GetWXPeer();
619 if ( wxpeer == NULL )
620 return NSDragOperationNone;
622 wxDropTarget* target = wxpeer->GetDropTarget();
623 if ( target == NULL )
624 return NSDragOperationNone;
626 wxDragResult result = wxDragNone;
627 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
629 if ( sourceDragMask & NSDragOperationLink )
631 else if ( sourceDragMask & NSDragOperationCopy )
633 else if ( sourceDragMask & NSDragOperationMove )
636 PasteboardRef pboardRef;
637 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
638 target->SetCurrentDragPasteboard(pboardRef);
639 result = target->OnEnter(pt.x, pt.y, result);
640 CFRelease(pboardRef);
642 NSDragOperation nsresult = NSDragOperationNone;
646 nsresult = NSDragOperationLink;
648 nsresult = NSDragOperationMove;
650 nsresult = NSDragOperationCopy;
657 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
659 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
660 NSPasteboard *pboard = [sender draggingPasteboard];
662 wxWindow* wxpeer = GetWXPeer();
663 if ( wxpeer == NULL )
666 wxDropTarget* target = wxpeer->GetDropTarget();
667 if ( target == NULL )
670 PasteboardRef pboardRef;
671 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
672 target->SetCurrentDragPasteboard(pboardRef);
674 CFRelease(pboardRef);
677 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
679 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
680 NSPasteboard *pboard = [sender draggingPasteboard];
681 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
683 wxWindow* wxpeer = GetWXPeer();
684 if ( wxpeer == NULL )
685 return NSDragOperationNone;
687 wxDropTarget* target = wxpeer->GetDropTarget();
688 if ( target == NULL )
689 return NSDragOperationNone;
691 wxDragResult result = wxDragNone;
692 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
694 if ( sourceDragMask & NSDragOperationLink )
696 else if ( sourceDragMask & NSDragOperationCopy )
698 else if ( sourceDragMask & NSDragOperationMove )
701 PasteboardRef pboardRef;
702 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
703 target->SetCurrentDragPasteboard(pboardRef);
704 result = target->OnDragOver(pt.x, pt.y, result);
705 CFRelease(pboardRef);
707 NSDragOperation nsresult = NSDragOperationNone;
711 nsresult = NSDragOperationLink;
713 nsresult = NSDragOperationMove;
715 nsresult = NSDragOperationCopy;
722 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
724 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
726 NSPasteboard *pboard = [sender draggingPasteboard];
727 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
729 wxWindow* wxpeer = GetWXPeer();
730 wxDropTarget* target = wxpeer->GetDropTarget();
731 wxDragResult result = wxDragNone;
732 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
734 if ( sourceDragMask & NSDragOperationLink )
736 else if ( sourceDragMask & NSDragOperationCopy )
738 else if ( sourceDragMask & NSDragOperationMove )
741 PasteboardRef pboardRef;
742 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
743 target->SetCurrentDragPasteboard(pboardRef);
744 result = target->OnData(pt.x, pt.y, result);
745 CFRelease(pboardRef);
747 return result != wxDragNone;
752 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
753 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
754 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
755 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
756 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
757 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
759 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
761 if ( !DoHandleMouseEvent(event) )
763 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
764 superimpl(slf, (SEL)_cmd, event);
768 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
770 if ( [[slf window] firstResponder] != slf || !DoHandleKeyEvent(event) )
772 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
773 superimpl(slf, (SEL)_cmd, event);
777 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
779 if (m_lastKeyDownEvent && !DoHandleCharEvent(m_lastKeyDownEvent, text) )
781 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
782 superimpl(slf, (SEL)_cmd, text);
784 m_lastKeyDownEvent = NULL;
788 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
790 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
791 return superimpl(slf, (SEL)_cmd, event);
794 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
796 // FIXME: We need to find a way to query AcceptsFocus here, but when we do it
797 // it calls native APIs which lead us back here and into a loop.
798 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
799 return superimpl(slf, (SEL)_cmd);
802 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
804 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
805 // get the current focus before running becomeFirstResponder
806 NSResponder* currentResponder = [[NSApp keyWindow] firstResponder];
807 NSView* otherView = (currentResponder != nil && [currentResponder isKindOfClass:[NSView class]]) ? (NSView*) currentResponder : NULL;
808 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
809 BOOL r = superimpl(slf, (SEL)_cmd);
811 DoNotifyFocusEvent( true, otherWindow );
815 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
817 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
818 BOOL r = superimpl(slf, (SEL)_cmd);
819 // get the current focus after running resignFirstResponder
820 NSResponder* currentResponder = [[NSApp keyWindow] firstResponder];
821 NSView* otherView = (currentResponder != nil && [currentResponder isKindOfClass:[NSView class]]) ? (NSView*) currentResponder : NULL;
822 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
824 DoNotifyFocusEvent( false, otherWindow );
828 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
830 wxWindow* wxpeer = GetWXPeer();
833 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
836 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
837 superimpl(slf, (SEL)_cmd);
841 [slf addCursorRect: [slf bounds]
847 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
853 #define OSX_DEBUG_DRAWING 0
855 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
857 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
858 CGContextSaveGState( context );
860 #if OSX_DEBUG_DRAWING
861 CGContextBeginPath( context );
862 CGContextMoveToPoint(context, 0, 0);
863 NSRect bounds = [self bounds];
864 CGContextAddLineToPoint(context, 10, 0);
865 CGContextMoveToPoint(context, 0, 0);
866 CGContextAddLineToPoint(context, 0, 10);
867 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
868 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
869 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
870 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
871 CGContextClosePath( context );
872 CGContextStrokePath(context);
877 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
878 CGContextScaleCTM( context, 1, -1 );
885 [slf getRectsBeingDrawn:&rects count:&count];
886 for ( int i = 0 ; i < count ; ++i )
888 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
891 wxWindow* wxpeer = GetWXPeer();
892 wxpeer->GetUpdateRegion() = updateRgn;
893 wxpeer->MacSetCGContextRef( context );
895 bool handled = wxpeer->MacDoRedraw( 0 );
897 CGContextRestoreGState( context );
899 CGContextSaveGState( context );
903 SEL _cmd = @selector(drawRect:);
904 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
905 superimpl(slf, _cmd, *(NSRect*)rect);
906 CGContextRestoreGState( context );
907 CGContextSaveGState( context );
909 wxpeer->MacPaintChildrenBorders();
910 wxpeer->MacSetCGContextRef( NULL );
911 CGContextRestoreGState( context );
914 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
916 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
918 wxpeer->OSXHandleClicked(0);
921 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
927 #if OBJC_API_VERSION >= 2
929 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
930 class_addMethod(c, s, i, t );
934 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
939 void wxOSXCocoaClassAddWXMethods(Class c)
942 #if OBJC_API_VERSION < 2
943 static objc_method wxmethods[] =
947 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
948 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
949 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
951 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
952 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
953 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
955 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
957 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
958 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
959 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
961 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
962 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
963 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
965 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
966 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
967 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
969 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
971 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
973 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
974 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
975 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
976 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
978 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
979 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
981 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
982 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
984 #if wxUSE_DRAG_AND_DROP
985 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
986 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
987 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
988 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
991 #if OBJC_API_VERSION < 2
993 static int method_count = WXSIZEOF( wxmethods );
994 static objc_method_list *wxmethodlist = NULL;
995 if ( wxmethodlist == NULL )
997 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
998 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
999 wxmethodlist->method_count = method_count;
1000 wxmethodlist->obsolete = 0;
1002 class_addMethods( c, wxmethodlist );
1007 // C++ implementation class
1010 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1012 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1013 wxWidgetImpl( peer, isRootControl )
1019 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1024 void wxWidgetCocoaImpl::Init()
1028 m_lastKeyDownEvent = NULL;
1031 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1033 RemoveAssociations( this );
1035 if ( !IsRootControl() )
1037 NSView *sv = [m_osxView superview];
1039 [m_osxView removeFromSuperview];
1041 [m_osxView release];
1044 bool wxWidgetCocoaImpl::IsVisible() const
1046 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1049 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1051 [m_osxView setHidden:(visible ? NO:YES)];
1054 void wxWidgetCocoaImpl::Raise()
1058 void wxWidgetCocoaImpl::Lower()
1062 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1067 // We should do something like this, but it wasn't working in 10.4.
1068 if (GetNeedsDisplay() )
1072 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1073 NSSize offset = NSMakeSize((float)dx, (float)dy);
1074 [m_osxView scrollRect:r by:offset];
1078 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1080 wxWindowMac* parent = GetWXPeer()->GetParent();
1081 // under Cocoa we might have a contentView in the wxParent to which we have to
1082 // adjust the coordinates
1083 if (parent && [m_osxView superview] != parent->GetHandle() )
1085 int cx = 0,cy = 0,cw = 0,ch = 0;
1086 if ( parent->GetPeer() )
1088 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1093 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1094 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1095 [m_osxView setFrame:r];
1096 [[m_osxView superview] setNeedsDisplayInRect:r];
1098 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1100 if ( [(wxNSView*)m_osxView trackingTag] )
1101 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1103 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1107 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1109 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1114 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1116 NSRect rect = [m_osxView frame];
1117 width = rect.size.width;
1118 height = rect.size.height;
1121 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1123 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1125 NSView* cv = [m_osxView contentView];
1127 NSRect bounds = [m_osxView bounds];
1128 NSRect rect = [cv frame];
1130 int y = rect.origin.y;
1131 int x = rect.origin.x;
1132 if ( ![ m_osxView isFlipped ] )
1133 y = bounds.size.height - (rect.origin.y + rect.size.height);
1136 width = rect.size.width;
1137 height = rect.size.height;
1142 GetSize( width, height );
1146 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1149 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1151 [m_osxView setNeedsDisplay:YES];
1154 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1156 return [m_osxView needsDisplay];
1159 bool wxWidgetCocoaImpl::CanFocus() const
1161 return [m_osxView canBecomeKeyView] == YES;
1164 bool wxWidgetCocoaImpl::HasFocus() const
1166 return ( [[m_osxView window] firstResponder] == m_osxView );
1169 bool wxWidgetCocoaImpl::SetFocus()
1171 if ( [m_osxView canBecomeKeyView] == NO )
1174 [[m_osxView window] makeFirstResponder: m_osxView] ;
1179 void wxWidgetCocoaImpl::RemoveFromParent()
1181 [m_osxView removeFromSuperview];
1184 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1186 NSView* container = parent->GetWXWidget() ;
1187 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1188 [container addSubview:m_osxView];
1191 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1193 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1196 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1198 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1200 wxCFStringRef cf( title , encoding );
1201 [m_osxView setTitle:cf.AsNSString()];
1203 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1205 wxCFStringRef cf( title , encoding );
1206 [m_osxView setStringValue:cf.AsNSString()];
1211 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1213 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1214 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1215 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1218 wxInt32 wxWidgetCocoaImpl::GetValue() const
1220 return [(NSControl*)m_osxView intValue];
1223 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1225 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1227 [m_osxView setIntValue:v];
1229 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1231 [m_osxView setFloatValue:(double)v];
1233 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1235 [m_osxView setDoubleValue:(double)v];
1239 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1241 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1243 [m_osxView setMinValue:(double)v];
1247 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1249 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1251 [m_osxView setMaxValue:(double)v];
1255 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1257 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1259 return [m_osxView minValue];
1264 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1266 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1268 return [m_osxView maxValue];
1273 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1275 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1277 [m_osxView setImage:bitmap.GetNSImage()];
1281 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1283 // implementation in subclass
1286 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1288 r->x = r->y = r->width = r->height = 0;
1290 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1292 NSRect former = [m_osxView frame];
1293 [m_osxView sizeToFit];
1294 NSRect best = [m_osxView frame];
1295 [m_osxView setFrame:former];
1296 r->width = best.size.width;
1297 r->height = best.size.height;
1301 bool wxWidgetCocoaImpl::IsEnabled() const
1303 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1304 return [m_osxView isEnabled];
1308 void wxWidgetCocoaImpl::Enable( bool enable )
1310 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1311 [m_osxView setEnabled:enable];
1314 void wxWidgetCocoaImpl::PulseGauge()
1318 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1322 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1324 NSControlSize size = NSRegularControlSize;
1328 case wxWINDOW_VARIANT_NORMAL :
1329 size = NSRegularControlSize;
1332 case wxWINDOW_VARIANT_SMALL :
1333 size = NSSmallControlSize;
1336 case wxWINDOW_VARIANT_MINI :
1337 size = NSMiniControlSize;
1340 case wxWINDOW_VARIANT_LARGE :
1341 size = NSRegularControlSize;
1345 wxFAIL_MSG(_T("unexpected window variant"));
1348 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1349 [m_osxView setControlSize:size];
1352 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1354 if ([m_osxView respondsToSelector:@selector(setFont:)])
1355 [m_osxView setFont: font.OSXGetNSFont()];
1358 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1360 WXWidget c = control ? control : (WXWidget) m_osxView;
1361 wxWidgetImpl::Associate( c, this ) ;
1362 if ([c respondsToSelector:@selector(setAction:)])
1365 [c setAction: @selector(controlAction:)];
1366 if ([c respondsToSelector:@selector(setDoubleAction:)])
1368 [c setDoubleAction: @selector(controlDoubleAction:)];
1374 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1376 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1377 SetupKeyEvent( wxevent, event, text );
1378 wxevent.SetEventObject(GetWXPeer());
1380 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1383 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1385 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1386 SetupKeyEvent( wxevent, event );
1387 wxevent.SetEventObject(GetWXPeer());
1388 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1390 // this will fire higher level events, like insertText, to help
1391 // us handle EVT_CHAR, etc.
1392 if ([event type] == NSKeyDown)
1394 m_lastKeyDownEvent = event;
1395 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1400 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1402 NSPoint clickLocation;
1403 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1404 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1405 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1406 SetupMouseEvent( wxevent , event ) ;
1407 wxevent.SetEventObject(GetWXPeer());
1411 return GetWXPeer()->HandleWindowEvent(wxevent);
1414 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1416 wxWindow* thisWindow = GetWXPeer();
1417 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1419 thisWindow->MacInvalidateBorders();
1422 if ( receivedFocus )
1424 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1425 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1426 thisWindow->HandleWindowEvent(eventFocus);
1429 if ( thisWindow->GetCaret() )
1430 thisWindow->GetCaret()->OnSetFocus();
1433 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1434 event.SetEventObject(thisWindow);
1436 event.SetWindow(otherWindow->GetWXPeer());
1437 thisWindow->HandleWindowEvent(event) ;
1439 else // !receivedFocuss
1442 if ( thisWindow->GetCaret() )
1443 thisWindow->GetCaret()->OnKillFocus();
1446 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1448 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1449 event.SetEventObject(thisWindow);
1451 event.SetWindow(otherWindow->GetWXPeer());
1452 thisWindow->HandleWindowEvent(event) ;
1456 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1458 NSPoint location = [NSEvent mouseLocation];
1459 location = [[m_osxView window] convertScreenToBase:location];
1460 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1462 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1464 [(NSCursor*)cursor.GetHCURSOR() set];
1466 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1469 void wxWidgetCocoaImpl::CaptureMouse()
1471 [[m_osxView window] disableCursorRects];
1474 void wxWidgetCocoaImpl::ReleaseMouse()
1476 [[m_osxView window] enableCursorRects];
1479 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1481 m_isFlipped = flipped;
1488 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1489 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1490 long WXUNUSED(style), long WXUNUSED(extraStyle))
1492 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1493 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1495 // temporary hook for dnd
1496 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1497 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1499 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1503 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1505 NSWindow* tlw = now->GetWXWindow();
1506 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1507 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1508 c->InstallEventHandler();
1509 [tlw setContentView:v];