+
+ if ( !GetBatchCount() )
+ CalcDimensions();
+}
+
+void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
+{
+ m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
+
+ 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, _T("invalid column index") );
+
+ // should we check that it's bigger than GetColMinimalWidth(col) here?
+ // (VZ)
+ // No, because it is reasonable to assume the library user know's
+ // what he is doing. However we should test against the weaker
+ // constraint of minimalAcceptableWidth, as this breaks rendering
+ //
+ // This test then fixes sf.net bug #645734
+
+ if ( width < GetColMinimalAcceptableWidth() )
+ return;
+
+ if ( m_colWidths.IsEmpty() )
+ {
+ // need to really create the array
+ InitColWidths();
+ }
+
+ // if < 0 then calculate new width from label
+ if ( width < 0 )
+ {
+ long w, h;
+ wxArrayString lines;
+ wxClientDC dc(m_colLabelWin);
+ dc.SetFont(GetLabelFont());
+ StringToLines(GetColLabelValue(col), lines);
+ GetTextBoxSize(dc, lines, &w, &h);
+ width = w + 6;
+ }
+
+ int w = wxMax( 0, width );
+ int diff = w - m_colWidths[col];
+ m_colWidths[col] = w;
+
+ int i;
+ int colPos;
+ for ( colPos = GetColPos( col ); colPos < m_numCols; colPos++ )
+ {
+ i = GetColAt( colPos );
+ m_colRights[i] += diff;
+ }
+
+ if ( !GetBatchCount() )
+ CalcDimensions();
+}
+
+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;
+ }
+}
+
+int wxGrid::GetColMinimalWidth(int col) const
+{
+ wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
+ wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
+
+ return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
+}
+
+int wxGrid::GetRowMinimalHeight(int row) const
+{
+ wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
+ wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
+
+ return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
+}
+
+void wxGrid::SetColMinimalAcceptableWidth( int width )
+{
+ // We do allow a width of 0 since this gives us
+ // an easy way to temporarily hiding columns.
+ if ( width >= 0 )
+ m_minAcceptableColWidth = width;
+}
+
+void wxGrid::SetRowMinimalAcceptableHeight( int height )
+{
+ // We do allow a height of 0 since this gives us
+ // an easy way to temporarily hiding rows.
+ if ( height >= 0 )
+ m_minAcceptableRowHeight = height;
+}
+
+int wxGrid::GetColMinimalAcceptableWidth() const
+{
+ return m_minAcceptableColWidth;
+}
+
+int wxGrid::GetRowMinimalAcceptableHeight() const
+{
+ return m_minAcceptableRowHeight;
+}
+
+// ----------------------------------------------------------------------------
+// auto sizing
+// ----------------------------------------------------------------------------
+
+void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
+{
+ wxClientDC dc(m_gridWin);
+
+ // cancel editing of cell
+ HideCellEditControl();
+ SaveEditControlValue();
+
+ // init both of them to avoid compiler warnings, even if we only need one
+ int row = -1,
+ col = -1;
+ if ( column )
+ col = colOrRow;
+ else
+ row = colOrRow;
+
+ wxCoord extent, extentMax = 0;
+ int max = column ? m_numRows : m_numCols;
+ for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
+ {
+ if ( column )
+ row = rowOrCol;
+ else
+ col = rowOrCol;
+
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
+ if ( renderer )
+ {
+ wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
+ extent = column ? size.x : size.y;
+ if ( extent > extentMax )
+ extentMax = extent;
+
+ renderer->DecRef();
+ }
+
+ attr->DecRef();
+ }
+
+ // now also compare with the column label extent
+ wxCoord w, h;
+ dc.SetFont( GetLabelFont() );
+
+ if ( column )
+ {
+ dc.GetTextExtent( GetColLabelValue(col), &w, &h );
+ if ( GetColLabelTextOrientation() == wxVERTICAL )
+ w = h;
+ }
+ else
+ dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
+
+ extent = column ? w : h;
+ if ( extent > extentMax )
+ extentMax = extent;
+
+ if ( !extentMax )
+ {
+ // empty column - give default extent (notice that if extentMax is less
+ // than default extent but != 0, it's OK)
+ extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
+ }
+ else
+ {
+ if ( column )
+ // leave some space around text
+ extentMax += 10;
+ else
+ extentMax += 6;
+ }
+
+ if ( column )
+ {
+ SetColSize( col, extentMax );
+ if ( !GetBatchCount() )
+ {
+ int cw, ch, dummy;
+ m_gridWin->GetClientSize( &cw, &ch );
+ wxRect rect ( CellToRect( 0, col ) );
+ rect.y = 0;
+ CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
+ rect.width = cw - rect.x;
+ rect.height = m_colLabelHeight;
+ m_colLabelWin->Refresh( true, &rect );
+ }
+ }
+ else
+ {
+ SetRowSize(row, extentMax);
+ if ( !GetBatchCount() )
+ {
+ int cw, ch, dummy;
+ m_gridWin->GetClientSize( &cw, &ch );
+ wxRect rect( CellToRect( row, 0 ) );
+ rect.x = 0;
+ CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
+ rect.width = m_rowLabelWidth;
+ rect.height = ch - rect.y;
+ m_rowLabelWin->Refresh( true, &rect );
+ }
+ }
+
+ if ( setAsMin )
+ {
+ if ( column )
+ SetColMinimalWidth(col, extentMax);
+ else
+ SetRowMinimalHeight(row, extentMax);
+ }
+}
+
+int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
+{
+ int width = m_rowLabelWidth;
+
+ if ( !calcOnly )
+ BeginBatch();
+
+ for ( int col = 0; col < m_numCols; col++ )
+ {
+ if ( !calcOnly )
+ AutoSizeColumn(col, setAsMin);
+
+ width += GetColWidth(col);
+ }
+
+ if ( !calcOnly )
+ EndBatch();
+
+ return width;
+}
+
+int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
+{
+ int height = m_colLabelHeight;
+
+ if ( !calcOnly )
+ BeginBatch();
+
+ for ( int row = 0; row < m_numRows; row++ )
+ {
+ if ( !calcOnly )
+ AutoSizeRow(row, setAsMin);
+
+ height += GetRowHeight(row);
+ }
+
+ if ( !calcOnly )
+ EndBatch();
+
+ return height;
+}
+
+void wxGrid::AutoSize()
+{
+ BeginBatch();
+
+ wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
+
+ // round up the size to a multiple of scroll step - this ensures that we
+ // won't get the scrollbars if we're sized exactly to this width
+ // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
+ // scrollbar steps
+ wxSize sizeFit(
+ GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX,
+ GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY );
+
+ // distribute the extra space between the columns/rows to avoid having
+ // extra white space
+
+ // Remove the extra m_extraWidth + 1 added above
+ wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
+ if ( diff && m_numCols )
+ {
+ // try to resize the columns uniformly
+ wxCoord diffPerCol = diff / m_numCols;
+ if ( diffPerCol )
+ {
+ for ( int col = 0; col < m_numCols; col++ )
+ {
+ SetColSize(col, GetColWidth(col) + diffPerCol);
+ }
+ }
+
+ // add remaining amount to the last columns
+ diff -= diffPerCol * m_numCols;
+ if ( diff )
+ {
+ for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
+ {
+ SetColSize(col, GetColWidth(col) + 1);
+ }
+ }
+ }
+
+ // same for rows
+ diff = sizeFit.y - size.y - (m_extraHeight + 1);
+ if ( diff && m_numRows )
+ {
+ // try to resize the columns uniformly
+ wxCoord diffPerRow = diff / m_numRows;
+ if ( diffPerRow )
+ {
+ for ( int row = 0; row < m_numRows; row++ )
+ {
+ SetRowSize(row, GetRowHeight(row) + diffPerRow);
+ }
+ }
+
+ // add remaining amount to the last rows
+ diff -= diffPerRow * m_numRows;
+ if ( diff )
+ {
+ for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
+ {
+ SetRowSize(row, GetRowHeight(row) + 1);
+ }
+ }
+ }
+
+ EndBatch();
+
+ SetClientSize(sizeFit);
+}
+
+void wxGrid::AutoSizeRowLabelSize( int row )
+{
+ wxArrayString lines;
+ long w, h;
+
+ // Hide the edit control, so it
+ // won't interfere with drag-shrinking.
+ if ( IsCellEditControlShown() )
+ {
+ HideCellEditControl();
+ SaveEditControlValue();
+ }
+
+ // autosize row height depending on label text
+ StringToLines( GetRowLabelValue( row ), lines );
+ wxClientDC dc( m_rowLabelWin );
+ GetTextBoxSize( dc, lines, &w, &h );
+ if ( h < m_defaultRowHeight )
+ h = m_defaultRowHeight;
+ SetRowSize(row, h);
+ ForceRefresh();
+}
+
+void wxGrid::AutoSizeColLabelSize( int col )
+{
+ wxArrayString lines;
+ long w, h;
+
+ // Hide the edit control, so it
+ // won't interfere with drag-shrinking.
+ if ( IsCellEditControlShown() )
+ {
+ HideCellEditControl();
+ SaveEditControlValue();
+ }
+
+ // autosize column width depending on label text
+ StringToLines( GetColLabelValue( col ), lines );
+ wxClientDC dc( m_colLabelWin );
+ if ( GetColLabelTextOrientation() == wxHORIZONTAL )
+ GetTextBoxSize( dc, lines, &w, &h );
+ else
+ GetTextBoxSize( dc, lines, &h, &w );
+ if ( w < m_defaultColWidth )
+ w = m_defaultColWidth;
+ SetColSize(col, w);
+ ForceRefresh();