diff --git a/src/CardinalUI.cpp b/src/CardinalUI.cpp index cdda709..33f37dc 100644 --- a/src/CardinalUI.cpp +++ b/src/CardinalUI.cpp @@ -17,10 +17,12 @@ #include #include +#include #include #include #include #include +#include #include #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(*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(*it)) { - for (rWidgetIterator it2 = button->children.begin(); it2 != button->children.end(); ++it2) + if (button->text == name) { - if (rMenuItem* const item = reinterpret_cast(*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(*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); diff --git a/src/Makefile b/src/Makefile index 67ad6ed..93330ec 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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)) # -------------------------------------------------------------- diff --git a/src/context.cpp b/src/context.cpp new file mode 100644 index 0000000..3efdb25 --- /dev/null +++ b/src/context.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include + +#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