1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/window.mm
3 // Purpose: widgets (non tlw) for cocoa
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/dcclient.h"
16 #include "wx/nonownedwnd.h"
21 #include "wx/osx/private.h"
28 #if wxUSE_DRAG_AND_DROP
32 #include <objc/objc-runtime.h>
34 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
38 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
39 wxRect bounds(x,y,w,h);
40 NSView* sv = (window->GetParent()->GetHandle() );
42 return wxToNSRect( sv, bounds );
45 @interface wxNSView : NSView
49 - (BOOL) canBecomeKeyView;
53 @interface NSView(PossibleMethods)
54 - (void)setTitle:(NSString *)aString;
55 - (void)setStringValue:(NSString *)aString;
56 - (void)setIntValue:(int)anInt;
57 - (void)setFloatValue:(float)aFloat;
58 - (void)setDoubleValue:(double)aDouble;
62 - (void)setMinValue:(double)aDouble;
63 - (void)setMaxValue:(double)aDouble;
68 - (void)setEnabled:(BOOL)flag;
70 - (void)setImage:(NSImage *)image;
71 - (void)setControlSize:(NSControlSize)size;
75 - (void)setTarget:(id)anObject;
76 - (void)setAction:(SEL)aSelector;
77 - (void)setDoubleAction:(SEL)aSelector;
80 long wxOSXTranslateCocoaKey( int unichar )
82 long retval = unichar;
85 case NSUpArrowFunctionKey :
88 case NSDownArrowFunctionKey :
91 case NSLeftArrowFunctionKey :
94 case NSRightArrowFunctionKey :
97 case NSInsertFunctionKey :
100 case NSDeleteFunctionKey :
103 case NSHomeFunctionKey :
106 // case NSBeginFunctionKey :
107 // retval = WXK_BEGIN;
109 case NSEndFunctionKey :
112 case NSPageUpFunctionKey :
115 case NSPageDownFunctionKey :
116 retval = WXK_PAGEDOWN;
118 case NSHelpFunctionKey :
123 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
124 retval = WXK_F1 + (unichar - NSF1FunctionKey );
130 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
132 UInt32 modifiers = [nsEvent modifierFlags] ;
133 int eventType = [nsEvent type];
135 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
136 wxevent.m_controlDown = modifiers & NSControlKeyMask;
137 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
138 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
140 wxevent.m_rawCode = [nsEvent keyCode];
141 wxevent.m_rawFlags = modifiers;
143 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
147 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
150 wxevent.SetEventType( wxEVT_KEY_UP ) ;
152 case NSFlagsChanged :
153 // setup common code here
160 if ( eventType != NSFlagsChanged )
162 NSString* nschars = [nsEvent characters];
165 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
166 chars = cfchars.AsString();
170 int unichar = chars.Length() > 0 ? chars[0] : 0;
171 long keyval = wxOSXTranslateCocoaKey(unichar) ;
172 if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
173 keyval = wxToupper( keyval ) ;
176 wxevent.m_uniChar = unichar;
178 wxevent.m_keyCode = keyval;
181 UInt32 g_lastButton = 0 ;
182 bool g_lastButtonWasFakeRight = false ;
184 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
186 int eventType = [nsEvent type];
187 UInt32 modifiers = [nsEvent modifierFlags] ;
188 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
190 // these parameters are not given for all events
191 UInt32 button = [nsEvent buttonNumber];
192 UInt32 clickCount = 0;
193 if ( eventType != NSScrollWheel )
194 [nsEvent clickCount];
196 wxevent.m_x = screenMouseLocation.x;
197 wxevent.m_y = screenMouseLocation.y;
198 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
199 wxevent.m_controlDown = modifiers & NSControlKeyMask;
200 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
201 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
202 wxevent.m_clickCount = clickCount;
203 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
205 UInt32 mouseChord = 0;
209 case NSLeftMouseDown :
210 case NSLeftMouseDragged :
213 case NSRightMouseDown :
214 case NSRightMouseDragged :
217 case NSOtherMouseDown :
218 case NSOtherMouseDragged :
223 // a control click is interpreted as a right click
224 bool thisButtonIsFakeRight = false ;
225 if ( button == 0 && (modifiers & NSControlKeyMask) )
228 thisButtonIsFakeRight = true ;
231 // otherwise we report double clicks by connecting a left click with a ctrl-left click
232 if ( clickCount > 1 && button != g_lastButton )
235 // we must make sure that our synthetic 'right' button corresponds in
236 // mouse down, moved and mouse up, and does not deliver a right down and left up
239 case NSLeftMouseDown :
240 case NSRightMouseDown :
241 case NSOtherMouseDown :
242 g_lastButton = button ;
243 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
250 g_lastButtonWasFakeRight = false ;
252 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
253 button = g_lastButton ;
255 // Adjust the chord mask to remove the primary button and add the
256 // secondary button. It is possible that the secondary button is
257 // already pressed, e.g. on a mouse connected to a laptop, but this
258 // possibility is ignored here:
259 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
260 mouseChord = ((mouseChord & ~1U) | 2U);
263 wxevent.m_leftDown = true ;
265 wxevent.m_rightDown = true ;
267 wxevent.m_middleDown = true ;
269 // translate into wx types
272 case NSLeftMouseDown :
273 case NSRightMouseDown :
274 case NSOtherMouseDown :
278 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
282 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
286 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
295 case NSRightMouseUp :
296 case NSOtherMouseUp :
300 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
304 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
308 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
318 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
319 wxevent.m_wheelDelta = 1;
320 wxevent.m_linesPerAction = 1;
321 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
323 wxevent.m_wheelAxis = 1;
324 wxevent.m_wheelRotation = [nsEvent deltaX] > 0.0 ? 1 : -1;
328 wxevent.m_wheelRotation = [nsEvent deltaY] > 0.0 ? 1 : -1;
333 case NSMouseEntered :
335 case NSLeftMouseDragged :
336 case NSRightMouseDragged :
337 case NSOtherMouseDragged :
339 wxevent.SetEventType( wxEVT_MOTION ) ;
346 @implementation wxNSView
350 static BOOL initialized = NO;
354 wxOSXCocoaClassAddWXMethods( self );
358 - (BOOL) canBecomeKeyView
369 #if wxUSE_DRAG_AND_DROP
371 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
372 // for details on the NSPasteboard -> PasteboardRef conversion
374 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
376 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
378 return NSDragOperationNone;
380 return impl->draggingEntered(sender, self, _cmd);
383 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
385 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
389 return impl->draggingExited(sender, self, _cmd);
392 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
394 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
396 return NSDragOperationNone;
398 return impl->draggingUpdated(sender, self, _cmd);
401 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
403 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
405 return NSDragOperationNone;
407 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
410 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
412 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
416 impl->mouseEvent(event, self, _cmd);
419 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
421 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
425 impl->keyEvent(event, self, _cmd);
428 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
430 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
434 return impl->performKeyEquivalent(event, self, _cmd);
437 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
439 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
443 return impl->becomeFirstResponder(self, _cmd);
446 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
448 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
452 return impl->resignFirstResponder(self, _cmd);
455 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
457 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
461 impl->resetCursorRects(self, _cmd);
464 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
466 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
470 return impl->isFlipped(self, _cmd) ? YES:NO;
473 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
475 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
479 return impl->drawRect(&rect, self, _cmd);
482 void wxOSX_clickedAction(NSView* self, SEL _cmd, id sender)
484 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
488 impl->clickedAction(self, _cmd, sender);
491 void wxOSX_doubleClickedAction(NSView* self, SEL _cmd, id sender)
493 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
497 impl->doubleClickedAction(self, _cmd, sender);
500 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
502 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
503 NSPasteboard *pboard = [sender draggingPasteboard];
504 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
506 wxWindow* wxpeer = GetWXPeer();
507 if ( wxpeer == NULL )
508 return NSDragOperationNone;
510 wxDropTarget* target = wxpeer->GetDropTarget();
511 if ( target == NULL )
512 return NSDragOperationNone;
514 wxDragResult result = wxDragNone;
515 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
517 if ( sourceDragMask & NSDragOperationLink )
519 else if ( sourceDragMask & NSDragOperationCopy )
521 else if ( sourceDragMask & NSDragOperationMove )
524 PasteboardRef pboardRef;
525 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
526 target->SetCurrentDragPasteboard(pboardRef);
527 result = target->OnEnter(pt.x, pt.y, result);
528 CFRelease(pboardRef);
530 NSDragOperation nsresult = NSDragOperationNone;
534 nsresult = NSDragOperationLink;
536 nsresult = NSDragOperationMove;
538 nsresult = NSDragOperationCopy;
545 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
547 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
548 NSPasteboard *pboard = [sender draggingPasteboard];
550 wxWindow* wxpeer = GetWXPeer();
551 if ( wxpeer == NULL )
554 wxDropTarget* target = wxpeer->GetDropTarget();
555 if ( target == NULL )
558 PasteboardRef pboardRef;
559 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
560 target->SetCurrentDragPasteboard(pboardRef);
562 CFRelease(pboardRef);
565 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
567 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
568 NSPasteboard *pboard = [sender draggingPasteboard];
569 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
571 wxWindow* wxpeer = GetWXPeer();
572 if ( wxpeer == NULL )
573 return NSDragOperationNone;
575 wxDropTarget* target = wxpeer->GetDropTarget();
576 if ( target == NULL )
577 return NSDragOperationNone;
579 wxDragResult result = wxDragNone;
580 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
582 if ( sourceDragMask & NSDragOperationLink )
584 else if ( sourceDragMask & NSDragOperationCopy )
586 else if ( sourceDragMask & NSDragOperationMove )
589 PasteboardRef pboardRef;
590 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
591 target->SetCurrentDragPasteboard(pboardRef);
592 result = target->OnDragOver(pt.x, pt.y, result);
593 CFRelease(pboardRef);
595 NSDragOperation nsresult = NSDragOperationNone;
599 nsresult = NSDragOperationLink;
601 nsresult = NSDragOperationMove;
603 nsresult = NSDragOperationCopy;
610 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
612 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
614 NSPasteboard *pboard = [sender draggingPasteboard];
615 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
617 wxWindow* wxpeer = GetWXPeer();
618 wxDropTarget* target = wxpeer->GetDropTarget();
619 wxDragResult result = wxDragNone;
620 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
622 if ( sourceDragMask & NSDragOperationLink )
624 else if ( sourceDragMask & NSDragOperationCopy )
626 else if ( sourceDragMask & NSDragOperationMove )
629 PasteboardRef pboardRef;
630 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
631 target->SetCurrentDragPasteboard(pboardRef);
632 result = target->OnData(pt.x, pt.y, result);
633 CFRelease(pboardRef);
635 return result != wxDragNone;
640 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
641 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
642 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
643 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
644 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
646 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
648 if ( !DoHandleMouseEvent(event) )
650 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
651 superimpl(slf, (SEL)_cmd, event);
655 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
657 if ( !DoHandleKeyEvent(event) )
659 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
660 superimpl(slf, (SEL)_cmd, event);
664 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
666 if ( !DoHandleKeyEvent(event) )
668 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
669 return superimpl(slf, (SEL)_cmd, event);
675 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
677 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
678 BOOL r = superimpl(slf, (SEL)_cmd);
680 DoNotifyFocusEvent( true );
684 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
686 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
687 BOOL r = superimpl(slf, (SEL)_cmd);
689 DoNotifyFocusEvent( false );
693 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
695 wxWindow* wxpeer = GetWXPeer();
698 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
701 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
702 superimpl(slf, (SEL)_cmd);
705 [slf addCursorRect: [slf bounds]
710 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
716 #define OSX_DEBUG_DRAWING 0
718 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
720 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
721 CGContextSaveGState( context );
723 #if OSX_DEBUG_DRAWING
724 CGContextBeginPath( context );
725 CGContextMoveToPoint(context, 0, 0);
726 NSRect bounds = [self bounds];
727 CGContextAddLineToPoint(context, 10, 0);
728 CGContextMoveToPoint(context, 0, 0);
729 CGContextAddLineToPoint(context, 0, 10);
730 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
731 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
732 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
733 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
734 CGContextClosePath( context );
735 CGContextStrokePath(context);
740 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
741 CGContextScaleCTM( context, 1, -1 );
748 [slf getRectsBeingDrawn:&rects count:&count];
749 for ( int i = 0 ; i < count ; ++i )
751 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
754 wxWindow* wxpeer = GetWXPeer();
755 wxpeer->GetUpdateRegion() = updateRgn;
756 wxpeer->MacSetCGContextRef( context );
758 bool handled = wxpeer->MacDoRedraw( 0 );
760 CGContextRestoreGState( context );
762 CGContextSaveGState( context );
766 SEL _cmd = @selector(drawRect:);
767 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
768 superimpl(slf, _cmd, *(NSRect*)rect);
769 CGContextRestoreGState( context );
770 CGContextSaveGState( context );
772 wxpeer->MacPaintChildrenBorders();
773 CGContextRestoreGState( context );
776 void wxWidgetCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender)
778 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
780 wxpeer->OSXHandleClicked(0);
783 void wxWidgetCocoaImpl::doubleClickedAction( WXWidget slf, void *_cmd, void *sender)
789 #if OBJC_API_VERSION >= 2
791 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
792 class_addMethod(c, s, i, t );
796 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
801 void wxOSXCocoaClassAddWXMethods(Class c)
804 #if OBJC_API_VERSION < 2
805 static objc_method wxmethods[] =
809 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
810 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
811 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
813 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
814 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
815 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
817 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
819 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
820 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
821 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
823 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
824 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
825 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
827 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
828 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
829 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
831 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
834 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
835 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
836 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
838 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
839 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
841 wxOSX_CLASS_ADD_METHOD(c, @selector(clickedAction:), (IMP) wxOSX_clickedAction, "v@:@" )
842 wxOSX_CLASS_ADD_METHOD(c, @selector(doubleClickedAction:), (IMP) wxOSX_doubleClickedAction, "v@:@" )
844 #if wxUSE_DRAG_AND_DROP
845 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
846 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
847 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
848 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
851 #if OBJC_API_VERSION < 2
853 static int method_count = WXSIZEOF( wxmethods );
854 static objc_method_list *wxmethodlist = NULL;
855 if ( wxmethodlist == NULL )
857 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
858 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
859 wxmethodlist->method_count = method_count;
860 wxmethodlist->obsolete = 0;
862 class_addMethods( c, wxmethodlist );
867 // C++ implementation class
870 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
872 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
873 wxWidgetImpl( peer, isRootControl )
879 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
884 void wxWidgetCocoaImpl::Init()
890 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
892 RemoveAssociations( this );
894 if ( !IsRootControl() )
896 NSView *sv = [m_osxView superview];
898 [m_osxView removeFromSuperview];
903 bool wxWidgetCocoaImpl::IsVisible() const
905 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
908 void wxWidgetCocoaImpl::SetVisibility( bool visible )
910 [m_osxView setHidden:(visible ? NO:YES)];
913 void wxWidgetCocoaImpl::Raise()
917 void wxWidgetCocoaImpl::Lower()
921 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
926 // We should do something like this, but it wasn't working in 10.4.
927 if (GetNeedsDisplay() )
931 NSRect r = wxToNSRect( [m_osxView superview], *rect );
932 NSSize offset = NSMakeSize((float)dx, (float)dy);
933 [m_osxView scrollRect:r by:offset];
937 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
939 wxWindowMac* parent = GetWXPeer()->GetParent();
940 // under Cocoa we might have a contentView in the wxParent to which we have to
941 // adjust the coordinates
944 wxPoint pt(parent->GetClientAreaOrigin());
948 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
949 [m_osxView setFrame:r];
952 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
954 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
959 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
961 NSRect rect = [m_osxView frame];
962 width = rect.size.width;
963 height = rect.size.height;
966 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
968 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
970 NSView* cv = [m_osxView contentView];
972 NSRect bounds = [m_osxView bounds];
973 NSRect rect = [cv frame];
975 int y = rect.origin.y;
976 int x = rect.origin.x;
977 if ( ![ m_osxView isFlipped ] )
978 y = bounds.size.height - (rect.origin.y + rect.size.height);
981 width = rect.size.width;
982 height = rect.size.height;
987 GetSize( width, height );
991 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
994 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
996 [m_osxView setNeedsDisplay:YES];
999 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1001 return [m_osxView needsDisplay];
1004 bool wxWidgetCocoaImpl::CanFocus() const
1006 return [m_osxView canBecomeKeyView] == YES;
1009 bool wxWidgetCocoaImpl::HasFocus() const
1011 return ( [[m_osxView window] firstResponder] == m_osxView );
1014 bool wxWidgetCocoaImpl::SetFocus()
1016 if ( [m_osxView canBecomeKeyView] == NO )
1019 [[m_osxView window] makeFirstResponder: m_osxView] ;
1024 void wxWidgetCocoaImpl::RemoveFromParent()
1026 [m_osxView removeFromSuperview];
1029 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1031 NSView* container = parent->GetWXWidget() ;
1032 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1033 [container addSubview:m_osxView];
1036 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1038 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1041 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1043 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1045 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1046 [m_osxView setTitle:cf.AsNSString()];
1051 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1053 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1054 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1055 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1058 wxInt32 wxWidgetCocoaImpl::GetValue() const
1060 return [(NSControl*)m_osxView intValue];
1063 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1065 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1067 [m_osxView setIntValue:v];
1069 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1071 [m_osxView setFloatValue:(double)v];
1073 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1075 [m_osxView setDoubleValue:(double)v];
1079 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1081 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1083 [m_osxView setMinValue:(double)v];
1087 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1089 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1091 [m_osxView setMaxValue:(double)v];
1095 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1097 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1099 return [m_osxView minValue];
1104 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1106 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1108 return [m_osxView maxValue];
1113 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1115 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1117 [m_osxView setImage:bitmap.GetNSImage()];
1121 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1123 // implementation in subclass
1126 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1128 r->x = r->y = r->width = r->height = 0;
1129 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1130 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1132 NSRect former = [m_osxView frame];
1133 [m_osxView sizeToFit];
1134 NSRect best = [m_osxView frame];
1135 [m_osxView setFrame:former];
1136 r->width = best.size.width;
1137 r->height = best.size.height;
1141 bool wxWidgetCocoaImpl::IsEnabled() const
1143 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1144 return [m_osxView isEnabled];
1148 void wxWidgetCocoaImpl::Enable( bool enable )
1150 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1151 [m_osxView setEnabled:enable];
1154 void wxWidgetCocoaImpl::PulseGauge()
1158 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1162 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1164 NSControlSize size = NSRegularControlSize;
1168 case wxWINDOW_VARIANT_NORMAL :
1169 size = NSRegularControlSize;
1172 case wxWINDOW_VARIANT_SMALL :
1173 size = NSSmallControlSize;
1176 case wxWINDOW_VARIANT_MINI :
1177 size = NSMiniControlSize;
1180 case wxWINDOW_VARIANT_LARGE :
1181 size = NSRegularControlSize;
1185 wxFAIL_MSG(_T("unexpected window variant"));
1188 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1189 [m_osxView setControlSize:size];
1192 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1197 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1199 WXWidget c = control ? control : (WXWidget) m_osxView;
1200 wxWidgetImpl::Associate( c, this ) ;
1201 if ([c respondsToSelector:@selector(setAction:)])
1204 [c setAction: @selector(clickedAction:)];
1205 if ([c respondsToSelector:@selector(setDoubleAction:)])
1207 [c setDoubleAction: @selector(doubleClickedAction:)];
1213 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1215 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1216 SetupKeyEvent( wxevent, event );
1218 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1221 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1223 NSPoint clickLocation;
1224 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1225 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1226 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1227 SetupMouseEvent( wxevent , event ) ;
1231 return GetWXPeer()->HandleWindowEvent(wxevent);
1234 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1236 wxWindow* thisWindow = GetWXPeer();
1237 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1239 thisWindow->MacInvalidateBorders();
1242 if ( receivedFocus )
1244 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1245 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1246 thisWindow->HandleWindowEvent(eventFocus);
1249 if ( thisWindow->GetCaret() )
1250 thisWindow->GetCaret()->OnSetFocus();
1253 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1254 event.SetEventObject(thisWindow);
1255 // TODO how to find out the targetFocusWindow ?
1256 // event.SetWindow(targetFocusWindow);
1257 thisWindow->HandleWindowEvent(event) ;
1259 else // !receivedFocuss
1262 if ( thisWindow->GetCaret() )
1263 thisWindow->GetCaret()->OnKillFocus();
1266 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1268 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1269 event.SetEventObject(thisWindow);
1270 // TODO how to find out the targetFocusWindow ?
1271 // event.SetWindow(targetFocusWindow);
1272 thisWindow->HandleWindowEvent(event) ;
1276 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1278 NSPoint location = [NSEvent mouseLocation];
1279 location = [[m_osxView window] convertScreenToBase:location];
1280 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1282 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1284 [(NSCursor*)cursor.GetHCURSOR() set];
1286 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1289 void wxWidgetCocoaImpl::CaptureMouse()
1291 [[m_osxView window] disableCursorRects];
1294 void wxWidgetCocoaImpl::ReleaseMouse()
1296 [[m_osxView window] enableCursorRects];
1299 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1301 m_isFlipped = flipped;
1308 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1309 long style, long extraStyle)
1311 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1312 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1314 // temporary hook for dnd
1315 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1316 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1318 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1322 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1324 NSWindow* tlw = now->GetWXWindow();
1325 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1326 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1327 [tlw setContentView:v];