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
49 - (BOOL) canBecomeKeyView;
53 @interface NSView(PossibleMethods)
54 - (void)setTitle:(NSString *)aString;
55 - (void)setStringValue:(NSString *)aString;
56 - (void)setIntValue:(int)anInt;
57 - (void)setFloatValue:(float)aFloat;
58 - (void)setDoubleValue:(double)aDouble;
62 - (void)setMinValue:(double)aDouble;
63 - (void)setMaxValue:(double)aDouble;
68 - (void)setEnabled:(BOOL)flag;
70 - (void)setImage:(NSImage *)image;
71 - (void)setControlSize:(NSControlSize)size;
75 - (void)setTarget:(id)anObject;
76 - (void)setAction:(SEL)aSelector;
77 - (void)setDoubleAction:(SEL)aSelector;
80 long wxOSXTranslateCocoaKey( int unichar )
82 long retval = unichar;
85 case NSUpArrowFunctionKey :
88 case NSDownArrowFunctionKey :
91 case NSLeftArrowFunctionKey :
94 case NSRightArrowFunctionKey :
97 case NSInsertFunctionKey :
100 case NSDeleteFunctionKey :
103 case NSHomeFunctionKey :
106 // case NSBeginFunctionKey :
107 // retval = WXK_BEGIN;
109 case NSEndFunctionKey :
112 case NSPageUpFunctionKey :
115 case NSPageDownFunctionKey :
116 retval = WXK_PAGEDOWN;
118 case NSHelpFunctionKey :
123 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
124 retval = WXK_F1 + (unichar - NSF1FunctionKey );
130 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
132 UInt32 modifiers = [nsEvent modifierFlags] ;
133 int eventType = [nsEvent type];
135 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
136 wxevent.m_controlDown = modifiers & NSControlKeyMask;
137 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
138 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
140 wxevent.m_rawCode = [nsEvent keyCode];
141 wxevent.m_rawFlags = modifiers;
143 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
147 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
150 wxevent.SetEventType( wxEVT_KEY_UP ) ;
152 case NSFlagsChanged :
153 // setup common code here
160 if ( eventType != NSFlagsChanged )
162 NSString* nschars = [nsEvent characters];
165 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
166 chars = cfchars.AsString();
170 int unichar = chars.Length() > 0 ? chars[0] : 0;
171 long keyval = wxOSXTranslateCocoaKey(unichar) ;
172 if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
173 keyval = wxToupper( keyval ) ;
176 wxevent.m_uniChar = unichar;
178 wxevent.m_keyCode = keyval;
181 UInt32 g_lastButton = 0 ;
182 bool g_lastButtonWasFakeRight = false ;
184 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
186 int eventType = [nsEvent type];
187 UInt32 modifiers = [nsEvent modifierFlags] ;
188 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
190 // these parameters are not given for all events
191 UInt32 button = [nsEvent buttonNumber];
192 UInt32 clickCount = 0;
193 if ( eventType != NSScrollWheel )
194 [nsEvent clickCount];
196 wxevent.m_x = screenMouseLocation.x;
197 wxevent.m_y = screenMouseLocation.y;
198 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
199 wxevent.m_controlDown = modifiers & NSControlKeyMask;
200 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
201 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
202 wxevent.m_clickCount = clickCount;
203 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
205 UInt32 mouseChord = 0;
209 case NSLeftMouseDown :
210 case NSLeftMouseDragged :
213 case NSRightMouseDown :
214 case NSRightMouseDragged :
217 case NSOtherMouseDown :
218 case NSOtherMouseDragged :
223 // a control click is interpreted as a right click
224 bool thisButtonIsFakeRight = false ;
225 if ( button == 0 && (modifiers & NSControlKeyMask) )
228 thisButtonIsFakeRight = true ;
231 // otherwise we report double clicks by connecting a left click with a ctrl-left click
232 if ( clickCount > 1 && button != g_lastButton )
235 // we must make sure that our synthetic 'right' button corresponds in
236 // mouse down, moved and mouse up, and does not deliver a right down and left up
239 case NSLeftMouseDown :
240 case NSRightMouseDown :
241 case NSOtherMouseDown :
242 g_lastButton = button ;
243 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
250 g_lastButtonWasFakeRight = false ;
252 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
253 button = g_lastButton ;
255 // Adjust the chord mask to remove the primary button and add the
256 // secondary button. It is possible that the secondary button is
257 // already pressed, e.g. on a mouse connected to a laptop, but this
258 // possibility is ignored here:
259 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
260 mouseChord = ((mouseChord & ~1U) | 2U);
263 wxevent.m_leftDown = true ;
265 wxevent.m_rightDown = true ;
267 wxevent.m_middleDown = true ;
269 // translate into wx types
272 case NSLeftMouseDown :
273 case NSRightMouseDown :
274 case NSOtherMouseDown :
278 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
282 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
286 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
295 case NSRightMouseUp :
296 case NSOtherMouseUp :
300 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
304 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
308 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
318 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
319 wxevent.m_wheelDelta = 10;
320 wxevent.m_linesPerAction = 1;
321 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
322 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
324 wxevent.m_wheelAxis = 1;
325 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
329 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
334 case NSMouseEntered :
336 case NSLeftMouseDragged :
337 case NSRightMouseDragged :
338 case NSOtherMouseDragged :
340 wxevent.SetEventType( wxEVT_MOTION ) ;
347 @implementation wxNSView
351 static BOOL initialized = NO;
355 wxOSXCocoaClassAddWXMethods( self );
359 - (BOOL) canBecomeKeyView
370 #if wxUSE_DRAG_AND_DROP
372 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
373 // for details on the NSPasteboard -> PasteboardRef conversion
375 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
377 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
379 return NSDragOperationNone;
381 return impl->draggingEntered(sender, self, _cmd);
384 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
386 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
390 return impl->draggingExited(sender, self, _cmd);
393 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
395 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
397 return NSDragOperationNone;
399 return impl->draggingUpdated(sender, self, _cmd);
402 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
404 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
406 return NSDragOperationNone;
408 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
411 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
413 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
417 impl->mouseEvent(event, self, _cmd);
420 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
422 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
426 impl->keyEvent(event, self, _cmd);
429 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
431 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
435 return impl->performKeyEquivalent(event, self, _cmd);
438 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
440 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
444 return impl->becomeFirstResponder(self, _cmd);
447 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
449 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
453 return impl->resignFirstResponder(self, _cmd);
456 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
458 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
462 impl->resetCursorRects(self, _cmd);
465 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
467 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
471 return impl->isFlipped(self, _cmd) ? YES:NO;
474 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
476 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
480 return impl->drawRect(&rect, self, _cmd);
483 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
485 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
489 impl->controlAction(self, _cmd, sender);
492 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
494 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
498 impl->controlDoubleAction(self, _cmd, sender);
501 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
503 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
504 NSPasteboard *pboard = [sender draggingPasteboard];
505 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
507 wxWindow* wxpeer = GetWXPeer();
508 if ( wxpeer == NULL )
509 return NSDragOperationNone;
511 wxDropTarget* target = wxpeer->GetDropTarget();
512 if ( target == NULL )
513 return NSDragOperationNone;
515 wxDragResult result = wxDragNone;
516 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
518 if ( sourceDragMask & NSDragOperationLink )
520 else if ( sourceDragMask & NSDragOperationCopy )
522 else if ( sourceDragMask & NSDragOperationMove )
525 PasteboardRef pboardRef;
526 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
527 target->SetCurrentDragPasteboard(pboardRef);
528 result = target->OnEnter(pt.x, pt.y, result);
529 CFRelease(pboardRef);
531 NSDragOperation nsresult = NSDragOperationNone;
535 nsresult = NSDragOperationLink;
537 nsresult = NSDragOperationMove;
539 nsresult = NSDragOperationCopy;
546 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
548 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
549 NSPasteboard *pboard = [sender draggingPasteboard];
551 wxWindow* wxpeer = GetWXPeer();
552 if ( wxpeer == NULL )
555 wxDropTarget* target = wxpeer->GetDropTarget();
556 if ( target == NULL )
559 PasteboardRef pboardRef;
560 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
561 target->SetCurrentDragPasteboard(pboardRef);
563 CFRelease(pboardRef);
566 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
568 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
569 NSPasteboard *pboard = [sender draggingPasteboard];
570 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
572 wxWindow* wxpeer = GetWXPeer();
573 if ( wxpeer == NULL )
574 return NSDragOperationNone;
576 wxDropTarget* target = wxpeer->GetDropTarget();
577 if ( target == NULL )
578 return NSDragOperationNone;
580 wxDragResult result = wxDragNone;
581 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
583 if ( sourceDragMask & NSDragOperationLink )
585 else if ( sourceDragMask & NSDragOperationCopy )
587 else if ( sourceDragMask & NSDragOperationMove )
590 PasteboardRef pboardRef;
591 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
592 target->SetCurrentDragPasteboard(pboardRef);
593 result = target->OnDragOver(pt.x, pt.y, result);
594 CFRelease(pboardRef);
596 NSDragOperation nsresult = NSDragOperationNone;
600 nsresult = NSDragOperationLink;
602 nsresult = NSDragOperationMove;
604 nsresult = NSDragOperationCopy;
611 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
613 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
615 NSPasteboard *pboard = [sender draggingPasteboard];
616 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
618 wxWindow* wxpeer = GetWXPeer();
619 wxDropTarget* target = wxpeer->GetDropTarget();
620 wxDragResult result = wxDragNone;
621 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
623 if ( sourceDragMask & NSDragOperationLink )
625 else if ( sourceDragMask & NSDragOperationCopy )
627 else if ( sourceDragMask & NSDragOperationMove )
630 PasteboardRef pboardRef;
631 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
632 target->SetCurrentDragPasteboard(pboardRef);
633 result = target->OnData(pt.x, pt.y, result);
634 CFRelease(pboardRef);
636 return result != wxDragNone;
641 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
642 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
643 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
644 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
645 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
647 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
649 if ( !DoHandleMouseEvent(event) )
651 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
652 superimpl(slf, (SEL)_cmd, event);
656 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
658 if ( !DoHandleKeyEvent(event) )
660 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
661 superimpl(slf, (SEL)_cmd, event);
665 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
667 if ( !DoHandleKeyEvent(event) )
669 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
670 return superimpl(slf, (SEL)_cmd, event);
676 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
678 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
679 BOOL r = superimpl(slf, (SEL)_cmd);
681 DoNotifyFocusEvent( true );
685 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
687 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
688 BOOL r = superimpl(slf, (SEL)_cmd);
690 DoNotifyFocusEvent( false );
694 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
696 wxWindow* wxpeer = GetWXPeer();
699 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
702 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
703 superimpl(slf, (SEL)_cmd);
707 [slf addCursorRect: [slf bounds]
713 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
719 #define OSX_DEBUG_DRAWING 0
721 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
723 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
724 CGContextSaveGState( context );
726 #if OSX_DEBUG_DRAWING
727 CGContextBeginPath( context );
728 CGContextMoveToPoint(context, 0, 0);
729 NSRect bounds = [self bounds];
730 CGContextAddLineToPoint(context, 10, 0);
731 CGContextMoveToPoint(context, 0, 0);
732 CGContextAddLineToPoint(context, 0, 10);
733 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
734 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
735 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
736 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
737 CGContextClosePath( context );
738 CGContextStrokePath(context);
743 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
744 CGContextScaleCTM( context, 1, -1 );
751 [slf getRectsBeingDrawn:&rects count:&count];
752 for ( int i = 0 ; i < count ; ++i )
754 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
757 wxWindow* wxpeer = GetWXPeer();
758 wxpeer->GetUpdateRegion() = updateRgn;
759 wxpeer->MacSetCGContextRef( context );
761 bool handled = wxpeer->MacDoRedraw( 0 );
763 CGContextRestoreGState( context );
765 CGContextSaveGState( context );
769 SEL _cmd = @selector(drawRect:);
770 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
771 superimpl(slf, _cmd, *(NSRect*)rect);
772 CGContextRestoreGState( context );
773 CGContextSaveGState( context );
775 wxpeer->MacPaintChildrenBorders();
776 wxpeer->MacSetCGContextRef( NULL );
777 CGContextRestoreGState( context );
780 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
782 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
784 wxpeer->OSXHandleClicked(0);
787 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
793 #if OBJC_API_VERSION >= 2
795 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
796 class_addMethod(c, s, i, t );
800 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
805 void wxOSXCocoaClassAddWXMethods(Class c)
808 #if OBJC_API_VERSION < 2
809 static objc_method wxmethods[] =
813 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
814 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
815 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
817 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
818 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
819 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
821 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
823 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
824 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
825 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
827 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
828 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
829 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
831 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
832 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
833 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
835 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
838 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
839 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
840 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
842 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
843 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
845 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
846 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
848 #if wxUSE_DRAG_AND_DROP
849 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
850 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
851 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
852 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
855 #if OBJC_API_VERSION < 2
857 static int method_count = WXSIZEOF( wxmethods );
858 static objc_method_list *wxmethodlist = NULL;
859 if ( wxmethodlist == NULL )
861 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
862 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
863 wxmethodlist->method_count = method_count;
864 wxmethodlist->obsolete = 0;
866 class_addMethods( c, wxmethodlist );
871 // C++ implementation class
874 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
876 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
877 wxWidgetImpl( peer, isRootControl )
883 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
888 void wxWidgetCocoaImpl::Init()
894 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
896 RemoveAssociations( this );
898 if ( !IsRootControl() )
900 NSView *sv = [m_osxView superview];
902 [m_osxView removeFromSuperview];
907 bool wxWidgetCocoaImpl::IsVisible() const
909 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
912 void wxWidgetCocoaImpl::SetVisibility( bool visible )
914 [m_osxView setHidden:(visible ? NO:YES)];
917 void wxWidgetCocoaImpl::Raise()
921 void wxWidgetCocoaImpl::Lower()
925 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
930 // We should do something like this, but it wasn't working in 10.4.
931 if (GetNeedsDisplay() )
935 NSRect r = wxToNSRect( [m_osxView superview], *rect );
936 NSSize offset = NSMakeSize((float)dx, (float)dy);
937 [m_osxView scrollRect:r by:offset];
941 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
943 wxWindowMac* parent = GetWXPeer()->GetParent();
944 // under Cocoa we might have a contentView in the wxParent to which we have to
945 // adjust the coordinates
948 int cx = 0,cy = 0,cw = 0,ch = 0;
949 if ( parent->GetPeer() )
951 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
956 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
957 [m_osxView setFrame:r];
960 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
962 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
967 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
969 NSRect rect = [m_osxView frame];
970 width = rect.size.width;
971 height = rect.size.height;
974 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
976 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
978 NSView* cv = [m_osxView contentView];
980 NSRect bounds = [m_osxView bounds];
981 NSRect rect = [cv frame];
983 int y = rect.origin.y;
984 int x = rect.origin.x;
985 if ( ![ m_osxView isFlipped ] )
986 y = bounds.size.height - (rect.origin.y + rect.size.height);
989 width = rect.size.width;
990 height = rect.size.height;
995 GetSize( width, height );
999 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1002 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1004 [m_osxView setNeedsDisplay:YES];
1007 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1009 return [m_osxView needsDisplay];
1012 bool wxWidgetCocoaImpl::CanFocus() const
1014 return [m_osxView canBecomeKeyView] == YES;
1017 bool wxWidgetCocoaImpl::HasFocus() const
1019 return ( [[m_osxView window] firstResponder] == m_osxView );
1022 bool wxWidgetCocoaImpl::SetFocus()
1024 if ( [m_osxView canBecomeKeyView] == NO )
1027 [[m_osxView window] makeFirstResponder: m_osxView] ;
1032 void wxWidgetCocoaImpl::RemoveFromParent()
1034 [m_osxView removeFromSuperview];
1037 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1039 NSView* container = parent->GetWXWidget() ;
1040 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1041 [container addSubview:m_osxView];
1044 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1046 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1049 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1051 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1053 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1054 [m_osxView setTitle:cf.AsNSString()];
1056 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1058 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1059 [m_osxView setStringValue:cf.AsNSString()];
1064 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1066 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1067 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1068 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1071 wxInt32 wxWidgetCocoaImpl::GetValue() const
1073 return [(NSControl*)m_osxView intValue];
1076 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1078 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1080 [m_osxView setIntValue:v];
1082 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1084 [m_osxView setFloatValue:(double)v];
1086 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1088 [m_osxView setDoubleValue:(double)v];
1092 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1094 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1096 [m_osxView setMinValue:(double)v];
1100 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1102 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1104 [m_osxView setMaxValue:(double)v];
1108 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1110 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1112 return [m_osxView minValue];
1117 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1119 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1121 return [m_osxView maxValue];
1126 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1128 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1130 [m_osxView setImage:bitmap.GetNSImage()];
1134 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1136 // implementation in subclass
1139 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1141 r->x = r->y = r->width = r->height = 0;
1142 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1143 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1145 NSRect former = [m_osxView frame];
1146 [m_osxView sizeToFit];
1147 NSRect best = [m_osxView frame];
1148 [m_osxView setFrame:former];
1149 r->width = best.size.width;
1150 r->height = best.size.height;
1154 bool wxWidgetCocoaImpl::IsEnabled() const
1156 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1157 return [m_osxView isEnabled];
1161 void wxWidgetCocoaImpl::Enable( bool enable )
1163 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1164 [m_osxView setEnabled:enable];
1167 void wxWidgetCocoaImpl::PulseGauge()
1171 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1175 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1177 NSControlSize size = NSRegularControlSize;
1181 case wxWINDOW_VARIANT_NORMAL :
1182 size = NSRegularControlSize;
1185 case wxWINDOW_VARIANT_SMALL :
1186 size = NSSmallControlSize;
1189 case wxWINDOW_VARIANT_MINI :
1190 size = NSMiniControlSize;
1193 case wxWINDOW_VARIANT_LARGE :
1194 size = NSRegularControlSize;
1198 wxFAIL_MSG(_T("unexpected window variant"));
1201 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1202 [m_osxView setControlSize:size];
1205 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1210 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1212 WXWidget c = control ? control : (WXWidget) m_osxView;
1213 wxWidgetImpl::Associate( c, this ) ;
1214 if ([c respondsToSelector:@selector(setAction:)])
1217 [c setAction: @selector(controlAction:)];
1218 if ([c respondsToSelector:@selector(setDoubleAction:)])
1220 [c setDoubleAction: @selector(controlDoubleAction:)];
1226 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1228 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1229 SetupKeyEvent( wxevent, event );
1231 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1234 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1236 NSPoint clickLocation;
1237 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1238 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1239 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1240 SetupMouseEvent( wxevent , event ) ;
1244 return GetWXPeer()->HandleWindowEvent(wxevent);
1247 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1249 wxWindow* thisWindow = GetWXPeer();
1250 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1252 thisWindow->MacInvalidateBorders();
1255 if ( receivedFocus )
1257 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1258 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1259 thisWindow->HandleWindowEvent(eventFocus);
1262 if ( thisWindow->GetCaret() )
1263 thisWindow->GetCaret()->OnSetFocus();
1266 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1267 event.SetEventObject(thisWindow);
1268 // TODO how to find out the targetFocusWindow ?
1269 // event.SetWindow(targetFocusWindow);
1270 thisWindow->HandleWindowEvent(event) ;
1272 else // !receivedFocuss
1275 if ( thisWindow->GetCaret() )
1276 thisWindow->GetCaret()->OnKillFocus();
1279 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1281 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1282 event.SetEventObject(thisWindow);
1283 // TODO how to find out the targetFocusWindow ?
1284 // event.SetWindow(targetFocusWindow);
1285 thisWindow->HandleWindowEvent(event) ;
1289 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1291 NSPoint location = [NSEvent mouseLocation];
1292 location = [[m_osxView window] convertScreenToBase:location];
1293 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1295 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1297 [(NSCursor*)cursor.GetHCURSOR() set];
1299 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1302 void wxWidgetCocoaImpl::CaptureMouse()
1304 [[m_osxView window] disableCursorRects];
1307 void wxWidgetCocoaImpl::ReleaseMouse()
1309 [[m_osxView window] enableCursorRects];
1312 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1314 m_isFlipped = flipped;
1321 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1322 long style, long extraStyle)
1324 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1325 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1327 // temporary hook for dnd
1328 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1329 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1331 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1335 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1337 NSWindow* tlw = now->GetWXWindow();
1338 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1339 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1340 [tlw setContentView:v];