]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/sizer.cpp
corrected signed/unsigned comparison warning
[wxWidgets.git] / src / common / sizer.cpp
index 02ca47f8f8998ec896615e17ad363626c0aca4f3..b4ae33b5a74b76291f83d83210f92b21b43ee220 100644 (file)
@@ -17,6 +17,7 @@
     #pragma hdrstop
 #endif
 
+#include "wx/display.h"
 #include "wx/sizer.h"
 
 #ifndef WX_PRECOMP
     #include "wx/math.h"
     #include "wx/utils.h"
     #include "wx/settings.h"
+    #include "wx/button.h"
     #include "wx/statbox.h"
+    #include "wx/toplevel.h"
 #endif // WX_PRECOMP
 
 #include "wx/listimpl.cpp"
 
-#if WXWIN_COMPATIBILITY_2_4
-    #include "wx/notebook.h"
-#endif
 
 //---------------------------------------------------------------------------
 
@@ -103,12 +103,10 @@ wxSizerItem::wxSizerItem()
     m_proportion = 0;
     m_border = 0;
     m_flag = 0;
-
-    m_kind = Item_None;
 }
 
 // window item
-void wxSizerItem::SetWindow(wxWindow *window)
+void wxSizerItem::DoSetWindow(wxWindow *window)
 {
     wxCHECK_RET( window, _T("NULL window in wxSizerItem::SetWindow()") );
 
@@ -130,16 +128,17 @@ wxSizerItem::wxSizerItem(wxWindow *window,
                          int flag,
                          int border,
                          wxObject* userData)
-           : m_proportion(proportion),
+           : m_kind(Item_None),
+             m_proportion(proportion),
              m_border(border),
              m_flag(flag),
              m_userData(userData)
 {
-    SetWindow(window);
+    DoSetWindow(window);
 }
 
 // sizer item
-void wxSizerItem::SetSizer(wxSizer *sizer)
+void wxSizerItem::DoSetSizer(wxSizer *sizer)
 {
     m_kind = Item_Sizer;
     m_sizer = sizer;
@@ -150,19 +149,21 @@ wxSizerItem::wxSizerItem(wxSizer *sizer,
                          int flag,
                          int border,
                          wxObject* userData)
-           : m_proportion(proportion),
+           : m_kind(Item_None),
+             m_sizer(NULL),
+             m_proportion(proportion),
              m_border(border),
              m_flag(flag),
              m_ratio(0.0),
              m_userData(userData)
 {
-    SetSizer(sizer);
+    DoSetSizer(sizer);
 
     // m_minSize is set later
 }
 
 // spacer item
-void wxSizerItem::SetSpacer(const wxSize& size)
+void wxSizerItem::DoSetSpacer(const wxSize& size)
 {
     m_kind = Item_Spacer;
     m_spacer = new wxSizerSpacer(size);
@@ -176,19 +177,25 @@ wxSizerItem::wxSizerItem(int width,
                          int flag,
                          int border,
                          wxObject* userData)
-           : m_minSize(width, height), // minimal size is the initial size
+           : m_kind(Item_None),
+             m_sizer(NULL),
+             m_minSize(width, height), // minimal size is the initial size
              m_proportion(proportion),
              m_border(border),
              m_flag(flag),
              m_userData(userData)
 {
-    SetSpacer(width, height);
+    DoSetSpacer(wxSize(width, height));
 }
 
 wxSizerItem::~wxSizerItem()
 {
     delete m_userData;
+    Free();
+}
 
+void wxSizerItem::Free()
+{
     switch ( m_kind )
     {
         case Item_None:
@@ -210,6 +217,8 @@ wxSizerItem::~wxSizerItem()
         default:
             wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
     }
+
+    m_kind = Item_None;
 }
 
 wxSize wxSizerItem::GetSpacer() const
@@ -274,7 +283,7 @@ wxSize wxSizerItem::CalcMin()
     {
         // Since the size of the window may change during runtime, we
         // should use the current minimal/best size.
-        m_minSize = m_window->GetBestFittingSize();
+        m_minSize = m_window->GetEffectiveMinSize();
     }
 
     return GetMinSizeWithBorder();
@@ -352,6 +361,11 @@ void wxSizerItem::SetDimension( const wxPoint& pos_, const wxSize& size_ )
         size.y -= m_border;
     }
 
+    if (size.x < 0)
+        size.x = 0;
+    if (size.y < 0)
+        size.y = 0;
+
     m_rect = wxRect(pos, size);
 
     switch ( m_kind )
@@ -452,6 +466,11 @@ bool wxSizerItem::IsShown() const
             // shown, so are we (this arbitrariness is the reason for
             // deprecating this function)
             {
+                // Some apps (such as dialog editors) depend on an empty sizer still
+                // being laid out correctly and reporting the correct size and position.
+                if (m_sizer->GetChildren().GetCount() == 0)
+                    return true;
+
                 for ( wxSizerItemList::compatibility_iterator
                         node = m_sizer->GetChildren().GetFirst();
                       node;
@@ -506,6 +525,29 @@ wxSizerItem* wxSizer::Insert( size_t index, wxSizerItem *item )
     return item;
 }
 
+void wxSizer::SetContainingWindow(wxWindow *win)
+{
+    if ( win == m_containingWindow )
+        return;
+
+    m_containingWindow = win;
+
+    // set the same window for all nested sizers as well, they also are in the
+    // same window
+    for ( wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+          node;
+          node = node->GetNext() )
+    {
+        wxSizerItem *const item = node->GetData();
+        wxSizer *const sizer = item->GetSizer();
+
+        if ( sizer )
+        {
+            sizer->SetContainingWindow(win);
+        }
+    }
+}
+
 #if WXWIN_COMPATIBILITY_2_6
 bool wxSizer::Remove( wxWindow *window )
 {
@@ -545,13 +587,9 @@ bool wxSizer::Remove( int index )
 
     wxCHECK_MSG( node, false, _T("Failed to find child node") );
 
-    wxSizerItem *item = node->GetData();
-
-    if ( item->IsWindow() )
-        item->GetWindow()->SetContainingSizer( NULL );
-
-    delete item;
+    delete node->GetData();
     m_children.Erase( node );
+
     return true;
 }
 
@@ -588,7 +626,6 @@ bool wxSizer::Detach( wxWindow *window )
 
         if (item->GetWindow() == window)
         {
-            item->GetWindow()->SetContainingSizer( NULL );
             delete item;
             m_children.Erase( node );
             return true;
@@ -613,14 +650,83 @@ bool wxSizer::Detach( int index )
 
     if ( item->IsSizer() )
         item->DetachSizer();
-    else if ( item->IsWindow() )
-        item->GetWindow()->SetContainingSizer( NULL );
 
     delete item;
     m_children.Erase( node );
     return true;
 }
 
+bool wxSizer::Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive )
+{
+    wxASSERT_MSG( oldwin, _T("Replacing NULL window") );
+    wxASSERT_MSG( newwin, _T("Replacing with NULL window") );
+
+    wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+    while (node)
+    {
+        wxSizerItem     *item = node->GetData();
+
+        if (item->GetWindow() == oldwin)
+        {
+            item->AssignWindow(newwin);
+            newwin->SetContainingSizer( this );
+            return true;
+        }
+        else if (recursive && item->IsSizer())
+        {
+            if (item->GetSizer()->Replace( oldwin, newwin, true ))
+                return true;
+        }
+
+        node = node->GetNext();
+    }
+
+    return false;
+}
+
+bool wxSizer::Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive )
+{
+    wxASSERT_MSG( oldsz, _T("Replacing NULL sizer") );
+    wxASSERT_MSG( newsz, _T("Replacing with NULL sizer") );
+
+    wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+    while (node)
+    {
+        wxSizerItem     *item = node->GetData();
+
+        if (item->GetSizer() == oldsz)
+        {
+            item->AssignSizer(newsz);
+            return true;
+        }
+        else if (recursive && item->IsSizer())
+        {
+            if (item->GetSizer()->Replace( oldsz, newsz, true ))
+                return true;
+        }
+
+        node = node->GetNext();
+    }
+
+    return false;
+}
+
+bool wxSizer::Replace( size_t old, wxSizerItem *newitem )
+{
+    wxCHECK_MSG( old < m_children.GetCount(), false, _T("Replace index is out of range") );
+    wxASSERT_MSG( newitem, _T("Replacing with NULL item") );
+
+    wxSizerItemList::compatibility_iterator node = m_children.Item( old );
+
+    wxCHECK_MSG( node, false, _T("Failed to find child node") );
+
+    wxSizerItem *item = node->GetData();
+    node->SetData(newitem);
+    delete item;
+
+    return true;
+}
+
 void wxSizer::Clear( bool delete_windows )
 {
     // First clear the ContainingSizer pointers
@@ -656,8 +762,37 @@ void wxSizer::DeleteWindows()
 
 wxSize wxSizer::Fit( wxWindow *window )
 {
-    wxSize size(window->IsTopLevel() ? FitSize(window)
-                                     : GetMinWindowSize(window));
+    // take the min size by default and limit it by max size
+    wxSize size = GetMinWindowSize(window);
+    wxSize sizeMax = GetMaxWindowSize(window);
+
+    wxTopLevelWindow *tlw = wxDynamicCast(window, wxTopLevelWindow);
+    if ( tlw )
+    {
+        // hack for small screen devices where TLWs are always full screen
+        if ( tlw->IsAlwaysMaximized() )
+        {
+            size = tlw->GetSize();
+        }
+        else // normal situation
+        {
+            // limit the window to the size of the display it is on
+            int disp = wxDisplay::GetFromWindow(window);
+            if ( disp == wxNOT_FOUND )
+            {
+                // or, if we don't know which one it is, of the main one
+                disp = 0;
+            }
+
+            sizeMax = wxDisplay(disp).GetClientArea().GetSize();
+        }
+    }
+
+    if ( sizeMax.x != wxDefaultCoord && size.x > sizeMax.x )
+        size.x = sizeMax.x;
+    if ( sizeMax.y != wxDefaultCoord && size.y > sizeMax.y )
+        size.y = sizeMax.y;
+
 
     window->SetSize( size );
 
@@ -729,32 +864,6 @@ wxSize wxSizer::GetMinWindowSize( wxWindow *window )
 // TODO on mac we need a function that determines how much free space this
 // min size contains, in order to make sure that we have 20 pixels of free
 // space around the controls
-
-// Return a window size that will fit within the screens dimensions
-wxSize wxSizer::FitSize( wxWindow *window )
-{
-    if ( window->IsTopLevel() )
-    {
-        wxTopLevelWindow *tlw = wxDynamicCast(window, wxTopLevelWindow);
-        if ( tlw && tlw->IsAlwaysMaximized() )
-        {
-            return tlw->GetClientSize();
-        }
-    }
-
-    wxSize size     = GetMinWindowSize( window );
-    wxSize sizeMax  = GetMaxWindowSize( window );
-
-    // Limit the size if sizeMax != wxDefaultSize
-
-    if ( size.x > sizeMax.x && sizeMax.x != wxDefaultCoord )
-        size.x = sizeMax.x;
-    if ( size.y > sizeMax.y && sizeMax.y != wxDefaultCoord )
-        size.y = sizeMax.y;
-
-    return size;
-}
-
 wxSize wxSizer::GetMaxClientSize( wxWindow *window ) const
 {
     wxSize maxSize( window->GetMaxSize() );
@@ -1597,6 +1706,16 @@ void wxBoxSizer::RecalcSizes()
                 //     wxALIGN_CENTER should be used in new code
                     child_pos.y += (m_size.y - size.y) / 2;
 
+                if ( m_containingWindow )
+                {
+                    child_pos.x = m_containingWindow->AdjustForLayoutDirection
+                                                      (
+                                                        child_pos.x,
+                                                        width,
+                                                        m_size.x
+                                                      );
+                }
+
                 item->SetDimension( child_pos, child_size );
 
                 pt.x += width;
@@ -1902,7 +2021,8 @@ void wxStdDialogButtonSizer::Realize()
             if (m_buttonAffirmative->GetId() == wxID_SAVE){
                 // these buttons have set labels under Mac so we should use them
                 m_buttonAffirmative->SetLabel(_("Save"));
-                m_buttonNegative->SetLabel(_("Don't Save"));
+                if (m_buttonNegative)
+                    m_buttonNegative->SetLabel(_("Don't Save"));
             }
         }
 
@@ -1984,88 +2104,3 @@ void wxStdDialogButtonSizer::Realize()
 }
 
 #endif // wxUSE_BUTTON
-
-#if WXWIN_COMPATIBILITY_2_4
-
-// ----------------------------------------------------------------------------
-// wxNotebookSizer
-// ----------------------------------------------------------------------------
-
-#if wxUSE_BOOKCTRL
-IMPLEMENT_CLASS(wxBookCtrlSizer, wxSizer)
-#if wxUSE_NOTEBOOK
-IMPLEMENT_CLASS(wxNotebookSizer, wxBookCtrlSizer)
-#endif // wxUSE_NOTEBOOK
-#endif // wxUSE_BOOKCTRL
-
-#if wxUSE_BOOKCTRL
-
-#if WXWIN_COMPATIBILITY_2_6
-
-wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrlBase *bookctrl)
-               : m_bookctrl(bookctrl)
-{
-    wxASSERT_MSG( bookctrl, wxT("wxBookCtrlSizer needs a control") );
-}
-
-#endif // WXWIN_COMPATIBILITY_2_6
-
-void wxBookCtrlSizer::RecalcSizes()
-{
-    m_bookctrl->SetSize( m_position.x, m_position.y, m_size.x, m_size.y );
-}
-
-wxSize wxBookCtrlSizer::CalcMin()
-{
-    wxSize sizeBorder = m_bookctrl->CalcSizeFromPage(wxSize(0,0));
-
-    sizeBorder.x += 5;
-    sizeBorder.y += 5;
-
-    if ( m_bookctrl->GetPageCount() == 0 )
-    {
-        return wxSize(sizeBorder.x + 10, sizeBorder.y + 10);
-    }
-
-    int maxX = 0;
-    int maxY = 0;
-
-    wxWindowList::compatibility_iterator
-        node = m_bookctrl->GetChildren().GetFirst();
-    while (node)
-    {
-        wxWindow *item = node->GetData();
-        wxSizer *itemsizer = item->GetSizer();
-
-        if (itemsizer)
-        {
-            wxSize subsize( itemsizer->CalcMin() );
-
-            if (subsize.x > maxX)
-                maxX = subsize.x;
-            if (subsize.y > maxY)
-                maxY = subsize.y;
-        }
-
-        node = node->GetNext();
-    }
-
-    return wxSize( maxX, maxY ) + sizeBorder;
-}
-
-#if wxUSE_NOTEBOOK
-
-#if WXWIN_COMPATIBILITY_2_6
-
-wxNotebookSizer::wxNotebookSizer(wxNotebook *nb)
-{
-    wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a control") );
-    m_bookctrl = nb;
-}
-
-#endif // WXWIN_COMPATIBILITY_2_6
-
-#endif // wxUSE_NOTEBOOOK
-#endif // wxUSE_BOOKCTRL
-
-#endif // WXWIN_COMPATIBILITY_2_4