You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.2KB

  1. /*
  2. * Syntax highlighting text editor (for ImGui in DPF)
  3. * Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (c) 2017 BalazsJako
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "ImGuiTextEditor.hpp"
  25. #include "DearImGuiColorTextEditor/TextEditor.h"
  26. #include <fstream>
  27. // --------------------------------------------------------------------------------------------------------------------
  28. struct ImGuiTextEditor::PrivateData {
  29. TextEditor editor;
  30. std::string file;
  31. };
  32. // --------------------------------------------------------------------------------------------------------------------
  33. ImGuiTextEditor::ImGuiTextEditor()
  34. : ImGuiWidget(),
  35. pData(new PrivateData())
  36. {
  37. setUseMonospaceFont();
  38. }
  39. ImGuiTextEditor::~ImGuiTextEditor()
  40. {
  41. delete pData;
  42. }
  43. // --------------------------------------------------------------------------------------------------------------------
  44. bool ImGuiTextEditor::setFile(const std::string& file)
  45. {
  46. std::ifstream f(file);
  47. if (! f.good())
  48. {
  49. pData->file.clear();
  50. return false;
  51. }
  52. pData->file = file;
  53. pData->editor.SetText(std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()));
  54. return true;
  55. }
  56. void ImGuiTextEditor::setFileWithKnownText(const std::string& file, const std::string& text)
  57. {
  58. pData->file = file;
  59. pData->editor.SetText(text);
  60. }
  61. std::string ImGuiTextEditor::getFile() const
  62. {
  63. return pData->file;
  64. }
  65. void ImGuiTextEditor::setLanguageDefinition(const std::string& lang)
  66. {
  67. pData->editor.SetColorizerEnable(true);
  68. if (lang == "AngelScript")
  69. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::AngelScript());
  70. if (lang == "C")
  71. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::C());
  72. if (lang == "C++")
  73. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::CPlusPlus());
  74. if (lang == "GLSL")
  75. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::GLSL());
  76. if (lang == "HLSL")
  77. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::HLSL());
  78. if (lang == "Lua")
  79. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::Lua());
  80. if (lang == "SQL")
  81. return pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition::SQL());
  82. pData->editor.SetColorizerEnable(false);
  83. pData->editor.SetLanguageDefinition(TextEditor::LanguageDefinition());
  84. }
  85. // --------------------------------------------------------------------------------------------------------------------
  86. void ImGuiTextEditor::setText(const std::string& text)
  87. {
  88. pData->file.clear();
  89. pData->editor.SetText(text);
  90. }
  91. std::string ImGuiTextEditor::getText() const
  92. {
  93. return pData->editor.GetText();
  94. }
  95. void ImGuiTextEditor::setTextLines(const std::vector<std::string>& lines)
  96. {
  97. pData->file.clear();
  98. pData->editor.SetTextLines(lines);
  99. }
  100. std::vector<std::string> ImGuiTextEditor::getTextLines() const
  101. {
  102. return pData->editor.GetTextLines();
  103. }
  104. std::string ImGuiTextEditor::getSelectedText() const
  105. {
  106. return pData->editor.GetSelectedText();
  107. }
  108. std::string ImGuiTextEditor::getCurrentLineText()const
  109. {
  110. return pData->editor.GetCurrentLineText();
  111. }
  112. // --------------------------------------------------------------------------------------------------------------------
  113. void ImGuiTextEditor::drawImGui()
  114. {
  115. const float scaleFactor = getScaleFactor();
  116. ImGui::SetNextWindowPos(ImVec2(0, 0));
  117. ImGui::SetNextWindowSize(ImVec2(box.size.x * scaleFactor, box.size.y * scaleFactor));
  118. if (ImGui::Begin("TextEdit", nullptr, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize))
  119. {
  120. TextEditor& editor(pData->editor);
  121. const TextEditor::Coordinates cpos = editor.GetCursorPosition();
  122. ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s",
  123. cpos.mLine + 1, cpos.mColumn + 1,
  124. editor.GetTotalLines(),
  125. editor.IsOverwrite() ? "Ovr" : "Ins",
  126. editor.GetLanguageDefinition().mName.c_str(),
  127. pData->file.c_str());
  128. editor.Render("TextEditor");
  129. }
  130. ImGui::End();
  131. }
  132. void ImGuiTextEditor::onButton(const ButtonEvent& e)
  133. {
  134. setAsCurrentContext();
  135. // if mouse press is over the top status bar, do nothing so editor can be moved in the Rack
  136. if (e.action == GLFW_PRESS && e.pos.y < 25)
  137. return;
  138. ImGuiWidget::onButton(e);
  139. }
  140. void ImGuiTextEditor::onHoverScroll(const HoverScrollEvent& e)
  141. {
  142. // use Rack view scrolling if there is no scrollbar
  143. if (pData->editor.GetTotalLines() < 27)
  144. return;
  145. // if there is a scrollbar, handle the event
  146. ImGuiWidget::onHoverScroll(e);
  147. }
  148. /*
  149. void ImGuiTextEditor::onSelectKey(const SelectKeyEvent& e)
  150. {
  151. ImGuiWidget::onSelectKey(e);
  152. if (e.action == GLFW_PRESS && (e.mods & GLFW_MOD_CONTROL) != 0)
  153. {
  154. switch (e.key)
  155. {
  156. case GLFW_KEY_X:
  157. pData->editor.Cut();
  158. break;
  159. case GLFW_KEY_C:
  160. pData->editor.Copy();
  161. break;
  162. case GLFW_KEY_V:
  163. pData->editor.Paste();
  164. break;
  165. }
  166. }
  167. }
  168. */