]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/collpaneg.cpp
Make wxWindow::SetSizeHints() do nothing.
[wxWidgets.git] / src / generic / collpaneg.cpp
index f8f7f3fcbc141f44e147acff1ffc25e258eb8429..2f1ea156b472f824c5cdc48ab0cb9ff04d1f51d3 100644 (file)
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
+
 #include "wx/collpane.h"
+
+#if wxUSE_BUTTON && wxUSE_STATLINE
+
+#ifndef WX_PRECOMP
+    #include "wx/toplevel.h"
+    #include "wx/button.h"
+    #include "wx/sizer.h"
+#endif // !WX_PRECOMP
+
 #include "wx/statline.h"
 
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// the number of pixels to leave between the button and the static line and
+// between the button and the pane
+#define wxCP_MARGIN         10
+
 // ============================================================================
 // implementation
 // ============================================================================
@@ -34,18 +52,19 @@ IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
 
 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
-    EVT_BUTTON(wxCP_BUTTON_ID, wxGenericCollapsiblePane::OnButton)
+    EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
     EVT_SIZE(wxGenericCollapsiblePane::OnSize)
 END_EVENT_TABLE()
 
 
-bool wxGenericCollapsiblePane::Create( wxWindow *parent, wxWindowID id,
-                                        const wxString& label,
-                                        const wxPoint& pos,
-                                        const wxSize& size,
-                                        long style,
-                                        const wxValidator& val,
-                                        const wxString& name)
+bool wxGenericCollapsiblePane::Create(wxWindow *parent,
+                                      wxWindowID id,
+                                      const wxString& label,
+                                      const wxPoint& pos,
+                                      const wxSize& size,
+                                      long style,
+                                      const wxValidator& val,
+                                      const wxString& name)
 {
     if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
         return false;
@@ -53,10 +72,11 @@ bool wxGenericCollapsiblePane::Create( wxWindow *parent, wxWindowID id,
     m_strLabel = label;
 
     // create children; their size & position is set in OnSize()
-    m_pButton = new wxButton(this, wxCP_BUTTON_ID, GetBtnLabel(), wxPoint(0, 0),
+    m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
                              wxDefaultSize, wxBU_EXACTFIT);
     m_pStatLine = new wxStaticLine(this, wxID_ANY);
-    m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER);
+    m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
+                           wxNO_BORDER);
 
     // start as collapsed:
     m_pPane->Hide();
@@ -70,84 +90,86 @@ wxSize wxGenericCollapsiblePane::DoGetBestSize() const
     wxSize sz = m_pButton->GetBestSize();
 
     // set width
-    sz.SetWidth( sz.GetWidth() + wxCP_MARGIN + m_pStatLine->GetBestSize().GetWidth() );
-    sz.SetWidth( wxMax(sz.GetWidth(), m_pPane->GetBestSize().GetWidth()) );
+    sz.SetWidth(sz.x + wxCP_MARGIN + m_pStatLine->GetBestSize().x);
+    const wxCoord paneWidth = m_pPane->GetBestSize().x;
+    if ( sz.x < paneWidth )
+        sz.x = paneWidth;
 
     // when expanded, we need more vertical space
-    if (!IsCollapsed())
-        sz.SetHeight( sz.GetHeight() + wxCP_MARGIN + m_pPane->GetBestSize().GetHeight() );
+    if ( IsExpanded() )
+        sz.SetHeight(sz.y + wxCP_MARGIN + m_pPane->GetBestSize().y);
 
     return sz;
 }
 
 wxString wxGenericCollapsiblePane::GetBtnLabel() const
 {
-    if (IsCollapsed())
-        return m_strLabel + wxT(" >>");
-    return m_strLabel + wxT(" <<");
+    return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
 }
 
-void wxGenericCollapsiblePane::Collapse(bool collapse)
+void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
 {
-    // optimization
-    if (IsCollapsed() == collapse)
-        return;
-
-    // update our state
-    m_pPane->Show(!collapse);
-
-    // update button label
-    // NB: this must be done after updating our "state"
-    m_pButton->SetLabel(GetBtnLabel());
-
     // minimal size has priority over the best size so set here our min size
-    wxSize sz = GetBestSize();
     SetMinSize(sz);
     SetSize(sz);
 
-    wxWindow *top = GetTopLevelParent();
-    if (top)
+    wxTopLevelWindow *
+        top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
+    if ( top )
     {
-        // we've changed our size, thus our top level parent needs to relayout itself
+        // we've changed our size, thus our top level parent needs to relayout
+        // itself
         top->Layout();
 
-        // FIXME: this makes wxGenericCollapsiblePane behave as the user expect but
-        //        maybe there are cases where this is unwanted!
+        // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
+        //        but maybe there are cases where this is unwanted!
         if (top->GetSizer())
 #ifdef __WXGTK__
-        // FIXME: the SetSizeHints() call would be required also for GTK+ for the
-        //        expanded->collapsed transition.
-        //        Unfortunately if we enable this line, then the GTK+ top window
-        //        won't always be resized by the SetClientSize() call below!
-        //        As a side effect of this dirty fix, the minimal size for the
-        //        pane window is not set in GTK+ and the user can hide it shrinking
-        //        the "top" window...
+        // FIXME: the SetSizeHints() call would be required also for GTK+ for
+        //        the expanded->collapsed transition. Unfortunately if we
+        //        enable this line, then the GTK+ top window won't always be
+        //        resized by the SetClientSize() call below! As a side effect
+        //        of this dirty fix, the minimal size for the pane window is
+        //        not set in GTK+ and the user can hide it shrinking the "top"
+        //        window...
         if (IsCollapsed())
 #endif
             top->GetSizer()->SetSizeHints(top);
 
-        if (IsCollapsed())
-        {
-            // NB: we need to use SetClientSize() and not SetSize() otherwise the size for
-            //     windows like e.g. wxFrames with wxMenubars won't be correctly set
-            top->SetClientSize(sz);
-        }
-        else
+
+        // we shouldn't attempt to resize a maximized window, whatever happens
+        if ( !top->IsMaximized() )
         {
-            // force our parent to "fit", i.e. expand so that it can honour
-            // our minimal size
-            top->Fit();
+            if ( IsCollapsed() )
+            {
+                // use SetClientSize() and not SetSize() otherwise the size for
+                // e.g. a wxFrame with a menubar wouldn't be correctly set
+                top->SetClientSize(sz);
+            }
+            else
+            {
+                // force our parent to "fit", i.e. expand so that it can honour
+                // our minimal size
+                top->Fit();
+            }
         }
     }
 }
 
-wxWindow *wxGenericCollapsiblePane::GetTopLevelParent()
+void wxGenericCollapsiblePane::Collapse(bool collapse)
 {
-    wxWindow *parent = GetParent();
-    while (parent && !parent->IsTopLevel())
-        parent = parent->GetParent();
+    // optimization
+    if ( IsCollapsed() == collapse )
+        return;
+
+    // update our state
+    m_pPane->Show(!collapse);
+
+    // update button label
+    // NB: this must be done after updating our "state"
+    m_pButton->SetLabel(GetBtnLabel());
 
-    return parent;
+    OnStateChange(GetBestSize());
 }
 
 void wxGenericCollapsiblePane::SetLabel(const wxString &label)
@@ -166,13 +188,13 @@ void wxGenericCollapsiblePane::LayoutChildren()
     // the button position & size are always ok...
 
     // move & resize the static line
-    m_pStatLine->SetSize(btnSz.GetWidth() + wxCP_MARGIN, btnSz.GetHeight()/2,
-                         GetSize().GetWidth() - btnSz.GetWidth() - wxCP_MARGIN, -1,
+    m_pStatLine->SetSize(btnSz.x + wxCP_MARGIN, btnSz.y/2,
+                         GetSize().x - btnSz.x - wxCP_MARGIN, -1,
                          wxSIZE_USE_EXISTING);
 
     // move & resize the container window
-    m_pPane->SetSize(0, btnSz.GetHeight() + wxCP_MARGIN,
-                     GetSize().GetWidth(), GetSize().GetHeight() - btnSz.GetHeight() - wxCP_MARGIN);
+    m_pPane->SetSize(0, btnSz.y + wxCP_MARGIN,
+                     GetSize().x, GetSize().y - btnSz.y - wxCP_MARGIN);
 }
 
 
@@ -181,8 +203,14 @@ void wxGenericCollapsiblePane::LayoutChildren()
 // wxGenericCollapsiblePane - event handlers
 //-----------------------------------------------------------------------------
 
-void wxGenericCollapsiblePane::OnButton(wxCommandEvent &WXUNUSED(event))
+void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
 {
+    if ( event.GetEventObject() != m_pButton )
+    {
+        event.Skip();
+        return;
+    }
+
     Collapse(!IsCollapsed());
 
     // this change was generated by the user - send the event
@@ -211,3 +239,4 @@ void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
     m_pPane->Layout();
 }
 
+#endif // wxUSE_BUTTON && wxUSE_STATLINE