X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/dc259b792613550edda31cc6202b42e172e2a240..4b1f929cc3421e43409f7d62260bf9ba0ffc75ab:/src/common/sizer.cpp diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index c83a90caa1..5a9c11a99f 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -1,12 +1,12 @@ ///////////////////////////////////////////////////////////////////////////// // Name: sizer.cpp // Purpose: provide new wxSizer class for layout -// Author: Robert Roebling and Robin Dunn +// Author: Robert Roebling and Robin Dunn, contributions by +// Dirk Holtwick, Ron Lee // Modified by: Ron Lee // Created: // RCS-ID: $Id$ -// Copyright: (c) Robin Dunn, Dirk Holtwick and Robert Roebling -// (c) 2003, Ron Lee +// Copyright: (c) Robin Dunn, Robert Roebling // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -56,7 +56,7 @@ wxSizerItem::wxSizerItem( int width, int height, int proportion, int flag, int b , m_proportion( proportion ) , m_border( border ) , m_flag( flag ) - , m_show( TRUE ) + , m_show( true ) , m_userData( userData ) { SetRatio( m_size ); @@ -69,7 +69,7 @@ wxSizerItem::wxSizerItem( wxWindow *window, int proportion, int flag, int border , m_proportion( proportion ) , m_border( border ) , m_flag( flag ) - , m_show( TRUE ) + , m_show( true ) , m_userData( userData ) { // aspect ratio calculated from initial size @@ -84,7 +84,7 @@ wxSizerItem::wxSizerItem( wxSizer *sizer, int proportion, int flag, int border, , m_proportion( proportion ) , m_border( border ) , m_flag( flag ) - , m_show( TRUE ) + , m_show( true ) , m_ratio( 0.0 ) , m_userData( userData ) { @@ -277,7 +277,7 @@ int wxSizerItem::GetOption() const wxSizer::wxSizer() : m_minSize( wxSize( 0, 0 ) ) { - m_children.DeleteContents( TRUE ); + m_children.DeleteContents( true ); } wxSizer::~wxSizer() @@ -396,20 +396,20 @@ bool wxSizer::Remove( wxSizer *sizer ) node = node->GetNext(); } - return FALSE; + return false; } -bool wxSizer::Remove( size_t index ) +bool wxSizer::Remove( int index ) { - wxCHECK_MSG( index < m_children.GetCount(), - FALSE, + wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(), + false, _T("Remove index is out of range") ); - wxSizerItemList::Node *node = m_children.Item( index ); + wxSizerItemList::Node *node = m_children.Item( index ); - wxCHECK_MSG( node, FALSE, _T("Failed to find child node") ); + wxCHECK_MSG( node, false, _T("Failed to find child node") ); - wxSizerItem *item = node->GetData(); + wxSizerItem *item = node->GetData(); if( item->IsWindow() ) item->GetWindow()->SetContainingSizer( NULL ); @@ -434,7 +434,7 @@ bool wxSizer::Detach( wxSizer *sizer ) node = node->GetNext(); } - return FALSE; + return false; } bool wxSizer::Detach( wxWindow *window ) @@ -454,20 +454,20 @@ bool wxSizer::Detach( wxWindow *window ) node = node->GetNext(); } - return FALSE; + return false; } -bool wxSizer::Detach( size_t index ) +bool wxSizer::Detach( int index ) { - wxCHECK_MSG( index < m_children.GetCount(), - FALSE, + wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(), + false, _T("Detach index is out of range") ); - wxSizerItemList::Node *node = m_children.Item( index ); + wxSizerItemList::Node *node = m_children.Item( index ); - wxCHECK_MSG( node, FALSE, _T("Failed to find child node") ); + wxCHECK_MSG( node, false, _T("Failed to find child node") ); - wxSizerItem *item = node->GetData(); + wxSizerItem *item = node->GetData(); if( item->IsSizer() ) item->DetachSizer(); @@ -670,7 +670,7 @@ bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height ) if (item->GetWindow() == window) { item->SetInitSize( width, height ); - return TRUE; + return true; } node = node->GetNext(); } @@ -686,12 +686,12 @@ bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height ) item->GetSizer()->DoSetItemMinSize( window, width, height ) ) { // A child sizer found the requested windw, exit. - return TRUE; + return true; } node = node->GetNext(); } - return FALSE; + return false; } bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height ) @@ -708,7 +708,7 @@ bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height ) if (item->GetSizer() == sizer) { item->GetSizer()->DoSetMinSize( width, height ); - return TRUE; + return true; } node = node->GetNext(); } @@ -724,19 +724,19 @@ bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height ) item->GetSizer()->DoSetItemMinSize( sizer, width, height ) ) { // A child found the requested sizer, exit. - return TRUE; + return true; } node = node->GetNext(); } - return FALSE; + return false; } bool wxSizer::DoSetItemMinSize( size_t index, int width, int height ) { wxSizerItemList::Node *node = m_children.Item( index ); - wxCHECK_MSG( node, FALSE, _T("Failed to find child node") ); + wxCHECK_MSG( node, false, _T("Failed to find child node") ); wxSizerItem *item = node->GetData(); @@ -751,7 +751,7 @@ bool wxSizer::DoSetItemMinSize( size_t index, int width, int height ) item->SetInitSize( width, height ); } - return TRUE; + return true; } void wxSizer::Show( wxWindow *window, bool show ) @@ -824,7 +824,7 @@ bool wxSizer::IsShown( wxWindow *window ) const wxFAIL_MSG( _T("IsShown failed to find sizer item") ); - return FALSE; + return false; } bool wxSizer::IsShown( wxSizer *sizer ) const @@ -843,13 +843,13 @@ bool wxSizer::IsShown( wxSizer *sizer ) const wxFAIL_MSG( _T("IsShown failed to find sizer item") ); - return FALSE; + return false; } bool wxSizer::IsShown( size_t index ) const { wxCHECK_MSG( index < m_children.GetCount(), - FALSE, + false, _T("IsShown index is out of range") ); return m_children.Item( index )->GetData()->IsShown(); @@ -1000,48 +1000,21 @@ void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h ) //--------------------------------------------------------------------------- wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap ) - : wxGridSizer( rows, cols, vgap, hgap ) - , m_rowHeights( NULL ) - , m_colWidths( NULL ) + : wxGridSizer( rows, cols, vgap, hgap ), + m_flexDirection(wxBOTH), + m_growMode(wxFLEX_GROWMODE_SPECIFIED) { } wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap ) - : wxGridSizer( cols, vgap, hgap ) - , m_rowHeights( NULL ) - , m_colWidths( NULL ) + : wxGridSizer( cols, vgap, hgap ), + m_flexDirection(wxBOTH), + m_growMode(wxFLEX_GROWMODE_SPECIFIED) { } wxFlexGridSizer::~wxFlexGridSizer() { - if (m_rowHeights) - delete[] m_rowHeights; - if (m_colWidths) - delete[] m_colWidths; -} - -void wxFlexGridSizer::CreateArrays() -{ - if (m_rowHeights) - delete[] m_rowHeights; - if (m_colWidths) - delete[] m_colWidths; - - int nitems, nrows, ncols; - if ( (nitems = CalcRowsCols(nrows, ncols)) == 0 ) - { - m_rowHeights = - m_colWidths = NULL; - } - - m_rowHeights = new int[nrows]; - m_colWidths = new int[ncols]; - - for (int col = 0; col < ncols; col++) - m_colWidths[ col ] = 0; - for (int row = 0; row < nrows; row++) - m_rowHeights[ row ] = 0; } void wxFlexGridSizer::RecalcSizes() @@ -1054,36 +1027,93 @@ void wxFlexGridSizer::RecalcSizes() wxSize minsz( CalcMin() ); wxPoint pt( GetPosition() ); int delta; - size_t idx,num; + size_t idx, num; wxArrayInt temp; + wxArrayInt temp_proportions; + int sum_proportions = 0; + int growable_space = 0; - // Transfer only those rows into temp which exist in the sizer - // ignoring the superflouus ones. This prevents a segfault when - // calling AddGrowableRow( 3 ) if the sizer only has 2 rows. - for (idx = 0; idx < m_growableRows.GetCount(); idx++) - if (m_growableRows[idx] < nrows) - temp.Add( m_growableRows[idx] ); - num = temp.GetCount(); + // what to do with the rows? by default, resize them proportionally + if ( (m_flexDirection & wxVERTICAL) || + (m_growMode == wxFLEX_GROWMODE_SPECIFIED) ) + { + // Transfer only those rows into temp which exist in the sizer + // ignoring the superfluous ones. This prevents a segfault when + // calling AddGrowableRow( 3 ) if the sizer only has 2 rows. + for (idx = 0; idx < m_growableRows.GetCount(); idx++) + { + if (m_growableRows[idx] < nrows) + { + temp.Add( m_growableRows[idx] ); + temp_proportions.Add( m_growableRowsProportions[idx] ); + sum_proportions += m_growableRowsProportions[idx]; + growable_space += m_rowHeights[ temp[idx] ]; + } + } - if ((num > 0) && (sz.y > minsz.y)) + num = temp.GetCount(); + + if ((num > 0) && (sz.y > minsz.y)) + { + for (idx = 0; idx < num; idx++) + { + delta = (sz.y - minsz.y); + if (sum_proportions == 0) + delta = (delta/num) + m_rowHeights[ temp[idx] ]; + else + delta = ((delta+growable_space)*temp_proportions[idx])/ + sum_proportions; + m_rowHeights[ temp[idx] ] = delta; + } + } + temp.Empty(); + temp_proportions.Empty(); + sum_proportions = 0; + growable_space = 0; + } + else if ( (m_growMode == wxFLEX_GROWMODE_ALL) && (sz.y > minsz.y) ) { - delta = (sz.y - minsz.y) / num; - for (idx = 0; idx < num; idx++) - m_rowHeights[ temp[idx] ] += delta; + // rounding problem? + for ( int row = 0; row < nrows; ++row ) + m_rowHeights[ row ] = sz.y / nrows; } - temp.Empty(); - // See above - for (idx = 0; idx < m_growableCols.GetCount(); idx++) - if (m_growableCols[idx] < ncols) - temp.Add( m_growableCols[idx] ); - num = temp.GetCount(); + // the same logic as above but for the columns + if ( (m_flexDirection & wxHORIZONTAL) || + (m_growMode == wxFLEX_GROWMODE_SPECIFIED) ) + { + // See above + for (idx = 0; idx < m_growableCols.GetCount(); idx++) + { + if (m_growableCols[idx] < ncols) + { + temp.Add( m_growableCols[idx] ); + temp_proportions.Add( m_growableColsProportions[idx] ); + sum_proportions += m_growableColsProportions[idx]; + growable_space += m_colWidths[idx]; + } + } + + num = temp.GetCount(); - if ((num > 0) && (sz.x > minsz.x)) + if ((num > 0) && (sz.x > minsz.x)) + { + for (idx = 0; idx < num; idx++) + { + delta = (sz.x - minsz.x); + if (sum_proportions == 0) + delta = (delta/num) + m_colWidths[ temp[idx] ]; + else + delta = ((delta+growable_space)*temp_proportions[idx])/ + sum_proportions; + m_colWidths[ temp[idx] ] = delta; + } + } + } + else if ( (m_growMode == wxFLEX_GROWMODE_ALL) && (sz.x > minsz.x) ) { - delta = (sz.x - minsz.x) / num; - for (idx = 0; idx < num; idx++) - m_colWidths[ temp[idx] ] += delta; + for ( int col=0; col < ncols; ++col ) + m_colWidths[ col ] = sz.x / ncols; } sz = wxSize( pt.x + sz.x, pt.y + sz.y ); @@ -1114,15 +1144,29 @@ void wxFlexGridSizer::RecalcSizes() wxSize wxFlexGridSizer::CalcMin() { - int nitems, nrows, ncols; - if ( (nitems = CalcRowsCols(nrows, ncols)) == 0 ) - return wxSize(10,10); + int nrows, + ncols; + size_t i, s; + + if ( !CalcRowsCols(nrows, ncols) ) + return wxSize(10, 10); + + // We have to clear the old sizes out if any item has wxADJUST_MINSIZE + // set on it. Its probably quicker to just always do it than test for + // that though. At least do it before resizing the arrays, SetCount will + // initialise new elements to zero. + for( s = m_rowHeights.GetCount(), i = 0; i < s; ++i ) + m_rowHeights[ i ] = 0; - CreateArrays(); + for( s = m_colWidths.GetCount(), i = 0; i < s; ++i ) + m_colWidths[ i ] = 0; + + m_rowHeights.SetCount(nrows); + m_colWidths.SetCount(ncols); - int i = 0; wxSizerItemList::Node *node = m_children.GetFirst(); + i = 0; while (node) { wxSizerItem *item = node->GetData(); @@ -1137,6 +1181,34 @@ wxSize wxFlexGridSizer::CalcMin() i++; } + // the logic above works when we resize flexibly in both directions but + // maybe this is not the case + if ( m_flexDirection != wxBOTH ) + { + // select the array corresponding to the direction in which we do *not* + // resize flexibly + wxArrayInt& array = m_flexDirection == wxVERTICAL ? m_colWidths + : m_rowHeights; + + const int count = array.GetCount(); + + // find the largest value in this array + int n, + largest = 0; + for ( n = 0; n < count; ++n ) + { + if ( array[n] > largest ) + largest = array[n]; + } + + // and now fill it with the largest value + for ( n = 0; n < count; ++n ) + { + array[n] = largest; + } + } + + int width = 0; for (int col = 0; col < ncols; col++) width += m_colWidths[ col ]; @@ -1149,18 +1221,20 @@ wxSize wxFlexGridSizer::CalcMin() height + (nrows-1) * m_vgap); } -void wxFlexGridSizer::AddGrowableRow( size_t idx ) +void wxFlexGridSizer::AddGrowableRow( size_t idx, int proportion ) { m_growableRows.Add( idx ); + m_growableRowsProportions.Add( proportion ); } void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) ) { } -void wxFlexGridSizer::AddGrowableCol( size_t idx ) +void wxFlexGridSizer::AddGrowableCol( size_t idx, int proportion ) { m_growableCols.Add( idx ); + m_growableColsProportions.Add( proportion ); } void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) ) @@ -1377,8 +1451,8 @@ static void GetStaticBoxBorders( wxStaticBox *box, *borderTop = 5; else #endif // __WXGTK__ - *borderTop = 15; - (void)box; + *borderTop = box->GetCharHeight(); + *borderOther = 5; }