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"
18 #include "wx/textctrl.h"
22 #include "wx/osx/private.h"
25 #include "wx/evtloop.h"
31 #if wxUSE_DRAG_AND_DROP
36 #include "wx/tooltip.h"
39 #include <objc/objc-runtime.h>
41 // Get the window with the focus
43 NSView* GetViewFromResponder( NSResponder* responder )
46 if ( [responder isKindOfClass:[NSTextView class]] )
48 NSView* delegate = (NSView*) [(NSTextView*)responder delegate];
49 if ( [delegate isKindOfClass:[NSTextField class] ] )
52 view = (NSView*) responder;
56 if ( [responder isKindOfClass:[NSView class]] )
57 view = (NSView*) responder;
62 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
64 NSView* focusedView = nil;
65 if ( keyWindow != nil )
66 focusedView = GetViewFromResponder([keyWindow firstResponder]);
71 WXWidget wxWidgetImpl::FindFocus()
73 return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
76 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
80 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
81 wxRect bounds(x,y,w,h);
82 NSView* sv = (window->GetParent()->GetHandle() );
84 return wxToNSRect( sv, bounds );
87 @interface wxNSView : NSView
89 NSTrackingRectTag rectTag;
90 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
91 NSTrackingArea* _trackingArea;
95 // the tracking tag is needed to track mouse enter / exit events
96 - (void) setTrackingTag: (NSTrackingRectTag)tag;
97 - (NSTrackingRectTag) trackingTag;
98 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
99 // under 10.5 we can also track mouse moved events on non-focused windows if
100 // we use the new NSTrackingArea APIs.
101 - (void) updateTrackingArea;
102 - (NSTrackingArea*) trackingArea;
106 @interface NSView(PossibleMethods)
107 - (void)setTitle:(NSString *)aString;
108 - (void)setStringValue:(NSString *)aString;
109 - (void)setIntValue:(int)anInt;
110 - (void)setFloatValue:(float)aFloat;
111 - (void)setDoubleValue:(double)aDouble;
115 - (void)setMinValue:(double)aDouble;
116 - (void)setMaxValue:(double)aDouble;
121 - (void)setEnabled:(BOOL)flag;
123 - (void)setImage:(NSImage *)image;
124 - (void)setControlSize:(NSControlSize)size;
126 - (void)setFont:(NSFont *)fontObject;
130 - (void)setTarget:(id)anObject;
131 - (void)setAction:(SEL)aSelector;
132 - (void)setDoubleAction:(SEL)aSelector;
133 - (void)setBackgroundColor:(NSColor*)aColor;
134 - (void)setImagePosition:(NSCellImagePosition)aPosition;
137 long wxOSXTranslateCocoaKey( NSEvent* event )
141 if ([event type] != NSFlagsChanged)
143 NSString* s = [event charactersIgnoringModifiers];
144 // backspace char reports as delete w/modifiers for some reason
147 switch ( [s characterAtIndex:0] )
154 case NSUpArrowFunctionKey :
157 case NSDownArrowFunctionKey :
160 case NSLeftArrowFunctionKey :
163 case NSRightArrowFunctionKey :
166 case NSInsertFunctionKey :
169 case NSDeleteFunctionKey :
172 case NSHomeFunctionKey :
175 // case NSBeginFunctionKey :
176 // retval = WXK_BEGIN;
178 case NSEndFunctionKey :
181 case NSPageUpFunctionKey :
184 case NSPageDownFunctionKey :
185 retval = WXK_PAGEDOWN;
187 case NSHelpFunctionKey :
191 int intchar = [s characterAtIndex: 0];
192 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
193 retval = WXK_F1 + (intchar - NSF1FunctionKey );
199 // Some keys don't seem to have constants. The code mimics the approach
200 // taken by WebKit. See:
201 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
202 switch( [event keyCode] )
207 retval = WXK_COMMAND;
211 retval = WXK_CAPITAL;
214 case 56: // Left Shift
215 case 60: // Right Shift
220 case 61: // Right Alt
224 case 59: // Left Ctrl
225 case 62: // Right Ctrl
226 retval = WXK_CONTROL;
238 retval = WXK_NUMPAD_DIVIDE;
241 retval = WXK_NUMPAD_MULTIPLY;
244 retval = WXK_NUMPAD_SUBTRACT;
247 retval = WXK_NUMPAD_ADD;
250 retval = WXK_NUMPAD_ENTER;
253 retval = WXK_NUMPAD_DECIMAL;
256 retval = WXK_NUMPAD0;
259 retval = WXK_NUMPAD1;
262 retval = WXK_NUMPAD2;
265 retval = WXK_NUMPAD3;
268 retval = WXK_NUMPAD4;
271 retval = WXK_NUMPAD5;
274 retval = WXK_NUMPAD6;
277 retval = WXK_NUMPAD7;
280 retval = WXK_NUMPAD8;
283 retval = WXK_NUMPAD9;
286 //retval = [event keyCode];
292 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
294 UInt32 modifiers = [nsEvent modifierFlags] ;
295 int eventType = [nsEvent type];
297 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
298 wxevent.m_controlDown = modifiers & NSControlKeyMask;
299 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
300 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
302 wxevent.m_rawCode = [nsEvent keyCode];
303 wxevent.m_rawFlags = modifiers;
305 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
308 if ( eventType != NSFlagsChanged )
310 NSString* nschars = [nsEvent charactersIgnoringModifiers];
313 // if charString is set, it did not come from key up / key down
314 wxevent.SetEventType( wxEVT_CHAR );
315 chars = wxCFStringRef::AsString(charString);
319 chars = wxCFStringRef::AsString(nschars);
323 int aunichar = chars.Length() > 0 ? chars[0] : 0;
326 if (wxevent.GetEventType() != wxEVT_CHAR)
328 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
332 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
335 wxevent.SetEventType( wxEVT_KEY_UP ) ;
337 case NSFlagsChanged :
341 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
344 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
347 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
350 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
361 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
362 keyval = wxToupper( aunichar ) ;
368 wxevent.m_uniChar = aunichar;
370 wxevent.m_keyCode = keyval;
372 wxWindowMac* peer = GetWXPeer();
375 wxevent.SetEventObject(peer);
376 wxevent.SetId(peer->GetId()) ;
380 UInt32 g_lastButton = 0 ;
381 bool g_lastButtonWasFakeRight = false ;
383 // better scroll wheel support
384 // see http://lists.apple.com/archives/cocoa-dev/2007/Feb/msg00050.html
386 @interface NSEvent (DeviceDelta)
387 - (float)deviceDeltaX;
388 - (float)deviceDeltaY;
391 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
393 int eventType = [nsEvent type];
394 UInt32 modifiers = [nsEvent modifierFlags] ;
395 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
397 // these parameters are not given for all events
398 UInt32 button = [nsEvent buttonNumber];
399 UInt32 clickCount = 0;
401 wxevent.m_x = screenMouseLocation.x;
402 wxevent.m_y = screenMouseLocation.y;
403 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
404 wxevent.m_controlDown = modifiers & NSControlKeyMask;
405 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
406 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
407 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
409 UInt32 mouseChord = 0;
413 case NSLeftMouseDown :
414 case NSLeftMouseDragged :
417 case NSRightMouseDown :
418 case NSRightMouseDragged :
421 case NSOtherMouseDown :
422 case NSOtherMouseDragged :
427 // a control click is interpreted as a right click
428 bool thisButtonIsFakeRight = false ;
429 if ( button == 0 && (modifiers & NSControlKeyMask) )
432 thisButtonIsFakeRight = true ;
435 // otherwise we report double clicks by connecting a left click with a ctrl-left click
436 if ( clickCount > 1 && button != g_lastButton )
439 // we must make sure that our synthetic 'right' button corresponds in
440 // mouse down, moved and mouse up, and does not deliver a right down and left up
443 case NSLeftMouseDown :
444 case NSRightMouseDown :
445 case NSOtherMouseDown :
446 g_lastButton = button ;
447 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
454 g_lastButtonWasFakeRight = false ;
456 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
457 button = g_lastButton ;
459 // Adjust the chord mask to remove the primary button and add the
460 // secondary button. It is possible that the secondary button is
461 // already pressed, e.g. on a mouse connected to a laptop, but this
462 // possibility is ignored here:
463 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
464 mouseChord = ((mouseChord & ~1U) | 2U);
467 wxevent.m_leftDown = true ;
469 wxevent.m_rightDown = true ;
471 wxevent.m_middleDown = true ;
473 // translate into wx types
476 case NSLeftMouseDown :
477 case NSRightMouseDown :
478 case NSOtherMouseDown :
479 clickCount = [nsEvent clickCount];
483 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
487 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
491 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
500 case NSRightMouseUp :
501 case NSOtherMouseUp :
502 clickCount = [nsEvent clickCount];
506 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
510 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
514 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
527 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
529 // see http://developer.apple.com/qa/qa2005/qa1453.html
530 // for more details on why we have to look for the exact type
532 const EventRef cEvent = (EventRef) [nsEvent eventRef];
533 bool isMouseScrollEvent = false;
535 isMouseScrollEvent = ::GetEventKind(cEvent) == kEventMouseScroll;
537 if ( isMouseScrollEvent )
539 deltaX = [nsEvent deviceDeltaX];
540 deltaY = [nsEvent deviceDeltaY];
544 deltaX = ([nsEvent deltaX] * 10);
545 deltaY = ([nsEvent deltaY] * 10);
548 wxevent.m_wheelDelta = 10;
549 wxevent.m_linesPerAction = 1;
551 if ( fabs(deltaX) > fabs(deltaY) )
553 wxevent.m_wheelAxis = 1;
554 wxevent.m_wheelRotation = (int)deltaX;
558 wxevent.m_wheelRotation = (int)deltaY;
564 case NSMouseEntered :
565 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
568 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
570 case NSLeftMouseDragged :
571 case NSRightMouseDragged :
572 case NSOtherMouseDragged :
574 wxevent.SetEventType( wxEVT_MOTION ) ;
580 wxevent.m_clickCount = clickCount;
581 wxWindowMac* peer = GetWXPeer();
584 wxevent.SetEventObject(peer);
585 wxevent.SetId(peer->GetId()) ;
589 @implementation wxNSView
593 static BOOL initialized = NO;
597 wxOSXCocoaClassAddWXMethods( self );
601 - (void) setTrackingTag: (NSTrackingRectTag)tag
606 - (NSTrackingRectTag) trackingTag
611 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
612 - (void) updateTrackingArea
616 [self removeTrackingArea: _trackingArea];
617 [_trackingArea release];
620 NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveAlways;
622 NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect: [self bounds] options: options owner: self userInfo: nil];
623 [self addTrackingArea: area];
625 _trackingArea = area;
628 - (NSTrackingArea*) trackingArea
630 return _trackingArea;
639 #if wxUSE_DRAG_AND_DROP
641 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
642 // for details on the NSPasteboard -> PasteboardRef conversion
644 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
646 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
648 return NSDragOperationNone;
650 return impl->draggingEntered(sender, self, _cmd);
653 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
655 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
659 return impl->draggingExited(sender, self, _cmd);
662 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
664 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
666 return NSDragOperationNone;
668 return impl->draggingUpdated(sender, self, _cmd);
671 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
673 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
675 return NSDragOperationNone;
677 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
682 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
684 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
688 impl->mouseEvent(event, self, _cmd);
691 BOOL wxOSX_acceptsFirstMouse(NSView* self, SEL _cmd, NSEvent *event)
693 // This is needed to support click through, otherwise the first click on a window
694 // will not do anything unless it is the active window already.
698 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
700 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
704 impl->keyEvent(event, self, _cmd);
707 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
709 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
713 impl->insertText(text, self, _cmd);
716 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
718 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
722 return impl->performKeyEquivalent(event, self, _cmd);
725 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
727 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
731 return impl->acceptsFirstResponder(self, _cmd);
734 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
736 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
740 return impl->becomeFirstResponder(self, _cmd);
743 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
745 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
749 return impl->resignFirstResponder(self, _cmd);
752 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
754 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
758 impl->resetCursorRects(self, _cmd);
761 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
763 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
767 return impl->isFlipped(self, _cmd) ? YES:NO;
770 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
772 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
776 return impl->drawRect(&rect, self, _cmd);
779 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
781 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
785 impl->controlAction(self, _cmd, sender);
788 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
790 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
794 impl->controlDoubleAction(self, _cmd, sender);
797 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
799 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
800 NSPasteboard *pboard = [sender draggingPasteboard];
801 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
803 wxWindow* wxpeer = GetWXPeer();
804 if ( wxpeer == NULL )
805 return NSDragOperationNone;
807 wxDropTarget* target = wxpeer->GetDropTarget();
808 if ( target == NULL )
809 return NSDragOperationNone;
811 wxDragResult result = wxDragNone;
812 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
813 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
815 if ( sourceDragMask & NSDragOperationLink )
817 else if ( sourceDragMask & NSDragOperationCopy )
819 else if ( sourceDragMask & NSDragOperationMove )
822 PasteboardRef pboardRef;
823 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
824 target->SetCurrentDragPasteboard(pboardRef);
825 result = target->OnEnter(pt.x, pt.y, result);
826 CFRelease(pboardRef);
828 NSDragOperation nsresult = NSDragOperationNone;
832 nsresult = NSDragOperationLink;
834 nsresult = NSDragOperationMove;
836 nsresult = NSDragOperationCopy;
843 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
845 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
846 NSPasteboard *pboard = [sender draggingPasteboard];
848 wxWindow* wxpeer = GetWXPeer();
849 if ( wxpeer == NULL )
852 wxDropTarget* target = wxpeer->GetDropTarget();
853 if ( target == NULL )
856 PasteboardRef pboardRef;
857 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
858 target->SetCurrentDragPasteboard(pboardRef);
860 CFRelease(pboardRef);
863 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
865 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
866 NSPasteboard *pboard = [sender draggingPasteboard];
867 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
869 wxWindow* wxpeer = GetWXPeer();
870 if ( wxpeer == NULL )
871 return NSDragOperationNone;
873 wxDropTarget* target = wxpeer->GetDropTarget();
874 if ( target == NULL )
875 return NSDragOperationNone;
877 wxDragResult result = wxDragNone;
878 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
879 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
881 if ( sourceDragMask & NSDragOperationLink )
883 else if ( sourceDragMask & NSDragOperationCopy )
885 else if ( sourceDragMask & NSDragOperationMove )
888 PasteboardRef pboardRef;
889 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
890 target->SetCurrentDragPasteboard(pboardRef);
891 result = target->OnDragOver(pt.x, pt.y, result);
892 CFRelease(pboardRef);
894 NSDragOperation nsresult = NSDragOperationNone;
898 nsresult = NSDragOperationLink;
900 nsresult = NSDragOperationMove;
902 nsresult = NSDragOperationCopy;
909 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
911 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
913 NSPasteboard *pboard = [sender draggingPasteboard];
914 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
916 wxWindow* wxpeer = GetWXPeer();
917 wxDropTarget* target = wxpeer->GetDropTarget();
918 wxDragResult result = wxDragNone;
919 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
920 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
922 if ( sourceDragMask & NSDragOperationLink )
924 else if ( sourceDragMask & NSDragOperationCopy )
926 else if ( sourceDragMask & NSDragOperationMove )
929 PasteboardRef pboardRef;
930 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
931 target->SetCurrentDragPasteboard(pboardRef);
933 if (target->OnDrop(pt.x, pt.y))
934 result = target->OnData(pt.x, pt.y, result);
936 CFRelease(pboardRef);
938 return result != wxDragNone;
941 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
942 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
943 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
944 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
945 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
946 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
948 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
950 if ( !DoHandleMouseEvent(event) )
952 // for plain NSView mouse events would propagate to parents otherwise
953 if (!m_wxPeer->MacIsUserPane())
955 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
956 superimpl(slf, (SEL)_cmd, event);
961 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
963 if ( [event type] == NSKeyDown )
964 m_lastKeyDownEvent = event;
965 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
967 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
968 superimpl(slf, (SEL)_cmd, event);
970 m_lastKeyDownEvent = NULL;
973 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
975 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
977 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
978 superimpl(slf, (SEL)_cmd, text);
983 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
985 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
986 return superimpl(slf, (SEL)_cmd, event);
989 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
991 if ( m_wxPeer->MacIsUserPane() )
992 return m_wxPeer->AcceptsFocus();
995 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
996 return superimpl(slf, (SEL)_cmd);
1000 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
1002 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1003 // get the current focus before running becomeFirstResponder
1004 NSView* otherView = FindFocus();
1006 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1007 BOOL r = superimpl(slf, (SEL)_cmd);
1010 DoNotifyFocusEvent( true, otherWindow );
1016 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
1018 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1019 BOOL r = superimpl(slf, (SEL)_cmd);
1020 // get the current focus after running resignFirstResponder
1021 // note that this value isn't reliable, it might return the same view that
1023 NSView* otherView = FindFocus();
1024 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1025 // NSTextViews have an editor as true responder, therefore the might get the
1026 // resign notification if their editor takes over, don't trigger any event then
1027 if ( r && !m_hasEditor)
1029 DoNotifyFocusEvent( false, otherWindow );
1034 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
1036 wxWindow* wxpeer = GetWXPeer();
1039 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
1042 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1043 superimpl(slf, (SEL)_cmd);
1047 [slf addCursorRect: [slf bounds]
1053 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1059 #define OSX_DEBUG_DRAWING 0
1061 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
1063 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
1064 CGContextSaveGState( context );
1066 #if OSX_DEBUG_DRAWING
1067 CGContextBeginPath( context );
1068 CGContextMoveToPoint(context, 0, 0);
1069 NSRect bounds = [self bounds];
1070 CGContextAddLineToPoint(context, 10, 0);
1071 CGContextMoveToPoint(context, 0, 0);
1072 CGContextAddLineToPoint(context, 0, 10);
1073 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1074 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1075 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1076 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1077 CGContextClosePath( context );
1078 CGContextStrokePath(context);
1083 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1084 CGContextScaleCTM( context, 1, -1 );
1088 const NSRect *rects;
1091 [slf getRectsBeingDrawn:&rects count:&count];
1092 for ( int i = 0 ; i < count ; ++i )
1094 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1097 wxWindow* wxpeer = GetWXPeer();
1098 wxpeer->GetUpdateRegion() = updateRgn;
1099 wxpeer->MacSetCGContextRef( context );
1101 bool handled = wxpeer->MacDoRedraw( 0 );
1103 CGContextRestoreGState( context );
1105 CGContextSaveGState( context );
1109 SEL _cmd = @selector(drawRect:);
1110 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1111 superimpl(slf, _cmd, *(NSRect*)rect);
1112 CGContextRestoreGState( context );
1113 CGContextSaveGState( context );
1115 wxpeer->MacPaintChildrenBorders();
1116 wxpeer->MacSetCGContextRef( NULL );
1117 CGContextRestoreGState( context );
1120 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1122 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1124 wxpeer->OSXHandleClicked(0);
1127 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1131 void wxWidgetCocoaImpl::controlTextDidChange()
1133 wxWindow* wxpeer = (wxWindow*)GetWXPeer();
1136 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
1137 event.SetEventObject( wxpeer );
1138 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
1139 wxpeer->HandleWindowEvent( event );
1145 #if OBJC_API_VERSION >= 2
1147 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1148 class_addMethod(c, s, i, t );
1152 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1157 void wxOSXCocoaClassAddWXMethods(Class c)
1160 #if OBJC_API_VERSION < 2
1161 static objc_method wxmethods[] =
1165 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1166 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1167 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1169 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1170 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1171 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1173 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1175 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1176 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1177 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1179 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstMouse:), (IMP) wxOSX_acceptsFirstMouse, "v@:@" )
1181 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1182 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1183 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1185 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1186 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1187 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1189 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1191 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1193 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1194 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1195 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1196 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1198 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1199 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1201 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1202 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1204 #if wxUSE_DRAG_AND_DROP
1205 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1206 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1207 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1208 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1211 #if OBJC_API_VERSION < 2
1213 static int method_count = WXSIZEOF( wxmethods );
1214 static objc_method_list *wxmethodlist = NULL;
1215 if ( wxmethodlist == NULL )
1217 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1218 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1219 wxmethodlist->method_count = method_count;
1220 wxmethodlist->obsolete = 0;
1222 class_addMethods( c, wxmethodlist );
1227 // C++ implementation class
1230 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1232 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1233 wxWidgetImpl( peer, isRootControl )
1238 // check if the user wants to create the control initially hidden
1239 if ( !peer->IsShown() )
1240 SetVisibility(false);
1242 // gc aware handling
1244 CFRetain(m_osxView);
1245 [m_osxView release];
1248 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1253 void wxWidgetCocoaImpl::Init()
1257 m_lastKeyDownEvent = NULL;
1258 m_hasEditor = false;
1261 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1263 RemoveAssociations( this );
1265 if ( !IsRootControl() )
1267 NSView *sv = [m_osxView superview];
1269 [m_osxView removeFromSuperview];
1271 // gc aware handling
1273 CFRelease(m_osxView);
1276 bool wxWidgetCocoaImpl::IsVisible() const
1278 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1281 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1283 [m_osxView setHidden:(visible ? NO:YES)];
1286 // ----------------------------------------------------------------------------
1287 // window animation stuff
1288 // ----------------------------------------------------------------------------
1290 // define a delegate used to refresh the window during animation
1291 @interface wxNSAnimationDelegate : NSObject wxOSX_10_6_AND_LATER(<NSAnimationDelegate>)
1297 - (id)init:(wxWindow *)win;
1301 // NSAnimationDelegate methods
1302 - (void)animationDidEnd:(NSAnimation*)animation;
1303 - (void)animation:(NSAnimation*)animation
1304 didReachProgressMark:(NSAnimationProgress)progress;
1307 @implementation wxNSAnimationDelegate
1309 - (id)init:(wxWindow *)win
1324 - (void)animation:(NSAnimation*)animation
1325 didReachProgressMark:(NSAnimationProgress)progress
1327 wxUnusedVar(animation);
1328 wxUnusedVar(progress);
1330 m_win->SendSizeEvent();
1333 - (void)animationDidEnd:(NSAnimation*)animation
1342 wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1344 wxShowEffect effect,
1347 // create the dictionary describing the animation to perform on this view
1349 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1350 NSMutableDictionary * const
1351 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1352 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1354 // determine the start and end rectangles assuming we're hiding the window
1355 const wxRect rectOrig = win->GetRect();
1363 if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1364 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1365 effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1366 else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1367 effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1368 effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1369 else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1370 effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1371 effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1372 else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1373 effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1374 effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1379 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1380 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1384 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1385 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1386 rectEnd.x = rectStart.GetRight();
1390 case wxSHOW_EFFECT_ROLL_TO_TOP:
1391 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1395 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1396 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1397 rectEnd.y = rectStart.GetBottom();
1401 case wxSHOW_EFFECT_EXPAND:
1402 rectEnd.x = rectStart.x + rectStart.width / 2;
1403 rectEnd.y = rectStart.y + rectStart.height / 2;
1408 case wxSHOW_EFFECT_BLEND:
1409 [dict setObject:(show ? NSViewAnimationFadeInEffect
1410 : NSViewAnimationFadeOutEffect)
1411 forKey:NSViewAnimationEffectKey];
1414 case wxSHOW_EFFECT_NONE:
1415 case wxSHOW_EFFECT_MAX:
1416 wxFAIL_MSG( "unexpected animation effect" );
1420 wxFAIL_MSG( "unknown animation effect" );
1426 // we need to restore it to the original rectangle instead of making it
1428 wxSwap(rectStart, rectEnd);
1430 // and as the window is currently hidden, we need to show it for the
1431 // animation to be visible at all (but don't restore it at its full
1432 // rectangle as it shouldn't appear immediately)
1433 win->SetSize(rectStart);
1437 NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1438 ? [(NSView *)viewOrWin superview]
1440 const NSRect rStart = wxToNSRect(parentView, rectStart);
1441 const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1443 [dict setObject:[NSValue valueWithRect:rStart]
1444 forKey:NSViewAnimationStartFrameKey];
1445 [dict setObject:[NSValue valueWithRect:rEnd]
1446 forKey:NSViewAnimationEndFrameKey];
1448 // create an animation using the values in the above dictionary
1449 NSViewAnimation * const
1450 anim = [[NSViewAnimation alloc]
1451 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1455 // what is a good default duration? Windows uses 200ms, Web frameworks
1456 // use anything from 250ms to 1s... choose something in the middle
1460 [anim setDuration:timeout/1000.]; // duration is in seconds here
1462 // if the window being animated changes its layout depending on its size
1463 // (which is almost always the case) we need to redo it during animation
1465 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1466 // controls in wxInfoBar visibly jump around)
1467 const int NUM_LAYOUTS = 20;
1468 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1469 [anim addProgressMark:f];
1471 wxNSAnimationDelegate * const
1472 animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1473 [anim setDelegate:animDelegate];
1474 [anim startAnimation];
1476 // Cocoa is capable of doing animation asynchronously or even from separate
1477 // thread but wx API doesn't provide any way to be notified about the
1478 // animation end and without this we really must ensure that the window has
1479 // the expected (i.e. the same as if a simple Show() had been used) size
1480 // when we return, so block here until the animation finishes
1482 // notice that because the default animation mode is NSAnimationBlocking,
1483 // no user input events ought to be processed from here
1485 wxEventLoopGuarantor ensureEventLoopExistence;
1486 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1487 while ( ![animDelegate isDone] )
1493 // NSViewAnimation is smart enough to hide the NSView being animated at
1494 // the end but we also must ensure that it's hidden for wx too
1497 // and we must also restore its size because it isn't expected to
1498 // change just because the window was hidden
1499 win->SetSize(rectOrig);
1503 // refresh it once again after the end to ensure that everything is in
1505 win->SendSizeEvent();
1508 [anim setDelegate:nil];
1509 [animDelegate release];
1515 bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1516 wxShowEffect effect,
1519 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1522 void wxWidgetCocoaImpl::Raise()
1527 void wxWidgetCocoaImpl::Lower()
1532 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1537 // We should do something like this, but it wasn't working in 10.4.
1538 if (GetNeedsDisplay() )
1542 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1543 NSSize offset = NSMakeSize((float)dx, (float)dy);
1544 [m_osxView scrollRect:r by:offset];
1548 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1550 wxWindowMac* parent = GetWXPeer()->GetParent();
1551 // under Cocoa we might have a contentView in the wxParent to which we have to
1552 // adjust the coordinates
1553 if (parent && [m_osxView superview] != parent->GetHandle() )
1555 int cx = 0,cy = 0,cw = 0,ch = 0;
1556 if ( parent->GetPeer() )
1558 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1563 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1564 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1565 [m_osxView setFrame:r];
1566 [[m_osxView superview] setNeedsDisplayInRect:r];
1568 wxNSView* wxview = (wxNSView*)m_osxView;
1569 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1570 if ([wxview respondsToSelector:@selector(updateTrackingArea)] )
1571 [wxview updateTrackingArea];
1573 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1575 if ( [wxview trackingTag] )
1576 [wxview removeTrackingRect: [wxview trackingTag]];
1578 [wxview setTrackingTag: [wxview addTrackingRect: [m_osxView bounds] owner: wxview userData: nil assumeInside: NO]];
1583 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1585 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1590 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1592 NSRect rect = [m_osxView frame];
1593 width = (int)rect.size.width;
1594 height = (int)rect.size.height;
1597 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1599 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1601 NSView* cv = [m_osxView contentView];
1603 NSRect bounds = [m_osxView bounds];
1604 NSRect rect = [cv frame];
1606 int y = (int)rect.origin.y;
1607 int x = (int)rect.origin.x;
1608 if ( ![ m_osxView isFlipped ] )
1609 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1612 width = (int)rect.size.width;
1613 height = (int)rect.size.height;
1618 GetSize( width, height );
1622 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1625 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1627 [m_osxView setNeedsDisplay:YES];
1630 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1632 return [m_osxView needsDisplay];
1635 bool wxWidgetCocoaImpl::CanFocus() const
1637 return [m_osxView canBecomeKeyView] == YES;
1640 bool wxWidgetCocoaImpl::HasFocus() const
1642 return ( FindFocus() == m_osxView );
1645 bool wxWidgetCocoaImpl::SetFocus()
1650 [[m_osxView window] makeFirstResponder: m_osxView] ;
1651 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1656 void wxWidgetCocoaImpl::RemoveFromParent()
1658 [m_osxView removeFromSuperview];
1661 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1663 NSView* container = parent->GetWXWidget() ;
1664 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1665 [container addSubview:m_osxView];
1668 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1670 NSView* targetView = m_osxView;
1671 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1672 targetView = [(NSScrollView*) m_osxView documentView];
1674 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1676 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1677 green:(CGFloat) (col.Green() / 255.0)
1678 blue:(CGFloat) (col.Blue() / 255.0)
1679 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1683 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1685 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1687 wxCFStringRef cf( title , encoding );
1688 [m_osxView setTitle:cf.AsNSString()];
1690 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1692 wxCFStringRef cf( title , encoding );
1693 [m_osxView setStringValue:cf.AsNSString()];
1698 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1700 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1701 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1702 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1705 wxInt32 wxWidgetCocoaImpl::GetValue() const
1707 return [(NSControl*)m_osxView intValue];
1710 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1712 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1714 [m_osxView setIntValue:v];
1716 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1718 [m_osxView setFloatValue:(double)v];
1720 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1722 [m_osxView setDoubleValue:(double)v];
1726 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1728 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1730 [m_osxView setMinValue:(double)v];
1734 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1736 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1738 [m_osxView setMaxValue:(double)v];
1742 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1744 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1746 return (int)[m_osxView minValue];
1751 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1753 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1755 return (int)[m_osxView maxValue];
1760 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1764 // TODO: how to create a wxBitmap from NSImage?
1766 if ( [m_osxView respondsToSelector:@selector(image:)] )
1767 bmp = [m_osxView image];
1773 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1775 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1777 [m_osxView setImage:bitmap.GetNSImage()];
1778 [m_osxView setNeedsDisplay:YES];
1782 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1784 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1786 NSCellImagePosition pos;
1806 wxFAIL_MSG( "invalid image position" );
1810 [m_osxView setImagePosition:pos];
1814 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1816 // implementation in subclass
1819 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1821 r->x = r->y = r->width = r->height = 0;
1823 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1825 NSRect former = [m_osxView frame];
1826 [m_osxView sizeToFit];
1827 NSRect best = [m_osxView frame];
1828 [m_osxView setFrame:former];
1829 r->width = (int)best.size.width;
1830 r->height = (int)best.size.height;
1834 bool wxWidgetCocoaImpl::IsEnabled() const
1836 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1837 return [m_osxView isEnabled];
1841 void wxWidgetCocoaImpl::Enable( bool enable )
1843 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1844 [m_osxView setEnabled:enable];
1847 void wxWidgetCocoaImpl::PulseGauge()
1851 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1855 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1857 NSControlSize size = NSRegularControlSize;
1861 case wxWINDOW_VARIANT_NORMAL :
1862 size = NSRegularControlSize;
1865 case wxWINDOW_VARIANT_SMALL :
1866 size = NSSmallControlSize;
1869 case wxWINDOW_VARIANT_MINI :
1870 size = NSMiniControlSize;
1873 case wxWINDOW_VARIANT_LARGE :
1874 size = NSRegularControlSize;
1878 wxFAIL_MSG(wxT("unexpected window variant"));
1881 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1882 [m_osxView setControlSize:size];
1883 else if ([m_osxView respondsToSelector:@selector(cell)])
1885 id cell = [(id)m_osxView cell];
1886 if ([cell respondsToSelector:@selector(setControlSize:)])
1887 [cell setControlSize:size];
1891 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1893 if ([m_osxView respondsToSelector:@selector(setFont:)])
1894 [m_osxView setFont: font.OSXGetNSFont()];
1897 void wxWidgetCocoaImpl::SetToolTip(wxToolTip* tooltip)
1901 wxCFStringRef cf( tooltip->GetTip() , m_wxPeer->GetFont().GetEncoding() );
1902 [m_osxView setToolTip: cf.AsNSString()];
1905 [m_osxView setToolTip: nil];
1909 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1911 WXWidget c = control ? control : (WXWidget) m_osxView;
1912 wxWidgetImpl::Associate( c, this ) ;
1913 if ([c respondsToSelector:@selector(setAction:)])
1916 [c setAction: @selector(controlAction:)];
1917 if ([c respondsToSelector:@selector(setDoubleAction:)])
1919 [c setDoubleAction: @selector(controlDoubleAction:)];
1925 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1927 wxKeyEvent wxevent(wxEVT_CHAR);
1928 SetupKeyEvent( wxevent, event, text );
1930 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1933 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1935 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1936 SetupKeyEvent( wxevent, event );
1937 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1939 // this will fire higher level events, like insertText, to help
1940 // us handle EVT_CHAR, etc.
1942 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1946 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1947 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1949 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1957 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1959 NSPoint clickLocation;
1960 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1961 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1962 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1963 SetupMouseEvent(wxevent , event) ;
1967 return GetWXPeer()->HandleWindowEvent(wxevent);
1970 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1972 wxWindow* thisWindow = GetWXPeer();
1973 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1975 thisWindow->MacInvalidateBorders();
1978 if ( receivedFocus )
1980 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1981 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1982 thisWindow->HandleWindowEvent(eventFocus);
1985 if ( thisWindow->GetCaret() )
1986 thisWindow->GetCaret()->OnSetFocus();
1989 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1990 event.SetEventObject(thisWindow);
1992 event.SetWindow(otherWindow->GetWXPeer());
1993 thisWindow->HandleWindowEvent(event) ;
1995 else // !receivedFocuss
1998 if ( thisWindow->GetCaret() )
1999 thisWindow->GetCaret()->OnKillFocus();
2002 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
2004 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
2005 event.SetEventObject(thisWindow);
2007 event.SetWindow(otherWindow->GetWXPeer());
2008 thisWindow->HandleWindowEvent(event) ;
2012 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
2014 NSPoint location = [NSEvent mouseLocation];
2015 location = [[m_osxView window] convertScreenToBase:location];
2016 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
2018 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
2020 [(NSCursor*)cursor.GetHCURSOR() set];
2022 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
2025 void wxWidgetCocoaImpl::CaptureMouse()
2027 [[m_osxView window] disableCursorRects];
2030 void wxWidgetCocoaImpl::ReleaseMouse()
2032 [[m_osxView window] enableCursorRects];
2035 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
2037 m_isFlipped = flipped;
2044 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
2045 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
2046 long WXUNUSED(style), long WXUNUSED(extraStyle))
2048 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
2049 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
2051 // temporary hook for dnd
2052 [v registerForDraggedTypes:[NSArray arrayWithObjects:
2053 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
2055 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
2059 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
2061 NSWindow* tlw = now->GetWXWindow();
2062 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
2063 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
2064 c->InstallEventHandler();
2065 [tlw setContentView:v];