// 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:
// 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();
// 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;
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; }
// 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_)
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);
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
// ----------------------------------------------------------------------------
}
// ----------------------------------------------------------------------------
-// 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 )
}
}
-wxGridCellAttr *wxGridCellAttrProviderData::GetAttr(int row, int col) const
+wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
{
wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
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++ )
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
// ----------------------------------------------------------------------------
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,
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)
return (wxGridCellAttr *)NULL;
}
-void wxGridTableBase::SetAttr(wxGridCellAttr *attr, int row, int col )
+void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
{
if ( m_attrProvider )
{
}
}
+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"
}
// ----------------------------------------------------------------------------
-// setting cell attributes: this is forwarded to the table
+// attribute support: cache, automatic provider creation, ...
// ----------------------------------------------------------------------------
bool wxGrid::CanHaveAttributes()
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() )