From c1a7e89fc235363e91d79eb547e92e9e16efd5e5 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Mon, 6 May 2013 17:55:33 -0400 Subject: [PATCH] Put the label truncation code back in BMenuItem. Just a few commits ago I moved the label truncation code out of BMenuItem and into BMCMenuBar because the truncation had to happen outside of BMenuItem. Turns out, that wasn't true so I'm moving the label truncation back into BMenuItem and removing the _DrawItems() method from BMCMenuBar. Note that the code is not a copy of what was there before, but, the updated version I created for BMCMenuBar. The main difference is that I use menuPrivate.Padding() instead of GetItemMargins() and I always use the width of the parent menu frame instead of using fBounds even if the state is not MENU_STATE_CLOSED. These are changes needed for BMCMenuBar but should work just as well for a regular BMenu. --- headers/private/interface/BMCPrivate.h | 2 - src/kits/interface/BMCPrivate.cpp | 54 -------------------------- src/kits/interface/MenuItem.cpp | 17 +++++++- 3 files changed, 16 insertions(+), 57 deletions(-) diff --git a/headers/private/interface/BMCPrivate.h b/headers/private/interface/BMCPrivate.h index f3fea82fc2..3fe04cdaf3 100644 --- a/headers/private/interface/BMCPrivate.h +++ b/headers/private/interface/BMCPrivate.h @@ -62,8 +62,6 @@ private: void _Init(bool setMaxContentWidth); - void _DrawItems(BRect updateRect); - BMenuField* fMenuField; bool fFixedSize; BMessageRunner* fRunner; diff --git a/src/kits/interface/BMCPrivate.cpp b/src/kits/interface/BMCPrivate.cpp index 09d99b5139..d3743b12ca 100644 --- a/src/kits/interface/BMCPrivate.cpp +++ b/src/kits/interface/BMCPrivate.cpp @@ -336,57 +336,3 @@ _BMCMenuBar_::_Init(bool setMaxContentWidth) if (setMaxContentWidth) SetMaxContentWidth(fPreviousWidth - (left + right)); } - - -void -_BMCMenuBar_::_DrawItems(BRect updateRect) -{ - MenuPrivate menuPrivate(this); - menuPrivate.CacheFontInfo(); - const BRect& padding = menuPrivate.Padding(); - float frameWidth = fMenuField->_MenuBarWidth() - - (padding.left + padding.right); - int32 itemCount = CountItems(); - - for (int32 i = 0; i < itemCount; i++) { - BMenuItem* item = ItemAt(i); - if (item == NULL) - continue; - - if (!item->Frame().Intersects(updateRect)) - continue; - - const char* label = item->Label(); - if (label == NULL) - continue; - - BPoint contentLocation(item->Frame().left + padding.left, - item->Frame().top + padding.top); - MovePenTo(contentLocation); - MovePenBy(0, menuPrivate.Ascent()); - - if (item->IsEnabled()) - SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR)); - else - SetHighColor(tint_color(LowColor(), B_DISABLED_LABEL_TINT)); - - SetDrawingMode(B_OP_OVER); - - if (frameWidth >= StringWidth(label)) - DrawString(label); - else { - // truncate label to fit - char* truncatedLabel = new char[strlen(label) + 4]; - BFont font; - GetFont(&font); - BString labelString(label); - font.TruncateString(&labelString, B_TRUNCATE_MIDDLE, frameWidth); - labelString.CopyInto(truncatedLabel, 0, labelString.Length()); - truncatedLabel[labelString.Length()] = '\0'; - DrawString(truncatedLabel); - delete[] truncatedLabel; - } - - SetDrawingMode(B_OP_COPY); - } -} diff --git a/src/kits/interface/MenuItem.cpp b/src/kits/interface/MenuItem.cpp index fb79e1bc8f..c27645e336 100644 --- a/src/kits/interface/MenuItem.cpp +++ b/src/kits/interface/MenuItem.cpp @@ -401,7 +401,22 @@ BMenuItem::DrawContent() fSuper->SetDrawingMode(B_OP_OVER); - fSuper->DrawString(fLabel); + float labelWidth; + float labelHeight; + GetContentSize(&labelWidth, &labelHeight); + + const BRect& padding = menuPrivate.Padding(); + float frameWidth = fSuper->Frame().Width() - padding.left - padding.right; + + if (frameWidth >= labelWidth) + fSuper->DrawString(fLabel); + else { + // truncate label to fit + char* truncatedLabel = new char[strlen(fLabel) + 4]; + TruncateLabel(frameWidth, truncatedLabel); + fSuper->DrawString(truncatedLabel); + delete[] truncatedLabel; + } if (fSuper->AreTriggersEnabled() && fTriggerIndex != -1) { float escapements[fTriggerIndex + 1];