From ede4ff9490df836985af0ca4200e74d89adb2df4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 13 Mar 2021 23:01:21 +0100 Subject: [PATCH 1/2] Factor out wxWidgetCocoaImpl::GetViewWithText() Extract this function from wxWidgetCocoaImpl::SetFont() to allow its reuse. No changes, this is a pure refactoring. --- include/wx/osx/cocoa/private.h | 4 ++++ src/osx/cocoa/window.mm | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 7c13d5897c..69b4d9795f 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -241,6 +241,10 @@ protected: // was the wx event for the current native key down event sent bool WasKeyDownSent() const; + + // Return the view to apply the font/colour to. + NSView* GetViewWithText() const; + NSEvent* m_lastKeyDownEvent; bool m_lastKeyDownWXSent; #if !wxOSX_USE_NATIVE_FLIPPED diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 3336fa0fca..70de5721ab 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -3543,13 +3543,19 @@ void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) } } +NSView* wxWidgetCocoaImpl::GetViewWithText() const +{ + if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) + return [(NSScrollView*) m_osxView documentView]; + else if ( [m_osxView isKindOfClass:[NSBox class] ] ) + return [(NSBox*) m_osxView titleCell]; + + return m_osxView; +} + void wxWidgetCocoaImpl::SetFont(wxFont const& font) { - NSView* targetView = m_osxView; - if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) - targetView = [(NSScrollView*) m_osxView documentView]; - else if ( [m_osxView isKindOfClass:[NSBox class] ] ) - targetView = [(NSBox*) m_osxView titleCell]; + NSView* const targetView = GetViewWithText(); if ([targetView respondsToSelector:@selector(setFont:)]) [targetView setFont: font.OSXGetNSFont()]; From ac4634b294e8313c0df32ff305a9abddb3452bc7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 13 Mar 2021 23:03:28 +0100 Subject: [PATCH 2/2] Update just the colour, not font, in wxOSX SetForegroundColour() Calling wxWindow::SetForegroundColour() also changed the window font, which was unexpected and unnecessary. Add a separate peer SetForegroundColour() method and implement it to change just the colour in Cocoa version (and leave unimplemented, just as it was before, for iOS) and use it in wxWindow to avoid the font change. --- include/wx/osx/cocoa/private.h | 1 + include/wx/osx/core/private.h | 1 + include/wx/osx/iphone/private.h | 1 + include/wx/osx/window.h | 1 - src/osx/cocoa/window.mm | 10 ++++++++-- src/osx/iphone/window.mm | 5 +++++ src/osx/window_osx.cpp | 29 ++++++++++++++--------------- 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 69b4d9795f..c145871fc6 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -101,6 +101,7 @@ public : virtual void SetBackgroundColour(const wxColour&) wxOVERRIDE; virtual bool SetBackgroundStyle(wxBackgroundStyle style) wxOVERRIDE; + virtual void SetForegroundColour(const wxColour& col) wxOVERRIDE; virtual void GetContentArea( int &left, int &top, int &width, int &height ) const wxOVERRIDE; virtual void Move(int x, int y, int width, int height) wxOVERRIDE; diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index cc584f563b..1c21fae66c 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -276,6 +276,7 @@ public : virtual void SetBackgroundColour( const wxColour& col ) = 0; virtual bool SetBackgroundStyle(wxBackgroundStyle style) = 0; + virtual void SetForegroundColour( const wxColour& col ) = 0; // all coordinates in native parent widget relative coordinates virtual void GetContentArea( int &left , int &top , int &width , int &height ) const = 0; diff --git a/include/wx/osx/iphone/private.h b/include/wx/osx/iphone/private.h index 53a3e926ce..42a4557879 100644 --- a/include/wx/osx/iphone/private.h +++ b/include/wx/osx/iphone/private.h @@ -56,6 +56,7 @@ public : virtual void SetBackgroundColour( const wxColour& col ) ; virtual bool SetBackgroundStyle(wxBackgroundStyle style) ; + virtual void SetForegroundColour( const wxColour& col ) ; virtual void GetContentArea( int &left , int &top , int &width , int &height ) const; virtual void Move(int x, int y, int width, int height); diff --git a/include/wx/osx/window.h b/include/wx/osx/window.h index f761a598bc..c19e5d1b9f 100644 --- a/include/wx/osx/window.h +++ b/include/wx/osx/window.h @@ -342,7 +342,6 @@ protected: bool MacHasScrollBarCorner() const; void MacCreateScrollBars( long style ) ; void MacRepositionScrollBars() ; - void MacUpdateControlFont() ; // implement the base class pure virtuals virtual void DoGetTextExtent(const wxString& string, diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 70de5721ab..f63b21ab59 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -3559,13 +3559,19 @@ void wxWidgetCocoaImpl::SetFont(wxFont const& font) if ([targetView respondsToSelector:@selector(setFont:)]) [targetView setFont: font.OSXGetNSFont()]; + if ([m_osxView respondsToSelector:@selector(setAttributedTitle:)]) + SetLabel(wxStripMenuCodes(GetWXPeer()->GetLabel(), wxStrip_Mnemonics), GetWXPeer()->GetFont().GetEncoding()); +} + +void wxWidgetCocoaImpl::SetForegroundColour(const wxColour& col) +{ + NSView* const targetView = GetViewWithText(); + if ([targetView respondsToSelector:@selector(setTextColor:)]) { wxColor col = GetWXPeer()->GetForegroundColour(); [targetView setTextColor: col.OSXGetNSColor()]; } - if ([m_osxView respondsToSelector:@selector(setAttributedTitle:)]) - SetLabel(wxStripMenuCodes(GetWXPeer()->GetLabel(), wxStrip_Mnemonics), GetWXPeer()->GetFont().GetEncoding()); } void wxWidgetCocoaImpl::SetToolTip(wxToolTip* tooltip) diff --git a/src/osx/iphone/window.mm b/src/osx/iphone/window.mm index 2dd707c28c..65854cc2cc 100644 --- a/src/osx/iphone/window.mm +++ b/src/osx/iphone/window.mm @@ -482,6 +482,11 @@ void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col ) m_osxView.backgroundColor = [UIColor colorWithCGColor:col.GetCGColor()]; } +void wxWidgetIPhoneImpl::SetForegroundColour( const wxColour &col ) +{ + // TODO: use textColor if available? +} + bool wxWidgetIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style) { if ( style == wxBG_STYLE_PAINT ) diff --git a/src/osx/window_osx.cpp b/src/osx/window_osx.cpp index ef60bef9da..d7e1b04040 100644 --- a/src/osx/window_osx.cpp +++ b/src/osx/window_osx.cpp @@ -504,33 +504,32 @@ void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant ) } } -void wxWindowMac::MacUpdateControlFont() -{ - if ( GetPeer() ) - GetPeer()->SetFont(GetFont()) ; - - // do not trigger refreshes upon invisible and possible partly created objects - if ( IsShownOnScreen() ) - Refresh() ; -} - bool wxWindowMac::SetFont(const wxFont& font) { bool retval = wxWindowBase::SetFont( font ); - MacUpdateControlFont() ; + if (retval) + { + if ( GetPeer() ) + GetPeer()->SetFont(GetFont()) ; + + // do not trigger refreshes upon invisible and possible partly created objects + if ( IsShownOnScreen() ) + Refresh() ; + } return retval; } bool wxWindowMac::SetForegroundColour(const wxColour& col ) { - bool retval = wxWindowBase::SetForegroundColour( col ); + if ( !wxWindowBase::SetForegroundColour( col ) ) + return false; - if (retval) - MacUpdateControlFont(); + if ( GetPeer() ) + GetPeer()->SetForegroundColour(col); - return retval; + return true; } bool wxWindowMac::SetBackgroundStyle(wxBackgroundStyle style)