1. added range checks in wxGridStringTable

2. added AutoSizeRow(), setting minimal size for the rows is supported as
   well now
3. replaced ugly wxHashTables with (just created) wxHashTableLong


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6355 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-02-29 17:21:20 +00:00
parent c2bb85e9e3
commit af547d51b7
2 changed files with 88 additions and 37 deletions

View File

@@ -1079,10 +1079,13 @@ public:
void SetColSize( int col, int width ); void SetColSize( int col, int width );
// automatically size the column to fit to its contents, if setAsMin is // automatically size the column or row to fit to its contents, if
// TRUE, this optimal width will also be set as minimal width for this // setAsMin is TRUE, this optimal width will also be set as minimal width
// column // for this column
void AutoSizeColumn( int col, bool setAsMin = TRUE ); void AutoSizeColumn( int col, bool setAsMin = TRUE )
{ AutoSizeColOrRow(col, setAsMin, TRUE); }
void AutoSizeRow( int row, bool setAsMin = TRUE )
{ AutoSizeColOrRow(row, setAsMin, FALSE); }
// auto size all columns (very ineffective for big grids!) // auto size all columns (very ineffective for big grids!)
void AutoSizeColumns( bool setAsMin = TRUE ) void AutoSizeColumns( bool setAsMin = TRUE )
@@ -1099,6 +1102,7 @@ public:
// the grid creation because it won't resize the column if it's already // the grid creation because it won't resize the column if it's already
// narrower than the minimal width // narrower than the minimal width
void SetColMinimalWidth( int col, int width ); void SetColMinimalWidth( int col, int width );
void SetRowMinimalHeight( int row, int width );
void SetDefaultCellBackgroundColour( const wxColour& ); void SetDefaultCellBackgroundColour( const wxColour& );
void SetCellBackgroundColour( int row, int col, const wxColour& ); void SetCellBackgroundColour( int row, int col, const wxColour& );
@@ -1488,12 +1492,17 @@ protected:
int SetOrCalcColumnSizes(bool calcOnly, bool setAsMin = TRUE); int SetOrCalcColumnSizes(bool calcOnly, bool setAsMin = TRUE);
int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = TRUE); int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = TRUE);
// common part of AutoSizeColumn/Row()
void AutoSizeColOrRow(int n, bool setAsMin, bool column /* or row? */);
// if a column has a minimal width, it will be the value for it in this // if a column has a minimal width, it will be the value for it in this
// hash table // hash table
wxHashTable m_colMinWidths; wxHashTableLong m_colMinWidths,
m_rowMinHeights;
// get the minimal width of the given column // get the minimal width of the given column/row
int GetColMinimalWidth(int col) const; int GetColMinimalWidth(int col) const;
int GetRowMinimalHeight(int col) const;
// do we have some place to store attributes in? // do we have some place to store attributes in?
bool CanHaveAttributes(); bool CanHaveAttributes();

View File

@@ -2163,26 +2163,28 @@ long wxGridStringTable::GetNumberCols()
wxString wxGridStringTable::GetValue( int row, int col ) wxString wxGridStringTable::GetValue( int row, int col )
{ {
// TODO: bounds checking wxASSERT_MSG( (row < GetNumberCols()) && (col < GetNumberCols()),
// _T("invalid row or column index in wxGridStringTable") );
return m_data[row][col]; return m_data[row][col];
} }
void wxGridStringTable::SetValue( int row, int col, const wxString& value ) void wxGridStringTable::SetValue( int row, int col, const wxString& value )
{ {
// TODO: bounds checking wxASSERT_MSG( (row < GetNumberCols()) && (col < GetNumberCols()),
// _T("invalid row or column index in wxGridStringTable") );
m_data[row][col] = value; m_data[row][col] = value;
} }
bool wxGridStringTable::IsEmptyCell( int row, int col ) bool wxGridStringTable::IsEmptyCell( int row, int col )
{ {
// TODO: bounds checking wxASSERT_MSG( (row < GetNumberCols()) && (col < GetNumberCols()),
// _T("invalid row or column index in wxGridStringTable") );
return (m_data[row][col] == wxEmptyString); return (m_data[row][col] == wxEmptyString);
} }
void wxGridStringTable::Clear() void wxGridStringTable::Clear()
{ {
int row, col; int row, col;
@@ -2748,7 +2750,8 @@ wxGrid::wxGrid( wxWindow *parent,
long style, long style,
const wxString& name ) const wxString& name )
: wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ), : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
m_colMinWidths(wxKEY_INTEGER, GRID_HASH_SIZE) m_colMinWidths(GRID_HASH_SIZE),
m_rowMinHeights(GRID_HASH_SIZE)
{ {
Create(); Create();
} }
@@ -3474,7 +3477,9 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
wxClientDC dc( m_gridWin ); wxClientDC dc( m_gridWin );
PrepareDC( dc ); PrepareDC( dc );
y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT ); y = wxMax( y,
GetRowTop(m_dragRowOrCol) +
GetRowMinimalHeight(m_dragRowOrCol) );
dc.SetLogicalFunction(wxINVERT); dc.SetLogicalFunction(wxINVERT);
if ( m_dragLastPos >= 0 ) if ( m_dragLastPos >= 0 )
{ {
@@ -6876,64 +6881,98 @@ void wxGrid::SetColSize( int col, int width )
void wxGrid::SetColMinimalWidth( int col, int width ) void wxGrid::SetColMinimalWidth( int col, int width )
{ {
m_colMinWidths.Put(col, (wxObject *)width); m_colMinWidths.Put(col, width);
}
void wxGrid::SetRowMinimalHeight( int row, int width )
{
m_rowMinHeights.Put(row, width);
} }
int wxGrid::GetColMinimalWidth(int col) const int wxGrid::GetColMinimalWidth(int col) const
{ {
wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol); long value = m_colMinWidths.Get(col);
return obj ? (int)obj : WXGRID_MIN_COL_WIDTH; return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
}
int wxGrid::GetRowMinimalHeight(int row) const
{
long value = m_rowMinHeights.Get(row);
return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// auto sizing // auto sizing
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxGrid::AutoSizeColumn( int col, bool setAsMin ) void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
{ {
wxClientDC dc(m_gridWin); wxClientDC dc(m_gridWin);
wxCoord width, widthMax = 0; int row, col;
for ( int row = 0; row < m_numRows; row++ ) if ( column )
col = colOrRow;
else
row = colOrRow;
wxCoord extent, extentMax = 0;
int max = column ? m_numRows : m_numCols;
for ( int rowOrCol = 0; rowOrCol < m_numRows; rowOrCol++ )
{ {
if ( column )
row = rowOrCol;
else
col = rowOrCol;
wxGridCellAttr* attr = GetCellAttr(row, col); wxGridCellAttr* attr = GetCellAttr(row, col);
wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
if ( renderer ) if ( renderer )
{ {
width = renderer->GetBestSize(*this, *attr, dc, row, col).x; wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
if ( width > widthMax ) extent = column ? size.x : size.y;
if ( extent > extentMax )
{ {
widthMax = width; extentMax = extent;
} }
} }
attr->DecRef(); attr->DecRef();
} }
// now also compare with the column label width // now also compare with the column label extent
wxCoord w, h;
dc.SetFont( GetLabelFont() ); dc.SetFont( GetLabelFont() );
dc.GetTextExtent( GetColLabelValue(col), &width, NULL ); dc.GetTextExtent( column ? GetColLabelValue(col)
if ( width > widthMax ) : GetRowLabelValue(row), &w, &h );
extent = column ? w : h;
if ( extent > extentMax )
{ {
widthMax = width; extentMax = extent;
} }
if ( !widthMax ) if ( !extentMax )
{ {
// empty column - give default width (notice that if widthMax is less // empty column - give default extent (notice that if extentMax is less
// than default width but != 0, it's ok) // than default extent but != 0, it's ok)
widthMax = m_defaultColWidth; extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
} }
else else
{ {
// leave some space around text // leave some space around text
widthMax += 10; extentMax += 10;
} }
SetColSize(col, widthMax); if ( column )
SetColSize(col, extentMax);
else
SetRowSize(col, extentMax);
if ( setAsMin ) if ( setAsMin )
{ {
SetColMinimalWidth(col, widthMax); if ( column )
SetColMinimalWidth(col, extentMax);
else
SetRowMinimalHeight(col, extentMax);
} }
} }
@@ -6960,7 +6999,10 @@ int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
for ( int row = 0; row < m_numRows; row++ ) for ( int row = 0; row < m_numRows; row++ )
{ {
// if ( !calcOnly ) AutoSizeRow(row, setAsMin) -- TODO if ( !calcOnly )
{
AutoSizeRow(row, setAsMin);
}
height += GetRowHeight(row); height += GetRowHeight(row);
} }