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
This commit is contained in:
Vadim Zeitlin
2011-12-19 12:54:38 +00:00
parent 02106526ea
commit 1c959a62ce
6 changed files with 63 additions and 2 deletions

View File

@@ -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)

View File

@@ -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;
};

View File

@@ -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);

View File

@@ -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.
*/

View File

@@ -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)
{

View File

@@ -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