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
35 #include <objc/objc-runtime.h>
37 // Get the window with the focus
39 NSView* GetViewFromResponder( NSResponder* responder )
42 if ( [responder isKindOfClass:[NSTextView class]] )
44 NSView* delegate = (NSView*) [(NSTextView*)responder delegate];
45 if ( [delegate isKindOfClass:[NSTextField class] ] )
48 view = (NSView*) responder;
52 if ( [responder isKindOfClass:[NSView class]] )
53 view = (NSView*) responder;
58 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
60 NSView* focusedView = nil;
61 if ( keyWindow != nil )
62 focusedView = GetViewFromResponder([keyWindow firstResponder]);
67 WXWidget wxWidgetImpl::FindFocus()
69 return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
72 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
76 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
77 wxRect bounds(x,y,w,h);
78 NSView* sv = (window->GetParent()->GetHandle() );
80 return wxToNSRect( sv, bounds );
83 @interface wxNSView : NSView
85 NSTrackingRectTag rectTag;
86 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
87 NSTrackingArea* _trackingArea;
91 // the tracking tag is needed to track mouse enter / exit events
92 - (void) setTrackingTag: (NSTrackingRectTag)tag;
93 - (NSTrackingRectTag) trackingTag;
94 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
95 // under 10.5 we can also track mouse moved events on non-focused windows if
96 // we use the new NSTrackingArea APIs.
97 - (void) updateTrackingArea;
98 - (NSTrackingArea*) trackingArea;
102 @interface NSView(PossibleMethods)
103 - (void)setTitle:(NSString *)aString;
104 - (void)setStringValue:(NSString *)aString;
105 - (void)setIntValue:(int)anInt;
106 - (void)setFloatValue:(float)aFloat;
107 - (void)setDoubleValue:(double)aDouble;
111 - (void)setMinValue:(double)aDouble;
112 - (void)setMaxValue:(double)aDouble;
117 - (void)setEnabled:(BOOL)flag;
119 - (void)setImage:(NSImage *)image;
120 - (void)setControlSize:(NSControlSize)size;
122 - (void)setFont:(NSFont *)fontObject;
126 - (void)setTarget:(id)anObject;
127 - (void)setAction:(SEL)aSelector;
128 - (void)setDoubleAction:(SEL)aSelector;
129 - (void)setBackgroundColor:(NSColor*)aColor;
130 - (void)setImagePosition:(NSCellImagePosition)aPosition;
133 long wxOSXTranslateCocoaKey( NSEvent* event )
137 if ([event type] != NSFlagsChanged)
139 NSString* s = [event charactersIgnoringModifiers];
140 // backspace char reports as delete w/modifiers for some reason
143 switch ( [s characterAtIndex:0] )
150 case NSUpArrowFunctionKey :
153 case NSDownArrowFunctionKey :
156 case NSLeftArrowFunctionKey :
159 case NSRightArrowFunctionKey :
162 case NSInsertFunctionKey :
165 case NSDeleteFunctionKey :
168 case NSHomeFunctionKey :
171 // case NSBeginFunctionKey :
172 // retval = WXK_BEGIN;
174 case NSEndFunctionKey :
177 case NSPageUpFunctionKey :
180 case NSPageDownFunctionKey :
181 retval = WXK_PAGEDOWN;
183 case NSHelpFunctionKey :
187 int intchar = [s characterAtIndex: 0];
188 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
189 retval = WXK_F1 + (intchar - NSF1FunctionKey );
195 // Some keys don't seem to have constants. The code mimics the approach
196 // taken by WebKit. See:
197 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
198 switch( [event keyCode] )
203 retval = WXK_COMMAND;
207 retval = WXK_CAPITAL;
210 case 56: // Left Shift
211 case 60: // Right Shift
216 case 61: // Right Alt
220 case 59: // Left Ctrl
221 case 62: // Right Ctrl
222 retval = WXK_CONTROL;
234 retval = WXK_NUMPAD_DIVIDE;
237 retval = WXK_NUMPAD_MULTIPLY;
240 retval = WXK_NUMPAD_SUBTRACT;
243 retval = WXK_NUMPAD_ADD;
246 retval = WXK_NUMPAD_ENTER;
249 retval = WXK_NUMPAD_DECIMAL;
252 retval = WXK_NUMPAD0;
255 retval = WXK_NUMPAD1;
258 retval = WXK_NUMPAD2;
261 retval = WXK_NUMPAD3;
264 retval = WXK_NUMPAD4;
267 retval = WXK_NUMPAD5;
270 retval = WXK_NUMPAD6;
273 retval = WXK_NUMPAD7;
276 retval = WXK_NUMPAD8;
279 retval = WXK_NUMPAD9;
282 //retval = [event keyCode];
288 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
290 UInt32 modifiers = [nsEvent modifierFlags] ;
291 int eventType = [nsEvent type];
293 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
294 wxevent.m_controlDown = modifiers & NSControlKeyMask;
295 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
296 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
298 wxevent.m_rawCode = [nsEvent keyCode];
299 wxevent.m_rawFlags = modifiers;
301 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
304 if ( eventType != NSFlagsChanged )
306 NSString* nschars = [nsEvent charactersIgnoringModifiers];
309 // if charString is set, it did not come from key up / key down
310 wxevent.SetEventType( wxEVT_CHAR );
311 chars = wxCFStringRef::AsString(charString);
315 chars = wxCFStringRef::AsString(nschars);
319 int aunichar = chars.Length() > 0 ? chars[0] : 0;
322 if (wxevent.GetEventType() != wxEVT_CHAR)
324 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
328 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
331 wxevent.SetEventType( wxEVT_KEY_UP ) ;
333 case NSFlagsChanged :
337 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
340 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
343 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
346 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
357 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
358 keyval = wxToupper( aunichar ) ;
364 wxevent.m_uniChar = aunichar;
366 wxevent.m_keyCode = keyval;
368 wxWindowMac* peer = GetWXPeer();
371 wxevent.SetEventObject(peer);
372 wxevent.SetId(peer->GetId()) ;
376 UInt32 g_lastButton = 0 ;
377 bool g_lastButtonWasFakeRight = false ;
379 // better scroll wheel support
380 // see http://lists.apple.com/archives/cocoa-dev/2007/Feb/msg00050.html
382 @interface NSEvent (DeviceDelta)
383 - (float)deviceDeltaX;
384 - (float)deviceDeltaY;
387 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
389 int eventType = [nsEvent type];
390 UInt32 modifiers = [nsEvent modifierFlags] ;
391 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
393 // these parameters are not given for all events
394 UInt32 button = [nsEvent buttonNumber];
395 UInt32 clickCount = 0;
397 wxevent.m_x = screenMouseLocation.x;
398 wxevent.m_y = screenMouseLocation.y;
399 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
400 wxevent.m_controlDown = modifiers & NSControlKeyMask;
401 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
402 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
403 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
405 UInt32 mouseChord = 0;
409 case NSLeftMouseDown :
410 case NSLeftMouseDragged :
413 case NSRightMouseDown :
414 case NSRightMouseDragged :
417 case NSOtherMouseDown :
418 case NSOtherMouseDragged :
423 // a control click is interpreted as a right click
424 bool thisButtonIsFakeRight = false ;
425 if ( button == 0 && (modifiers & NSControlKeyMask) )
428 thisButtonIsFakeRight = true ;
431 // otherwise we report double clicks by connecting a left click with a ctrl-left click
432 if ( clickCount > 1 && button != g_lastButton )
435 // we must make sure that our synthetic 'right' button corresponds in
436 // mouse down, moved and mouse up, and does not deliver a right down and left up
439 case NSLeftMouseDown :
440 case NSRightMouseDown :
441 case NSOtherMouseDown :
442 g_lastButton = button ;
443 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
450 g_lastButtonWasFakeRight = false ;
452 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
453 button = g_lastButton ;
455 // Adjust the chord mask to remove the primary button and add the
456 // secondary button. It is possible that the secondary button is
457 // already pressed, e.g. on a mouse connected to a laptop, but this
458 // possibility is ignored here:
459 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
460 mouseChord = ((mouseChord & ~1U) | 2U);
463 wxevent.m_leftDown = true ;
465 wxevent.m_rightDown = true ;
467 wxevent.m_middleDown = true ;
469 // translate into wx types
472 case NSLeftMouseDown :
473 case NSRightMouseDown :
474 case NSOtherMouseDown :
475 clickCount = [nsEvent clickCount];
479 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
483 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
487 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
496 case NSRightMouseUp :
497 case NSOtherMouseUp :
498 clickCount = [nsEvent clickCount];
502 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
506 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
510 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
523 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
525 // see http://developer.apple.com/qa/qa2005/qa1453.html
526 // for more details on why we have to look for the exact type
528 const EventRef cEvent = (EventRef) [nsEvent eventRef];
529 bool isMouseScrollEvent = false;
531 isMouseScrollEvent = ::GetEventKind(cEvent) == kEventMouseScroll;
533 if ( isMouseScrollEvent )
535 deltaX = [nsEvent deviceDeltaX];
536 deltaY = [nsEvent deviceDeltaY];
540 deltaX = ([nsEvent deltaX] * 10);
541 deltaY = ([nsEvent deltaY] * 10);
544 wxevent.m_wheelDelta = 10;
545 wxevent.m_linesPerAction = 1;
547 if ( fabs(deltaX) > fabs(deltaY) )
549 wxevent.m_wheelAxis = 1;
550 wxevent.m_wheelRotation = (int)deltaX;
554 wxevent.m_wheelRotation = (int)deltaY;
560 case NSMouseEntered :
561 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
564 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
566 case NSLeftMouseDragged :
567 case NSRightMouseDragged :
568 case NSOtherMouseDragged :
570 wxevent.SetEventType( wxEVT_MOTION ) ;
576 wxevent.m_clickCount = clickCount;
577 wxWindowMac* peer = GetWXPeer();
580 wxevent.SetEventObject(peer);
581 wxevent.SetId(peer->GetId()) ;
585 @implementation wxNSView
589 static BOOL initialized = NO;
593 wxOSXCocoaClassAddWXMethods( self );
597 - (void) setTrackingTag: (NSTrackingRectTag)tag
602 - (NSTrackingRectTag) trackingTag
607 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
608 - (void) updateTrackingArea
612 [self removeTrackingArea: _trackingArea];
613 [_trackingArea release];
616 NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveAlways;
618 NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect: [self bounds] options: options owner: self userInfo: nil];
619 [self addTrackingArea: area];
621 _trackingArea = area;
624 - (NSTrackingArea*) trackingArea
626 return _trackingArea;
635 #if wxUSE_DRAG_AND_DROP
637 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
638 // for details on the NSPasteboard -> PasteboardRef conversion
640 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
642 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
644 return NSDragOperationNone;
646 return impl->draggingEntered(sender, self, _cmd);
649 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
651 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
655 return impl->draggingExited(sender, self, _cmd);
658 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
660 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
662 return NSDragOperationNone;
664 return impl->draggingUpdated(sender, self, _cmd);
667 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
669 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
671 return NSDragOperationNone;
673 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
678 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
680 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
684 impl->mouseEvent(event, self, _cmd);
687 BOOL wxOSX_acceptsFirstMouse(NSView* self, SEL _cmd, NSEvent *event)
689 // This is needed to support click through, otherwise the first click on a window
690 // will not do anything unless it is the active window already.
694 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
696 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
700 impl->keyEvent(event, self, _cmd);
703 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
705 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
709 impl->insertText(text, self, _cmd);
712 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
714 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
718 return impl->performKeyEquivalent(event, self, _cmd);
721 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
723 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
727 return impl->acceptsFirstResponder(self, _cmd);
730 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
732 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
736 return impl->becomeFirstResponder(self, _cmd);
739 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
741 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
745 return impl->resignFirstResponder(self, _cmd);
748 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
750 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
754 impl->resetCursorRects(self, _cmd);
757 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
759 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
763 return impl->isFlipped(self, _cmd) ? YES:NO;
766 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
768 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
772 return impl->drawRect(&rect, self, _cmd);
775 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
777 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
781 impl->controlAction(self, _cmd, sender);
784 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
786 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
790 impl->controlDoubleAction(self, _cmd, sender);
793 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
795 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
796 NSPasteboard *pboard = [sender draggingPasteboard];
797 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
799 wxWindow* wxpeer = GetWXPeer();
800 if ( wxpeer == NULL )
801 return NSDragOperationNone;
803 wxDropTarget* target = wxpeer->GetDropTarget();
804 if ( target == NULL )
805 return NSDragOperationNone;
807 wxDragResult result = wxDragNone;
808 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
809 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
811 if ( sourceDragMask & NSDragOperationLink )
813 else if ( sourceDragMask & NSDragOperationCopy )
815 else if ( sourceDragMask & NSDragOperationMove )
818 PasteboardRef pboardRef;
819 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
820 target->SetCurrentDragPasteboard(pboardRef);
821 result = target->OnEnter(pt.x, pt.y, result);
822 CFRelease(pboardRef);
824 NSDragOperation nsresult = NSDragOperationNone;
828 nsresult = NSDragOperationLink;
830 nsresult = NSDragOperationMove;
832 nsresult = NSDragOperationCopy;
839 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
841 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
842 NSPasteboard *pboard = [sender draggingPasteboard];
844 wxWindow* wxpeer = GetWXPeer();
845 if ( wxpeer == NULL )
848 wxDropTarget* target = wxpeer->GetDropTarget();
849 if ( target == NULL )
852 PasteboardRef pboardRef;
853 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
854 target->SetCurrentDragPasteboard(pboardRef);
856 CFRelease(pboardRef);
859 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
861 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
862 NSPasteboard *pboard = [sender draggingPasteboard];
863 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
865 wxWindow* wxpeer = GetWXPeer();
866 if ( wxpeer == NULL )
867 return NSDragOperationNone;
869 wxDropTarget* target = wxpeer->GetDropTarget();
870 if ( target == NULL )
871 return NSDragOperationNone;
873 wxDragResult result = wxDragNone;
874 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
875 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
877 if ( sourceDragMask & NSDragOperationLink )
879 else if ( sourceDragMask & NSDragOperationCopy )
881 else if ( sourceDragMask & NSDragOperationMove )
884 PasteboardRef pboardRef;
885 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
886 target->SetCurrentDragPasteboard(pboardRef);
887 result = target->OnDragOver(pt.x, pt.y, result);
888 CFRelease(pboardRef);
890 NSDragOperation nsresult = NSDragOperationNone;
894 nsresult = NSDragOperationLink;
896 nsresult = NSDragOperationMove;
898 nsresult = NSDragOperationCopy;
905 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
907 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
909 NSPasteboard *pboard = [sender draggingPasteboard];
910 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
912 wxWindow* wxpeer = GetWXPeer();
913 wxDropTarget* target = wxpeer->GetDropTarget();
914 wxDragResult result = wxDragNone;
915 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
916 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
918 if ( sourceDragMask & NSDragOperationLink )
920 else if ( sourceDragMask & NSDragOperationCopy )
922 else if ( sourceDragMask & NSDragOperationMove )
925 PasteboardRef pboardRef;
926 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
927 target->SetCurrentDragPasteboard(pboardRef);
929 if (target->OnDrop(pt.x, pt.y))
930 result = target->OnData(pt.x, pt.y, result);
932 CFRelease(pboardRef);
934 return result != wxDragNone;
937 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
938 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
939 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
940 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
941 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
942 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
944 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
946 if ( !DoHandleMouseEvent(event) )
948 // for plain NSView mouse events would propagate to parents otherwise
949 if (!m_wxPeer->MacIsUserPane())
951 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
952 superimpl(slf, (SEL)_cmd, event);
957 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
959 if ( [event type] == NSKeyDown )
960 m_lastKeyDownEvent = event;
961 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
963 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
964 superimpl(slf, (SEL)_cmd, event);
966 m_lastKeyDownEvent = NULL;
969 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
971 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
973 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
974 superimpl(slf, (SEL)_cmd, text);
979 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
981 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
982 return superimpl(slf, (SEL)_cmd, event);
985 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
987 if ( m_wxPeer->MacIsUserPane() )
988 return m_wxPeer->AcceptsFocus();
991 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
992 return superimpl(slf, (SEL)_cmd);
996 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
998 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
999 // get the current focus before running becomeFirstResponder
1000 NSView* otherView = FindFocus();
1002 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1003 BOOL r = superimpl(slf, (SEL)_cmd);
1006 DoNotifyFocusEvent( true, otherWindow );
1012 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
1014 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1015 BOOL r = superimpl(slf, (SEL)_cmd);
1016 // get the current focus after running resignFirstResponder
1017 // note that this value isn't reliable, it might return the same view that
1019 NSView* otherView = FindFocus();
1020 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1021 // NSTextViews have an editor as true responder, therefore the might get the
1022 // resign notification if their editor takes over, don't trigger any event then
1023 if ( r && !m_hasEditor)
1025 DoNotifyFocusEvent( false, otherWindow );
1030 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
1032 wxWindow* wxpeer = GetWXPeer();
1035 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
1038 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1039 superimpl(slf, (SEL)_cmd);
1043 [slf addCursorRect: [slf bounds]
1049 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1055 #define OSX_DEBUG_DRAWING 0
1057 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
1059 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
1060 CGContextSaveGState( context );
1062 #if OSX_DEBUG_DRAWING
1063 CGContextBeginPath( context );
1064 CGContextMoveToPoint(context, 0, 0);
1065 NSRect bounds = [self bounds];
1066 CGContextAddLineToPoint(context, 10, 0);
1067 CGContextMoveToPoint(context, 0, 0);
1068 CGContextAddLineToPoint(context, 0, 10);
1069 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1070 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1071 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1072 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1073 CGContextClosePath( context );
1074 CGContextStrokePath(context);
1079 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1080 CGContextScaleCTM( context, 1, -1 );
1084 const NSRect *rects;
1087 [slf getRectsBeingDrawn:&rects count:&count];
1088 for ( int i = 0 ; i < count ; ++i )
1090 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1093 wxWindow* wxpeer = GetWXPeer();
1094 wxpeer->GetUpdateRegion() = updateRgn;
1095 wxpeer->MacSetCGContextRef( context );
1097 bool handled = wxpeer->MacDoRedraw( 0 );
1099 CGContextRestoreGState( context );
1101 CGContextSaveGState( context );
1105 SEL _cmd = @selector(drawRect:);
1106 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1107 superimpl(slf, _cmd, *(NSRect*)rect);
1108 CGContextRestoreGState( context );
1109 CGContextSaveGState( context );
1111 wxpeer->MacPaintChildrenBorders();
1112 wxpeer->MacSetCGContextRef( NULL );
1113 CGContextRestoreGState( context );
1116 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1118 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1120 wxpeer->OSXHandleClicked(0);
1123 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1127 void wxWidgetCocoaImpl::controlTextDidChange()
1129 wxWindow* wxpeer = (wxWindow*)GetWXPeer();
1132 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
1133 event.SetEventObject( wxpeer );
1134 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
1135 wxpeer->HandleWindowEvent( event );
1141 #if OBJC_API_VERSION >= 2
1143 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1144 class_addMethod(c, s, i, t );
1148 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1153 void wxOSXCocoaClassAddWXMethods(Class c)
1156 #if OBJC_API_VERSION < 2
1157 static objc_method wxmethods[] =
1161 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1162 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1163 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1165 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1166 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1167 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1169 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1171 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1172 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1173 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1175 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstMouse:), (IMP) wxOSX_acceptsFirstMouse, "v@:@" )
1177 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1178 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1179 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1181 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1182 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1183 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1185 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1187 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1189 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1190 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1191 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1192 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1194 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1195 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1197 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1198 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1200 #if wxUSE_DRAG_AND_DROP
1201 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1202 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1203 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1204 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1207 #if OBJC_API_VERSION < 2
1209 static int method_count = WXSIZEOF( wxmethods );
1210 static objc_method_list *wxmethodlist = NULL;
1211 if ( wxmethodlist == NULL )
1213 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1214 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1215 wxmethodlist->method_count = method_count;
1216 wxmethodlist->obsolete = 0;
1218 class_addMethods( c, wxmethodlist );
1223 // C++ implementation class
1226 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1228 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1229 wxWidgetImpl( peer, isRootControl )
1234 // check if the user wants to create the control initially hidden
1235 if ( !peer->IsShown() )
1236 SetVisibility(false);
1238 // gc aware handling
1240 CFRetain(m_osxView);
1241 [m_osxView release];
1244 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1249 void wxWidgetCocoaImpl::Init()
1253 m_lastKeyDownEvent = NULL;
1254 m_hasEditor = false;
1257 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1259 RemoveAssociations( this );
1261 if ( !IsRootControl() )
1263 NSView *sv = [m_osxView superview];
1265 [m_osxView removeFromSuperview];
1267 // gc aware handling
1269 CFRelease(m_osxView);
1272 bool wxWidgetCocoaImpl::IsVisible() const
1274 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1277 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1279 [m_osxView setHidden:(visible ? NO:YES)];
1282 // ----------------------------------------------------------------------------
1283 // window animation stuff
1284 // ----------------------------------------------------------------------------
1286 // define a delegate used to refresh the window during animation
1287 @interface wxNSAnimationDelegate : NSObject wxOSX_10_6_AND_LATER(<NSAnimationDelegate>)
1293 - (id)init:(wxWindow *)win;
1297 // NSAnimationDelegate methods
1298 - (void)animationDidEnd:(NSAnimation*)animation;
1299 - (void)animation:(NSAnimation*)animation
1300 didReachProgressMark:(NSAnimationProgress)progress;
1303 @implementation wxNSAnimationDelegate
1305 - (id)init:(wxWindow *)win
1320 - (void)animation:(NSAnimation*)animation
1321 didReachProgressMark:(NSAnimationProgress)progress
1323 wxUnusedVar(animation);
1324 wxUnusedVar(progress);
1326 m_win->SendSizeEvent();
1329 - (void)animationDidEnd:(NSAnimation*)animation
1338 wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1340 wxShowEffect effect,
1343 // create the dictionary describing the animation to perform on this view
1345 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1346 NSMutableDictionary * const
1347 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1348 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1350 // determine the start and end rectangles assuming we're hiding the window
1351 const wxRect rectOrig = win->GetRect();
1359 if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1360 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1361 effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1362 else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1363 effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1364 effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1365 else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1366 effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1367 effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1368 else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1369 effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1370 effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1375 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1376 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1380 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1381 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1382 rectEnd.x = rectStart.GetRight();
1386 case wxSHOW_EFFECT_ROLL_TO_TOP:
1387 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1391 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1392 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1393 rectEnd.y = rectStart.GetBottom();
1397 case wxSHOW_EFFECT_EXPAND:
1398 rectEnd.x = rectStart.x + rectStart.width / 2;
1399 rectEnd.y = rectStart.y + rectStart.height / 2;
1404 case wxSHOW_EFFECT_BLEND:
1405 [dict setObject:(show ? NSViewAnimationFadeInEffect
1406 : NSViewAnimationFadeOutEffect)
1407 forKey:NSViewAnimationEffectKey];
1410 case wxSHOW_EFFECT_NONE:
1411 case wxSHOW_EFFECT_MAX:
1412 wxFAIL_MSG( "unexpected animation effect" );
1416 wxFAIL_MSG( "unknown animation effect" );
1422 // we need to restore it to the original rectangle instead of making it
1424 wxSwap(rectStart, rectEnd);
1426 // and as the window is currently hidden, we need to show it for the
1427 // animation to be visible at all (but don't restore it at its full
1428 // rectangle as it shouldn't appear immediately)
1429 win->SetSize(rectStart);
1433 NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1434 ? [(NSView *)viewOrWin superview]
1436 const NSRect rStart = wxToNSRect(parentView, rectStart);
1437 const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1439 [dict setObject:[NSValue valueWithRect:rStart]
1440 forKey:NSViewAnimationStartFrameKey];
1441 [dict setObject:[NSValue valueWithRect:rEnd]
1442 forKey:NSViewAnimationEndFrameKey];
1444 // create an animation using the values in the above dictionary
1445 NSViewAnimation * const
1446 anim = [[NSViewAnimation alloc]
1447 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1451 // what is a good default duration? Windows uses 200ms, Web frameworks
1452 // use anything from 250ms to 1s... choose something in the middle
1456 [anim setDuration:timeout/1000.]; // duration is in seconds here
1458 // if the window being animated changes its layout depending on its size
1459 // (which is almost always the case) we need to redo it during animation
1461 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1462 // controls in wxInfoBar visibly jump around)
1463 const int NUM_LAYOUTS = 20;
1464 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1465 [anim addProgressMark:f];
1467 wxNSAnimationDelegate * const
1468 animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1469 [anim setDelegate:animDelegate];
1470 [anim startAnimation];
1472 // Cocoa is capable of doing animation asynchronously or even from separate
1473 // thread but wx API doesn't provide any way to be notified about the
1474 // animation end and without this we really must ensure that the window has
1475 // the expected (i.e. the same as if a simple Show() had been used) size
1476 // when we return, so block here until the animation finishes
1478 // notice that because the default animation mode is NSAnimationBlocking,
1479 // no user input events ought to be processed from here
1481 wxEventLoopGuarantor ensureEventLoopExistence;
1482 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1483 while ( ![animDelegate isDone] )
1489 // NSViewAnimation is smart enough to hide the NSView being animated at
1490 // the end but we also must ensure that it's hidden for wx too
1493 // and we must also restore its size because it isn't expected to
1494 // change just because the window was hidden
1495 win->SetSize(rectOrig);
1499 // refresh it once again after the end to ensure that everything is in
1501 win->SendSizeEvent();
1504 [anim setDelegate:nil];
1505 [animDelegate release];
1511 bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1512 wxShowEffect effect,
1515 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1518 void wxWidgetCocoaImpl::Raise()
1523 void wxWidgetCocoaImpl::Lower()
1528 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1533 // We should do something like this, but it wasn't working in 10.4.
1534 if (GetNeedsDisplay() )
1538 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1539 NSSize offset = NSMakeSize((float)dx, (float)dy);
1540 [m_osxView scrollRect:r by:offset];
1544 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1546 wxWindowMac* parent = GetWXPeer()->GetParent();
1547 // under Cocoa we might have a contentView in the wxParent to which we have to
1548 // adjust the coordinates
1549 if (parent && [m_osxView superview] != parent->GetHandle() )
1551 int cx = 0,cy = 0,cw = 0,ch = 0;
1552 if ( parent->GetPeer() )
1554 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1559 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1560 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1561 [m_osxView setFrame:r];
1562 [[m_osxView superview] setNeedsDisplayInRect:r];
1564 wxNSView* wxview = (wxNSView*)m_osxView;
1565 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1566 if ([wxview respondsToSelector:@selector(updateTrackingArea)] )
1567 [wxview updateTrackingArea];
1569 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1571 if ( [wxview trackingTag] )
1572 [wxview removeTrackingRect: [wxview trackingTag]];
1574 [wxview setTrackingTag: [wxview addTrackingRect: [m_osxView bounds] owner: wxview userData: nil assumeInside: NO]];
1579 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1581 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1586 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1588 NSRect rect = [m_osxView frame];
1589 width = (int)rect.size.width;
1590 height = (int)rect.size.height;
1593 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1595 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1597 NSView* cv = [m_osxView contentView];
1599 NSRect bounds = [m_osxView bounds];
1600 NSRect rect = [cv frame];
1602 int y = (int)rect.origin.y;
1603 int x = (int)rect.origin.x;
1604 if ( ![ m_osxView isFlipped ] )
1605 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1608 width = (int)rect.size.width;
1609 height = (int)rect.size.height;
1614 GetSize( width, height );
1618 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1621 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1623 [m_osxView setNeedsDisplay:YES];
1626 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1628 return [m_osxView needsDisplay];
1631 bool wxWidgetCocoaImpl::CanFocus() const
1633 return [m_osxView canBecomeKeyView] == YES;
1636 bool wxWidgetCocoaImpl::HasFocus() const
1638 return ( FindFocus() == m_osxView );
1641 bool wxWidgetCocoaImpl::SetFocus()
1643 if ( [m_osxView canBecomeKeyView] == NO )
1646 [[m_osxView window] makeFirstResponder: m_osxView] ;
1647 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1652 void wxWidgetCocoaImpl::RemoveFromParent()
1654 [m_osxView removeFromSuperview];
1657 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1659 NSView* container = parent->GetWXWidget() ;
1660 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1661 [container addSubview:m_osxView];
1664 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1666 NSView* targetView = m_osxView;
1667 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1668 targetView = [(NSScrollView*) m_osxView documentView];
1670 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1672 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1673 green:(CGFloat) (col.Green() / 255.0)
1674 blue:(CGFloat) (col.Blue() / 255.0)
1675 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1679 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1681 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1683 wxCFStringRef cf( title , encoding );
1684 [m_osxView setTitle:cf.AsNSString()];
1686 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1688 wxCFStringRef cf( title , encoding );
1689 [m_osxView setStringValue:cf.AsNSString()];
1694 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1696 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1697 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1698 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1701 wxInt32 wxWidgetCocoaImpl::GetValue() const
1703 return [(NSControl*)m_osxView intValue];
1706 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1708 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1710 [m_osxView setIntValue:v];
1712 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1714 [m_osxView setFloatValue:(double)v];
1716 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1718 [m_osxView setDoubleValue:(double)v];
1722 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1724 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1726 [m_osxView setMinValue:(double)v];
1730 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1732 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1734 [m_osxView setMaxValue:(double)v];
1738 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1740 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1742 return (int)[m_osxView minValue];
1747 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1749 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1751 return (int)[m_osxView maxValue];
1756 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1760 // TODO: how to create a wxBitmap from NSImage?
1762 if ( [m_osxView respondsToSelector:@selector(image:)] )
1763 bmp = [m_osxView image];
1769 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1771 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1773 [m_osxView setImage:bitmap.GetNSImage()];
1774 [m_osxView setNeedsDisplay:YES];
1778 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1780 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1782 NSCellImagePosition pos;
1802 wxFAIL_MSG( "invalid image position" );
1806 [m_osxView setImagePosition:pos];
1810 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1812 // implementation in subclass
1815 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1817 r->x = r->y = r->width = r->height = 0;
1819 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1821 NSRect former = [m_osxView frame];
1822 [m_osxView sizeToFit];
1823 NSRect best = [m_osxView frame];
1824 [m_osxView setFrame:former];
1825 r->width = (int)best.size.width;
1826 r->height = (int)best.size.height;
1830 bool wxWidgetCocoaImpl::IsEnabled() const
1832 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1833 return [m_osxView isEnabled];
1837 void wxWidgetCocoaImpl::Enable( bool enable )
1839 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1840 [m_osxView setEnabled:enable];
1843 void wxWidgetCocoaImpl::PulseGauge()
1847 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1851 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1853 NSControlSize size = NSRegularControlSize;
1857 case wxWINDOW_VARIANT_NORMAL :
1858 size = NSRegularControlSize;
1861 case wxWINDOW_VARIANT_SMALL :
1862 size = NSSmallControlSize;
1865 case wxWINDOW_VARIANT_MINI :
1866 size = NSMiniControlSize;
1869 case wxWINDOW_VARIANT_LARGE :
1870 size = NSRegularControlSize;
1874 wxFAIL_MSG(wxT("unexpected window variant"));
1877 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1878 [m_osxView setControlSize:size];
1879 else if ([m_osxView respondsToSelector:@selector(cell)])
1881 id cell = [(id)m_osxView cell];
1882 if ([cell respondsToSelector:@selector(setControlSize:)])
1883 [cell setControlSize:size];
1887 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1889 if ([m_osxView respondsToSelector:@selector(setFont:)])
1890 [m_osxView setFont: font.OSXGetNSFont()];
1893 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1895 WXWidget c = control ? control : (WXWidget) m_osxView;
1896 wxWidgetImpl::Associate( c, this ) ;
1897 if ([c respondsToSelector:@selector(setAction:)])
1900 [c setAction: @selector(controlAction:)];
1901 if ([c respondsToSelector:@selector(setDoubleAction:)])
1903 [c setDoubleAction: @selector(controlDoubleAction:)];
1909 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1911 wxKeyEvent wxevent(wxEVT_CHAR);
1912 SetupKeyEvent( wxevent, event, text );
1914 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1917 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1919 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1920 SetupKeyEvent( wxevent, event );
1921 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1923 // this will fire higher level events, like insertText, to help
1924 // us handle EVT_CHAR, etc.
1926 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1930 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1931 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1933 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1941 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1943 NSPoint clickLocation;
1944 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1945 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1946 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1947 SetupMouseEvent(wxevent , event) ;
1951 return GetWXPeer()->HandleWindowEvent(wxevent);
1954 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1956 wxWindow* thisWindow = GetWXPeer();
1957 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1959 thisWindow->MacInvalidateBorders();
1962 if ( receivedFocus )
1964 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1965 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1966 thisWindow->HandleWindowEvent(eventFocus);
1969 if ( thisWindow->GetCaret() )
1970 thisWindow->GetCaret()->OnSetFocus();
1973 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1974 event.SetEventObject(thisWindow);
1976 event.SetWindow(otherWindow->GetWXPeer());
1977 thisWindow->HandleWindowEvent(event) ;
1979 else // !receivedFocuss
1982 if ( thisWindow->GetCaret() )
1983 thisWindow->GetCaret()->OnKillFocus();
1986 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1988 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1989 event.SetEventObject(thisWindow);
1991 event.SetWindow(otherWindow->GetWXPeer());
1992 thisWindow->HandleWindowEvent(event) ;
1996 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1998 NSPoint location = [NSEvent mouseLocation];
1999 location = [[m_osxView window] convertScreenToBase:location];
2000 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
2002 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
2004 [(NSCursor*)cursor.GetHCURSOR() set];
2006 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
2009 void wxWidgetCocoaImpl::CaptureMouse()
2011 [[m_osxView window] disableCursorRects];
2014 void wxWidgetCocoaImpl::ReleaseMouse()
2016 [[m_osxView window] enableCursorRects];
2019 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
2021 m_isFlipped = flipped;
2028 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
2029 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
2030 long WXUNUSED(style), long WXUNUSED(extraStyle))
2032 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
2033 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
2035 // temporary hook for dnd
2036 [v registerForDraggedTypes:[NSArray arrayWithObjects:
2037 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
2039 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
2043 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
2045 NSWindow* tlw = now->GetWXWindow();
2046 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
2047 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
2048 c->InstallEventHandler();
2049 [tlw setContentView:v];