]> git.saurik.com Git - wxWidgets.git/commitdiff
added attr assignment for rows/columns
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 11 Feb 2000 21:14:58 +0000 (21:14 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 11 Feb 2000 21:14:58 +0000 (21:14 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5975 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/generic/grid.h
samples/newgrid/griddemo.cpp
src/generic/grid.cpp

index a6fc79412b6afc3dacb3e043acc7ddc25dc7f4d5..3095e18d37bc77f91ab22d3e7d0283cbf2314bdc 100644 (file)
@@ -199,7 +199,10 @@ private:
 // implementation note: we separate it from wxGridTableBase because we wish to
 // avoid deriving a new table class if possible, and sometimes it will be
 // enough to just derive another wxGridCellAttrProvider instead
-
+//
+// the default implementation is reasonably efficient for the generic case,
+// but you might still wish to implement your own for some specific situations
+// if you have performance problems with the stock one
 class WXDLLEXPORT wxGridCellAttrProvider
 {
 public:
@@ -209,8 +212,11 @@ public:
     // DecRef() must be called on the returned pointer
     virtual wxGridCellAttr *GetAttr(int row, int col) const;
 
-    // takes ownership of the pointer, don't call DecRef() on it
+    // all these functions take ownership of the pointer, don't call DecRef()
+    // on it
     virtual void SetAttr(wxGridCellAttr *attr, int row, int col);
+    virtual void SetRowAttr(wxGridCellAttr *attr, int row);
+    virtual void SetColAttr(wxGridCellAttr *attr, int col);
 
 private:
     void InitData();
@@ -270,8 +276,10 @@ public:
     // overridden to handle attributes directly in this class.
     virtual wxGridCellAttr *GetAttr( int row, int col );
 
-    // takes ownership of the pointer
-    virtual void SetAttr(wxGridCellAttr *attr, int row, int col );
+    // these functions take ownership of the pointer
+    virtual void SetAttr(wxGridCellAttr* attr, int row, int col);
+    virtual void SetRowAttr(wxGridCellAttr *attr, int row);
+    virtual void SetColAttr(wxGridCellAttr *attr, int col);
 
 private:
     wxGrid * m_view;
@@ -674,6 +682,10 @@ public:
     void     SetColLabelValue( int col, const wxString& );
     void     SetGridLineColour( const wxColour& );
 
+    // this sets the specified attribute for all cells in this row/col
+    void     SetRowAttr(int row, wxGridCellAttr *attr);
+    void     SetColAttr(int col, wxGridCellAttr *attr);
+
     void     EnableGridLines( bool enable = TRUE );
     bool     GridLinesEnabled() { return m_gridLinesEnabled; }
 
index 768c9c5737ef9eeeb2297b2a088cd28d90afe4f6..99b7a7492ef74d3ff054af2965da9451dd30db83 100644 (file)
@@ -201,6 +201,14 @@ GridFrame::GridFrame()
     grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
     grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
 
+    wxGridCellAttr *attr;
+    attr = new wxGridCellAttr;
+    attr->SetTextColour(*wxBLUE);
+    grid->SetColAttr(5, attr);
+    attr = new wxGridCellAttr;
+    attr->SetBackgroundColour(*wxBLUE);
+    grid->SetRowAttr(5, attr);
+
     wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
     topSizer->Add( grid,
                    1,
@@ -447,14 +455,20 @@ void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
 {
     wxColour col = wxGetColourFromUser(this);
     if ( col.Ok() )
+    {
         grid->SetDefaultCellTextColour(col);
+        grid->Refresh();
+    }
 }
 
 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
 {
     wxColour col = wxGetColourFromUser(this);
     if ( col.Ok() )
+    {
         grid->SetDefaultCellBackgroundColour(col);
+        grid->Refresh();
+    }
 }
 
 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
@@ -573,6 +587,9 @@ void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
 // MyGridCellRenderer
 // ----------------------------------------------------------------------------
 
+// do something that the default renderer doesn't here just to show that it is
+// possible to alter the appearance of the cell beyond what the attributes
+// allow
 void MyGridCellRenderer::Draw(wxGrid& grid,
                               wxDC& dc,
                               const wxRect& rect,
index 157ec1f569875cc54aaf2253850e7b93dc5a3d75..022349a967d8f0409859b3e049c14764079fd2ae 100644 (file)
@@ -9,6 +9,14 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
 #ifdef __GNUG__
     #pragma implementation "grid.h"
 #endif
 #include "wx/generic/grid.h"
 
 // ----------------------------------------------------------------------------
-// array classes instantiation
+// array classes
 // ----------------------------------------------------------------------------
 
+WX_DEFINE_ARRAY(wxGridCellAttr *, wxArrayAttrs);
+
 struct wxGridCellWithAttr
 {
     wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
@@ -156,10 +166,12 @@ private:
     DECLARE_EVENT_TABLE()
 };
 
+// ----------------------------------------------------------------------------
 // the internal data representation used by wxGridCellAttrProvider
-//
-// TODO make it more efficient
-class WXDLLEXPORT wxGridCellAttrProviderData
+// ----------------------------------------------------------------------------
+
+// this class stores attributes set for cells
+class WXDLLEXPORT wxGridCellAttrData
 {
 public:
     void SetAttr(wxGridCellAttr *attr, int row, int col);
@@ -172,6 +184,30 @@ private:
     wxGridCellWithAttrArray m_attrs;
 };
 
+// this class stores attributes set for rows or columns
+class WXDLLEXPORT wxGridRowOrColAttrData
+{
+public:
+    ~wxGridRowOrColAttrData();
+
+    void SetAttr(wxGridCellAttr *attr, int rowOrCol);
+    wxGridCellAttr *GetAttr(int rowOrCol) const;
+
+private:
+    wxArrayInt m_rowsOrCols;
+    wxArrayAttrs m_attrs;
+};
+
+// NB: this is just a wrapper around 3 objects: one which stores cell
+//     attributes, and 2 others for row/col ones
+class WXDLLEXPORT wxGridCellAttrProviderData
+{
+public:
+    wxGridCellAttrData m_cellAttrs;
+    wxGridRowOrColAttrData m_rowAttrs,
+                           m_colAttrs;
+};
+
 // ----------------------------------------------------------------------------
 // conditional compilation
 // ----------------------------------------------------------------------------
@@ -266,11 +302,10 @@ void wxGridCellStringRenderer::Draw(wxGrid& grid,
 }
 
 // ----------------------------------------------------------------------------
-// wxGridCellAttrProviderData
+// wxGridCellAttrData
 // ----------------------------------------------------------------------------
 
-void wxGridCellAttrProviderData::SetAttr(wxGridCellAttr *attr,
-                                         int row, int col)
+void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
 {
     int n = FindIndex(row, col);
     if ( n == wxNOT_FOUND )
@@ -293,7 +328,7 @@ void wxGridCellAttrProviderData::SetAttr(wxGridCellAttr *attr,
     }
 }
 
-wxGridCellAttr *wxGridCellAttrProviderData::GetAttr(int row, int col) const
+wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
 {
     wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
 
@@ -307,7 +342,7 @@ wxGridCellAttr *wxGridCellAttrProviderData::GetAttr(int row, int col) const
     return attr;
 }
 
-int wxGridCellAttrProviderData::FindIndex(int row, int col) const
+int wxGridCellAttrData::FindIndex(int row, int col) const
 {
     size_t count = m_attrs.GetCount();
     for ( size_t n = 0; n < count; n++ )
@@ -322,6 +357,59 @@ int wxGridCellAttrProviderData::FindIndex(int row, int col) const
     return wxNOT_FOUND;
 }
 
+// ----------------------------------------------------------------------------
+// wxGridRowOrColAttrData
+// ----------------------------------------------------------------------------
+
+wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
+{
+    size_t count = m_attrs.Count();
+    for ( size_t n = 0; n < count; n++ )
+    {
+        m_attrs[n]->DecRef();
+    }
+}
+
+wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
+{
+    wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
+
+    int n = m_rowsOrCols.Index(rowOrCol);
+    if ( n != wxNOT_FOUND )
+    {
+        attr = m_attrs[(size_t)n];
+        attr->IncRef();
+    }
+
+    return attr;
+}
+
+void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
+{
+    int n = m_rowsOrCols.Index(rowOrCol);
+    if ( n == wxNOT_FOUND )
+    {
+        // add the attribute
+        m_rowsOrCols.Add(rowOrCol);
+        m_attrs.Add(attr);
+    }
+    else
+    {
+        if ( attr )
+        {
+            // change the attribute
+            m_attrs[(size_t)n] = attr;
+        }
+        else
+        {
+            // remove this attribute
+            m_attrs[(size_t)n]->DecRef();
+            m_rowsOrCols.RemoveAt((size_t)n);
+            m_attrs.RemoveAt((size_t)n);
+        }
+    }
+}
+
 // ----------------------------------------------------------------------------
 // wxGridCellAttrProvider
 // ----------------------------------------------------------------------------
@@ -343,7 +431,27 @@ void wxGridCellAttrProvider::InitData()
 
 wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const
 {
-    return m_data ? m_data->GetAttr(row, col) : (wxGridCellAttr *)NULL;
+    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 )
+        {
+            // 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 )
+        {
+            // finally try the row attributes
+            attr = m_data->m_rowAttrs.GetAttr(row);
+        }
+    }
+
+    return attr;
 }
 
 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
@@ -352,9 +460,29 @@ void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
     if ( !m_data )
         InitData();
 
-    m_data->SetAttr(attr, row, col);
+    m_data->m_cellAttrs.SetAttr(attr, row, col);
+}
+
+void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
+{
+    if ( !m_data )
+        InitData();
+
+    m_data->m_rowAttrs.SetAttr(attr, row);
+}
+
+void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
+{
+    if ( !m_data )
+        InitData();
+
+    m_data->m_colAttrs.SetAttr(attr, col);
 }
 
+// ----------------------------------------------------------------------------
+// wxGridTableBase
+// ----------------------------------------------------------------------------
+
 //////////////////////////////////////////////////////////////////////
 //
 // Abstract base class for grid data (the model)
@@ -387,7 +515,7 @@ wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col)
         return (wxGridCellAttr *)NULL;
 }
 
-void wxGridTableBase::SetAttr(wxGridCellAttr *attr, int row, int col )
+void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
 {
     if ( m_attrProvider )
     {
@@ -401,6 +529,34 @@ void wxGridTableBase::SetAttr(wxGridCellAttr *attr, int row, int col )
     }
 }
 
+void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
+{
+    if ( m_attrProvider )
+    {
+        m_attrProvider->SetRowAttr(attr, row);
+    }
+    else
+    {
+        // as we take ownership of the pointer and don't store it, we must
+        // free it now
+        attr->SafeDecRef();
+    }
+}
+
+void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
+{
+    if ( m_attrProvider )
+    {
+        m_attrProvider->SetColAttr(attr, col);
+    }
+    else
+    {
+        // as we take ownership of the pointer and don't store it, we must
+        // free it now
+        attr->SafeDecRef();
+    }
+}
+
 bool wxGridTableBase::InsertRows( size_t pos, size_t numRows )
 {
     wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
@@ -4744,7 +4900,7 @@ void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
 }
 
 // ----------------------------------------------------------------------------
-// setting cell attributes: this is forwarded to the table
+// attribute support: cache, automatic provider creation, ...
 // ----------------------------------------------------------------------------
 
 bool wxGrid::CanHaveAttributes()
@@ -4845,6 +5001,34 @@ wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
     return attr;
 }
 
+// ----------------------------------------------------------------------------
+// setting cell attributes: this is forwarded to the table
+// ----------------------------------------------------------------------------
+
+void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
+{
+    if ( CanHaveAttributes() )
+    {
+        m_table->SetRowAttr(attr, row);
+    }
+    else
+    {
+        attr->SafeDecRef();
+    }
+}
+
+void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
+{
+    if ( CanHaveAttributes() )
+    {
+        m_table->SetColAttr(attr, col);
+    }
+    else
+    {
+        attr->SafeDecRef();
+    }
+}
+
 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
 {
     if ( CanHaveAttributes() )