+ m_defaultCellAttr->SetTextColour(col);
+}
+
+void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
+{
+ m_defaultCellAttr->SetAlignment(horiz, vert);
+}
+
+void wxGrid::SetDefaultCellOverflow( bool allow )
+{
+ m_defaultCellAttr->SetOverflow(allow);
+}
+
+void wxGrid::SetDefaultCellFont( const wxFont& font )
+{
+ m_defaultCellAttr->SetFont(font);
+}
+
+// For editors and renderers the type registry takes precedence over the
+// default attr, so we need to register the new editor/renderer for the string
+// data type in order to make setting a default editor/renderer appear to
+// work correctly.
+
+void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
+{
+ RegisterDataType(wxGRID_VALUE_STRING,
+ renderer,
+ GetDefaultEditorForType(wxGRID_VALUE_STRING));
+}
+
+void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
+{
+ RegisterDataType(wxGRID_VALUE_STRING,
+ GetDefaultRendererForType(wxGRID_VALUE_STRING),
+ editor);
+}
+
+// ----------------------------------------------------------------------------
+// access to the default attributes
+// ----------------------------------------------------------------------------
+
+wxColour wxGrid::GetDefaultCellBackgroundColour() const
+{
+ return m_defaultCellAttr->GetBackgroundColour();
+}
+
+wxColour wxGrid::GetDefaultCellTextColour() const
+{
+ return m_defaultCellAttr->GetTextColour();
+}
+
+wxFont wxGrid::GetDefaultCellFont() const
+{
+ return m_defaultCellAttr->GetFont();
+}
+
+void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
+{
+ m_defaultCellAttr->GetAlignment(horiz, vert);
+}
+
+bool wxGrid::GetDefaultCellOverflow() const
+{
+ return m_defaultCellAttr->GetOverflow();
+}
+
+wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
+{
+ return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
+}
+
+wxGridCellEditor *wxGrid::GetDefaultEditor() const
+{
+ return m_defaultCellAttr->GetEditor(NULL, 0, 0);
+}
+
+// ----------------------------------------------------------------------------
+// access to cell attributes
+// ----------------------------------------------------------------------------
+
+wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
+{
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ wxColour colour = attr->GetBackgroundColour();
+ attr->DecRef();
+
+ return colour;
+}
+
+wxColour wxGrid::GetCellTextColour( int row, int col ) const
+{
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ wxColour colour = attr->GetTextColour();
+ attr->DecRef();
+
+ return colour;
+}
+
+wxFont wxGrid::GetCellFont( int row, int col ) const
+{
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ wxFont font = attr->GetFont();
+ attr->DecRef();
+
+ return font;
+}
+
+void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
+{
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ attr->GetAlignment(horiz, vert);
+ attr->DecRef();
+}
+
+bool wxGrid::GetCellOverflow( int row, int col ) const
+{
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ bool allow = attr->GetOverflow();
+ attr->DecRef();
+
+ return allow;
+}
+
+wxGrid::CellSpan
+wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
+{
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ attr->GetSize( num_rows, num_cols );
+ attr->DecRef();
+
+ if ( *num_rows == 1 && *num_cols == 1 )
+ return CellSpan_None; // just a normal cell
+
+ if ( *num_rows < 0 || *num_cols < 0 )
+ return CellSpan_Inside; // covered by a multi-span cell
+
+ // this cell spans multiple cells to its right/bottom
+ return CellSpan_Main;
+}
+
+wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
+{
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
+ attr->DecRef();
+
+ return renderer;
+}
+
+wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
+{
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellEditor* editor = attr->GetEditor(this, row, col);
+ attr->DecRef();
+
+ return editor;
+}
+
+bool wxGrid::IsReadOnly(int row, int col) const
+{
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ bool isReadOnly = attr->IsReadOnly();
+ attr->DecRef();
+
+ return isReadOnly;
+}
+
+// ----------------------------------------------------------------------------
+// attribute support: cache, automatic provider creation, ...
+// ----------------------------------------------------------------------------
+
+bool wxGrid::CanHaveAttributes() const
+{
+ if ( !m_table )
+ {
+ return false;
+ }
+
+ return m_table->CanHaveAttributes();
+}
+
+void wxGrid::ClearAttrCache()
+{
+ if ( m_attrCache.row != -1 )
+ {
+ wxGridCellAttr *oldAttr = m_attrCache.attr;
+ m_attrCache.attr = NULL;
+ m_attrCache.row = -1;
+ // wxSafeDecRec(...) might cause event processing that accesses
+ // the cached attribute, if one exists (e.g. by deleting the
+ // editor stored within the attribute). Therefore it is important
+ // to invalidate the cache before calling wxSafeDecRef!
+ wxSafeDecRef(oldAttr);
+ }
+}
+
+void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
+{
+ if ( attr != NULL )
+ {
+ wxGrid *self = (wxGrid *)this; // const_cast
+
+ self->ClearAttrCache();
+ self->m_attrCache.row = row;
+ self->m_attrCache.col = col;
+ self->m_attrCache.attr = attr;
+ wxSafeIncRef(attr);
+ }
+}
+
+bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
+{
+ if ( row == m_attrCache.row && col == m_attrCache.col )
+ {
+ *attr = m_attrCache.attr;
+ wxSafeIncRef(m_attrCache.attr);
+
+#ifdef DEBUG_ATTR_CACHE
+ gs_nAttrCacheHits++;
+#endif
+
+ return true;
+ }
+ else
+ {
+#ifdef DEBUG_ATTR_CACHE
+ gs_nAttrCacheMisses++;
+#endif
+
+ return false;
+ }
+}
+
+wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
+{
+ wxGridCellAttr *attr = NULL;
+ // Additional test to avoid looking at the cache e.g. for
+ // wxNoCellCoords, as this will confuse memory management.
+ if ( row >= 0 )
+ {
+ if ( !LookupAttr(row, col, &attr) )
+ {
+ attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
+ : NULL;
+ CacheAttr(row, col, attr);
+ }
+ }
+
+ if (attr)
+ {
+ attr->SetDefAttr(m_defaultCellAttr);
+ }
+ else
+ {
+ attr = m_defaultCellAttr;
+ attr->IncRef();
+ }
+
+ return attr;
+}
+
+wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
+{
+ wxGridCellAttr *attr = NULL;
+ bool canHave = ((wxGrid*)this)->CanHaveAttributes();
+
+ wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed"));
+ wxCHECK_MSG( m_table, attr, wxT("must have a table") );
+
+ attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
+ if ( !attr )
+ {
+ attr = new wxGridCellAttr(m_defaultCellAttr);
+
+ // artificially inc the ref count to match DecRef() in caller
+ attr->IncRef();
+ m_table->SetAttr(attr, row, col);
+ }
+
+ return attr;
+}
+
+// ----------------------------------------------------------------------------
+// setting column attributes (wrappers around SetColAttr)
+// ----------------------------------------------------------------------------
+
+void wxGrid::SetColFormatBool(int col)
+{
+ SetColFormatCustom(col, wxGRID_VALUE_BOOL);
+}
+
+void wxGrid::SetColFormatNumber(int col)
+{
+ SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
+}
+
+void wxGrid::SetColFormatFloat(int col, int width, int precision)
+{
+ wxString typeName = wxGRID_VALUE_FLOAT;
+ if ( (width != -1) || (precision != -1) )
+ {
+ typeName << wxT(':') << width << wxT(',') << precision;
+ }
+
+ SetColFormatCustom(col, typeName);
+}
+
+void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
+{
+ wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
+ if (!attr)
+ attr = new wxGridCellAttr;
+ wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
+ attr->SetRenderer(renderer);
+ wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
+ attr->SetEditor(editor);
+
+ SetColAttr(col, attr);
+
+}
+
+// ----------------------------------------------------------------------------
+// setting cell attributes: this is forwarded to the table
+// ----------------------------------------------------------------------------
+
+void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
+{
+ if ( CanHaveAttributes() )
+ {
+ m_table->SetAttr(attr, row, col);
+ ClearAttrCache();
+ }
+ else
+ {
+ wxSafeDecRef(attr);
+ }
+}
+
+void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
+{
+ if ( CanHaveAttributes() )
+ {
+ m_table->SetRowAttr(attr, row);
+ ClearAttrCache();
+ }
+ else
+ {
+ wxSafeDecRef(attr);
+ }
+}
+
+void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
+{
+ if ( CanHaveAttributes() )
+ {
+ m_table->SetColAttr(attr, col);
+ ClearAttrCache();
+ }
+ else
+ {
+ wxSafeDecRef(attr);
+ }
+}
+
+void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetBackgroundColour(colour);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetTextColour(colour);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellFont( int row, int col, const wxFont& font )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetFont(font);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetAlignment(horiz, vert);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellOverflow( int row, int col, bool allow )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetOverflow(allow);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
+{
+ if ( CanHaveAttributes() )
+ {
+ int cell_rows, cell_cols;
+
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->GetSize(&cell_rows, &cell_cols);
+ attr->SetSize(num_rows, num_cols);
+ attr->DecRef();
+
+ // Cannot set the size of a cell to 0 or negative values
+ // While it is perfectly legal to do that, this function cannot
+ // handle all the possibilies, do it by hand by getting the CellAttr.
+ // You can only set the size of a cell to 1,1 or greater with this fn
+ wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
+ wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
+ wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
+ wxT("wxGrid::SetCellSize setting cell size to < 1"));
+
+ // if this was already a multicell then "turn off" the other cells first
+ if ((cell_rows > 1) || (cell_cols > 1))
+ {
+ int i, j;
+ for (j=row; j < row + cell_rows; j++)
+ {
+ for (i=col; i < col + cell_cols; i++)
+ {
+ if ((i != col) || (j != row))
+ {
+ wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
+ attr_stub->SetSize( 1, 1 );
+ attr_stub->DecRef();
+ }
+ }
+ }
+ }
+
+ // mark the cells that will be covered by this cell to
+ // negative or zero values to point back at this cell
+ if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
+ {
+ int i, j;
+ for (j=row; j < row + num_rows; j++)
+ {
+ for (i=col; i < col + num_cols; i++)
+ {
+ if ((i != col) || (j != row))
+ {
+ wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
+ attr_stub->SetSize( row - j, col - i );
+ attr_stub->DecRef();
+ }
+ }
+ }
+ }
+ }
+}
+
+void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetRenderer(renderer);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetEditor(editor);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
+ attr->SetReadOnly(isReadOnly);
+ attr->DecRef();
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Data type registration
+// ----------------------------------------------------------------------------
+
+void wxGrid::RegisterDataType(const wxString& typeName,
+ wxGridCellRenderer* renderer,
+ wxGridCellEditor* editor)
+{
+ m_typeRegistry->RegisterDataType(typeName, renderer, editor);
+}
+
+
+wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
+{
+ wxString typeName = m_table->GetTypeName(row, col);
+ return GetDefaultEditorForType(typeName);
+}
+
+wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
+{
+ wxString typeName = m_table->GetTypeName(row, col);
+ return GetDefaultRendererForType(typeName);
+}
+
+wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
+{
+ int index = m_typeRegistry->FindOrCloneDataType(typeName);
+ if ( index == wxNOT_FOUND )
+ {
+ wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
+
+ return NULL;
+ }
+
+ return m_typeRegistry->GetEditor(index);
+}
+
+wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
+{
+ int index = m_typeRegistry->FindOrCloneDataType(typeName);
+ if ( index == wxNOT_FOUND )
+ {
+ wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
+
+ return NULL;
+ }
+
+ return m_typeRegistry->GetRenderer(index);
+}
+
+// ----------------------------------------------------------------------------
+// row/col size
+// ----------------------------------------------------------------------------
+
+void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed)
+{
+ if ( !setFixed )
+ {
+ setFixed = new wxGridFixedIndicesSet;
+ }
+
+ setFixed->insert(line);
+}
+
+bool
+wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const
+{
+ return !setFixed || !setFixed->count(line);
+}
+
+void wxGrid::EnableDragRowSize( bool enable )
+{
+ m_canDragRowSize = enable;
+}
+
+void wxGrid::EnableDragColSize( bool enable )
+{
+ m_canDragColSize = enable;
+}
+
+void wxGrid::EnableDragGridSize( bool enable )
+{
+ m_canDragGridSize = enable;
+}
+
+void wxGrid::EnableDragCell( bool enable )
+{
+ m_canDragCell = enable;
+}
+
+void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
+{
+ m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
+
+ if ( resizeExistingRows )
+ {
+ // since we are resizing all rows to the default row size,
+ // we can simply clear the row heights and row bottoms
+ // arrays (which also allows us to take advantage of
+ // some speed optimisations)
+ m_rowHeights.Empty();
+ m_rowBottoms.Empty();
+ if ( !GetBatchCount() )
+ CalcDimensions();
+ }
+}
+
+void wxGrid::SetRowSize( int row, int height )
+{
+ wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") );
+
+ // if < 0 then calculate new height from label
+ if ( height < 0 )
+ {
+ long w, h;
+ wxArrayString lines;
+ wxClientDC dc(m_rowLabelWin);
+ dc.SetFont(GetLabelFont());
+ StringToLines(GetRowLabelValue( row ), lines);
+ GetTextBoxSize( dc, lines, &w, &h );
+ //check that it is not less than the minimal height
+ height = wxMax(h, GetRowMinimalAcceptableHeight());
+ }
+
+ // See comment in SetColSize
+ if ( height < GetRowMinimalAcceptableHeight())
+ return;
+
+ if ( m_rowHeights.IsEmpty() )
+ {
+ // need to really create the array
+ InitRowHeights();
+ }
+
+ int h = wxMax( 0, height );
+ int diff = h - m_rowHeights[row];
+
+ m_rowHeights[row] = h;
+ for ( int i = row; i < m_numRows; i++ )
+ {
+ m_rowBottoms[i] += diff;
+ }
+
+ if ( !GetBatchCount() )
+ CalcDimensions();
+}
+
+void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
+{
+ // we dont allow zero default column width
+ m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
+
+ if ( resizeExistingCols )
+ {
+ // since we are resizing all columns to the default column size,
+ // we can simply clear the col widths and col rights
+ // arrays (which also allows us to take advantage of
+ // some speed optimisations)
+ m_colWidths.Empty();
+ m_colRights.Empty();
+ if ( !GetBatchCount() )
+ CalcDimensions();
+ }
+}
+
+void wxGrid::SetColSize( int col, int width )
+{
+ wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") );
+
+ // if < 0 then calculate new width from label
+ if ( width < 0 )
+ {
+ long w, h;
+ wxArrayString lines;
+ wxClientDC dc(m_colWindow);
+ dc.SetFont(GetLabelFont());
+ StringToLines(GetColLabelValue(col), lines);
+ if ( GetColLabelTextOrientation() == wxHORIZONTAL )
+ GetTextBoxSize( dc, lines, &w, &h );
+ else
+ GetTextBoxSize( dc, lines, &h, &w );
+ width = w + 6;
+ //check that it is not less than the minimal width
+ width = wxMax(width, GetColMinimalAcceptableWidth());
+ }
+
+ // we intentionally don't test whether the width is less than
+ // GetColMinimalWidth() here but we do compare it with
+ // GetColMinimalAcceptableWidth() as otherwise things currently break (see
+ // #651) -- and we also always allow the width of 0 as it has the special
+ // sense of hiding the column
+ if ( width > 0 && width < GetColMinimalAcceptableWidth() )
+ return;
+
+ if ( m_colWidths.IsEmpty() )
+ {
+ // need to really create the array
+ InitColWidths();
+ }
+
+ const int diff = width - m_colWidths[col];
+ m_colWidths[col] = width;
+ if ( m_useNativeHeader )
+ GetGridColHeader()->UpdateColumn(col);
+ //else: will be refreshed when the header is redrawn
+
+ for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
+ {
+ m_colRights[GetColAt(colPos)] += diff;
+ }
+
+ if ( !GetBatchCount() )
+ {
+ CalcDimensions();
+ Refresh();
+ }
+}
+
+void wxGrid::SetColMinimalWidth( int col, int width )
+{
+ if (width > GetColMinimalAcceptableWidth())
+ {
+ wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
+ m_colMinWidths[key] = width;
+ }
+}
+
+void wxGrid::SetRowMinimalHeight( int row, int width )
+{
+ if (width > GetRowMinimalAcceptableHeight())
+ {
+ wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
+ m_rowMinHeights[key] = width;
+ }