Hi,
ComboBox class is very useful to expose a user choice from a list of values in a small GUI space.
But sometime there is a lot of choices and classical popup menu is not very user friendly.
(When PopupMenu is bigger than screen for instance)
To resolve this issue, it can be interesting to allow ComboBox’s menu customization.
The actual ComboBox interface do not permit it, but with the following modification it’s working nicely:
First move the declaration of private struct ItemInfo in a protected block and add to ComboBox a new virtual method with the following prototype:
Then modify ComboBox::showPopup method to call the default createPopupMenu implementation:
[code]void ComboBox::showPopup()
{
if (! menuActive)
{
const int selectedId = getSelectedId();
PopupMenu menu;
menu.setLookAndFeel (&getLookAndFeel());
if (items.size() == 0)
menu.addItem (1, noChoicesMessage, false);
else
createPopupMenu(menu, items); //Construct the popup menu here.
menuActive = true;
menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
.withItemThatMustBeVisible (selectedId)
.withMinimumWidth (getWidth())
.withMaximumNumColumns (1)
.withStandardItemHeight (jlimit (12, 24, getHeight())),
ModalCallbackFunction::forComponent (popupMenuFinishedCallback, this));
}
}[/code]
The PopupMenu related code of showPopup methods is moved into createPopupMenu default implementation.
[code]void ComboBox::createPopupMenu(PopupMenu& menu, const OwnedArray& items)
{
const int selectedId = getSelectedId();
for (int i = 0; i < items.size(); ++i)
{
const ItemInfo* const item = items.getUnchecked(i);
if (item->isSeparator())
menu.addSeparator();
else if (item->isHeading)
menu.addSectionHeader (item->name);
else
menu.addItem (item->itemId, item->name,
item->isEnabled, item->itemId == selectedId);
}
}[/code]
Now the ComboBox child classes can extend createPopupMenu method.
For instance, they can create menu with sub menu when special prefixes or separator character are found in item->name.
This allow having smaller popup with grouped item choices.
If I’m not the only one interested in this feature, it can be great having something like that in future JUCE release.
Thanks in advance.