Implement background color attribute for wxDataViewCtrl in wxOSX

Add support for this attribute for text-like cells to the native macOS
version too, to bring it up to parity with the generic and GTK ones.

Closes https://github.com/wxWidgets/wxWidgets/pull/1673
This commit is contained in:
Ian McInerney
2019-12-07 00:51:27 +00:00
committed by Vadim Zeitlin
parent c4af8be615
commit 540fed9216
3 changed files with 61 additions and 19 deletions

View File

@@ -142,6 +142,7 @@ public:
[m_origFont release]; [m_origFont release];
[m_origTextColour release]; [m_origTextColour release];
[m_origBackgroundColour release];
} }
NSCell* GetColumnCell() const { return m_ColumnCell; } NSCell* GetColumnCell() const { return m_ColumnCell; }
@@ -186,6 +187,7 @@ public:
// ones that do. // ones that do.
NSFont *GetOriginalFont() const { return m_origFont; } NSFont *GetOriginalFont() const { return m_origFont; }
NSColor *GetOriginalTextColour() const { return m_origTextColour; } NSColor *GetOriginalTextColour() const { return m_origTextColour; }
NSColor *GetOriginalBackgroundColour() const { return m_origBackgroundColour; }
void SaveOriginalFont(NSFont *font) void SaveOriginalFont(NSFont *font)
{ {
@@ -197,6 +199,11 @@ public:
m_origTextColour = [textColour retain]; m_origTextColour = [textColour retain];
} }
void SaveOriginalBackgroundColour(NSColor *backgroundColour)
{
m_origBackgroundColour = [backgroundColour retain];
}
// The ellipsization mode which we need to set for each cell being rendered. // The ellipsization mode which we need to set for each cell being rendered.
void SetEllipsizeMode(wxEllipsizeMode mode) { m_ellipsizeMode = mode; } void SetEllipsizeMode(wxEllipsizeMode mode) { m_ellipsizeMode = mode; }
wxEllipsizeMode GetEllipsizeMode() const { return m_ellipsizeMode; } wxEllipsizeMode GetEllipsizeMode() const { return m_ellipsizeMode; }
@@ -226,6 +233,7 @@ private:
// we own those if they're non-NULL // we own those if they're non-NULL
NSFont *m_origFont; NSFont *m_origFont;
NSColor *m_origTextColour; NSColor *m_origTextColour;
NSColor *m_origBackgroundColour;
wxEllipsizeMode m_ellipsizeMode; wxEllipsizeMode m_ellipsizeMode;

View File

@@ -713,10 +713,9 @@ public:
/** /**
Call this to set the background colour to use. Call this to set the background colour to use.
Currently this attribute is only supported in the generic version of @since 2.9.4 - Generic
wxDataViewCtrl and ignored by the native GTK+ and OS X implementations. @since 3.1.1 - wxGTK
@since 3.1.4 - wxOSX
@since 2.9.4
*/ */
void SetBackgroundColour(const wxColour& colour); void SetBackgroundColour(const wxColour& colour);

View File

@@ -2720,6 +2720,7 @@ void wxDataViewRendererNativeData::Init()
{ {
m_origFont = NULL; m_origFont = NULL;
m_origTextColour = NULL; m_origTextColour = NULL;
m_origBackgroundColour = NULL;
m_ellipsizeMode = wxELLIPSIZE_MIDDLE; m_ellipsizeMode = wxELLIPSIZE_MIDDLE;
m_hasCustomFont = false; m_hasCustomFont = false;
@@ -2862,12 +2863,13 @@ void wxDataViewRenderer::SetAttr(const wxDataViewItemAttr& attr)
wxDataViewRendererNativeData * const data = GetNativeData(); wxDataViewRendererNativeData * const data = GetNativeData();
NSCell * const cell = data->GetItemCell(); NSCell * const cell = data->GetItemCell();
// set the font and text colour to use: we need to do it if we had ever // set the font, background and text colour to use: we need to do it if we
// changed them before, even if this item itself doesn't have any special // had ever changed them before, even if this item itself doesn't have any
// attributes as otherwise it would reuse the attributes from the previous // special attributes as otherwise it would reuse the attributes from the
// cell rendered using the same renderer // previous cell rendered using the same renderer
NSFont *font = NULL; NSFont *font = NULL;
NSColor *colText = NULL; NSColor *colText = NULL;
NSColor *colBack = NULL;
if ( attr.HasFont() ) if ( attr.HasFont() )
{ {
@@ -2892,34 +2894,67 @@ void wxDataViewRenderer::SetAttr(const wxDataViewItemAttr& attr)
//else: can't change font if the cell doesn't have any //else: can't change font if the cell doesn't have any
} }
if ( attr.HasColour() && [cell backgroundStyle] == NSBackgroundStyleLight ) // We don't apply the text or background colours if the cell is selected.
if ( [cell backgroundStyle] == NSBackgroundStyleLight )
{ {
// we can set font for any cell but only NSTextFieldCell provides if ( attr.HasColour() )
// a method for setting text colour so check that this method is
// available before using it
if ( [cell respondsToSelector:@selector(setTextColor:)] &&
[cell respondsToSelector:@selector(textColor)] )
{ {
if ( !data->GetOriginalTextColour() ) // we can set font for any cell but only NSTextFieldCell provides
// a method for setting text colour so check that this method is
// available before using it
if ( [cell respondsToSelector:@selector(setTextColor:)] &&
[cell respondsToSelector:@selector(textColor)] )
{ {
// the cast to (untyped) id is safe because of the check above if ( !data->GetOriginalTextColour() )
data->SaveOriginalTextColour([(id)cell textColor]); {
} // the cast to (untyped) id is safe because of the check above
data->SaveOriginalTextColour([(id)cell textColor]);
}
colText = attr.GetColour().OSXGetNSColor(); colText = attr.GetColour().OSXGetNSColor();
}
}
if ( attr.HasBackgroundColour() )
{
// Use the same logic as the text colour check above
if ( [cell respondsToSelector:@selector(setBackgroundColor:)] &&
[cell respondsToSelector:@selector(backgroundColor)] )
{
if ( !data->GetOriginalBackgroundColour() )
data->SaveOriginalTextColour([(id)cell backgroundColor]);
colBack = attr.GetBackgroundColour().OSXGetNSColor();
}
} }
} }
if ( !font ) if ( !font )
font = data->GetOriginalFont(); font = data->GetOriginalFont();
if ( !colText ) if ( !colText )
colText = data->GetOriginalTextColour(); colText = data->GetOriginalTextColour();
if ( !colBack )
colBack = data->GetOriginalBackgroundColour();
if ( font ) if ( font )
[cell setFont:font]; [cell setFont:font];
if ( colText ) if ( colText )
[(id)cell setTextColor:colText]; [(id)cell setTextColor:colText];
if ( [cell respondsToSelector:@selector(setDrawsBackground:)] )
{
if ( colBack )
{
[(id)cell setDrawsBackground:true];
[(id)cell setBackgroundColor:colBack];
}
else
{
[(id)cell setDrawsBackground:false];
}
}
} }
void wxDataViewRenderer::SetEnabled(bool enabled) void wxDataViewRenderer::SetEnabled(bool enabled)