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.
This commit is contained in:
John Scipione 2013-05-06 17:55:33 -04:00
parent 13cec30211
commit c1a7e89fc2
3 changed files with 16 additions and 57 deletions

View File

@ -62,8 +62,6 @@ private:
void _Init(bool setMaxContentWidth);
void _DrawItems(BRect updateRect);
BMenuField* fMenuField;
bool fFixedSize;
BMessageRunner* fRunner;

View File

@ -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);
}
}

View File

@ -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];