X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4e4e06e3497733c5d5726656835b5a4127089f99..ab9df4fb452e785836de38712af84d209113b5af:/src/common/sizer.cpp diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index c4502c852e..a4c17f8b8c 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -1333,18 +1333,50 @@ wxGridSizer::wxGridSizer( int cols, int vgap, int hgap ) { } +wxSizerItem *wxGridSizer::Insert(size_t index, wxSizerItem *item) +{ + // if only the number of columns or the number of rows is specified for a + // sizer, arbitrarily many items can be added to it but if both of them are + // fixed, then the sizer can't have more than that many items -- check for + // this here to ensure that we detect errors as soon as possible + if ( m_cols && m_rows ) + { + const int nitems = m_children.GetCount(); + if ( nitems == m_cols*m_rows ) + { + wxFAIL_MSG( + wxString::Format( + "too many items (%d > %d*%d) in grid sizer (maybe you " + "should omit the number of either rows or columns?)", + nitems + 1, m_cols, m_rows) + ); + + // additionally, continuing to use the specified number of columns + // and rows is not a good idea as callers of CalcRowsCols() expect + // that all sizer items can fit into m_cols/m_rows-sized arrays + // which is not the case if there are too many items and results in + // crashes, so let it compute the number of rows automatically by + // forgetting the (wrong) number of rows specified (this also has a + // nice side effect of giving only one assert even if there are + // many more items than allowed in this sizer) + m_rows = 0; + } + } + + return wxSizer::Insert(index, item); +} + int wxGridSizer::CalcRowsCols(int& nrows, int& ncols) const { const int nitems = m_children.GetCount(); if ( m_cols && m_rows ) { - // if both rows and columns are specified by user, use the provided - // values even if we don't have enough items but check that we don't - // have too many of them as this is going to result in problems later ncols = m_cols; nrows = m_rows; - wxASSERT_MSG( ncols*nrows >= nitems, "too many items in grid sizer" ); + // this should be impossible because the too high number of items + // should have been detected by Insert() above + wxASSERT_MSG( nitems <= ncols*nrows, "logic error in wxGridSizer" ); } else if ( m_cols ) { @@ -2119,19 +2151,10 @@ wxStaticBoxSizer::~wxStaticBoxSizer() delete m_staticBox; } -static void GetStaticBoxBorders( wxStaticBox *box, - int *borderTop, - int *borderOther) -{ - // this has to be done platform by platform as there is no way to - // guess the thickness of a wxStaticBox border - box->GetBordersForSizer(borderTop, borderOther); -} - void wxStaticBoxSizer::RecalcSizes() { int top_border, other_border; - GetStaticBoxBorders(m_staticBox, &top_border, &other_border); + m_staticBox->GetBordersForSizer(&top_border, &other_border); m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); @@ -2151,10 +2174,17 @@ void wxStaticBoxSizer::RecalcSizes() wxSize wxStaticBoxSizer::CalcMin() { int top_border, other_border; - GetStaticBoxBorders(m_staticBox, &top_border, &other_border); + m_staticBox->GetBordersForSizer(&top_border, &other_border); wxSize ret( wxBoxSizer::CalcMin() ); ret.x += 2*other_border; + + // ensure that we're wide enough to show the static box label (there is no + // need to check for the static box best size in vertical direction though) + const int boxWidth = m_staticBox->GetBestSize().x; + if ( ret.x < boxWidth ) + ret.x = boxWidth; + ret.y += other_border + top_border; return ret;