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/nonownedwnd.h"
20 #include "wx/osx/private.h"
27 #if wxUSE_DRAG_AND_DROP
31 #include <objc/objc-runtime.h>
33 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
37 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
38 wxRect bounds(x,y,w,h);
39 NSView* sv = (window->GetParent()->GetHandle() );
41 return wxToNSRect( sv, bounds );
44 @interface wxNSView : NSView
48 - (BOOL) canBecomeKeyView;
52 @interface NSView(PossibleMethods)
53 - (void)setTitle:(NSString *)aString;
54 - (void)setStringValue:(NSString *)aString;
55 - (void)setIntValue:(int)anInt;
56 - (void)setFloatValue:(float)aFloat;
57 - (void)setDoubleValue:(double)aDouble;
61 - (void)setMinValue:(double)aDouble;
62 - (void)setMaxValue:(double)aDouble;
67 - (void)setEnabled:(BOOL)flag;
69 - (void)setImage:(NSImage *)image;
70 - (void)setControlSize:(NSControlSize)size;
74 - (void)setTarget:(id)anObject;
75 - (void)setAction:(SEL)aSelector;
76 - (void)setDoubleAction:(SEL)aSelector;
79 long wxOSXTranslateCocoaKey( int unichar )
81 long retval = unichar;
84 case NSUpArrowFunctionKey :
87 case NSDownArrowFunctionKey :
90 case NSLeftArrowFunctionKey :
93 case NSRightArrowFunctionKey :
96 case NSInsertFunctionKey :
99 case NSDeleteFunctionKey :
102 case NSHomeFunctionKey :
105 // case NSBeginFunctionKey :
106 // retval = WXK_BEGIN;
108 case NSEndFunctionKey :
111 case NSPageUpFunctionKey :
114 case NSPageDownFunctionKey :
115 retval = WXK_PAGEDOWN;
117 case NSHelpFunctionKey :
122 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
123 retval = WXK_F1 + (unichar - NSF1FunctionKey );
129 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
131 UInt32 modifiers = [nsEvent modifierFlags] ;
132 int eventType = [nsEvent type];
134 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
135 wxevent.m_controlDown = modifiers & NSControlKeyMask;
136 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
137 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
139 wxevent.m_rawCode = [nsEvent keyCode];
140 wxevent.m_rawFlags = modifiers;
142 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
146 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
149 wxevent.SetEventType( wxEVT_KEY_UP ) ;
151 case NSFlagsChanged :
152 // setup common code here
159 if ( eventType != NSFlagsChanged )
161 NSString* nschars = [nsEvent characters];
164 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
165 chars = cfchars.AsString();
169 int unichar = chars.Length() > 0 ? chars[0] : 0;
170 long keyval = wxOSXTranslateCocoaKey(unichar) ;
171 if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
172 keyval = wxToupper( keyval ) ;
175 wxevent.m_uniChar = unichar;
177 wxevent.m_keyCode = keyval;
180 UInt32 g_lastButton = 0 ;
181 bool g_lastButtonWasFakeRight = false ;
183 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
185 int eventType = [nsEvent type];
186 UInt32 modifiers = [nsEvent modifierFlags] ;
187 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
189 // these parameters are not given for all events
190 UInt32 button = [nsEvent buttonNumber];
191 UInt32 clickCount = 0;
192 if ( eventType != NSScrollWheel )
193 [nsEvent clickCount];
195 wxevent.m_x = screenMouseLocation.x;
196 wxevent.m_y = screenMouseLocation.y;
197 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
198 wxevent.m_controlDown = modifiers & NSControlKeyMask;
199 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
200 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
201 wxevent.m_clickCount = clickCount;
202 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
204 UInt32 mouseChord = 0;
208 case NSLeftMouseDown :
209 case NSLeftMouseDragged :
212 case NSRightMouseDown :
213 case NSRightMouseDragged :
216 case NSOtherMouseDown :
217 case NSOtherMouseDragged :
222 // a control click is interpreted as a right click
223 bool thisButtonIsFakeRight = false ;
224 if ( button == 0 && (modifiers & NSControlKeyMask) )
227 thisButtonIsFakeRight = true ;
230 // otherwise we report double clicks by connecting a left click with a ctrl-left click
231 if ( clickCount > 1 && button != g_lastButton )
234 // we must make sure that our synthetic 'right' button corresponds in
235 // mouse down, moved and mouse up, and does not deliver a right down and left up
238 case NSLeftMouseDown :
239 case NSRightMouseDown :
240 case NSOtherMouseDown :
241 g_lastButton = button ;
242 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
249 g_lastButtonWasFakeRight = false ;
251 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
252 button = g_lastButton ;
254 // Adjust the chord mask to remove the primary button and add the
255 // secondary button. It is possible that the secondary button is
256 // already pressed, e.g. on a mouse connected to a laptop, but this
257 // possibility is ignored here:
258 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
259 mouseChord = ((mouseChord & ~1U) | 2U);
262 wxevent.m_leftDown = true ;
264 wxevent.m_rightDown = true ;
266 wxevent.m_middleDown = true ;
268 // translate into wx types
271 case NSLeftMouseDown :
272 case NSRightMouseDown :
273 case NSOtherMouseDown :
277 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
281 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
285 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
294 case NSRightMouseUp :
295 case NSOtherMouseUp :
299 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
303 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
307 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
317 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
318 wxevent.m_wheelDelta = 1;
319 wxevent.m_linesPerAction = 1;
320 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
322 wxevent.m_wheelAxis = 1;
323 wxevent.m_wheelRotation = [nsEvent deltaX] > 0.0 ? 1 : -1;
327 wxevent.m_wheelRotation = [nsEvent deltaY] > 0.0 ? 1 : -1;
332 case NSMouseEntered :
334 case NSLeftMouseDragged :
335 case NSRightMouseDragged :
336 case NSOtherMouseDragged :
338 wxevent.SetEventType( wxEVT_MOTION ) ;
345 @implementation wxNSView
349 static BOOL initialized = NO;
353 wxOSXCocoaClassAddWXMethods( self );
357 - (BOOL) canBecomeKeyView
368 #if wxUSE_DRAG_AND_DROP
370 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
371 // for details on the NSPasteboard -> PasteboardRef conversion
373 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
375 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
377 return NSDragOperationNone;
379 return impl->draggingEntered(sender, self, _cmd);
382 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
384 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
388 return impl->draggingExited(sender, self, _cmd);
391 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
393 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
395 return NSDragOperationNone;
397 return impl->draggingUpdated(sender, self, _cmd);
400 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
402 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
404 return NSDragOperationNone;
406 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
409 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
411 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
415 impl->mouseEvent(event, self, _cmd);
418 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
420 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
424 impl->keyEvent(event, self, _cmd);
427 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
429 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
433 return impl->performKeyEquivalent(event, self, _cmd);
436 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
438 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
442 return impl->becomeFirstResponder(self, _cmd);
445 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
447 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
451 return impl->resignFirstResponder(self, _cmd);
454 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
456 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
460 impl->resetCursorRects(self, _cmd);
463 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
465 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
469 return impl->isFlipped(self, _cmd) ? YES:NO;
472 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
474 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
478 return impl->drawRect(&rect, self, _cmd);
481 void wxOSX_clickedAction(NSView* self, SEL _cmd, id sender)
483 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
487 impl->clickedAction(self, _cmd, sender);
490 void wxOSX_doubleClickedAction(NSView* self, SEL _cmd, id sender)
492 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
496 impl->doubleClickedAction(self, _cmd, sender);
499 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
501 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
502 NSPasteboard *pboard = [sender draggingPasteboard];
503 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
505 wxWindow* wxpeer = GetWXPeer();
506 if ( wxpeer == NULL )
507 return NSDragOperationNone;
509 wxDropTarget* target = wxpeer->GetDropTarget();
510 if ( target == NULL )
511 return NSDragOperationNone;
513 wxDragResult result = wxDragNone;
514 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
516 if ( sourceDragMask & NSDragOperationLink )
518 else if ( sourceDragMask & NSDragOperationCopy )
520 else if ( sourceDragMask & NSDragOperationMove )
523 PasteboardRef pboardRef;
524 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
525 target->SetCurrentDragPasteboard(pboardRef);
526 result = target->OnEnter(pt.x, pt.y, result);
527 CFRelease(pboardRef);
529 NSDragOperation nsresult = NSDragOperationNone;
533 nsresult = NSDragOperationLink;
535 nsresult = NSDragOperationMove;
537 nsresult = NSDragOperationCopy;
544 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
546 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
547 NSPasteboard *pboard = [sender draggingPasteboard];
549 wxWindow* wxpeer = GetWXPeer();
550 if ( wxpeer == NULL )
553 wxDropTarget* target = wxpeer->GetDropTarget();
554 if ( target == NULL )
557 PasteboardRef pboardRef;
558 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
559 target->SetCurrentDragPasteboard(pboardRef);
561 CFRelease(pboardRef);
564 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
566 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
567 NSPasteboard *pboard = [sender draggingPasteboard];
568 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
570 wxWindow* wxpeer = GetWXPeer();
571 if ( wxpeer == NULL )
572 return NSDragOperationNone;
574 wxDropTarget* target = wxpeer->GetDropTarget();
575 if ( target == NULL )
576 return NSDragOperationNone;
578 wxDragResult result = wxDragNone;
579 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
581 if ( sourceDragMask & NSDragOperationLink )
583 else if ( sourceDragMask & NSDragOperationCopy )
585 else if ( sourceDragMask & NSDragOperationMove )
588 PasteboardRef pboardRef;
589 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
590 target->SetCurrentDragPasteboard(pboardRef);
591 result = target->OnDragOver(pt.x, pt.y, result);
592 CFRelease(pboardRef);
594 NSDragOperation nsresult = NSDragOperationNone;
598 nsresult = NSDragOperationLink;
600 nsresult = NSDragOperationMove;
602 nsresult = NSDragOperationCopy;
609 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
611 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
613 NSPasteboard *pboard = [sender draggingPasteboard];
614 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
616 wxWindow* wxpeer = GetWXPeer();
617 wxDropTarget* target = wxpeer->GetDropTarget();
618 wxDragResult result = wxDragNone;
619 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
621 if ( sourceDragMask & NSDragOperationLink )
623 else if ( sourceDragMask & NSDragOperationCopy )
625 else if ( sourceDragMask & NSDragOperationMove )
628 PasteboardRef pboardRef;
629 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
630 target->SetCurrentDragPasteboard(pboardRef);
631 result = target->OnData(pt.x, pt.y, result);
632 CFRelease(pboardRef);
634 return result != wxDragNone;
639 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
640 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
641 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
642 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
643 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
645 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
647 if ( !DoHandleMouseEvent(event) )
649 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
650 superimpl(slf, (SEL)_cmd, event);
654 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
656 if ( !DoHandleKeyEvent(event) )
658 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
659 superimpl(slf, (SEL)_cmd, event);
663 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
665 if ( !DoHandleKeyEvent(event) )
667 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
668 return superimpl(slf, (SEL)_cmd, event);
674 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
676 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
677 BOOL r = superimpl(slf, (SEL)_cmd);
679 DoNotifyFocusEvent( true );
683 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
685 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
686 BOOL r = superimpl(slf, (SEL)_cmd);
688 DoNotifyFocusEvent( false );
692 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
694 wxWindow* wxpeer = GetWXPeer();
697 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
700 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
701 superimpl(slf, (SEL)_cmd);
704 [slf addCursorRect: [slf bounds]
709 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
715 #define OSX_DEBUG_DRAWING 0
717 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
719 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
720 CGContextSaveGState( context );
722 #if OSX_DEBUG_DRAWING
723 CGContextBeginPath( context );
724 CGContextMoveToPoint(context, 0, 0);
725 NSRect bounds = [self bounds];
726 CGContextAddLineToPoint(context, 10, 0);
727 CGContextMoveToPoint(context, 0, 0);
728 CGContextAddLineToPoint(context, 0, 10);
729 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
730 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
731 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
732 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
733 CGContextClosePath( context );
734 CGContextStrokePath(context);
739 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
740 CGContextScaleCTM( context, 1, -1 );
747 [slf getRectsBeingDrawn:&rects count:&count];
748 for ( int i = 0 ; i < count ; ++i )
750 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
753 wxWindow* wxpeer = GetWXPeer();
754 wxpeer->GetUpdateRegion() = updateRgn;
755 wxpeer->MacSetCGContextRef( context );
758 event.SetTimestamp(0); // todo
759 event.SetEventObject(wxpeer);
760 bool handled = wxpeer->HandleWindowEvent(event);
762 CGContextRestoreGState( context );
767 SEL _cmd = @selector(drawRect:);
768 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
769 superimpl(slf, _cmd, *(NSRect*)rect);
773 void wxWidgetCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender)
775 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
777 wxpeer->OSXHandleClicked(0);
780 void wxWidgetCocoaImpl::doubleClickedAction( WXWidget slf, void *_cmd, void *sender)
786 #if OBJC_API_VERSION >= 2
788 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
789 class_addMethod(c, s, i, t );
793 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
798 void wxOSXCocoaClassAddWXMethods(Class c)
801 #if OBJC_API_VERSION < 2
802 static objc_method wxmethods[] =
806 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
807 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
808 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
810 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
811 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
812 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
814 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
816 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
817 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
818 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
820 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
821 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
822 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
824 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
825 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
826 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
828 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
831 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
832 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
833 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
835 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
836 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
838 wxOSX_CLASS_ADD_METHOD(c, @selector(clickedAction:), (IMP) wxOSX_clickedAction, "v@:@" )
839 wxOSX_CLASS_ADD_METHOD(c, @selector(doubleClickedAction:), (IMP) wxOSX_doubleClickedAction, "v@:@" )
841 #if wxUSE_DRAG_AND_DROP
842 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
843 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
844 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
845 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
848 #if OBJC_API_VERSION < 2
850 static int method_count = WXSIZEOF( wxmethods );
851 static objc_method_list *wxmethodlist = NULL;
852 if ( wxmethodlist == NULL )
854 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
855 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
856 wxmethodlist->method_count = method_count;
857 wxmethodlist->obsolete = 0;
859 class_addMethods( c, wxmethodlist );
864 // C++ implementation class
867 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
869 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
870 wxWidgetImpl( peer, isRootControl )
876 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
881 void wxWidgetCocoaImpl::Init()
887 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
889 RemoveAssociations( this );
891 if ( !IsRootControl() )
893 NSView *sv = [m_osxView superview];
895 [m_osxView removeFromSuperview];
900 bool wxWidgetCocoaImpl::IsVisible() const
902 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
905 void wxWidgetCocoaImpl::SetVisibility( bool visible )
907 [m_osxView setHidden:(visible ? NO:YES)];
910 void wxWidgetCocoaImpl::Raise()
914 void wxWidgetCocoaImpl::Lower()
918 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
923 // We should do something like this, but it wasn't working in 10.4.
924 if (GetNeedsDisplay() )
928 NSRect r = wxToNSRect( [m_osxView superview], *rect );
929 NSSize offset = NSMakeSize((float)dx, (float)dy);
930 [m_osxView scrollRect:r by:offset];
934 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
936 wxWindowMac* parent = GetWXPeer()->GetParent();
937 // under Cocoa we might have a contentView in the wxParent to which we have to
938 // adjust the coordinates
941 wxPoint pt(parent->GetClientAreaOrigin());
945 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
946 [m_osxView setFrame:r];
949 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
951 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
956 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
958 NSRect rect = [m_osxView frame];
959 width = rect.size.width;
960 height = rect.size.height;
963 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
965 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
967 NSView* cv = [m_osxView contentView];
969 NSRect bounds = [m_osxView bounds];
970 NSRect rect = [cv frame];
972 int y = rect.origin.y;
973 int x = rect.origin.x;
974 if ( ![ m_osxView isFlipped ] )
975 y = bounds.size.height - (rect.origin.y + rect.size.height);
978 width = rect.size.width;
979 height = rect.size.height;
984 GetSize( width, height );
988 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
991 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
993 [m_osxView setNeedsDisplay:YES];
996 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
998 return [m_osxView needsDisplay];
1001 bool wxWidgetCocoaImpl::CanFocus() const
1003 return [m_osxView canBecomeKeyView] == YES;
1006 bool wxWidgetCocoaImpl::HasFocus() const
1008 return ( [[m_osxView window] firstResponder] == m_osxView );
1011 bool wxWidgetCocoaImpl::SetFocus()
1013 if ( [m_osxView canBecomeKeyView] == NO )
1016 [[m_osxView window] makeFirstResponder: m_osxView] ;
1021 void wxWidgetCocoaImpl::RemoveFromParent()
1023 [m_osxView removeFromSuperview];
1026 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1028 NSView* container = parent->GetWXWidget() ;
1029 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1030 [container addSubview:m_osxView];
1033 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1035 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1038 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1040 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1042 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1043 [m_osxView setTitle:cf.AsNSString()];
1048 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1050 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1051 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1052 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1055 wxInt32 wxWidgetCocoaImpl::GetValue() const
1057 return [(NSControl*)m_osxView intValue];
1060 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1062 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1064 [m_osxView setIntValue:v];
1066 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1068 [m_osxView setFloatValue:(double)v];
1070 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1072 [m_osxView setDoubleValue:(double)v];
1076 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1078 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1080 [m_osxView setMinValue:(double)v];
1084 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1086 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1088 [m_osxView setMaxValue:(double)v];
1092 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1094 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1096 return [m_osxView minValue];
1101 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1103 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1105 return [m_osxView maxValue];
1110 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1112 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1114 [m_osxView setImage:bitmap.GetNSImage()];
1118 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1120 // implementation in subclass
1123 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1125 r->x = r->y = r->width = r->height = 0;
1126 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1127 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1129 NSRect former = [m_osxView frame];
1130 [m_osxView sizeToFit];
1131 NSRect best = [m_osxView frame];
1132 [m_osxView setFrame:former];
1133 r->width = best.size.width;
1134 r->height = best.size.height;
1138 bool wxWidgetCocoaImpl::IsEnabled() const
1140 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1141 return [m_osxView isEnabled];
1145 void wxWidgetCocoaImpl::Enable( bool enable )
1147 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1148 [m_osxView setEnabled:enable];
1151 void wxWidgetCocoaImpl::PulseGauge()
1155 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1159 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1161 NSControlSize size = NSRegularControlSize;
1165 case wxWINDOW_VARIANT_NORMAL :
1166 size = NSRegularControlSize;
1169 case wxWINDOW_VARIANT_SMALL :
1170 size = NSSmallControlSize;
1173 case wxWINDOW_VARIANT_MINI :
1174 size = NSMiniControlSize;
1177 case wxWINDOW_VARIANT_LARGE :
1178 size = NSRegularControlSize;
1182 wxFAIL_MSG(_T("unexpected window variant"));
1185 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1186 [m_osxView setControlSize:size];
1189 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1194 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1196 WXWidget c = control ? control : (WXWidget) m_osxView;
1197 wxWidgetImpl::Associate( c, this ) ;
1198 if ([c respondsToSelector:@selector(setAction:)])
1201 [c setAction: @selector(clickedAction:)];
1202 if ([c respondsToSelector:@selector(setDoubleAction:)])
1204 [c setDoubleAction: @selector(doubleClickedAction:)];
1210 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1212 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1213 SetupKeyEvent( wxevent, event );
1215 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1218 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1220 NSPoint clickLocation;
1221 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1222 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1223 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1224 SetupMouseEvent( wxevent , event ) ;
1228 return GetWXPeer()->HandleWindowEvent(wxevent);
1231 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1233 wxWindow* thisWindow = GetWXPeer();
1234 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1236 thisWindow->MacInvalidateBorders();
1239 if ( receivedFocus )
1241 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1242 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1243 thisWindow->HandleWindowEvent(eventFocus);
1246 if ( thisWindow->GetCaret() )
1247 thisWindow->GetCaret()->OnSetFocus();
1250 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1251 event.SetEventObject(thisWindow);
1252 // TODO how to find out the targetFocusWindow ?
1253 // event.SetWindow(targetFocusWindow);
1254 thisWindow->HandleWindowEvent(event) ;
1256 else // !receivedFocuss
1259 if ( thisWindow->GetCaret() )
1260 thisWindow->GetCaret()->OnKillFocus();
1263 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1265 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1266 event.SetEventObject(thisWindow);
1267 // TODO how to find out the targetFocusWindow ?
1268 // event.SetWindow(targetFocusWindow);
1269 thisWindow->HandleWindowEvent(event) ;
1273 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1275 NSPoint location = [NSEvent mouseLocation];
1276 location = [[m_osxView window] convertScreenToBase:location];
1277 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1279 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1281 [(NSCursor*)cursor.GetHCURSOR() set];
1283 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1286 void wxWidgetCocoaImpl::CaptureMouse()
1288 [[m_osxView window] disableCursorRects];
1291 void wxWidgetCocoaImpl::ReleaseMouse()
1293 [[m_osxView window] enableCursorRects];
1296 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1298 m_isFlipped = flipped;
1305 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1306 long style, long extraStyle)
1308 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1309 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1311 // temporary hook for dnd
1312 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1313 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1315 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1319 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1321 NSWindow* tlw = now->GetWXWindow();
1322 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1323 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1324 [tlw setContentView:v];