From a68c1246db34618d4e60e2a6ccdac4f35700a3f1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 28 Feb 2000 19:08:01 +0000 Subject: [PATCH] wxGridCellAttr::Clone() added to allow the demo of custom grid cell attr provider to work git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6339 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 9 ++++- samples/newgrid/griddemo.cpp | 64 ++++++++++++++++++++++++++++++++++-- samples/newgrid/griddemo.h | 16 +++++++++ src/generic/grid.cpp | 31 +++++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index dcbd638f76..32560636f6 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -435,6 +435,7 @@ private: wxArrayString m_choices; bool m_allowOthers; }; + // ---------------------------------------------------------------------------- // wxGridCellAttr: this class can be used to alter the cells appearance in // the grid by changing their colour/font/... from default. An object of this @@ -464,7 +465,10 @@ public: SetAlignment(hAlign, vAlign); } - // default copy ctor ok + // creates a new copy of this object: warning, this is destructive copy + // (this is why it's non const), the renderer and editor are "given to" + // the new object + wxGridCellAttr *Clone(); // this class is ref counted: it is created with ref count of 1, so // calling DecRef() once will delete it. Calling IncRef() allows to lock @@ -540,6 +544,9 @@ private: bool m_isReadOnly; + // use Clone() instead + DECLARE_NO_COPY_CLASS(wxGridCellAttr); + // suppress the stupid gcc warning about the class having private dtor and // no friends friend class wxGridCellAttrDummyFriend; diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index 5095fe7809..7272409b2e 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -698,17 +698,62 @@ void MyGridCellRenderer::Draw(wxGrid& grid, dc.DrawEllipse(rect); } - // ---------------------------------------------------------------------------- -// BigGridFrame and BigGridTable: Sample of a non-standard table +// MyGridCellAttrProvider // ---------------------------------------------------------------------------- +MyGridCellAttrProvider::MyGridCellAttrProvider() +{ + m_attrForOddRows = new wxGridCellAttr; + m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY); +} + +MyGridCellAttrProvider::~MyGridCellAttrProvider() +{ + m_attrForOddRows->DecRef(); +} + +wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const +{ + wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col); + + if ( row % 2 ) + { + if ( !attr ) + { + attr = m_attrForOddRows; + attr->IncRef(); + } + else + { + if ( !attr->HasBackgroundColour() ) + { + wxGridCellAttr *attrNew = attr->Clone(); + attr->DecRef(); + attr = attrNew; + attr->SetBackgroundColour(*wxLIGHT_GREY); + } + } + } + + return attr; +} + +// ============================================================================ +// BigGridFrame and BigGridTable: Sample of a non-standard table +// ============================================================================ + BigGridFrame::BigGridFrame(long sizeGrid) : wxFrame(NULL, -1, "Plugin Virtual Table", wxDefaultPosition, wxSize(500, 450)) { m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize); m_table = new BigGridTable(sizeGrid); + + // VZ: I don't understand why this slows down the display that much, + // must profile it... + //m_table->SetAttrProvider(new MyGridCellAttrProvider); + m_grid->SetTable(m_table, TRUE); #if defined __WXMOTIF__ @@ -719,8 +764,12 @@ BigGridFrame::BigGridFrame(long sizeGrid) #endif } -// ---------------------------------------------------------------------------- +// ============================================================================ // BugsGridFrame: a "realistic" table +// ============================================================================ + +// ---------------------------------------------------------------------------- +// bugs table data // ---------------------------------------------------------------------------- enum Columns @@ -789,6 +838,10 @@ static const wxChar *headers[Col_Max] = _T("Opened?"), }; +// ---------------------------------------------------------------------------- +// BugsGridTable +// ---------------------------------------------------------------------------- + wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col) { switch ( col ) @@ -992,12 +1045,17 @@ BugsGridTable::BugsGridTable() { } +// ---------------------------------------------------------------------------- +// BugsGridFrame +// ---------------------------------------------------------------------------- + BugsGridFrame::BugsGridFrame() : wxFrame(NULL, -1, "Bugs table", wxDefaultPosition, wxSize(500, 300)) { wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition); wxGridTableBase *table = new BugsGridTable(); + table->SetAttrProvider(new MyGridCellAttrProvider); grid->SetTable(table, TRUE); wxGridCellAttr *attrRO = new wxGridCellAttr, diff --git a/samples/newgrid/griddemo.h b/samples/newgrid/griddemo.h index 2bc68c2082..bddc25387a 100644 --- a/samples/newgrid/griddemo.h +++ b/samples/newgrid/griddemo.h @@ -158,6 +158,22 @@ private: BigGridTable* m_table; }; +// ---------------------------------------------------------------------------- +// an example of custom attr provider: this one makes all odd rows appear grey +// ---------------------------------------------------------------------------- + +class MyGridCellAttrProvider : public wxGridCellAttrProvider +{ +public: + MyGridCellAttrProvider(); + virtual ~MyGridCellAttrProvider(); + + virtual wxGridCellAttr *GetAttr(int row, int col) const; + +private: + wxGridCellAttr *m_attrForOddRows; +}; + // ---------------------------------------------------------------------------- // another, more realistic, grid example: shows typed columns and more // ---------------------------------------------------------------------------- diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 00f2271e51..a174ccf7a2 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -1354,6 +1354,37 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid, // wxGridCellAttr // ---------------------------------------------------------------------------- +wxGridCellAttr *wxGridCellAttr::Clone() +{ + wxGridCellAttr *attr = new wxGridCellAttr; + if ( HasTextColour() ) + attr->SetTextColour(GetTextColour()); + if ( HasBackgroundColour() ) + attr->SetBackgroundColour(GetBackgroundColour()); + if ( HasFont() ) + attr->SetFont(GetFont()); + if ( HasAlignment() ) + attr->SetAlignment(m_hAlign, m_vAlign); + + if ( m_renderer ) + { + attr->SetRenderer(m_renderer); + m_renderer = NULL; + } + if ( m_editor ) + { + attr->SetEditor(m_editor); + m_editor = NULL; + } + + if ( IsReadOnly() ) + attr->SetReadOnly(); + + attr->SetDefAttr(m_defGridAttr); + + return attr; +} + const wxColour& wxGridCellAttr::GetTextColour() const { if (HasTextColour()) -- 2.45.2