From 6e803a4c0e8ec64c4764532b0e144e1f003f25d1 Mon Sep 17 00:00:00 2001 From: scheffle Date: Fri, 5 Jan 2024 11:03:38 +0100 Subject: [PATCH] Merge pull request #317 from steinbergmedia/bugfix/coptionmenu-get-range-and-max fix COptionMenu().getMax() (cherry picked from commit 24f79738c06120caed6d2609c9033696c713510e) --- vstgui/lib/controls/coptionmenu.cpp | 8 ++++++++ vstgui/lib/controls/coptionmenu.h | 2 +- vstgui/tests/unittest/lib/controls/coptionmenu_test.cpp | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/vstgui/lib/controls/coptionmenu.cpp b/vstgui/lib/controls/coptionmenu.cpp index 415bd1df1..82c69ba46 100644 --- a/vstgui/lib/controls/coptionmenu.cpp +++ b/vstgui/lib/controls/coptionmenu.cpp @@ -888,6 +888,14 @@ void COptionMenu::setValue (float val) setDirty (); } +//------------------------------------------------------------------------ +float COptionMenu::getMax () const +{ + if (menuItems->empty ()) + return 0.f; + return static_cast (menuItems->size () - 1); +} + //------------------------------------------------------------------------ void COptionMenu::takeFocus () { diff --git a/vstgui/lib/controls/coptionmenu.h b/vstgui/lib/controls/coptionmenu.h index b0acc7747..d4277db6a 100644 --- a/vstgui/lib/controls/coptionmenu.h +++ b/vstgui/lib/controls/coptionmenu.h @@ -307,7 +307,7 @@ class COptionMenu : public CParamDisplay void setMin (float val) override {} float getMin () const override { return 0; } void setMax (float val) override {} - float getMax () const override { return (float)(menuItems->size () - 1); } + float getMax () const override; void draw (CDrawContext* pContext) override; CMouseEventResult onMouseDown (CPoint& where, const CButtonState& buttons) override; diff --git a/vstgui/tests/unittest/lib/controls/coptionmenu_test.cpp b/vstgui/tests/unittest/lib/controls/coptionmenu_test.cpp index bc3a941dc..27c76275c 100644 --- a/vstgui/tests/unittest/lib/controls/coptionmenu_test.cpp +++ b/vstgui/tests/unittest/lib/controls/coptionmenu_test.cpp @@ -49,4 +49,10 @@ TEST_CASE (CCommandMenuItemTest, DescConstructor3) EXPECT_EQ (item.getCommandName (), "CommandNme"); } +TEST_CASE (COptionMenuTest, GetMaxWhenEmpty) +{ + COptionMenu menu; + EXPECT_EQ (menu.getMax (), 0.f); +} + } // VSTGUI