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/evtloop.h"
30 #if wxUSE_DRAG_AND_DROP
34 #include <objc/objc-runtime.h>
36 // Get the window with the focus
38 NSView* GetViewFromResponder( NSResponder* responder )
41 if ( [responder isKindOfClass:[NSTextView class]] )
43 NSView* delegate = [(NSTextView*)responder delegate];
44 if ( [delegate isKindOfClass:[NSTextField class] ] )
47 view = (NSView*) responder;
51 if ( [responder isKindOfClass:[NSView class]] )
52 view = (NSView*) responder;
57 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
59 NSView* focusedView = nil;
60 if ( keyWindow != nil )
61 focusedView = GetViewFromResponder([keyWindow firstResponder]);
66 WXWidget wxWidgetImpl::FindFocus()
68 return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
71 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
75 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
76 wxRect bounds(x,y,w,h);
77 NSView* sv = (window->GetParent()->GetHandle() );
79 return wxToNSRect( sv, bounds );
82 @interface wxNSView : NSView
84 NSTrackingRectTag rectTag;
87 // the tracking tag is needed to track mouse enter / exit events
88 - (void) setTrackingTag: (NSTrackingRectTag)tag;
89 - (NSTrackingRectTag) trackingTag;
92 @interface NSView(PossibleMethods)
93 - (void)setTitle:(NSString *)aString;
94 - (void)setStringValue:(NSString *)aString;
95 - (void)setIntValue:(int)anInt;
96 - (void)setFloatValue:(float)aFloat;
97 - (void)setDoubleValue:(double)aDouble;
101 - (void)setMinValue:(double)aDouble;
102 - (void)setMaxValue:(double)aDouble;
107 - (void)setEnabled:(BOOL)flag;
109 - (void)setImage:(NSImage *)image;
110 - (void)setControlSize:(NSControlSize)size;
112 - (void)setFont:(NSFont *)fontObject;
116 - (void)setTarget:(id)anObject;
117 - (void)setAction:(SEL)aSelector;
118 - (void)setDoubleAction:(SEL)aSelector;
119 - (void)setBackgroundColor:(NSColor*)aColor;
120 - (void)setImagePosition:(NSCellImagePosition)aPosition;
123 long wxOSXTranslateCocoaKey( NSEvent* event )
127 if ([event type] != NSFlagsChanged)
129 NSString* s = [event charactersIgnoringModifiers];
130 // backspace char reports as delete w/modifiers for some reason
133 switch ( [s characterAtIndex:0] )
140 case NSUpArrowFunctionKey :
143 case NSDownArrowFunctionKey :
146 case NSLeftArrowFunctionKey :
149 case NSRightArrowFunctionKey :
152 case NSInsertFunctionKey :
155 case NSDeleteFunctionKey :
158 case NSHomeFunctionKey :
161 // case NSBeginFunctionKey :
162 // retval = WXK_BEGIN;
164 case NSEndFunctionKey :
167 case NSPageUpFunctionKey :
170 case NSPageDownFunctionKey :
171 retval = WXK_PAGEDOWN;
173 case NSHelpFunctionKey :
177 int intchar = [s characterAtIndex: 0];
178 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
179 retval = WXK_F1 + (intchar - NSF1FunctionKey );
185 // Some keys don't seem to have constants. The code mimics the approach
186 // taken by WebKit. See:
187 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
188 switch( [event keyCode] )
193 retval = WXK_COMMAND;
197 retval = WXK_CAPITAL;
200 case 56: // Left Shift
201 case 60: // Right Shift
206 case 61: // Right Alt
210 case 59: // Left Ctrl
211 case 62: // Right Ctrl
212 retval = WXK_CONTROL;
224 retval = WXK_NUMPAD_DIVIDE;
227 retval = WXK_NUMPAD_MULTIPLY;
230 retval = WXK_NUMPAD_SUBTRACT;
233 retval = WXK_NUMPAD_ADD;
236 retval = WXK_NUMPAD_ENTER;
239 retval = WXK_NUMPAD_DECIMAL;
242 retval = WXK_NUMPAD0;
245 retval = WXK_NUMPAD1;
248 retval = WXK_NUMPAD2;
251 retval = WXK_NUMPAD3;
254 retval = WXK_NUMPAD4;
257 retval = WXK_NUMPAD5;
260 retval = WXK_NUMPAD6;
263 retval = WXK_NUMPAD7;
266 retval = WXK_NUMPAD8;
269 retval = WXK_NUMPAD9;
272 //retval = [event keyCode];
278 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
280 UInt32 modifiers = [nsEvent modifierFlags] ;
281 int eventType = [nsEvent type];
283 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
284 wxevent.m_controlDown = modifiers & NSControlKeyMask;
285 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
286 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
288 wxevent.m_rawCode = [nsEvent keyCode];
289 wxevent.m_rawFlags = modifiers;
291 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
294 if ( eventType != NSFlagsChanged )
296 NSString* nschars = (wxevent.GetEventType() != wxEVT_CHAR) ? [nsEvent charactersIgnoringModifiers] : [nsEvent characters];
299 // if charString is set, it did not come from key up / key down
300 wxevent.SetEventType( wxEVT_CHAR );
301 chars = wxCFStringRef::AsString(charString);
305 chars = wxCFStringRef::AsString(nschars);
309 int aunichar = chars.Length() > 0 ? chars[0] : 0;
312 if (wxevent.GetEventType() != wxEVT_CHAR)
314 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
318 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
321 wxevent.SetEventType( wxEVT_KEY_UP ) ;
323 case NSFlagsChanged :
327 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
330 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
333 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
336 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
347 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
348 keyval = wxToupper( aunichar ) ;
354 wxevent.m_uniChar = aunichar;
356 wxevent.m_keyCode = keyval;
358 wxWindowMac* peer = GetWXPeer();
361 wxevent.SetEventObject(peer);
362 wxevent.SetId(peer->GetId()) ;
366 UInt32 g_lastButton = 0 ;
367 bool g_lastButtonWasFakeRight = false ;
369 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
371 int eventType = [nsEvent type];
372 UInt32 modifiers = [nsEvent modifierFlags] ;
373 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
375 // these parameters are not given for all events
376 UInt32 button = [nsEvent buttonNumber];
377 UInt32 clickCount = 0;
379 wxevent.m_x = screenMouseLocation.x;
380 wxevent.m_y = screenMouseLocation.y;
381 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
382 wxevent.m_controlDown = modifiers & NSControlKeyMask;
383 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
384 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
385 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
387 UInt32 mouseChord = 0;
391 case NSLeftMouseDown :
392 case NSLeftMouseDragged :
395 case NSRightMouseDown :
396 case NSRightMouseDragged :
399 case NSOtherMouseDown :
400 case NSOtherMouseDragged :
405 // a control click is interpreted as a right click
406 bool thisButtonIsFakeRight = false ;
407 if ( button == 0 && (modifiers & NSControlKeyMask) )
410 thisButtonIsFakeRight = true ;
413 // otherwise we report double clicks by connecting a left click with a ctrl-left click
414 if ( clickCount > 1 && button != g_lastButton )
417 // we must make sure that our synthetic 'right' button corresponds in
418 // mouse down, moved and mouse up, and does not deliver a right down and left up
421 case NSLeftMouseDown :
422 case NSRightMouseDown :
423 case NSOtherMouseDown :
424 g_lastButton = button ;
425 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
432 g_lastButtonWasFakeRight = false ;
434 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
435 button = g_lastButton ;
437 // Adjust the chord mask to remove the primary button and add the
438 // secondary button. It is possible that the secondary button is
439 // already pressed, e.g. on a mouse connected to a laptop, but this
440 // possibility is ignored here:
441 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
442 mouseChord = ((mouseChord & ~1U) | 2U);
445 wxevent.m_leftDown = true ;
447 wxevent.m_rightDown = true ;
449 wxevent.m_middleDown = true ;
451 // translate into wx types
454 case NSLeftMouseDown :
455 case NSRightMouseDown :
456 case NSOtherMouseDown :
457 clickCount = [nsEvent clickCount];
461 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
465 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
469 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
478 case NSRightMouseUp :
479 case NSOtherMouseUp :
480 clickCount = [nsEvent clickCount];
484 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
488 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
492 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
502 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
503 wxevent.m_wheelDelta = 10;
504 wxevent.m_linesPerAction = 1;
506 if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
508 wxevent.m_wheelAxis = 1;
509 wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
513 wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
518 case NSMouseEntered :
519 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
522 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
524 case NSLeftMouseDragged :
525 case NSRightMouseDragged :
526 case NSOtherMouseDragged :
528 wxevent.SetEventType( wxEVT_MOTION ) ;
534 wxevent.m_clickCount = clickCount;
535 wxWindowMac* peer = GetWXPeer();
538 wxevent.SetEventObject(peer);
539 wxevent.SetId(peer->GetId()) ;
543 @implementation wxNSView
547 static BOOL initialized = NO;
551 wxOSXCocoaClassAddWXMethods( self );
555 - (void) setTrackingTag: (NSTrackingRectTag)tag
560 - (NSTrackingRectTag) trackingTag
571 #if wxUSE_DRAG_AND_DROP
573 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
574 // for details on the NSPasteboard -> PasteboardRef conversion
576 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
578 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
580 return NSDragOperationNone;
582 return impl->draggingEntered(sender, self, _cmd);
585 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
587 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
591 return impl->draggingExited(sender, self, _cmd);
594 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
596 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
598 return NSDragOperationNone;
600 return impl->draggingUpdated(sender, self, _cmd);
603 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
605 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
607 return NSDragOperationNone;
609 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
614 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
616 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
620 impl->mouseEvent(event, self, _cmd);
623 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
625 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
629 impl->keyEvent(event, self, _cmd);
632 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
634 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
638 impl->insertText(text, self, _cmd);
641 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
643 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
647 return impl->performKeyEquivalent(event, self, _cmd);
650 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
652 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
656 return impl->acceptsFirstResponder(self, _cmd);
659 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
661 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
665 return impl->becomeFirstResponder(self, _cmd);
668 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
670 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
674 return impl->resignFirstResponder(self, _cmd);
677 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
679 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
683 impl->resetCursorRects(self, _cmd);
686 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
688 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
692 return impl->isFlipped(self, _cmd) ? YES:NO;
695 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
697 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
701 return impl->drawRect(&rect, self, _cmd);
704 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
706 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
710 impl->controlAction(self, _cmd, sender);
713 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
715 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
719 impl->controlDoubleAction(self, _cmd, sender);
722 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
724 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
725 NSPasteboard *pboard = [sender draggingPasteboard];
726 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
728 wxWindow* wxpeer = GetWXPeer();
729 if ( wxpeer == NULL )
730 return NSDragOperationNone;
732 wxDropTarget* target = wxpeer->GetDropTarget();
733 if ( target == NULL )
734 return NSDragOperationNone;
736 wxDragResult result = wxDragNone;
737 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
738 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
740 if ( sourceDragMask & NSDragOperationLink )
742 else if ( sourceDragMask & NSDragOperationCopy )
744 else if ( sourceDragMask & NSDragOperationMove )
747 PasteboardRef pboardRef;
748 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
749 target->SetCurrentDragPasteboard(pboardRef);
750 result = target->OnEnter(pt.x, pt.y, result);
751 CFRelease(pboardRef);
753 NSDragOperation nsresult = NSDragOperationNone;
757 nsresult = NSDragOperationLink;
759 nsresult = NSDragOperationMove;
761 nsresult = NSDragOperationCopy;
768 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
770 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
771 NSPasteboard *pboard = [sender draggingPasteboard];
773 wxWindow* wxpeer = GetWXPeer();
774 if ( wxpeer == NULL )
777 wxDropTarget* target = wxpeer->GetDropTarget();
778 if ( target == NULL )
781 PasteboardRef pboardRef;
782 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
783 target->SetCurrentDragPasteboard(pboardRef);
785 CFRelease(pboardRef);
788 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
790 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
791 NSPasteboard *pboard = [sender draggingPasteboard];
792 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
794 wxWindow* wxpeer = GetWXPeer();
795 if ( wxpeer == NULL )
796 return NSDragOperationNone;
798 wxDropTarget* target = wxpeer->GetDropTarget();
799 if ( target == NULL )
800 return NSDragOperationNone;
802 wxDragResult result = wxDragNone;
803 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
804 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
806 if ( sourceDragMask & NSDragOperationLink )
808 else if ( sourceDragMask & NSDragOperationCopy )
810 else if ( sourceDragMask & NSDragOperationMove )
813 PasteboardRef pboardRef;
814 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
815 target->SetCurrentDragPasteboard(pboardRef);
816 result = target->OnDragOver(pt.x, pt.y, result);
817 CFRelease(pboardRef);
819 NSDragOperation nsresult = NSDragOperationNone;
823 nsresult = NSDragOperationLink;
825 nsresult = NSDragOperationMove;
827 nsresult = NSDragOperationCopy;
834 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
836 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
838 NSPasteboard *pboard = [sender draggingPasteboard];
839 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
841 wxWindow* wxpeer = GetWXPeer();
842 wxDropTarget* target = wxpeer->GetDropTarget();
843 wxDragResult result = wxDragNone;
844 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
845 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
847 if ( sourceDragMask & NSDragOperationLink )
849 else if ( sourceDragMask & NSDragOperationCopy )
851 else if ( sourceDragMask & NSDragOperationMove )
854 PasteboardRef pboardRef;
855 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
856 target->SetCurrentDragPasteboard(pboardRef);
858 if (target->OnDrop(pt.x, pt.y))
859 result = target->OnData(pt.x, pt.y, result);
861 CFRelease(pboardRef);
863 return result != wxDragNone;
866 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
867 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
868 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
869 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
870 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
871 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
873 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
875 if ( !DoHandleMouseEvent(event) )
877 // for plain NSView mouse events would propagate to parents otherwise
878 if (!m_wxPeer->MacIsUserPane())
880 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
881 superimpl(slf, (SEL)_cmd, event);
886 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
888 if ( [event type] == NSKeyDown )
889 m_lastKeyDownEvent = event;
890 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
892 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
893 superimpl(slf, (SEL)_cmd, event);
895 m_lastKeyDownEvent = NULL;
898 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
900 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
902 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
903 superimpl(slf, (SEL)_cmd, text);
908 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
910 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
911 return superimpl(slf, (SEL)_cmd, event);
914 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
916 if ( m_wxPeer->MacIsUserPane() )
917 return m_wxPeer->AcceptsFocus();
920 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
921 return superimpl(slf, (SEL)_cmd);
925 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
927 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
928 // get the current focus before running becomeFirstResponder
929 NSView* otherView = FindFocus();
931 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
932 BOOL r = superimpl(slf, (SEL)_cmd);
935 DoNotifyFocusEvent( true, otherWindow );
941 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
943 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
944 BOOL r = superimpl(slf, (SEL)_cmd);
945 // get the current focus after running resignFirstResponder
946 // note that this value isn't reliable, it might return the same view that
948 NSView* otherView = FindFocus();
949 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
950 // NSTextViews have an editor as true responder, therefore the might get the
951 // resign notification if their editor takes over, don't trigger any event then
952 if ( r && !m_hasEditor)
954 DoNotifyFocusEvent( false, otherWindow );
959 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
961 wxWindow* wxpeer = GetWXPeer();
964 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
967 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
968 superimpl(slf, (SEL)_cmd);
972 [slf addCursorRect: [slf bounds]
978 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
984 #define OSX_DEBUG_DRAWING 0
986 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
988 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
989 CGContextSaveGState( context );
991 #if OSX_DEBUG_DRAWING
992 CGContextBeginPath( context );
993 CGContextMoveToPoint(context, 0, 0);
994 NSRect bounds = [self bounds];
995 CGContextAddLineToPoint(context, 10, 0);
996 CGContextMoveToPoint(context, 0, 0);
997 CGContextAddLineToPoint(context, 0, 10);
998 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
999 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1000 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1001 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1002 CGContextClosePath( context );
1003 CGContextStrokePath(context);
1008 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1009 CGContextScaleCTM( context, 1, -1 );
1013 const NSRect *rects;
1016 [slf getRectsBeingDrawn:&rects count:&count];
1017 for ( int i = 0 ; i < count ; ++i )
1019 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1022 wxWindow* wxpeer = GetWXPeer();
1023 wxpeer->GetUpdateRegion() = updateRgn;
1024 wxpeer->MacSetCGContextRef( context );
1026 bool handled = wxpeer->MacDoRedraw( 0 );
1028 CGContextRestoreGState( context );
1030 CGContextSaveGState( context );
1034 SEL _cmd = @selector(drawRect:);
1035 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1036 superimpl(slf, _cmd, *(NSRect*)rect);
1037 CGContextRestoreGState( context );
1038 CGContextSaveGState( context );
1040 wxpeer->MacPaintChildrenBorders();
1041 wxpeer->MacSetCGContextRef( NULL );
1042 CGContextRestoreGState( context );
1045 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1047 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1049 wxpeer->OSXHandleClicked(0);
1052 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1058 #if OBJC_API_VERSION >= 2
1060 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1061 class_addMethod(c, s, i, t );
1065 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1070 void wxOSXCocoaClassAddWXMethods(Class c)
1073 #if OBJC_API_VERSION < 2
1074 static objc_method wxmethods[] =
1078 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1079 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1080 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1082 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1083 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1084 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1086 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1088 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1089 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1090 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1092 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1093 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1094 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1096 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1097 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1098 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1100 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1102 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1104 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1105 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1106 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1107 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1109 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1110 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1112 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1113 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1115 #if wxUSE_DRAG_AND_DROP
1116 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1117 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1118 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1119 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1122 #if OBJC_API_VERSION < 2
1124 static int method_count = WXSIZEOF( wxmethods );
1125 static objc_method_list *wxmethodlist = NULL;
1126 if ( wxmethodlist == NULL )
1128 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1129 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1130 wxmethodlist->method_count = method_count;
1131 wxmethodlist->obsolete = 0;
1133 class_addMethods( c, wxmethodlist );
1138 // C++ implementation class
1141 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1143 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1144 wxWidgetImpl( peer, isRootControl )
1149 // check if the user wants to create the control initially hidden
1150 if ( !peer->IsShown() )
1151 SetVisibility(false);
1153 // gc aware handling
1155 CFRetain(m_osxView);
1156 [m_osxView release];
1159 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1164 void wxWidgetCocoaImpl::Init()
1168 m_lastKeyDownEvent = NULL;
1169 m_hasEditor = false;
1172 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1174 RemoveAssociations( this );
1176 if ( !IsRootControl() )
1178 NSView *sv = [m_osxView superview];
1180 [m_osxView removeFromSuperview];
1182 // gc aware handling
1184 CFRelease(m_osxView);
1187 bool wxWidgetCocoaImpl::IsVisible() const
1189 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1192 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1194 [m_osxView setHidden:(visible ? NO:YES)];
1197 // ----------------------------------------------------------------------------
1198 // window animation stuff
1199 // ----------------------------------------------------------------------------
1201 // define a delegate used to refresh the window during animation
1202 @interface wxNSAnimationDelegate : NSObject
1203 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1204 <NSAnimationDelegate>
1211 - (id)init:(wxWindow *)win;
1215 // NSAnimationDelegate methods
1216 - (void)animationDidEnd:(NSAnimation*)animation;
1217 - (void)animation:(NSAnimation*)animation
1218 didReachProgressMark:(NSAnimationProgress)progress;
1221 @implementation wxNSAnimationDelegate
1223 - (id)init:(wxWindow *)win
1238 - (void)animation:(NSAnimation*)animation
1239 didReachProgressMark:(NSAnimationProgress)progress
1241 wxUnusedVar(animation);
1242 wxUnusedVar(progress);
1244 m_win->SendSizeEvent();
1247 - (void)animationDidEnd:(NSAnimation*)animation
1256 wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1258 wxShowEffect effect,
1261 // create the dictionary describing the animation to perform on this view
1263 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1264 NSMutableDictionary * const
1265 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1266 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1268 // determine the start and end rectangles assuming we're hiding the window
1269 const wxRect rectOrig = win->GetRect();
1277 if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1278 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1279 effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1280 else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1281 effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1282 effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1283 else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1284 effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1285 effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1286 else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1287 effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1288 effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1293 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1294 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1298 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1299 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1300 rectEnd.x = rectStart.GetRight();
1304 case wxSHOW_EFFECT_ROLL_TO_TOP:
1305 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1309 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1310 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1311 rectEnd.y = rectStart.GetBottom();
1315 case wxSHOW_EFFECT_EXPAND:
1316 rectEnd.x = rectStart.x + rectStart.width / 2;
1317 rectEnd.y = rectStart.y + rectStart.height / 2;
1322 case wxSHOW_EFFECT_BLEND:
1323 [dict setObject:(show ? NSViewAnimationFadeInEffect
1324 : NSViewAnimationFadeOutEffect)
1325 forKey:NSViewAnimationEffectKey];
1328 case wxSHOW_EFFECT_NONE:
1329 case wxSHOW_EFFECT_MAX:
1330 wxFAIL_MSG( "unexpected animation effect" );
1334 wxFAIL_MSG( "unknown animation effect" );
1340 // we need to restore it to the original rectangle instead of making it
1342 wxSwap(rectStart, rectEnd);
1344 // and as the window is currently hidden, we need to show it for the
1345 // animation to be visible at all (but don't restore it at its full
1346 // rectangle as it shouldn't appear immediately)
1347 win->SetSize(rectStart);
1351 NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1352 ? [(NSView *)viewOrWin superview]
1354 const NSRect rStart = wxToNSRect(parentView, rectStart);
1355 const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1357 [dict setObject:[NSValue valueWithRect:rStart]
1358 forKey:NSViewAnimationStartFrameKey];
1359 [dict setObject:[NSValue valueWithRect:rEnd]
1360 forKey:NSViewAnimationEndFrameKey];
1362 // create an animation using the values in the above dictionary
1363 NSViewAnimation * const
1364 anim = [[NSViewAnimation alloc]
1365 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1369 // what is a good default duration? Windows uses 200ms, Web frameworks
1370 // use anything from 250ms to 1s... choose something in the middle
1374 [anim setDuration:timeout/1000.]; // duration is in seconds here
1376 // if the window being animated changes its layout depending on its size
1377 // (which is almost always the case) we need to redo it during animation
1379 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1380 // controls in wxInfoBar visibly jump around)
1381 const int NUM_LAYOUTS = 20;
1382 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1383 [anim addProgressMark:f];
1385 wxNSAnimationDelegate * const
1386 animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1387 [anim setDelegate:animDelegate];
1388 [anim startAnimation];
1390 // Cocoa is capable of doing animation asynchronously or even from separate
1391 // thread but wx API doesn't provide any way to be notified about the
1392 // animation end and without this we really must ensure that the window has
1393 // the expected (i.e. the same as if a simple Show() had been used) size
1394 // when we return, so block here until the animation finishes
1396 // notice that because the default animation mode is NSAnimationBlocking,
1397 // no user input events ought to be processed from here
1399 wxEventLoopGuarantor ensureEventLoopExistence;
1400 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1401 while ( ![animDelegate isDone] )
1407 // NSViewAnimation is smart enough to hide the NSView being animated at
1408 // the end but we also must ensure that it's hidden for wx too
1411 // and we must also restore its size because it isn't expected to
1412 // change just because the window was hidden
1413 win->SetSize(rectOrig);
1417 // refresh it once again after the end to ensure that everything is in
1419 win->SendSizeEvent();
1422 [anim setDelegate:nil];
1423 [animDelegate release];
1429 bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1430 wxShowEffect effect,
1433 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1436 void wxWidgetCocoaImpl::Raise()
1441 void wxWidgetCocoaImpl::Lower()
1446 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1451 // We should do something like this, but it wasn't working in 10.4.
1452 if (GetNeedsDisplay() )
1456 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1457 NSSize offset = NSMakeSize((float)dx, (float)dy);
1458 [m_osxView scrollRect:r by:offset];
1462 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1464 wxWindowMac* parent = GetWXPeer()->GetParent();
1465 // under Cocoa we might have a contentView in the wxParent to which we have to
1466 // adjust the coordinates
1467 if (parent && [m_osxView superview] != parent->GetHandle() )
1469 int cx = 0,cy = 0,cw = 0,ch = 0;
1470 if ( parent->GetPeer() )
1472 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1477 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1478 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1479 [m_osxView setFrame:r];
1480 [[m_osxView superview] setNeedsDisplayInRect:r];
1482 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1484 if ( [(wxNSView*)m_osxView trackingTag] )
1485 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1487 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1491 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1493 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1498 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1500 NSRect rect = [m_osxView frame];
1501 width = (int)rect.size.width;
1502 height = (int)rect.size.height;
1505 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1507 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1509 NSView* cv = [m_osxView contentView];
1511 NSRect bounds = [m_osxView bounds];
1512 NSRect rect = [cv frame];
1514 int y = (int)rect.origin.y;
1515 int x = (int)rect.origin.x;
1516 if ( ![ m_osxView isFlipped ] )
1517 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1520 width = (int)rect.size.width;
1521 height = (int)rect.size.height;
1526 GetSize( width, height );
1530 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1533 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1535 [m_osxView setNeedsDisplay:YES];
1538 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1540 return [m_osxView needsDisplay];
1543 bool wxWidgetCocoaImpl::CanFocus() const
1545 return [m_osxView canBecomeKeyView] == YES;
1548 bool wxWidgetCocoaImpl::HasFocus() const
1550 return ( FindFocus() == m_osxView );
1553 bool wxWidgetCocoaImpl::SetFocus()
1555 if ( [m_osxView canBecomeKeyView] == NO )
1558 [[m_osxView window] makeFirstResponder: m_osxView] ;
1559 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1564 void wxWidgetCocoaImpl::RemoveFromParent()
1566 [m_osxView removeFromSuperview];
1569 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1571 NSView* container = parent->GetWXWidget() ;
1572 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1573 [container addSubview:m_osxView];
1576 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1578 NSView* targetView = m_osxView;
1579 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1580 targetView = [(NSScrollView*) m_osxView documentView];
1582 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1584 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1585 green:(CGFloat) (col.Green() / 255.0)
1586 blue:(CGFloat) (col.Blue() / 255.0)
1587 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1591 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1593 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1595 wxCFStringRef cf( title , encoding );
1596 [m_osxView setTitle:cf.AsNSString()];
1598 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1600 wxCFStringRef cf( title , encoding );
1601 [m_osxView setStringValue:cf.AsNSString()];
1606 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1608 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1609 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1610 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1613 wxInt32 wxWidgetCocoaImpl::GetValue() const
1615 return [(NSControl*)m_osxView intValue];
1618 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1620 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1622 [m_osxView setIntValue:v];
1624 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1626 [m_osxView setFloatValue:(double)v];
1628 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1630 [m_osxView setDoubleValue:(double)v];
1634 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1636 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1638 [m_osxView setMinValue:(double)v];
1642 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1644 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1646 [m_osxView setMaxValue:(double)v];
1650 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1652 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1654 return (int)[m_osxView minValue];
1659 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1661 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1663 return (int)[m_osxView maxValue];
1668 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1672 // TODO: how to create a wxBitmap from NSImage?
1674 if ( [m_osxView respondsToSelector:@selector(image:)] )
1675 bmp = [m_osxView image];
1681 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1683 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1685 [m_osxView setImage:bitmap.GetNSImage()];
1686 [m_osxView setNeedsDisplay:YES];
1690 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1692 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1694 NSCellImagePosition pos;
1714 wxFAIL_MSG( "invalid image position" );
1718 [m_osxView setImagePosition:pos];
1722 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1724 // implementation in subclass
1727 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1729 r->x = r->y = r->width = r->height = 0;
1731 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1733 NSRect former = [m_osxView frame];
1734 [m_osxView sizeToFit];
1735 NSRect best = [m_osxView frame];
1736 [m_osxView setFrame:former];
1737 r->width = (int)best.size.width;
1738 r->height = (int)best.size.height;
1742 bool wxWidgetCocoaImpl::IsEnabled() const
1744 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1745 return [m_osxView isEnabled];
1749 void wxWidgetCocoaImpl::Enable( bool enable )
1751 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1752 [m_osxView setEnabled:enable];
1755 void wxWidgetCocoaImpl::PulseGauge()
1759 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1763 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1765 NSControlSize size = NSRegularControlSize;
1769 case wxWINDOW_VARIANT_NORMAL :
1770 size = NSRegularControlSize;
1773 case wxWINDOW_VARIANT_SMALL :
1774 size = NSSmallControlSize;
1777 case wxWINDOW_VARIANT_MINI :
1778 size = NSMiniControlSize;
1781 case wxWINDOW_VARIANT_LARGE :
1782 size = NSRegularControlSize;
1786 wxFAIL_MSG(wxT("unexpected window variant"));
1789 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1790 [m_osxView setControlSize:size];
1791 else if ([m_osxView respondsToSelector:@selector(cell)])
1793 id cell = [(id)m_osxView cell];
1794 if ([cell respondsToSelector:@selector(setControlSize:)])
1795 [cell setControlSize:size];
1799 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1801 if ([m_osxView respondsToSelector:@selector(setFont:)])
1802 [m_osxView setFont: font.OSXGetNSFont()];
1805 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1807 WXWidget c = control ? control : (WXWidget) m_osxView;
1808 wxWidgetImpl::Associate( c, this ) ;
1809 if ([c respondsToSelector:@selector(setAction:)])
1812 [c setAction: @selector(controlAction:)];
1813 if ([c respondsToSelector:@selector(setDoubleAction:)])
1815 [c setDoubleAction: @selector(controlDoubleAction:)];
1821 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1823 wxKeyEvent wxevent(wxEVT_CHAR);
1824 SetupKeyEvent( wxevent, event, text );
1826 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1829 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1831 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1832 SetupKeyEvent( wxevent, event );
1833 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1835 // this will fire higher level events, like insertText, to help
1836 // us handle EVT_CHAR, etc.
1838 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1842 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1843 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1845 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1853 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1855 NSPoint clickLocation;
1856 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1857 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1858 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1859 SetupMouseEvent(wxevent , event) ;
1863 return GetWXPeer()->HandleWindowEvent(wxevent);
1866 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1868 wxWindow* thisWindow = GetWXPeer();
1869 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1871 thisWindow->MacInvalidateBorders();
1874 if ( receivedFocus )
1876 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1877 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1878 thisWindow->HandleWindowEvent(eventFocus);
1881 if ( thisWindow->GetCaret() )
1882 thisWindow->GetCaret()->OnSetFocus();
1885 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1886 event.SetEventObject(thisWindow);
1888 event.SetWindow(otherWindow->GetWXPeer());
1889 thisWindow->HandleWindowEvent(event) ;
1891 else // !receivedFocuss
1894 if ( thisWindow->GetCaret() )
1895 thisWindow->GetCaret()->OnKillFocus();
1898 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1900 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1901 event.SetEventObject(thisWindow);
1903 event.SetWindow(otherWindow->GetWXPeer());
1904 thisWindow->HandleWindowEvent(event) ;
1908 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1910 NSPoint location = [NSEvent mouseLocation];
1911 location = [[m_osxView window] convertScreenToBase:location];
1912 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1914 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1916 [(NSCursor*)cursor.GetHCURSOR() set];
1918 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1921 void wxWidgetCocoaImpl::CaptureMouse()
1923 [[m_osxView window] disableCursorRects];
1926 void wxWidgetCocoaImpl::ReleaseMouse()
1928 [[m_osxView window] enableCursorRects];
1931 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1933 m_isFlipped = flipped;
1940 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1941 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1942 long WXUNUSED(style), long WXUNUSED(extraStyle))
1944 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1945 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1947 // temporary hook for dnd
1948 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1949 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1951 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1955 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1957 NSWindow* tlw = now->GetWXWindow();
1958 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1959 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1960 c->InstallEventHandler();
1961 [tlw setContentView:v];