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 ( [nsEvent respondsToSelector:@selector(clickCount:)] )
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 :
335 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
338 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
340 case NSLeftMouseDragged :
341 case NSRightMouseDragged :
342 case NSOtherMouseDragged :
344 wxevent.SetEventType( wxEVT_MOTION ) ;
351 @implementation wxNSView
355 static BOOL initialized = NO;
359 wxOSXCocoaClassAddWXMethods( self );
363 - (BOOL) canBecomeKeyView
374 #if wxUSE_DRAG_AND_DROP
376 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
377 // for details on the NSPasteboard -> PasteboardRef conversion
379 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
381 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
383 return NSDragOperationNone;
385 return impl->draggingEntered(sender, self, _cmd);
388 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
390 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
394 return impl->draggingExited(sender, self, _cmd);
397 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
399 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
401 return NSDragOperationNone;
403 return impl->draggingUpdated(sender, self, _cmd);
406 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
408 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
410 return NSDragOperationNone;
412 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
415 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
417 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
421 impl->mouseEvent(event, self, _cmd);
424 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
426 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
430 impl->keyEvent(event, self, _cmd);
433 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
435 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
439 return impl->performKeyEquivalent(event, self, _cmd);
442 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
444 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
448 return impl->becomeFirstResponder(self, _cmd);
451 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
453 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
457 return impl->resignFirstResponder(self, _cmd);
460 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
462 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
466 impl->resetCursorRects(self, _cmd);
469 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
471 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
475 return impl->isFlipped(self, _cmd) ? YES:NO;
478 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
480 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
484 return impl->drawRect(&rect, self, _cmd);
487 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
489 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
493 impl->controlAction(self, _cmd, sender);
496 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
498 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
502 impl->controlDoubleAction(self, _cmd, sender);
505 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
507 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
508 NSPasteboard *pboard = [sender draggingPasteboard];
509 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
511 wxWindow* wxpeer = GetWXPeer();
512 if ( wxpeer == NULL )
513 return NSDragOperationNone;
515 wxDropTarget* target = wxpeer->GetDropTarget();
516 if ( target == NULL )
517 return NSDragOperationNone;
519 wxDragResult result = wxDragNone;
520 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
522 if ( sourceDragMask & NSDragOperationLink )
524 else if ( sourceDragMask & NSDragOperationCopy )
526 else if ( sourceDragMask & NSDragOperationMove )
529 PasteboardRef pboardRef;
530 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
531 target->SetCurrentDragPasteboard(pboardRef);
532 result = target->OnEnter(pt.x, pt.y, result);
533 CFRelease(pboardRef);
535 NSDragOperation nsresult = NSDragOperationNone;
539 nsresult = NSDragOperationLink;
541 nsresult = NSDragOperationMove;
543 nsresult = NSDragOperationCopy;
550 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
552 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
553 NSPasteboard *pboard = [sender draggingPasteboard];
555 wxWindow* wxpeer = GetWXPeer();
556 if ( wxpeer == NULL )
559 wxDropTarget* target = wxpeer->GetDropTarget();
560 if ( target == NULL )
563 PasteboardRef pboardRef;
564 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
565 target->SetCurrentDragPasteboard(pboardRef);
567 CFRelease(pboardRef);
570 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
572 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
573 NSPasteboard *pboard = [sender draggingPasteboard];
574 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
576 wxWindow* wxpeer = GetWXPeer();
577 if ( wxpeer == NULL )
578 return NSDragOperationNone;
580 wxDropTarget* target = wxpeer->GetDropTarget();
581 if ( target == NULL )
582 return NSDragOperationNone;
584 wxDragResult result = wxDragNone;
585 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
587 if ( sourceDragMask & NSDragOperationLink )
589 else if ( sourceDragMask & NSDragOperationCopy )
591 else if ( sourceDragMask & NSDragOperationMove )
594 PasteboardRef pboardRef;
595 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
596 target->SetCurrentDragPasteboard(pboardRef);
597 result = target->OnDragOver(pt.x, pt.y, result);
598 CFRelease(pboardRef);
600 NSDragOperation nsresult = NSDragOperationNone;
604 nsresult = NSDragOperationLink;
606 nsresult = NSDragOperationMove;
608 nsresult = NSDragOperationCopy;
615 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
617 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
619 NSPasteboard *pboard = [sender draggingPasteboard];
620 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
622 wxWindow* wxpeer = GetWXPeer();
623 wxDropTarget* target = wxpeer->GetDropTarget();
624 wxDragResult result = wxDragNone;
625 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
627 if ( sourceDragMask & NSDragOperationLink )
629 else if ( sourceDragMask & NSDragOperationCopy )
631 else if ( sourceDragMask & NSDragOperationMove )
634 PasteboardRef pboardRef;
635 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
636 target->SetCurrentDragPasteboard(pboardRef);
637 result = target->OnData(pt.x, pt.y, result);
638 CFRelease(pboardRef);
640 return result != wxDragNone;
645 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
646 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
647 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
648 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
649 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
651 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
653 if ( !DoHandleMouseEvent(event) )
655 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
656 superimpl(slf, (SEL)_cmd, event);
660 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
662 if ( !DoHandleKeyEvent(event) )
664 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
665 superimpl(slf, (SEL)_cmd, event);
669 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
671 if ( !DoHandleKeyEvent(event) )
673 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
674 return superimpl(slf, (SEL)_cmd, event);
680 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
682 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
683 BOOL r = superimpl(slf, (SEL)_cmd);
685 DoNotifyFocusEvent( true );
689 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
691 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
692 BOOL r = superimpl(slf, (SEL)_cmd);
694 DoNotifyFocusEvent( false );
698 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
700 wxWindow* wxpeer = GetWXPeer();
703 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
706 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
707 superimpl(slf, (SEL)_cmd);
711 [slf addCursorRect: [slf bounds]
717 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
723 #define OSX_DEBUG_DRAWING 0
725 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
727 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
728 CGContextSaveGState( context );
730 #if OSX_DEBUG_DRAWING
731 CGContextBeginPath( context );
732 CGContextMoveToPoint(context, 0, 0);
733 NSRect bounds = [self bounds];
734 CGContextAddLineToPoint(context, 10, 0);
735 CGContextMoveToPoint(context, 0, 0);
736 CGContextAddLineToPoint(context, 0, 10);
737 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
738 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
739 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
740 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
741 CGContextClosePath( context );
742 CGContextStrokePath(context);
747 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
748 CGContextScaleCTM( context, 1, -1 );
755 [slf getRectsBeingDrawn:&rects count:&count];
756 for ( int i = 0 ; i < count ; ++i )
758 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
761 wxWindow* wxpeer = GetWXPeer();
762 wxpeer->GetUpdateRegion() = updateRgn;
763 wxpeer->MacSetCGContextRef( context );
765 bool handled = wxpeer->MacDoRedraw( 0 );
767 CGContextRestoreGState( context );
769 CGContextSaveGState( context );
773 SEL _cmd = @selector(drawRect:);
774 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
775 superimpl(slf, _cmd, *(NSRect*)rect);
776 CGContextRestoreGState( context );
777 CGContextSaveGState( context );
779 wxpeer->MacPaintChildrenBorders();
780 wxpeer->MacSetCGContextRef( NULL );
781 CGContextRestoreGState( context );
784 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
786 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
788 wxpeer->OSXHandleClicked(0);
791 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
797 #if OBJC_API_VERSION >= 2
799 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
800 class_addMethod(c, s, i, t );
804 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
809 void wxOSXCocoaClassAddWXMethods(Class c)
812 #if OBJC_API_VERSION < 2
813 static objc_method wxmethods[] =
817 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
818 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
819 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
821 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
822 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
823 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
825 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
827 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
828 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
829 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
831 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
832 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
833 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
835 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
836 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
837 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
839 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
842 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
843 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
844 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
846 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
847 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
849 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
850 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
852 #if wxUSE_DRAG_AND_DROP
853 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
854 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
855 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
856 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
859 #if OBJC_API_VERSION < 2
861 static int method_count = WXSIZEOF( wxmethods );
862 static objc_method_list *wxmethodlist = NULL;
863 if ( wxmethodlist == NULL )
865 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
866 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
867 wxmethodlist->method_count = method_count;
868 wxmethodlist->obsolete = 0;
870 class_addMethods( c, wxmethodlist );
875 // C++ implementation class
878 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
880 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
881 wxWidgetImpl( peer, isRootControl )
887 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
892 void wxWidgetCocoaImpl::Init()
898 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
900 RemoveAssociations( this );
902 if ( !IsRootControl() )
904 NSView *sv = [m_osxView superview];
906 [m_osxView removeFromSuperview];
911 bool wxWidgetCocoaImpl::IsVisible() const
913 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
916 void wxWidgetCocoaImpl::SetVisibility( bool visible )
918 [m_osxView setHidden:(visible ? NO:YES)];
921 void wxWidgetCocoaImpl::Raise()
925 void wxWidgetCocoaImpl::Lower()
929 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
934 // We should do something like this, but it wasn't working in 10.4.
935 if (GetNeedsDisplay() )
939 NSRect r = wxToNSRect( [m_osxView superview], *rect );
940 NSSize offset = NSMakeSize((float)dx, (float)dy);
941 [m_osxView scrollRect:r by:offset];
945 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
947 wxWindowMac* parent = GetWXPeer()->GetParent();
948 // under Cocoa we might have a contentView in the wxParent to which we have to
949 // adjust the coordinates
952 int cx = 0,cy = 0,cw = 0,ch = 0;
953 if ( parent->GetPeer() )
955 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
960 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
961 [m_osxView setFrame:r];
964 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
966 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
971 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
973 NSRect rect = [m_osxView frame];
974 width = rect.size.width;
975 height = rect.size.height;
978 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
980 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
982 NSView* cv = [m_osxView contentView];
984 NSRect bounds = [m_osxView bounds];
985 NSRect rect = [cv frame];
987 int y = rect.origin.y;
988 int x = rect.origin.x;
989 if ( ![ m_osxView isFlipped ] )
990 y = bounds.size.height - (rect.origin.y + rect.size.height);
993 width = rect.size.width;
994 height = rect.size.height;
999 GetSize( width, height );
1003 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1006 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1008 [m_osxView setNeedsDisplay:YES];
1011 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1013 return [m_osxView needsDisplay];
1016 bool wxWidgetCocoaImpl::CanFocus() const
1018 return [m_osxView canBecomeKeyView] == YES;
1021 bool wxWidgetCocoaImpl::HasFocus() const
1023 return ( [[m_osxView window] firstResponder] == m_osxView );
1026 bool wxWidgetCocoaImpl::SetFocus()
1028 if ( [m_osxView canBecomeKeyView] == NO )
1031 [[m_osxView window] makeFirstResponder: m_osxView] ;
1036 void wxWidgetCocoaImpl::RemoveFromParent()
1038 [m_osxView removeFromSuperview];
1041 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1043 NSView* container = parent->GetWXWidget() ;
1044 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1045 [container addSubview:m_osxView];
1048 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1050 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1053 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1055 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1057 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1058 [m_osxView setTitle:cf.AsNSString()];
1060 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1062 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1063 [m_osxView setStringValue:cf.AsNSString()];
1068 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1070 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1071 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1072 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1075 wxInt32 wxWidgetCocoaImpl::GetValue() const
1077 return [(NSControl*)m_osxView intValue];
1080 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1082 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1084 [m_osxView setIntValue:v];
1086 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1088 [m_osxView setFloatValue:(double)v];
1090 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1092 [m_osxView setDoubleValue:(double)v];
1096 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1098 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1100 [m_osxView setMinValue:(double)v];
1104 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1106 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1108 [m_osxView setMaxValue:(double)v];
1112 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1114 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1116 return [m_osxView minValue];
1121 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1123 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1125 return [m_osxView maxValue];
1130 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1132 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1134 [m_osxView setImage:bitmap.GetNSImage()];
1138 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1140 // implementation in subclass
1143 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1145 r->x = r->y = r->width = r->height = 0;
1146 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1147 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1149 NSRect former = [m_osxView frame];
1150 [m_osxView sizeToFit];
1151 NSRect best = [m_osxView frame];
1152 [m_osxView setFrame:former];
1153 r->width = best.size.width;
1154 r->height = best.size.height;
1158 bool wxWidgetCocoaImpl::IsEnabled() const
1160 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1161 return [m_osxView isEnabled];
1165 void wxWidgetCocoaImpl::Enable( bool enable )
1167 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1168 [m_osxView setEnabled:enable];
1171 void wxWidgetCocoaImpl::PulseGauge()
1175 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1179 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1181 NSControlSize size = NSRegularControlSize;
1185 case wxWINDOW_VARIANT_NORMAL :
1186 size = NSRegularControlSize;
1189 case wxWINDOW_VARIANT_SMALL :
1190 size = NSSmallControlSize;
1193 case wxWINDOW_VARIANT_MINI :
1194 size = NSMiniControlSize;
1197 case wxWINDOW_VARIANT_LARGE :
1198 size = NSRegularControlSize;
1202 wxFAIL_MSG(_T("unexpected window variant"));
1205 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1206 [m_osxView setControlSize:size];
1209 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1214 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1216 WXWidget c = control ? control : (WXWidget) m_osxView;
1217 wxWidgetImpl::Associate( c, this ) ;
1218 if ([c respondsToSelector:@selector(setAction:)])
1221 [c setAction: @selector(controlAction:)];
1222 if ([c respondsToSelector:@selector(setDoubleAction:)])
1224 [c setDoubleAction: @selector(controlDoubleAction:)];
1230 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1232 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1233 SetupKeyEvent( wxevent, event );
1235 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1238 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1240 NSPoint clickLocation;
1241 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1242 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1243 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1244 SetupMouseEvent( wxevent , event ) ;
1248 return GetWXPeer()->HandleWindowEvent(wxevent);
1251 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1253 wxWindow* thisWindow = GetWXPeer();
1254 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1256 thisWindow->MacInvalidateBorders();
1259 if ( receivedFocus )
1261 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1262 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1263 thisWindow->HandleWindowEvent(eventFocus);
1266 if ( thisWindow->GetCaret() )
1267 thisWindow->GetCaret()->OnSetFocus();
1270 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1271 event.SetEventObject(thisWindow);
1272 // TODO how to find out the targetFocusWindow ?
1273 // event.SetWindow(targetFocusWindow);
1274 thisWindow->HandleWindowEvent(event) ;
1276 else // !receivedFocuss
1279 if ( thisWindow->GetCaret() )
1280 thisWindow->GetCaret()->OnKillFocus();
1283 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1285 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1286 event.SetEventObject(thisWindow);
1287 // TODO how to find out the targetFocusWindow ?
1288 // event.SetWindow(targetFocusWindow);
1289 thisWindow->HandleWindowEvent(event) ;
1293 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1295 NSPoint location = [NSEvent mouseLocation];
1296 location = [[m_osxView window] convertScreenToBase:location];
1297 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1299 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1301 [(NSCursor*)cursor.GetHCURSOR() set];
1303 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1306 void wxWidgetCocoaImpl::CaptureMouse()
1308 [[m_osxView window] disableCursorRects];
1311 void wxWidgetCocoaImpl::ReleaseMouse()
1313 [[m_osxView window] enableCursorRects];
1316 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1318 m_isFlipped = flipped;
1325 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1326 long style, long extraStyle)
1328 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1329 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1331 // temporary hook for dnd
1332 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1333 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1335 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1339 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1341 NSWindow* tlw = now->GetWXWindow();
1342 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1343 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1344 c->InstallEventHandler();
1345 [tlw setContentView:v];