From: Vadim Zeitlin Date: Mon, 19 Dec 2011 12:54:38 +0000 (+0000) Subject: Add possibility to set item background in generic wxDataViewCtrl. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/1c959a62ce1aea3b65fd1d642bc3b2de7c5f2111 Add possibility to set item background in generic wxDataViewCtrl. Added wxDataViewItemAttr::SetBackgroundColour() and code to honour it in the generic implementation of wxDataViewCtrl. Closes #12621. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70050 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 914f239308..b13873e7b6 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -451,6 +451,8 @@ Major new features in this release All (GUI): - Added wxFilePickerCtrl::SetInitialDirectory(). +- Added wxDataViewItemAttr::SetBackgroundColour() and implemented it in generic + wxDataViewCtrl (Andrew Xu). 2.9.3: (released 2011-12-14) diff --git a/include/wx/dataview.h b/include/wx/dataview.h index a1639f2533..1252be4a43 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -143,6 +143,7 @@ public: void SetColour(const wxColour& colour) { m_colour = colour; } void SetBold( bool set ) { m_bold = set; } void SetItalic( bool set ) { m_italic = set; } + void SetBackgroundColour(const wxColour& colour) { m_bgColour = colour; } // accessors bool HasColour() const { return m_colour.IsOk(); } @@ -152,7 +153,10 @@ public: bool GetBold() const { return m_bold; } bool GetItalic() const { return m_italic; } - bool IsDefault() const { return !(HasColour() || HasFont()); } + bool HasBackgroundColour() const { return m_bgColour.IsOk(); } + const wxColour& GetBackgroundColour() const { return m_bgColour; } + + bool IsDefault() const { return !(HasColour() || HasFont() || HasBackgroundColour()); } // Return the font based on the given one with this attribute applied to it. wxFont GetEffectiveFont(const wxFont& font) const; @@ -161,6 +165,7 @@ private: wxColour m_colour; bool m_bold; bool m_italic; + wxColour m_bgColour; }; diff --git a/include/wx/dvrenderers.h b/include/wx/dvrenderers.h index 9e34a32c87..99f1e8a837 100644 --- a/include/wx/dvrenderers.h +++ b/include/wx/dvrenderers.h @@ -290,6 +290,9 @@ public: // platform-specific classes. virtual wxDC *GetDC() = 0; + // To draw background use the background colour in wxDataViewItemAttr + virtual void RenderBackground(wxDC* dc, const wxRect& rect); + // Prepare DC to use attributes and call Render(). void WXCallRender(wxRect rect, wxDC *dc, int state); diff --git a/interface/wx/dataview.h b/interface/wx/dataview.h index 78a4bb98f4..1acce35939 100644 --- a/interface/wx/dataview.h +++ b/interface/wx/dataview.h @@ -580,6 +580,16 @@ public: */ void SetColour(const wxColour& colour); + /** + 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 + */ + void SetBackgroundColour(const wxColour& colour); + /** Call this to indicate that the item shall be displayed in italic text. */ diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index 9ee735abad..ae7dcd4043 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -822,6 +822,18 @@ bool wxDataViewCustomRendererBase::ActivateCell(const wxRect& cell, return Activate(cell, model, item, col); } +void wxDataViewCustomRendererBase::RenderBackground(wxDC* dc, const wxRect& rect) +{ + if ( !m_attr.HasBackgroundColour() ) + return; + + const wxColour& colour = m_attr.GetBackgroundColour(); + wxDCPenChanger changePen(*dc, colour); + wxDCBrushChanger changeBrush(*dc, colour); + + dc->DrawRectangle(rect); +} + void wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state) { diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 99763c1787..60ebbc4b3b 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -712,6 +712,8 @@ private: wxDataViewColumn *FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode); + void DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect ); + private: wxDataViewCtrl *m_owner; int m_lineHeight; @@ -1935,6 +1937,11 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) cell_rect.y = GetLineStart( item ); cell_rect.height = GetLineHeight( item ); + // draw the background + bool selected = m_selection.Index( item ) != wxNOT_FOUND; + if ( !selected ) + DrawCellBackground( cell, dc, cell_rect ); + // deal with the expander int indent = 0; if ((!IsList()) && (col == expander)) @@ -1988,7 +1995,7 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) continue; int state = 0; - if (m_hasFocus && (m_selection.Index(item) != wxNOT_FOUND)) + if (m_hasFocus && selected) state |= wxDATAVIEW_CELL_SELECTED; // TODO: it would be much more efficient to create a clipping @@ -2007,6 +2014,28 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) } } + +void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect ) +{ + wxRect rectBg( rect ); + + // don't overlap the horizontal rules + if ( m_owner->HasFlag(wxDV_HORIZ_RULES) ) + { + rectBg.x++; + rectBg.width--; + } + + // don't overlap the vertical rules + if ( m_owner->HasFlag(wxDV_VERT_RULES) ) + { + rectBg.y++; + rectBg.height--; + } + + cell->RenderBackground(&dc, rectBg); +} + void wxDataViewMainWindow::OnRenameTimer() { // We have to call this here because changes may just have