From 5e3af71d64eb325f78fcdafadd8a81d3b15f8ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 9 Nov 2005 15:39:38 +0000 Subject: [PATCH] More or less correctly implemented GetPreferredSize(). The menu bar is now resized to a proper size. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14790 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/MenuField.cpp | 43 ++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/kits/interface/MenuField.cpp b/src/kits/interface/MenuField.cpp index e4b748bf2f..03e665bf05 100644 --- a/src/kits/interface/MenuField.cpp +++ b/src/kits/interface/MenuField.cpp @@ -465,9 +465,39 @@ void BMenuField::ResizeToPreferred() BView::ResizeToPreferred(); } //------------------------------------------------------------------------------ -void BMenuField::GetPreferredSize(float *width, float *height) +void +BMenuField::GetPreferredSize(float *_width, float *_height) { - BView::GetPreferredSize(width, height); + BView::GetPreferredSize(_width, _height); + + if (!fFixedSizeMB) { + BMenu* menu = Menu(); + float width = 0; + + if (menu != NULL) { + if (menu->IsLabelFromMarked()) { + // find the width of the largest item + for (int32 i = menu->CountItems(); i-- > 0;) { + BMenuItem* item = menu->ItemAt(i); + + if (item != NULL && item->Label() != NULL) { + float labelWidth = StringWidth(item->Label()); + if (labelWidth > width) + width = labelWidth; + } + } + } else if (menu->Superitem() != NULL) + width = StringWidth(menu->Superitem()->Label()); + } + + // TODO: fix these values (they should match the visual appearance) + *_width = width + 45 + fDivider; + + if (Label()) + *_width += StringWidth(Label()) + 5; + } + + *_height = fMenuBar->Bounds().Height(); } //------------------------------------------------------------------------------ status_t BMenuField::Perform(perform_code d, void *arg) @@ -508,6 +538,15 @@ void BMenuField::InitObject(const char *label) void BMenuField::InitObject2() { // TODO set filter + + font_height fontHeight; + GetFontHeight(&fontHeight); + + // TODO: fix this calculation + float height = floorf(fontHeight.ascent + fontHeight.descent + + fontHeight.leading) + 7; + + fMenuBar->ResizeTo(Bounds().Width() - fDivider, height); } //------------------------------------------------------------------------------ void BMenuField::DrawLabel(BRect bounds, BRect update)