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;
78 - (void)setTarget:(id)anObject;
79 - (void)setAction:(SEL)aSelector;
80 - (void)setDoubleAction:(SEL)aSelector;
83 long wxOSXTranslateCocoaKey( int unichar )
85 long retval = unichar;
88 case NSUpArrowFunctionKey :
91 case NSDownArrowFunctionKey :
94 case NSLeftArrowFunctionKey :
97 case NSRightArrowFunctionKey :
100 case NSInsertFunctionKey :
103 case NSDeleteFunctionKey :
106 case NSHomeFunctionKey :
109 // case NSBeginFunctionKey :
110 // retval = WXK_BEGIN;
112 case NSEndFunctionKey :
115 case NSPageUpFunctionKey :
118 case NSPageDownFunctionKey :
119 retval = WXK_PAGEDOWN;
121 case NSHelpFunctionKey :
126 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
127 retval = WXK_F1 + (unichar - NSF1FunctionKey );
133 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString = NULL )
135 UInt32 modifiers = [nsEvent modifierFlags] ;
136 int eventType = [nsEvent type];
138 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
139 wxevent.m_controlDown = modifiers & NSControlKeyMask;
140 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
141 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
143 wxevent.m_rawCode = [nsEvent keyCode];
144 wxevent.m_rawFlags = modifiers;
146 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
150 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
153 wxevent.SetEventType( wxEVT_KEY_UP ) ;
155 case NSFlagsChanged :
156 // setup common code here
163 if ( eventType != NSFlagsChanged )
165 NSString* nschars = [nsEvent characters];
167 nschars = charString;
171 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
172 chars = cfchars.AsString();
176 int unichar = chars.Length() > 0 ? chars[0] : 0;
177 long keyval = wxOSXTranslateCocoaKey(unichar) ;
178 if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
179 keyval = wxToupper( keyval ) ;
182 wxevent.m_uniChar = unichar;
184 wxevent.m_keyCode = keyval;
187 UInt32 g_lastButton = 0 ;
188 bool g_lastButtonWasFakeRight = false ;
190 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
192 int eventType = [nsEvent type];
193 UInt32 modifiers = [nsEvent modifierFlags] ;
194 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
196 // these parameters are not given for all events
197 UInt32 button = [nsEvent buttonNumber];
198 UInt32 clickCount = 0;
199 if ( [nsEvent respondsToSelector:@selector(clickCount:)] )
200 [nsEvent clickCount];
202 wxevent.m_x = screenMouseLocation.x;
203 wxevent.m_y = screenMouseLocation.y;
204 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
205 wxevent.m_controlDown = modifiers & NSControlKeyMask;
206 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
207 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
208 wxevent.m_clickCount = clickCount;
209 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
211 UInt32 mouseChord = 0;
215 case NSLeftMouseDown :
216 case NSLeftMouseDragged :
219 case NSRightMouseDown :
220 case NSRightMouseDragged :
223 case NSOtherMouseDown :
224 case NSOtherMouseDragged :
229 // a control click is interpreted as a right click
230 bool thisButtonIsFakeRight = false ;
231 if ( button == 0 && (modifiers & NSControlKeyMask) )
234 thisButtonIsFakeRight = true ;
237 // otherwise we report double clicks by connecting a left click with a ctrl-left click
238 if ( clickCount > 1 && button != g_lastButton )
241 // we must make sure that our synthetic 'right' button corresponds in
242 // mouse down, moved and mouse up, and does not deliver a right down and left up
245 case NSLeftMouseDown :
246 case NSRightMouseDown :
247 case NSOtherMouseDown :
248 g_lastButton = button ;
249 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
256 g_lastButtonWasFakeRight = false ;
258 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
259 button = g_lastButton ;
261 // Adjust the chord mask to remove the primary button and add the
262 // secondary button. It is possible that the secondary button is
263 // already pressed, e.g. on a mouse connected to a laptop, but this
264 // possibility is ignored here:
265 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
266 mouseChord = ((mouseChord & ~1U) | 2U);
269 wxevent.m_leftDown = true ;
271 wxevent.m_rightDown = true ;
273 wxevent.m_middleDown = true ;
275 // translate into wx types
278 case NSLeftMouseDown :
279 case NSRightMouseDown :
280 case NSOtherMouseDown :
284 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
288 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
292 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
301 case NSRightMouseUp :
302 case NSOtherMouseUp :
306 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
310 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
314 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
324 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
325 wxevent.m_wheelDelta = 10;
326 wxevent.m_linesPerAction = 1;
327 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
328 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
330 wxevent.m_wheelAxis = 1;
331 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
335 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
340 case NSMouseEntered :
341 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
344 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
346 case NSLeftMouseDragged :
347 case NSRightMouseDragged :
348 case NSOtherMouseDragged :
350 wxevent.SetEventType( wxEVT_MOTION ) ;
357 @implementation wxNSView
361 static BOOL initialized = NO;
365 wxOSXCocoaClassAddWXMethods( self );
369 - (BOOL) canBecomeKeyView
374 - (void) setTrackingTag: (NSTrackingRectTag)tag
379 - (NSTrackingRectTag) trackingTag
390 #if wxUSE_DRAG_AND_DROP
392 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
393 // for details on the NSPasteboard -> PasteboardRef conversion
395 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
397 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
399 return NSDragOperationNone;
401 return impl->draggingEntered(sender, self, _cmd);
404 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
406 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
410 return impl->draggingExited(sender, self, _cmd);
413 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
415 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
417 return NSDragOperationNone;
419 return impl->draggingUpdated(sender, self, _cmd);
422 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
424 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
426 return NSDragOperationNone;
428 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
431 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
433 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
437 impl->mouseEvent(event, self, _cmd);
440 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
442 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
446 impl->keyEvent(event, self, _cmd);
449 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
451 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
455 impl->insertText(text, self, _cmd);
458 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
460 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
464 return impl->performKeyEquivalent(event, self, _cmd);
467 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
469 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
473 return impl->becomeFirstResponder(self, _cmd);
476 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
478 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
482 return impl->resignFirstResponder(self, _cmd);
485 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
487 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
491 impl->resetCursorRects(self, _cmd);
494 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
496 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
500 return impl->isFlipped(self, _cmd) ? YES:NO;
503 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
505 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
509 return impl->drawRect(&rect, self, _cmd);
512 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
514 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
518 impl->controlAction(self, _cmd, sender);
521 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
523 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
527 impl->controlDoubleAction(self, _cmd, sender);
530 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
532 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
533 NSPasteboard *pboard = [sender draggingPasteboard];
534 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
536 wxWindow* wxpeer = GetWXPeer();
537 if ( wxpeer == NULL )
538 return NSDragOperationNone;
540 wxDropTarget* target = wxpeer->GetDropTarget();
541 if ( target == NULL )
542 return NSDragOperationNone;
544 wxDragResult result = wxDragNone;
545 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
547 if ( sourceDragMask & NSDragOperationLink )
549 else if ( sourceDragMask & NSDragOperationCopy )
551 else if ( sourceDragMask & NSDragOperationMove )
554 PasteboardRef pboardRef;
555 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
556 target->SetCurrentDragPasteboard(pboardRef);
557 result = target->OnEnter(pt.x, pt.y, result);
558 CFRelease(pboardRef);
560 NSDragOperation nsresult = NSDragOperationNone;
564 nsresult = NSDragOperationLink;
566 nsresult = NSDragOperationMove;
568 nsresult = NSDragOperationCopy;
575 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
577 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
578 NSPasteboard *pboard = [sender draggingPasteboard];
580 wxWindow* wxpeer = GetWXPeer();
581 if ( wxpeer == NULL )
584 wxDropTarget* target = wxpeer->GetDropTarget();
585 if ( target == NULL )
588 PasteboardRef pboardRef;
589 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
590 target->SetCurrentDragPasteboard(pboardRef);
592 CFRelease(pboardRef);
595 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
597 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
598 NSPasteboard *pboard = [sender draggingPasteboard];
599 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
601 wxWindow* wxpeer = GetWXPeer();
602 if ( wxpeer == NULL )
603 return NSDragOperationNone;
605 wxDropTarget* target = wxpeer->GetDropTarget();
606 if ( target == NULL )
607 return NSDragOperationNone;
609 wxDragResult result = wxDragNone;
610 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
612 if ( sourceDragMask & NSDragOperationLink )
614 else if ( sourceDragMask & NSDragOperationCopy )
616 else if ( sourceDragMask & NSDragOperationMove )
619 PasteboardRef pboardRef;
620 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
621 target->SetCurrentDragPasteboard(pboardRef);
622 result = target->OnDragOver(pt.x, pt.y, result);
623 CFRelease(pboardRef);
625 NSDragOperation nsresult = NSDragOperationNone;
629 nsresult = NSDragOperationLink;
631 nsresult = NSDragOperationMove;
633 nsresult = NSDragOperationCopy;
640 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
642 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
644 NSPasteboard *pboard = [sender draggingPasteboard];
645 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
647 wxWindow* wxpeer = GetWXPeer();
648 wxDropTarget* target = wxpeer->GetDropTarget();
649 wxDragResult result = wxDragNone;
650 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
652 if ( sourceDragMask & NSDragOperationLink )
654 else if ( sourceDragMask & NSDragOperationCopy )
656 else if ( sourceDragMask & NSDragOperationMove )
659 PasteboardRef pboardRef;
660 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
661 target->SetCurrentDragPasteboard(pboardRef);
662 result = target->OnData(pt.x, pt.y, result);
663 CFRelease(pboardRef);
665 return result != wxDragNone;
670 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
671 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
672 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
673 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
674 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
676 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
678 if ( !DoHandleMouseEvent(event) )
680 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
681 superimpl(slf, (SEL)_cmd, event);
685 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
687 if ( [[slf window] firstResponder] != slf || !DoHandleKeyEvent(event) )
689 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
690 superimpl(slf, (SEL)_cmd, event);
694 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
696 if (m_lastKeyDownEvent && !DoHandleCharEvent(m_lastKeyDownEvent, text) )
698 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:@selector(insertText:)];
699 superimpl(slf, @selector(insertText:), text);
701 m_lastKeyDownEvent = NULL;
705 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
707 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
708 return superimpl(slf, (SEL)_cmd, event);
711 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
713 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
714 BOOL r = superimpl(slf, (SEL)_cmd);
716 DoNotifyFocusEvent( true );
720 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
722 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
723 BOOL r = superimpl(slf, (SEL)_cmd);
725 DoNotifyFocusEvent( false );
729 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
731 wxWindow* wxpeer = GetWXPeer();
734 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
737 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
738 superimpl(slf, (SEL)_cmd);
742 [slf addCursorRect: [slf bounds]
748 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
754 #define OSX_DEBUG_DRAWING 0
756 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
758 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
759 CGContextSaveGState( context );
761 #if OSX_DEBUG_DRAWING
762 CGContextBeginPath( context );
763 CGContextMoveToPoint(context, 0, 0);
764 NSRect bounds = [self bounds];
765 CGContextAddLineToPoint(context, 10, 0);
766 CGContextMoveToPoint(context, 0, 0);
767 CGContextAddLineToPoint(context, 0, 10);
768 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
769 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
770 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
771 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
772 CGContextClosePath( context );
773 CGContextStrokePath(context);
778 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
779 CGContextScaleCTM( context, 1, -1 );
786 [slf getRectsBeingDrawn:&rects count:&count];
787 for ( int i = 0 ; i < count ; ++i )
789 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
792 wxWindow* wxpeer = GetWXPeer();
793 wxpeer->GetUpdateRegion() = updateRgn;
794 wxpeer->MacSetCGContextRef( context );
796 bool handled = wxpeer->MacDoRedraw( 0 );
798 CGContextRestoreGState( context );
800 CGContextSaveGState( context );
804 SEL _cmd = @selector(drawRect:);
805 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
806 superimpl(slf, _cmd, *(NSRect*)rect);
807 CGContextRestoreGState( context );
808 CGContextSaveGState( context );
810 wxpeer->MacPaintChildrenBorders();
811 wxpeer->MacSetCGContextRef( NULL );
812 CGContextRestoreGState( context );
815 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
817 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
819 wxpeer->OSXHandleClicked(0);
822 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
828 #if OBJC_API_VERSION >= 2
830 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
831 class_addMethod(c, s, i, t );
835 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
840 void wxOSXCocoaClassAddWXMethods(Class c)
843 #if OBJC_API_VERSION < 2
844 static objc_method wxmethods[] =
848 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
849 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
850 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
852 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
853 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
854 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
856 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
858 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
859 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
860 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
862 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
863 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
864 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
866 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
867 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
868 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
870 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
872 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
875 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
876 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
877 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
879 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
880 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
882 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
883 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
885 #if wxUSE_DRAG_AND_DROP
886 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
887 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
888 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
889 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
892 #if OBJC_API_VERSION < 2
894 static int method_count = WXSIZEOF( wxmethods );
895 static objc_method_list *wxmethodlist = NULL;
896 if ( wxmethodlist == NULL )
898 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
899 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
900 wxmethodlist->method_count = method_count;
901 wxmethodlist->obsolete = 0;
903 class_addMethods( c, wxmethodlist );
908 // C++ implementation class
911 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
913 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
914 wxWidgetImpl( peer, isRootControl )
920 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
925 void wxWidgetCocoaImpl::Init()
929 m_lastKeyDownEvent = NULL;
932 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
934 RemoveAssociations( this );
936 if ( !IsRootControl() )
938 NSView *sv = [m_osxView superview];
940 [m_osxView removeFromSuperview];
945 bool wxWidgetCocoaImpl::IsVisible() const
947 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
950 void wxWidgetCocoaImpl::SetVisibility( bool visible )
952 [m_osxView setHidden:(visible ? NO:YES)];
955 void wxWidgetCocoaImpl::Raise()
959 void wxWidgetCocoaImpl::Lower()
963 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
968 // We should do something like this, but it wasn't working in 10.4.
969 if (GetNeedsDisplay() )
973 NSRect r = wxToNSRect( [m_osxView superview], *rect );
974 NSSize offset = NSMakeSize((float)dx, (float)dy);
975 [m_osxView scrollRect:r by:offset];
979 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
981 wxWindowMac* parent = GetWXPeer()->GetParent();
982 // under Cocoa we might have a contentView in the wxParent to which we have to
983 // adjust the coordinates
986 int cx = 0,cy = 0,cw = 0,ch = 0;
987 if ( parent->GetPeer() )
989 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
994 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
995 [m_osxView setFrame:r];
997 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
999 if ( [(wxNSView*)m_osxView trackingTag] )
1000 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1002 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1006 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1008 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1013 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1015 NSRect rect = [m_osxView frame];
1016 width = rect.size.width;
1017 height = rect.size.height;
1020 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1022 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1024 NSView* cv = [m_osxView contentView];
1026 NSRect bounds = [m_osxView bounds];
1027 NSRect rect = [cv frame];
1029 int y = rect.origin.y;
1030 int x = rect.origin.x;
1031 if ( ![ m_osxView isFlipped ] )
1032 y = bounds.size.height - (rect.origin.y + rect.size.height);
1035 width = rect.size.width;
1036 height = rect.size.height;
1041 GetSize( width, height );
1045 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1048 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1050 [m_osxView setNeedsDisplay:YES];
1053 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1055 return [m_osxView needsDisplay];
1058 bool wxWidgetCocoaImpl::CanFocus() const
1060 return [m_osxView canBecomeKeyView] == YES;
1063 bool wxWidgetCocoaImpl::HasFocus() const
1065 return ( [[m_osxView window] firstResponder] == m_osxView );
1068 bool wxWidgetCocoaImpl::SetFocus()
1070 if ( [m_osxView canBecomeKeyView] == NO )
1073 [[m_osxView window] makeFirstResponder: m_osxView] ;
1078 void wxWidgetCocoaImpl::RemoveFromParent()
1080 [m_osxView removeFromSuperview];
1083 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1085 NSView* container = parent->GetWXWidget() ;
1086 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1087 [container addSubview:m_osxView];
1090 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1092 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1095 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1097 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1099 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1100 [m_osxView setTitle:cf.AsNSString()];
1102 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1104 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1105 [m_osxView setStringValue:cf.AsNSString()];
1110 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1112 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1113 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1114 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1117 wxInt32 wxWidgetCocoaImpl::GetValue() const
1119 return [(NSControl*)m_osxView intValue];
1122 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1124 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1126 [m_osxView setIntValue:v];
1128 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1130 [m_osxView setFloatValue:(double)v];
1132 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1134 [m_osxView setDoubleValue:(double)v];
1138 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1140 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1142 [m_osxView setMinValue:(double)v];
1146 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1148 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1150 [m_osxView setMaxValue:(double)v];
1154 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1156 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1158 return [m_osxView minValue];
1163 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1165 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1167 return [m_osxView maxValue];
1172 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1174 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1176 [m_osxView setImage:bitmap.GetNSImage()];
1180 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1182 // implementation in subclass
1185 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1187 r->x = r->y = r->width = r->height = 0;
1188 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1189 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1191 NSRect former = [m_osxView frame];
1192 [m_osxView sizeToFit];
1193 NSRect best = [m_osxView frame];
1194 [m_osxView setFrame:former];
1195 r->width = best.size.width;
1196 r->height = best.size.height;
1200 bool wxWidgetCocoaImpl::IsEnabled() const
1202 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1203 return [m_osxView isEnabled];
1207 void wxWidgetCocoaImpl::Enable( bool enable )
1209 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1210 [m_osxView setEnabled:enable];
1213 void wxWidgetCocoaImpl::PulseGauge()
1217 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1221 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1223 NSControlSize size = NSRegularControlSize;
1227 case wxWINDOW_VARIANT_NORMAL :
1228 size = NSRegularControlSize;
1231 case wxWINDOW_VARIANT_SMALL :
1232 size = NSSmallControlSize;
1235 case wxWINDOW_VARIANT_MINI :
1236 size = NSMiniControlSize;
1239 case wxWINDOW_VARIANT_LARGE :
1240 size = NSRegularControlSize;
1244 wxFAIL_MSG(_T("unexpected window variant"));
1247 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1248 [m_osxView setControlSize:size];
1251 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1256 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1258 WXWidget c = control ? control : (WXWidget) m_osxView;
1259 wxWidgetImpl::Associate( c, this ) ;
1260 if ([c respondsToSelector:@selector(setAction:)])
1263 [c setAction: @selector(controlAction:)];
1264 if ([c respondsToSelector:@selector(setDoubleAction:)])
1266 [c setDoubleAction: @selector(controlDoubleAction:)];
1272 static bool g_inKeyEvent = false;
1274 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1276 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1277 SetupKeyEvent( wxevent, event, text );
1279 wxevent.SetEventType(wxEVT_CHAR);
1281 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1284 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1286 wxASSERT_MSG(!g_inKeyEvent, "Re-entering key handler...\n");
1287 g_inKeyEvent = true;
1288 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1289 SetupKeyEvent( wxevent, event );
1291 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1293 // this will fire higher level events, like insertText, to help
1294 // us handle EVT_CHAR, etc.
1295 if ([event type] == NSKeyDown)
1297 m_lastKeyDownEvent = event;
1298 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1300 g_inKeyEvent = false;
1304 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1306 NSPoint clickLocation;
1307 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1308 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1309 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1310 SetupMouseEvent( wxevent , event ) ;
1314 return GetWXPeer()->HandleWindowEvent(wxevent);
1317 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1319 wxWindow* thisWindow = GetWXPeer();
1320 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1322 thisWindow->MacInvalidateBorders();
1325 if ( receivedFocus )
1327 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1328 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1329 thisWindow->HandleWindowEvent(eventFocus);
1332 if ( thisWindow->GetCaret() )
1333 thisWindow->GetCaret()->OnSetFocus();
1336 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1337 event.SetEventObject(thisWindow);
1338 // TODO how to find out the targetFocusWindow ?
1339 // event.SetWindow(targetFocusWindow);
1340 thisWindow->HandleWindowEvent(event) ;
1342 else // !receivedFocuss
1345 if ( thisWindow->GetCaret() )
1346 thisWindow->GetCaret()->OnKillFocus();
1349 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1351 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1352 event.SetEventObject(thisWindow);
1353 // TODO how to find out the targetFocusWindow ?
1354 // event.SetWindow(targetFocusWindow);
1355 thisWindow->HandleWindowEvent(event) ;
1359 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1361 NSPoint location = [NSEvent mouseLocation];
1362 location = [[m_osxView window] convertScreenToBase:location];
1363 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1365 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1367 [(NSCursor*)cursor.GetHCURSOR() set];
1369 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1372 void wxWidgetCocoaImpl::CaptureMouse()
1374 [[m_osxView window] disableCursorRects];
1377 void wxWidgetCocoaImpl::ReleaseMouse()
1379 [[m_osxView window] enableCursorRects];
1382 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1384 m_isFlipped = flipped;
1391 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1392 long style, long extraStyle)
1394 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1395 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1397 // temporary hook for dnd
1398 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1399 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1401 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1405 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1407 NSWindow* tlw = now->GetWXWindow();
1408 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1409 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1410 c->InstallEventHandler();
1411 [tlw setContentView:v];