TextEditor: Add undo, redo, cut, copy, paste actions

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-02-02 19:48:06 +00:00
parent cd2dd92549
commit 5c3367299e
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
3 changed files with 81 additions and 36 deletions

View file

@ -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;
}
}
}
*/

View file

@ -1,6 +1,6 @@
/*
* Syntax highlighting text editor (for ImGui in DPF, converted to VCV)
* Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
* 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;
*/
};

View file

@ -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