X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/08c1b1340d318dfdbdec8bd2723925c2ec404386..5f65ba36570e9cf4296673391472cf11b9f0c673:/src/osx/cocoa/window.mm diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index b5a0f099e5..cf91905014 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -31,6 +31,29 @@ #include +// Get the window with the focus + +WXWidget wxWidgetImpl::FindFocus() +{ + NSView* focusedView = nil; + NSWindow* keyWindow = [[NSApplication sharedApplication] keyWindow]; + if ( keyWindow != nil ) + { + NSResponder* responder = [keyWindow firstResponder]; + if ( [responder isKindOfClass:[NSTextView class]] && + [keyWindow fieldEditor:NO forObject:nil] != nil ) + { + focusedView = [(NSTextView*)responder delegate]; + } + else + { + if ( [responder isKindOfClass:[NSView class]] ) + focusedView = (NSView*) responder; + } + } + return focusedView; +} + NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin ) { int x, y, w, h ; @@ -47,7 +70,6 @@ NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const NSTrackingRectTag rectTag; } -- (BOOL) canBecomeKeyView; // the tracking tag is needed to track mouse enter / exit events - (void) setTrackingTag: (NSTrackingRectTag)tag; - (NSTrackingRectTag) trackingTag; @@ -73,6 +95,8 @@ NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const - (void)setImage:(NSImage *)image; - (void)setControlSize:(NSControlSize)size; +- (void)setFont:(NSFont *)fontObject; + - (id)contentView; - (void)setTarget:(id)anObject; @@ -134,8 +158,8 @@ long wxOSXTranslateCocoaKey( NSEvent* event ) retval = WXK_HELP; break; default: - int intchar = [[event characters] characterAtIndex: 0]; - if ( intchar >= NSF1FunctionKey && intchar >= NSF24FunctionKey ) + int intchar = [s characterAtIndex: 0]; + if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey ) retval = WXK_F1 + (intchar - NSF1FunctionKey ); break; } @@ -437,11 +461,6 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) } } -- (BOOL) canBecomeKeyView -{ - return YES; -} - - (void) setTrackingTag: (NSTrackingRectTag)tag { rectTag = tag; @@ -535,6 +554,15 @@ BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) return impl->performKeyEquivalent(event, self, _cmd); } +BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NO; + + return impl->acceptsFirstResponder(self, _cmd); +} + BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd) { wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); @@ -598,7 +626,7 @@ void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender) impl->controlDoubleAction(self, _cmd, sender); } -unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd) +unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) { id sender = (id ) s; NSPasteboard *pboard = [sender draggingPasteboard]; @@ -643,7 +671,7 @@ unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cm return nsresult; } -void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd) +void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) { id sender = (id ) s; NSPasteboard *pboard = [sender draggingPasteboard]; @@ -663,7 +691,7 @@ void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd) CFRelease(pboardRef); } -unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd) +unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) { id sender = (id ) s; NSPasteboard *pboard = [sender draggingPasteboard]; @@ -708,7 +736,7 @@ unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cm return nsresult; } -bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd) +bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) { id sender = (id ) s; @@ -780,12 +808,28 @@ bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, voi return superimpl(slf, (SEL)_cmd, event); } +bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd) +{ + if ( m_wxPeer->MacIsUserPane() ) + return m_wxPeer->AcceptsFocus(); + else + { + wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + return superimpl(slf, (SEL)_cmd); + } +} + bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd) { wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; + // get the current focus before running becomeFirstResponder + NSView* otherView = FindFocus(); + wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); BOOL r = superimpl(slf, (SEL)_cmd); if ( r ) - DoNotifyFocusEvent( true ); + { + DoNotifyFocusEvent( true, otherWindow ); + } return r; } @@ -793,8 +837,15 @@ bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd) { wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; BOOL r = superimpl(slf, (SEL)_cmd); - if ( r ) - DoNotifyFocusEvent( false ); + // get the current focus after running resignFirstResponder + NSView* otherView = FindFocus(); + wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); + // NSTextViews have an editor as true responder, therefore the might get the + // resign notification if their editor takes over, don't trigger any event hen + if ( r && otherWindow != this) + { + DoNotifyFocusEvent( false, otherWindow ); + } return r; } @@ -817,7 +868,7 @@ void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd) } } -bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd) +bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) { return m_isFlipped; } @@ -825,7 +876,7 @@ bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd) #define OSX_DEBUG_DRAWING 0 -void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd) +void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd)) { CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; CGContextSaveGState( context ); @@ -884,14 +935,14 @@ void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd) CGContextRestoreGState( context ); } -void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender) +void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) { wxWindow* wxpeer = (wxWindow*) GetWXPeer(); if ( wxpeer ) wxpeer->OSXHandleClicked(0); } -void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender) +void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) { } @@ -943,6 +994,7 @@ void wxOSXCocoaClassAddWXMethods(Class c) wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" ) wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" ) wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" ) wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" ) @@ -1031,7 +1083,7 @@ void wxWidgetCocoaImpl::Lower() { } -void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy ) +void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) ) { #if 1 SetNeedsDisplay() ; @@ -1052,7 +1104,7 @@ void wxWidgetCocoaImpl::Move(int x, int y, int width, int height) wxWindowMac* parent = GetWXPeer()->GetParent(); // under Cocoa we might have a contentView in the wxParent to which we have to // adjust the coordinates - if (parent) + if (parent && [m_osxView superview] != parent->GetHandle() ) { int cx = 0,cy = 0,cw = 0,ch = 0; if ( parent->GetPeer() ) @@ -1062,8 +1114,10 @@ void wxWidgetCocoaImpl::Move(int x, int y, int width, int height) y -= cy; } } + [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]]; NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) ); [m_osxView setFrame:r]; + [[m_osxView superview] setNeedsDisplayInRect:r]; if ([m_osxView respondsToSelector:@selector(trackingTag)] ) { @@ -1133,7 +1187,7 @@ bool wxWidgetCocoaImpl::CanFocus() const bool wxWidgetCocoaImpl::HasFocus() const { - return ( [[m_osxView window] firstResponder] == m_osxView ); + return ( FindFocus() == m_osxView ); } bool wxWidgetCocoaImpl::SetFocus() @@ -1167,12 +1221,12 @@ void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding { if ( [m_osxView respondsToSelector:@selector(setTitle:) ] ) { - wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() ); + wxCFStringRef cf( title , encoding ); [m_osxView setTitle:cf.AsNSString()]; } else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] ) { - wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() ); + wxCFStringRef cf( title , encoding ); [m_osxView setStringValue:cf.AsNSString()]; } } @@ -1248,7 +1302,7 @@ void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) } } -void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook) +void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook)) { // implementation in subclass } @@ -1256,7 +1310,7 @@ void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook) void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const { r->x = r->y = r->width = r->height = 0; -// if ( [m_osxView isKindOfClass:[NSControl class]] ) + if ( [m_osxView respondsToSelector:@selector(sizeToFit)] ) { NSRect former = [m_osxView frame]; @@ -1285,7 +1339,7 @@ void wxWidgetCocoaImpl::PulseGauge() { } -void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view ) +void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) ) { } @@ -1319,9 +1373,10 @@ void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) [m_osxView setControlSize:size]; } -void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool) +void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool) { - // TODO + if ([m_osxView respondsToSelector:@selector(setFont:)]) + [m_osxView setFont: font.OSXGetNSFont()]; } void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) @@ -1344,6 +1399,7 @@ bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text) { wxKeyEvent wxevent(wxEVT_KEY_DOWN); SetupKeyEvent( wxevent, event, text ); + wxevent.SetEventObject(GetWXPeer()); return GetWXPeer()->OSXHandleKeyEvent(wxevent); } @@ -1352,7 +1408,7 @@ bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event) { wxKeyEvent wxevent(wxEVT_KEY_DOWN); SetupKeyEvent( wxevent, event ); - + wxevent.SetEventObject(GetWXPeer()); bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent); // this will fire higher level events, like insertText, to help @@ -1372,13 +1428,14 @@ bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) wxPoint pt = wxFromNSPoint( m_osxView, clickLocation ); wxMouseEvent wxevent(wxEVT_LEFT_DOWN); SetupMouseEvent( wxevent , event ) ; + wxevent.SetEventObject(GetWXPeer()); wxevent.m_x = pt.x; wxevent.m_y = pt.y; return GetWXPeer()->HandleWindowEvent(wxevent); } -void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus) +void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow) { wxWindow* thisWindow = GetWXPeer(); if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() ) @@ -1399,8 +1456,8 @@ void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus) wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId()); event.SetEventObject(thisWindow); - // TODO how to find out the targetFocusWindow ? - // event.SetWindow(targetFocusWindow); + if (otherWindow) + event.SetWindow(otherWindow->GetWXPeer()); thisWindow->HandleWindowEvent(event) ; } else // !receivedFocuss @@ -1414,8 +1471,8 @@ void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus) wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId()); event.SetEventObject(thisWindow); - // TODO how to find out the targetFocusWindow ? - // event.SetWindow(targetFocusWindow); + if (otherWindow) + event.SetWindow(otherWindow->GetWXPeer()); thisWindow->HandleWindowEvent(event) ; } } @@ -1452,8 +1509,9 @@ void wxWidgetCocoaImpl::SetFlipped(bool flipped) // Factory methods // -wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, - long style, long extraStyle) +wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size, + long WXUNUSED(style), long WXUNUSED(extraStyle)) { NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSView* v = [[wxNSView alloc] initWithFrame:r];