| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <geogram_gfx/gui/text_editor.h> | ||
| 41 | #include <geogram_gfx/gui/simple_application.h> | ||
| 42 | #include <geogram_gfx/imgui_ext/imgui_ext.h> | ||
| 43 | #include <geogram/basic/string.h> | ||
| 44 | #include <fstream> | ||
| 45 | |||
| 46 | namespace { | ||
| 47 | using namespace GEO; | ||
| 48 | ✗ | void text_editor_callback(TextEditorAction action, void* client_data) { | |
| 49 | ✗ | GEO::TextEditor* target = static_cast<GEO::TextEditor*>(client_data); | |
| 50 | ✗ | geo_argused(target); | |
| 51 | ✗ | switch(action) { | |
| 52 | ✗ | case TEXT_EDITOR_RUN: { | |
| 53 | ✗ | SimpleApplication* sapp = dynamic_cast<SimpleApplication*>( | |
| 54 | ✗ | Application::instance() | |
| 55 | ✗ | ); | |
| 56 | ✗ | if(sapp != nullptr) { | |
| 57 | ✗ | sapp->run_key_func("F5"); | |
| 58 | } | ||
| 59 | ✗ | } break; | |
| 60 | ✗ | case TEXT_EDITOR_SAVE: | |
| 61 | case TEXT_EDITOR_STOP: | ||
| 62 | case TEXT_EDITOR_TOOLTIP: | ||
| 63 | case TEXT_EDITOR_COMPLETION: | ||
| 64 | case TEXT_EDITOR_TEXT_CHANGED: | ||
| 65 | ✗ | break; | |
| 66 | } | ||
| 67 | ✗ | } | |
| 68 | } | ||
| 69 | |||
| 70 | namespace GEO { | ||
| 71 | |||
| 72 | ✗ | TextEditor::TextEditor(bool* visible) : visible_(visible) { | |
| 73 | ✗ | impl_.SetText(""); | |
| 74 | ✗ | impl_.SetCursorPosition( | |
| 75 | ✗ | ::TextEditor::CursorPosition(0,0) | |
| 76 | ); | ||
| 77 | ✗ | impl_.SetLanguage(::TextEditor::Language::Lua()); | |
| 78 | ✗ | impl_.SetPalette(::TextEditor::GetDarkPalette()); | |
| 79 | ✗ | impl_.set_callback(text_editor_callback, this); | |
| 80 | ✗ | fixed_layout_ = true; | |
| 81 | ✗ | } | |
| 82 | |||
| 83 | ✗ | std::string TextEditor::text() const { | |
| 84 | ✗ | return impl_.GetText(); | |
| 85 | } | ||
| 86 | |||
| 87 | ✗ | void TextEditor::draw() { | |
| 88 | ✗ | ImGui::Begin( | |
| 89 | "Text Editor", visible_, | ||
| 90 | ✗ | fixed_layout_ ? ( | |
| 91 | ImGuiWindowFlags_NoResize | | ||
| 92 | ImGuiWindowFlags_NoMove | | ||
| 93 | ImGuiWindowFlags_NoCollapse | ||
| 94 | ) : 0 | ||
| 95 | ); | ||
| 96 | |||
| 97 | ✗ | ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[1]); | |
| 98 | |||
| 99 | ✗ | if(Application::instance() != nullptr) { | |
| 100 | ✗ | if( | |
| 101 | ✗ | String::string_starts_with( | |
| 102 | Application::instance()->get_style(), | ||
| 103 | ✗ | "Light" | |
| 104 | ) | ||
| 105 | ) { | ||
| 106 | ✗ | impl_.SetPalette(::TextEditor::GetLightPalette()); | |
| 107 | } else { | ||
| 108 | ✗ | impl_.SetPalette(::TextEditor::GetDarkPalette()); | |
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | ✗ | impl_.Render("##source"); | |
| 113 | |||
| 114 | ✗ | ImGui::PopFont(); | |
| 115 | |||
| 116 | ✗ | ImGui::End(); | |
| 117 | ✗ | } | |
| 118 | |||
| 119 | ✗ | void TextEditor::load(const std::string& filename) { | |
| 120 | ✗ | std::ifstream in(filename.c_str()); | |
| 121 | ✗ | std::string text; | |
| 122 | ✗ | std::string line; | |
| 123 | ✗ | while(std::getline(in,line)) { | |
| 124 | ✗ | text += line; | |
| 125 | ✗ | text += "\n"; | |
| 126 | } | ||
| 127 | ✗ | impl_.SetText(text); | |
| 128 | ✗ | impl_.SetCursorPosition( | |
| 129 | ✗ | ::TextEditor::CursorPosition(0,0) | |
| 130 | ); | ||
| 131 | ✗ | } | |
| 132 | |||
| 133 | ✗ | void TextEditor::save(const std::string& filename) { | |
| 134 | ✗ | std::ofstream out(filename.c_str()); | |
| 135 | ✗ | out << impl_.GetText(); | |
| 136 | ✗ | } | |
| 137 | |||
| 138 | ✗ | void TextEditor::clear() { | |
| 139 | ✗ | impl_.SetText(""); | |
| 140 | ✗ | impl_.SetCursorPosition( | |
| 141 | ✗ | ::TextEditor::CursorPosition(0,0) | |
| 142 | ); | ||
| 143 | ✗ | } | |
| 144 | |||
| 145 | ✗ | void TextEditor::load_data(const char* data) { | |
| 146 | ✗ | impl_.SetText(data); | |
| 147 | ✗ | impl_.SetCursorPosition( | |
| 148 | ✗ | ::TextEditor::CursorPosition(0,0) | |
| 149 | ); | ||
| 150 | ✗ | } | |
| 151 | |||
| 152 | } | ||
| 153 |