Custom file menu

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2021-10-22 13:26:07 +01:00
parent 08b6a8ef8b
commit 0a0f09bc8a
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
3 changed files with 164 additions and 21 deletions

View file

@ -17,10 +17,12 @@
#include <app/Scene.hpp>
#include <context.hpp>
#include <helpers.hpp>
#include <patch.hpp>
#include <settings.hpp>
#include <ui/Button.hpp>
#include <ui/MenuItem.hpp>
#include <ui/MenuSeparator.hpp>
#include <window/Window.hpp>
#ifdef NDEBUG
@ -48,8 +50,70 @@ START_NAMESPACE_DISTRHO
// -----------------------------------------------------------------------------------------------------------
struct CardinalMenuButton : rack::ui::Button {
void step() override {
box.size.x = bndLabelWidth(APP->window->vg, -1, text.c_str()) + 1.0;
Widget::step();
}
void draw(const DrawArgs& args) override {
BNDwidgetState state = BND_DEFAULT;
if (APP->event->hoveredWidget == this)
state = BND_HOVER;
if (APP->event->draggedWidget == this)
state = BND_ACTIVE;
bndMenuItem(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str());
Widget::draw(args);
}
};
struct CardinalFileButton : CardinalMenuButton {
CardinalFileButton()
: CardinalMenuButton()
{
text = "File";
}
void onAction(const ActionEvent& e) override {
rack::ui::Menu* menu = rack::createMenu();
menu->cornerFlags = BND_CORNER_TOP;
menu->box.pos = getAbsoluteOffset(rack::math::Vec(0, box.size.y));
menu->addChild(rack::createMenuItem("New", RACK_MOD_CTRL_NAME "+N", []() {
APP->patch->loadTemplateDialog();
}));
menu->addChild(rack::createMenuItem("Open", RACK_MOD_CTRL_NAME "+O", []() {
APP->patch->loadDialog();
}));
/*
menu->addChild(rack::createMenuItem("Save", RACK_MOD_CTRL_NAME "+S", []() {
APP->patch->saveDialog();
}));
menu->addChild(rack::createMenuItem("Save as", RACK_MOD_CTRL_NAME "+Shift+S", []() {
APP->patch->saveAsDialog();
}));
*/
menu->addChild(rack::createMenuItem("Revert", RACK_MOD_CTRL_NAME "+" RACK_MOD_SHIFT_NAME "+O", []() {
APP->patch->revertDialog();
}, APP->patch->path == ""));
menu->addChild(new rack::ui::MenuSeparator);
menu->addChild(rack::createMenuItem("Quit", RACK_MOD_CTRL_NAME "+Q", []() {
APP->window->close();
}));
}
};
// -----------------------------------------------------------------------------------------------------------
CardinalPluginContext* getRackContextFromPlugin(void* ptr);
// -----------------------------------------------------------------------------------------------------------
class CardinalUI : public UI,
public WindowParametersCallback
{
@ -116,36 +180,45 @@ public:
rWidget* const layout = fContext->scene->menuBar->children.front();
for (rWidgetIterator it = layout->children.begin(); it != layout->children.end(); ++it)
const auto removeMenu = [layout](const char* const name) -> void
{
if (rButton* const button = reinterpret_cast<rButton*>(*it))
for (rWidgetIterator it = layout->children.begin(); it != layout->children.end(); ++it)
{
/* FIXME this doesnt work
if (button->text == "Engine")
if (rButton* const button = reinterpret_cast<rButton*>(*it))
{
for (rWidgetIterator it2 = button->children.begin(); it2 != button->children.end(); ++it2)
if (button->text == name)
{
if (rMenuItem* const item = reinterpret_cast<rMenuItem*>(*it2))
{
if (item->text == "Sample rate")
{
button->children.erase(it2);
delete button;
break;
}
}
layout->removeChild(button);
// button->parent = nullptr;
delete button;
break;
}
}
*/
if (button->text == "Library")
}
};
removeMenu("File");
removeMenu("Library");
layout->addChildBottom(new CardinalFileButton());
/* FIXME this doesnt work
if (button->text == "Engine")
{
for (rWidgetIterator it2 = button->children.begin(); it2 != button->children.end(); ++it2)
{
if (rMenuItem* const item = reinterpret_cast<rMenuItem*>(*it2))
{
layout->children.erase(it);
button->parent = nullptr;
delete button;
break;
if (item->text == "Sample rate")
{
button->children.erase(it2);
delete button;
break;
}
}
}
}
*/
}
WindowParametersSetCallback(fContext->window, this);

View file

@ -24,6 +24,7 @@ FILES_DSP = \
FILES_UI = \
CardinalUI.cpp \
context.cpp \
dep.cpp \
Window.cpp
@ -51,9 +52,21 @@ else
FILES_UI += Rack/dep/osdialog/osdialog_zenity.c
endif
IGNORED_FILES = Rack/src/asset.cpp
IGNORED_FILES += Rack/src/common.cpp
IGNORED_FILES += Rack/src/context.cpp
IGNORED_FILES += Rack/src/dep.cpp
IGNORED_FILES += Rack/src/discord.cpp
IGNORED_FILES += Rack/src/gamepad.cpp
IGNORED_FILES += Rack/src/keyboard.cpp
IGNORED_FILES += Rack/src/library.cpp
IGNORED_FILES += Rack/src/network.cpp
IGNORED_FILES += Rack/src/rtaudio.cpp
IGNORED_FILES += Rack/src/rtmidi.cpp
FILES_DSP += $(wildcard Rack/src/*.c)
FILES_DSP += $(wildcard Rack/src/*/*.c)
FILES_DSP += $(filter-out Rack/src/asset.cpp Rack/src/common.cpp Rack/src/dep.cpp Rack/src/discord.cpp Rack/src/gamepad.cpp Rack/src/keyboard.cpp Rack/src/library.cpp Rack/src/network.cpp Rack/src/rtaudio.cpp Rack/src/rtmidi.cpp, $(wildcard Rack/src/*.cpp))
FILES_DSP += $(filter-out $(IGNORED_FILES),$(wildcard Rack/src/*.cpp))
FILES_DSP += $(filter-out Rack/src/window/Window.cpp, $(wildcard Rack/src/*/*.cpp))
# --------------------------------------------------------------

57
src/context.cpp Normal file
View file

@ -0,0 +1,57 @@
#include <context.hpp>
#include <window/Window.hpp>
#include <patch.hpp>
#include <engine/Engine.hpp>
#include <app/Scene.hpp>
#include <history.hpp>
#include <settings.hpp>
#include "DistrhoPluginUtils.hpp"
namespace rack {
Context::~Context() {
// Deleting NULL is safe in C++.
// Set pointers to NULL so other objects will segfault when attempting to access them
delete window;
window = NULL;
delete patch;
patch = NULL;
delete scene;
scene = NULL;
delete event;
event = NULL;
delete history;
history = NULL;
delete engine;
engine = NULL;
}
static thread_local Context* threadContext = nullptr;
Context* contextGet() {
DISTRHO_SAFE_ASSERT(threadContext != nullptr);
return threadContext;
}
// Apple's clang incorrectly compiles this function when -O2 or higher is enabled.
#ifdef ARCH_MAC
__attribute__((optnone))
#endif
void contextSet(Context* const context) {
// DISTRHO_SAFE_ASSERT(threadContext == nullptr);
threadContext = context;
}
} // namespace rack