From: Vadim Zeitlin Date: Mon, 28 Feb 2000 16:40:14 +0000 (+0000) Subject: grid autosize fixes/changes X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/266e83674a53ffcbfff9c05cb22ae5d40c4d5f96 grid autosize fixes/changes git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6334 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 06345817af..dcbd638f76 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -1078,9 +1078,11 @@ public: void AutoSizeColumn( int col, bool setAsMin = TRUE ); // auto size all columns (very ineffective for big grids!) - void AutoSizeColumns( bool setAsMin = TRUE ); + void AutoSizeColumns( bool setAsMin = TRUE ) + { (void)SetOrCalcColumnSizes(TRUE, setAsMin); } - void AutoSizeRows( bool setAsMin = TRUE ); + void AutoSizeRows( bool setAsMin = TRUE ) + { (void)SetOrCalcRowSizes(TRUE, setAsMin); } // auto size the grid, that is make the columns/rows of the "right" size // and also set the grid size to just fit its contents @@ -1222,7 +1224,13 @@ public: wxGridCellEditor* GetDefaultEditorForType(const wxString& typeName) const; wxGridCellRenderer* GetDefaultRendererForType(const wxString& typeName) const; - + // grid may occupy more space than needed for its rows/columns, this + // function allows to set how big this extra space is + void SetMargins(int extraWidth, int extraHeight) + { + m_extraWidth = extraWidth; + m_extraHeight = extraHeight; + } // ------ For compatibility with previous wxGrid only... // @@ -1384,7 +1392,12 @@ public: wxGRID_CHOICE, wxGRID_COMBOBOX }; + // overridden wxWindow methods + virtual void Fit(); + protected: + virtual wxSize DoGetBestSize() const; + bool m_created; bool m_displayed; @@ -1444,6 +1457,10 @@ protected: int m_rowLabelWidth; int m_colLabelHeight; + // the size of the margin left to the right and bottom of the cell area + int m_extraWidth, + m_extraHeight; + wxColour m_labelBackgroundColour; wxColour m_labelTextColour; wxFont m_labelFont; @@ -1459,6 +1476,10 @@ protected: wxColour m_gridLineColour; bool m_gridLinesEnabled; + // common part of AutoSizeColumn/Row() and GetBestSize() + int SetOrCalcColumnSizes(bool calcOnly, bool setAsMin = TRUE); + int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = TRUE); + // if a column has a minimal width, it will be the value for it in this // hash table wxHashTable m_colMinWidths; diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index 739152eb63..5095fe7809 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -756,10 +756,20 @@ static const wxChar* severities[] = static struct BugsGridData { int id; - const wxChar *summary; +#ifndef __BORLANDC__ + wxString +#else + const wxChar * +#endif + summary; Severity severity; int prio; - const wxChar *platform; +#ifndef __BORLANDC__ + wxString +#else + const wxChar * +#endif + platform; bool opened; } gs_dataBugsGrid [] = { @@ -1003,7 +1013,8 @@ BugsGridFrame::BugsGridFrame() grid->SetColAttr(Col_Priority, attrRangeEditor); grid->SetColAttr(Col_Severity, attrCombo); - grid->AutoSize(); + grid->SetMargins(0, 0); + grid->Fit(); Fit(); } diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 9bf212b8e0..00f2271e51 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -2944,6 +2944,9 @@ void wxGrid::Init() m_inOnKeyDown = FALSE; m_batchCount = 0; + m_extraWidth = + m_extraHeight = 50; + CalcDimensions(); } @@ -3031,8 +3034,8 @@ void wxGrid::CalcDimensions() if ( m_numRows > 0 && m_numCols > 0 ) { - int right = GetColRight( m_numCols-1 ) + 50; - int bottom = GetRowBottom( m_numRows-1 ) + 50; + int right = GetColRight( m_numCols-1 ) + m_extraWidth; + int bottom = GetRowBottom( m_numRows-1 ) + m_extraHeight; // TODO: restore the scroll position that we had before sizing // @@ -6903,39 +6906,55 @@ void wxGrid::AutoSizeColumn( int col, bool setAsMin ) } } -void wxGrid::AutoSizeColumns( bool setAsMin ) +int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) { int width = m_rowLabelWidth; for ( int col = 0; col < m_numCols; col++ ) { - AutoSizeColumn(col, setAsMin); + if ( !calcOnly ) + { + AutoSizeColumn(col, setAsMin); + } width += GetColWidth(col); } - // also set the grid size to just fit the columns - SetSize(width, -1); + return width; } -void wxGrid::AutoSizeRows(bool WXUNUSED(setAsMin)) +int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) { int height = m_colLabelHeight; for ( int row = 0; row < m_numRows; row++ ) { - // AutoSizeRow(row, setAsMin) -- TODO + // if ( !calcOnly ) AutoSizeRow(row, setAsMin) -- TODO height += GetRowHeight(row); } - SetSize(-1, height); + return height; } void wxGrid::AutoSize() { - AutoSizeColumns(); - AutoSizeRows(); + // set the size too + SetSize(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE)); +} + +wxSize wxGrid::DoGetBestSize() const +{ + // don't set sizes, only calculate them + wxGrid *self = (wxGrid *)this; // const_cast + + return wxSize(self->SetOrCalcColumnSizes(TRUE), + self->SetOrCalcRowSizes(TRUE)); +} + +void wxGrid::Fit() +{ + AutoSize(); } // ----------------------------------------------------------------------------