Factor out wxMenuItem::GetMenuTextExtent() helper

The code of this function was used in two places, factor it out into
just a single one before modifying it -- see upcoming commit.

No real changes yet.
This commit is contained in:
Vadim Zeitlin 2022-11-10 13:43:31 +00:00
parent 0515ccf3a1
commit a240aa8cfe
2 changed files with 15 additions and 18 deletions

View file

@ -137,6 +137,9 @@ private:
// position (which is not really supposed to ever happen).
int MSGetMenuItemPos() const;
// Get the extent of the given text using the correct font.
wxSize GetMenuTextExtent(const wxString& text) const;
// item bitmaps
wxBitmapBundle m_bmpUnchecked; // (used only for checkable items)
#if wxUSE_OWNER_DRAWN

View file

@ -778,19 +778,21 @@ void wxMenuItem::SetupBitmaps()
#if wxUSE_OWNER_DRAWN
int wxMenuItem::MeasureAccelWidth() const
wxSize wxMenuItem::GetMenuTextExtent(const wxString& text) const
{
wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
wxMemoryDC dc;
wxFont font;
GetFontToUse(font);
dc.SetFont(font);
wxCoord w;
dc.GetTextExtent(accel, &w, nullptr);
return dc.GetTextExtent(text);
}
return w;
int wxMenuItem::MeasureAccelWidth() const
{
wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
return GetMenuTextExtent(accel).x;
}
wxString wxMenuItem::GetName() const
@ -816,20 +818,12 @@ bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height)
return true;
}
wxString str = GetName();
const wxSize extent = GetMenuTextExtent(GetName());
wxMemoryDC dc;
wxFont font;
GetFontToUse(font);
dc.SetFont(font);
*width = data->TextBorder + extent.x + data->AccelBorder;
*height = extent.y;
wxCoord w, h;
dc.GetTextExtent(str, &w, &h);
*width = data->TextBorder + w + data->AccelBorder;
*height = h;
w = m_parentMenu->GetMaxAccelWidth();
int w = m_parentMenu->GetMaxAccelWidth();
if ( w > 0 )
*width += w + data->ArrowBorder;