1/**************************************************************************/
2/* code_editor.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef CODE_EDITOR_H
32#define CODE_EDITOR_H
33
34#include "scene/gui/box_container.h"
35#include "scene/gui/button.h"
36#include "scene/gui/check_box.h"
37#include "scene/gui/code_edit.h"
38#include "scene/gui/dialogs.h"
39#include "scene/gui/label.h"
40#include "scene/gui/line_edit.h"
41#include "scene/main/timer.h"
42
43class GotoLineDialog : public ConfirmationDialog {
44 GDCLASS(GotoLineDialog, ConfirmationDialog);
45
46 Label *line_label = nullptr;
47 LineEdit *line = nullptr;
48
49 CodeEdit *text_editor = nullptr;
50
51 virtual void ok_pressed() override;
52
53public:
54 void popup_find_line(CodeEdit *p_edit);
55 int get_line() const;
56
57 GotoLineDialog();
58};
59
60class CodeTextEditor;
61
62class FindReplaceBar : public HBoxContainer {
63 GDCLASS(FindReplaceBar, HBoxContainer);
64
65 LineEdit *search_text = nullptr;
66 Label *matches_label = nullptr;
67 Button *find_prev = nullptr;
68 Button *find_next = nullptr;
69 CheckBox *case_sensitive = nullptr;
70 CheckBox *whole_words = nullptr;
71 TextureButton *hide_button = nullptr;
72
73 LineEdit *replace_text = nullptr;
74 Button *replace = nullptr;
75 Button *replace_all = nullptr;
76 CheckBox *selection_only = nullptr;
77
78 VBoxContainer *vbc_lineedit = nullptr;
79 HBoxContainer *hbc_button_replace = nullptr;
80 HBoxContainer *hbc_option_replace = nullptr;
81
82 CodeTextEditor *base_text_editor = nullptr;
83 CodeEdit *text_editor = nullptr;
84
85 uint32_t flags = 0;
86
87 int result_line = 0;
88 int result_col = 0;
89 int results_count = -1;
90 int results_count_to_current = -1;
91
92 bool replace_all_mode = false;
93 bool preserve_cursor = false;
94
95 void _get_search_from(int &r_line, int &r_col, bool p_is_searching_next = false);
96 void _update_results_count();
97 void _update_matches_label();
98
99 void _show_search(bool p_focus_replace = false, bool p_show_only = false);
100 void _hide_bar();
101
102 void _editor_text_changed();
103 void _search_options_changed(bool p_pressed);
104 void _search_text_changed(const String &p_text);
105 void _search_text_submitted(const String &p_text);
106 void _replace_text_submitted(const String &p_text);
107
108protected:
109 void _notification(int p_what);
110 virtual void unhandled_input(const Ref<InputEvent> &p_event) override;
111
112 bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
113
114 void _replace();
115 void _replace_all();
116
117 static void _bind_methods();
118
119public:
120 String get_search_text() const;
121 String get_replace_text() const;
122
123 bool is_case_sensitive() const;
124 bool is_whole_words() const;
125 bool is_selection_only() const;
126 void set_error(const String &p_label);
127
128 void set_text_edit(CodeTextEditor *p_text_editor);
129
130 void popup_search(bool p_show_only = false);
131 void popup_replace();
132
133 bool search_current();
134 bool search_prev();
135 bool search_next();
136
137 bool needs_to_count_results = true;
138 bool line_col_changed_for_result = false;
139
140 FindReplaceBar();
141};
142
143typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced);
144
145class CodeTextEditor : public VBoxContainer {
146 GDCLASS(CodeTextEditor, VBoxContainer);
147
148 CodeEdit *text_editor = nullptr;
149 FindReplaceBar *find_replace_bar = nullptr;
150 HBoxContainer *status_bar = nullptr;
151
152 Button *toggle_scripts_button = nullptr;
153 Button *error_button = nullptr;
154 Button *warning_button = nullptr;
155
156 Label *line_and_col_txt = nullptr;
157
158 Label *info = nullptr;
159 Timer *idle = nullptr;
160 bool code_complete_enabled = true;
161 Timer *code_complete_timer = nullptr;
162 int code_complete_timer_line = 0;
163
164 Timer *font_resize_timer = nullptr;
165 int font_resize_val;
166 real_t font_size;
167
168 Label *error = nullptr;
169 int error_line;
170 int error_column;
171
172 void _on_settings_change();
173 void _apply_settings_change();
174
175 void _update_text_editor_theme();
176 void _complete_request();
177 Ref<Texture2D> _get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option);
178 void _font_resize_timeout();
179 bool _add_font_size(int p_delta);
180
181 virtual void input(const Ref<InputEvent> &event) override;
182 void _text_editor_gui_input(const Ref<InputEvent> &p_event);
183 void _zoom_in();
184 void _zoom_out();
185 void _zoom_changed();
186 void _reset_zoom();
187
188 Color completion_font_color;
189 Color completion_string_color;
190 Color completion_comment_color;
191 CodeTextEditorCodeCompleteFunc code_complete_func;
192 void *code_complete_ud = nullptr;
193
194 void _error_button_pressed();
195 void _warning_button_pressed();
196 void _set_show_errors_panel(bool p_show);
197 void _set_show_warnings_panel(bool p_show);
198 void _error_pressed(const Ref<InputEvent> &p_event);
199
200 void _update_status_bar_theme();
201
202 void _toggle_scripts_pressed();
203
204 int _get_affected_lines_from(int p_caret);
205 int _get_affected_lines_to(int p_caret);
206
207protected:
208 virtual void _load_theme_settings() {}
209 virtual void _validate_script() {}
210 virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {}
211
212 void _text_changed_idle_timeout();
213 void _code_complete_timer_timeout();
214 void _text_changed();
215 void _line_col_changed();
216 void _notification(int);
217 static void _bind_methods();
218
219 bool is_warnings_panel_opened = false;
220 bool is_errors_panel_opened = false;
221
222public:
223 void trim_trailing_whitespace();
224 void insert_final_newline();
225
226 enum CaseStyle {
227 UPPER,
228 LOWER,
229 CAPITALIZE,
230 };
231 void convert_case(CaseStyle p_case);
232
233 void move_lines_up();
234 void move_lines_down();
235 void delete_lines();
236 void duplicate_selection();
237
238 /// Toggle inline comment on currently selected lines, or on current line if nothing is selected,
239 /// by adding or removing comment delimiter
240 void toggle_inline_comment(const String &delimiter);
241
242 void goto_line(int p_line);
243 void goto_line_selection(int p_line, int p_begin, int p_end);
244 void goto_line_centered(int p_line);
245 void set_executing_line(int p_line);
246 void clear_executing_line();
247
248 Variant get_edit_state();
249 void set_edit_state(const Variant &p_state);
250 Variant get_navigation_state();
251
252 void set_error_count(int p_error_count);
253 void set_warning_count(int p_warning_count);
254
255 void update_editor_settings();
256 void set_error(const String &p_error);
257 void set_error_pos(int p_line, int p_column);
258 Point2i get_error_pos() const;
259 void update_line_and_column() { _line_col_changed(); }
260 CodeEdit *get_text_editor() { return text_editor; }
261 FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
262 void set_find_replace_bar(FindReplaceBar *p_bar);
263 void remove_find_replace_bar();
264 virtual void apply_code() {}
265 virtual void goto_error();
266
267 void toggle_bookmark();
268 void goto_next_bookmark();
269 void goto_prev_bookmark();
270 void remove_all_bookmarks();
271
272 void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);
273
274 void validate_script();
275
276 void show_toggle_scripts_button();
277 void update_toggle_scripts_button();
278
279 CodeTextEditor();
280};
281
282#endif // CODE_EDITOR_H
283