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 UInt32 modifiers = [nsEvent modifierFlags] ;
186 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
188 // these parameters are not given for all events
189 UInt32 button = [nsEvent buttonNumber];
190 UInt32 clickCount = [nsEvent clickCount];
192 wxevent.m_x = screenMouseLocation.x;
193 wxevent.m_y = screenMouseLocation.y;
194 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
195 wxevent.m_controlDown = modifiers & NSControlKeyMask;
196 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
197 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
198 wxevent.m_clickCount = clickCount;
199 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
201 UInt32 mouseChord = 0;
202 int eventType = [nsEvent type];
206 case NSLeftMouseDown :
207 case NSLeftMouseDragged :
210 case NSRightMouseDown :
211 case NSRightMouseDragged :
214 case NSOtherMouseDown :
215 case NSOtherMouseDragged :
220 // a control click is interpreted as a right click
221 bool thisButtonIsFakeRight = false ;
222 if ( button == 0 && (modifiers & NSControlKeyMask) )
225 thisButtonIsFakeRight = true ;
228 // otherwise we report double clicks by connecting a left click with a ctrl-left click
229 if ( clickCount > 1 && button != g_lastButton )
232 // we must make sure that our synthetic 'right' button corresponds in
233 // mouse down, moved and mouse up, and does not deliver a right down and left up
236 case NSLeftMouseDown :
237 case NSRightMouseDown :
238 case NSOtherMouseDown :
239 g_lastButton = button ;
240 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
247 g_lastButtonWasFakeRight = false ;
249 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
250 button = g_lastButton ;
252 // Adjust the chord mask to remove the primary button and add the
253 // secondary button. It is possible that the secondary button is
254 // already pressed, e.g. on a mouse connected to a laptop, but this
255 // possibility is ignored here:
256 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
257 mouseChord = ((mouseChord & ~1U) | 2U);
260 wxevent.m_leftDown = true ;
262 wxevent.m_rightDown = true ;
264 wxevent.m_middleDown = true ;
266 // translate into wx types
269 case NSLeftMouseDown :
270 case NSRightMouseDown :
271 case NSOtherMouseDown :
275 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
279 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
283 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
292 case NSRightMouseUp :
293 case NSOtherMouseUp :
297 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
301 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
305 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
315 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
317 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
318 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
320 wxevent.m_wheelRotation = 10; // delta;
321 wxevent.m_wheelDelta = 1;
322 wxevent.m_linesPerAction = 1;
323 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
324 wxevent.m_wheelAxis = 1;
328 case NSMouseEntered :
330 case NSLeftMouseDragged :
331 case NSRightMouseDragged :
332 case NSOtherMouseDragged :
334 wxevent.SetEventType( wxEVT_MOTION ) ;
341 @implementation wxNSView
345 static BOOL initialized = NO;
349 wxOSXCocoaClassAddWXMethods( self );
353 - (BOOL) canBecomeKeyView
364 #if wxUSE_DRAG_AND_DROP
366 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
367 // for details on the NSPasteboard -> PasteboardRef conversion
369 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
371 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
373 return NSDragOperationNone;
375 return impl->draggingEntered(sender, self, _cmd);
378 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
380 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
384 return impl->draggingExited(sender, self, _cmd);
387 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
389 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
391 return NSDragOperationNone;
393 return impl->draggingUpdated(sender, self, _cmd);
396 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
398 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
400 return NSDragOperationNone;
402 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
405 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
407 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
411 impl->mouseEvent(event, self, _cmd);
414 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
416 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
420 impl->keyEvent(event, self, _cmd);
423 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
425 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
429 return impl->performKeyEquivalent(event, self, _cmd);
432 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
434 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
438 return impl->becomeFirstResponder(self, _cmd);
441 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
443 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
447 return impl->resignFirstResponder(self, _cmd);
450 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
452 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
456 impl->resetCursorRects(self, _cmd);
459 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
461 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
465 return impl->isFlipped(self, _cmd) ? YES:NO;
468 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
470 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
474 return impl->drawRect(&rect, self, _cmd);
477 void wxOSX_clickedAction(NSView* self, SEL _cmd, id sender)
479 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
483 impl->clickedAction(self, _cmd, sender);
486 void wxOSX_doubleClickedAction(NSView* self, SEL _cmd, id sender)
488 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
492 impl->doubleClickedAction(self, _cmd, sender);
495 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
497 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
498 NSPasteboard *pboard = [sender draggingPasteboard];
499 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
501 wxWindow* wxpeer = GetWXPeer();
502 if ( wxpeer == NULL )
503 return NSDragOperationNone;
505 wxDropTarget* target = wxpeer->GetDropTarget();
506 if ( target == NULL )
507 return NSDragOperationNone;
509 wxDragResult result = wxDragNone;
510 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
512 if ( sourceDragMask & NSDragOperationLink )
514 else if ( sourceDragMask & NSDragOperationCopy )
516 else if ( sourceDragMask & NSDragOperationMove )
519 PasteboardRef pboardRef;
520 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
521 target->SetCurrentDragPasteboard(pboardRef);
522 result = target->OnEnter(pt.x, pt.y, result);
523 CFRelease(pboardRef);
525 NSDragOperation nsresult = NSDragOperationNone;
529 nsresult = NSDragOperationLink;
531 nsresult = NSDragOperationMove;
533 nsresult = NSDragOperationCopy;
540 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
542 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
543 NSPasteboard *pboard = [sender draggingPasteboard];
545 wxWindow* wxpeer = GetWXPeer();
546 if ( wxpeer == NULL )
549 wxDropTarget* target = wxpeer->GetDropTarget();
550 if ( target == NULL )
553 PasteboardRef pboardRef;
554 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
555 target->SetCurrentDragPasteboard(pboardRef);
557 CFRelease(pboardRef);
560 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
562 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
563 NSPasteboard *pboard = [sender draggingPasteboard];
564 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
566 wxWindow* wxpeer = GetWXPeer();
567 if ( wxpeer == NULL )
568 return NSDragOperationNone;
570 wxDropTarget* target = wxpeer->GetDropTarget();
571 if ( target == NULL )
572 return NSDragOperationNone;
574 wxDragResult result = wxDragNone;
575 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
577 if ( sourceDragMask & NSDragOperationLink )
579 else if ( sourceDragMask & NSDragOperationCopy )
581 else if ( sourceDragMask & NSDragOperationMove )
584 PasteboardRef pboardRef;
585 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
586 target->SetCurrentDragPasteboard(pboardRef);
587 result = target->OnDragOver(pt.x, pt.y, result);
588 CFRelease(pboardRef);
590 NSDragOperation nsresult = NSDragOperationNone;
594 nsresult = NSDragOperationLink;
596 nsresult = NSDragOperationMove;
598 nsresult = NSDragOperationCopy;
605 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
607 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
609 NSPasteboard *pboard = [sender draggingPasteboard];
610 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
612 wxWindow* wxpeer = GetWXPeer();
613 wxDropTarget* target = wxpeer->GetDropTarget();
614 wxDragResult result = wxDragNone;
615 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
617 if ( sourceDragMask & NSDragOperationLink )
619 else if ( sourceDragMask & NSDragOperationCopy )
621 else if ( sourceDragMask & NSDragOperationMove )
624 PasteboardRef pboardRef;
625 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
626 target->SetCurrentDragPasteboard(pboardRef);
627 result = target->OnData(pt.x, pt.y, result);
628 CFRelease(pboardRef);
630 return result != wxDragNone;
635 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
636 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
637 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
638 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
639 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
641 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
643 if ( !DoHandleMouseEvent(event) )
645 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
646 superimpl(slf, (SEL)_cmd, event);
650 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
652 if ( !DoHandleKeyEvent(event) )
654 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
655 superimpl(slf, (SEL)_cmd, event);
659 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
661 if ( !DoHandleKeyEvent(event) )
663 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
664 return superimpl(slf, (SEL)_cmd, event);
670 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
672 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
673 BOOL r = superimpl(slf, (SEL)_cmd);
675 DoNotifyFocusEvent( true );
679 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
681 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
682 BOOL r = superimpl(slf, (SEL)_cmd);
684 DoNotifyFocusEvent( false );
688 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
690 wxWindow* wxpeer = GetWXPeer();
693 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
696 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
697 superimpl(slf, (SEL)_cmd);
700 [slf addCursorRect: [slf bounds]
705 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
711 #define OSX_DEBUG_DRAWING 0
713 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
715 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
716 CGContextSaveGState( context );
718 #if OSX_DEBUG_DRAWING
719 CGContextBeginPath( context );
720 CGContextMoveToPoint(context, 0, 0);
721 NSRect bounds = [self bounds];
722 CGContextAddLineToPoint(context, 10, 0);
723 CGContextMoveToPoint(context, 0, 0);
724 CGContextAddLineToPoint(context, 0, 10);
725 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
726 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
727 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
728 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
729 CGContextClosePath( context );
730 CGContextStrokePath(context);
735 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
736 CGContextScaleCTM( context, 1, -1 );
743 [slf getRectsBeingDrawn:&rects count:&count];
744 for ( int i = 0 ; i < count ; ++i )
746 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
749 wxWindow* wxpeer = GetWXPeer();
750 wxpeer->GetUpdateRegion() = updateRgn;
751 wxpeer->MacSetCGContextRef( context );
754 event.SetTimestamp(0); // todo
755 event.SetEventObject(wxpeer);
756 bool handled = wxpeer->HandleWindowEvent(event);
758 CGContextRestoreGState( context );
763 SEL _cmd = @selector(drawRect:);
764 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
765 superimpl(slf, _cmd, *(NSRect*)rect);
769 void wxWidgetCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender)
771 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
773 wxpeer->OSXHandleClicked(0);
776 void wxWidgetCocoaImpl::doubleClickedAction( WXWidget slf, void *_cmd, void *sender)
782 #if OBJC_API_VERSION >= 2
784 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
785 class_addMethod(c, s, i, t );
789 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
794 void wxOSXCocoaClassAddWXMethods(Class c)
797 #if OBJC_API_VERSION < 2
798 static objc_method wxmethods[] =
802 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
803 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
804 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
806 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
807 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
808 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
810 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
812 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
813 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
814 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
816 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
817 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
818 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
820 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
821 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
822 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
824 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
827 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
828 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
829 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
831 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
832 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
834 wxOSX_CLASS_ADD_METHOD(c, @selector(clickedAction:), (IMP) wxOSX_clickedAction, "v@:@" )
835 wxOSX_CLASS_ADD_METHOD(c, @selector(doubleClickedAction:), (IMP) wxOSX_doubleClickedAction, "v@:@" )
837 #if wxUSE_DRAG_AND_DROP
838 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
839 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
840 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
841 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
844 #if OBJC_API_VERSION < 2
846 static int method_count = WXSIZEOF( wxmethods );
847 static objc_method_list *wxmethodlist = NULL;
848 if ( wxmethodlist == NULL )
850 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
851 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
852 wxmethodlist->method_count = method_count;
853 wxmethodlist->obsolete = 0;
855 class_addMethods( c, wxmethodlist );
860 // C++ implementation class
863 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
865 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
866 wxWidgetImpl( peer, isRootControl )
872 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
877 void wxWidgetCocoaImpl::Init()
883 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
885 RemoveAssociations( this );
887 if ( !IsRootControl() )
889 NSView *sv = [m_osxView superview];
891 [m_osxView removeFromSuperview];
896 bool wxWidgetCocoaImpl::IsVisible() const
898 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
901 void wxWidgetCocoaImpl::SetVisibility( bool visible )
903 [m_osxView setHidden:(visible ? NO:YES)];
906 void wxWidgetCocoaImpl::Raise()
910 void wxWidgetCocoaImpl::Lower()
914 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
919 // We should do something like this, but it wasn't working in 10.4.
920 if (GetNeedsDisplay() )
924 NSRect r = wxToNSRect( [m_osxView superview], *rect );
925 NSSize offset = NSMakeSize((float)dx, (float)dy);
926 [m_osxView scrollRect:r by:offset];
930 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
932 wxWindowMac* parent = GetWXPeer()->GetParent();
933 // under Cocoa we might have a contentView in the wxParent to which we have to
934 // adjust the coordinates
937 wxPoint pt(parent->GetClientAreaOrigin());
941 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
942 [m_osxView setFrame:r];
945 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
947 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
952 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
954 NSRect rect = [m_osxView frame];
955 width = rect.size.width;
956 height = rect.size.height;
959 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
961 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
963 NSView* cv = [m_osxView contentView];
965 NSRect bounds = [m_osxView bounds];
966 NSRect rect = [cv frame];
968 int y = rect.origin.y;
969 int x = rect.origin.x;
970 if ( ![ m_osxView isFlipped ] )
971 y = bounds.size.height - (rect.origin.y + rect.size.height);
974 width = rect.size.width;
975 height = rect.size.height;
980 GetSize( width, height );
984 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
987 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
989 [m_osxView setNeedsDisplay:YES];
992 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
994 return [m_osxView needsDisplay];
997 bool wxWidgetCocoaImpl::CanFocus() const
999 return [m_osxView canBecomeKeyView] == YES;
1002 bool wxWidgetCocoaImpl::HasFocus() const
1004 return ( [[m_osxView window] firstResponder] == m_osxView );
1007 bool wxWidgetCocoaImpl::SetFocus()
1009 if ( [m_osxView canBecomeKeyView] == NO )
1012 [[m_osxView window] makeFirstResponder: m_osxView] ;
1017 void wxWidgetCocoaImpl::RemoveFromParent()
1019 [m_osxView removeFromSuperview];
1022 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1024 NSView* container = parent->GetWXWidget() ;
1025 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1026 [container addSubview:m_osxView];
1029 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1031 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1034 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1036 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1038 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1039 [m_osxView setTitle:cf.AsNSString()];
1044 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1046 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1047 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1048 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1051 wxInt32 wxWidgetCocoaImpl::GetValue() const
1053 return [(NSControl*)m_osxView intValue];
1056 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1058 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1060 [m_osxView setIntValue:v];
1062 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1064 [m_osxView setFloatValue:(double)v];
1066 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1068 [m_osxView setDoubleValue:(double)v];
1072 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1074 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1076 [m_osxView setMinValue:(double)v];
1080 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1082 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1084 [m_osxView setMaxValue:(double)v];
1088 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1090 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1092 return [m_osxView minValue];
1097 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1099 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1101 return [m_osxView maxValue];
1106 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1108 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1110 [m_osxView setImage:bitmap.GetNSImage()];
1114 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1116 // implementation in subclass
1119 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1121 r->x = r->y = r->width = r->height = 0;
1122 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1123 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1125 NSRect former = [m_osxView frame];
1126 [m_osxView sizeToFit];
1127 NSRect best = [m_osxView frame];
1128 [m_osxView setFrame:former];
1129 r->width = best.size.width;
1130 r->height = best.size.height;
1134 bool wxWidgetCocoaImpl::IsEnabled() const
1136 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1137 return [m_osxView isEnabled];
1141 void wxWidgetCocoaImpl::Enable( bool enable )
1143 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1144 [m_osxView setEnabled:enable];
1147 void wxWidgetCocoaImpl::PulseGauge()
1151 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1155 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1157 NSControlSize size = NSRegularControlSize;
1161 case wxWINDOW_VARIANT_NORMAL :
1162 size = NSRegularControlSize;
1165 case wxWINDOW_VARIANT_SMALL :
1166 size = NSSmallControlSize;
1169 case wxWINDOW_VARIANT_MINI :
1170 size = NSMiniControlSize;
1173 case wxWINDOW_VARIANT_LARGE :
1174 size = NSRegularControlSize;
1178 wxFAIL_MSG(_T("unexpected window variant"));
1181 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1182 [m_osxView setControlSize:size];
1185 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1190 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1192 WXWidget c = control ? control : (WXWidget) m_osxView;
1193 wxWidgetImpl::Associate( c, this ) ;
1194 if ([c respondsToSelector:@selector(setAction:)])
1197 [c setAction: @selector(clickedAction:)];
1198 if ([c respondsToSelector:@selector(setDoubleAction:)])
1200 [c setDoubleAction: @selector(doubleClickedAction:)];
1206 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1208 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1209 SetupKeyEvent( wxevent, event );
1211 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1214 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1216 NSPoint clickLocation;
1217 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1218 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1219 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1220 SetupMouseEvent( wxevent , event ) ;
1224 return GetWXPeer()->HandleWindowEvent(wxevent);
1227 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1229 wxWindow* thisWindow = GetWXPeer();
1230 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1232 thisWindow->MacInvalidateBorders();
1235 if ( receivedFocus )
1237 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1238 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1239 thisWindow->HandleWindowEvent(eventFocus);
1242 if ( thisWindow->GetCaret() )
1243 thisWindow->GetCaret()->OnSetFocus();
1246 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1247 event.SetEventObject(thisWindow);
1248 // TODO how to find out the targetFocusWindow ?
1249 // event.SetWindow(targetFocusWindow);
1250 thisWindow->HandleWindowEvent(event) ;
1252 else // !receivedFocuss
1255 if ( thisWindow->GetCaret() )
1256 thisWindow->GetCaret()->OnKillFocus();
1259 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1261 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1262 event.SetEventObject(thisWindow);
1263 // TODO how to find out the targetFocusWindow ?
1264 // event.SetWindow(targetFocusWindow);
1265 thisWindow->HandleWindowEvent(event) ;
1269 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1271 NSPoint location = [NSEvent mouseLocation];
1272 location = [[m_osxView window] convertScreenToBase:location];
1273 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1275 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1277 [(NSCursor*)cursor.GetHCURSOR() set];
1279 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1282 void wxWidgetCocoaImpl::CaptureMouse()
1284 [[m_osxView window] disableCursorRects];
1287 void wxWidgetCocoaImpl::ReleaseMouse()
1289 [[m_osxView window] enableCursorRects];
1292 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1294 m_isFlipped = flipped;
1301 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1302 long style, long extraStyle)
1304 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1305 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1307 // temporary hook for dnd
1308 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1309 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1311 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1315 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1317 NSWindow* tlw = now->GetWXWindow();
1318 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1319 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1320 [tlw setContentView:v];