diff --git a/include/wx/osx/cocoa/dataview.h b/include/wx/osx/cocoa/dataview.h index 169fe7e754..554dcbe0e6 100644 --- a/include/wx/osx/cocoa/dataview.h +++ b/include/wx/osx/cocoa/dataview.h @@ -142,6 +142,7 @@ public: [m_origFont release]; [m_origTextColour release]; + [m_origBackgroundColour release]; } NSCell* GetColumnCell() const { return m_ColumnCell; } @@ -186,6 +187,7 @@ public: // ones that do. NSFont *GetOriginalFont() const { return m_origFont; } NSColor *GetOriginalTextColour() const { return m_origTextColour; } + NSColor *GetOriginalBackgroundColour() const { return m_origBackgroundColour; } void SaveOriginalFont(NSFont *font) { @@ -197,6 +199,11 @@ public: 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. void SetEllipsizeMode(wxEllipsizeMode mode) { m_ellipsizeMode = mode; } wxEllipsizeMode GetEllipsizeMode() const { return m_ellipsizeMode; } @@ -226,6 +233,7 @@ private: // we own those if they're non-NULL NSFont *m_origFont; NSColor *m_origTextColour; + NSColor *m_origBackgroundColour; wxEllipsizeMode m_ellipsizeMode; diff --git a/interface/wx/dataview.h b/interface/wx/dataview.h index a9a8942ff2..f9decd1a4c 100644 --- a/interface/wx/dataview.h +++ b/interface/wx/dataview.h @@ -713,10 +713,9 @@ public: /** Call this to set the background colour to use. - Currently this attribute is only supported in the generic version of - wxDataViewCtrl and ignored by the native GTK+ and OS X implementations. - - @since 2.9.4 + @since 2.9.4 - Generic + @since 3.1.1 - wxGTK + @since 3.1.4 - wxOSX */ void SetBackgroundColour(const wxColour& colour); diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index 8ade48612f..1fe19e0c82 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -2720,6 +2720,7 @@ void wxDataViewRendererNativeData::Init() { m_origFont = NULL; m_origTextColour = NULL; + m_origBackgroundColour = NULL; m_ellipsizeMode = wxELLIPSIZE_MIDDLE; m_hasCustomFont = false; @@ -2862,12 +2863,13 @@ void wxDataViewRenderer::SetAttr(const wxDataViewItemAttr& attr) wxDataViewRendererNativeData * const data = GetNativeData(); NSCell * const cell = data->GetItemCell(); - // set the font and text colour to use: we need to do it if we had ever - // changed them before, even if this item itself doesn't have any special - // attributes as otherwise it would reuse the attributes from the previous - // cell rendered using the same renderer + // set the font, background and text colour to use: we need to do it if we + // had ever changed them before, even if this item itself doesn't have any + // special attributes as otherwise it would reuse the attributes from the + // previous cell rendered using the same renderer NSFont *font = NULL; NSColor *colText = NULL; + NSColor *colBack = NULL; 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 } - 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 - // 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 ( attr.HasColour() ) { - 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 - data->SaveOriginalTextColour([(id)cell textColor]); - } + if ( !data->GetOriginalTextColour() ) + { + // 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 ) font = data->GetOriginalFont(); if ( !colText ) colText = data->GetOriginalTextColour(); + if ( !colBack ) + colBack = data->GetOriginalBackgroundColour(); if ( font ) [cell setFont:font]; if ( 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)