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 // Get the window with the focus
36 NSView* GetViewFromResponder( NSResponder* responder )
39 if ( [responder isKindOfClass:[NSTextView class]] )
41 NSView* delegate = [(NSTextView*)responder delegate];
42 if ( [delegate isKindOfClass:[NSTextField class] ] )
45 view = (NSView*) responder;
49 if ( [responder isKindOfClass:[NSView class]] )
50 view = (NSView*) responder;
55 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
57 NSView* focusedView = nil;
58 if ( keyWindow != nil )
59 focusedView = GetViewFromResponder([keyWindow firstResponder]);
64 WXWidget wxWidgetImpl::FindFocus()
66 return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
69 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
73 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
74 wxRect bounds(x,y,w,h);
75 NSView* sv = (window->GetParent()->GetHandle() );
77 return wxToNSRect( sv, bounds );
80 @interface wxNSView : NSView
82 NSTrackingRectTag rectTag;
85 // the tracking tag is needed to track mouse enter / exit events
86 - (void) setTrackingTag: (NSTrackingRectTag)tag;
87 - (NSTrackingRectTag) trackingTag;
90 @interface NSView(PossibleMethods)
91 - (void)setTitle:(NSString *)aString;
92 - (void)setStringValue:(NSString *)aString;
93 - (void)setIntValue:(int)anInt;
94 - (void)setFloatValue:(float)aFloat;
95 - (void)setDoubleValue:(double)aDouble;
99 - (void)setMinValue:(double)aDouble;
100 - (void)setMaxValue:(double)aDouble;
105 - (void)setEnabled:(BOOL)flag;
107 - (void)setImage:(NSImage *)image;
108 - (void)setControlSize:(NSControlSize)size;
110 - (void)setFont:(NSFont *)fontObject;
114 - (void)setTarget:(id)anObject;
115 - (void)setAction:(SEL)aSelector;
116 - (void)setDoubleAction:(SEL)aSelector;
119 long wxOSXTranslateCocoaKey( NSEvent* event )
123 if ([event type] != NSFlagsChanged)
125 NSString* s = [event charactersIgnoringModifiers];
126 // backspace char reports as delete w/modifiers for some reason
129 switch ( [s characterAtIndex:0] )
136 case NSUpArrowFunctionKey :
139 case NSDownArrowFunctionKey :
142 case NSLeftArrowFunctionKey :
145 case NSRightArrowFunctionKey :
148 case NSInsertFunctionKey :
151 case NSDeleteFunctionKey :
154 case NSHomeFunctionKey :
157 // case NSBeginFunctionKey :
158 // retval = WXK_BEGIN;
160 case NSEndFunctionKey :
163 case NSPageUpFunctionKey :
166 case NSPageDownFunctionKey :
167 retval = WXK_PAGEDOWN;
169 case NSHelpFunctionKey :
173 int intchar = [s characterAtIndex: 0];
174 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
175 retval = WXK_F1 + (intchar - NSF1FunctionKey );
181 // Some keys don't seem to have constants. The code mimics the approach
182 // taken by WebKit. See:
183 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
184 switch( [event keyCode] )
189 retval = WXK_COMMAND;
193 retval = WXK_CAPITAL;
196 case 56: // Left Shift
197 case 60: // Right Shift
202 case 61: // Right Alt
206 case 59: // Left Ctrl
207 case 62: // Right Ctrl
208 retval = WXK_CONTROL;
220 retval = WXK_NUMPAD_DIVIDE;
223 retval = WXK_NUMPAD_MULTIPLY;
226 retval = WXK_NUMPAD_SUBTRACT;
229 retval = WXK_NUMPAD_ADD;
232 retval = WXK_NUMPAD_ENTER;
235 retval = WXK_NUMPAD_DECIMAL;
238 retval = WXK_NUMPAD0;
241 retval = WXK_NUMPAD1;
244 retval = WXK_NUMPAD2;
247 retval = WXK_NUMPAD3;
250 retval = WXK_NUMPAD4;
253 retval = WXK_NUMPAD5;
256 retval = WXK_NUMPAD6;
259 retval = WXK_NUMPAD7;
262 retval = WXK_NUMPAD8;
265 retval = WXK_NUMPAD9;
268 //retval = [event keyCode];
274 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString = NULL )
276 UInt32 modifiers = [nsEvent modifierFlags] ;
277 int eventType = [nsEvent type];
279 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
280 wxevent.m_controlDown = modifiers & NSControlKeyMask;
281 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
282 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
284 wxevent.m_rawCode = [nsEvent keyCode];
285 wxevent.m_rawFlags = modifiers;
287 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
290 if ( eventType != NSFlagsChanged )
292 NSString* nschars = (wxevent.GetEventType() != wxEVT_CHAR) ? [nsEvent charactersIgnoringModifiers] : [nsEvent characters];
295 // if charString is set, it did not come from key up / key down
296 wxevent.SetEventType( wxEVT_CHAR );
297 chars = wxCFStringRef::AsString(charString);
301 chars = wxCFStringRef::AsString(nschars);
305 int aunichar = chars.Length() > 0 ? chars[0] : 0;
308 if (wxevent.GetEventType() != wxEVT_CHAR)
310 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
314 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
317 wxevent.SetEventType( wxEVT_KEY_UP ) ;
319 case NSFlagsChanged :
323 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
326 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
329 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
332 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
343 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
344 keyval = wxToupper( aunichar ) ;
350 wxevent.m_uniChar = aunichar;
352 wxevent.m_keyCode = keyval;
355 UInt32 g_lastButton = 0 ;
356 bool g_lastButtonWasFakeRight = false ;
358 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
360 int eventType = [nsEvent type];
361 UInt32 modifiers = [nsEvent modifierFlags] ;
362 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
364 // these parameters are not given for all events
365 UInt32 button = [nsEvent buttonNumber];
366 UInt32 clickCount = 0;
368 wxevent.m_x = screenMouseLocation.x;
369 wxevent.m_y = screenMouseLocation.y;
370 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
371 wxevent.m_controlDown = modifiers & NSControlKeyMask;
372 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
373 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
374 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
376 UInt32 mouseChord = 0;
380 case NSLeftMouseDown :
381 case NSLeftMouseDragged :
384 case NSRightMouseDown :
385 case NSRightMouseDragged :
388 case NSOtherMouseDown :
389 case NSOtherMouseDragged :
394 // a control click is interpreted as a right click
395 bool thisButtonIsFakeRight = false ;
396 if ( button == 0 && (modifiers & NSControlKeyMask) )
399 thisButtonIsFakeRight = true ;
402 // otherwise we report double clicks by connecting a left click with a ctrl-left click
403 if ( clickCount > 1 && button != g_lastButton )
406 // we must make sure that our synthetic 'right' button corresponds in
407 // mouse down, moved and mouse up, and does not deliver a right down and left up
410 case NSLeftMouseDown :
411 case NSRightMouseDown :
412 case NSOtherMouseDown :
413 g_lastButton = button ;
414 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
421 g_lastButtonWasFakeRight = false ;
423 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
424 button = g_lastButton ;
426 // Adjust the chord mask to remove the primary button and add the
427 // secondary button. It is possible that the secondary button is
428 // already pressed, e.g. on a mouse connected to a laptop, but this
429 // possibility is ignored here:
430 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
431 mouseChord = ((mouseChord & ~1U) | 2U);
434 wxevent.m_leftDown = true ;
436 wxevent.m_rightDown = true ;
438 wxevent.m_middleDown = true ;
440 // translate into wx types
443 case NSLeftMouseDown :
444 case NSRightMouseDown :
445 case NSOtherMouseDown :
446 clickCount = [nsEvent clickCount];
450 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
454 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
458 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
467 case NSRightMouseUp :
468 case NSOtherMouseUp :
469 clickCount = [nsEvent clickCount];
473 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
477 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
481 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
491 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
492 wxevent.m_wheelDelta = 10;
493 wxevent.m_linesPerAction = 1;
495 if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
497 wxevent.m_wheelAxis = 1;
498 wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
502 wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
507 case NSMouseEntered :
508 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
511 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
513 case NSLeftMouseDragged :
514 case NSRightMouseDragged :
515 case NSOtherMouseDragged :
517 wxevent.SetEventType( wxEVT_MOTION ) ;
523 wxevent.m_clickCount = clickCount;
527 @implementation wxNSView
531 static BOOL initialized = NO;
535 wxOSXCocoaClassAddWXMethods( self );
539 - (void) setTrackingTag: (NSTrackingRectTag)tag
544 - (NSTrackingRectTag) trackingTag
555 #if wxUSE_DRAG_AND_DROP
557 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
558 // for details on the NSPasteboard -> PasteboardRef conversion
560 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
562 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
564 return NSDragOperationNone;
566 return impl->draggingEntered(sender, self, _cmd);
569 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
571 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
575 return impl->draggingExited(sender, self, _cmd);
578 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
580 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
582 return NSDragOperationNone;
584 return impl->draggingUpdated(sender, self, _cmd);
587 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
589 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
591 return NSDragOperationNone;
593 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
598 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
600 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
604 impl->mouseEvent(event, self, _cmd);
607 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
609 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
613 impl->keyEvent(event, self, _cmd);
616 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
618 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
622 impl->insertText(text, self, _cmd);
625 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
627 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
631 return impl->performKeyEquivalent(event, self, _cmd);
634 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
636 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
640 return impl->acceptsFirstResponder(self, _cmd);
643 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
645 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
649 return impl->becomeFirstResponder(self, _cmd);
652 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
654 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
658 return impl->resignFirstResponder(self, _cmd);
661 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
663 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
667 impl->resetCursorRects(self, _cmd);
670 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
672 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
676 return impl->isFlipped(self, _cmd) ? YES:NO;
679 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
681 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
685 return impl->drawRect(&rect, self, _cmd);
688 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
690 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
694 impl->controlAction(self, _cmd, sender);
697 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
699 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
703 impl->controlDoubleAction(self, _cmd, sender);
706 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
708 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
709 NSPasteboard *pboard = [sender draggingPasteboard];
710 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
712 wxWindow* wxpeer = GetWXPeer();
713 if ( wxpeer == NULL )
714 return NSDragOperationNone;
716 wxDropTarget* target = wxpeer->GetDropTarget();
717 if ( target == NULL )
718 return NSDragOperationNone;
720 wxDragResult result = wxDragNone;
721 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
722 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
724 if ( sourceDragMask & NSDragOperationLink )
726 else if ( sourceDragMask & NSDragOperationCopy )
728 else if ( sourceDragMask & NSDragOperationMove )
731 PasteboardRef pboardRef;
732 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
733 target->SetCurrentDragPasteboard(pboardRef);
734 result = target->OnEnter(pt.x, pt.y, result);
735 CFRelease(pboardRef);
737 NSDragOperation nsresult = NSDragOperationNone;
741 nsresult = NSDragOperationLink;
743 nsresult = NSDragOperationMove;
745 nsresult = NSDragOperationCopy;
752 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
754 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
755 NSPasteboard *pboard = [sender draggingPasteboard];
757 wxWindow* wxpeer = GetWXPeer();
758 if ( wxpeer == NULL )
761 wxDropTarget* target = wxpeer->GetDropTarget();
762 if ( target == NULL )
765 PasteboardRef pboardRef;
766 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
767 target->SetCurrentDragPasteboard(pboardRef);
769 CFRelease(pboardRef);
772 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
774 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
775 NSPasteboard *pboard = [sender draggingPasteboard];
776 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
778 wxWindow* wxpeer = GetWXPeer();
779 if ( wxpeer == NULL )
780 return NSDragOperationNone;
782 wxDropTarget* target = wxpeer->GetDropTarget();
783 if ( target == NULL )
784 return NSDragOperationNone;
786 wxDragResult result = wxDragNone;
787 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
788 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
790 if ( sourceDragMask & NSDragOperationLink )
792 else if ( sourceDragMask & NSDragOperationCopy )
794 else if ( sourceDragMask & NSDragOperationMove )
797 PasteboardRef pboardRef;
798 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
799 target->SetCurrentDragPasteboard(pboardRef);
800 result = target->OnDragOver(pt.x, pt.y, result);
801 CFRelease(pboardRef);
803 NSDragOperation nsresult = NSDragOperationNone;
807 nsresult = NSDragOperationLink;
809 nsresult = NSDragOperationMove;
811 nsresult = NSDragOperationCopy;
818 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
820 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
822 NSPasteboard *pboard = [sender draggingPasteboard];
823 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
825 wxWindow* wxpeer = GetWXPeer();
826 wxDropTarget* target = wxpeer->GetDropTarget();
827 wxDragResult result = wxDragNone;
828 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
829 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
831 if ( sourceDragMask & NSDragOperationLink )
833 else if ( sourceDragMask & NSDragOperationCopy )
835 else if ( sourceDragMask & NSDragOperationMove )
838 PasteboardRef pboardRef;
839 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
840 target->SetCurrentDragPasteboard(pboardRef);
842 if (target->OnDrop(pt.x, pt.y))
843 result = target->OnData(pt.x, pt.y, result);
845 CFRelease(pboardRef);
847 return result != wxDragNone;
850 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
851 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
852 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
853 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
854 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
855 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
857 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
859 if ( !DoHandleMouseEvent(event) )
861 // for plain NSView mouse events would propagate to parents otherwise
862 if (!m_wxPeer->MacIsUserPane())
864 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
865 superimpl(slf, (SEL)_cmd, event);
870 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
872 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
874 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
875 superimpl(slf, (SEL)_cmd, event);
879 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
881 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
883 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
884 superimpl(slf, (SEL)_cmd, text);
886 m_lastKeyDownEvent = NULL;
890 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
892 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
893 return superimpl(slf, (SEL)_cmd, event);
896 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
898 if ( m_wxPeer->MacIsUserPane() )
899 return m_wxPeer->AcceptsFocus();
902 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
903 return superimpl(slf, (SEL)_cmd);
907 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
909 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
910 // get the current focus before running becomeFirstResponder
911 NSView* otherView = FindFocus();
913 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
914 BOOL r = superimpl(slf, (SEL)_cmd);
917 DoNotifyFocusEvent( true, otherWindow );
923 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
925 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
926 BOOL r = superimpl(slf, (SEL)_cmd);
927 // get the current focus after running resignFirstResponder
928 // note that this value isn't reliable, it might return the same view that
930 NSView* otherView = FindFocus();
931 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
932 // NSTextViews have an editor as true responder, therefore the might get the
933 // resign notification if their editor takes over, don't trigger any event then
934 if ( r && !m_hasEditor)
936 DoNotifyFocusEvent( false, otherWindow );
941 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
943 wxWindow* wxpeer = GetWXPeer();
946 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
949 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
950 superimpl(slf, (SEL)_cmd);
954 [slf addCursorRect: [slf bounds]
960 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
966 #define OSX_DEBUG_DRAWING 0
968 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
970 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
971 CGContextSaveGState( context );
973 #if OSX_DEBUG_DRAWING
974 CGContextBeginPath( context );
975 CGContextMoveToPoint(context, 0, 0);
976 NSRect bounds = [self bounds];
977 CGContextAddLineToPoint(context, 10, 0);
978 CGContextMoveToPoint(context, 0, 0);
979 CGContextAddLineToPoint(context, 0, 10);
980 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
981 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
982 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
983 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
984 CGContextClosePath( context );
985 CGContextStrokePath(context);
990 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
991 CGContextScaleCTM( context, 1, -1 );
998 [slf getRectsBeingDrawn:&rects count:&count];
999 for ( int i = 0 ; i < count ; ++i )
1001 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1004 wxWindow* wxpeer = GetWXPeer();
1005 wxpeer->GetUpdateRegion() = updateRgn;
1006 wxpeer->MacSetCGContextRef( context );
1008 bool handled = wxpeer->MacDoRedraw( 0 );
1010 CGContextRestoreGState( context );
1012 CGContextSaveGState( context );
1016 SEL _cmd = @selector(drawRect:);
1017 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1018 superimpl(slf, _cmd, *(NSRect*)rect);
1019 CGContextRestoreGState( context );
1020 CGContextSaveGState( context );
1022 wxpeer->MacPaintChildrenBorders();
1023 wxpeer->MacSetCGContextRef( NULL );
1024 CGContextRestoreGState( context );
1027 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1029 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1031 wxpeer->OSXHandleClicked(0);
1034 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1040 #if OBJC_API_VERSION >= 2
1042 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1043 class_addMethod(c, s, i, t );
1047 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1052 void wxOSXCocoaClassAddWXMethods(Class c)
1055 #if OBJC_API_VERSION < 2
1056 static objc_method wxmethods[] =
1060 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1061 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1062 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1064 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1065 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1066 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1068 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1070 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1071 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1072 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1074 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1075 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1076 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1078 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1079 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1080 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1082 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1084 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1086 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1087 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1088 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1089 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1091 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1092 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1094 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1095 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1097 #if wxUSE_DRAG_AND_DROP
1098 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1099 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1100 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1101 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1104 #if OBJC_API_VERSION < 2
1106 static int method_count = WXSIZEOF( wxmethods );
1107 static objc_method_list *wxmethodlist = NULL;
1108 if ( wxmethodlist == NULL )
1110 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1111 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1112 wxmethodlist->method_count = method_count;
1113 wxmethodlist->obsolete = 0;
1115 class_addMethods( c, wxmethodlist );
1120 // C++ implementation class
1123 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1125 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1126 wxWidgetImpl( peer, isRootControl )
1131 // check if the user wants to create the control initially hidden
1132 if ( !peer->IsShown() )
1133 SetVisibility(false);
1135 // gc aware handling
1137 CFRetain(m_osxView);
1138 [m_osxView release];
1141 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1146 void wxWidgetCocoaImpl::Init()
1150 m_lastKeyDownEvent = NULL;
1151 m_hasEditor = false;
1154 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1156 RemoveAssociations( this );
1158 if ( !IsRootControl() )
1160 NSView *sv = [m_osxView superview];
1162 [m_osxView removeFromSuperview];
1164 // gc aware handling
1166 CFRelease(m_osxView);
1169 bool wxWidgetCocoaImpl::IsVisible() const
1171 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1174 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1176 [m_osxView setHidden:(visible ? NO:YES)];
1179 void wxWidgetCocoaImpl::Raise()
1184 void wxWidgetCocoaImpl::Lower()
1189 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1194 // We should do something like this, but it wasn't working in 10.4.
1195 if (GetNeedsDisplay() )
1199 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1200 NSSize offset = NSMakeSize((float)dx, (float)dy);
1201 [m_osxView scrollRect:r by:offset];
1205 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1207 wxWindowMac* parent = GetWXPeer()->GetParent();
1208 // under Cocoa we might have a contentView in the wxParent to which we have to
1209 // adjust the coordinates
1210 if (parent && [m_osxView superview] != parent->GetHandle() )
1212 int cx = 0,cy = 0,cw = 0,ch = 0;
1213 if ( parent->GetPeer() )
1215 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1220 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1221 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1222 [m_osxView setFrame:r];
1223 [[m_osxView superview] setNeedsDisplayInRect:r];
1225 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1227 if ( [(wxNSView*)m_osxView trackingTag] )
1228 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1230 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1234 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1236 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1241 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1243 NSRect rect = [m_osxView frame];
1244 width = (int)rect.size.width;
1245 height = (int)rect.size.height;
1248 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1250 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1252 NSView* cv = [m_osxView contentView];
1254 NSRect bounds = [m_osxView bounds];
1255 NSRect rect = [cv frame];
1257 int y = (int)rect.origin.y;
1258 int x = (int)rect.origin.x;
1259 if ( ![ m_osxView isFlipped ] )
1260 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1263 width = (int)rect.size.width;
1264 height = (int)rect.size.height;
1269 GetSize( width, height );
1273 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1276 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1278 [m_osxView setNeedsDisplay:YES];
1281 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1283 return [m_osxView needsDisplay];
1286 bool wxWidgetCocoaImpl::CanFocus() const
1288 return [m_osxView canBecomeKeyView] == YES;
1291 bool wxWidgetCocoaImpl::HasFocus() const
1293 return ( FindFocus() == m_osxView );
1296 bool wxWidgetCocoaImpl::SetFocus()
1298 if ( [m_osxView canBecomeKeyView] == NO )
1301 [[m_osxView window] makeFirstResponder: m_osxView] ;
1302 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1307 void wxWidgetCocoaImpl::RemoveFromParent()
1309 [m_osxView removeFromSuperview];
1312 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1314 NSView* container = parent->GetWXWidget() ;
1315 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1316 [container addSubview:m_osxView];
1319 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1321 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1324 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1326 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1328 wxCFStringRef cf( title , encoding );
1329 [m_osxView setTitle:cf.AsNSString()];
1331 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1333 wxCFStringRef cf( title , encoding );
1334 [m_osxView setStringValue:cf.AsNSString()];
1339 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1341 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1342 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1343 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1346 wxInt32 wxWidgetCocoaImpl::GetValue() const
1348 return [(NSControl*)m_osxView intValue];
1351 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1353 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1355 [m_osxView setIntValue:v];
1357 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1359 [m_osxView setFloatValue:(double)v];
1361 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1363 [m_osxView setDoubleValue:(double)v];
1367 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1369 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1371 [m_osxView setMinValue:(double)v];
1375 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1377 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1379 [m_osxView setMaxValue:(double)v];
1383 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1385 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1387 return (int)[m_osxView minValue];
1392 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1394 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1396 return (int)[m_osxView maxValue];
1401 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1405 // TODO: how to create a wxBitmap from NSImage?
1407 if ( [m_osxView respondsToSelector:@selector(image:)] )
1408 bmp = [m_osxView image];
1414 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1416 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1418 [m_osxView setImage:bitmap.GetNSImage()];
1422 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1424 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1426 NSCellImagePosition pos;
1446 wxFAIL_MSG( "invalid image position" );
1450 [m_osxView setImagePosition:pos];
1454 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1456 // implementation in subclass
1459 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1461 r->x = r->y = r->width = r->height = 0;
1463 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1465 NSRect former = [m_osxView frame];
1466 [m_osxView sizeToFit];
1467 NSRect best = [m_osxView frame];
1468 [m_osxView setFrame:former];
1469 r->width = (int)best.size.width;
1470 r->height = (int)best.size.height;
1474 bool wxWidgetCocoaImpl::IsEnabled() const
1476 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1477 return [m_osxView isEnabled];
1481 void wxWidgetCocoaImpl::Enable( bool enable )
1483 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1484 [m_osxView setEnabled:enable];
1487 void wxWidgetCocoaImpl::PulseGauge()
1491 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1495 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1497 NSControlSize size = NSRegularControlSize;
1501 case wxWINDOW_VARIANT_NORMAL :
1502 size = NSRegularControlSize;
1505 case wxWINDOW_VARIANT_SMALL :
1506 size = NSSmallControlSize;
1509 case wxWINDOW_VARIANT_MINI :
1510 size = NSMiniControlSize;
1513 case wxWINDOW_VARIANT_LARGE :
1514 size = NSRegularControlSize;
1518 wxFAIL_MSG(wxT("unexpected window variant"));
1521 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1522 [m_osxView setControlSize:size];
1523 else if ([m_osxView respondsToSelector:@selector(cell)])
1525 id cell = [(id)m_osxView cell];
1526 if ([cell respondsToSelector:@selector(setControlSize:)])
1527 [cell setControlSize:size];
1531 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1533 if ([m_osxView respondsToSelector:@selector(setFont:)])
1534 [m_osxView setFont: font.OSXGetNSFont()];
1537 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1539 WXWidget c = control ? control : (WXWidget) m_osxView;
1540 wxWidgetImpl::Associate( c, this ) ;
1541 if ([c respondsToSelector:@selector(setAction:)])
1544 [c setAction: @selector(controlAction:)];
1545 if ([c respondsToSelector:@selector(setDoubleAction:)])
1547 [c setDoubleAction: @selector(controlDoubleAction:)];
1553 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1555 wxKeyEvent wxevent(wxEVT_CHAR);
1556 SetupKeyEvent( wxevent, event, text );
1557 wxevent.SetEventObject(GetWXPeer());
1559 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1562 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1564 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1565 SetupKeyEvent( wxevent, event );
1566 wxevent.SetEventObject(GetWXPeer());
1567 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1569 // this will fire higher level events, like insertText, to help
1570 // us handle EVT_CHAR, etc.
1571 if ( !m_hasEditor && [event type] == NSKeyDown)
1573 m_lastKeyDownEvent = event;
1576 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1577 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1579 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1586 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1588 NSPoint clickLocation;
1589 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1590 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1591 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1592 SetupMouseEvent( wxevent , event ) ;
1593 wxevent.SetEventObject(GetWXPeer());
1597 return GetWXPeer()->HandleWindowEvent(wxevent);
1600 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1602 wxWindow* thisWindow = GetWXPeer();
1603 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1605 thisWindow->MacInvalidateBorders();
1608 if ( receivedFocus )
1610 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1611 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1612 thisWindow->HandleWindowEvent(eventFocus);
1615 if ( thisWindow->GetCaret() )
1616 thisWindow->GetCaret()->OnSetFocus();
1619 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1620 event.SetEventObject(thisWindow);
1622 event.SetWindow(otherWindow->GetWXPeer());
1623 thisWindow->HandleWindowEvent(event) ;
1625 else // !receivedFocuss
1628 if ( thisWindow->GetCaret() )
1629 thisWindow->GetCaret()->OnKillFocus();
1632 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1634 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1635 event.SetEventObject(thisWindow);
1637 event.SetWindow(otherWindow->GetWXPeer());
1638 thisWindow->HandleWindowEvent(event) ;
1642 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1644 NSPoint location = [NSEvent mouseLocation];
1645 location = [[m_osxView window] convertScreenToBase:location];
1646 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1648 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1650 [(NSCursor*)cursor.GetHCURSOR() set];
1652 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1655 void wxWidgetCocoaImpl::CaptureMouse()
1657 [[m_osxView window] disableCursorRects];
1660 void wxWidgetCocoaImpl::ReleaseMouse()
1662 [[m_osxView window] enableCursorRects];
1665 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1667 m_isFlipped = flipped;
1674 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1675 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1676 long WXUNUSED(style), long WXUNUSED(extraStyle))
1678 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1679 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1681 // temporary hook for dnd
1682 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1683 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1685 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1689 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1691 NSWindow* tlw = now->GetWXWindow();
1692 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1693 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1694 c->InstallEventHandler();
1695 [tlw setContentView:v];