Minor cleanup in wxMSW wxSpinCtrl::Create()

No real changes, just explain the real reason why we can't create the windows
with the correct sizes and need to set them later (it's not because of the
font but because UDM_SETBUDDY changes them) and also use a helper
"effectiveSpinWidth" variable.
This commit is contained in:
Vadim Zeitlin
2017-05-15 22:26:15 +02:00
parent c4b2d5c1ff
commit 84c566bb05

View File

@@ -340,20 +340,23 @@ bool wxSpinCtrl::Create(wxWindow *parent,
if (!m_hasFont) if (!m_hasFont)
SetFont(GetDefaultAttributes().font); SetFont(GetDefaultAttributes().font);
// finally deal with the size, now that both windows are created and the // Finally deal with the size: notice that this can only be done now both
// font is set // windows are created and the text one is set up as buddy because
const wxSize sizeBtn = wxSpinButton::DoGetBestSize(); // UDM_SETBUDDY changes its size using some unknown algorithm, so setting
// the sizes earlier is useless.
const int bestSpinWidth = wxSpinButton::DoGetBestSize().x;
const int effectiveSpinWidth = bestSpinWidth - GetOverlap();
wxSize sizeCtrl(size); wxSize sizeCtrl(size);
if ( sizeCtrl.x <= 0 ) if ( sizeCtrl.x <= 0 )
{ {
// DEFAULT_ITEM_WIDTH is the default width for the text control // DEFAULT_ITEM_WIDTH is the default width for the text control
sizeCtrl.x = FromDIP(DEFAULT_ITEM_WIDTH) + sizeBtn.x - GetOverlap(); sizeCtrl.x = FromDIP(DEFAULT_ITEM_WIDTH) + effectiveSpinWidth;
} }
else if ( sizeCtrl.x <= sizeBtn.x ) else if ( sizeCtrl.x <= effectiveSpinWidth )
{ {
wxLogDebug(wxS("wxSpinCtrl \"%s\": initial width %d is too small, ") wxLogDebug(wxS("wxSpinCtrl \"%s\": initial width %d is too small, ")
wxS("at least %d pixels needed."), wxS("at least %d pixels needed."),
name, size.x, sizeBtn.x); name, size.x, effectiveSpinWidth);
} }
// adjust an invalid height for text control // adjust an invalid height for text control
@@ -365,6 +368,7 @@ bool wxSpinCtrl::Create(wxWindow *parent,
sizeCtrl.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); sizeCtrl.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
} }
// This will call our DoMoveWindow() and lay out the windows correctly.
SetInitialSize(sizeCtrl); SetInitialSize(sizeCtrl);
(void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);