From 2e9a6788171a422dd2ec3daa5dedc775892e8fd7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 11 Feb 2000 18:18:18 +0000 Subject: [PATCH] fixed setting more than one attr for a cell git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5970 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 35 ++++- samples/newgrid/griddemo.cpp | 4 +- src/generic/grid.cpp | 279 ++++++++++++++++++++--------------- 3 files changed, 194 insertions(+), 124 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 610afeffe9..4a52aa1b2e 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -73,6 +73,7 @@ public: // ctors wxGridCellAttr() { + Init(); SetAlignment(0, 0); } @@ -83,11 +84,19 @@ public: int vAlign) : m_colText(colText), m_colBack(colBack), m_font(font) { + Init(); SetAlignment(hAlign, vAlign); } // default copy ctor ok + // 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 + // it until the matching DecRef() is called + void IncRef() { m_nRef++; } + void DecRef() { if ( !--m_nRef ) delete this; } + void SafeDecRef() { if ( this ) DecRef(); } + // setters void SetTextColour(const wxColour& colText) { m_colText = colText; } void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; } @@ -114,11 +123,24 @@ public: } private: + // the common part of all ctors + void Init() { m_nRef = 1; } + + // the dtor is private because only DecRef() can delete us + ~wxGridCellAttr() { } + + // the ref count - when it goes to 0, we die + size_t m_nRef; + wxColour m_colText, m_colBack; wxFont m_font; int m_hAlign, m_vAlign; + + // suppress the stupid gcc warning about the class having private dtor and + // no friends + friend class wxGridCellAttrDummyFriend; }; // ---------------------------------------------------------------------------- @@ -136,8 +158,11 @@ public: wxGridCellAttrProvider(); virtual ~wxGridCellAttrProvider(); + // DecRef() must be called on the returned pointer virtual wxGridCellAttr *GetAttr(int row, int col) const; - virtual void SetAttr(const wxGridCellAttr *attr, int row, int col); + + // takes ownership of the pointer, don't call DecRef() on it + virtual void SetAttr(wxGridCellAttr *attr, int row, int col); private: void InitData(); @@ -198,7 +223,7 @@ public: virtual wxGridCellAttr *GetAttr( int row, int col ); // takes ownership of the pointer - virtual void SetAttr(const wxGridCellAttr *attr, int row, int col ); + virtual void SetAttr(wxGridCellAttr *attr, int row, int col ); private: wxGrid * m_view; @@ -940,6 +965,12 @@ protected: // do we have some place to store attributes in? bool CanHaveAttributes(); + // returns the attribute we may modify in place: a new one if this cell + // doesn't have any yet or the existing one if it does + // + // DecRef() must be called on the returned pointer, as usual + wxGridCellAttr *GetCellAttr(int row, int col) const; + wxGridCellCoordsArray m_cellsExposed; wxArrayInt m_rowsExposed; wxArrayInt m_colsExposed; diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index ae06c29b18..6d650a36f3 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -165,9 +165,9 @@ GridFrame::GridFrame() grid->SetCellValue(2, 2, "red"); grid->SetCellTextColour(2, 2, *wxRED); - grid->SetCellValue(3, 3, "green on white"); + grid->SetCellValue(3, 3, "green on grey"); grid->SetCellTextColour(3, 3, *wxGREEN); - grid->SetCellBackgroundColour(3, 3, *wxWHITE); + grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY); wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); topSizer->Add( grid, diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index af3daea9cf..9af3fd5575 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -44,13 +44,18 @@ struct wxGridCellWithAttr { - wxGridCellWithAttr(int row, int col, const wxGridCellAttr *pAttr) - : coords(row, col), attr(*pAttr) + wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) + : coords(row, col), attr(attr_) { } + ~wxGridCellWithAttr() + { + attr->DecRef(); + } + wxGridCellCoords coords; - wxGridCellAttr attr; + wxGridCellAttr *attr; }; WX_DECLARE_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray); @@ -157,7 +162,7 @@ private: class WXDLLEXPORT wxGridCellAttrProviderData { public: - void SetAttr(const wxGridCellAttr *attr, int row, int col); + void SetAttr(wxGridCellAttr *attr, int row, int col); wxGridCellAttr *GetAttr(int row, int col) const; private: @@ -188,7 +193,7 @@ static const size_t GRID_SCROLL_LINE = 10; // wxGridCellAttrProviderData // ---------------------------------------------------------------------------- -void wxGridCellAttrProviderData::SetAttr(const wxGridCellAttr *attr, +void wxGridCellAttrProviderData::SetAttr(wxGridCellAttr *attr, int row, int col) { int n = FindIndex(row, col); @@ -202,7 +207,7 @@ void wxGridCellAttrProviderData::SetAttr(const wxGridCellAttr *attr, if ( attr ) { // change the attribute - m_attrs[(size_t)n].attr = *attr; + m_attrs[(size_t)n].attr = attr; } else { @@ -210,8 +215,6 @@ void wxGridCellAttrProviderData::SetAttr(const wxGridCellAttr *attr, m_attrs.RemoveAt((size_t)n); } } - - delete attr; } wxGridCellAttr *wxGridCellAttrProviderData::GetAttr(int row, int col) const @@ -221,7 +224,8 @@ wxGridCellAttr *wxGridCellAttrProviderData::GetAttr(int row, int col) const int n = FindIndex(row, col); if ( n != wxNOT_FOUND ) { - attr = new wxGridCellAttr(m_attrs[(size_t)n].attr); + attr = m_attrs[(size_t)n].attr; + attr->IncRef(); } return attr; @@ -266,7 +270,7 @@ wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const return m_data ? m_data->GetAttr(row, col) : (wxGridCellAttr *)NULL; } -void wxGridCellAttrProvider::SetAttr(const wxGridCellAttr *attr, +void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, int row, int col) { if ( !m_data ) @@ -307,7 +311,7 @@ wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col) return (wxGridCellAttr *)NULL; } -void wxGridTableBase::SetAttr(const wxGridCellAttr *attr, int row, int col ) +void wxGridTableBase::SetAttr(wxGridCellAttr *attr, int row, int col ) { if ( m_attrProvider ) { @@ -317,7 +321,7 @@ void wxGridTableBase::SetAttr(const wxGridCellAttr *attr, int row, int col ) { // as we take ownership of the pointer and don't store it, we must // free it now - delete attr; + attr->SafeDecRef(); } } @@ -4566,12 +4570,68 @@ int wxGrid::GetColSize( int col ) return m_colWidths[col]; } +// ============================================================================ +// access to the grid attributes: each of them has a default value in the grid +// itself and may be overidden on a per-cell basis +// ============================================================================ + +// ---------------------------------------------------------------------------- +// setting default attributes +// ---------------------------------------------------------------------------- + +void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) +{ + m_gridWin->SetBackgroundColour(col); +} + +void wxGrid::SetDefaultCellTextColour( const wxColour& col ) +{ + m_gridWin->SetForegroundColour(col); +} + +void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) +{ + m_defaultCellHAlign = horiz; + m_defaultCellVAlign = vert; +} + +void wxGrid::SetDefaultCellFont( const wxFont& font ) +{ + m_defaultCellFont = font; +} + +// ---------------------------------------------------------------------------- +// access to the default attrbiutes +// ---------------------------------------------------------------------------- + wxColour wxGrid::GetDefaultCellBackgroundColour() { return m_gridWin->GetBackgroundColour(); } -// TODO VZ: this must be optimized to allow only retrieveing attr once! +wxColour wxGrid::GetDefaultCellTextColour() +{ + return m_gridWin->GetForegroundColour(); +} + +wxFont wxGrid::GetDefaultCellFont() +{ + return m_defaultCellFont; +} + +void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) +{ + if ( horiz ) + *horiz = m_defaultCellHAlign; + if ( vert ) + *vert = m_defaultCellVAlign; +} + +// ---------------------------------------------------------------------------- +// access to cell attributes +// ---------------------------------------------------------------------------- + +// TODO VZ: we must cache the attr to allow only retrieveing it once! wxColour wxGrid::GetCellBackgroundColour(int row, int col) { @@ -4583,16 +4643,11 @@ wxColour wxGrid::GetCellBackgroundColour(int row, int col) else colour = GetDefaultCellBackgroundColour(); - delete attr; + attr->SafeDecRef(); return colour; } -wxColour wxGrid::GetDefaultCellTextColour() -{ - return m_gridWin->GetForegroundColour(); -} - wxColour wxGrid::GetCellTextColour( int row, int col ) { wxGridCellAttr *attr = m_table ? m_table->GetAttr(row, col) : NULL; @@ -4603,17 +4658,11 @@ wxColour wxGrid::GetCellTextColour( int row, int col ) else colour = GetDefaultCellTextColour(); - delete attr; + attr->SafeDecRef(); return colour; } - -wxFont wxGrid::GetDefaultCellFont() -{ - return m_defaultCellFont; -} - wxFont wxGrid::GetCellFont( int row, int col ) { wxGridCellAttr *attr = m_table ? m_table->GetAttr(row, col) : NULL; @@ -4624,19 +4673,11 @@ wxFont wxGrid::GetCellFont( int row, int col ) else font = GetDefaultCellFont(); - delete attr; + attr->SafeDecRef(); return font; } -void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) -{ - if ( horiz ) - *horiz = m_defaultCellHAlign; - if ( vert ) - *vert = m_defaultCellVAlign; -} - void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) { wxGridCellAttr *attr = m_table ? m_table->GetAttr(row, col) : NULL; @@ -4646,9 +4687,91 @@ void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) else GetDefaultCellAlignment(horiz, vert); - delete attr; + attr->SafeDecRef(); +} + +// ---------------------------------------------------------------------------- +// setting cell attributes: this is forwarded to the table +// ---------------------------------------------------------------------------- + +bool wxGrid::CanHaveAttributes() +{ + if ( !m_table ) + { + return FALSE; + } + + if ( !m_table->GetAttrProvider() ) + { + // use the default attr provider by default + // (another choice would be to just return FALSE thus forcing the user + // to it himself) + m_table->SetAttrProvider(new wxGridCellAttrProvider); + } + + return TRUE; +} + +wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const +{ + wxGridCellAttr *attr = m_table->GetAttr(row, col); + if ( !attr ) + { + attr = new wxGridCellAttr; + + // artificially inc the ref count to match DecRef() in caller + attr->IncRef(); + + m_table->SetAttr(attr, row, col); + } + + return attr; +} + +void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) +{ + if ( CanHaveAttributes() ) + { + wxGridCellAttr *attr = GetCellAttr(row, col); + attr->SetBackgroundColour(colour); + attr->DecRef(); + } +} + +void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) +{ + if ( CanHaveAttributes() ) + { + wxGridCellAttr *attr = GetCellAttr(row, col); + attr->SetTextColour(colour); + attr->DecRef(); + } +} + +void wxGrid::SetCellFont( int row, int col, const wxFont& font ) +{ + if ( CanHaveAttributes() ) + { + wxGridCellAttr *attr = GetCellAttr(row, col); + attr->SetFont(font); + attr->DecRef(); + } +} + +void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) +{ + if ( CanHaveAttributes() ) + { + wxGridCellAttr *attr = GetCellAttr(row, col); + attr->SetAlignment(horiz, vert); + attr->DecRef(); + } } +// ---------------------------------------------------------------------------- +// row/col size +// ---------------------------------------------------------------------------- + void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) { m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT ); @@ -4719,90 +4842,6 @@ void wxGrid::SetColSize( int col, int width ) CalcDimensions(); } -void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col ) -{ - m_gridWin->SetBackgroundColour(col); -} - -void wxGrid::SetDefaultCellTextColour( const wxColour& col ) -{ - m_gridWin->SetForegroundColour(col); -} - -void wxGrid::SetDefaultCellAlignment( int horiz, int vert ) -{ - m_defaultCellHAlign = horiz; - m_defaultCellVAlign = vert; -} - -bool wxGrid::CanHaveAttributes() -{ - if ( !m_table ) - { - return FALSE; - } - - if ( !m_table->GetAttrProvider() ) - { - // use the default attr provider by default - // (another choice would be to just return FALSE thus forcing the user - // to it himself) - m_table->SetAttrProvider(new wxGridCellAttrProvider); - } - - return TRUE; -} - -void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour ) -{ - if ( CanHaveAttributes() ) - { - wxGridCellAttr *attr = new wxGridCellAttr; - attr->SetBackgroundColour(colour); - - m_table->SetAttr(attr, row, col); - } -} - -void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour ) -{ - if ( CanHaveAttributes() ) - { - wxGridCellAttr *attr = new wxGridCellAttr; - attr->SetTextColour(colour); - - m_table->SetAttr(attr, row, col); - } -} - -void wxGrid::SetDefaultCellFont( const wxFont& font ) -{ - m_defaultCellFont = font; -} - -void wxGrid::SetCellFont( int row, int col, const wxFont& font ) -{ - if ( CanHaveAttributes() ) - { - wxGridCellAttr *attr = new wxGridCellAttr; - attr->SetFont(font); - - m_table->SetAttr(attr, row, col); - } -} - -void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert ) -{ - if ( CanHaveAttributes() ) - { - wxGridCellAttr *attr = new wxGridCellAttr; - attr->SetAlignment(horiz, vert); - - m_table->SetAttr(attr, row, col); - } -} - - // // ------ cell value accessor functions -- 2.45.2