diff --git a/docs/changes.txt b/docs/changes.txt index 155f872e8d..9fff4549f4 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -94,6 +94,7 @@ Major new features in 2.8 release All (GUI): - Added wxWindow::GetNextSibling() and GetPrevSibling() +- Support wxGRID_AUTOSIZE in wxGrid::SetRow/ColLabelSize() (Evgeniy Tarassov) All (Unix): diff --git a/docs/latex/wx/grid.tex b/docs/latex/wx/grid.tex index 22d35e8019..87a6b6caad 100644 --- a/docs/latex/wx/grid.tex +++ b/docs/latex/wx/grid.tex @@ -1596,6 +1596,10 @@ Vertical alignment should be one of wxALIGN\_TOP, wxALIGN\_CENTRE or wxALIGN\_BO Sets the height of the column labels. +If \arg{height} equals to \textt{wxGRID\_AUTOSIZE} then height is calculated +automatically so that no label is truncated. Note that this could be slow for a +large table. This flag is new since wxWidgets version 2.8.8. + \membersection{wxGrid::SetColLabelValue}\label{wxgridsetcollabelvalue} @@ -1835,6 +1839,10 @@ Vertical alignment should be one of wxALIGN\_TOP, wxALIGN\_CENTRE or wxALIGN\_BO Sets the width of the row labels. +If \arg{width} equals \textt{wxGRID\_AUTOSIZE} then width is calculated +automatically so that no label is truncated. Note that this could be slow for a +large table. This flag is new since wxWidgets version 2.8.8. + \membersection{wxGrid::SetRowLabelValue}\label{wxgridsetrowlabelvalue} diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index a08efaa371..e015b7c3e8 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -51,6 +51,12 @@ extern WXDLLIMPEXP_DATA_ADV(const wxChar) wxGridNameStr[]; #define wxGRID_VALUE_TEXT wxGRID_VALUE_STRING #define wxGRID_VALUE_LONG wxGRID_VALUE_NUMBER +#if wxABI_VERSION >= 20808 + // magic constant which tells (to some functions) to automatically + // calculate the appropriate size + #define wxGRID_AUTOSIZE (-1) +#endif // wxABI_VERSION >= 20808 + // ---------------------------------------------------------------------------- // forward declarations // ---------------------------------------------------------------------------- @@ -1996,6 +2002,10 @@ protected: bool GetModelValues(); bool SetModelValues(); +private: + // Calculate the minimum acceptable size for labels area + wxCoord CalcColOrRowLabelAreaMinSize(bool column /* or row? */); + friend class WXDLLIMPEXP_ADV wxGridSelection; DECLARE_DYNAMIC_CLASS( wxGrid ) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 251860415a..76af28f3df 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -9374,7 +9374,13 @@ wxString wxGrid::GetColLabelValue( int col ) void wxGrid::SetRowLabelSize( int width ) { - width = wxMax( width, 0 ); + wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE ); + + if ( width == wxGRID_AUTOSIZE ) + { + width = CalcColOrRowLabelAreaMinSize(false/*row*/); + } + if ( width != m_rowLabelWidth ) { if ( width == 0 ) @@ -9397,7 +9403,13 @@ void wxGrid::SetRowLabelSize( int width ) void wxGrid::SetColLabelSize( int height ) { - height = wxMax( height, 0 ); + wxASSERT( height >=0 || height == wxGRID_AUTOSIZE ); + + if ( height == wxGRID_AUTOSIZE ) + { + height = CalcColOrRowLabelAreaMinSize(true/*column*/); + } + if ( height != m_colLabelHeight ) { if ( height == 0 ) @@ -10555,6 +10567,58 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) } } +wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(bool column) +{ + // calculate size for the rows or columns? + const bool calcRows = !column; + + wxClientDC dc(calcRows ? GetGridRowLabelWindow() + : GetGridColLabelWindow()); + dc.SetFont(GetLabelFont()); + + // which dimension should we take into account for calculations? + // + // for columns, the text can be only horizontal so it's easy but for rows + // we also have to take into account the text orientation + const bool + useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL); + + wxArrayString lines; + wxCoord extentMax = 0; + + const int numRowsOrCols = calcRows ? m_numRows : m_numCols; + for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ ) + { + lines.Clear(); + StringToLines(calcRows ? GetRowLabelValue(rowOrCol) + : GetColLabelValue(rowOrCol), + lines); + + long w, h; + GetTextBoxSize(dc, lines, &w, &h); + + const wxCoord extent = useWidth ? 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 = calcRows ? GetDefaultRowLabelSize() + : GetDefaultColLabelSize(); + } + + // leave some space around text (taken from AutoSizeColOrRow) + if ( calcRows ) + extentMax += 10; + else + extentMax += 6; + + return extentMax; +} + int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) { int width = m_rowLabelWidth;