From 5f88744263675d2d6307d5870114d26099aebd8c Mon Sep 17 00:00:00 2001 From: PulkoMandy Date: Sat, 18 Feb 2023 13:26:22 +0100 Subject: [PATCH] BMenuField: allow to use non-fixed size in combination with layout mode In fixed size mode, the menu field always uses all the available width. In non-fixed size mode, the menu field will resize itself to be as small as possible. With layout, usually the difference isn't noticeable, since the layout will already try to resize the control to the smallest possible size. But there are a few cases where it makes a difference, when the layout is over-constrained and the menu field can't be made as small as possible. In that case, the menu field would be forced to fill its allocated space, where we can instead make it a little smaller. Change-Id: I911d497218a09aab3824865968558df5d4b3cf98 Reviewed-on: https://review.haiku-os.org/c/haiku/+/6076 Reviewed-by: waddlesplash Reviewed-by: John Scipione Reviewed-by: Adrien Destugues --- headers/os/interface/MenuField.h | 4 ++++ src/kits/interface/BMCPrivate.cpp | 30 ++++++++++++++++-------------- src/kits/interface/MenuField.cpp | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/headers/os/interface/MenuField.h b/headers/os/interface/MenuField.h index 5963e7138c..c9e531f773 100644 --- a/headers/os/interface/MenuField.h +++ b/headers/os/interface/MenuField.h @@ -27,6 +27,10 @@ public: BMenuField(const char* name, const char* label, BMenu* menu, uint32 flags = B_WILL_DRAW | B_NAVIGABLE); + BMenuField(const char* name, + const char* label, BMenu* menu, + bool fixed_size, + uint32 flags = B_WILL_DRAW | B_NAVIGABLE); BMenuField(const char* label, BMenu* menu, uint32 flags = B_WILL_DRAW | B_NAVIGABLE); BMenuField(BMessage* data); diff --git a/src/kits/interface/BMCPrivate.cpp b/src/kits/interface/BMCPrivate.cpp index 6e6ca327e4..6db8a248e2 100644 --- a/src/kits/interface/BMCPrivate.cpp +++ b/src/kits/interface/BMCPrivate.cpp @@ -179,20 +179,22 @@ _BMCMenuBar_::AttachedToWindow() void _BMCMenuBar_::Draw(BRect updateRect) { - if (fFixedSize) { - // Set the width of the menu bar because the menu bar bounds may have - // been expanded by the selected menu item. - ResizeTo(fMenuField->_MenuBarWidth(), Bounds().Height()); - } else { - // For compatability with BeOS R5: - // - Set to the minimum of the menu bar width set by the menu frame - // and the selected menu item width. - // - Set the height to the preferred height ignoring the height of the - // menu field. - float height; - BMenuBar::GetPreferredSize(NULL, &height); - ResizeTo(std::min(Bounds().Width(), fMenuField->_MenuBarWidth()), - height); + if ((Flags() & B_SUPPORTS_LAYOUT) == 0) { + if (fFixedSize) { + // Set the width of the menu bar because the menu bar bounds may have + // been expanded by the selected menu item. + ResizeTo(fMenuField->_MenuBarWidth(), Bounds().Height()); + } else { + // For compatability with BeOS R5: + // - Set to the minimum of the menu bar width set by the menu frame + // and the selected menu item width. + // - Set the height to the preferred height ignoring the height of the + // menu field. + float height; + BMenuBar::GetPreferredSize(NULL, &height); + ResizeTo(std::min(Bounds().Width(), fMenuField->_MenuBarWidth()), + height); + } } BRect rect(Bounds()); diff --git a/src/kits/interface/MenuField.cpp b/src/kits/interface/MenuField.cpp index 870e6b446f..5c7f4f30c7 100644 --- a/src/kits/interface/MenuField.cpp +++ b/src/kits/interface/MenuField.cpp @@ -247,6 +247,21 @@ BMenuField::BMenuField(const char* name, const char* label, BMenu* menu, } +BMenuField::BMenuField(const char* name, const char* label, BMenu* menu, + bool fixedSize, uint32 flags) + : + BView(name, flags | B_FRAME_EVENTS) +{ + InitObject(label); + + fFixedSizeMB = fixedSize; + + _InitMenuBar(menu, BRect(0, 0, 100, 15), fixedSize); + + InitObject2(); +} + + BMenuField::BMenuField(const char* label, BMenu* menu, uint32 flags) : BView(NULL, flags | B_FRAME_EVENTS)