luna80
luna80

Reputation: 153

wxDataViewListCtrl change cell background color

As the title, is possible to change the background color of a single cell in a wxDataViewListCtrl? If not, is possible to change the background color of an entire row? How can I do it? Others solutions? Thanks a lot in advance for some advice

Upvotes: 2

Views: 624

Answers (2)

luna80
luna80

Reputation: 153

For those who might be interested I have solved as follows

  1. Extend wxDataViewCustomRenderer
class MyColorRenderer: public wxDataViewCustomRenderer
{
public:
    explicit MyColorRenderer(wxDataViewCellMode mode) : wxDataViewCustomRenderer("string", mode, wxALIGN_CENTER)
    { 
  }
    virtual bool Render( wxRect rect, wxDC *dc, int state ) override;

    virtual bool ActivateCell(const wxRect& WXUNUSED(cell), wxDataViewModel*WXUNUSED(model), const wxDataViewItem &WXUNUSED(item), unsigned int WXUNUSED(col), const wxMouseEvent *mouseEvent) override
    {
        return false;
    }
    virtual wxSize GetSize() const override
    {
        return wxSize(60,20);
    }

    virtual bool SetValue( const wxVariant &value ) override
    {
        m_value = value.GetString();
        return true;
    }

    virtual bool GetValue( wxVariant &WXUNUSED(value) ) const override { return true; }
    virtual bool HasEditorCtrl() const override { return true; }

    virtual wxWindow*
    CreateEditorCtrl(wxWindow* parent, wxRect labelRect,  const wxVariant& value) override
    {
        wxTextCtrl* text = new wxTextCtrl(parent, wxID_ANY, value, labelRect.GetPosition(), labelRect.GetSize(), wxTE_PROCESS_ENTER);
        text->SetInsertionPointEnd();

        return text;
    }
    virtual bool
    GetValueFromEditorCtrl(wxWindow* ctrl, wxVariant& value) override
    {
        wxTextCtrl* text = wxDynamicCast(ctrl, wxTextCtrl);
        if ( !text )
            return false;

        value = text->GetValue();

        return true;
    }
private:
    wxString m_value;
};
  1. Override render method
 bool MyColorRenderer::Render( wxRect rect, wxDC *dc, int state )
    {
        dc->SetBrush( wxColour(m_value) );
        rect.Deflate(2);
        dc->DrawRoundedRectangle( rect, 5 );
        return true;
   }
  1. create the cell as needed

lst->AppendColumn(new wxDataViewColumn("Colore",new MyColorRenderer(wxDATAVIEW_CELL_INERT),5, 60, wxALIGN_CENTER));

Upvotes: 2

VZ.
VZ.

Reputation: 22688

You can override wxDataViewListModel::GetAttrByRow() in your custom model to return the attributes with the colours (and fonts etc) you want for any cell.

Upvotes: 1

Related Questions