1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/window.mm
3 // Purpose: widgets (non tlw) for cocoa
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/dcclient.h"
16 #include "wx/nonownedwnd.h"
21 #include "wx/osx/private.h"
24 #include "wx/hashmap.h"
30 #if wxUSE_DRAG_AND_DROP
34 #include <objc/objc-runtime.h>
39 // stop animation of this window if one is in progress
40 void StopAnimation(wxWindow *win);
42 } // anonymous namespace
45 // Get the window with the focus
47 NSView* GetViewFromResponder( NSResponder* responder )
50 if ( [responder isKindOfClass:[NSTextView class]] )
52 NSView* delegate = [(NSTextView*)responder delegate];
53 if ( [delegate isKindOfClass:[NSTextField class] ] )
56 view = (NSView*) responder;
60 if ( [responder isKindOfClass:[NSView class]] )
61 view = (NSView*) responder;
66 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
68 NSView* focusedView = nil;
69 if ( keyWindow != nil )
70 focusedView = GetViewFromResponder([keyWindow firstResponder]);
75 WXWidget wxWidgetImpl::FindFocus()
77 return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
80 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
84 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
85 wxRect bounds(x,y,w,h);
86 NSView* sv = (window->GetParent()->GetHandle() );
88 return wxToNSRect( sv, bounds );
91 @interface wxNSView : NSView
93 NSTrackingRectTag rectTag;
96 // the tracking tag is needed to track mouse enter / exit events
97 - (void) setTrackingTag: (NSTrackingRectTag)tag;
98 - (NSTrackingRectTag) trackingTag;
101 @interface NSView(PossibleMethods)
102 - (void)setTitle:(NSString *)aString;
103 - (void)setStringValue:(NSString *)aString;
104 - (void)setIntValue:(int)anInt;
105 - (void)setFloatValue:(float)aFloat;
106 - (void)setDoubleValue:(double)aDouble;
110 - (void)setMinValue:(double)aDouble;
111 - (void)setMaxValue:(double)aDouble;
116 - (void)setEnabled:(BOOL)flag;
118 - (void)setImage:(NSImage *)image;
119 - (void)setControlSize:(NSControlSize)size;
121 - (void)setFont:(NSFont *)fontObject;
125 - (void)setTarget:(id)anObject;
126 - (void)setAction:(SEL)aSelector;
127 - (void)setDoubleAction:(SEL)aSelector;
128 - (void)setBackgroundColor:(NSColor*)aColor;
129 - (void)setImagePosition:(NSCellImagePosition)aPosition;
132 long wxOSXTranslateCocoaKey( NSEvent* event )
136 if ([event type] != NSFlagsChanged)
138 NSString* s = [event charactersIgnoringModifiers];
139 // backspace char reports as delete w/modifiers for some reason
142 switch ( [s characterAtIndex:0] )
149 case NSUpArrowFunctionKey :
152 case NSDownArrowFunctionKey :
155 case NSLeftArrowFunctionKey :
158 case NSRightArrowFunctionKey :
161 case NSInsertFunctionKey :
164 case NSDeleteFunctionKey :
167 case NSHomeFunctionKey :
170 // case NSBeginFunctionKey :
171 // retval = WXK_BEGIN;
173 case NSEndFunctionKey :
176 case NSPageUpFunctionKey :
179 case NSPageDownFunctionKey :
180 retval = WXK_PAGEDOWN;
182 case NSHelpFunctionKey :
186 int intchar = [s characterAtIndex: 0];
187 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
188 retval = WXK_F1 + (intchar - NSF1FunctionKey );
194 // Some keys don't seem to have constants. The code mimics the approach
195 // taken by WebKit. See:
196 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
197 switch( [event keyCode] )
202 retval = WXK_COMMAND;
206 retval = WXK_CAPITAL;
209 case 56: // Left Shift
210 case 60: // Right Shift
215 case 61: // Right Alt
219 case 59: // Left Ctrl
220 case 62: // Right Ctrl
221 retval = WXK_CONTROL;
233 retval = WXK_NUMPAD_DIVIDE;
236 retval = WXK_NUMPAD_MULTIPLY;
239 retval = WXK_NUMPAD_SUBTRACT;
242 retval = WXK_NUMPAD_ADD;
245 retval = WXK_NUMPAD_ENTER;
248 retval = WXK_NUMPAD_DECIMAL;
251 retval = WXK_NUMPAD0;
254 retval = WXK_NUMPAD1;
257 retval = WXK_NUMPAD2;
260 retval = WXK_NUMPAD3;
263 retval = WXK_NUMPAD4;
266 retval = WXK_NUMPAD5;
269 retval = WXK_NUMPAD6;
272 retval = WXK_NUMPAD7;
275 retval = WXK_NUMPAD8;
278 retval = WXK_NUMPAD9;
281 //retval = [event keyCode];
287 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
289 UInt32 modifiers = [nsEvent modifierFlags] ;
290 int eventType = [nsEvent type];
292 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
293 wxevent.m_controlDown = modifiers & NSControlKeyMask;
294 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
295 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
297 wxevent.m_rawCode = [nsEvent keyCode];
298 wxevent.m_rawFlags = modifiers;
300 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
303 if ( eventType != NSFlagsChanged )
305 NSString* nschars = (wxevent.GetEventType() != wxEVT_CHAR) ? [nsEvent charactersIgnoringModifiers] : [nsEvent characters];
308 // if charString is set, it did not come from key up / key down
309 wxevent.SetEventType( wxEVT_CHAR );
310 chars = wxCFStringRef::AsString(charString);
314 chars = wxCFStringRef::AsString(nschars);
318 int aunichar = chars.Length() > 0 ? chars[0] : 0;
321 if (wxevent.GetEventType() != wxEVT_CHAR)
323 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
327 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
330 wxevent.SetEventType( wxEVT_KEY_UP ) ;
332 case NSFlagsChanged :
336 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
339 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
342 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
345 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
356 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
357 keyval = wxToupper( aunichar ) ;
363 wxevent.m_uniChar = aunichar;
365 wxevent.m_keyCode = keyval;
367 wxWindowMac* peer = GetWXPeer();
370 wxevent.SetEventObject(peer);
371 wxevent.SetId(peer->GetId()) ;
375 UInt32 g_lastButton = 0 ;
376 bool g_lastButtonWasFakeRight = false ;
378 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
380 int eventType = [nsEvent type];
381 UInt32 modifiers = [nsEvent modifierFlags] ;
382 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
384 // these parameters are not given for all events
385 UInt32 button = [nsEvent buttonNumber];
386 UInt32 clickCount = 0;
388 wxevent.m_x = screenMouseLocation.x;
389 wxevent.m_y = screenMouseLocation.y;
390 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
391 wxevent.m_controlDown = modifiers & NSControlKeyMask;
392 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
393 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
394 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
396 UInt32 mouseChord = 0;
400 case NSLeftMouseDown :
401 case NSLeftMouseDragged :
404 case NSRightMouseDown :
405 case NSRightMouseDragged :
408 case NSOtherMouseDown :
409 case NSOtherMouseDragged :
414 // a control click is interpreted as a right click
415 bool thisButtonIsFakeRight = false ;
416 if ( button == 0 && (modifiers & NSControlKeyMask) )
419 thisButtonIsFakeRight = true ;
422 // otherwise we report double clicks by connecting a left click with a ctrl-left click
423 if ( clickCount > 1 && button != g_lastButton )
426 // we must make sure that our synthetic 'right' button corresponds in
427 // mouse down, moved and mouse up, and does not deliver a right down and left up
430 case NSLeftMouseDown :
431 case NSRightMouseDown :
432 case NSOtherMouseDown :
433 g_lastButton = button ;
434 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
441 g_lastButtonWasFakeRight = false ;
443 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
444 button = g_lastButton ;
446 // Adjust the chord mask to remove the primary button and add the
447 // secondary button. It is possible that the secondary button is
448 // already pressed, e.g. on a mouse connected to a laptop, but this
449 // possibility is ignored here:
450 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
451 mouseChord = ((mouseChord & ~1U) | 2U);
454 wxevent.m_leftDown = true ;
456 wxevent.m_rightDown = true ;
458 wxevent.m_middleDown = true ;
460 // translate into wx types
463 case NSLeftMouseDown :
464 case NSRightMouseDown :
465 case NSOtherMouseDown :
466 clickCount = [nsEvent clickCount];
470 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
474 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
478 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
487 case NSRightMouseUp :
488 case NSOtherMouseUp :
489 clickCount = [nsEvent clickCount];
493 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
497 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
501 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
511 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
512 wxevent.m_wheelDelta = 10;
513 wxevent.m_linesPerAction = 1;
515 if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
517 wxevent.m_wheelAxis = 1;
518 wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
522 wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
527 case NSMouseEntered :
528 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
531 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
533 case NSLeftMouseDragged :
534 case NSRightMouseDragged :
535 case NSOtherMouseDragged :
537 wxevent.SetEventType( wxEVT_MOTION ) ;
543 wxevent.m_clickCount = clickCount;
544 wxWindowMac* peer = GetWXPeer();
547 wxevent.SetEventObject(peer);
548 wxevent.SetId(peer->GetId()) ;
552 @implementation wxNSView
556 static BOOL initialized = NO;
560 wxOSXCocoaClassAddWXMethods( self );
564 - (void) setTrackingTag: (NSTrackingRectTag)tag
569 - (NSTrackingRectTag) trackingTag
580 #if wxUSE_DRAG_AND_DROP
582 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
583 // for details on the NSPasteboard -> PasteboardRef conversion
585 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
587 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
589 return NSDragOperationNone;
591 return impl->draggingEntered(sender, self, _cmd);
594 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
596 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
600 return impl->draggingExited(sender, self, _cmd);
603 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
605 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
607 return NSDragOperationNone;
609 return impl->draggingUpdated(sender, self, _cmd);
612 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
614 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
616 return NSDragOperationNone;
618 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
623 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
625 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
629 impl->mouseEvent(event, self, _cmd);
632 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
634 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
638 impl->keyEvent(event, self, _cmd);
641 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
643 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
647 impl->insertText(text, self, _cmd);
650 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
652 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
656 return impl->performKeyEquivalent(event, self, _cmd);
659 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
661 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
665 return impl->acceptsFirstResponder(self, _cmd);
668 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
670 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
674 return impl->becomeFirstResponder(self, _cmd);
677 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
679 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
683 return impl->resignFirstResponder(self, _cmd);
686 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
688 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
692 impl->resetCursorRects(self, _cmd);
695 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
697 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
701 return impl->isFlipped(self, _cmd) ? YES:NO;
704 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
706 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
710 return impl->drawRect(&rect, self, _cmd);
713 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
715 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
719 impl->controlAction(self, _cmd, sender);
722 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
724 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
728 impl->controlDoubleAction(self, _cmd, sender);
731 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
733 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
734 NSPasteboard *pboard = [sender draggingPasteboard];
735 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
737 wxWindow* wxpeer = GetWXPeer();
738 if ( wxpeer == NULL )
739 return NSDragOperationNone;
741 wxDropTarget* target = wxpeer->GetDropTarget();
742 if ( target == NULL )
743 return NSDragOperationNone;
745 wxDragResult result = wxDragNone;
746 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
747 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
749 if ( sourceDragMask & NSDragOperationLink )
751 else if ( sourceDragMask & NSDragOperationCopy )
753 else if ( sourceDragMask & NSDragOperationMove )
756 PasteboardRef pboardRef;
757 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
758 target->SetCurrentDragPasteboard(pboardRef);
759 result = target->OnEnter(pt.x, pt.y, result);
760 CFRelease(pboardRef);
762 NSDragOperation nsresult = NSDragOperationNone;
766 nsresult = NSDragOperationLink;
768 nsresult = NSDragOperationMove;
770 nsresult = NSDragOperationCopy;
777 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
779 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
780 NSPasteboard *pboard = [sender draggingPasteboard];
782 wxWindow* wxpeer = GetWXPeer();
783 if ( wxpeer == NULL )
786 wxDropTarget* target = wxpeer->GetDropTarget();
787 if ( target == NULL )
790 PasteboardRef pboardRef;
791 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
792 target->SetCurrentDragPasteboard(pboardRef);
794 CFRelease(pboardRef);
797 unsigned int wxWidgetCocoaImpl::draggingUpdated(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->OnDragOver(pt.x, pt.y, result);
826 CFRelease(pboardRef);
828 NSDragOperation nsresult = NSDragOperationNone;
832 nsresult = NSDragOperationLink;
834 nsresult = NSDragOperationMove;
836 nsresult = NSDragOperationCopy;
843 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
845 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
847 NSPasteboard *pboard = [sender draggingPasteboard];
848 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
850 wxWindow* wxpeer = GetWXPeer();
851 wxDropTarget* target = wxpeer->GetDropTarget();
852 wxDragResult result = wxDragNone;
853 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
854 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
856 if ( sourceDragMask & NSDragOperationLink )
858 else if ( sourceDragMask & NSDragOperationCopy )
860 else if ( sourceDragMask & NSDragOperationMove )
863 PasteboardRef pboardRef;
864 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
865 target->SetCurrentDragPasteboard(pboardRef);
867 if (target->OnDrop(pt.x, pt.y))
868 result = target->OnData(pt.x, pt.y, result);
870 CFRelease(pboardRef);
872 return result != wxDragNone;
875 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
876 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
877 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
878 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
879 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
880 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
882 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
884 if ( !DoHandleMouseEvent(event) )
886 // for plain NSView mouse events would propagate to parents otherwise
887 if (!m_wxPeer->MacIsUserPane())
889 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
890 superimpl(slf, (SEL)_cmd, event);
895 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
897 if ( [event type] == NSKeyDown )
898 m_lastKeyDownEvent = event;
899 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
901 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
902 superimpl(slf, (SEL)_cmd, event);
904 m_lastKeyDownEvent = NULL;
907 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
909 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
911 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
912 superimpl(slf, (SEL)_cmd, text);
917 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
919 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
920 return superimpl(slf, (SEL)_cmd, event);
923 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
925 if ( m_wxPeer->MacIsUserPane() )
926 return m_wxPeer->AcceptsFocus();
929 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
930 return superimpl(slf, (SEL)_cmd);
934 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
936 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
937 // get the current focus before running becomeFirstResponder
938 NSView* otherView = FindFocus();
940 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
941 BOOL r = superimpl(slf, (SEL)_cmd);
944 DoNotifyFocusEvent( true, otherWindow );
950 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
952 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
953 BOOL r = superimpl(slf, (SEL)_cmd);
954 // get the current focus after running resignFirstResponder
955 // note that this value isn't reliable, it might return the same view that
957 NSView* otherView = FindFocus();
958 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
959 // NSTextViews have an editor as true responder, therefore the might get the
960 // resign notification if their editor takes over, don't trigger any event then
961 if ( r && !m_hasEditor)
963 DoNotifyFocusEvent( false, otherWindow );
968 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
970 wxWindow* wxpeer = GetWXPeer();
973 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
976 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
977 superimpl(slf, (SEL)_cmd);
981 [slf addCursorRect: [slf bounds]
987 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
993 #define OSX_DEBUG_DRAWING 0
995 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
997 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
998 CGContextSaveGState( context );
1000 #if OSX_DEBUG_DRAWING
1001 CGContextBeginPath( context );
1002 CGContextMoveToPoint(context, 0, 0);
1003 NSRect bounds = [self bounds];
1004 CGContextAddLineToPoint(context, 10, 0);
1005 CGContextMoveToPoint(context, 0, 0);
1006 CGContextAddLineToPoint(context, 0, 10);
1007 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1008 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1009 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1010 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1011 CGContextClosePath( context );
1012 CGContextStrokePath(context);
1017 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1018 CGContextScaleCTM( context, 1, -1 );
1022 const NSRect *rects;
1025 [slf getRectsBeingDrawn:&rects count:&count];
1026 for ( int i = 0 ; i < count ; ++i )
1028 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1031 wxWindow* wxpeer = GetWXPeer();
1032 wxpeer->GetUpdateRegion() = updateRgn;
1033 wxpeer->MacSetCGContextRef( context );
1035 bool handled = wxpeer->MacDoRedraw( 0 );
1037 CGContextRestoreGState( context );
1039 CGContextSaveGState( context );
1043 SEL _cmd = @selector(drawRect:);
1044 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1045 superimpl(slf, _cmd, *(NSRect*)rect);
1046 CGContextRestoreGState( context );
1047 CGContextSaveGState( context );
1049 wxpeer->MacPaintChildrenBorders();
1050 wxpeer->MacSetCGContextRef( NULL );
1051 CGContextRestoreGState( context );
1054 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1056 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1058 wxpeer->OSXHandleClicked(0);
1061 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1067 #if OBJC_API_VERSION >= 2
1069 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1070 class_addMethod(c, s, i, t );
1074 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1079 void wxOSXCocoaClassAddWXMethods(Class c)
1082 #if OBJC_API_VERSION < 2
1083 static objc_method wxmethods[] =
1087 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1088 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1089 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1091 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1092 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1093 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1095 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1097 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1098 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1099 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1101 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1102 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1103 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1105 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1106 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1107 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1109 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1111 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1113 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1114 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1115 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1116 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1118 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1119 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1121 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1122 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1124 #if wxUSE_DRAG_AND_DROP
1125 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1126 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1127 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1128 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1131 #if OBJC_API_VERSION < 2
1133 static int method_count = WXSIZEOF( wxmethods );
1134 static objc_method_list *wxmethodlist = NULL;
1135 if ( wxmethodlist == NULL )
1137 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1138 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1139 wxmethodlist->method_count = method_count;
1140 wxmethodlist->obsolete = 0;
1142 class_addMethods( c, wxmethodlist );
1147 // C++ implementation class
1150 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1152 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1153 wxWidgetImpl( peer, isRootControl )
1158 // check if the user wants to create the control initially hidden
1159 if ( !peer->IsShown() )
1160 SetVisibility(false);
1162 // gc aware handling
1164 CFRetain(m_osxView);
1165 [m_osxView release];
1168 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1173 void wxWidgetCocoaImpl::Init()
1177 m_lastKeyDownEvent = NULL;
1178 m_hasEditor = false;
1181 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1183 StopAnimation(m_wxPeer);
1185 RemoveAssociations( this );
1187 if ( !IsRootControl() )
1189 NSView *sv = [m_osxView superview];
1191 [m_osxView removeFromSuperview];
1193 // gc aware handling
1195 CFRelease(m_osxView);
1198 bool wxWidgetCocoaImpl::IsVisible() const
1200 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1203 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1205 [m_osxView setHidden:(visible ? NO:YES)];
1208 // ----------------------------------------------------------------------------
1209 // window animation stuff
1210 // ----------------------------------------------------------------------------
1215 WX_DECLARE_VOIDPTR_HASH_MAP(NSViewAnimation *, wxNSViewAnimations);
1217 // all currently active animations
1219 // this is MT-safe because windows can only be animated from the main
1221 wxNSViewAnimations gs_activeAnimations;
1223 void StopAnimation(wxWindow *win)
1225 wxNSViewAnimations::iterator it = gs_activeAnimations.find(win);
1226 if ( it != gs_activeAnimations.end() )
1228 [it->second stopAnimation];
1232 } // anonymous namespace
1234 // define a delegate used to detect the end of the window animation
1235 @interface wxNSAnimationDelegate : NSObject
1236 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1237 <NSAnimationDelegate>
1240 // can't use wxRect here as it has a user-defined ctor and so can't be used
1241 // as an Objective-C field
1250 - (id)initWithWindow:(wxWindow *)win show:(bool)show;
1252 // NSAnimationDelegate methods
1253 - (void)animation:(NSAnimation*)animation
1254 didReachProgressMark:(NSAnimationProgress)progress;
1255 - (void)animationDidStop:(NSAnimation *)animation;
1256 - (void)animationDidEnd:(NSAnimation *)animation;
1259 - (void)finishAnimation:(NSAnimation *)animation;
1262 @implementation wxNSAnimationDelegate
1264 - (id)initWithWindow:(wxWindow *)win show:(bool)show
1272 m_win->GetPosition(&m_origRect.x, &m_origRect.y);
1273 m_win->GetSize(&m_origRect.w, &m_origRect.h);
1278 - (void)animation:(NSAnimation*)animation
1279 didReachProgressMark:(NSAnimationProgress)progress
1281 wxUnusedVar(animation);
1282 wxUnusedVar(progress);
1284 m_win->SendSizeEvent();
1287 - (void)animationDidStop:(NSAnimation *)animation
1289 [self finishAnimation:animation];
1292 - (void)animationDidEnd:(NSAnimation *)animation
1294 [self finishAnimation:animation];
1297 - (void)finishAnimation:(NSAnimation *)animation
1301 // the window expects to be sent a size event when it is shown normally
1302 // and so it should also get one when it is shown with effect
1303 m_win->SendSizeEvent();
1305 else // window was being hidden
1307 // NSViewAnimation is smart enough to hide the NSView itself but we
1308 // also need to ensure that it's considered to be hidden at wx level
1311 // and we also need to restore its original size which was changed by
1313 m_win->SetSize(m_origRect.x, m_origRect.y, m_origRect.w, m_origRect.h);
1316 wxNSViewAnimations::iterator it = gs_activeAnimations.find(m_win);
1317 wxASSERT_MSG( it != gs_activeAnimations.end() && it->second == animation,
1318 "corrupted active animations list?" );
1320 gs_activeAnimations.erase(it);
1322 // we don't dare to release it immediately as we're called from its code
1323 // but schedule the animation itself for deletion soon
1324 [animation autorelease];
1326 // ensure that this delegate is definitely not needed any more before
1328 [animation setDelegate:nil];
1336 wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1338 wxShowEffect effect,
1341 // first of all, check if this window is not being already animated and
1342 // cancel the previous animation if it is as performing more than one
1343 // animation on the same window at the same time results in some really
1344 // unexpected effects
1348 // create the dictionary describing the animation to perform on this view
1350 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1351 NSMutableDictionary * const
1352 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1353 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1355 // determine the start and end rectangles assuming we're hiding the window
1359 rectEnd = 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
1450 // notice that it will be released when it is removed from
1451 // gs_activeAnimations
1452 NSViewAnimation * const
1453 anim = [[NSViewAnimation alloc]
1454 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1455 gs_activeAnimations[win] = anim;
1459 // what is a good default duration? Windows uses 200ms, Web frameworks
1460 // use anything from 250ms to 1s... choose something in the middle
1464 [anim setDuration:timeout/1000.]; // duration is in seconds here
1466 // if the window being animated changes its layout depending on its size
1467 // (which is almost always the case) we need to redo it during animation
1469 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1470 // controls in wxInfoBar visibly jump around)
1471 const int NUM_LAYOUTS = 20;
1472 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1473 [anim addProgressMark:f];
1475 [anim setDelegate:[[wxNSAnimationDelegate alloc] initWithWindow:win show:show]];
1476 [anim startAnimation];
1481 bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1482 wxShowEffect effect,
1485 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1488 void wxWidgetCocoaImpl::Raise()
1493 void wxWidgetCocoaImpl::Lower()
1498 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1503 // We should do something like this, but it wasn't working in 10.4.
1504 if (GetNeedsDisplay() )
1508 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1509 NSSize offset = NSMakeSize((float)dx, (float)dy);
1510 [m_osxView scrollRect:r by:offset];
1514 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1516 wxWindowMac* parent = GetWXPeer()->GetParent();
1517 // under Cocoa we might have a contentView in the wxParent to which we have to
1518 // adjust the coordinates
1519 if (parent && [m_osxView superview] != parent->GetHandle() )
1521 int cx = 0,cy = 0,cw = 0,ch = 0;
1522 if ( parent->GetPeer() )
1524 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1529 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1530 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1531 [m_osxView setFrame:r];
1532 [[m_osxView superview] setNeedsDisplayInRect:r];
1534 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1536 if ( [(wxNSView*)m_osxView trackingTag] )
1537 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1539 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1543 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1545 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1550 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1552 NSRect rect = [m_osxView frame];
1553 width = (int)rect.size.width;
1554 height = (int)rect.size.height;
1557 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1559 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1561 NSView* cv = [m_osxView contentView];
1563 NSRect bounds = [m_osxView bounds];
1564 NSRect rect = [cv frame];
1566 int y = (int)rect.origin.y;
1567 int x = (int)rect.origin.x;
1568 if ( ![ m_osxView isFlipped ] )
1569 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1572 width = (int)rect.size.width;
1573 height = (int)rect.size.height;
1578 GetSize( width, height );
1582 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1585 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1587 [m_osxView setNeedsDisplay:YES];
1590 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1592 return [m_osxView needsDisplay];
1595 bool wxWidgetCocoaImpl::CanFocus() const
1597 return [m_osxView canBecomeKeyView] == YES;
1600 bool wxWidgetCocoaImpl::HasFocus() const
1602 return ( FindFocus() == m_osxView );
1605 bool wxWidgetCocoaImpl::SetFocus()
1607 if ( [m_osxView canBecomeKeyView] == NO )
1610 [[m_osxView window] makeFirstResponder: m_osxView] ;
1611 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1616 void wxWidgetCocoaImpl::RemoveFromParent()
1618 [m_osxView removeFromSuperview];
1621 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1623 NSView* container = parent->GetWXWidget() ;
1624 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1625 [container addSubview:m_osxView];
1628 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1630 NSView* targetView = m_osxView;
1631 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1632 targetView = [(NSScrollView*) m_osxView documentView];
1634 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1636 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1637 green:(CGFloat) (col.Green() / 255.0)
1638 blue:(CGFloat) (col.Blue() / 255.0)
1639 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1643 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1645 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1647 wxCFStringRef cf( title , encoding );
1648 [m_osxView setTitle:cf.AsNSString()];
1650 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1652 wxCFStringRef cf( title , encoding );
1653 [m_osxView setStringValue:cf.AsNSString()];
1658 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1660 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1661 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1662 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1665 wxInt32 wxWidgetCocoaImpl::GetValue() const
1667 return [(NSControl*)m_osxView intValue];
1670 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1672 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1674 [m_osxView setIntValue:v];
1676 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1678 [m_osxView setFloatValue:(double)v];
1680 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1682 [m_osxView setDoubleValue:(double)v];
1686 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1688 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1690 [m_osxView setMinValue:(double)v];
1694 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1696 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1698 [m_osxView setMaxValue:(double)v];
1702 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1704 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1706 return (int)[m_osxView minValue];
1711 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1713 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1715 return (int)[m_osxView maxValue];
1720 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1724 // TODO: how to create a wxBitmap from NSImage?
1726 if ( [m_osxView respondsToSelector:@selector(image:)] )
1727 bmp = [m_osxView image];
1733 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1735 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1737 [m_osxView setImage:bitmap.GetNSImage()];
1741 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1743 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1745 NSCellImagePosition pos;
1765 wxFAIL_MSG( "invalid image position" );
1769 [m_osxView setImagePosition:pos];
1773 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1775 // implementation in subclass
1778 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1780 r->x = r->y = r->width = r->height = 0;
1782 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1784 NSRect former = [m_osxView frame];
1785 [m_osxView sizeToFit];
1786 NSRect best = [m_osxView frame];
1787 [m_osxView setFrame:former];
1788 r->width = (int)best.size.width;
1789 r->height = (int)best.size.height;
1793 bool wxWidgetCocoaImpl::IsEnabled() const
1795 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1796 return [m_osxView isEnabled];
1800 void wxWidgetCocoaImpl::Enable( bool enable )
1802 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1803 [m_osxView setEnabled:enable];
1806 void wxWidgetCocoaImpl::PulseGauge()
1810 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1814 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1816 NSControlSize size = NSRegularControlSize;
1820 case wxWINDOW_VARIANT_NORMAL :
1821 size = NSRegularControlSize;
1824 case wxWINDOW_VARIANT_SMALL :
1825 size = NSSmallControlSize;
1828 case wxWINDOW_VARIANT_MINI :
1829 size = NSMiniControlSize;
1832 case wxWINDOW_VARIANT_LARGE :
1833 size = NSRegularControlSize;
1837 wxFAIL_MSG(wxT("unexpected window variant"));
1840 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1841 [m_osxView setControlSize:size];
1842 else if ([m_osxView respondsToSelector:@selector(cell)])
1844 id cell = [(id)m_osxView cell];
1845 if ([cell respondsToSelector:@selector(setControlSize:)])
1846 [cell setControlSize:size];
1850 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1852 if ([m_osxView respondsToSelector:@selector(setFont:)])
1853 [m_osxView setFont: font.OSXGetNSFont()];
1856 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1858 WXWidget c = control ? control : (WXWidget) m_osxView;
1859 wxWidgetImpl::Associate( c, this ) ;
1860 if ([c respondsToSelector:@selector(setAction:)])
1863 [c setAction: @selector(controlAction:)];
1864 if ([c respondsToSelector:@selector(setDoubleAction:)])
1866 [c setDoubleAction: @selector(controlDoubleAction:)];
1872 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1874 wxKeyEvent wxevent(wxEVT_CHAR);
1875 SetupKeyEvent( wxevent, event, text );
1877 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1880 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1882 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1883 SetupKeyEvent( wxevent, event );
1884 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1886 // this will fire higher level events, like insertText, to help
1887 // us handle EVT_CHAR, etc.
1889 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1893 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1894 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1896 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1904 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1906 NSPoint clickLocation;
1907 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1908 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1909 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1910 SetupMouseEvent(wxevent , event) ;
1914 return GetWXPeer()->HandleWindowEvent(wxevent);
1917 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1919 wxWindow* thisWindow = GetWXPeer();
1920 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1922 thisWindow->MacInvalidateBorders();
1925 if ( receivedFocus )
1927 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1928 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1929 thisWindow->HandleWindowEvent(eventFocus);
1932 if ( thisWindow->GetCaret() )
1933 thisWindow->GetCaret()->OnSetFocus();
1936 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1937 event.SetEventObject(thisWindow);
1939 event.SetWindow(otherWindow->GetWXPeer());
1940 thisWindow->HandleWindowEvent(event) ;
1942 else // !receivedFocuss
1945 if ( thisWindow->GetCaret() )
1946 thisWindow->GetCaret()->OnKillFocus();
1949 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1951 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1952 event.SetEventObject(thisWindow);
1954 event.SetWindow(otherWindow->GetWXPeer());
1955 thisWindow->HandleWindowEvent(event) ;
1959 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1961 NSPoint location = [NSEvent mouseLocation];
1962 location = [[m_osxView window] convertScreenToBase:location];
1963 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1965 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1967 [(NSCursor*)cursor.GetHCURSOR() set];
1969 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1972 void wxWidgetCocoaImpl::CaptureMouse()
1974 [[m_osxView window] disableCursorRects];
1977 void wxWidgetCocoaImpl::ReleaseMouse()
1979 [[m_osxView window] enableCursorRects];
1982 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1984 m_isFlipped = flipped;
1991 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1992 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1993 long WXUNUSED(style), long WXUNUSED(extraStyle))
1995 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1996 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1998 // temporary hook for dnd
1999 [v registerForDraggedTypes:[NSArray arrayWithObjects:
2000 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
2002 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
2006 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
2008 NSWindow* tlw = now->GetWXWindow();
2009 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
2010 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
2011 c->InstallEventHandler();
2012 [tlw setContentView:v];