]> git.saurik.com Git - wxWidgets.git/commitdiff
Honour window min and max sizes in wxWindow::GetBestSize().
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 15 Aug 2012 23:34:18 +0000 (23:34 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 15 Aug 2012 23:34:18 +0000 (23:34 +0000)
The best size of the window should be at least as large as its min size and
less than its max size. This allows to override the windows own best size
determination with an explicit SetMinSize() or SetMaxSize() call.

See #11497.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72343 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

interface/wx/window.h
src/common/wincmn.cpp
tests/window/setsize.cpp

index 7b32e9c5c5c0b20bb105ebad5402cb04980e2b7b..29300d796d52b9abd7e1df6765f8fa1766b0fa8f 100644 (file)
@@ -845,6 +845,12 @@ public:
         convenient, DoGetBestClientSize() when writing your own custom window
         class to change the value returned by this public non-virtual method.
 
+        Notice that the best size respects the minimal and maximal size
+        explicitly set for the window, if any. So even if some window believes
+        that it needs 200 pixels horizontally, calling SetMaxSize() with a
+        width of 100 would ensure that GetBestSize() returns the width of at
+        most 100 pixels.
+
         @see CacheBestSize(), @ref overview_windowsizing
     */
     wxSize GetBestSize() const;
index 031712592b76b163131661d4b449506a7e2a1553..ae072ece15b39481f86ec380f301b90a21b41e51 100644 (file)
@@ -906,14 +906,19 @@ wxSize wxWindowBase::GetBestSize() const
     // it to be used
     wxSize size = DoGetBestClientSize();
     if ( size != wxDefaultSize )
-    {
         size += DoGetBorderSize();
+    else
+        size = DoGetBestSize();
 
-        CacheBestSize(size);
-        return size;
-    }
+    // Ensure that the best size is at least as large as min size.
+    size.IncTo(GetMinSize());
+
+    // And not larger than max size.
+    size.DecToIfSpecified(GetMaxSize());
 
-    return DoGetBestSize();
+    // Finally cache result and return.
+    CacheBestSize(size);
+    return size;
 }
 
 int wxWindowBase::GetBestHeight(int width) const
@@ -938,12 +943,16 @@ void wxWindowBase::SetMinSize(const wxSize& minSize)
 {
     m_minWidth = minSize.x;
     m_minHeight = minSize.y;
+
+    InvalidateBestSize();
 }
 
 void wxWindowBase::SetMaxSize(const wxSize& maxSize)
 {
     m_maxWidth = maxSize.x;
     m_maxHeight = maxSize.y;
+
+    InvalidateBestSize();
 }
 
 void wxWindowBase::SetInitialSize(const wxSize& size)
index c1fd6f8535c5d967951b821326d58d7538983e05..b720b793352cf4c37af2faeb71f0177032b64fe5 100644 (file)
@@ -40,10 +40,25 @@ private:
     CPPUNIT_TEST_SUITE( SetSizeTestCase );
         CPPUNIT_TEST( SetSize );
         CPPUNIT_TEST( SetSizeLessThanMinSize );
+        CPPUNIT_TEST( BestSize );
     CPPUNIT_TEST_SUITE_END();
 
     void SetSize();
     void SetSizeLessThanMinSize();
+    void BestSize();
+
+    // Helper class overriding DoGetBestSize() for testing purposes.
+    class MyWindow : public wxWindow
+    {
+    public:
+        MyWindow(wxWindow* parent)
+            : wxWindow(parent, wxID_ANY)
+        {
+        }
+
+    protected:
+        virtual wxSize DoGetBestSize() const { return wxSize(50, 250); }
+    };
 
     wxWindow *m_win;
 
@@ -62,7 +77,7 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SetSizeTestCase, "SetSizeTestCase" );
 
 void SetSizeTestCase::setUp()
 {
-    m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
+    m_win = new MyWindow(wxTheApp->GetTopWindow());
 }
 
 void SetSizeTestCase::tearDown()
@@ -91,3 +106,13 @@ void SetSizeTestCase::SetSizeLessThanMinSize()
     CPPUNIT_ASSERT_EQUAL( size, m_win->GetSize() );
 }
 
+void SetSizeTestCase::BestSize()
+{
+    CPPUNIT_ASSERT_EQUAL( wxSize(50, 250), m_win->GetBestSize() );
+
+    m_win->SetMinSize(wxSize(100, 100));
+    CPPUNIT_ASSERT_EQUAL( wxSize(100, 250), m_win->GetBestSize() );
+
+    m_win->SetMaxSize(wxSize(200, 200));
+    CPPUNIT_ASSERT_EQUAL( wxSize(100, 200), m_win->GetBestSize() );
+}