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 wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
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;
354 wxWindowMac* peer = GetWXPeer();
357 wxevent.SetEventObject(peer);
358 wxevent.SetId(peer->GetId()) ;
362 UInt32 g_lastButton = 0 ;
363 bool g_lastButtonWasFakeRight = false ;
365 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
367 int eventType = [nsEvent type];
368 UInt32 modifiers = [nsEvent modifierFlags] ;
369 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
371 // these parameters are not given for all events
372 UInt32 button = [nsEvent buttonNumber];
373 UInt32 clickCount = 0;
375 wxevent.m_x = screenMouseLocation.x;
376 wxevent.m_y = screenMouseLocation.y;
377 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
378 wxevent.m_controlDown = modifiers & NSControlKeyMask;
379 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
380 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
381 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
383 UInt32 mouseChord = 0;
387 case NSLeftMouseDown :
388 case NSLeftMouseDragged :
391 case NSRightMouseDown :
392 case NSRightMouseDragged :
395 case NSOtherMouseDown :
396 case NSOtherMouseDragged :
401 // a control click is interpreted as a right click
402 bool thisButtonIsFakeRight = false ;
403 if ( button == 0 && (modifiers & NSControlKeyMask) )
406 thisButtonIsFakeRight = true ;
409 // otherwise we report double clicks by connecting a left click with a ctrl-left click
410 if ( clickCount > 1 && button != g_lastButton )
413 // we must make sure that our synthetic 'right' button corresponds in
414 // mouse down, moved and mouse up, and does not deliver a right down and left up
417 case NSLeftMouseDown :
418 case NSRightMouseDown :
419 case NSOtherMouseDown :
420 g_lastButton = button ;
421 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
428 g_lastButtonWasFakeRight = false ;
430 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
431 button = g_lastButton ;
433 // Adjust the chord mask to remove the primary button and add the
434 // secondary button. It is possible that the secondary button is
435 // already pressed, e.g. on a mouse connected to a laptop, but this
436 // possibility is ignored here:
437 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
438 mouseChord = ((mouseChord & ~1U) | 2U);
441 wxevent.m_leftDown = true ;
443 wxevent.m_rightDown = true ;
445 wxevent.m_middleDown = true ;
447 // translate into wx types
450 case NSLeftMouseDown :
451 case NSRightMouseDown :
452 case NSOtherMouseDown :
453 clickCount = [nsEvent clickCount];
457 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
461 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
465 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
474 case NSRightMouseUp :
475 case NSOtherMouseUp :
476 clickCount = [nsEvent clickCount];
480 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
484 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
488 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
498 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
499 wxevent.m_wheelDelta = 10;
500 wxevent.m_linesPerAction = 1;
502 if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
504 wxevent.m_wheelAxis = 1;
505 wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
509 wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
514 case NSMouseEntered :
515 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
518 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
520 case NSLeftMouseDragged :
521 case NSRightMouseDragged :
522 case NSOtherMouseDragged :
524 wxevent.SetEventType( wxEVT_MOTION ) ;
530 wxevent.m_clickCount = clickCount;
531 wxWindowMac* peer = GetWXPeer();
534 wxevent.SetEventObject(peer);
535 wxevent.SetId(peer->GetId()) ;
539 @implementation wxNSView
543 static BOOL initialized = NO;
547 wxOSXCocoaClassAddWXMethods( self );
551 - (void) setTrackingTag: (NSTrackingRectTag)tag
556 - (NSTrackingRectTag) trackingTag
567 #if wxUSE_DRAG_AND_DROP
569 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
570 // for details on the NSPasteboard -> PasteboardRef conversion
572 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
574 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
576 return NSDragOperationNone;
578 return impl->draggingEntered(sender, self, _cmd);
581 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
583 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
587 return impl->draggingExited(sender, self, _cmd);
590 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
592 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
594 return NSDragOperationNone;
596 return impl->draggingUpdated(sender, self, _cmd);
599 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
601 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
603 return NSDragOperationNone;
605 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
610 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
612 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
616 impl->mouseEvent(event, self, _cmd);
619 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
621 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
625 impl->keyEvent(event, self, _cmd);
628 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
630 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
634 impl->insertText(text, self, _cmd);
637 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
639 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
643 return impl->performKeyEquivalent(event, self, _cmd);
646 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
648 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
652 return impl->acceptsFirstResponder(self, _cmd);
655 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
657 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
661 return impl->becomeFirstResponder(self, _cmd);
664 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
666 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
670 return impl->resignFirstResponder(self, _cmd);
673 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
675 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
679 impl->resetCursorRects(self, _cmd);
682 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
684 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
688 return impl->isFlipped(self, _cmd) ? YES:NO;
691 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
693 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
697 return impl->drawRect(&rect, self, _cmd);
700 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
702 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
706 impl->controlAction(self, _cmd, sender);
709 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
711 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
715 impl->controlDoubleAction(self, _cmd, sender);
718 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
720 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
721 NSPasteboard *pboard = [sender draggingPasteboard];
722 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
724 wxWindow* wxpeer = GetWXPeer();
725 if ( wxpeer == NULL )
726 return NSDragOperationNone;
728 wxDropTarget* target = wxpeer->GetDropTarget();
729 if ( target == NULL )
730 return NSDragOperationNone;
732 wxDragResult result = wxDragNone;
733 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
734 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
736 if ( sourceDragMask & NSDragOperationLink )
738 else if ( sourceDragMask & NSDragOperationCopy )
740 else if ( sourceDragMask & NSDragOperationMove )
743 PasteboardRef pboardRef;
744 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
745 target->SetCurrentDragPasteboard(pboardRef);
746 result = target->OnEnter(pt.x, pt.y, result);
747 CFRelease(pboardRef);
749 NSDragOperation nsresult = NSDragOperationNone;
753 nsresult = NSDragOperationLink;
755 nsresult = NSDragOperationMove;
757 nsresult = NSDragOperationCopy;
764 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
766 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
767 NSPasteboard *pboard = [sender draggingPasteboard];
769 wxWindow* wxpeer = GetWXPeer();
770 if ( wxpeer == NULL )
773 wxDropTarget* target = wxpeer->GetDropTarget();
774 if ( target == NULL )
777 PasteboardRef pboardRef;
778 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
779 target->SetCurrentDragPasteboard(pboardRef);
781 CFRelease(pboardRef);
784 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
786 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
787 NSPasteboard *pboard = [sender draggingPasteboard];
788 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
790 wxWindow* wxpeer = GetWXPeer();
791 if ( wxpeer == NULL )
792 return NSDragOperationNone;
794 wxDropTarget* target = wxpeer->GetDropTarget();
795 if ( target == NULL )
796 return NSDragOperationNone;
798 wxDragResult result = wxDragNone;
799 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
800 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
802 if ( sourceDragMask & NSDragOperationLink )
804 else if ( sourceDragMask & NSDragOperationCopy )
806 else if ( sourceDragMask & NSDragOperationMove )
809 PasteboardRef pboardRef;
810 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
811 target->SetCurrentDragPasteboard(pboardRef);
812 result = target->OnDragOver(pt.x, pt.y, result);
813 CFRelease(pboardRef);
815 NSDragOperation nsresult = NSDragOperationNone;
819 nsresult = NSDragOperationLink;
821 nsresult = NSDragOperationMove;
823 nsresult = NSDragOperationCopy;
830 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
832 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
834 NSPasteboard *pboard = [sender draggingPasteboard];
835 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
837 wxWindow* wxpeer = GetWXPeer();
838 wxDropTarget* target = wxpeer->GetDropTarget();
839 wxDragResult result = wxDragNone;
840 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
841 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
843 if ( sourceDragMask & NSDragOperationLink )
845 else if ( sourceDragMask & NSDragOperationCopy )
847 else if ( sourceDragMask & NSDragOperationMove )
850 PasteboardRef pboardRef;
851 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
852 target->SetCurrentDragPasteboard(pboardRef);
854 if (target->OnDrop(pt.x, pt.y))
855 result = target->OnData(pt.x, pt.y, result);
857 CFRelease(pboardRef);
859 return result != wxDragNone;
862 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
863 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
864 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
865 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
866 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
867 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
869 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
871 if ( !DoHandleMouseEvent(event) )
873 // for plain NSView mouse events would propagate to parents otherwise
874 if (!m_wxPeer->MacIsUserPane())
876 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
877 superimpl(slf, (SEL)_cmd, event);
882 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
884 if ( [event type] == NSKeyDown )
885 m_lastKeyDownEvent = event;
886 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
888 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
889 superimpl(slf, (SEL)_cmd, event);
891 m_lastKeyDownEvent = NULL;
894 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
896 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
898 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
899 superimpl(slf, (SEL)_cmd, text);
904 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
906 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
907 return superimpl(slf, (SEL)_cmd, event);
910 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
912 if ( m_wxPeer->MacIsUserPane() )
913 return m_wxPeer->AcceptsFocus();
916 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
917 return superimpl(slf, (SEL)_cmd);
921 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
923 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
924 // get the current focus before running becomeFirstResponder
925 NSView* otherView = FindFocus();
927 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
928 BOOL r = superimpl(slf, (SEL)_cmd);
931 DoNotifyFocusEvent( true, otherWindow );
937 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
939 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
940 BOOL r = superimpl(slf, (SEL)_cmd);
941 // get the current focus after running resignFirstResponder
942 // note that this value isn't reliable, it might return the same view that
944 NSView* otherView = FindFocus();
945 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
946 // NSTextViews have an editor as true responder, therefore the might get the
947 // resign notification if their editor takes over, don't trigger any event then
948 if ( r && !m_hasEditor)
950 DoNotifyFocusEvent( false, otherWindow );
955 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
957 wxWindow* wxpeer = GetWXPeer();
960 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
963 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
964 superimpl(slf, (SEL)_cmd);
968 [slf addCursorRect: [slf bounds]
974 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
980 #define OSX_DEBUG_DRAWING 0
982 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
984 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
985 CGContextSaveGState( context );
987 #if OSX_DEBUG_DRAWING
988 CGContextBeginPath( context );
989 CGContextMoveToPoint(context, 0, 0);
990 NSRect bounds = [self bounds];
991 CGContextAddLineToPoint(context, 10, 0);
992 CGContextMoveToPoint(context, 0, 0);
993 CGContextAddLineToPoint(context, 0, 10);
994 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
995 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
996 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
997 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
998 CGContextClosePath( context );
999 CGContextStrokePath(context);
1004 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1005 CGContextScaleCTM( context, 1, -1 );
1009 const NSRect *rects;
1012 [slf getRectsBeingDrawn:&rects count:&count];
1013 for ( int i = 0 ; i < count ; ++i )
1015 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1018 wxWindow* wxpeer = GetWXPeer();
1019 wxpeer->GetUpdateRegion() = updateRgn;
1020 wxpeer->MacSetCGContextRef( context );
1022 bool handled = wxpeer->MacDoRedraw( 0 );
1024 CGContextRestoreGState( context );
1026 CGContextSaveGState( context );
1030 SEL _cmd = @selector(drawRect:);
1031 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1032 superimpl(slf, _cmd, *(NSRect*)rect);
1033 CGContextRestoreGState( context );
1034 CGContextSaveGState( context );
1036 wxpeer->MacPaintChildrenBorders();
1037 wxpeer->MacSetCGContextRef( NULL );
1038 CGContextRestoreGState( context );
1041 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1043 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1045 wxpeer->OSXHandleClicked(0);
1048 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1054 #if OBJC_API_VERSION >= 2
1056 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1057 class_addMethod(c, s, i, t );
1061 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1066 void wxOSXCocoaClassAddWXMethods(Class c)
1069 #if OBJC_API_VERSION < 2
1070 static objc_method wxmethods[] =
1074 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1075 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1076 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1078 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1079 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1080 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1082 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1084 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1085 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1086 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1088 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1089 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1090 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1092 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1093 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1094 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1096 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1098 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1100 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1101 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1102 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1103 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1105 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1106 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1108 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1109 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1111 #if wxUSE_DRAG_AND_DROP
1112 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1113 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1114 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1115 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1118 #if OBJC_API_VERSION < 2
1120 static int method_count = WXSIZEOF( wxmethods );
1121 static objc_method_list *wxmethodlist = NULL;
1122 if ( wxmethodlist == NULL )
1124 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1125 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1126 wxmethodlist->method_count = method_count;
1127 wxmethodlist->obsolete = 0;
1129 class_addMethods( c, wxmethodlist );
1134 // C++ implementation class
1137 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1139 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1140 wxWidgetImpl( peer, isRootControl )
1145 // check if the user wants to create the control initially hidden
1146 if ( !peer->IsShown() )
1147 SetVisibility(false);
1149 // gc aware handling
1151 CFRetain(m_osxView);
1152 [m_osxView release];
1155 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1160 void wxWidgetCocoaImpl::Init()
1164 m_lastKeyDownEvent = NULL;
1165 m_hasEditor = false;
1168 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1170 RemoveAssociations( this );
1172 if ( !IsRootControl() )
1174 NSView *sv = [m_osxView superview];
1176 [m_osxView removeFromSuperview];
1178 // gc aware handling
1180 CFRelease(m_osxView);
1183 bool wxWidgetCocoaImpl::IsVisible() const
1185 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1188 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1190 [m_osxView setHidden:(visible ? NO:YES)];
1193 void wxWidgetCocoaImpl::Raise()
1198 void wxWidgetCocoaImpl::Lower()
1203 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1208 // We should do something like this, but it wasn't working in 10.4.
1209 if (GetNeedsDisplay() )
1213 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1214 NSSize offset = NSMakeSize((float)dx, (float)dy);
1215 [m_osxView scrollRect:r by:offset];
1219 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1221 wxWindowMac* parent = GetWXPeer()->GetParent();
1222 // under Cocoa we might have a contentView in the wxParent to which we have to
1223 // adjust the coordinates
1224 if (parent && [m_osxView superview] != parent->GetHandle() )
1226 int cx = 0,cy = 0,cw = 0,ch = 0;
1227 if ( parent->GetPeer() )
1229 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1234 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1235 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1236 [m_osxView setFrame:r];
1237 [[m_osxView superview] setNeedsDisplayInRect:r];
1239 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1241 if ( [(wxNSView*)m_osxView trackingTag] )
1242 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1244 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1248 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1250 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1255 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1257 NSRect rect = [m_osxView frame];
1258 width = (int)rect.size.width;
1259 height = (int)rect.size.height;
1262 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1264 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1266 NSView* cv = [m_osxView contentView];
1268 NSRect bounds = [m_osxView bounds];
1269 NSRect rect = [cv frame];
1271 int y = (int)rect.origin.y;
1272 int x = (int)rect.origin.x;
1273 if ( ![ m_osxView isFlipped ] )
1274 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1277 width = (int)rect.size.width;
1278 height = (int)rect.size.height;
1283 GetSize( width, height );
1287 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1290 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1292 [m_osxView setNeedsDisplay:YES];
1295 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1297 return [m_osxView needsDisplay];
1300 bool wxWidgetCocoaImpl::CanFocus() const
1302 return [m_osxView canBecomeKeyView] == YES;
1305 bool wxWidgetCocoaImpl::HasFocus() const
1307 return ( FindFocus() == m_osxView );
1310 bool wxWidgetCocoaImpl::SetFocus()
1312 if ( [m_osxView canBecomeKeyView] == NO )
1315 [[m_osxView window] makeFirstResponder: m_osxView] ;
1316 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1321 void wxWidgetCocoaImpl::RemoveFromParent()
1323 [m_osxView removeFromSuperview];
1326 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1328 NSView* container = parent->GetWXWidget() ;
1329 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1330 [container addSubview:m_osxView];
1333 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1335 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1338 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1340 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1342 wxCFStringRef cf( title , encoding );
1343 [m_osxView setTitle:cf.AsNSString()];
1345 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1347 wxCFStringRef cf( title , encoding );
1348 [m_osxView setStringValue:cf.AsNSString()];
1353 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1355 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1356 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1357 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1360 wxInt32 wxWidgetCocoaImpl::GetValue() const
1362 return [(NSControl*)m_osxView intValue];
1365 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1367 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1369 [m_osxView setIntValue:v];
1371 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1373 [m_osxView setFloatValue:(double)v];
1375 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1377 [m_osxView setDoubleValue:(double)v];
1381 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1383 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1385 [m_osxView setMinValue:(double)v];
1389 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1391 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1393 [m_osxView setMaxValue:(double)v];
1397 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1399 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1401 return (int)[m_osxView minValue];
1406 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1408 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1410 return (int)[m_osxView maxValue];
1415 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1419 // TODO: how to create a wxBitmap from NSImage?
1421 if ( [m_osxView respondsToSelector:@selector(image:)] )
1422 bmp = [m_osxView image];
1428 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1430 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1432 [m_osxView setImage:bitmap.GetNSImage()];
1436 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1438 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1440 NSCellImagePosition pos;
1460 wxFAIL_MSG( "invalid image position" );
1464 [m_osxView setImagePosition:pos];
1468 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1470 // implementation in subclass
1473 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1475 r->x = r->y = r->width = r->height = 0;
1477 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1479 NSRect former = [m_osxView frame];
1480 [m_osxView sizeToFit];
1481 NSRect best = [m_osxView frame];
1482 [m_osxView setFrame:former];
1483 r->width = (int)best.size.width;
1484 r->height = (int)best.size.height;
1488 bool wxWidgetCocoaImpl::IsEnabled() const
1490 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1491 return [m_osxView isEnabled];
1495 void wxWidgetCocoaImpl::Enable( bool enable )
1497 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1498 [m_osxView setEnabled:enable];
1501 void wxWidgetCocoaImpl::PulseGauge()
1505 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1509 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1511 NSControlSize size = NSRegularControlSize;
1515 case wxWINDOW_VARIANT_NORMAL :
1516 size = NSRegularControlSize;
1519 case wxWINDOW_VARIANT_SMALL :
1520 size = NSSmallControlSize;
1523 case wxWINDOW_VARIANT_MINI :
1524 size = NSMiniControlSize;
1527 case wxWINDOW_VARIANT_LARGE :
1528 size = NSRegularControlSize;
1532 wxFAIL_MSG(wxT("unexpected window variant"));
1535 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1536 [m_osxView setControlSize:size];
1537 else if ([m_osxView respondsToSelector:@selector(cell)])
1539 id cell = [(id)m_osxView cell];
1540 if ([cell respondsToSelector:@selector(setControlSize:)])
1541 [cell setControlSize:size];
1545 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1547 if ([m_osxView respondsToSelector:@selector(setFont:)])
1548 [m_osxView setFont: font.OSXGetNSFont()];
1551 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1553 WXWidget c = control ? control : (WXWidget) m_osxView;
1554 wxWidgetImpl::Associate( c, this ) ;
1555 if ([c respondsToSelector:@selector(setAction:)])
1558 [c setAction: @selector(controlAction:)];
1559 if ([c respondsToSelector:@selector(setDoubleAction:)])
1561 [c setDoubleAction: @selector(controlDoubleAction:)];
1567 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1569 wxKeyEvent wxevent(wxEVT_CHAR);
1570 SetupKeyEvent( wxevent, event, text );
1572 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1575 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1577 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1578 SetupKeyEvent( wxevent, event );
1579 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1581 // this will fire higher level events, like insertText, to help
1582 // us handle EVT_CHAR, etc.
1584 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1588 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1589 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1591 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1599 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1601 NSPoint clickLocation;
1602 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1603 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1604 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1605 SetupMouseEvent(wxevent , event) ;
1609 return GetWXPeer()->HandleWindowEvent(wxevent);
1612 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1614 wxWindow* thisWindow = GetWXPeer();
1615 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1617 thisWindow->MacInvalidateBorders();
1620 if ( receivedFocus )
1622 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1623 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1624 thisWindow->HandleWindowEvent(eventFocus);
1627 if ( thisWindow->GetCaret() )
1628 thisWindow->GetCaret()->OnSetFocus();
1631 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1632 event.SetEventObject(thisWindow);
1634 event.SetWindow(otherWindow->GetWXPeer());
1635 thisWindow->HandleWindowEvent(event) ;
1637 else // !receivedFocuss
1640 if ( thisWindow->GetCaret() )
1641 thisWindow->GetCaret()->OnKillFocus();
1644 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1646 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1647 event.SetEventObject(thisWindow);
1649 event.SetWindow(otherWindow->GetWXPeer());
1650 thisWindow->HandleWindowEvent(event) ;
1654 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1656 NSPoint location = [NSEvent mouseLocation];
1657 location = [[m_osxView window] convertScreenToBase:location];
1658 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1660 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1662 [(NSCursor*)cursor.GetHCURSOR() set];
1664 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1667 void wxWidgetCocoaImpl::CaptureMouse()
1669 [[m_osxView window] disableCursorRects];
1672 void wxWidgetCocoaImpl::ReleaseMouse()
1674 [[m_osxView window] enableCursorRects];
1677 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1679 m_isFlipped = flipped;
1686 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1687 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1688 long WXUNUSED(style), long WXUNUSED(extraStyle))
1690 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1691 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1693 // temporary hook for dnd
1694 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1695 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1697 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1701 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1703 NSWindow* tlw = now->GetWXWindow();
1704 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1705 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1706 c->InstallEventHandler();
1707 [tlw setContentView:v];