From 5c3367299e744e93291a120701a26abf918faa4f Mon Sep 17 00:00:00 2001 From: falkTX Date: Wed, 2 Feb 2022 19:48:06 +0000 Subject: [PATCH] TextEditor: Add undo, redo, cut, copy, paste actions Signed-off-by: falkTX --- plugins/Cardinal/src/ImGuiTextEditor.cpp | 70 +++++++++++++++--------- plugins/Cardinal/src/ImGuiTextEditor.hpp | 17 ++++-- plugins/Cardinal/src/TextEditor.cpp | 30 +++++++--- 3 files changed, 81 insertions(+), 36 deletions(-) diff --git a/plugins/Cardinal/src/ImGuiTextEditor.cpp b/plugins/Cardinal/src/ImGuiTextEditor.cpp index d0b0bc9..671cfff 100644 --- a/plugins/Cardinal/src/ImGuiTextEditor.cpp +++ b/plugins/Cardinal/src/ImGuiTextEditor.cpp @@ -133,6 +133,51 @@ std::string ImGuiTextEditor::getCurrentLineText()const return pData->editor.GetCurrentLineText(); } +bool ImGuiTextEditor::hasSelection() const +{ + return pData->editor.HasSelection(); +} + +void ImGuiTextEditor::selectAll() +{ + pData->editor.SelectAll(); +} + +void ImGuiTextEditor::copy() +{ + pData->editor.Copy(); +} + +void ImGuiTextEditor::cut() +{ + pData->editor.Cut(); +} + +void ImGuiTextEditor::paste() +{ + pData->editor.Paste(); +} + +bool ImGuiTextEditor::canUndo() const +{ + return pData->editor.CanUndo(); +} + +bool ImGuiTextEditor::canRedo() const +{ + return pData->editor.CanRedo(); +} + +void ImGuiTextEditor::undo() +{ + pData->editor.Undo(); +} + +void ImGuiTextEditor::redo() +{ + pData->editor.Redo(); +} + // -------------------------------------------------------------------------------------------------------------------- void ImGuiTextEditor::drawImGui() @@ -163,8 +208,6 @@ void ImGuiTextEditor::drawImGui() void ImGuiTextEditor::onButton(const ButtonEvent& e) { - setAsCurrentContext(); - // if mouse press is over the top status bar, do nothing so editor can be moved in the Rack if (e.action == GLFW_PRESS && e.pos.y < 25) return; @@ -181,26 +224,3 @@ void ImGuiTextEditor::onHoverScroll(const HoverScrollEvent& e) // if there is a scrollbar, handle the event ImGuiWidget::onHoverScroll(e); } - -/* -void ImGuiTextEditor::onSelectKey(const SelectKeyEvent& e) -{ - ImGuiWidget::onSelectKey(e); - - if (e.action == GLFW_PRESS && (e.mods & GLFW_MOD_CONTROL) != 0) - { - switch (e.key) - { - case GLFW_KEY_X: - pData->editor.Cut(); - break; - case GLFW_KEY_C: - pData->editor.Copy(); - break; - case GLFW_KEY_V: - pData->editor.Paste(); - break; - } - } -} -*/ diff --git a/plugins/Cardinal/src/ImGuiTextEditor.hpp b/plugins/Cardinal/src/ImGuiTextEditor.hpp index b393313..4dbdf36 100644 --- a/plugins/Cardinal/src/ImGuiTextEditor.hpp +++ b/plugins/Cardinal/src/ImGuiTextEditor.hpp @@ -1,6 +1,6 @@ /* * Syntax highlighting text editor (for ImGui in DPF, converted to VCV) - * Copyright (C) 2021 Filipe Coelho + * Copyright (C) 2021-2022 Filipe Coelho * Copyright (c) 2017 BalazsJako * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -56,12 +56,21 @@ struct ImGuiTextEditor : ImGuiWidget std::string getSelectedText() const; std::string getCurrentLineText()const; + bool hasSelection() const; + void selectAll(); + + void copy(); + void cut(); + void paste(); + + bool canUndo() const; + bool canRedo() const; + void undo(); + void redo(); + protected: /** @internal */ void drawImGui() override; void onButton(const ButtonEvent& e) override; void onHoverScroll(const HoverScrollEvent& e) override; - /* - void onSelectKey(const SelectKeyEvent& e) override; - */ }; diff --git a/plugins/Cardinal/src/TextEditor.cpp b/plugins/Cardinal/src/TextEditor.cpp index cbad142..e11e167 100644 --- a/plugins/Cardinal/src/TextEditor.cpp +++ b/plugins/Cardinal/src/TextEditor.cpp @@ -364,13 +364,6 @@ struct TextEditorModuleWidget : ModuleWidget { } } - void appendContextMenu(Menu *menu) override - { - menu->addChild(new MenuSeparator); - menu->addChild(new TextEditorLoadFileItem(textEditorModule, textEditorWidget)); - menu->addChild(new TextEditorLangSelectMenuItem(textEditorModule, textEditorWidget)); - } - void step() override { if (textEditorModule) @@ -394,6 +387,29 @@ struct TextEditorModuleWidget : ModuleWidget { ModuleWidget::draw(args); } + void appendContextMenu(Menu* const menu) override + { + menu->addChild(new MenuSeparator); + menu->addChild(new TextEditorLoadFileItem(textEditorModule, textEditorWidget)); + menu->addChild(new TextEditorLangSelectMenuItem(textEditorModule, textEditorWidget)); + + menu->addChild(new ui::MenuSeparator); + menu->addChild(createMenuItem("Undo", RACK_MOD_CTRL_NAME "+Z", [=]{ textEditorWidget->undo(); }, + !textEditorWidget->canUndo())); + menu->addChild(createMenuItem("Redo", RACK_MOD_CTRL_NAME "+Y", [=]{ textEditorWidget->redo(); }, + !textEditorWidget->canRedo())); + + menu->addChild(new ui::MenuSeparator); + menu->addChild(createMenuItem("Cut", RACK_MOD_CTRL_NAME "+X", [=]{ textEditorWidget->cut(); }, + !textEditorWidget->hasSelection())); + menu->addChild(createMenuItem("Copy", RACK_MOD_CTRL_NAME "+C", [=]{ textEditorWidget->copy(); }, + !textEditorWidget->hasSelection())); + menu->addChild(createMenuItem("Paste", RACK_MOD_CTRL_NAME "+V", [=]{ textEditorWidget->paste(); })); + + menu->addChild(new ui::MenuSeparator); + menu->addChild(createMenuItem("Select all", RACK_MOD_CTRL_NAME "+A", [=]{ textEditorWidget->selectAll(); })); + } + DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(TextEditorModuleWidget) }; #else