1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/window.mm
3 // Purpose: widgets (non tlw) for cocoa
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/dcclient.h"
16 #include "wx/nonownedwnd.h"
21 #include "wx/osx/private.h"
28 #if wxUSE_DRAG_AND_DROP
32 #include <objc/objc-runtime.h>
34 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
38 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
39 wxRect bounds(x,y,w,h);
40 NSView* sv = (window->GetParent()->GetHandle() );
42 return wxToNSRect( sv, bounds );
45 @interface wxNSView : NSView
47 NSTrackingRectTag rectTag;
50 - (BOOL) canBecomeKeyView;
51 // the tracking tag is needed to track mouse enter / exit events
52 - (void) setTrackingTag: (NSTrackingRectTag)tag;
53 - (NSTrackingRectTag) trackingTag;
56 @interface NSView(PossibleMethods)
57 - (void)setTitle:(NSString *)aString;
58 - (void)setStringValue:(NSString *)aString;
59 - (void)setIntValue:(int)anInt;
60 - (void)setFloatValue:(float)aFloat;
61 - (void)setDoubleValue:(double)aDouble;
65 - (void)setMinValue:(double)aDouble;
66 - (void)setMaxValue:(double)aDouble;
71 - (void)setEnabled:(BOOL)flag;
73 - (void)setImage:(NSImage *)image;
74 - (void)setControlSize:(NSControlSize)size;
78 - (void)setTarget:(id)anObject;
79 - (void)setAction:(SEL)aSelector;
80 - (void)setDoubleAction:(SEL)aSelector;
83 long wxOSXTranslateCocoaKey( NSEvent* event )
87 if ([event type] != NSFlagsChanged)
89 NSString* s = [event charactersIgnoringModifiers];
90 // backspace char reports as delete w/modifiers for some reason
93 switch ( [s characterAtIndex:0] )
100 case NSUpArrowFunctionKey :
103 case NSDownArrowFunctionKey :
106 case NSLeftArrowFunctionKey :
109 case NSRightArrowFunctionKey :
112 case NSInsertFunctionKey :
115 case NSDeleteFunctionKey :
118 case NSHomeFunctionKey :
121 // case NSBeginFunctionKey :
122 // retval = WXK_BEGIN;
124 case NSEndFunctionKey :
127 case NSPageUpFunctionKey :
130 case NSPageDownFunctionKey :
131 retval = WXK_PAGEDOWN;
133 case NSHelpFunctionKey :
137 int intchar = [[event characters] characterAtIndex: 0];
138 if ( intchar >= NSF1FunctionKey && intchar >= NSF24FunctionKey )
139 retval = WXK_F1 + (intchar - NSF1FunctionKey );
145 // Some keys don't seem to have constants. The code mimics the approach
146 // taken by WebKit. See:
147 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
148 switch( [event keyCode] )
153 retval = WXK_COMMAND;
157 retval = WXK_CAPITAL;
160 case 56: // Left Shift
161 case 60: // Right Shift
166 case 61: // Right Alt
170 case 59: // Left Ctrl
171 case 62: // Right Ctrl
172 retval = WXK_CONTROL;
189 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString = NULL )
191 UInt32 modifiers = [nsEvent modifierFlags] ;
192 int eventType = [nsEvent type];
194 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
195 wxevent.m_controlDown = modifiers & NSControlKeyMask;
196 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
197 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
199 wxevent.m_rawCode = [nsEvent keyCode];
200 wxevent.m_rawFlags = modifiers;
202 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
206 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
209 wxevent.SetEventType( wxEVT_KEY_UP ) ;
211 case NSFlagsChanged :
212 // setup common code here
219 if ( eventType != NSFlagsChanged )
221 NSString* nschars = [nsEvent characters];
224 // if charString is set, it did not come from key up / key down
225 wxevent.SetEventType( wxEVT_CHAR );
226 wxCFStringRef cfchars((CFStringRef)[charString retain]);
227 chars = cfchars.AsString();
231 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
232 chars = cfchars.AsString();
236 int aunichar = chars.Length() > 0 ? chars[0] : 0;
239 if (wxevent.GetEventType() != wxEVT_CHAR)
240 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
244 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
245 keyval = wxToupper( aunichar ) ;
251 wxevent.m_uniChar = aunichar;
253 wxevent.m_keyCode = keyval;
256 UInt32 g_lastButton = 0 ;
257 bool g_lastButtonWasFakeRight = false ;
259 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
261 int eventType = [nsEvent type];
262 UInt32 modifiers = [nsEvent modifierFlags] ;
263 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
265 // these parameters are not given for all events
266 UInt32 button = [nsEvent buttonNumber];
267 UInt32 clickCount = 0;
268 if ( [nsEvent respondsToSelector:@selector(clickCount:)] )
269 [nsEvent clickCount];
271 wxevent.m_x = screenMouseLocation.x;
272 wxevent.m_y = screenMouseLocation.y;
273 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
274 wxevent.m_controlDown = modifiers & NSControlKeyMask;
275 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
276 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
277 wxevent.m_clickCount = clickCount;
278 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
280 UInt32 mouseChord = 0;
284 case NSLeftMouseDown :
285 case NSLeftMouseDragged :
288 case NSRightMouseDown :
289 case NSRightMouseDragged :
292 case NSOtherMouseDown :
293 case NSOtherMouseDragged :
298 // a control click is interpreted as a right click
299 bool thisButtonIsFakeRight = false ;
300 if ( button == 0 && (modifiers & NSControlKeyMask) )
303 thisButtonIsFakeRight = true ;
306 // otherwise we report double clicks by connecting a left click with a ctrl-left click
307 if ( clickCount > 1 && button != g_lastButton )
310 // we must make sure that our synthetic 'right' button corresponds in
311 // mouse down, moved and mouse up, and does not deliver a right down and left up
314 case NSLeftMouseDown :
315 case NSRightMouseDown :
316 case NSOtherMouseDown :
317 g_lastButton = button ;
318 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
325 g_lastButtonWasFakeRight = false ;
327 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
328 button = g_lastButton ;
330 // Adjust the chord mask to remove the primary button and add the
331 // secondary button. It is possible that the secondary button is
332 // already pressed, e.g. on a mouse connected to a laptop, but this
333 // possibility is ignored here:
334 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
335 mouseChord = ((mouseChord & ~1U) | 2U);
338 wxevent.m_leftDown = true ;
340 wxevent.m_rightDown = true ;
342 wxevent.m_middleDown = true ;
344 // translate into wx types
347 case NSLeftMouseDown :
348 case NSRightMouseDown :
349 case NSOtherMouseDown :
353 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
357 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
361 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
370 case NSRightMouseUp :
371 case NSOtherMouseUp :
375 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
379 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
383 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
393 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
394 wxevent.m_wheelDelta = 10;
395 wxevent.m_linesPerAction = 1;
396 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
397 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
399 wxevent.m_wheelAxis = 1;
400 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
404 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
409 case NSMouseEntered :
410 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
413 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
415 case NSLeftMouseDragged :
416 case NSRightMouseDragged :
417 case NSOtherMouseDragged :
419 wxevent.SetEventType( wxEVT_MOTION ) ;
426 @implementation wxNSView
430 static BOOL initialized = NO;
434 wxOSXCocoaClassAddWXMethods( self );
438 - (BOOL) canBecomeKeyView
443 - (void) setTrackingTag: (NSTrackingRectTag)tag
448 - (NSTrackingRectTag) trackingTag
459 #if wxUSE_DRAG_AND_DROP
461 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
462 // for details on the NSPasteboard -> PasteboardRef conversion
464 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
466 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
468 return NSDragOperationNone;
470 return impl->draggingEntered(sender, self, _cmd);
473 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
475 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
479 return impl->draggingExited(sender, self, _cmd);
482 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
484 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
486 return NSDragOperationNone;
488 return impl->draggingUpdated(sender, self, _cmd);
491 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
493 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
495 return NSDragOperationNone;
497 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
500 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
502 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
506 impl->mouseEvent(event, self, _cmd);
509 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
511 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
515 impl->keyEvent(event, self, _cmd);
518 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
520 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
524 impl->insertText(text, self, _cmd);
527 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
529 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
533 return impl->performKeyEquivalent(event, self, _cmd);
536 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
538 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
542 return impl->becomeFirstResponder(self, _cmd);
545 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
547 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
551 return impl->resignFirstResponder(self, _cmd);
554 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
556 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
560 impl->resetCursorRects(self, _cmd);
563 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
565 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
569 return impl->isFlipped(self, _cmd) ? YES:NO;
572 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
574 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
578 return impl->drawRect(&rect, self, _cmd);
581 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
583 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
587 impl->controlAction(self, _cmd, sender);
590 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
592 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
596 impl->controlDoubleAction(self, _cmd, sender);
599 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
601 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
602 NSPasteboard *pboard = [sender draggingPasteboard];
603 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
605 wxWindow* wxpeer = GetWXPeer();
606 if ( wxpeer == NULL )
607 return NSDragOperationNone;
609 wxDropTarget* target = wxpeer->GetDropTarget();
610 if ( target == NULL )
611 return NSDragOperationNone;
613 wxDragResult result = wxDragNone;
614 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
616 if ( sourceDragMask & NSDragOperationLink )
618 else if ( sourceDragMask & NSDragOperationCopy )
620 else if ( sourceDragMask & NSDragOperationMove )
623 PasteboardRef pboardRef;
624 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
625 target->SetCurrentDragPasteboard(pboardRef);
626 result = target->OnEnter(pt.x, pt.y, result);
627 CFRelease(pboardRef);
629 NSDragOperation nsresult = NSDragOperationNone;
633 nsresult = NSDragOperationLink;
635 nsresult = NSDragOperationMove;
637 nsresult = NSDragOperationCopy;
644 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
646 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
647 NSPasteboard *pboard = [sender draggingPasteboard];
649 wxWindow* wxpeer = GetWXPeer();
650 if ( wxpeer == NULL )
653 wxDropTarget* target = wxpeer->GetDropTarget();
654 if ( target == NULL )
657 PasteboardRef pboardRef;
658 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
659 target->SetCurrentDragPasteboard(pboardRef);
661 CFRelease(pboardRef);
664 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
666 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
667 NSPasteboard *pboard = [sender draggingPasteboard];
668 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
670 wxWindow* wxpeer = GetWXPeer();
671 if ( wxpeer == NULL )
672 return NSDragOperationNone;
674 wxDropTarget* target = wxpeer->GetDropTarget();
675 if ( target == NULL )
676 return NSDragOperationNone;
678 wxDragResult result = wxDragNone;
679 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
681 if ( sourceDragMask & NSDragOperationLink )
683 else if ( sourceDragMask & NSDragOperationCopy )
685 else if ( sourceDragMask & NSDragOperationMove )
688 PasteboardRef pboardRef;
689 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
690 target->SetCurrentDragPasteboard(pboardRef);
691 result = target->OnDragOver(pt.x, pt.y, result);
692 CFRelease(pboardRef);
694 NSDragOperation nsresult = NSDragOperationNone;
698 nsresult = NSDragOperationLink;
700 nsresult = NSDragOperationMove;
702 nsresult = NSDragOperationCopy;
709 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
711 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
713 NSPasteboard *pboard = [sender draggingPasteboard];
714 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
716 wxWindow* wxpeer = GetWXPeer();
717 wxDropTarget* target = wxpeer->GetDropTarget();
718 wxDragResult result = wxDragNone;
719 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
721 if ( sourceDragMask & NSDragOperationLink )
723 else if ( sourceDragMask & NSDragOperationCopy )
725 else if ( sourceDragMask & NSDragOperationMove )
728 PasteboardRef pboardRef;
729 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
730 target->SetCurrentDragPasteboard(pboardRef);
731 result = target->OnData(pt.x, pt.y, result);
732 CFRelease(pboardRef);
734 return result != wxDragNone;
739 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
740 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
741 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
742 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
743 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
744 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
746 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
748 if ( !DoHandleMouseEvent(event) )
750 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
751 superimpl(slf, (SEL)_cmd, event);
755 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
757 if ( [[slf window] firstResponder] != slf || !DoHandleKeyEvent(event) )
759 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
760 superimpl(slf, (SEL)_cmd, event);
764 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
766 if (m_lastKeyDownEvent && !DoHandleCharEvent(m_lastKeyDownEvent, text) )
768 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
769 superimpl(slf, (SEL)_cmd, text);
771 m_lastKeyDownEvent = NULL;
775 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
777 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
778 return superimpl(slf, (SEL)_cmd, event);
781 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
783 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
784 BOOL r = superimpl(slf, (SEL)_cmd);
786 DoNotifyFocusEvent( true );
790 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
792 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
793 BOOL r = superimpl(slf, (SEL)_cmd);
795 DoNotifyFocusEvent( false );
799 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
801 wxWindow* wxpeer = GetWXPeer();
804 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
807 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
808 superimpl(slf, (SEL)_cmd);
812 [slf addCursorRect: [slf bounds]
818 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
824 #define OSX_DEBUG_DRAWING 0
826 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
828 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
829 CGContextSaveGState( context );
831 #if OSX_DEBUG_DRAWING
832 CGContextBeginPath( context );
833 CGContextMoveToPoint(context, 0, 0);
834 NSRect bounds = [self bounds];
835 CGContextAddLineToPoint(context, 10, 0);
836 CGContextMoveToPoint(context, 0, 0);
837 CGContextAddLineToPoint(context, 0, 10);
838 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
839 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
840 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
841 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
842 CGContextClosePath( context );
843 CGContextStrokePath(context);
848 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
849 CGContextScaleCTM( context, 1, -1 );
856 [slf getRectsBeingDrawn:&rects count:&count];
857 for ( int i = 0 ; i < count ; ++i )
859 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
862 wxWindow* wxpeer = GetWXPeer();
863 wxpeer->GetUpdateRegion() = updateRgn;
864 wxpeer->MacSetCGContextRef( context );
866 bool handled = wxpeer->MacDoRedraw( 0 );
868 CGContextRestoreGState( context );
870 CGContextSaveGState( context );
874 SEL _cmd = @selector(drawRect:);
875 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
876 superimpl(slf, _cmd, *(NSRect*)rect);
877 CGContextRestoreGState( context );
878 CGContextSaveGState( context );
880 wxpeer->MacPaintChildrenBorders();
881 wxpeer->MacSetCGContextRef( NULL );
882 CGContextRestoreGState( context );
885 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
887 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
889 wxpeer->OSXHandleClicked(0);
892 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
898 #if OBJC_API_VERSION >= 2
900 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
901 class_addMethod(c, s, i, t );
905 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
910 void wxOSXCocoaClassAddWXMethods(Class c)
913 #if OBJC_API_VERSION < 2
914 static objc_method wxmethods[] =
918 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
919 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
920 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
922 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
923 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
924 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
926 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
928 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
929 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
930 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
932 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
933 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
934 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
936 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
937 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
938 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
940 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
942 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
944 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
945 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
946 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
948 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
949 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
951 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
952 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
954 #if wxUSE_DRAG_AND_DROP
955 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
956 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
957 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
958 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
961 #if OBJC_API_VERSION < 2
963 static int method_count = WXSIZEOF( wxmethods );
964 static objc_method_list *wxmethodlist = NULL;
965 if ( wxmethodlist == NULL )
967 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
968 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
969 wxmethodlist->method_count = method_count;
970 wxmethodlist->obsolete = 0;
972 class_addMethods( c, wxmethodlist );
977 // C++ implementation class
980 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
982 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
983 wxWidgetImpl( peer, isRootControl )
989 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
994 void wxWidgetCocoaImpl::Init()
998 m_lastKeyDownEvent = NULL;
1001 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1003 RemoveAssociations( this );
1005 if ( !IsRootControl() )
1007 NSView *sv = [m_osxView superview];
1009 [m_osxView removeFromSuperview];
1011 [m_osxView release];
1014 bool wxWidgetCocoaImpl::IsVisible() const
1016 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1019 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1021 [m_osxView setHidden:(visible ? NO:YES)];
1024 void wxWidgetCocoaImpl::Raise()
1028 void wxWidgetCocoaImpl::Lower()
1032 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
1037 // We should do something like this, but it wasn't working in 10.4.
1038 if (GetNeedsDisplay() )
1042 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1043 NSSize offset = NSMakeSize((float)dx, (float)dy);
1044 [m_osxView scrollRect:r by:offset];
1048 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1050 wxWindowMac* parent = GetWXPeer()->GetParent();
1051 // under Cocoa we might have a contentView in the wxParent to which we have to
1052 // adjust the coordinates
1055 int cx = 0,cy = 0,cw = 0,ch = 0;
1056 if ( parent->GetPeer() )
1058 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1063 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1064 [m_osxView setFrame:r];
1066 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1068 if ( [(wxNSView*)m_osxView trackingTag] )
1069 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1071 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1075 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1077 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1082 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1084 NSRect rect = [m_osxView frame];
1085 width = rect.size.width;
1086 height = rect.size.height;
1089 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1091 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1093 NSView* cv = [m_osxView contentView];
1095 NSRect bounds = [m_osxView bounds];
1096 NSRect rect = [cv frame];
1098 int y = rect.origin.y;
1099 int x = rect.origin.x;
1100 if ( ![ m_osxView isFlipped ] )
1101 y = bounds.size.height - (rect.origin.y + rect.size.height);
1104 width = rect.size.width;
1105 height = rect.size.height;
1110 GetSize( width, height );
1114 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1117 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1119 [m_osxView setNeedsDisplay:YES];
1122 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1124 return [m_osxView needsDisplay];
1127 bool wxWidgetCocoaImpl::CanFocus() const
1129 return [m_osxView canBecomeKeyView] == YES;
1132 bool wxWidgetCocoaImpl::HasFocus() const
1134 return ( [[m_osxView window] firstResponder] == m_osxView );
1137 bool wxWidgetCocoaImpl::SetFocus()
1139 if ( [m_osxView canBecomeKeyView] == NO )
1142 [[m_osxView window] makeFirstResponder: m_osxView] ;
1147 void wxWidgetCocoaImpl::RemoveFromParent()
1149 [m_osxView removeFromSuperview];
1152 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1154 NSView* container = parent->GetWXWidget() ;
1155 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1156 [container addSubview:m_osxView];
1159 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1161 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1164 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1166 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1168 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1169 [m_osxView setTitle:cf.AsNSString()];
1171 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1173 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1174 [m_osxView setStringValue:cf.AsNSString()];
1179 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1181 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1182 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1183 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1186 wxInt32 wxWidgetCocoaImpl::GetValue() const
1188 return [(NSControl*)m_osxView intValue];
1191 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1193 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1195 [m_osxView setIntValue:v];
1197 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1199 [m_osxView setFloatValue:(double)v];
1201 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1203 [m_osxView setDoubleValue:(double)v];
1207 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1209 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1211 [m_osxView setMinValue:(double)v];
1215 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1217 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1219 [m_osxView setMaxValue:(double)v];
1223 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1225 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1227 return [m_osxView minValue];
1232 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1234 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1236 return [m_osxView maxValue];
1241 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1243 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1245 [m_osxView setImage:bitmap.GetNSImage()];
1249 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1251 // implementation in subclass
1254 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1256 r->x = r->y = r->width = r->height = 0;
1257 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1258 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1260 NSRect former = [m_osxView frame];
1261 [m_osxView sizeToFit];
1262 NSRect best = [m_osxView frame];
1263 [m_osxView setFrame:former];
1264 r->width = best.size.width;
1265 r->height = best.size.height;
1269 bool wxWidgetCocoaImpl::IsEnabled() const
1271 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1272 return [m_osxView isEnabled];
1276 void wxWidgetCocoaImpl::Enable( bool enable )
1278 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1279 [m_osxView setEnabled:enable];
1282 void wxWidgetCocoaImpl::PulseGauge()
1286 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1290 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1292 NSControlSize size = NSRegularControlSize;
1296 case wxWINDOW_VARIANT_NORMAL :
1297 size = NSRegularControlSize;
1300 case wxWINDOW_VARIANT_SMALL :
1301 size = NSSmallControlSize;
1304 case wxWINDOW_VARIANT_MINI :
1305 size = NSMiniControlSize;
1308 case wxWINDOW_VARIANT_LARGE :
1309 size = NSRegularControlSize;
1313 wxFAIL_MSG(_T("unexpected window variant"));
1316 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1317 [m_osxView setControlSize:size];
1320 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1325 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1327 WXWidget c = control ? control : (WXWidget) m_osxView;
1328 wxWidgetImpl::Associate( c, this ) ;
1329 if ([c respondsToSelector:@selector(setAction:)])
1332 [c setAction: @selector(controlAction:)];
1333 if ([c respondsToSelector:@selector(setDoubleAction:)])
1335 [c setDoubleAction: @selector(controlDoubleAction:)];
1341 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1343 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1344 SetupKeyEvent( wxevent, event, text );
1346 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1349 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1351 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1352 SetupKeyEvent( wxevent, event );
1354 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1356 // this will fire higher level events, like insertText, to help
1357 // us handle EVT_CHAR, etc.
1358 if ([event type] == NSKeyDown)
1360 m_lastKeyDownEvent = event;
1361 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1366 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1368 NSPoint clickLocation;
1369 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1370 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1371 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1372 SetupMouseEvent( wxevent , event ) ;
1376 return GetWXPeer()->HandleWindowEvent(wxevent);
1379 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1381 wxWindow* thisWindow = GetWXPeer();
1382 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1384 thisWindow->MacInvalidateBorders();
1387 if ( receivedFocus )
1389 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1390 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1391 thisWindow->HandleWindowEvent(eventFocus);
1394 if ( thisWindow->GetCaret() )
1395 thisWindow->GetCaret()->OnSetFocus();
1398 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1399 event.SetEventObject(thisWindow);
1400 // TODO how to find out the targetFocusWindow ?
1401 // event.SetWindow(targetFocusWindow);
1402 thisWindow->HandleWindowEvent(event) ;
1404 else // !receivedFocuss
1407 if ( thisWindow->GetCaret() )
1408 thisWindow->GetCaret()->OnKillFocus();
1411 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1413 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1414 event.SetEventObject(thisWindow);
1415 // TODO how to find out the targetFocusWindow ?
1416 // event.SetWindow(targetFocusWindow);
1417 thisWindow->HandleWindowEvent(event) ;
1421 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1423 NSPoint location = [NSEvent mouseLocation];
1424 location = [[m_osxView window] convertScreenToBase:location];
1425 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1427 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1429 [(NSCursor*)cursor.GetHCURSOR() set];
1431 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1434 void wxWidgetCocoaImpl::CaptureMouse()
1436 [[m_osxView window] disableCursorRects];
1439 void wxWidgetCocoaImpl::ReleaseMouse()
1441 [[m_osxView window] enableCursorRects];
1444 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1446 m_isFlipped = flipped;
1453 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1454 long style, long extraStyle)
1456 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1457 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1459 // temporary hook for dnd
1460 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1461 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1463 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1467 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1469 NSWindow* tlw = now->GetWXWindow();
1470 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1471 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1472 c->InstallEventHandler();
1473 [tlw setContentView:v];