- int n = FindIndex(row, col);
- if ( n == wxNOT_FOUND )
- {
- // add the attribute
- m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
- }
- else
- {
- if ( attr )
- {
- // change the attribute
- m_attrs[(size_t)n].attr = attr;
- }
- else
- {
- // remove this attribute
- m_attrs.RemoveAt((size_t)n);
- }
- }
-}
-
-wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
-{
- wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
-
- int n = FindIndex(row, col);
- if ( n != wxNOT_FOUND )
- {
- attr = m_attrs[(size_t)n].attr;
- attr->IncRef();
- }
-
- return attr;
-}
-
-void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
-{
- size_t count = m_attrs.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- wxGridCellCoords& coords = m_attrs[n].coords;
- wxCoord row = coords.GetRow();
- if ((size_t)row >= pos)
- {
- if (numRows > 0)
- {
- // If rows inserted, include row counter where necessary
- coords.SetRow(row + numRows);
- }
- else if (numRows < 0)
- {
- // If rows deleted ...
- if ((size_t)row >= pos - numRows)
- {
- // ...either decrement row counter (if row still exists)...
- coords.SetRow(row + numRows);
- }
- else
- {
- // ...or remove the attribute
- m_attrs.RemoveAt((size_t)n);
- n--; count--;
- }
- }
- }
- }
-}
-
-void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
-{
- size_t count = m_attrs.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- wxGridCellCoords& coords = m_attrs[n].coords;
- wxCoord col = coords.GetCol();
- if ( (size_t)col >= pos )
- {
- if ( numCols > 0 )
- {
- // If rows inserted, include row counter where necessary
- coords.SetCol(col + numCols);
- }
- else if (numCols < 0)
- {
- // If rows deleted ...
- if ((size_t)col >= pos - numCols)
- {
- // ...either decrement row counter (if row still exists)...
- coords.SetCol(col + numCols);
- }
- else
- {
- // ...or remove the attribute
- m_attrs.RemoveAt((size_t)n);
- n--; count--;
- }
- }
- }
- }
-}
-
-int wxGridCellAttrData::FindIndex(int row, int col) const
-{
- size_t count = m_attrs.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- const wxGridCellCoords& coords = m_attrs[n].coords;
- if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
- {
- return 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);
- }
- }
-}
-
-void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
-{
- size_t count = m_attrs.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- int & rowOrCol = m_rowsOrCols[n];
- if ( (size_t)rowOrCol >= pos )
- {
- if ( numRowsOrCols > 0 )
- {
- // If rows inserted, include row counter where necessary
- rowOrCol += numRowsOrCols;
- }
- else if ( numRowsOrCols < 0)
- {
- // If rows deleted, either decrement row counter (if row still exists)
- if ((size_t)rowOrCol >= pos - numRowsOrCols)
- rowOrCol += numRowsOrCols;
- else
- {
- m_rowsOrCols.RemoveAt((size_t)n);
- m_attrs.RemoveAt((size_t)n);
- n--; count--;
- }
- }
- }
- }
-}
-
-// ----------------------------------------------------------------------------
-// wxGridCellAttrProvider
-// ----------------------------------------------------------------------------
-
-wxGridCellAttrProvider::wxGridCellAttrProvider()
-{
- m_data = (wxGridCellAttrProviderData *)NULL;
-}
-
-wxGridCellAttrProvider::~wxGridCellAttrProvider()
-{
- delete m_data;
-}
-
-void wxGridCellAttrProvider::InitData()
-{
- m_data = new wxGridCellAttrProviderData;
-}
-
-wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) 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 )
- {
- // 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,
- int row, int col)
-{
- if ( !m_data )
- InitData();
-
- 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);
-}
-
-void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
-{
- if ( m_data )
- {
- m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
-
- m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
- }
-}
-
-void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
-{
- if ( m_data )
- {
- m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
-
- m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
- }
-}
-
-// ----------------------------------------------------------------------------
-// wxGridTypeRegistry
-// ----------------------------------------------------------------------------
-
-wxGridTypeRegistry::~wxGridTypeRegistry()
-{
- size_t count = m_typeinfo.Count();
- for ( size_t i = 0; i < count; i++ )
- delete m_typeinfo[i];
-}
-
-
-void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
- wxGridCellRenderer* renderer,
- wxGridCellEditor* editor)
-{
- int loc;
- wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
-
- // is it already registered?
- if ((loc = FindDataType(typeName)) != -1) {
- delete m_typeinfo[loc];
- m_typeinfo[loc] = info;
- }
- else {
- m_typeinfo.Add(info);
- }
-}
-
-int wxGridTypeRegistry::FindDataType(const wxString& typeName)
-{
- int found = -1;
-
- for (size_t i=0; i<m_typeinfo.Count(); i++) {
- if (typeName == m_typeinfo[i]->m_typeName) {
- found = i;
- break;
- }
- }
-
- return found;
-}
-
-wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
-{
- wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
- return renderer;
-}
-
-wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
-{
- wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
- return editor;
-}
-
-// ----------------------------------------------------------------------------
-// wxGridTableBase
-// ----------------------------------------------------------------------------
-
-IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
-
-
-wxGridTableBase::wxGridTableBase()
-{
- m_view = (wxGrid *) NULL;
- m_attrProvider = (wxGridCellAttrProvider *) NULL;
-}
-
-wxGridTableBase::~wxGridTableBase()
-{
- delete m_attrProvider;
-}
-
-void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
-{
- delete m_attrProvider;
- m_attrProvider = attrProvider;
-}
-
-bool wxGridTableBase::CanHaveAttributes()
-{
- if ( ! GetAttrProvider() )
- {
- // use the default attr provider by default
- SetAttrProvider(new wxGridCellAttrProvider);
- }
- return TRUE;
-}
-
-wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col)
-{
- if ( m_attrProvider )
- return m_attrProvider->GetAttr(row, col);
- else
- return (wxGridCellAttr *)NULL;
-}
-
-void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
-{
- if ( m_attrProvider )
- {
- m_attrProvider->SetAttr(attr, row, col);
- }
- else
- {
- // as we take ownership of the pointer and don't store it, we must
- // free it now
- attr->SafeDecRef();
- }
-}
-
-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();
- }
-}
-
-void wxGridTableBase::UpdateAttrRows( size_t pos, int numRows )
-{
- if ( m_attrProvider )
- {
- m_attrProvider->UpdateAttrRows( pos, numRows );
- }
-}
-
-void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols )
-{
- if ( m_attrProvider )
- {
- m_attrProvider->UpdateAttrCols( pos, numCols );
- }
-}
-
-bool wxGridTableBase::InsertRows( size_t pos, size_t numRows )
-{
- wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
- "but your derived table class does not override this function") );
-
- return FALSE;
-}
-
-bool wxGridTableBase::AppendRows( size_t numRows )
-{
- wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
- "but your derived table class does not override this function"));
-
- return FALSE;
-}
-
-bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows )
-{
- wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
- "but your derived table class does not override this function"));
-
- return FALSE;
-}
-
-bool wxGridTableBase::InsertCols( size_t pos, size_t numCols )
-{
- wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
- "but your derived table class does not override this function"));
-
- return FALSE;
-}
-
-bool wxGridTableBase::AppendCols( size_t numCols )
-{
- wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
- "but your derived table class does not override this function"));
-
- return FALSE;
-}
-
-bool wxGridTableBase::DeleteCols( size_t pos, size_t numCols )
-{
- wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
- "but your derived table class does not override this function"));
-
- return FALSE;
-}
-
-
-wxString wxGridTableBase::GetRowLabelValue( int row )
-{
- wxString s;
- s << row + 1; // RD: Starting the rows at zero confuses users, no matter
- // how much it makes sense to us geeks.
- return s;
-}
-
-wxString wxGridTableBase::GetColLabelValue( int col )
-{
- // default col labels are:
- // cols 0 to 25 : A-Z
- // cols 26 to 675 : AA-ZZ
- // etc.
-
- wxString s;
- unsigned int i, n;
- for ( n = 1; ; n++ )
- {
- s += (_T('A') + (wxChar)( col%26 ));
- col = col/26 - 1;
- if ( col < 0 ) break;
- }
-
- // reverse the string...
- wxString s2;
- for ( i = 0; i < n; i++ )
- {
- s2 += s[n-i-1];
- }
-
- return s2;
-}
-
-
-wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
-{
- return wxGRID_VALUE_STRING;
-}
-
-bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
- const wxString& typeName )
-{
- return typeName == wxGRID_VALUE_STRING;
-}
-
-bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
-{
- return CanGetValueAs(row, col, typeName);
-}
-
-long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
-{
- return 0;
-}
-
-double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
-{
- return 0.0;
-}
-
-bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
-{
- return FALSE;
-}
-
-void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
- long WXUNUSED(value) )
-{
-}
-
-void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
- double WXUNUSED(value) )
-{
-}
-
-void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
- bool WXUNUSED(value) )
-{
-}
-
-
-void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
- const wxString& WXUNUSED(typeName) )
-{
- return NULL;
-}
-
-void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
- const wxString& WXUNUSED(typeName),
- void* WXUNUSED(value) )
-{
-}
-
-
-//////////////////////////////////////////////////////////////////////
-//
-// Message class for the grid table to send requests and notifications
-// to the grid view
-//
-
-wxGridTableMessage::wxGridTableMessage()
-{
- m_table = (wxGridTableBase *) NULL;