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;
57 @interface NSView(PossibleMethods)
58 - (void)setTitle:(NSString *)aString;
59 - (void)setStringValue:(NSString *)aString;
60 - (void)setIntValue:(int)anInt;
61 - (void)setFloatValue:(float)aFloat;
62 - (void)setDoubleValue:(double)aDouble;
66 - (void)setMinValue:(double)aDouble;
67 - (void)setMaxValue:(double)aDouble;
72 - (void)setEnabled:(BOOL)flag;
74 - (void)setImage:(NSImage *)image;
75 - (void)setControlSize:(NSControlSize)size;
79 - (void)setTarget:(id)anObject;
80 - (void)setAction:(SEL)aSelector;
81 - (void)setDoubleAction:(SEL)aSelector;
84 long wxOSXTranslateCocoaKey( int unichar )
86 long retval = unichar;
89 case NSUpArrowFunctionKey :
92 case NSDownArrowFunctionKey :
95 case NSLeftArrowFunctionKey :
98 case NSRightArrowFunctionKey :
101 case NSInsertFunctionKey :
104 case NSDeleteFunctionKey :
107 case NSHomeFunctionKey :
110 // case NSBeginFunctionKey :
111 // retval = WXK_BEGIN;
113 case NSEndFunctionKey :
116 case NSPageUpFunctionKey :
119 case NSPageDownFunctionKey :
120 retval = WXK_PAGEDOWN;
122 case NSHelpFunctionKey :
127 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
128 retval = WXK_F1 + (unichar - NSF1FunctionKey );
134 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
136 UInt32 modifiers = [nsEvent modifierFlags] ;
137 int eventType = [nsEvent type];
139 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
140 wxevent.m_controlDown = modifiers & NSControlKeyMask;
141 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
142 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
144 wxevent.m_rawCode = [nsEvent keyCode];
145 wxevent.m_rawFlags = modifiers;
147 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
151 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
154 wxevent.SetEventType( wxEVT_KEY_UP ) ;
156 case NSFlagsChanged :
157 // setup common code here
164 if ( eventType != NSFlagsChanged )
166 NSString* nschars = [nsEvent characters];
169 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
170 chars = cfchars.AsString();
174 int unichar = chars.Length() > 0 ? chars[0] : 0;
175 long keyval = wxOSXTranslateCocoaKey(unichar) ;
176 if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
177 keyval = wxToupper( keyval ) ;
180 wxevent.m_uniChar = unichar;
182 wxevent.m_keyCode = keyval;
185 UInt32 g_lastButton = 0 ;
186 bool g_lastButtonWasFakeRight = false ;
188 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
190 int eventType = [nsEvent type];
191 UInt32 modifiers = [nsEvent modifierFlags] ;
192 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
194 // these parameters are not given for all events
195 UInt32 button = [nsEvent buttonNumber];
196 UInt32 clickCount = 0;
197 if ( [nsEvent respondsToSelector:@selector(clickCount:)] )
198 [nsEvent clickCount];
200 wxevent.m_x = screenMouseLocation.x;
201 wxevent.m_y = screenMouseLocation.y;
202 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
203 wxevent.m_controlDown = modifiers & NSControlKeyMask;
204 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
205 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
206 wxevent.m_clickCount = clickCount;
207 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
209 UInt32 mouseChord = 0;
213 case NSLeftMouseDown :
214 case NSLeftMouseDragged :
217 case NSRightMouseDown :
218 case NSRightMouseDragged :
221 case NSOtherMouseDown :
222 case NSOtherMouseDragged :
227 // a control click is interpreted as a right click
228 bool thisButtonIsFakeRight = false ;
229 if ( button == 0 && (modifiers & NSControlKeyMask) )
232 thisButtonIsFakeRight = true ;
235 // otherwise we report double clicks by connecting a left click with a ctrl-left click
236 if ( clickCount > 1 && button != g_lastButton )
239 // we must make sure that our synthetic 'right' button corresponds in
240 // mouse down, moved and mouse up, and does not deliver a right down and left up
243 case NSLeftMouseDown :
244 case NSRightMouseDown :
245 case NSOtherMouseDown :
246 g_lastButton = button ;
247 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
254 g_lastButtonWasFakeRight = false ;
256 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
257 button = g_lastButton ;
259 // Adjust the chord mask to remove the primary button and add the
260 // secondary button. It is possible that the secondary button is
261 // already pressed, e.g. on a mouse connected to a laptop, but this
262 // possibility is ignored here:
263 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
264 mouseChord = ((mouseChord & ~1U) | 2U);
267 wxevent.m_leftDown = true ;
269 wxevent.m_rightDown = true ;
271 wxevent.m_middleDown = true ;
273 // translate into wx types
276 case NSLeftMouseDown :
277 case NSRightMouseDown :
278 case NSOtherMouseDown :
282 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
286 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
290 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
299 case NSRightMouseUp :
300 case NSOtherMouseUp :
304 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
308 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
312 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
322 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
323 wxevent.m_wheelDelta = 10;
324 wxevent.m_linesPerAction = 1;
325 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
326 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
328 wxevent.m_wheelAxis = 1;
329 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
333 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
338 case NSMouseEntered :
339 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
342 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
344 case NSLeftMouseDragged :
345 case NSRightMouseDragged :
346 case NSOtherMouseDragged :
348 wxevent.SetEventType( wxEVT_MOTION ) ;
355 @implementation wxNSView
359 static BOOL initialized = NO;
363 wxOSXCocoaClassAddWXMethods( self );
367 - (BOOL) canBecomeKeyView
372 - (void) setTrackingTag: (NSTrackingRectTag)tag
377 - (NSTrackingRectTag) trackingTag
388 #if wxUSE_DRAG_AND_DROP
390 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
391 // for details on the NSPasteboard -> PasteboardRef conversion
393 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
395 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
397 return NSDragOperationNone;
399 return impl->draggingEntered(sender, self, _cmd);
402 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
404 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
408 return impl->draggingExited(sender, self, _cmd);
411 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
413 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
415 return NSDragOperationNone;
417 return impl->draggingUpdated(sender, self, _cmd);
420 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
422 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
424 return NSDragOperationNone;
426 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
429 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
431 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
435 impl->mouseEvent(event, self, _cmd);
438 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
440 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
444 impl->keyEvent(event, self, _cmd);
447 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
449 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
453 return impl->performKeyEquivalent(event, self, _cmd);
456 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
458 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
462 return impl->becomeFirstResponder(self, _cmd);
465 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
467 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
471 return impl->resignFirstResponder(self, _cmd);
474 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
476 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
480 impl->resetCursorRects(self, _cmd);
483 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
485 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
489 return impl->isFlipped(self, _cmd) ? YES:NO;
492 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
494 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
498 return impl->drawRect(&rect, self, _cmd);
501 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
503 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
507 impl->controlAction(self, _cmd, sender);
510 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
512 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
516 impl->controlDoubleAction(self, _cmd, sender);
519 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
521 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
522 NSPasteboard *pboard = [sender draggingPasteboard];
523 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
525 wxWindow* wxpeer = GetWXPeer();
526 if ( wxpeer == NULL )
527 return NSDragOperationNone;
529 wxDropTarget* target = wxpeer->GetDropTarget();
530 if ( target == NULL )
531 return NSDragOperationNone;
533 wxDragResult result = wxDragNone;
534 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
536 if ( sourceDragMask & NSDragOperationLink )
538 else if ( sourceDragMask & NSDragOperationCopy )
540 else if ( sourceDragMask & NSDragOperationMove )
543 PasteboardRef pboardRef;
544 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
545 target->SetCurrentDragPasteboard(pboardRef);
546 result = target->OnEnter(pt.x, pt.y, result);
547 CFRelease(pboardRef);
549 NSDragOperation nsresult = NSDragOperationNone;
553 nsresult = NSDragOperationLink;
555 nsresult = NSDragOperationMove;
557 nsresult = NSDragOperationCopy;
564 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
566 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
567 NSPasteboard *pboard = [sender draggingPasteboard];
569 wxWindow* wxpeer = GetWXPeer();
570 if ( wxpeer == NULL )
573 wxDropTarget* target = wxpeer->GetDropTarget();
574 if ( target == NULL )
577 PasteboardRef pboardRef;
578 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
579 target->SetCurrentDragPasteboard(pboardRef);
581 CFRelease(pboardRef);
584 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
586 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
587 NSPasteboard *pboard = [sender draggingPasteboard];
588 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
590 wxWindow* wxpeer = GetWXPeer();
591 if ( wxpeer == NULL )
592 return NSDragOperationNone;
594 wxDropTarget* target = wxpeer->GetDropTarget();
595 if ( target == NULL )
596 return NSDragOperationNone;
598 wxDragResult result = wxDragNone;
599 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
601 if ( sourceDragMask & NSDragOperationLink )
603 else if ( sourceDragMask & NSDragOperationCopy )
605 else if ( sourceDragMask & NSDragOperationMove )
608 PasteboardRef pboardRef;
609 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
610 target->SetCurrentDragPasteboard(pboardRef);
611 result = target->OnDragOver(pt.x, pt.y, result);
612 CFRelease(pboardRef);
614 NSDragOperation nsresult = NSDragOperationNone;
618 nsresult = NSDragOperationLink;
620 nsresult = NSDragOperationMove;
622 nsresult = NSDragOperationCopy;
629 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
631 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
633 NSPasteboard *pboard = [sender draggingPasteboard];
634 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
636 wxWindow* wxpeer = GetWXPeer();
637 wxDropTarget* target = wxpeer->GetDropTarget();
638 wxDragResult result = wxDragNone;
639 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
641 if ( sourceDragMask & NSDragOperationLink )
643 else if ( sourceDragMask & NSDragOperationCopy )
645 else if ( sourceDragMask & NSDragOperationMove )
648 PasteboardRef pboardRef;
649 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
650 target->SetCurrentDragPasteboard(pboardRef);
651 result = target->OnData(pt.x, pt.y, result);
652 CFRelease(pboardRef);
654 return result != wxDragNone;
659 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
660 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
661 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
662 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
663 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
665 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
667 if ( !DoHandleMouseEvent(event) )
669 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
670 superimpl(slf, (SEL)_cmd, event);
674 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
676 if ( !DoHandleKeyEvent(event) )
678 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
679 superimpl(slf, (SEL)_cmd, event);
683 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
685 if ( !DoHandleKeyEvent(event) )
687 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
688 return superimpl(slf, (SEL)_cmd, event);
694 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
696 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
697 BOOL r = superimpl(slf, (SEL)_cmd);
699 DoNotifyFocusEvent( true );
703 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
705 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
706 BOOL r = superimpl(slf, (SEL)_cmd);
708 DoNotifyFocusEvent( false );
712 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
714 wxWindow* wxpeer = GetWXPeer();
717 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
720 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
721 superimpl(slf, (SEL)_cmd);
725 [slf addCursorRect: [slf bounds]
731 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
737 #define OSX_DEBUG_DRAWING 0
739 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
741 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
742 CGContextSaveGState( context );
744 #if OSX_DEBUG_DRAWING
745 CGContextBeginPath( context );
746 CGContextMoveToPoint(context, 0, 0);
747 NSRect bounds = [self bounds];
748 CGContextAddLineToPoint(context, 10, 0);
749 CGContextMoveToPoint(context, 0, 0);
750 CGContextAddLineToPoint(context, 0, 10);
751 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
752 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
753 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
754 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
755 CGContextClosePath( context );
756 CGContextStrokePath(context);
761 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
762 CGContextScaleCTM( context, 1, -1 );
769 [slf getRectsBeingDrawn:&rects count:&count];
770 for ( int i = 0 ; i < count ; ++i )
772 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
775 wxWindow* wxpeer = GetWXPeer();
776 wxpeer->GetUpdateRegion() = updateRgn;
777 wxpeer->MacSetCGContextRef( context );
779 bool handled = wxpeer->MacDoRedraw( 0 );
781 CGContextRestoreGState( context );
783 CGContextSaveGState( context );
787 SEL _cmd = @selector(drawRect:);
788 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
789 superimpl(slf, _cmd, *(NSRect*)rect);
790 CGContextRestoreGState( context );
791 CGContextSaveGState( context );
793 wxpeer->MacPaintChildrenBorders();
794 wxpeer->MacSetCGContextRef( NULL );
795 CGContextRestoreGState( context );
798 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
800 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
802 wxpeer->OSXHandleClicked(0);
805 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
811 #if OBJC_API_VERSION >= 2
813 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
814 class_addMethod(c, s, i, t );
818 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
823 void wxOSXCocoaClassAddWXMethods(Class c)
826 #if OBJC_API_VERSION < 2
827 static objc_method wxmethods[] =
831 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
832 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
833 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
835 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
836 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
837 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
839 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
841 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
842 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
843 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
845 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
846 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
847 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
849 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
850 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
851 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
853 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
856 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
857 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
858 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
860 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
861 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
863 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
864 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
866 #if wxUSE_DRAG_AND_DROP
867 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
868 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
869 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
870 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
873 #if OBJC_API_VERSION < 2
875 static int method_count = WXSIZEOF( wxmethods );
876 static objc_method_list *wxmethodlist = NULL;
877 if ( wxmethodlist == NULL )
879 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
880 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
881 wxmethodlist->method_count = method_count;
882 wxmethodlist->obsolete = 0;
884 class_addMethods( c, wxmethodlist );
889 // C++ implementation class
892 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
894 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
895 wxWidgetImpl( peer, isRootControl )
901 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
906 void wxWidgetCocoaImpl::Init()
912 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
914 RemoveAssociations( this );
916 if ( !IsRootControl() )
918 NSView *sv = [m_osxView superview];
920 [m_osxView removeFromSuperview];
925 bool wxWidgetCocoaImpl::IsVisible() const
927 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
930 void wxWidgetCocoaImpl::SetVisibility( bool visible )
932 [m_osxView setHidden:(visible ? NO:YES)];
935 void wxWidgetCocoaImpl::Raise()
939 void wxWidgetCocoaImpl::Lower()
943 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
948 // We should do something like this, but it wasn't working in 10.4.
949 if (GetNeedsDisplay() )
953 NSRect r = wxToNSRect( [m_osxView superview], *rect );
954 NSSize offset = NSMakeSize((float)dx, (float)dy);
955 [m_osxView scrollRect:r by:offset];
959 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
961 wxWindowMac* parent = GetWXPeer()->GetParent();
962 // under Cocoa we might have a contentView in the wxParent to which we have to
963 // adjust the coordinates
966 int cx = 0,cy = 0,cw = 0,ch = 0;
967 if ( parent->GetPeer() )
969 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
974 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
975 [m_osxView setFrame:r];
977 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
979 if ( [(wxNSView*)m_osxView trackingTag] )
980 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
982 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
986 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
988 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
993 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
995 NSRect rect = [m_osxView frame];
996 width = rect.size.width;
997 height = rect.size.height;
1000 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1002 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1004 NSView* cv = [m_osxView contentView];
1006 NSRect bounds = [m_osxView bounds];
1007 NSRect rect = [cv frame];
1009 int y = rect.origin.y;
1010 int x = rect.origin.x;
1011 if ( ![ m_osxView isFlipped ] )
1012 y = bounds.size.height - (rect.origin.y + rect.size.height);
1015 width = rect.size.width;
1016 height = rect.size.height;
1021 GetSize( width, height );
1025 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1028 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1030 [m_osxView setNeedsDisplay:YES];
1033 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1035 return [m_osxView needsDisplay];
1038 bool wxWidgetCocoaImpl::CanFocus() const
1040 return [m_osxView canBecomeKeyView] == YES;
1043 bool wxWidgetCocoaImpl::HasFocus() const
1045 return ( [[m_osxView window] firstResponder] == m_osxView );
1048 bool wxWidgetCocoaImpl::SetFocus()
1050 if ( [m_osxView canBecomeKeyView] == NO )
1053 [[m_osxView window] makeFirstResponder: m_osxView] ;
1058 void wxWidgetCocoaImpl::RemoveFromParent()
1060 [m_osxView removeFromSuperview];
1063 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1065 NSView* container = parent->GetWXWidget() ;
1066 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1067 [container addSubview:m_osxView];
1070 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1072 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1075 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1077 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1079 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1080 [m_osxView setTitle:cf.AsNSString()];
1082 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1084 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1085 [m_osxView setStringValue:cf.AsNSString()];
1090 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1092 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1093 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1094 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1097 wxInt32 wxWidgetCocoaImpl::GetValue() const
1099 return [(NSControl*)m_osxView intValue];
1102 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1104 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1106 [m_osxView setIntValue:v];
1108 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1110 [m_osxView setFloatValue:(double)v];
1112 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1114 [m_osxView setDoubleValue:(double)v];
1118 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1120 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1122 [m_osxView setMinValue:(double)v];
1126 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1128 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1130 [m_osxView setMaxValue:(double)v];
1134 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1136 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1138 return [m_osxView minValue];
1143 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1145 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1147 return [m_osxView maxValue];
1152 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1154 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1156 [m_osxView setImage:bitmap.GetNSImage()];
1160 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1162 // implementation in subclass
1165 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1167 r->x = r->y = r->width = r->height = 0;
1168 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1169 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1171 NSRect former = [m_osxView frame];
1172 [m_osxView sizeToFit];
1173 NSRect best = [m_osxView frame];
1174 [m_osxView setFrame:former];
1175 r->width = best.size.width;
1176 r->height = best.size.height;
1180 bool wxWidgetCocoaImpl::IsEnabled() const
1182 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1183 return [m_osxView isEnabled];
1187 void wxWidgetCocoaImpl::Enable( bool enable )
1189 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1190 [m_osxView setEnabled:enable];
1193 void wxWidgetCocoaImpl::PulseGauge()
1197 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1201 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1203 NSControlSize size = NSRegularControlSize;
1207 case wxWINDOW_VARIANT_NORMAL :
1208 size = NSRegularControlSize;
1211 case wxWINDOW_VARIANT_SMALL :
1212 size = NSSmallControlSize;
1215 case wxWINDOW_VARIANT_MINI :
1216 size = NSMiniControlSize;
1219 case wxWINDOW_VARIANT_LARGE :
1220 size = NSRegularControlSize;
1224 wxFAIL_MSG(_T("unexpected window variant"));
1227 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1228 [m_osxView setControlSize:size];
1231 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1236 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1238 WXWidget c = control ? control : (WXWidget) m_osxView;
1239 wxWidgetImpl::Associate( c, this ) ;
1240 if ([c respondsToSelector:@selector(setAction:)])
1243 [c setAction: @selector(controlAction:)];
1244 if ([c respondsToSelector:@selector(setDoubleAction:)])
1246 [c setDoubleAction: @selector(controlDoubleAction:)];
1252 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1254 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1255 SetupKeyEvent( wxevent, event );
1257 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1260 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1262 NSPoint clickLocation;
1263 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1264 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1265 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1266 SetupMouseEvent( wxevent , event ) ;
1270 return GetWXPeer()->HandleWindowEvent(wxevent);
1273 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1275 wxWindow* thisWindow = GetWXPeer();
1276 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1278 thisWindow->MacInvalidateBorders();
1281 if ( receivedFocus )
1283 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1284 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1285 thisWindow->HandleWindowEvent(eventFocus);
1288 if ( thisWindow->GetCaret() )
1289 thisWindow->GetCaret()->OnSetFocus();
1292 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1293 event.SetEventObject(thisWindow);
1294 // TODO how to find out the targetFocusWindow ?
1295 // event.SetWindow(targetFocusWindow);
1296 thisWindow->HandleWindowEvent(event) ;
1298 else // !receivedFocuss
1301 if ( thisWindow->GetCaret() )
1302 thisWindow->GetCaret()->OnKillFocus();
1305 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1307 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1308 event.SetEventObject(thisWindow);
1309 // TODO how to find out the targetFocusWindow ?
1310 // event.SetWindow(targetFocusWindow);
1311 thisWindow->HandleWindowEvent(event) ;
1315 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1317 NSPoint location = [NSEvent mouseLocation];
1318 location = [[m_osxView window] convertScreenToBase:location];
1319 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1321 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1323 [(NSCursor*)cursor.GetHCURSOR() set];
1325 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1328 void wxWidgetCocoaImpl::CaptureMouse()
1330 [[m_osxView window] disableCursorRects];
1333 void wxWidgetCocoaImpl::ReleaseMouse()
1335 [[m_osxView window] enableCursorRects];
1338 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1340 m_isFlipped = flipped;
1347 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1348 long style, long extraStyle)
1350 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1351 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1353 // temporary hook for dnd
1354 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1355 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1357 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1361 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1363 NSWindow* tlw = now->GetWXWindow();
1364 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1365 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1366 c->InstallEventHandler();
1367 [tlw setContentView:v];