From 743b426605478eeb31232c30e804e70ff493b1f2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 22 Jun 2009 20:36:13 +0000 Subject: [PATCH] Added DoGetClientBestSize() and use it for a couple of controls in wxMSW. DoGetClientBestSize() returns the best size of the client area, without accounting for the border which is done by GetBestSize() itself and DoGetBorderSize() called from it. Use DoGetClientBestSize() in wxStaticText (where it was done insideDoGetBestSize() before) and in wxListBox, to fix its height calculation. Also use correct height of listbox items as returned by the control itself instead of trying to guess it. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61169 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/listbox.h | 4 ++-- include/wx/msw/stattext.h | 2 +- include/wx/msw/window.h | 2 ++ include/wx/window.h | 18 ++++++++++++++++++ src/common/wincmn.cpp | 13 ++++++++++++- src/msw/listbox.cpp | 15 +++++---------- src/msw/stattext.cpp | 37 ++----------------------------------- src/msw/window.cpp | 30 ++++++++++++++++++++++++++++++ 8 files changed, 72 insertions(+), 49 deletions(-) diff --git a/include/wx/msw/listbox.h b/include/wx/msw/listbox.h index 2d77f86c1a..8157cbdcbd 100644 --- a/include/wx/msw/listbox.h +++ b/include/wx/msw/listbox.h @@ -133,6 +133,8 @@ public: virtual bool CanApplyThemeBorder() const { return false; } protected: + virtual wxSize DoGetBestClientSize() const; + virtual void DoClear(); virtual void DoDeleteOneItem(unsigned int n); @@ -155,8 +157,6 @@ protected: unsigned int m_noItems; - virtual wxSize DoGetBestSize() const; - #if wxUSE_OWNER_DRAWN // control items wxListBoxItemsArray m_aItems; diff --git a/include/wx/msw/stattext.h b/include/wx/msw/stattext.h index 28e1b79656..2bae454889 100644 --- a/include/wx/msw/stattext.h +++ b/include/wx/msw/stattext.h @@ -46,7 +46,7 @@ protected: // implement/override some base class virtuals virtual void DoSetSize(int x, int y, int w, int h, int sizeFlags = wxSIZE_AUTO); - virtual wxSize DoGetBestSize() const; + virtual wxSize DoGetBestClientSize() const; virtual wxString DoGetLabel() const; virtual void DoSetLabel(const wxString& str); diff --git a/include/wx/msw/window.h b/include/wx/msw/window.h index 8a9a2aa7bc..4026e7ea06 100644 --- a/include/wx/msw/window.h +++ b/include/wx/msw/window.h @@ -501,6 +501,8 @@ protected: int sizeFlags = wxSIZE_AUTO); virtual void DoSetClientSize(int width, int height); + virtual wxSize DoGetBorderSize() const; + virtual void DoCaptureMouse(); virtual void DoReleaseMouse(); diff --git a/include/wx/window.h b/include/wx/window.h index 913cd7d0aa..c9e0c59546 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -1633,6 +1633,11 @@ protected: // same size as it would have after a call to Fit() virtual wxSize DoGetBestSize() const; + // this method can be overridden instead of DoGetBestSize() if it computes + // the best size of the client area of the window only, excluding borders + // (GetBorderSize() will be used to add them) + virtual wxSize DoGetBestClientSize() const { return wxDefaultSize; } + // this is the virtual function to be overriden 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 @@ -1647,6 +1652,19 @@ protected: int maxW, int maxH, int incW, int incH ); + // return the total size of the window borders, i.e. the sum of the widths + // of the left and the right border in the x component of the returned size + // and the sum of the heights of the top and bottom borders in the y one + // + // NB: this is new/temporary API only implemented by wxMSW so far and + // subject to change, don't use + virtual wxSize DoGetBorderSize() const + { + wxFAIL_MSG( "must be overridden if called" ); + + return wxDefaultSize; + } + // move the window to the specified location and resize it: this is called // from both DoSetSize() and DoSetClientSize() and would usually just // reposition this window except for composite controls which will want to diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 8a0ae4f54f..6243c4d73d 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -705,9 +705,20 @@ wxSize wxWindowBase::GetEffectiveMinSize() const wxSize wxWindowBase::GetBestSize() const { - if ((!m_windowSizer) && (m_bestSizeCache.IsFullySpecified())) + if ( !m_windowSizer && m_bestSizeCache.IsFullySpecified() ) return m_bestSizeCache; + // call DoGetBestClientSize() first, if a derived class overrides it wants + // it to be used + wxSize size = DoGetBestClientSize(); + if ( size != wxDefaultSize ) + { + size += DoGetBorderSize(); + + CacheBestSize(size); + return size; + } + return DoGetBestSize(); } diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index 3e6b8a7612..84634d3088 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -590,7 +590,7 @@ void wxListBox::SetHorizontalExtent(const wxString& s) //else: it shouldn't change } -wxSize wxListBox::DoGetBestSize() const +wxSize wxListBox::DoGetBestClientSize() const { // find the widest string int wLine; @@ -609,22 +609,17 @@ wxSize wxListBox::DoGetBestSize() const wListbox = 100; // the listbox should be slightly larger than the widest string - int cx, cy; - wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); + wListbox += 3*GetCharWidth(); - wListbox += 3*cx; - - // Add room for the scrollbar + // add room for the scrollbar wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); // don't make the listbox too tall (limit height to 10 items) but don't // make it too small neither - int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)* + int hListbox = SendMessage(GetHwnd(), LB_GETITEMHEIGHT, 0, 0)* wxMin(wxMax(m_noItems, 3), 10); - wxSize best(wListbox, hListbox); - CacheBestSize(best); - return best; + return wxSize(wListbox, hListbox); } // ---------------------------------------------------------------------------- diff --git a/src/msw/stattext.cpp b/src/msw/stattext.cpp index 7a20a94ae0..898487a88c 100644 --- a/src/msw/stattext.cpp +++ b/src/msw/stattext.cpp @@ -142,7 +142,7 @@ WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const return msStyle; } -wxSize wxStaticText::DoGetBestSize() const +wxSize wxStaticText::DoGetBestClientSize() const { wxClientDC dc(const_cast(this)); wxFont font(GetFont()); @@ -159,40 +159,7 @@ wxSize wxStaticText::DoGetBestSize() const widthTextMax += 2; #endif // __WXWINCE__ - // border takes extra space - // - // TODO: this is probably not wxStaticText-specific and should be moved - wxCoord border; - switch ( GetBorder() ) - { - case wxBORDER_STATIC: - case wxBORDER_SIMPLE: - border = 1; - break; - - case wxBORDER_SUNKEN: - border = 2; - break; - - case wxBORDER_RAISED: - case wxBORDER_DOUBLE: - border = 3; - break; - - default: - wxFAIL_MSG( _T("unknown border style") ); - // fall through - - case wxBORDER_NONE: - border = 0; - } - - widthTextMax += 2*border; - heightTextTotal += 2*border; - - wxSize best(widthTextMax, heightTextTotal); - CacheBestSize(best); - return best; + return wxSize(widthTextMax, heightTextTotal); } void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags) diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 45a3767f01..be4133f07f 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -2112,6 +2112,36 @@ void wxWindowMSW::DoSetClientSize(int width, int height) } } +wxSize wxWindowMSW::DoGetBorderSize() const +{ + wxCoord border; + switch ( GetBorder() ) + { + case wxBORDER_STATIC: + case wxBORDER_SIMPLE: + border = 1; + break; + + case wxBORDER_SUNKEN: + border = 2; + break; + + case wxBORDER_RAISED: + case wxBORDER_DOUBLE: + border = 3; + break; + + default: + wxFAIL_MSG( _T("unknown border style") ); + // fall through + + case wxBORDER_NONE: + border = 0; + } + + return 2*wxSize(border, border); +} + // --------------------------------------------------------------------------- // text metrics // ---------------------------------------------------------------------------