Hey guys, JUCE noob here. I am working on a standalone application. but after 3 days I am still not able to get the basic menu bar to work. I’ve read the MenusDemo and other things from forums which did not help me, I need to get a menu bar up and working, i.e the menu names should be visible, and there should be a pop-up when pressed n one of them, a list should be shown whose items on pressed should call a function.This is the code i have still now, which is only creating a small slit at the top for menu(probably because of the Component class) but nothing related to menus is being shown, any help would be really appreciated :
MenuComponent.h
#pragma once
#include <JuceHeader.h>
class MenuComponent : public juce::MenuBarModel,
public juce::Component {
public:
MenuComponent();
~MenuComponent() override;
juce::StringArray getMenuBarNames() override;
juce::PopupMenu getMenuForIndex(int topLevelMenuIndex, const juce::String& menuName) override;
void menuItemSelected(int menuItemID, int topLevelMenuIndex) override;
void paint(juce::Graphics& g) override;
void resized() override;
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenuComponent)
};
MenuComponent.cpp :
#include "MenuComponent.h"
MenuComponent::MenuComponent() {
setSize(getParentWidth(), 20);
setVisible(true);
}
MenuComponent::~MenuComponent() {}
void MenuComponent::paint(juce::Graphics& g) {
g.fillAll(juce::Colours::white);
g.setFont (juce::Font (16.0f));
g.setColour (juce::Colours::black);
}
void MenuComponent::resized() {
setSize(getParentWidth(), 20);
}
juce::StringArray MenuComponent::getMenuBarNames() {
return {"Instrument", "Settings", "Edit"};
}
juce::PopupMenu MenuComponent::getMenuForIndex(int topLevelMenuIndex, const juce::String& menuName) {
juce::PopupMenu menu;
if (menuName == "Instrument") {
menu.addItem(1, "Initialize");
menu.addSeparator();
menu.addItem(2, "Load Instrument");
menu.addItem(3, "Save Instrument");
menu.addItem(4, "Save Instrument as");
menu.addSeparator();
menu.addItem(5, "Exit");
} else if (menuName == "Settings") {
menu.addItem(15, "Theme");
menu.addItem(16, "Scale+");
menu.addItem(17, "Scale-");
} else if (menuName == "Edit") {
menu.addItem(21, "undo");
}
return menu;
}
void MenuComponent::menuItemSelected(int menuItemID, int topLevelMenuIndex) {
switch (menuItemID) {
case 1:
std::cout << "Initialize selected";
break;
case 5:
std::cout << "Exit selected";
break;
default:
break;
}
}
MainComponent.cpp :
#include "MainComponent.h"
#include "MenuComponent.h"
#include <memory.h>
MainComponent::MainComponent() {
setSize (1200, 800);
this->menuBarComponent = std::make_unique<MenuComponent>();
addChildComponent(this->menuBarComponent.get());
}
void MainComponent::paint (juce::Graphics& g) {
g.fillAll (juce::Colours::brown);
g.setFont (juce::Font (16.0f));
g.setColour (juce::Colours::blanchedalmond);
}
void MainComponent::resized() {}
The Main.cpp is exactly left as how it is created for juce GUI, from projuicer.
I am using JUCE version 7.0.9 on linuxmint(Cinnamon)