| 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 | #ifndef H_GEOGRAM_GFX_GUI_CONSOLE_H | ||
| 41 | #define H_GEOGRAM_GFX_GUI_CONSOLE_H | ||
| 42 | |||
| 43 | #include <geogram_gfx/basic/common.h> | ||
| 44 | #include <geogram_gfx/imgui_ext/imgui_ext.h> | ||
| 45 | #include <geogram/basic/logger.h> | ||
| 46 | |||
| 47 | /** | ||
| 48 | * \file geogram_gfx/gui/console.h | ||
| 49 | * \brief A console. | ||
| 50 | */ | ||
| 51 | |||
| 52 | namespace GEO { | ||
| 53 | |||
| 54 | /** | ||
| 55 | * \brief A console, that displays logger messages, and where the | ||
| 56 | * user can enter commands. | ||
| 57 | * \details Inspired from ImGui AppLog example. | ||
| 58 | */ | ||
| 59 | class GEOGRAM_GFX_API Console : public GEO::LoggerClient { | ||
| 60 | |||
| 61 | public: | ||
| 62 | |||
| 63 | /** | ||
| 64 | * \brief Console constructor. | ||
| 65 | * \param[in] visible_flag an optional pointer to application's | ||
| 66 | * variable that controls the visibility of this Console. | ||
| 67 | */ | ||
| 68 | Console(bool* visible_flag = nullptr); | ||
| 69 | |||
| 70 | /** | ||
| 71 | * \copydoc GEO::LoggerClient::div() | ||
| 72 | */ | ||
| 73 | void div(const std::string& value) override; | ||
| 74 | |||
| 75 | /** | ||
| 76 | * \copydoc GEO::LoggerClient::out() | ||
| 77 | */ | ||
| 78 | void out(const std::string& value) override; | ||
| 79 | |||
| 80 | /** | ||
| 81 | * \copydoc GEO::LoggerClient::warn() | ||
| 82 | */ | ||
| 83 | void warn(const std::string& value) override; | ||
| 84 | |||
| 85 | /** | ||
| 86 | * \copydoc GEO::LoggerClient::err() | ||
| 87 | */ | ||
| 88 | void err(const std::string& value) override; | ||
| 89 | |||
| 90 | /** | ||
| 91 | * \copydoc GEO::LoggerClient::status() | ||
| 92 | */ | ||
| 93 | void status(const std::string& value) override; | ||
| 94 | |||
| 95 | /** | ||
| 96 | * \brief Clears the contents of the console. | ||
| 97 | */ | ||
| 98 | void clear(); | ||
| 99 | |||
| 100 | /** | ||
| 101 | * \brief Displays a formatted string to the console. | ||
| 102 | */ | ||
| 103 | virtual void printf(const char* fmt, ...) /* IM_FMTARGS(1) */; | ||
| 104 | |||
| 105 | /** | ||
| 106 | * \brief Draws the console and handles the gui. | ||
| 107 | * \param[in] visible an optional pointer to a visibility | ||
| 108 | * flag, controlled by a close button if different from nullptr. | ||
| 109 | * \param[in] with_window if true, then creates a new window | ||
| 110 | * using imgui::Begin() / imgui::End(), else caller is responsible | ||
| 111 | * for doing that. | ||
| 112 | */ | ||
| 113 | virtual void draw(bool* visible=nullptr, bool with_window=true); | ||
| 114 | |||
| 115 | int TextEditCallback(ImGuiInputTextCallbackData* data); | ||
| 116 | |||
| 117 | void show() { | ||
| 118 | *visible_flag_ = true; | ||
| 119 | } | ||
| 120 | |||
| 121 | void hide() { | ||
| 122 | *visible_flag_ = false; | ||
| 123 | } | ||
| 124 | |||
| 125 | typedef void (*CompletionCallback)( | ||
| 126 | Console* console, | ||
| 127 | const std::string& line, index_t startw, index_t endw, | ||
| 128 | const std::string& cmpword, std::vector<std::string>& matches | ||
| 129 | ); | ||
| 130 | |||
| 131 | void set_completion_callback(CompletionCallback CB) { | ||
| 132 | completion_callback_ = CB; | ||
| 133 | } | ||
| 134 | |||
| 135 | typedef void (*HistoryCallback)( | ||
| 136 | Console* console, | ||
| 137 | index_t index, | ||
| 138 | std::string& command | ||
| 139 | ); | ||
| 140 | |||
| 141 | void set_history_callback(HistoryCallback CB) { | ||
| 142 | history_callback_ = CB; | ||
| 143 | } | ||
| 144 | |||
| 145 | void set_history_size(index_t n) { | ||
| 146 | if(n != max_history_index_) { | ||
| 147 | history_index_ = n; | ||
| 148 | } | ||
| 149 | max_history_index_ = n; | ||
| 150 | } | ||
| 151 | |||
| 152 | void show_command_prompt() { | ||
| 153 | ✗ | command_prompt_ = true; | |
| 154 | } | ||
| 155 | |||
| 156 | void hide_command_prompt() { | ||
| 157 | ✗ | command_prompt_ = false; | |
| 158 | } | ||
| 159 | |||
| 160 | protected: | ||
| 161 | /** | ||
| 162 | * \brief This function is called whenever an error is | ||
| 163 | * displayed using err() | ||
| 164 | * \details Base implementation does nothing. This function | ||
| 165 | * is meant to be overloaded in derived classes. | ||
| 166 | * \param[in] err the error message sent to err() | ||
| 167 | */ | ||
| 168 | virtual void notify_error(const std::string& err); | ||
| 169 | |||
| 170 | virtual bool exec_command(const char* command); | ||
| 171 | |||
| 172 | /** | ||
| 173 | * \brief Redraws the GUI. | ||
| 174 | */ | ||
| 175 | virtual void update(); | ||
| 176 | |||
| 177 | bool command_prompt_; | ||
| 178 | ImGuiTextBuffer buf_; | ||
| 179 | ImGuiTextFilter filter_; | ||
| 180 | /** \brief Index to lines offset */ | ||
| 181 | ImVector<int> line_offsets_; | ||
| 182 | index_t scroll_to_bottom_; | ||
| 183 | bool* visible_flag_; | ||
| 184 | char input_buf_[geo_imgui_string_length]; | ||
| 185 | CompletionCallback completion_callback_; | ||
| 186 | HistoryCallback history_callback_; | ||
| 187 | index_t history_index_; | ||
| 188 | index_t max_history_index_; | ||
| 189 | }; | ||
| 190 | |||
| 191 | typedef SmartPointer<Console> Console_var; | ||
| 192 | } | ||
| 193 | |||
| 194 | #endif | ||
| 195 |