From d119983947975b98ad4ca1295be46ae9d7ffe805 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 9 May 2012 14:24:37 +0000 Subject: [PATCH] Add wxWindow::GetBest{Height,Width}(). These functions will be used when it is necessary to determine the best size of the control if one of its size components is fixed. Currently none of the classes implements DoGetBestClient{Height,Width}() yet but wxListCtrl will do it soon, see #13898. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71392 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/window.h | 15 ++++++++++++ interface/wx/window.h | 55 +++++++++++++++++++++++++++++++++++++++++++ src/common/wincmn.cpp | 18 ++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/include/wx/window.h b/include/wx/window.h index 7a15b0bee7..af1b4886ea 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -379,6 +379,13 @@ public: *h = s.y; } + // Determine the best size in the other direction if one of them is + // fixed. This is used with windows that can wrap their contents and + // returns input-independent best size for the others. + int GetBestHeight(int width) const; + int GetBestWidth(int height) const; + + void SetScrollHelper( wxScrollHelper *sh ) { m_scrollHelper = sh; } wxScrollHelper *GetScrollHelper() { return m_scrollHelper; } @@ -1687,6 +1694,14 @@ protected: // (GetBorderSize() will be used to add them) virtual wxSize DoGetBestClientSize() const { return wxDefaultSize; } + // These two methods can be overridden to implement intelligent + // width-for-height and/or height-for-width best size determination for the + // window. By default the fixed best size is used. + virtual int DoGetBestClientHeight(int WXUNUSED(width)) const + { return wxDefaultCoord; } + virtual int DoGetBestClientWidth(int WXUNUSED(height)) const + { return wxDefaultCoord; } + // this is the virtual function to be overridden in any derived class which // wants to change how SetSize() or Move() works - it is called by all // versions of these functions in the base class diff --git a/interface/wx/window.h b/interface/wx/window.h index 91981a7537..d98fb8b0ab 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -827,6 +827,24 @@ public: */ wxSize GetBestSize() const; + /** + Returns the best height needed by this window if it had the given width. + + @see DoGetBestClientHeight() + + @since 2.9.4 + */ + int GetBestHeight(int width) const; + + /** + Returns the best width needed by this window if it had the given height. + + @see DoGetBestClientWidth() + + @since 2.9.4 + */ + int GetBestWidth(int height) const; + /** Returns the size of the window 'client area' in pixels. @@ -3479,6 +3497,43 @@ protected: */ virtual wxSize DoGetBestClientSize() const; + /** + Override this method to implement height-for-width best size + calculation. + + Return the height needed to fully display the control contents if its + width is fixed to the given value. Custom classes implementing + wrapping should override this method and return the height + corresponding to the number of lines needed to lay out the control + contents at this width. + + Currently this method is not used by wxWidgets yet, however it is + planned that it will be used by the new sizer classes implementing + height-for-width layout strategy in the future. + + Notice that implementing this method or even implementing both it and + DoGetBestClientWidth() doesn't replace overriding DoGetBestClientSize(), + i.e. you still need to implement the latter as well in order to provide + the best size when neither width nor height are constrained. + + By default returns ::wxDefaultCoord meaning that the vertical component + of DoGetBestClientSize() return value should be used. + + @since 2.9.4 + */ + virtual int DoGetBestClientHeight(int width) const; + + /** + Override this method to implement width-for-height best size + calculation. + + This method is exactly the same as DoGetBestClientHeight() except that + it determines the width assuming the height is fixed instead of vice + versa. + + @since 2.9.4 + */ + virtual int DoGetBestClientWidth(int height) const; /** Sets the initial window size if none is given (i.e. at least one of the diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 17d8f40a31..ce2bb0a237 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -916,6 +916,24 @@ wxSize wxWindowBase::GetBestSize() const return DoGetBestSize(); } +int wxWindowBase::GetBestHeight(int width) const +{ + const int height = DoGetBestClientHeight(width); + + return height == wxDefaultCoord + ? GetBestSize().y + : height + DoGetBorderSize().y; +} + +int wxWindowBase::GetBestWidth(int height) const +{ + const int width = DoGetBestClientWidth(height); + + return width == wxDefaultCoord + ? GetBestSize().x + : width + DoGetBorderSize().x; +} + void wxWindowBase::SetMinSize(const wxSize& minSize) { m_minWidth = minSize.x; -- 2.45.2