Disabling items in menubar

Hi,

I’m using a simple main menu (File, Edit, View, About, etc…) in my JUCE app with a mainmenubar model that contains all my menu entries and sub-entries.

I have an undo/redo system and it also keeps track of if the user has made any changes since the last save to file.

If changes were made, the user can select File → Save to save it to the file saved to before or opened from before. If the user tries to exit the app, the app will ask if you want to save changes or not before exiting.

If no changes have been made, there’s no need to do a File → Save, so I would like to have the File → Save menu entry disabled. (eg I’ll make it a darker colour and it should’nt be selectable). This is what many commercial windows applications do. They also won’t ask to do a save on exitting the app.

So, how can I easily change one of my existing menubarmodel items (in this case File → Save) to look disabled and it being unselectable ?

Here’s a snippet of code for my menubarmodel:

juce::StringArray MainMenuBarComponent::getMenuBarNames()
{
return juce::StringArray(“File”,“Edit”, “View”, “About”);
}

juce::PopupMenu MainMenuBarComponent::getMenuForIndex(int n, const juce::String& menuName)
{
PopupMenu menu;

if (menuName == "File")
{
    menu.addItem(1, "New...");
    menu.addItem(2, "Open...");
    menu.addItem(3, "Save");
    menu.addItem(4, "Save as...");
    menu.addItem(5, "Exit");
}
else if (menuName == "Edit")
{
    menu.addItem(6, "Undo");
    menu.addItem(7, "Redo");
    menu.addItem(8, "Project Preferences...");
}
else if (menuName == "View")
{
    menu.addItem(9, "Zoom In");
    menu.addItem(10, "Zoom Out");
}
else if (menuName == "About")
{
    menu.addItem(11, "About Hummingbird...");
}

return menu;

}

void MainMenuBarComponent::menuItemSelected(int menuItemID, int topLevelMenuIndex)
{
switch (menuItemID)
{
case 1: // New…
rootManager->NewProject(true);
break;
case 2: // Open…
rootManager->LoadProjectFromFile();
break;
case 3: // Save
rootManager->SaveProjectToFile();
break;
case 4: // Save as…
rootManager->SaveAsProjectToFile();
break;

That’s it…

Thanks for your time, it’s much appreciated…

Cheers,
Terrence

Are you using the ApplicationCommandManager? If so, in your getCommandInfo() function, you can set the result that is passed in with result.setActive(false) if you want a particular menu item disabled.

However, from the code I suspect you are not. So in that case, you could make a function to update the menu items and use a PopupMenu::MenuItemIterator to loop through the items in your popups menus and set them to enabled/disabled with Item::setEnabled().

https://docs.juce.com/master/structPopupMenu_1_1Item.html#ac1cf39dd10c639161cd8e8562e508bee

You perhaps have not noticed that menu.addItem() has additional parameters.

juce::PopMenu::addItem(int itemResultID, juce::String itemText, bool isEnabled = true, bool isTicked = false)

The isEnabled parameter is the one you want.

No not using any applicationcommandmanager…

Thanks,

I manager to get it working with the additional bool isEnabled = true menuitem :slight_smile:

Thanks a lot, problem solved!,
Terrence