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;
117 - (void)setBackgroundColor:(NSColor*)aColor;
118 - (void)setImagePosition:(NSCellImagePosition)aPosition;
121 long wxOSXTranslateCocoaKey( NSEvent* event )
125 if ([event type] != NSFlagsChanged)
127 NSString* s = [event charactersIgnoringModifiers];
128 // backspace char reports as delete w/modifiers for some reason
131 switch ( [s characterAtIndex:0] )
138 case NSUpArrowFunctionKey :
141 case NSDownArrowFunctionKey :
144 case NSLeftArrowFunctionKey :
147 case NSRightArrowFunctionKey :
150 case NSInsertFunctionKey :
153 case NSDeleteFunctionKey :
156 case NSHomeFunctionKey :
159 // case NSBeginFunctionKey :
160 // retval = WXK_BEGIN;
162 case NSEndFunctionKey :
165 case NSPageUpFunctionKey :
168 case NSPageDownFunctionKey :
169 retval = WXK_PAGEDOWN;
171 case NSHelpFunctionKey :
175 int intchar = [s characterAtIndex: 0];
176 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
177 retval = WXK_F1 + (intchar - NSF1FunctionKey );
183 // Some keys don't seem to have constants. The code mimics the approach
184 // taken by WebKit. See:
185 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
186 switch( [event keyCode] )
191 retval = WXK_COMMAND;
195 retval = WXK_CAPITAL;
198 case 56: // Left Shift
199 case 60: // Right Shift
204 case 61: // Right Alt
208 case 59: // Left Ctrl
209 case 62: // Right Ctrl
210 retval = WXK_CONTROL;
222 retval = WXK_NUMPAD_DIVIDE;
225 retval = WXK_NUMPAD_MULTIPLY;
228 retval = WXK_NUMPAD_SUBTRACT;
231 retval = WXK_NUMPAD_ADD;
234 retval = WXK_NUMPAD_ENTER;
237 retval = WXK_NUMPAD_DECIMAL;
240 retval = WXK_NUMPAD0;
243 retval = WXK_NUMPAD1;
246 retval = WXK_NUMPAD2;
249 retval = WXK_NUMPAD3;
252 retval = WXK_NUMPAD4;
255 retval = WXK_NUMPAD5;
258 retval = WXK_NUMPAD6;
261 retval = WXK_NUMPAD7;
264 retval = WXK_NUMPAD8;
267 retval = WXK_NUMPAD9;
270 //retval = [event keyCode];
276 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
278 UInt32 modifiers = [nsEvent modifierFlags] ;
279 int eventType = [nsEvent type];
281 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
282 wxevent.m_controlDown = modifiers & NSControlKeyMask;
283 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
284 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
286 wxevent.m_rawCode = [nsEvent keyCode];
287 wxevent.m_rawFlags = modifiers;
289 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
292 if ( eventType != NSFlagsChanged )
294 NSString* nschars = (wxevent.GetEventType() != wxEVT_CHAR) ? [nsEvent charactersIgnoringModifiers] : [nsEvent characters];
297 // if charString is set, it did not come from key up / key down
298 wxevent.SetEventType( wxEVT_CHAR );
299 chars = wxCFStringRef::AsString(charString);
303 chars = wxCFStringRef::AsString(nschars);
307 int aunichar = chars.Length() > 0 ? chars[0] : 0;
310 if (wxevent.GetEventType() != wxEVT_CHAR)
312 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
316 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
319 wxevent.SetEventType( wxEVT_KEY_UP ) ;
321 case NSFlagsChanged :
325 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
328 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
331 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
334 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
345 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
346 keyval = wxToupper( aunichar ) ;
352 wxevent.m_uniChar = aunichar;
354 wxevent.m_keyCode = keyval;
356 wxWindowMac* peer = GetWXPeer();
359 wxevent.SetEventObject(peer);
360 wxevent.SetId(peer->GetId()) ;
364 UInt32 g_lastButton = 0 ;
365 bool g_lastButtonWasFakeRight = false ;
367 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
369 int eventType = [nsEvent type];
370 UInt32 modifiers = [nsEvent modifierFlags] ;
371 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
373 // these parameters are not given for all events
374 UInt32 button = [nsEvent buttonNumber];
375 UInt32 clickCount = 0;
377 wxevent.m_x = screenMouseLocation.x;
378 wxevent.m_y = screenMouseLocation.y;
379 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
380 wxevent.m_controlDown = modifiers & NSControlKeyMask;
381 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
382 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
383 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
385 UInt32 mouseChord = 0;
389 case NSLeftMouseDown :
390 case NSLeftMouseDragged :
393 case NSRightMouseDown :
394 case NSRightMouseDragged :
397 case NSOtherMouseDown :
398 case NSOtherMouseDragged :
403 // a control click is interpreted as a right click
404 bool thisButtonIsFakeRight = false ;
405 if ( button == 0 && (modifiers & NSControlKeyMask) )
408 thisButtonIsFakeRight = true ;
411 // otherwise we report double clicks by connecting a left click with a ctrl-left click
412 if ( clickCount > 1 && button != g_lastButton )
415 // we must make sure that our synthetic 'right' button corresponds in
416 // mouse down, moved and mouse up, and does not deliver a right down and left up
419 case NSLeftMouseDown :
420 case NSRightMouseDown :
421 case NSOtherMouseDown :
422 g_lastButton = button ;
423 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
430 g_lastButtonWasFakeRight = false ;
432 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
433 button = g_lastButton ;
435 // Adjust the chord mask to remove the primary button and add the
436 // secondary button. It is possible that the secondary button is
437 // already pressed, e.g. on a mouse connected to a laptop, but this
438 // possibility is ignored here:
439 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
440 mouseChord = ((mouseChord & ~1U) | 2U);
443 wxevent.m_leftDown = true ;
445 wxevent.m_rightDown = true ;
447 wxevent.m_middleDown = true ;
449 // translate into wx types
452 case NSLeftMouseDown :
453 case NSRightMouseDown :
454 case NSOtherMouseDown :
455 clickCount = [nsEvent clickCount];
459 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
463 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
467 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
476 case NSRightMouseUp :
477 case NSOtherMouseUp :
478 clickCount = [nsEvent clickCount];
482 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
486 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
490 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
500 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
501 wxevent.m_wheelDelta = 10;
502 wxevent.m_linesPerAction = 1;
504 if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
506 wxevent.m_wheelAxis = 1;
507 wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
511 wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
516 case NSMouseEntered :
517 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
520 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
522 case NSLeftMouseDragged :
523 case NSRightMouseDragged :
524 case NSOtherMouseDragged :
526 wxevent.SetEventType( wxEVT_MOTION ) ;
532 wxevent.m_clickCount = clickCount;
533 wxWindowMac* peer = GetWXPeer();
536 wxevent.SetEventObject(peer);
537 wxevent.SetId(peer->GetId()) ;
541 @implementation wxNSView
545 static BOOL initialized = NO;
549 wxOSXCocoaClassAddWXMethods( self );
553 - (void) setTrackingTag: (NSTrackingRectTag)tag
558 - (NSTrackingRectTag) trackingTag
569 #if wxUSE_DRAG_AND_DROP
571 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
572 // for details on the NSPasteboard -> PasteboardRef conversion
574 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
576 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
578 return NSDragOperationNone;
580 return impl->draggingEntered(sender, self, _cmd);
583 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
585 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
589 return impl->draggingExited(sender, self, _cmd);
592 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
594 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
596 return NSDragOperationNone;
598 return impl->draggingUpdated(sender, self, _cmd);
601 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
603 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
605 return NSDragOperationNone;
607 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
612 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
614 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
618 impl->mouseEvent(event, self, _cmd);
621 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
623 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
627 impl->keyEvent(event, self, _cmd);
630 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
632 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
636 impl->insertText(text, self, _cmd);
639 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
641 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
645 return impl->performKeyEquivalent(event, self, _cmd);
648 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
650 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
654 return impl->acceptsFirstResponder(self, _cmd);
657 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
659 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
663 return impl->becomeFirstResponder(self, _cmd);
666 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
668 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
672 return impl->resignFirstResponder(self, _cmd);
675 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
677 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
681 impl->resetCursorRects(self, _cmd);
684 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
686 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
690 return impl->isFlipped(self, _cmd) ? YES:NO;
693 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
695 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
699 return impl->drawRect(&rect, self, _cmd);
702 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
704 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
708 impl->controlAction(self, _cmd, sender);
711 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
713 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
717 impl->controlDoubleAction(self, _cmd, sender);
720 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
722 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
723 NSPasteboard *pboard = [sender draggingPasteboard];
724 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
726 wxWindow* wxpeer = GetWXPeer();
727 if ( wxpeer == NULL )
728 return NSDragOperationNone;
730 wxDropTarget* target = wxpeer->GetDropTarget();
731 if ( target == NULL )
732 return NSDragOperationNone;
734 wxDragResult result = wxDragNone;
735 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
736 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
738 if ( sourceDragMask & NSDragOperationLink )
740 else if ( sourceDragMask & NSDragOperationCopy )
742 else if ( sourceDragMask & NSDragOperationMove )
745 PasteboardRef pboardRef;
746 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
747 target->SetCurrentDragPasteboard(pboardRef);
748 result = target->OnEnter(pt.x, pt.y, result);
749 CFRelease(pboardRef);
751 NSDragOperation nsresult = NSDragOperationNone;
755 nsresult = NSDragOperationLink;
757 nsresult = NSDragOperationMove;
759 nsresult = NSDragOperationCopy;
766 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
768 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
769 NSPasteboard *pboard = [sender draggingPasteboard];
771 wxWindow* wxpeer = GetWXPeer();
772 if ( wxpeer == NULL )
775 wxDropTarget* target = wxpeer->GetDropTarget();
776 if ( target == NULL )
779 PasteboardRef pboardRef;
780 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
781 target->SetCurrentDragPasteboard(pboardRef);
783 CFRelease(pboardRef);
786 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
788 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
789 NSPasteboard *pboard = [sender draggingPasteboard];
790 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
792 wxWindow* wxpeer = GetWXPeer();
793 if ( wxpeer == NULL )
794 return NSDragOperationNone;
796 wxDropTarget* target = wxpeer->GetDropTarget();
797 if ( target == NULL )
798 return NSDragOperationNone;
800 wxDragResult result = wxDragNone;
801 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
802 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
804 if ( sourceDragMask & NSDragOperationLink )
806 else if ( sourceDragMask & NSDragOperationCopy )
808 else if ( sourceDragMask & NSDragOperationMove )
811 PasteboardRef pboardRef;
812 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
813 target->SetCurrentDragPasteboard(pboardRef);
814 result = target->OnDragOver(pt.x, pt.y, result);
815 CFRelease(pboardRef);
817 NSDragOperation nsresult = NSDragOperationNone;
821 nsresult = NSDragOperationLink;
823 nsresult = NSDragOperationMove;
825 nsresult = NSDragOperationCopy;
832 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
834 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
836 NSPasteboard *pboard = [sender draggingPasteboard];
837 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
839 wxWindow* wxpeer = GetWXPeer();
840 wxDropTarget* target = wxpeer->GetDropTarget();
841 wxDragResult result = wxDragNone;
842 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
843 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
845 if ( sourceDragMask & NSDragOperationLink )
847 else if ( sourceDragMask & NSDragOperationCopy )
849 else if ( sourceDragMask & NSDragOperationMove )
852 PasteboardRef pboardRef;
853 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
854 target->SetCurrentDragPasteboard(pboardRef);
856 if (target->OnDrop(pt.x, pt.y))
857 result = target->OnData(pt.x, pt.y, result);
859 CFRelease(pboardRef);
861 return result != wxDragNone;
864 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
865 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
866 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
867 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
868 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
869 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
871 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
873 if ( !DoHandleMouseEvent(event) )
875 // for plain NSView mouse events would propagate to parents otherwise
876 if (!m_wxPeer->MacIsUserPane())
878 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
879 superimpl(slf, (SEL)_cmd, event);
884 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
886 if ( [event type] == NSKeyDown )
887 m_lastKeyDownEvent = event;
888 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
890 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
891 superimpl(slf, (SEL)_cmd, event);
893 m_lastKeyDownEvent = NULL;
896 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
898 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
900 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
901 superimpl(slf, (SEL)_cmd, text);
906 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
908 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
909 return superimpl(slf, (SEL)_cmd, event);
912 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
914 if ( m_wxPeer->MacIsUserPane() )
915 return m_wxPeer->AcceptsFocus();
918 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
919 return superimpl(slf, (SEL)_cmd);
923 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
925 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
926 // get the current focus before running becomeFirstResponder
927 NSView* otherView = FindFocus();
929 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
930 BOOL r = superimpl(slf, (SEL)_cmd);
933 DoNotifyFocusEvent( true, otherWindow );
939 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
941 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
942 BOOL r = superimpl(slf, (SEL)_cmd);
943 // get the current focus after running resignFirstResponder
944 // note that this value isn't reliable, it might return the same view that
946 NSView* otherView = FindFocus();
947 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
948 // NSTextViews have an editor as true responder, therefore the might get the
949 // resign notification if their editor takes over, don't trigger any event then
950 if ( r && !m_hasEditor)
952 DoNotifyFocusEvent( false, otherWindow );
957 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
959 wxWindow* wxpeer = GetWXPeer();
962 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
965 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
966 superimpl(slf, (SEL)_cmd);
970 [slf addCursorRect: [slf bounds]
976 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
982 #define OSX_DEBUG_DRAWING 0
984 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
986 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
987 CGContextSaveGState( context );
989 #if OSX_DEBUG_DRAWING
990 CGContextBeginPath( context );
991 CGContextMoveToPoint(context, 0, 0);
992 NSRect bounds = [self bounds];
993 CGContextAddLineToPoint(context, 10, 0);
994 CGContextMoveToPoint(context, 0, 0);
995 CGContextAddLineToPoint(context, 0, 10);
996 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
997 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
998 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
999 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1000 CGContextClosePath( context );
1001 CGContextStrokePath(context);
1006 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1007 CGContextScaleCTM( context, 1, -1 );
1011 const NSRect *rects;
1014 [slf getRectsBeingDrawn:&rects count:&count];
1015 for ( int i = 0 ; i < count ; ++i )
1017 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1020 wxWindow* wxpeer = GetWXPeer();
1021 wxpeer->GetUpdateRegion() = updateRgn;
1022 wxpeer->MacSetCGContextRef( context );
1024 bool handled = wxpeer->MacDoRedraw( 0 );
1026 CGContextRestoreGState( context );
1028 CGContextSaveGState( context );
1032 SEL _cmd = @selector(drawRect:);
1033 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1034 superimpl(slf, _cmd, *(NSRect*)rect);
1035 CGContextRestoreGState( context );
1036 CGContextSaveGState( context );
1038 wxpeer->MacPaintChildrenBorders();
1039 wxpeer->MacSetCGContextRef( NULL );
1040 CGContextRestoreGState( context );
1043 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1045 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1047 wxpeer->OSXHandleClicked(0);
1050 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1056 #if OBJC_API_VERSION >= 2
1058 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1059 class_addMethod(c, s, i, t );
1063 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1068 void wxOSXCocoaClassAddWXMethods(Class c)
1071 #if OBJC_API_VERSION < 2
1072 static objc_method wxmethods[] =
1076 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1077 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1078 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1080 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1081 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1082 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1084 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1086 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1087 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1088 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1090 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1091 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1092 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1094 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1095 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1096 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1098 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1100 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1102 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1103 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1104 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1105 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1107 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1108 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1110 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1111 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1113 #if wxUSE_DRAG_AND_DROP
1114 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1115 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1116 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1117 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1120 #if OBJC_API_VERSION < 2
1122 static int method_count = WXSIZEOF( wxmethods );
1123 static objc_method_list *wxmethodlist = NULL;
1124 if ( wxmethodlist == NULL )
1126 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1127 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1128 wxmethodlist->method_count = method_count;
1129 wxmethodlist->obsolete = 0;
1131 class_addMethods( c, wxmethodlist );
1136 // C++ implementation class
1139 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1141 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1142 wxWidgetImpl( peer, isRootControl )
1147 // check if the user wants to create the control initially hidden
1148 if ( !peer->IsShown() )
1149 SetVisibility(false);
1151 // gc aware handling
1153 CFRetain(m_osxView);
1154 [m_osxView release];
1157 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1162 void wxWidgetCocoaImpl::Init()
1166 m_lastKeyDownEvent = NULL;
1167 m_hasEditor = false;
1170 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1172 RemoveAssociations( this );
1174 if ( !IsRootControl() )
1176 NSView *sv = [m_osxView superview];
1178 [m_osxView removeFromSuperview];
1180 // gc aware handling
1182 CFRelease(m_osxView);
1185 bool wxWidgetCocoaImpl::IsVisible() const
1187 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1190 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1192 [m_osxView setHidden:(visible ? NO:YES)];
1195 void wxWidgetCocoaImpl::Raise()
1200 void wxWidgetCocoaImpl::Lower()
1205 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1210 // We should do something like this, but it wasn't working in 10.4.
1211 if (GetNeedsDisplay() )
1215 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1216 NSSize offset = NSMakeSize((float)dx, (float)dy);
1217 [m_osxView scrollRect:r by:offset];
1221 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1223 wxWindowMac* parent = GetWXPeer()->GetParent();
1224 // under Cocoa we might have a contentView in the wxParent to which we have to
1225 // adjust the coordinates
1226 if (parent && [m_osxView superview] != parent->GetHandle() )
1228 int cx = 0,cy = 0,cw = 0,ch = 0;
1229 if ( parent->GetPeer() )
1231 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1236 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1237 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1238 [m_osxView setFrame:r];
1239 [[m_osxView superview] setNeedsDisplayInRect:r];
1241 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1243 if ( [(wxNSView*)m_osxView trackingTag] )
1244 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1246 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1250 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1252 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1257 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1259 NSRect rect = [m_osxView frame];
1260 width = (int)rect.size.width;
1261 height = (int)rect.size.height;
1264 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1266 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1268 NSView* cv = [m_osxView contentView];
1270 NSRect bounds = [m_osxView bounds];
1271 NSRect rect = [cv frame];
1273 int y = (int)rect.origin.y;
1274 int x = (int)rect.origin.x;
1275 if ( ![ m_osxView isFlipped ] )
1276 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1279 width = (int)rect.size.width;
1280 height = (int)rect.size.height;
1285 GetSize( width, height );
1289 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1292 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1294 [m_osxView setNeedsDisplay:YES];
1297 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1299 return [m_osxView needsDisplay];
1302 bool wxWidgetCocoaImpl::CanFocus() const
1304 return [m_osxView canBecomeKeyView] == YES;
1307 bool wxWidgetCocoaImpl::HasFocus() const
1309 return ( FindFocus() == m_osxView );
1312 bool wxWidgetCocoaImpl::SetFocus()
1314 if ( [m_osxView canBecomeKeyView] == NO )
1317 [[m_osxView window] makeFirstResponder: m_osxView] ;
1318 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1323 void wxWidgetCocoaImpl::RemoveFromParent()
1325 [m_osxView removeFromSuperview];
1328 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1330 NSView* container = parent->GetWXWidget() ;
1331 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1332 [container addSubview:m_osxView];
1335 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1337 NSView* targetView = m_osxView;
1338 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1339 targetView = [(NSScrollView*) m_osxView documentView];
1341 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1343 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1344 green:(CGFloat) (col.Green() / 255.0)
1345 blue:(CGFloat) (col.Blue() / 255.0)
1346 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1350 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1352 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1354 wxCFStringRef cf( title , encoding );
1355 [m_osxView setTitle:cf.AsNSString()];
1357 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1359 wxCFStringRef cf( title , encoding );
1360 [m_osxView setStringValue:cf.AsNSString()];
1365 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1367 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1368 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1369 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1372 wxInt32 wxWidgetCocoaImpl::GetValue() const
1374 return [(NSControl*)m_osxView intValue];
1377 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1379 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1381 [m_osxView setIntValue:v];
1383 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1385 [m_osxView setFloatValue:(double)v];
1387 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1389 [m_osxView setDoubleValue:(double)v];
1393 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1395 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1397 [m_osxView setMinValue:(double)v];
1401 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1403 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1405 [m_osxView setMaxValue:(double)v];
1409 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1411 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1413 return (int)[m_osxView minValue];
1418 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1420 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1422 return (int)[m_osxView maxValue];
1427 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1431 // TODO: how to create a wxBitmap from NSImage?
1433 if ( [m_osxView respondsToSelector:@selector(image:)] )
1434 bmp = [m_osxView image];
1440 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1442 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1444 [m_osxView setImage:bitmap.GetNSImage()];
1448 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1450 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1452 NSCellImagePosition pos;
1472 wxFAIL_MSG( "invalid image position" );
1476 [m_osxView setImagePosition:pos];
1480 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1482 // implementation in subclass
1485 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1487 r->x = r->y = r->width = r->height = 0;
1489 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1491 NSRect former = [m_osxView frame];
1492 [m_osxView sizeToFit];
1493 NSRect best = [m_osxView frame];
1494 [m_osxView setFrame:former];
1495 r->width = (int)best.size.width;
1496 r->height = (int)best.size.height;
1500 bool wxWidgetCocoaImpl::IsEnabled() const
1502 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1503 return [m_osxView isEnabled];
1507 void wxWidgetCocoaImpl::Enable( bool enable )
1509 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1510 [m_osxView setEnabled:enable];
1513 void wxWidgetCocoaImpl::PulseGauge()
1517 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1521 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1523 NSControlSize size = NSRegularControlSize;
1527 case wxWINDOW_VARIANT_NORMAL :
1528 size = NSRegularControlSize;
1531 case wxWINDOW_VARIANT_SMALL :
1532 size = NSSmallControlSize;
1535 case wxWINDOW_VARIANT_MINI :
1536 size = NSMiniControlSize;
1539 case wxWINDOW_VARIANT_LARGE :
1540 size = NSRegularControlSize;
1544 wxFAIL_MSG(wxT("unexpected window variant"));
1547 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1548 [m_osxView setControlSize:size];
1549 else if ([m_osxView respondsToSelector:@selector(cell)])
1551 id cell = [(id)m_osxView cell];
1552 if ([cell respondsToSelector:@selector(setControlSize:)])
1553 [cell setControlSize:size];
1557 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1559 if ([m_osxView respondsToSelector:@selector(setFont:)])
1560 [m_osxView setFont: font.OSXGetNSFont()];
1563 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1565 WXWidget c = control ? control : (WXWidget) m_osxView;
1566 wxWidgetImpl::Associate( c, this ) ;
1567 if ([c respondsToSelector:@selector(setAction:)])
1570 [c setAction: @selector(controlAction:)];
1571 if ([c respondsToSelector:@selector(setDoubleAction:)])
1573 [c setDoubleAction: @selector(controlDoubleAction:)];
1579 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1581 wxKeyEvent wxevent(wxEVT_CHAR);
1582 SetupKeyEvent( wxevent, event, text );
1584 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1587 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1589 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1590 SetupKeyEvent( wxevent, event );
1591 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1593 // this will fire higher level events, like insertText, to help
1594 // us handle EVT_CHAR, etc.
1596 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1600 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1601 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1603 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1611 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1613 NSPoint clickLocation;
1614 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1615 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1616 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1617 SetupMouseEvent(wxevent , event) ;
1621 return GetWXPeer()->HandleWindowEvent(wxevent);
1624 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1626 wxWindow* thisWindow = GetWXPeer();
1627 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1629 thisWindow->MacInvalidateBorders();
1632 if ( receivedFocus )
1634 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1635 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1636 thisWindow->HandleWindowEvent(eventFocus);
1639 if ( thisWindow->GetCaret() )
1640 thisWindow->GetCaret()->OnSetFocus();
1643 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1644 event.SetEventObject(thisWindow);
1646 event.SetWindow(otherWindow->GetWXPeer());
1647 thisWindow->HandleWindowEvent(event) ;
1649 else // !receivedFocuss
1652 if ( thisWindow->GetCaret() )
1653 thisWindow->GetCaret()->OnKillFocus();
1656 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1658 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1659 event.SetEventObject(thisWindow);
1661 event.SetWindow(otherWindow->GetWXPeer());
1662 thisWindow->HandleWindowEvent(event) ;
1666 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1668 NSPoint location = [NSEvent mouseLocation];
1669 location = [[m_osxView window] convertScreenToBase:location];
1670 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1672 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1674 [(NSCursor*)cursor.GetHCURSOR() set];
1676 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1679 void wxWidgetCocoaImpl::CaptureMouse()
1681 [[m_osxView window] disableCursorRects];
1684 void wxWidgetCocoaImpl::ReleaseMouse()
1686 [[m_osxView window] enableCursorRects];
1689 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1691 m_isFlipped = flipped;
1698 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1699 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1700 long WXUNUSED(style), long WXUNUSED(extraStyle))
1702 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1703 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1705 // temporary hook for dnd
1706 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1707 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1709 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1713 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1715 NSWindow* tlw = now->GetWXWindow();
1716 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1717 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1718 c->InstallEventHandler();
1719 [tlw setContentView:v];