From 7c8ba45705353ca23cd490f14ce74594a360753e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 24 Feb 2019 18:19:49 +0100 Subject: [PATCH] Reuse helper MSWGetFittingtSizeForControl() in a couple of places Refactor the code to reuse the same function for determining the size needed by an embedded control in the toolbar, including its label. --- include/wx/msw/toolbar.h | 4 ++++ src/msw/toolbar.cpp | 35 +++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/include/wx/msw/toolbar.h b/include/wx/msw/toolbar.h index ff5da800c5..ba3581515b 100644 --- a/include/wx/msw/toolbar.h +++ b/include/wx/msw/toolbar.h @@ -183,6 +183,10 @@ private: return HasFlag(wxTB_TEXT) && !HasFlag(wxTB_NOICONS); } + // Return the size required to accommodate the given tool which must be of + // "control" type. + wxSize MSWGetFittingtSizeForControl(class wxToolBarTool* tool) const; + wxDECLARE_EVENT_TABLE(); wxDECLARE_DYNAMIC_CLASS(wxToolBar); diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index 9f60b5b773..a9f9dfb6b3 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -530,6 +530,31 @@ wxToolBar::~wxToolBar() delete m_disabledImgList; } +wxSize wxToolBar::MSWGetFittingtSizeForControl(wxToolBarTool* tool) const +{ + wxSize size = tool->GetControl()->GetBestSize(); + + // Account for the label, if any. + if ( wxStaticText * const staticText = tool->GetStaticText() ) + { + if ( AreControlLabelsShown() ) + { + const wxSize sizeLabel = staticText->GetSize(); + + if ( size.x < sizeLabel.x ) + size.x = sizeLabel.x; + + size.y += sizeLabel.y; + size.y += MARGIN_CONTROL_LABEL; + } + } + + // Also account for the tool padding value. + size += wxSize(m_toolPacking, m_toolPacking); + + return size; +} + wxSize wxToolBar::DoGetBestSize() const { wxSize sizeBest; @@ -578,10 +603,8 @@ wxSize wxToolBar::DoGetBestSize() const tool = static_cast(node->GetData()); if (tool->IsControl()) { - int y = tool->GetControl()->GetSize().y; - // Approximate border size - if (y > (sizeBest.y - 4)) - sizeBest.y = y + 4; + // Ensure we're tall enough for the embedded controls. + sizeBest.IncTo(wxSize(-1, MSWGetFittingtSizeForControl(tool).y)); } } @@ -1057,8 +1080,8 @@ bool wxToolBar::Realize() // taking into account tool padding value. // (height is not used but it is set for the sake of consistency). { - const wxSize sizeControl = tool->GetControl()->GetSize(); - button.iBitmap = m_toolPacking + (IsVertical() ? sizeControl.y : sizeControl.x); + const wxSize size = MSWGetFittingtSizeForControl(tool); + button.iBitmap = size.x; } wxFALLTHROUGH;