As long as my main menu bar class is used as the main component, as demonstrated in MenusDemo.h, menu items are available as expected.
class MainMenu: public MenuBarComponent,
public ApplicationCommandTarget,
public MenuBarModel
...
MainWindow (String name) (...)
{
...
setContentOwned (new MainMenu(), true);
...
}
However, when I create a new MainComponent and add an instance of MainMenu to it…
class MainComponent : public AudioAppComponent
{
public:
MainComponent() : mainMenu()
{
...
addChildComponent(mainMenu);
...
}
void resized() override
{
...
mainMenu.setBounds(getBounds());
}
private:
...
MainMenu mainMenu;
...
};
MainWindow (String name) (...)
{
...
setContentOwned (new MainComponent(), true);
...
}
…then all menu items except for the bar names are disabled.
I noticed that in PopupMenu::addCommandItem (juce_PopupMenu.cpp) in line 1422
auto* target = commandManager->getTargetForCommand (commandID, info);
target is defined in the first case but not defined in the second case.
In
ApplicationCommandManager::getTargetForCommand
nullptr is returned by
auto* target = getFirstCommandTarget (commandID);
if (target == nullptr)
target = JUCEApplication::getInstance();
if (target != nullptr)
target = target->getTargetForCommand (commandID); //returns nullptr!
So it seems to be a problem with ApplicationCommandManager.
Since the MainMenu class works works fine if directly used as the main component, I suppose I need to forward something to it when using it in MainComponent.
What am I doing wrong / missing?
Thanks a lot!
P.S.: commandIDs seems to be wrong - it is {4097} but should be {1,2,3,4,5,6,7,8}.
P.P.S.: In the DialogWindow Class Reference it says that
Any of the methods available to a DocumentWindow or ResizableWindow are also available to this, so it can be made resizable, have a menu bar, etc.
Does that mean that only ‘window’ objects are supposed to have menu bars?
P.P.P.S.: The menu seems to work when doing
class MainComponent : public AudioAppComponent, public MainMenu
but since MainComponent also has
private: MainMenu mainMenu;
this seems to be a bad solution…
Maybe… do I have to move the ApplicationCommandTarget to MainComponent?
