From 19d7140ec28c705ad1cfe75c7817ad47492825fc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 16 May 2001 13:20:31 +0000 Subject: [PATCH] applied patch 415912 (fixing problems with wxGrid attrs) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10178 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 43 +++++++-- samples/newgrid/griddemo.cpp | 26 ++++-- samples/newgrid/griddemo.h | 4 +- src/generic/grid.cpp | 172 +++++++++++++++++++++++++++-------- 4 files changed, 190 insertions(+), 55 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 8a32fe9f70..02c593a38a 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -560,6 +560,16 @@ private: class WXDLLEXPORT wxGridCellAttr { public: + enum wxAttrKind + { + Any, + Default, + Cell, + Row, + Col, + Merged + }; + // ctors wxGridCellAttr() { @@ -583,6 +593,7 @@ public: // creates a new copy of this object wxGridCellAttr *Clone() const; + void MergeWith(wxGridCellAttr *mergefrom); // 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 @@ -599,7 +610,8 @@ public: m_hAlign = hAlign; m_vAlign = vAlign; } - void SetReadOnly(bool isReadOnly = TRUE) { m_isReadOnly = isReadOnly; } + void SetReadOnly(bool isReadOnly = TRUE) + { m_isReadOnly = isReadOnly ? ReadOnly : ReadWrite; } // takes ownership of the pointer void SetRenderer(wxGridCellRenderer *renderer) @@ -607,6 +619,8 @@ public: void SetEditor(wxGridCellEditor* editor) { wxSafeDecRef(m_editor); m_editor = editor; } + void SetKind(wxAttrKind kind) { m_attrkind = kind; } + // accessors bool HasTextColour() const { return m_colText.Ok(); } bool HasBackgroundColour() const { return m_colBack.Ok(); } @@ -614,6 +628,7 @@ public: bool HasAlignment() const { return (m_hAlign != -1 || m_vAlign != -1); } bool HasRenderer() const { return m_renderer != NULL; } bool HasEditor() const { return m_editor != NULL; } + bool HasReadWriteMode() const { return m_isReadOnly != Unset; } const wxColour& GetTextColour() const; const wxColour& GetBackgroundColour() const; @@ -622,20 +637,31 @@ public: wxGridCellRenderer *GetRenderer(wxGrid* grid, int row, int col) const; wxGridCellEditor *GetEditor(wxGrid* grid, int row, int col) const; - bool IsReadOnly() const { return m_isReadOnly; } + bool IsReadOnly() const { return m_isReadOnly == wxGridCellAttr::ReadOnly; } + + wxAttrKind GetKind() { return m_attrkind; } void SetDefAttr(wxGridCellAttr* defAttr) { m_defGridAttr = defAttr; } private: + enum wxAttrReadMode + { + Unset = -1, + ReadWrite, + ReadOnly + }; + // the common part of all ctors void Init() { m_nRef = 1; - m_isReadOnly = FALSE; + m_isReadOnly = Unset; m_renderer = NULL; m_editor = NULL; + + m_attrkind = wxGridCellAttr::Cell; } // the dtor is private because only DecRef() can delete us @@ -658,7 +684,9 @@ private: wxGridCellEditor* m_editor; wxGridCellAttr* m_defGridAttr; - bool m_isReadOnly; + wxAttrReadMode m_isReadOnly; + + wxAttrKind m_attrkind; // use Clone() instead DECLARE_NO_COPY_CLASS(wxGridCellAttr); @@ -687,7 +715,8 @@ public: virtual ~wxGridCellAttrProvider(); // DecRef() must be called on the returned pointer - virtual wxGridCellAttr *GetAttr(int row, int col) const; + virtual wxGridCellAttr *GetAttr(int row, int col, + wxGridCellAttr::wxAttrKind kind ) const; // all these functions take ownership of the pointer, don't call DecRef() // on it @@ -778,7 +807,9 @@ public: // by default forwarded to wxGridCellAttrProvider if any. May be // overridden to handle attributes directly in the table. - virtual wxGridCellAttr *GetAttr( int row, int col ); + virtual wxGridCellAttr *GetAttr( int row, int col, + wxGridCellAttr::wxAttrKind kind ); + // these functions take ownership of the pointer virtual void SetAttr(wxGridCellAttr* attr, int row, int col); diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index f8b14785a4..6d8e208c06 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -262,7 +262,7 @@ GridFrame::GridFrame() grid->SetCellAlignment(4, 4, wxALIGN_CENTRE, wxALIGN_CENTRE); grid->SetCellRenderer(4, 4, new MyGridCellRenderer); - grid->SetCellValue(3, 0, "1"); + grid->SetCellValue(3, 0, "0"); grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer); grid->SetCellEditor(3, 0, new wxGridCellBoolEditor); @@ -271,23 +271,27 @@ GridFrame::GridFrame() attr->SetTextColour(*wxBLUE); grid->SetColAttr(5, attr); attr = new wxGridCellAttr; - attr->SetBackgroundColour(*wxBLUE); + attr->SetBackgroundColour(*wxRED); grid->SetRowAttr(5, attr); grid->SetCellValue(2, 4, "a wider column"); grid->SetColSize(4, 120); grid->SetColMinimalWidth(4, 120); - grid->SetColFormatFloat(5); - grid->SetCellValue(0, 5, "3.1415"); - grid->SetCellValue(1, 5, "1415"); - grid->SetCellValue(2, 5, "12345.67890"); + grid->SetCellTextColour(5, 8, *wxGREEN); + grid->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr"); + grid->SetCellValue(5, 5, "Bg from row attr\nText col from col attr"); - grid->SetColFormatFloat(6, 6, 2); + grid->SetColFormatFloat(6); grid->SetCellValue(0, 6, "3.1415"); grid->SetCellValue(1, 6, "1415"); grid->SetCellValue(2, 6, "12345.67890"); + grid->SetColFormatFloat(7, 6, 2); + grid->SetCellValue(0, 7, "3.1415"); + grid->SetCellValue(1, 7, "1415"); + grid->SetCellValue(2, 7, "12345.67890"); + wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); topSizer->Add( grid, 1, @@ -833,6 +837,7 @@ void GridFrame::OnBugsTable(wxCommandEvent& ) frame->Show(TRUE); } + void GridFrame::OnVTable(wxCommandEvent& ) { static long s_sizeGrid = 10000; @@ -898,9 +903,10 @@ MyGridCellAttrProvider::~MyGridCellAttrProvider() m_attrForOddRows->DecRef(); } -wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const +wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col, + wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const { - wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col); + wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind); if ( row % 2 ) { @@ -1256,3 +1262,5 @@ BugsGridFrame::BugsGridFrame() size.y += 10; SetClientSize(size); } + + diff --git a/samples/newgrid/griddemo.h b/samples/newgrid/griddemo.h index b35d9cfc59..48f5d77870 100644 --- a/samples/newgrid/griddemo.h +++ b/samples/newgrid/griddemo.h @@ -207,7 +207,8 @@ public: MyGridCellAttrProvider(); virtual ~MyGridCellAttrProvider(); - virtual wxGridCellAttr *GetAttr(int row, int col) const; + virtual wxGridCellAttr *GetAttr(int row, int col, + wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const; private: wxGridCellAttr *m_attrForOddRows; @@ -247,5 +248,6 @@ public: BugsGridFrame(); }; + #endif // griddemo_h diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index d6664efc0e..4957e2a461 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -1664,8 +1664,15 @@ wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) { if ( m_width == -1 ) { + if ( m_precision == -1 ) + { // default width/precision m_format = _T("%f"); + } + else + { + m_format.Printf(_T("%%.%df"), m_precision); + } } else if ( m_precision == -1 ) { @@ -1679,6 +1686,7 @@ wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) } text.Printf(m_format, val); + } //else: text already contains the string @@ -1717,8 +1725,6 @@ wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, void wxGridCellFloatRenderer::SetParameters(const wxString& params) { - bool ok = TRUE; - if ( !params ) { // reset to defaults @@ -1731,37 +1737,34 @@ void wxGridCellFloatRenderer::SetParameters(const wxString& params) if ( !!tmp ) { long width; - if ( !tmp.ToLong(&width) ) + if ( tmp.ToLong(&width) ) { - ok = FALSE; + SetWidth((int)width); } else { - SetWidth((int)width); + wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); + } + } tmp = params.AfterFirst(_T(',')); if ( !!tmp ) { long precision; - if ( !tmp.ToLong(&precision) ) + if ( tmp.ToLong(&precision) ) { - ok = FALSE; + SetPrecision((int)precision); } else { - SetPrecision((int)precision); - } - } - } + wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); } - if ( !ok ) - { - wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params.c_str()); } } } + // ---------------------------------------------------------------------------- // wxGridCellBoolRenderer // ---------------------------------------------------------------------------- @@ -1888,11 +1891,47 @@ wxGridCellAttr *wxGridCellAttr::Clone() const if ( IsReadOnly() ) attr->SetReadOnly(); + attr->SetKind( m_attrkind ); + attr->SetDefAttr(m_defGridAttr); return attr; } +void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) +{ + if ( !HasTextColour() && mergefrom->HasTextColour() ) + SetTextColour(mergefrom->GetTextColour()); + if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) + SetBackgroundColour(mergefrom->GetBackgroundColour()); + if ( !HasFont() && mergefrom->HasFont() ) + SetFont(mergefrom->GetFont()); + if ( !!HasAlignment() && mergefrom->HasAlignment() ){ + int hAlign, vAlign; + mergefrom->GetAlignment( &hAlign, &vAlign); + SetAlignment(hAlign, vAlign); + } + + // Directly access member functions as GetRender/Editor don't just return + // m_renderer/m_editor + // + // Maybe add support for merge of Render and Editor? + if (!HasRenderer() && mergefrom->HasRenderer() ) + { + m_renderer = mergefrom->m_renderer; + m_renderer->IncRef(); + } + if ( !HasEditor() && mergefrom->HasEditor() ) + { + m_editor = mergefrom->m_editor; + m_editor->IncRef(); + } + if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) + SetReadOnly(mergefrom->IsReadOnly()); + + SetDefAttr(mergefrom->m_defGridAttr); +} + const wxColour& wxGridCellAttr::GetTextColour() const { if (HasTextColour()) @@ -2245,28 +2284,77 @@ void wxGridCellAttrProvider::InitData() m_data = new wxGridCellAttrProviderData; } -wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const +wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, + wxGridCellAttr::wxAttrKind kind ) const { wxGridCellAttr *attr = (wxGridCellAttr *)NULL; if ( m_data ) { - // first look for the attribute of this specific cell - attr = m_data->m_cellAttrs.GetAttr(row, col); - - if ( !attr ) + switch(kind) { - // then look for the col attr (col attributes are more common than - // the row ones, hence they have priority) - attr = m_data->m_colAttrs.GetAttr(col); - } - - if ( !attr ) + case (wxGridCellAttr::Any): + //Get cached merge attributes. + // Currenlty not used as no cache implemented as not mutiable + // attr = m_data->m_mergeAttr.GetAttr(row, col); + if(!attr) + { + //Basicaly implement old version. + //Also check merge cache, so we don't have to re-merge every time.. + wxGridCellAttr *attrcell = (wxGridCellAttr *)NULL, + *attrrow = (wxGridCellAttr *)NULL, + *attrcol = (wxGridCellAttr *)NULL; + + attrcell = m_data->m_cellAttrs.GetAttr(row, col); + attrcol = m_data->m_colAttrs.GetAttr(col); + attrrow = m_data->m_rowAttrs.GetAttr(row); + + if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){ + // Two or move are non NULL + attr = new wxGridCellAttr; + attr->SetKind(wxGridCellAttr::Merged); + + //Order important.. + if(attrcell){ + attr->MergeWith(attrcell); + attrcell->DecRef(); + } + if(attrcol){ + attr->MergeWith(attrcol); + attrcol->DecRef(); + } + if(attrrow){ + attr->MergeWith(attrrow); + attrrow->DecRef(); + } + //store merge attr if cache implemented + //attr->IncRef(); + //m_data->m_mergeAttr.SetAttr(attr, row, col); + } + else { - // finally try the row attributes + // one or none is non null return it or null. + if(attrrow) attr = attrrow; + if(attrcol) attr = attrcol; + if(attrcell) attr = attrcell; + } + } + break; + case (wxGridCellAttr::Cell): + attr = m_data->m_cellAttrs.GetAttr(row, col); + break; + case (wxGridCellAttr::Col): + attr = m_data->m_colAttrs.GetAttr(col); + break; + case (wxGridCellAttr::Row): attr = m_data->m_rowAttrs.GetAttr(row); + break; + default: + // unused as yet... + // (wxGridCellAttr::Default): + // (wxGridCellAttr::Merged): + break; } } - return attr; } @@ -2498,10 +2586,10 @@ bool wxGridTableBase::CanHaveAttributes() return TRUE; } -wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col) +wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) { if ( m_attrProvider ) - return m_attrProvider->GetAttr(row, col); + return m_attrProvider->GetAttr(row, col, kind); else return (wxGridCellAttr *)NULL; } @@ -2510,6 +2598,7 @@ void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) { if ( m_attrProvider ) { + attr->SetKind(wxGridCellAttr::Cell); m_attrProvider->SetAttr(attr, row, col); } else @@ -2524,6 +2613,7 @@ void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) { if ( m_attrProvider ) { + attr->SetKind(wxGridCellAttr::Row); m_attrProvider->SetRowAttr(attr, row); } else @@ -2538,6 +2628,7 @@ void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) { if ( m_attrProvider ) { + attr->SetKind(wxGridCellAttr::Col); m_attrProvider->SetColAttr(attr, col); } else @@ -3447,6 +3538,7 @@ void wxGrid::Create() m_defaultCellAttr->SetDefAttr(m_defaultCellAttr); // Set default cell attributes + m_defaultCellAttr->SetKind(wxGridCellAttr::Default); m_defaultCellAttr->SetFont(GetFont()); m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP); m_defaultCellAttr->SetTextColour( @@ -7801,6 +7893,7 @@ void wxGrid::ClearAttrCache() if ( m_attrCache.row != -1 ) { wxSafeDecRef(m_attrCache.attr); + m_attrCache.attr = NULL; m_attrCache.row = -1; } } @@ -7843,7 +7936,7 @@ wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const wxGridCellAttr *attr; if ( !LookupAttr(row, col, &attr) ) { - attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL; + attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any) : (wxGridCellAttr *)NULL; CacheAttr(row, col, attr); } if (attr) @@ -7861,25 +7954,19 @@ wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const { - wxGridCellAttr *attr; - if ( !LookupAttr(row, col, &attr) || !attr ) - { + wxGridCellAttr *attr = (wxGridCellAttr *)NULL; wxASSERT_MSG( m_table, _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") ); - attr = m_table->GetAttr(row, col); + attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell ); if ( !attr ) { attr = new wxGridCellAttr; // artificially inc the ref count to match DecRef() in caller attr->IncRef(); - m_table->SetAttr(attr, row, col); } - - CacheAttr(row, col, attr); - } attr->SetDefAttr(m_defaultCellAttr); return attr; } @@ -7911,11 +7998,16 @@ void wxGrid::SetColFormatFloat(int col, int width, int precision) void wxGrid::SetColFormatCustom(int col, const wxString& typeName) { - wxGridCellAttr *attr = new wxGridCellAttr; + wxGridCellAttr *attr = (wxGridCellAttr *)NULL; + + attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col ); + if(!attr) + attr = new wxGridCellAttr; wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName); attr->SetRenderer(renderer); SetColAttr(col, attr); + } // ---------------------------------------------------------------------------- @@ -7927,6 +8019,7 @@ void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) if ( CanHaveAttributes() ) { m_table->SetRowAttr(attr, row); + ClearAttrCache(); } else { @@ -7939,6 +8032,7 @@ void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) if ( CanHaveAttributes() ) { m_table->SetColAttr(attr, col); + ClearAttrCache(); } else { -- 2.45.2