Don't make buttons with images too wide in wxMSW.

We used to increase the button size to the standard size first and then add
the extra padding the buttons with images. This resulted in too much padding
for the buttons with short text labels and images.

Instead, add the padding for the image first and only then increase the button
size to the standard one if still needed.

See #12924.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66892 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-02-14 22:12:10 +00:00
parent c3d652aeb9
commit 1e43584aa7
2 changed files with 29 additions and 14 deletions

View File

@@ -47,15 +47,29 @@ enum
Size_AuthNeeded = 1 Size_AuthNeeded = 1
}; };
// common implementation of wxButton and wxToggleButton::DoGetBestSize() // NB: All the functions below are implemented in src/msw/button.cpp
// (implemented in src/msw/button.cpp)
wxSize ComputeBestSize(wxControl *btn, int flags = 0);
// compute the button size (as if wxBU_EXACTFIT were specified, i.e. without // Compute the button size (as if wxBU_EXACTFIT were specified, i.e. without
// adjusting it to be of default size if it's smaller) for the given label size // adjusting it to be of default size if it's smaller) for the given label size
WXDLLIMPEXP_CORE wxSize WXDLLIMPEXP_CORE wxSize
GetFittingSize(wxWindow *win, const wxSize& sizeLabel, int flags = 0); GetFittingSize(wxWindow *win, const wxSize& sizeLabel, int flags = 0);
// Compute the button size (as if wxBU_EXACTFIT were specified) by computing
// its label size and then calling GetFittingSize().
wxSize ComputeBestFittingSize(wxControl *btn, int flags = 0);
// Increase the size passed as parameter to be at least the standard button
// size if the control doesn't have wxBU_EXACTFIT style and also cache it as
// the best size and return its value -- this is used in DoGetBestSize()
// implementation.
wxSize IncreaseToStdSizeAndCache(wxControl *btn, const wxSize& size);
// helper of wxToggleButton::DoGetBestSize()
inline wxSize ComputeBestSize(wxControl *btn, int flags = 0)
{
return IncreaseToStdSizeAndCache(btn, ComputeBestFittingSize(btn, flags));
}
} // namespace wxMSWButton } // namespace wxMSWButton
#endif // _WX_MSW_PRIVATE_BUTTON_H_ #endif // _WX_MSW_PRIVATE_BUTTON_H_

View File

@@ -397,14 +397,19 @@ wxSize wxMSWButton::GetFittingSize(wxWindow *win,
return sizeBtn; return sizeBtn;
} }
wxSize wxMSWButton::ComputeBestSize(wxControl *btn, int flags) wxSize wxMSWButton::ComputeBestFittingSize(wxControl *btn, int flags)
{ {
wxClientDC dc(btn); wxClientDC dc(btn);
wxSize sizeBtn; wxSize sizeBtn;
dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y); dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y);
sizeBtn = GetFittingSize(btn, sizeBtn, flags); return GetFittingSize(btn, sizeBtn, flags);
}
wxSize wxMSWButton::IncreaseToStdSizeAndCache(wxControl *btn, const wxSize& size)
{
wxSize sizeBtn(size);
// all buttons have at least the standard size unless the user explicitly // all buttons have at least the standard size unless the user explicitly
// wants them to be of smaller size and used wxBU_EXACTFIT style when // wants them to be of smaller size and used wxBU_EXACTFIT style when
@@ -595,6 +600,8 @@ void wxButton::AdjustForBitmapSize(wxSize &size) const
wxSize wxButton::DoGetBestSize() const wxSize wxButton::DoGetBestSize() const
{ {
wxButton * const self = const_cast<wxButton *>(this);
wxSize size; wxSize size;
// account for the text part if we have it or if we don't have any image at // account for the text part if we have it or if we don't have any image at
@@ -606,19 +613,13 @@ wxSize wxButton::DoGetBestSize() const
if ( GetAuthNeeded() ) if ( GetAuthNeeded() )
flags |= wxMSWButton::Size_AuthNeeded; flags |= wxMSWButton::Size_AuthNeeded;
size = wxMSWButton::ComputeBestSize(const_cast<wxButton *>(this), flags); size = wxMSWButton::ComputeBestFittingSize(self, flags);
} }
if ( m_imageData ) if ( m_imageData )
{
AdjustForBitmapSize(size); AdjustForBitmapSize(size);
// The best size has changed so even if it had been already cached by return wxMSWButton::IncreaseToStdSizeAndCache(self, size);
// ComputeBestSize() call above we still need to update it.
CacheBestSize(size);
}
return size;
} }
/* static */ /* static */