1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/window.mm
3 // Purpose: widgets (non tlw) for cocoa
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/dcclient.h"
18 #include "wx/textctrl.h"
19 #include "wx/combobox.h"
23 #include "wx/osx/private.h"
26 #include "wx/evtloop.h"
32 #if wxUSE_DRAG_AND_DROP
37 #include "wx/tooltip.h"
40 #include <objc/objc-runtime.h>
42 // Get the window with the focus
44 NSView* GetViewFromResponder( NSResponder* responder )
47 if ( [responder isKindOfClass:[NSTextView class]] )
49 NSView* delegate = (NSView*) [(NSTextView*)responder delegate];
50 if ( [delegate isKindOfClass:[NSTextField class] ] )
53 view = (NSView*) responder;
57 if ( [responder isKindOfClass:[NSView class]] )
58 view = (NSView*) responder;
63 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
65 NSView* focusedView = nil;
66 if ( keyWindow != nil )
67 focusedView = GetViewFromResponder([keyWindow firstResponder]);
72 WXWidget wxWidgetImpl::FindFocus()
74 return GetFocusedViewInWindow( [NSApp keyWindow] );
77 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
81 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
82 wxRect bounds(x,y,w,h);
83 NSView* sv = (window->GetParent()->GetHandle() );
85 return wxToNSRect( sv, bounds );
88 @interface wxNSView : NSView
91 NSTrackingRectTag _lastToolTipTrackTag;
99 @interface NSView(PossibleMethods)
100 - (void)setTitle:(NSString *)aString;
101 - (void)setStringValue:(NSString *)aString;
102 - (void)setIntValue:(int)anInt;
103 - (void)setFloatValue:(float)aFloat;
104 - (void)setDoubleValue:(double)aDouble;
108 - (void)setMinValue:(double)aDouble;
109 - (void)setMaxValue:(double)aDouble;
114 - (void)setEnabled:(BOOL)flag;
116 - (void)setImage:(NSImage *)image;
117 - (void)setControlSize:(NSControlSize)size;
119 - (void)setFont:(NSFont *)fontObject;
123 - (void)setTarget:(id)anObject;
124 - (void)setAction:(SEL)aSelector;
125 - (void)setDoubleAction:(SEL)aSelector;
126 - (void)setBackgroundColor:(NSColor*)aColor;
127 - (void)setOpaque:(BOOL)opaque;
128 - (void)setTextColor:(NSColor *)color;
129 - (void)setImagePosition:(NSCellImagePosition)aPosition;
132 // The following code is a combination of the code listed here:
133 // http://lists.apple.com/archives/cocoa-dev/2008/Apr/msg01582.html
134 // (which can't be used because KLGetCurrentKeyboardLayout etc aren't 64-bit)
135 // and the code here:
136 // http://inquisitivecocoa.com/category/objective-c/
137 @interface NSEvent (OsGuiUtilsAdditions)
138 - (NSString*) charactersIgnoringModifiersIncludingShift;
141 @implementation NSEvent (OsGuiUtilsAdditions)
142 - (NSString*) charactersIgnoringModifiersIncludingShift {
143 // First try -charactersIgnoringModifiers and look for keys which UCKeyTranslate translates
144 // differently than AppKit.
145 NSString* c = [self charactersIgnoringModifiers];
146 if ([c length] == 1) {
147 unichar codepoint = [c characterAtIndex:0];
148 if ((codepoint >= 0xF700 && codepoint <= 0xF8FF) || codepoint == 0x7F) {
152 // This is not a "special" key, so ask UCKeyTranslate to give us the character with no
153 // modifiers attached. Actually, that's not quite accurate; we attach the Command modifier
154 // which hints the OS to use Latin characters where possible, which is generally what we want.
155 NSString* result = @"";
156 TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
157 CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
158 CFRelease(currentKeyboard);
160 // this can happen for some non-U.S. input methods (eg. Romaji or Hiragana)
163 const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
164 if (keyboardLayout) {
165 UInt32 deadKeyState = 0;
166 UniCharCount maxStringLength = 255;
167 UniCharCount actualStringLength = 0;
168 UniChar unicodeString[maxStringLength];
170 OSStatus status = UCKeyTranslate(keyboardLayout,
173 cmdKey >> 8, // force the Command key to "on"
175 kUCKeyTranslateNoDeadKeysMask,
182 result = [NSString stringWithCharacters:unicodeString length:(NSInteger)actualStringLength];
188 long wxOSXTranslateCocoaKey( NSEvent* event, int eventType )
192 if ([event type] != NSFlagsChanged)
194 NSString* s = [event charactersIgnoringModifiersIncludingShift];
195 // backspace char reports as delete w/modifiers for some reason
198 if ( eventType == wxEVT_CHAR && ([event modifierFlags] & NSControlKeyMask) && ( [s characterAtIndex:0] >= 'a' && [s characterAtIndex:0] <= 'z' ) )
200 retval = WXK_CONTROL_A + ([s characterAtIndex:0] - 'a');
204 switch ( [s characterAtIndex:0] )
211 case NSUpArrowFunctionKey :
214 case NSDownArrowFunctionKey :
217 case NSLeftArrowFunctionKey :
220 case NSRightArrowFunctionKey :
223 case NSInsertFunctionKey :
226 case NSDeleteFunctionKey :
229 case NSHomeFunctionKey :
232 // case NSBeginFunctionKey :
233 // retval = WXK_BEGIN;
235 case NSEndFunctionKey :
238 case NSPageUpFunctionKey :
241 case NSPageDownFunctionKey :
242 retval = WXK_PAGEDOWN;
244 case NSHelpFunctionKey :
248 int intchar = [s characterAtIndex: 0];
249 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
250 retval = WXK_F1 + (intchar - NSF1FunctionKey );
251 else if ( intchar > 0 && intchar < 32 )
259 // Some keys don't seem to have constants. The code mimics the approach
260 // taken by WebKit. See:
261 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
262 switch( [event keyCode] )
267 retval = WXK_CONTROL;
271 retval = WXK_CAPITAL;
274 case 56: // Left Shift
275 case 60: // Right Shift
280 case 61: // Right Alt
284 case 59: // Left Ctrl
285 case 62: // Right Ctrl
286 retval = WXK_RAW_CONTROL;
298 retval = WXK_NUMPAD_DIVIDE;
301 retval = WXK_NUMPAD_MULTIPLY;
304 retval = WXK_NUMPAD_SUBTRACT;
307 retval = WXK_NUMPAD_ADD;
310 retval = WXK_NUMPAD_ENTER;
313 retval = WXK_NUMPAD_DECIMAL;
316 retval = WXK_NUMPAD0;
319 retval = WXK_NUMPAD1;
322 retval = WXK_NUMPAD2;
325 retval = WXK_NUMPAD3;
328 retval = WXK_NUMPAD4;
331 retval = WXK_NUMPAD5;
334 retval = WXK_NUMPAD6;
337 retval = WXK_NUMPAD7;
340 retval = WXK_NUMPAD8;
343 retval = WXK_NUMPAD9;
346 //retval = [event keyCode];
352 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
354 UInt32 modifiers = [nsEvent modifierFlags] ;
355 int eventType = [nsEvent type];
357 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
358 wxevent.m_rawControlDown = modifiers & NSControlKeyMask;
359 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
360 wxevent.m_controlDown = modifiers & NSCommandKeyMask;
362 wxevent.m_rawCode = [nsEvent keyCode];
363 wxevent.m_rawFlags = modifiers;
365 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
368 if ( eventType != NSFlagsChanged )
370 NSString* nschars = [[nsEvent charactersIgnoringModifiersIncludingShift] uppercaseString];
373 // if charString is set, it did not come from key up / key down
374 wxevent.SetEventType( wxEVT_CHAR );
375 chars = wxCFStringRef::AsString(charString);
379 chars = wxCFStringRef::AsString(nschars);
383 int aunichar = chars.Length() > 0 ? chars[0] : 0;
386 if (wxevent.GetEventType() != wxEVT_CHAR)
388 keyval = wxOSXTranslateCocoaKey(nsEvent, wxevent.GetEventType()) ;
392 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
395 wxevent.SetEventType( wxEVT_KEY_UP ) ;
397 case NSFlagsChanged :
401 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
404 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
407 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
409 case WXK_RAW_CONTROL:
410 wxevent.SetEventType( wxevent.m_rawControlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
421 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
422 keyval = wxToupper( aunichar ) ;
428 // OS X generates events with key codes in Unicode private use area for
429 // unprintable symbols such as cursor arrows (WXK_UP is mapped to U+F700)
430 // and function keys (WXK_F2 is U+F705). We don't want to use them as the
431 // result of wxKeyEvent::GetUnicodeKey() however as it's supposed to return
432 // WXK_NONE for "non characters" so explicitly exclude them.
434 // We only exclude the private use area inside the Basic Multilingual Plane
435 // as key codes beyond it don't seem to be currently used.
436 if ( !(aunichar >= 0xe000 && aunichar < 0xf900) )
437 wxevent.m_uniChar = aunichar;
439 wxevent.m_keyCode = keyval;
441 wxWindowMac* peer = GetWXPeer();
444 wxevent.SetEventObject(peer);
445 wxevent.SetId(peer->GetId()) ;
449 UInt32 g_lastButton = 0 ;
450 bool g_lastButtonWasFakeRight = false ;
452 // better scroll wheel support
453 // see http://lists.apple.com/archives/cocoa-dev/2007/Feb/msg00050.html
455 @interface NSEvent (DeviceDelta)
456 - (CGFloat)deviceDeltaX;
457 - (CGFloat)deviceDeltaY;
460 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
462 int eventType = [nsEvent type];
463 UInt32 modifiers = [nsEvent modifierFlags] ;
465 NSPoint locationInWindow = [nsEvent locationInWindow];
467 // adjust coordinates for the window of the target view
468 if ( [nsEvent window] != [m_osxView window] )
470 if ( [nsEvent window] != nil )
471 locationInWindow = [[nsEvent window] convertBaseToScreen:locationInWindow];
473 if ( [m_osxView window] != nil )
474 locationInWindow = [[m_osxView window] convertScreenToBase:locationInWindow];
477 NSPoint locationInView = [m_osxView convertPoint:locationInWindow fromView:nil];
478 wxPoint locationInViewWX = wxFromNSPoint( m_osxView, locationInView );
480 // these parameters are not given for all events
481 UInt32 button = [nsEvent buttonNumber];
482 UInt32 clickCount = 0;
484 wxevent.m_x = locationInViewWX.x;
485 wxevent.m_y = locationInViewWX.y;
486 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
487 wxevent.m_rawControlDown = modifiers & NSControlKeyMask;
488 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
489 wxevent.m_controlDown = modifiers & NSCommandKeyMask;
490 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
492 UInt32 mouseChord = 0;
496 case NSLeftMouseDown :
497 case NSLeftMouseDragged :
500 case NSRightMouseDown :
501 case NSRightMouseDragged :
504 case NSOtherMouseDown :
505 case NSOtherMouseDragged :
510 // a control click is interpreted as a right click
511 bool thisButtonIsFakeRight = false ;
512 if ( button == 0 && (modifiers & NSControlKeyMask) )
515 thisButtonIsFakeRight = true ;
518 // otherwise we report double clicks by connecting a left click with a ctrl-left click
519 if ( clickCount > 1 && button != g_lastButton )
522 // we must make sure that our synthetic 'right' button corresponds in
523 // mouse down, moved and mouse up, and does not deliver a right down and left up
526 case NSLeftMouseDown :
527 case NSRightMouseDown :
528 case NSOtherMouseDown :
529 g_lastButton = button ;
530 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
537 g_lastButtonWasFakeRight = false ;
539 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
540 button = g_lastButton ;
542 // Adjust the chord mask to remove the primary button and add the
543 // secondary button. It is possible that the secondary button is
544 // already pressed, e.g. on a mouse connected to a laptop, but this
545 // possibility is ignored here:
546 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
547 mouseChord = ((mouseChord & ~1U) | 2U);
550 wxevent.m_leftDown = true ;
552 wxevent.m_rightDown = true ;
554 wxevent.m_middleDown = true ;
556 // translate into wx types
559 case NSLeftMouseDown :
560 case NSRightMouseDown :
561 case NSOtherMouseDown :
562 clickCount = [nsEvent clickCount];
566 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
570 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
574 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
583 case NSRightMouseUp :
584 case NSOtherMouseUp :
585 clickCount = [nsEvent clickCount];
589 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
593 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
597 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
610 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
612 // see http://developer.apple.com/qa/qa2005/qa1453.html
613 // for more details on why we have to look for the exact type
615 const EventRef cEvent = (EventRef) [nsEvent eventRef];
616 bool isMouseScrollEvent = false;
618 isMouseScrollEvent = ::GetEventKind(cEvent) == kEventMouseScroll;
620 if ( isMouseScrollEvent )
622 deltaX = [nsEvent deviceDeltaX];
623 deltaY = [nsEvent deviceDeltaY];
627 deltaX = ([nsEvent deltaX] * 10);
628 deltaY = ([nsEvent deltaY] * 10);
631 wxevent.m_wheelDelta = 10;
632 wxevent.m_linesPerAction = 1;
634 if ( fabs(deltaX) > fabs(deltaY) )
636 wxevent.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL;
637 wxevent.m_wheelRotation = (int)deltaX;
641 wxevent.m_wheelRotation = (int)deltaY;
647 case NSMouseEntered :
648 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
651 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
653 case NSLeftMouseDragged :
654 case NSRightMouseDragged :
655 case NSOtherMouseDragged :
657 wxevent.SetEventType( wxEVT_MOTION ) ;
663 wxevent.m_clickCount = clickCount;
664 wxWindowMac* peer = GetWXPeer();
667 wxevent.SetEventObject(peer);
668 wxevent.SetId(peer->GetId()) ;
672 @implementation wxNSView
676 static BOOL initialized = NO;
680 wxOSXCocoaClassAddWXMethods( self );
684 /* idea taken from webkit sources: overwrite the methods that (private) NSToolTipManager will use to attach its tracking rectangle
685 * then when changing the tooltip send fake view-exit and view-enter methods which will lead to a tooltip refresh
689 - (void)_sendToolTipMouseExited
691 // Nothing matters except window, trackingNumber, and userData.
692 NSEvent *fakeEvent = [NSEvent enterExitEventWithType:NSMouseExited
693 location:NSMakePoint(0, 0)
696 windowNumber:[[self window] windowNumber]
699 trackingNumber:_lastToolTipTrackTag
700 userData:_lastUserData];
701 [_lastToolTipOwner mouseExited:fakeEvent];
704 - (void)_sendToolTipMouseEntered
706 // Nothing matters except window, trackingNumber, and userData.
707 NSEvent *fakeEvent = [NSEvent enterExitEventWithType:NSMouseEntered
708 location:NSMakePoint(0, 0)
711 windowNumber:[[self window] windowNumber]
714 trackingNumber:_lastToolTipTrackTag
715 userData:_lastUserData];
716 [_lastToolTipOwner mouseEntered:fakeEvent];
719 - (void)setToolTip:(NSString *)string;
725 [self _sendToolTipMouseExited];
728 [super setToolTip:string];
730 [self _sendToolTipMouseEntered];
736 [self _sendToolTipMouseExited];
737 [super setToolTip:nil];
743 - (NSTrackingRectTag)addTrackingRect:(NSRect)rect owner:(id)owner userData:(void *)data assumeInside:(BOOL)assumeInside
745 NSTrackingRectTag tag = [super addTrackingRect:rect owner:owner userData:data assumeInside:assumeInside];
748 _lastUserData = data;
749 _lastToolTipOwner = owner;
750 _lastToolTipTrackTag = tag;
755 - (void)removeTrackingRect:(NSTrackingRectTag)tag
757 if (tag == _lastToolTipTrackTag)
759 _lastUserData = NULL;
760 _lastToolTipOwner = nil;
761 _lastToolTipTrackTag = 0;
763 [super removeTrackingRect:tag];
771 #if wxUSE_DRAG_AND_DROP
773 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
774 // for details on the NSPasteboard -> PasteboardRef conversion
776 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
778 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
780 return NSDragOperationNone;
782 return impl->draggingEntered(sender, self, _cmd);
785 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
787 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
791 return impl->draggingExited(sender, self, _cmd);
794 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
796 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
798 return NSDragOperationNone;
800 return impl->draggingUpdated(sender, self, _cmd);
803 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
805 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
807 return NSDragOperationNone;
809 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
814 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
816 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
820 impl->mouseEvent(event, self, _cmd);
823 void wxOSX_cursorUpdate(NSView* self, SEL _cmd, NSEvent *event)
825 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
829 impl->cursorUpdate(event, self, _cmd);
832 BOOL wxOSX_acceptsFirstMouse(NSView* WXUNUSED(self), SEL WXUNUSED(_cmd), NSEvent *WXUNUSED(event))
834 // This is needed to support click through, otherwise the first click on a window
835 // will not do anything unless it is the active window already.
839 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
841 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
845 impl->keyEvent(event, self, _cmd);
848 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
850 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
854 impl->insertText(text, self, _cmd);
857 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
859 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
863 return impl->performKeyEquivalent(event, self, _cmd);
866 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
868 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
872 return impl->acceptsFirstResponder(self, _cmd);
875 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
877 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
881 return impl->becomeFirstResponder(self, _cmd);
884 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
886 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
890 return impl->resignFirstResponder(self, _cmd);
893 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
895 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
899 return impl->isFlipped(self, _cmd) ? YES:NO;
902 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
904 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
906 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
911 // OS X starts a NSUIHeartBeatThread for animating the default button in a
912 // dialog. This causes a drawRect of the active dialog from outside the
913 // main UI thread. This causes an occasional crash since the wx drawing
914 // objects (like wxPen) are not thread safe.
916 // Notice that NSUIHeartBeatThread seems to be undocumented and doing
917 // [NSWindow setAllowsConcurrentViewDrawing:NO] does not affect it.
918 if ( !wxThread::IsMain() )
920 if ( impl->IsUserPane() )
922 wxWindow* win = impl->GetWXPeer();
923 if ( win->UseBgCol() )
926 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
927 CGContextSaveGState( context );
929 CGContextSetFillColorWithColor( context, win->GetBackgroundColour().GetCGColor());
930 CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
931 CGContextFillRect( context, r );
933 CGContextRestoreGState( context );
938 // just call the superclass handler, we don't need any custom wx drawing
939 // here and it seems to work fine:
940 wxOSX_DrawRectHandlerPtr
941 superimpl = (wxOSX_DrawRectHandlerPtr)
942 [[self superclass] instanceMethodForSelector:_cmd];
943 superimpl(self, _cmd, rect);
948 #endif // wxUSE_THREADS
950 return impl->drawRect(&rect, self, _cmd);
953 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
955 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
959 impl->controlAction(self, _cmd, sender);
962 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
964 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
968 impl->controlDoubleAction(self, _cmd, sender);
971 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
973 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
974 NSPasteboard *pboard = [sender draggingPasteboard];
975 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
977 wxWindow* wxpeer = GetWXPeer();
978 if ( wxpeer == NULL )
979 return NSDragOperationNone;
981 wxDropTarget* target = wxpeer->GetDropTarget();
982 if ( target == NULL )
983 return NSDragOperationNone;
985 wxDragResult result = wxDragNone;
986 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
987 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
989 if ( sourceDragMask & NSDragOperationLink )
991 else if ( sourceDragMask & NSDragOperationCopy )
993 else if ( sourceDragMask & NSDragOperationMove )
996 PasteboardRef pboardRef;
997 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
998 target->SetCurrentDragPasteboard(pboardRef);
999 result = target->OnEnter(pt.x, pt.y, result);
1000 CFRelease(pboardRef);
1002 NSDragOperation nsresult = NSDragOperationNone;
1006 nsresult = NSDragOperationLink;
1008 nsresult = NSDragOperationMove;
1010 nsresult = NSDragOperationCopy;
1017 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1019 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
1020 NSPasteboard *pboard = [sender draggingPasteboard];
1022 wxWindow* wxpeer = GetWXPeer();
1023 if ( wxpeer == NULL )
1026 wxDropTarget* target = wxpeer->GetDropTarget();
1027 if ( target == NULL )
1030 PasteboardRef pboardRef;
1031 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
1032 target->SetCurrentDragPasteboard(pboardRef);
1034 CFRelease(pboardRef);
1037 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1039 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
1040 NSPasteboard *pboard = [sender draggingPasteboard];
1041 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
1043 wxWindow* wxpeer = GetWXPeer();
1044 if ( wxpeer == NULL )
1045 return NSDragOperationNone;
1047 wxDropTarget* target = wxpeer->GetDropTarget();
1048 if ( target == NULL )
1049 return NSDragOperationNone;
1051 wxDragResult result = wxDragNone;
1052 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
1053 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
1055 if ( sourceDragMask & NSDragOperationLink )
1056 result = wxDragLink;
1057 else if ( sourceDragMask & NSDragOperationCopy )
1058 result = wxDragCopy;
1059 else if ( sourceDragMask & NSDragOperationMove )
1060 result = wxDragMove;
1062 PasteboardRef pboardRef;
1063 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
1064 target->SetCurrentDragPasteboard(pboardRef);
1065 result = target->OnDragOver(pt.x, pt.y, result);
1066 CFRelease(pboardRef);
1068 NSDragOperation nsresult = NSDragOperationNone;
1072 nsresult = NSDragOperationLink;
1074 nsresult = NSDragOperationMove;
1076 nsresult = NSDragOperationCopy;
1083 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1085 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
1087 NSPasteboard *pboard = [sender draggingPasteboard];
1088 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
1090 wxWindow* wxpeer = GetWXPeer();
1091 wxDropTarget* target = wxpeer->GetDropTarget();
1092 wxDragResult result = wxDragNone;
1093 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
1094 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
1096 if ( sourceDragMask & NSDragOperationLink )
1097 result = wxDragLink;
1098 else if ( sourceDragMask & NSDragOperationCopy )
1099 result = wxDragCopy;
1100 else if ( sourceDragMask & NSDragOperationMove )
1101 result = wxDragMove;
1103 PasteboardRef pboardRef;
1104 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
1105 target->SetCurrentDragPasteboard(pboardRef);
1107 if (target->OnDrop(pt.x, pt.y))
1108 result = target->OnData(pt.x, pt.y, result);
1110 CFRelease(pboardRef);
1112 return result != wxDragNone;
1115 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
1116 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
1117 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
1118 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
1120 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
1122 if ( !DoHandleMouseEvent(event) )
1124 // for plain NSView mouse events would propagate to parents otherwise
1127 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1128 superimpl(slf, (SEL)_cmd, event);
1130 // super of built-ins keeps the mouse up, as wx expects this event, we have to synthesize it
1132 if ( [ event type] == NSLeftMouseDown )
1134 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1135 SetupMouseEvent(wxevent , event) ;
1136 wxevent.SetEventType(wxEVT_LEFT_UP);
1138 GetWXPeer()->HandleWindowEvent(wxevent);
1144 void wxWidgetCocoaImpl::cursorUpdate(WX_NSEvent event, WXWidget slf, void *_cmd)
1146 NSCursor *cursor = (NSCursor*)GetWXPeer()->GetCursor().GetHCURSOR();
1149 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1150 superimpl(slf, (SEL)_cmd, event);
1160 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
1162 if ( [event type] == NSKeyDown )
1164 // there are key equivalents that are not command-combos and therefore not handled by cocoa automatically,
1165 // therefore we call the menubar directly here, exit if the menu is handling the shortcut
1166 if ( [[[NSApplication sharedApplication] mainMenu] performKeyEquivalent:event] )
1169 m_lastKeyDownEvent = event;
1172 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
1174 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1175 superimpl(slf, (SEL)_cmd, event);
1177 m_lastKeyDownEvent = NULL;
1180 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
1182 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
1184 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1185 superimpl(slf, (SEL)_cmd, text);
1190 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
1192 bool handled = false;
1194 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1195 SetupKeyEvent( wxevent, event );
1197 // because performKeyEquivalent is going up the entire view hierarchy, we don't have to
1198 // walk up the ancestors ourselves but let cocoa do it
1200 int command = m_wxPeer->GetAcceleratorTable()->GetCommand( wxevent );
1203 wxEvtHandler * const handler = m_wxPeer->GetEventHandler();
1205 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1206 command_event.SetEventObject( wxevent.GetEventObject() );
1207 handled = handler->ProcessEvent( command_event );
1211 // accelerators can also be used with buttons, try them too
1212 command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED);
1213 handled = handler->ProcessEvent( command_event );
1219 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1220 return superimpl(slf, (SEL)_cmd, event);
1225 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
1228 return m_wxPeer->AcceptsFocus();
1231 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1232 return superimpl(slf, (SEL)_cmd);
1236 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
1238 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1239 // get the current focus before running becomeFirstResponder
1240 NSView* otherView = FindFocus();
1242 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1243 BOOL r = superimpl(slf, (SEL)_cmd);
1246 DoNotifyFocusEvent( true, otherWindow );
1252 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
1254 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1255 BOOL r = superimpl(slf, (SEL)_cmd);
1256 // get the current focus after running resignFirstResponder
1257 // note that this value isn't reliable, it might return the same view that
1259 NSView* otherView = FindFocus();
1260 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1262 // It doesn't make sense to notify about the loss of focus if we're not
1263 // really losing it and the window which has just gained focus is the same
1264 // one as this window itself. Of course, this should never happen in the
1265 // first place but somehow it does in wxGrid code and without this check we
1266 // enter into an infinite recursion, see #12267.
1267 if ( otherWindow == this )
1270 // NSTextViews have an editor as true responder, therefore the might get the
1271 // resign notification if their editor takes over, don't trigger any event then
1272 if ( r && !m_hasEditor)
1274 DoNotifyFocusEvent( false, otherWindow );
1279 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1285 #define OSX_DEBUG_DRAWING 0
1287 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
1289 // preparing the update region
1292 const NSRect *rects;
1295 [slf getRectsBeingDrawn:&rects count:&count];
1296 for ( int i = 0 ; i < count ; ++i )
1298 updateRgn.Union(wxFromNSRect(slf, rects[i]));
1301 wxWindow* wxpeer = GetWXPeer();
1303 if ( wxpeer->MacGetLeftBorderSize() != 0 || wxpeer->MacGetTopBorderSize() != 0 )
1305 // as this update region is in native window locals we must adapt it to wx window local
1306 updateRgn.Offset( wxpeer->MacGetLeftBorderSize() , wxpeer->MacGetTopBorderSize() );
1309 // Restrict the update region to the shape of the window, if any, and also
1310 // remember the region that we need to clear later.
1311 wxNonOwnedWindow* const tlwParent = wxpeer->MacGetTopLevelWindow();
1312 const bool isTopLevel = tlwParent == wxpeer;
1314 if ( tlwParent->GetWindowStyle() & wxFRAME_SHAPED )
1317 clearRgn = updateRgn;
1319 int xoffset = 0, yoffset = 0;
1320 wxRegion rgn = tlwParent->GetShape();
1321 wxpeer->MacRootWindowToWindow( &xoffset, &yoffset );
1322 rgn.Offset( xoffset, yoffset );
1323 updateRgn.Intersect(rgn);
1327 // Exclude the window shape from the region to be cleared below.
1328 rgn.Xor(wxpeer->GetSize());
1329 clearRgn.Intersect(rgn);
1333 wxpeer->GetUpdateRegion() = updateRgn;
1335 // setting up the drawing context
1337 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
1338 CGContextSaveGState( context );
1340 #if OSX_DEBUG_DRAWING
1341 CGContextBeginPath( context );
1342 CGContextMoveToPoint(context, 0, 0);
1343 NSRect bounds = [slf bounds];
1344 CGContextAddLineToPoint(context, 10, 0);
1345 CGContextMoveToPoint(context, 0, 0);
1346 CGContextAddLineToPoint(context, 0, 10);
1347 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1348 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1349 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1350 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1351 CGContextClosePath( context );
1352 CGContextStrokePath(context);
1357 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1358 CGContextScaleCTM( context, 1, -1 );
1361 wxpeer->MacSetCGContextRef( context );
1363 bool handled = wxpeer->MacDoRedraw( 0 );
1364 CGContextRestoreGState( context );
1366 CGContextSaveGState( context );
1370 SEL _cmd = @selector(drawRect:);
1371 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1372 superimpl(slf, _cmd, *(NSRect*)rect);
1373 CGContextRestoreGState( context );
1374 CGContextSaveGState( context );
1376 // as we called restore above, we have to flip again if necessary
1379 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1380 CGContextScaleCTM( context, 1, -1 );
1385 // We also need to explicitly draw the part of the top level window
1386 // outside of its region with transparent colour to ensure that it is
1387 // really transparent.
1388 if ( clearRgn.IsOk() )
1390 wxMacCGContextStateSaver saveState(context);
1391 wxWindowDC dc(wxpeer);
1392 dc.SetBackground(wxBrush(wxTransparentColour));
1393 dc.SetDeviceClippingRegion(clearRgn);
1397 #if wxUSE_GRAPHICS_CONTEXT
1398 // If the window shape is defined by a path, stroke the path to show
1399 // the window border.
1400 const wxGraphicsPath& path = tlwParent->GetShapePath();
1401 if ( !path.IsNull() )
1403 CGContextSetLineWidth(context, 1);
1404 CGContextSetStrokeColorWithColor(context, wxLIGHT_GREY->GetCGColor());
1405 CGContextAddPath(context, (CGPathRef) path.GetNativePath());
1406 CGContextStrokePath(context);
1408 #endif // wxUSE_GRAPHICS_CONTEXT
1411 wxpeer->MacPaintChildrenBorders();
1412 wxpeer->MacSetCGContextRef( NULL );
1413 CGContextRestoreGState( context );
1416 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1418 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1420 wxpeer->OSXHandleClicked(0);
1423 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1427 void wxWidgetCocoaImpl::controlTextDidChange()
1429 wxWindow* wxpeer = (wxWindow*)GetWXPeer();
1432 // since native rtti doesn't have to be enabled and wx' rtti is not aware of the mixin wxTextEntry, workaround is needed
1433 wxTextCtrl *tc = wxDynamicCast( wxpeer , wxTextCtrl );
1434 wxComboBox *cb = wxDynamicCast( wxpeer , wxComboBox );
1436 tc->SendTextUpdatedEventIfAllowed();
1438 cb->SendTextUpdatedEventIfAllowed();
1441 wxFAIL_MSG("Unexpected class for controlTextDidChange event");
1448 #if OBJC_API_VERSION >= 2
1450 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1451 class_addMethod(c, s, i, t );
1455 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1456 { s, (char*) t, i },
1460 void wxOSXCocoaClassAddWXMethods(Class c)
1463 #if OBJC_API_VERSION < 2
1464 static objc_method wxmethods[] =
1468 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1469 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1470 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1472 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1473 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1474 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1476 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1478 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1479 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1480 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1482 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstMouse:), (IMP) wxOSX_acceptsFirstMouse, "v@:@" )
1484 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1485 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1486 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1488 wxOSX_CLASS_ADD_METHOD(c, @selector(cursorUpdate:), (IMP) wxOSX_cursorUpdate, "v@:@" )
1490 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1491 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1492 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1494 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1496 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1498 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1499 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1500 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1502 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1503 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1505 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1506 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1508 #if wxUSE_DRAG_AND_DROP
1509 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1510 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1511 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1512 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1515 #if OBJC_API_VERSION < 2
1517 static int method_count = WXSIZEOF( wxmethods );
1518 static objc_method_list *wxmethodlist = NULL;
1519 if ( wxmethodlist == NULL )
1521 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1522 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1523 wxmethodlist->method_count = method_count;
1524 wxmethodlist->obsolete = 0;
1526 class_addMethods( c, wxmethodlist );
1531 // C++ implementation class
1534 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1536 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl, bool isUserPane ) :
1537 wxWidgetImpl( peer, isRootControl, isUserPane )
1542 // check if the user wants to create the control initially hidden
1543 if ( !peer->IsShown() )
1544 SetVisibility(false);
1546 // gc aware handling
1548 CFRetain(m_osxView);
1549 [m_osxView release];
1552 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1557 void wxWidgetCocoaImpl::Init()
1561 m_lastKeyDownEvent = NULL;
1562 m_hasEditor = false;
1565 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1567 RemoveAssociations( this );
1569 if ( !IsRootControl() )
1571 NSView *sv = [m_osxView superview];
1573 [m_osxView removeFromSuperview];
1575 // gc aware handling
1577 CFRelease(m_osxView);
1580 bool wxWidgetCocoaImpl::IsVisible() const
1582 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1585 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1587 [m_osxView setHidden:(visible ? NO:YES)];
1590 // ----------------------------------------------------------------------------
1591 // window animation stuff
1592 // ----------------------------------------------------------------------------
1594 // define a delegate used to refresh the window during animation
1595 @interface wxNSAnimationDelegate : NSObject wxOSX_10_6_AND_LATER(<NSAnimationDelegate>)
1601 - (id)init:(wxWindow *)win;
1605 // NSAnimationDelegate methods
1606 - (void)animationDidEnd:(NSAnimation*)animation;
1607 - (void)animation:(NSAnimation*)animation
1608 didReachProgressMark:(NSAnimationProgress)progress;
1611 @implementation wxNSAnimationDelegate
1613 - (id)init:(wxWindow *)win
1615 self = [super init];
1628 - (void)animation:(NSAnimation*)animation
1629 didReachProgressMark:(NSAnimationProgress)progress
1631 wxUnusedVar(animation);
1632 wxUnusedVar(progress);
1634 m_win->SendSizeEvent();
1635 m_win->MacOnInternalSize();
1638 - (void)animationDidEnd:(NSAnimation*)animation
1640 wxUnusedVar(animation);
1648 wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1650 wxShowEffect effect,
1653 // create the dictionary describing the animation to perform on this view
1655 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1656 NSMutableDictionary * const
1657 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1658 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1660 // determine the start and end rectangles assuming we're hiding the window
1661 const wxRect rectOrig = win->GetRect();
1669 if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1670 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1671 effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1672 else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1673 effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1674 effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1675 else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1676 effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1677 effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1678 else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1679 effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1680 effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1685 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1686 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1690 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1691 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1692 rectEnd.x = rectStart.GetRight();
1696 case wxSHOW_EFFECT_ROLL_TO_TOP:
1697 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1701 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1702 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1703 rectEnd.y = rectStart.GetBottom();
1707 case wxSHOW_EFFECT_EXPAND:
1708 rectEnd.x = rectStart.x + rectStart.width / 2;
1709 rectEnd.y = rectStart.y + rectStart.height / 2;
1714 case wxSHOW_EFFECT_BLEND:
1715 [dict setObject:(show ? NSViewAnimationFadeInEffect
1716 : NSViewAnimationFadeOutEffect)
1717 forKey:NSViewAnimationEffectKey];
1720 case wxSHOW_EFFECT_NONE:
1721 case wxSHOW_EFFECT_MAX:
1722 wxFAIL_MSG( "unexpected animation effect" );
1726 wxFAIL_MSG( "unknown animation effect" );
1732 // we need to restore it to the original rectangle instead of making it
1734 wxSwap(rectStart, rectEnd);
1736 // and as the window is currently hidden, we need to show it for the
1737 // animation to be visible at all (but don't restore it at its full
1738 // rectangle as it shouldn't appear immediately)
1739 win->SetSize(rectStart);
1743 NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1744 ? [(NSView *)viewOrWin superview]
1746 const NSRect rStart = wxToNSRect(parentView, rectStart);
1747 const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1749 [dict setObject:[NSValue valueWithRect:rStart]
1750 forKey:NSViewAnimationStartFrameKey];
1751 [dict setObject:[NSValue valueWithRect:rEnd]
1752 forKey:NSViewAnimationEndFrameKey];
1754 // create an animation using the values in the above dictionary
1755 NSViewAnimation * const
1756 anim = [[NSViewAnimation alloc]
1757 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1761 // what is a good default duration? Windows uses 200ms, Web frameworks
1762 // use anything from 250ms to 1s... choose something in the middle
1766 [anim setDuration:timeout/1000.]; // duration is in seconds here
1768 // if the window being animated changes its layout depending on its size
1769 // (which is almost always the case) we need to redo it during animation
1771 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1772 // controls in wxInfoBar visibly jump around)
1773 const int NUM_LAYOUTS = 20;
1774 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1775 [anim addProgressMark:f];
1777 wxNSAnimationDelegate * const
1778 animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1779 [anim setDelegate:animDelegate];
1780 [anim startAnimation];
1782 // Cocoa is capable of doing animation asynchronously or even from separate
1783 // thread but wx API doesn't provide any way to be notified about the
1784 // animation end and without this we really must ensure that the window has
1785 // the expected (i.e. the same as if a simple Show() had been used) size
1786 // when we return, so block here until the animation finishes
1788 // notice that because the default animation mode is NSAnimationBlocking,
1789 // no user input events ought to be processed from here
1791 wxEventLoopGuarantor ensureEventLoopExistence;
1792 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1793 while ( ![animDelegate isDone] )
1799 // NSViewAnimation is smart enough to hide the NSView being animated at
1800 // the end but we also must ensure that it's hidden for wx too
1803 // and we must also restore its size because it isn't expected to
1804 // change just because the window was hidden
1805 win->SetSize(rectOrig);
1809 // refresh it once again after the end to ensure that everything is in
1811 win->SendSizeEvent();
1812 win->MacOnInternalSize();
1815 [anim setDelegate:nil];
1816 [animDelegate release];
1822 bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1823 wxShowEffect effect,
1826 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1829 /* note that the drawing order between siblings is not defined under 10.4 */
1830 /* only starting from 10.5 the subview order is respected */
1832 /* NSComparisonResult is typedef'd as an enum pre-Leopard but typedef'd as
1833 * NSInteger post-Leopard. Pre-Leopard the Cocoa toolkit expects a function
1834 * returning int and not NSComparisonResult. Post-Leopard the Cocoa toolkit
1835 * expects a function returning the new non-enum NSComparsionResult.
1836 * Hence we create a typedef named CocoaWindowCompareFunctionResult.
1838 #if defined(NSINTEGER_DEFINED)
1839 typedef NSComparisonResult CocoaWindowCompareFunctionResult;
1841 typedef int CocoaWindowCompareFunctionResult;
1844 class CocoaWindowCompareContext
1846 wxDECLARE_NO_COPY_CLASS(CocoaWindowCompareContext);
1848 CocoaWindowCompareContext(); // Not implemented
1849 CocoaWindowCompareContext(NSView *target, NSArray *subviews)
1852 // Cocoa sorts subviews in-place.. make a copy
1853 m_subviews = [subviews copy];
1856 ~CocoaWindowCompareContext()
1857 { // release the copy
1858 [m_subviews release];
1861 { return m_target; }
1864 { return m_subviews; }
1866 /* Helper function that returns the comparison based off of the original ordering */
1867 CocoaWindowCompareFunctionResult CompareUsingOriginalOrdering(id first, id second)
1869 NSUInteger firstI = [m_subviews indexOfObjectIdenticalTo:first];
1870 NSUInteger secondI = [m_subviews indexOfObjectIdenticalTo:second];
1871 // NOTE: If either firstI or secondI is NSNotFound then it will be NSIntegerMax and thus will
1872 // likely compare higher than the other view which is reasonable considering the only way that
1873 // can happen is if the subview was added after our call to subviews but before the call to
1874 // sortSubviewsUsingFunction:context:. Thus we don't bother checking. Particularly because
1875 // that case should never occur anyway because that would imply a multi-threaded GUI call
1876 // which is a big no-no with Cocoa.
1878 // Subviews are ordered from back to front meaning one that is already lower will have an lower index.
1879 NSComparisonResult result = (firstI < secondI)
1880 ? NSOrderedAscending /* -1 */
1881 : (firstI > secondI)
1882 ? NSOrderedDescending /* 1 */
1883 : NSOrderedSame /* 0 */;
1888 /* The subview we are trying to Raise or Lower */
1890 /* A copy of the original array of subviews */
1891 NSArray *m_subviews;
1894 /* Causes Cocoa to raise the target view to the top of the Z-Order by telling the sort function that
1895 * the target view is always higher than every other view. When comparing two views neither of
1896 * which is the target, it returns the correct response based on the original ordering
1898 static CocoaWindowCompareFunctionResult CocoaRaiseWindowCompareFunction(id first, id second, void *ctx)
1900 CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx;
1901 // first should be ordered higher
1902 if(first==compareContext->target())
1903 return NSOrderedDescending;
1904 // second should be ordered higher
1905 if(second==compareContext->target())
1906 return NSOrderedAscending;
1907 return compareContext->CompareUsingOriginalOrdering(first,second);
1910 void wxWidgetCocoaImpl::Raise()
1912 NSView* nsview = m_osxView;
1914 NSView *superview = [nsview superview];
1915 CocoaWindowCompareContext compareContext(nsview, [superview subviews]);
1917 [superview sortSubviewsUsingFunction:
1918 CocoaRaiseWindowCompareFunction
1919 context: &compareContext];
1923 /* Causes Cocoa to lower the target view to the bottom of the Z-Order by telling the sort function that
1924 * the target view is always lower than every other view. When comparing two views neither of
1925 * which is the target, it returns the correct response based on the original ordering
1927 static CocoaWindowCompareFunctionResult CocoaLowerWindowCompareFunction(id first, id second, void *ctx)
1929 CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx;
1930 // first should be ordered lower
1931 if(first==compareContext->target())
1932 return NSOrderedAscending;
1933 // second should be ordered lower
1934 if(second==compareContext->target())
1935 return NSOrderedDescending;
1936 return compareContext->CompareUsingOriginalOrdering(first,second);
1939 void wxWidgetCocoaImpl::Lower()
1941 NSView* nsview = m_osxView;
1943 NSView *superview = [nsview superview];
1944 CocoaWindowCompareContext compareContext(nsview, [superview subviews]);
1946 [superview sortSubviewsUsingFunction:
1947 CocoaLowerWindowCompareFunction
1948 context: &compareContext];
1951 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1956 // We should do something like this, but it wasn't working in 10.4.
1957 if (GetNeedsDisplay() )
1961 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1962 NSSize offset = NSMakeSize((float)dx, (float)dy);
1963 [m_osxView scrollRect:r by:offset];
1967 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1969 wxWindowMac* parent = GetWXPeer()->GetParent();
1970 // under Cocoa we might have a contentView in the wxParent to which we have to
1971 // adjust the coordinates
1972 if (parent && [m_osxView superview] != parent->GetHandle() )
1974 int cx = 0,cy = 0,cw = 0,ch = 0;
1975 if ( parent->GetPeer() )
1977 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1982 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1983 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1984 [m_osxView setFrame:r];
1985 [[m_osxView superview] setNeedsDisplayInRect:r];
1988 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1990 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1994 // under Cocoa we might have a contentView in the wxParent to which we have to
1995 // adjust the coordinates
1996 wxWindowMac* parent = GetWXPeer()->GetParent();
1997 if (parent && [m_osxView superview] != parent->GetHandle() )
1999 int cx = 0,cy = 0,cw = 0,ch = 0;
2000 if ( parent->GetPeer() )
2002 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
2009 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
2011 NSRect rect = [m_osxView frame];
2012 width = (int)rect.size.width;
2013 height = (int)rect.size.height;
2016 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
2018 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
2020 NSView* cv = [m_osxView contentView];
2022 NSRect bounds = [m_osxView bounds];
2023 NSRect rect = [cv frame];
2025 int y = (int)rect.origin.y;
2026 int x = (int)rect.origin.x;
2027 if ( ![ m_osxView isFlipped ] )
2028 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
2031 width = (int)rect.size.width;
2032 height = (int)rect.size.height;
2037 GetSize( width, height );
2041 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
2044 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
2046 [m_osxView setNeedsDisplay:YES];
2049 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
2051 return [m_osxView needsDisplay];
2054 bool wxWidgetCocoaImpl::CanFocus() const
2056 return [m_osxView canBecomeKeyView] == YES;
2059 bool wxWidgetCocoaImpl::HasFocus() const
2061 return ( FindFocus() == m_osxView );
2064 bool wxWidgetCocoaImpl::SetFocus()
2069 // TODO remove if no issues arise: should not raise the window, only assign focus
2070 //[[m_osxView window] makeKeyAndOrderFront:nil] ;
2071 [[m_osxView window] makeFirstResponder: m_osxView] ;
2076 void wxWidgetCocoaImpl::RemoveFromParent()
2078 [m_osxView removeFromSuperview];
2081 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
2083 NSView* container = parent->GetWXWidget() ;
2084 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
2085 [container addSubview:m_osxView];
2088 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
2090 NSView* targetView = m_osxView;
2091 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
2092 targetView = [(NSScrollView*) m_osxView documentView];
2094 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
2096 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
2097 green:(CGFloat) (col.Green() / 255.0)
2098 blue:(CGFloat) (col.Blue() / 255.0)
2099 alpha:(CGFloat) (col.Alpha() / 255.0)]];
2103 bool wxWidgetCocoaImpl::SetBackgroundStyle( wxBackgroundStyle style )
2105 BOOL opaque = ( style == wxBG_STYLE_PAINT );
2107 if ( [m_osxView respondsToSelector:@selector(setOpaque:) ] )
2109 [m_osxView setOpaque: opaque];
2115 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
2117 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
2119 wxCFStringRef cf( title , encoding );
2120 [m_osxView setTitle:cf.AsNSString()];
2122 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
2124 wxCFStringRef cf( title , encoding );
2125 [m_osxView setStringValue:cf.AsNSString()];
2130 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
2132 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
2133 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
2134 *pt = wxFromNSPoint( to->GetWXWidget(), p );
2137 wxInt32 wxWidgetCocoaImpl::GetValue() const
2139 return [(NSControl*)m_osxView intValue];
2142 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
2144 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
2146 [m_osxView setIntValue:v];
2148 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
2150 [m_osxView setFloatValue:(double)v];
2152 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
2154 [m_osxView setDoubleValue:(double)v];
2158 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
2160 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
2162 [m_osxView setMinValue:(double)v];
2166 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
2168 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
2170 [m_osxView setMaxValue:(double)v];
2174 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
2176 if ( [m_osxView respondsToSelector:@selector(minValue)] )
2178 return (int)[m_osxView minValue];
2183 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
2185 if ( [m_osxView respondsToSelector:@selector(maxValue)] )
2187 return (int)[m_osxView maxValue];
2192 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
2196 // TODO: how to create a wxBitmap from NSImage?
2198 if ( [m_osxView respondsToSelector:@selector(image:)] )
2199 bmp = [m_osxView image];
2205 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
2207 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
2210 [m_osxView setImage:bitmap.GetNSImage()];
2212 [m_osxView setImage:nil];
2214 [m_osxView setNeedsDisplay:YES];
2218 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
2220 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
2222 NSCellImagePosition pos;
2242 wxFAIL_MSG( "invalid image position" );
2246 [m_osxView setImagePosition:pos];
2250 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
2252 // implementation in subclass
2255 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
2257 r->x = r->y = r->width = r->height = 0;
2259 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
2261 NSRect former = [m_osxView frame];
2262 [m_osxView sizeToFit];
2263 NSRect best = [m_osxView frame];
2264 [m_osxView setFrame:former];
2265 r->width = (int)best.size.width;
2266 r->height = (int)best.size.height;
2270 bool wxWidgetCocoaImpl::IsEnabled() const
2272 NSView* targetView = m_osxView;
2273 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
2274 targetView = [(NSScrollView*) m_osxView documentView];
2276 if ( [targetView respondsToSelector:@selector(isEnabled) ] )
2277 return [targetView isEnabled];
2281 void wxWidgetCocoaImpl::Enable( bool enable )
2283 NSView* targetView = m_osxView;
2284 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
2285 targetView = [(NSScrollView*) m_osxView documentView];
2287 if ( [targetView respondsToSelector:@selector(setEnabled:) ] )
2288 [targetView setEnabled:enable];
2291 void wxWidgetCocoaImpl::PulseGauge()
2295 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
2299 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
2301 NSControlSize size = NSRegularControlSize;
2305 case wxWINDOW_VARIANT_NORMAL :
2306 size = NSRegularControlSize;
2309 case wxWINDOW_VARIANT_SMALL :
2310 size = NSSmallControlSize;
2313 case wxWINDOW_VARIANT_MINI :
2314 size = NSMiniControlSize;
2317 case wxWINDOW_VARIANT_LARGE :
2318 size = NSRegularControlSize;
2322 wxFAIL_MSG(wxT("unexpected window variant"));
2325 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
2326 [m_osxView setControlSize:size];
2327 else if ([m_osxView respondsToSelector:@selector(cell)])
2329 id cell = [(id)m_osxView cell];
2330 if ([cell respondsToSelector:@selector(setControlSize:)])
2331 [cell setControlSize:size];
2335 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&col, long, bool)
2337 if ([m_osxView respondsToSelector:@selector(setFont:)])
2338 [m_osxView setFont: font.OSXGetNSFont()];
2339 if ([m_osxView respondsToSelector:@selector(setTextColor:)])
2340 [m_osxView setTextColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
2341 green:(CGFloat) (col.Green() / 255.0)
2342 blue:(CGFloat) (col.Blue() / 255.0)
2343 alpha:(CGFloat) (col.Alpha() / 255.0)]];
2346 void wxWidgetCocoaImpl::SetToolTip(wxToolTip* tooltip)
2350 wxCFStringRef cf( tooltip->GetTip() , m_wxPeer->GetFont().GetEncoding() );
2351 [m_osxView setToolTip: cf.AsNSString()];
2355 [m_osxView setToolTip:nil];
2359 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
2361 WXWidget c = control ? control : (WXWidget) m_osxView;
2362 wxWidgetImpl::Associate( c, this ) ;
2363 if ([c respondsToSelector:@selector(setAction:)])
2366 [c setAction: @selector(controlAction:)];
2367 if ([c respondsToSelector:@selector(setDoubleAction:)])
2369 [c setDoubleAction: @selector(controlDoubleAction:)];
2373 NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingCursorUpdate|NSTrackingMouseMoved|NSTrackingActiveAlways|NSTrackingInVisibleRect;
2374 NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect: NSZeroRect options: options owner: m_osxView userInfo: nil];
2375 [m_osxView addTrackingArea: area];
2379 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
2381 wxKeyEvent wxevent(wxEVT_CHAR);
2382 SetupKeyEvent( wxevent, event, text );
2384 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
2387 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
2389 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
2390 SetupKeyEvent( wxevent, event );
2392 // Generate wxEVT_CHAR_HOOK before sending any other events but only when
2393 // the key is pressed, not when it's released (the type of wxevent is
2394 // changed by SetupKeyEvent() so it can be wxEVT_KEY_UP too by now).
2395 if ( wxevent.GetEventType() == wxEVT_KEY_DOWN )
2397 wxKeyEvent eventHook(wxEVT_CHAR_HOOK, wxevent);
2398 if ( GetWXPeer()->OSXHandleKeyEvent(eventHook)
2399 && !eventHook.IsNextEventAllowed() )
2403 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
2405 // this will fire higher level events, like insertText, to help
2406 // us handle EVT_CHAR, etc.
2410 if ( [event type] == NSKeyDown)
2412 long keycode = wxOSXTranslateCocoaKey( event, wxEVT_CHAR );
2414 if ( (keycode > 0 && keycode < WXK_SPACE) || keycode == WXK_DELETE || keycode >= WXK_START )
2416 // eventually we could setup a doCommandBySelector catcher and retransform this into the wx key chars
2417 wxKeyEvent wxevent2(wxevent) ;
2418 wxevent2.SetEventType(wxEVT_CHAR);
2419 SetupKeyEvent( wxevent2, event );
2420 wxevent2.m_keyCode = keycode;
2421 result = GetWXPeer()->OSXHandleKeyEvent(wxevent2);
2423 else if (wxevent.CmdDown())
2425 wxKeyEvent wxevent2(wxevent) ;
2426 wxevent2.SetEventType(wxEVT_CHAR);
2427 SetupKeyEvent( wxevent2, event );
2428 result = GetWXPeer()->OSXHandleKeyEvent(wxevent2);
2432 if ( IsUserPane() && !wxevent.CmdDown() )
2434 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
2435 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
2437 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
2447 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
2449 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
2450 SetupMouseEvent(wxevent , event) ;
2451 return GetWXPeer()->HandleWindowEvent(wxevent);
2454 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
2456 wxWindow* thisWindow = GetWXPeer();
2457 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
2459 thisWindow->MacInvalidateBorders();
2462 if ( receivedFocus )
2464 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
2465 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
2466 thisWindow->HandleWindowEvent(eventFocus);
2469 if ( thisWindow->GetCaret() )
2470 thisWindow->GetCaret()->OnSetFocus();
2473 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
2474 event.SetEventObject(thisWindow);
2476 event.SetWindow(otherWindow->GetWXPeer());
2477 thisWindow->HandleWindowEvent(event) ;
2479 else // !receivedFocuss
2482 if ( thisWindow->GetCaret() )
2483 thisWindow->GetCaret()->OnKillFocus();
2486 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
2488 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
2489 event.SetEventObject(thisWindow);
2491 event.SetWindow(otherWindow->GetWXPeer());
2492 thisWindow->HandleWindowEvent(event) ;
2496 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
2500 NSPoint location = [NSEvent mouseLocation];
2501 location = [[m_osxView window] convertScreenToBase:location];
2502 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
2504 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
2506 [(NSCursor*)cursor.GetHCURSOR() set];
2511 void wxWidgetCocoaImpl::CaptureMouse()
2513 [[m_osxView window] disableCursorRects];
2516 void wxWidgetCocoaImpl::ReleaseMouse()
2518 [[m_osxView window] enableCursorRects];
2521 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
2523 m_isFlipped = flipped;
2530 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
2531 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
2532 long WXUNUSED(style), long WXUNUSED(extraStyle))
2534 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
2535 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
2537 // temporary hook for dnd
2538 [v registerForDraggedTypes:[NSArray arrayWithObjects:
2539 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
2541 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v, false, true );
2545 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
2547 NSWindow* tlw = now->GetWXWindow();
2549 wxWidgetCocoaImpl* c = NULL;
2550 if ( now->IsNativeWindowWrapper() )
2552 NSView* cv = [tlw contentView];
2553 c = new wxWidgetCocoaImpl( now, cv, true );
2554 // increase ref count, because the impl destructor will decrement it again
2556 if ( !now->IsShown() )
2562 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
2563 c = new wxWidgetCocoaImpl( now, v, true );
2564 c->InstallEventHandler();
2565 [tlw setContentView:v];