Debian Codegraph @

scene/gui/text_edit.h

raw symbols SCIP
1 /**************************************************************************/
2 /* text_edit.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 #pragma once
32
33 #include "scene/gui/control.h"
34 #include "scene/gui/popup_menu.h"
35 #include "scene/gui/scroll_bar.h"
36 #include "scene/main/timer.h"
37 #include "scene/resources/syntax_highlighter.h"
38 #include "scene/resources/text_paragraph.h"
39
40 class TextEdit : public Control {
41 GDCLASS(TextEdit, Control);
42
43 public:
44 /* Edit Actions. */
45 enum EditAction {
46 ACTION_NONE,
47 ACTION_TYPING,
48 ACTION_BACKSPACE,
49 ACTION_DELETE,
50 };
51
52 /* Caret. */
53 enum CaretType {
54 CARET_TYPE_LINE,
55 CARET_TYPE_BLOCK
56 };
57
58 /* Selection */
59 enum SelectionMode {
60 SELECTION_MODE_NONE,
61 SELECTION_MODE_SHIFT,
62 SELECTION_MODE_POINTER,
63 SELECTION_MODE_WORD,
64 SELECTION_MODE_LINE
65 };
66
67 /* Line Wrapping.*/
68 enum LineWrappingMode {
69 LINE_WRAPPING_NONE,
70 LINE_WRAPPING_BOUNDARY
71 };
72
73 /* Gutters. */
74 enum GutterType {
75 GUTTER_TYPE_STRING,
76 GUTTER_TYPE_ICON,
77 GUTTER_TYPE_CUSTOM
78 };
79
80 /* Context Menu. */
81 enum MenuItems {
82 MENU_CUT,
83 MENU_COPY,
84 MENU_PASTE,
85 MENU_CLEAR,
86 MENU_SELECT_ALL,
87 MENU_UNDO,
88 MENU_REDO,
89 MENU_SUBMENU_TEXT_DIR,
90 MENU_DIR_INHERITED,
91 MENU_DIR_AUTO,
92 MENU_DIR_LTR,
93 MENU_DIR_RTL,
94 MENU_DISPLAY_UCC,
95 MENU_SUBMENU_INSERT_UCC,
96 MENU_INSERT_LRM,
97 MENU_INSERT_RLM,
98 MENU_INSERT_LRE,
99 MENU_INSERT_RLE,
100 MENU_INSERT_LRO,
101 MENU_INSERT_RLO,
102 MENU_INSERT_PDF,
103 MENU_INSERT_ALM,
104 MENU_INSERT_LRI,
105 MENU_INSERT_RLI,
106 MENU_INSERT_FSI,
107 MENU_INSERT_PDI,
108 MENU_INSERT_ZWJ,
109 MENU_INSERT_ZWNJ,
110 MENU_INSERT_WJ,
111 MENU_INSERT_SHY,
112 MENU_EMOJI_AND_SYMBOL,
113 MENU_MAX
114
115 };
116
117 /* Search. */
118 enum SearchFlags {
119 SEARCH_MATCH_CASE = 1,
120 SEARCH_WHOLE_WORDS = 2,
121 SEARCH_BACKWARDS = 4
122 };
123
124 private:
125 struct GutterInfo {
126 GutterType type = GutterType::GUTTER_TYPE_STRING;
127 String name = "";
128 int width = 24;
129 bool draw = true;
130 bool clickable = false;
131 bool overwritable = false;
132
133 Callable custom_draw_callback;
134 };
135
136 class Text {
137 public:
138 struct Gutter {
139 Variant metadata;
140 bool clickable = false;
141
142 Ref<Texture2D> icon;
143 String text = "";
144 Color color = Color(1, 1, 1);
145 };
146
147 mutable int64_t next_item_id = 0;
148
149 struct Line {
150 Vector<Gutter> gutters;
151
152 String data;
153 Array bidi_override;
154 Ref<TextParagraph> data_buf;
155 Vector<RID> accessibility_text_root_element;
156
157 String ime_data;
158 Array ime_bidi_override;
159
160 Color background_color = Color(0, 0, 0, 0);
161 bool hidden = false;
162 int line_count = 0;
163 int height = 0;
164 int width = 0;
165 float indent_ofs = -1.0;
166
167 Line() {
168 data_buf.instantiate();
169 }
170 };
171
172 private:
173 bool is_dirty = false;
174 bool tab_size_dirty = false;
175
176 mutable Vector<Line> text;
177 Ref<Font> font;
178 int font_size = -1;
179 int font_height = 0;
180
181 String language;
182 TextServer::Direction direction = TextServer::DIRECTION_AUTO;
183 BitField<TextServer::LineBreakFlag> brk_flags = TextServer::BREAK_MANDATORY;
184 bool draw_control_chars = false;
185 String custom_word_separators;
186 bool use_default_word_separators = true;
187 bool use_custom_word_separators = false;
188 Callable inline_object_parser;
189
190 mutable bool max_line_width_dirty = true;
191 mutable bool max_line_height_dirty = true;
192 mutable int max_line_width = 0;
193 mutable int max_line_height = 0;
194 mutable int total_visible_line_count = 0;
195 int width = -1;
196
197 int tab_size = 4;
198 int gutter_count = 0;
199 bool indent_wrapped_lines = false;
200
201 public:
202 void set_tab_size(int p_tab_size);
203 int get_tab_size() const;
204 void set_indent_wrapped_lines(bool p_enabled);
205 bool is_indent_wrapped_lines() const;
206
207 void set_font(const Ref<Font> &p_font);
208 void set_font_size(int p_font_size);
209 void set_direction_and_language(TextServer::Direction p_direction, const String &p_language);
210 void set_draw_control_chars(bool p_enabled);
211 void set_inline_object_parser(const Callable &p_parser);
212
213 int get_line_height() const;
214 int get_line_width(int p_line, int p_wrap_index = -1) const;
215 int get_max_width() const;
216 int get_total_visible_line_count() const;
217
218 void set_use_default_word_separators(bool p_enabled);
219 bool is_default_word_separators_enabled() const;
220
221 void set_use_custom_word_separators(bool p_enabled);
222 bool is_custom_word_separators_enabled() const;
223
224 void set_custom_word_separators(const String &p_separators);
225 String get_enabled_word_separators() const;
226 String get_custom_word_separators() const;
227 String get_default_word_separators() const;
228
229 void set_width(float p_width);
230 float get_width() const;
231 void set_brk_flags(BitField<TextServer::LineBreakFlag> p_flags);
232 BitField<TextServer::LineBreakFlag> get_brk_flags() const;
233 int get_line_wrap_amount(int p_line) const;
234
235 const Vector<RID> get_accessibility_elements(int p_line);
236 void update_accessibility(int p_line, RID p_root);
237 void clear_accessibility() {
238 for (int i = 0; i < text.size(); i++) {
239 text.write[i].accessibility_text_root_element.clear();
240 }
241 }
242
243 Vector<Vector2i> get_line_wrap_ranges(int p_line) const;
244 const Ref<TextParagraph> get_line_data(int p_line) const;
245 float get_indent_offset(int p_line, bool p_rtl) const;
246
247 void set(int p_line, const String &p_text, const Array &p_bidi_override);
248 void set_ime(int p_line, const String &p_text, const Array &p_bidi_override);
249 void set_hidden(int p_line, bool p_hidden);
250 bool is_hidden(int p_line) const;
251 void insert(int p_at, const Vector<String> &p_text, const Vector<Array> &p_bidi_override);
252 void remove_range(int p_from_line, int p_to_line);
253 int size() const { return text.size(); }
254 void clear();
255
256 void invalidate_cache(int p_line, bool p_text_changed = false);
257 void invalidate_font();
258 void invalidate_all();
259 void invalidate_all_lines();
260
261 _FORCE_INLINE_ const String &operator[](int p_line) const;
262 _FORCE_INLINE_ const String &get_text_with_ime(int p_line) const;
263
264 /* Gutters. */
265 void add_gutter(int p_at);
266 void remove_gutter(int p_gutter);
267 void move_gutters(int p_from_line, int p_to_line);
268
269 void set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata) { text.write[p_line].gutters.write[p_gutter].metadata = p_metadata; }
270 const Variant &get_line_gutter_metadata(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].metadata; }
271
272 void set_line_gutter_text(int p_line, int p_gutter, const String &p_text) { text.write[p_line].gutters.write[p_gutter].text = p_text; }
273 const String &get_line_gutter_text(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].text; }
274
275 void set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) { text.write[p_line].gutters.write[p_gutter].icon = p_icon; }
276 const Ref<Texture2D> &get_line_gutter_icon(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].icon; }
277
278 void set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) { text.write[p_line].gutters.write[p_gutter].color = p_color; }
279 const Color &get_line_gutter_item_color(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].color; }
280
281 void set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable) { text.write[p_line].gutters.write[p_gutter].clickable = p_clickable; }
282 bool is_line_gutter_clickable(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].clickable; }
283
284 /* Line style. */
285 void set_line_background_color(int p_line, const Color &p_color) { text.write[p_line].background_color = p_color; }
286 const Color get_line_background_color(int p_line) const { return text[p_line].background_color; }
287 };
288
289 /* Text */
290 Text text;
291 RID text_ci;
292 bool setting_text = false;
293
294 enum AltInputMode {
295 ALT_INPUT_NONE,
296 ALT_INPUT_UNICODE,
297 ALT_INPUT_OEM,
298 ALT_INPUT_WIN,
299 };
300
301 AltInputMode alt_mode = ALT_INPUT_NONE;
302 bool alt_start = false;
303 bool alt_start_no_hold = false;
304 uint32_t alt_code = 0;
305
306 bool tab_input_mode = true;
307
308 // Text properties.
309 String ime_text = "";
310 Point2 ime_selection;
311
312 // Placeholder
313 String placeholder_text = "";
314 Array placeholder_bidi_override;
315 Ref<TextParagraph> placeholder_data_buf;
316 int placeholder_line_height = -1;
317 int placeholder_max_width = -1;
318
319 Vector<String> placeholder_wrapped_rows;
320
321 void _update_placeholder();
322 bool _using_placeholder() const;
323
324 /* Initialize to opposite first, so we get past the early-out in set_editable. */
325 bool editable = false;
326
327 TextDirection text_direction = TEXT_DIRECTION_AUTO;
328 TextDirection input_direction = TEXT_DIRECTION_LTR;
329
330 String language = "";
331
332 TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
333 Array st_args;
334
335 void _clear();
336 void _update_caches(bool p_invalidate_all = false);
337
338 void _close_ime_window();
339 void _update_ime_window_position();
340 void _update_ime_text();
341
342 // User control.
343 bool overtype_mode = false;
344 bool context_menu_enabled = true;
345 bool emoji_menu_enabled = true;
346 bool backspace_deletes_composite_character_enabled = false;
347 bool shortcut_keys_enabled = true;
348 bool virtual_keyboard_enabled = true;
349 bool virtual_keyboard_show_on_focus = true;
350 bool middle_mouse_paste_enabled = true;
351 bool empty_selection_clipboard_enabled = true;
352
353 // Overridable actions.
354 String cut_copy_line = "";
355
356 // Context menu.
357 PopupMenu *menu = nullptr;
358 PopupMenu *menu_dir = nullptr;
359 PopupMenu *menu_ctl = nullptr;
360
361 Callable inline_object_drawer;
362 Callable inline_object_click_handler;
363
364 Key _get_menu_action_accelerator(const String &p_action);
365 void _generate_context_menu();
366 void _update_context_menu();
367
368 /* Versioning */
369 struct Caret;
370 struct TextOperation {
371 enum Type {
372 TYPE_NONE,
373 TYPE_INSERT,
374 TYPE_REMOVE
375 };
376 Vector<Caret> start_carets;
377 Vector<Caret> end_carets;
378
379 Type type = TYPE_NONE;
380 int from_line = 0;
381 int from_column = 0;
382 int to_line = 0;
383 int to_column = 0;
384 String text;
385 uint32_t prev_version = 0;
386 uint32_t version = 0;
387 bool chain_forward = false;
388 bool chain_backward = false;
389 };
390
391 bool undo_enabled = true;
392 int undo_stack_max_size = 50;
393
394 EditAction current_action = EditAction::ACTION_NONE;
395 bool pending_action_end = false;
396 bool in_action = false;
397
398 int complex_operation_count = 0;
399 bool next_operation_is_complex = false;
400
401 TextOperation current_op;
402 List<TextOperation> undo_stack;
403 List<TextOperation>::Element *undo_stack_pos = nullptr;
404
405 Timer *idle_detect = nullptr;
406
407 uint32_t version = 0;
408 uint32_t saved_version = 0;
409
410 void _push_current_op();
411 void _do_text_op(const TextOperation &p_op, bool p_reverse);
412 void _clear_redo();
413
414 /* Search */
415 String search_text = "";
416 uint32_t search_flags = 0;
417
418 int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) const;
419
420 /* Tooltip. */
421 Callable tooltip_callback;
422
423 /* Mouse */
424 struct LineDrawingCache {
425 int y_offset = 0;
426 Vector<int> first_visible_chars;
427 Vector<int> last_visible_chars;
428 };
429
430 HashMap<int, LineDrawingCache> line_drawing_cache;
431
432 int _get_char_pos_for_line(int p_px, int p_line, int p_wrap_index = 0) const;
433
434 /* Caret. */
435 struct Selection {
436 bool active = false;
437
438 int origin_line = 0;
439 int origin_column = 0;
440 int origin_last_fit_x = 0;
441 int word_begin_column = 0;
442 int word_end_column = 0;
443 };
444
445 struct Caret {
446 Selection selection;
447
448 Point2 draw_pos;
449 bool visible = false;
450 int last_fit_x = 0;
451 int line = 0;
452 int column = 0;
453 };
454
455 // Vector containing all the carets, index '0' is the "main caret" and should never be removed.
456 Vector<Caret> carets;
457
458 bool setting_caret_line = false;
459 bool caret_pos_dirty = false;
460
461 int multicaret_edit_count = 0;
462 bool multicaret_edit_merge_queued = false;
463 HashSet<int> multicaret_edit_ignore_carets;
464
465 CaretType caret_type = CaretType::CARET_TYPE_LINE;
466
467 bool draw_caret = true;
468 bool draw_caret_when_editable_disabled = false;
469
470 bool caret_blink_enabled = false;
471 Timer *caret_blink_timer = nullptr;
472
473 bool move_caret_on_right_click = true;
474
475 bool caret_mid_grapheme_enabled = false;
476
477 bool multi_carets_enabled = true;
478
479 bool drag_action = false;
480 bool drag_caret_force_displayed = false;
481
482 void _caret_changed(int p_caret = -1);
483 void _emit_caret_changed();
484
485 void _show_virtual_keyboard();
486 void _reset_caret_blink_timer();
487 void _toggle_draw_caret();
488
489 int _get_column_x_offset_for_line(int p_char, int p_line, int p_column) const;
490 bool _is_line_col_in_range(int p_line, int p_column, int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_include_edges = true) const;
491
492 void _offset_carets_after(int p_old_line, int p_old_column, int p_new_line, int p_new_column, bool p_include_selection_begin = true, bool p_include_selection_end = true);
493
494 void _cancel_drag_and_drop_text();
495
496 /* Selection. */
497 SelectionMode selecting_mode = SelectionMode::SELECTION_MODE_NONE;
498
499 bool selecting_enabled = true;
500 bool deselect_on_focus_loss_enabled = true;
501 bool drag_and_drop_selection_enabled = true;
502
503 bool use_selected_font_color = false;
504
505 bool selection_drag_attempt = false;
506 bool dragging_selection = false;
507 int drag_and_drop_origin_caret_index = -1;
508 int drag_caret_index = -1;
509
510 Timer *click_select_held = nullptr;
511 uint64_t last_dblclk = 0;
512 Vector2 last_dblclk_pos;
513
514 void _selection_changed(int p_caret = -1);
515 void _click_selection_held();
516
517 void _update_selection_mode_pointer(bool p_initial = false);
518 void _update_selection_mode_word(bool p_initial = false);
519 void _update_selection_mode_line(bool p_initial = false);
520
521 void _pre_shift_selection(int p_caret);
522
523 bool _selection_contains(int p_caret, int p_line, int p_column, bool p_include_edges = true, bool p_only_selections = true) const;
524
525 /* Line wrapping. */
526 LineWrappingMode line_wrapping_mode = LineWrappingMode::LINE_WRAPPING_NONE;
527 TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_WORD_SMART;
528
529 int wrap_at_column = 0;
530
531 void _update_wrap_at_column(bool p_force = false);
532
533 /* Viewport. */
534 HScrollBar *h_scroll = nullptr;
535 VScrollBar *v_scroll = nullptr;
536
537 Vector2i content_size_cache;
538 bool fit_content_height = false;
539 bool fit_content_width = false;
540 bool scroll_past_end_of_file_enabled = false;
541
542 // Smooth scrolling.
543 bool smooth_scroll_enabled = false;
544 float target_v_scroll = 0.0;
545 float v_scroll_speed = 80.0;
546
547 // Scrolling.
548 int first_visible_line = 0;
549 int first_visible_line_wrap_ofs = 0;
550 int first_visible_col = 0;
551
552 bool scrolling = false;
553 bool updating_scrolls = false;
554
555 void _update_scrollbars();
556 int _get_control_height() const;
557
558 void _v_scroll_input();
559 void _scroll_moved(double p_to_val);
560
561 double _get_visible_lines_offset() const;
562 double _get_v_scroll_offset() const;
563
564 void _scroll_up(real_t p_delta, bool p_animate);
565 void _scroll_down(real_t p_delta, bool p_animate);
566
567 void _scroll_lines_up();
568 void _scroll_lines_down();
569
570 void _adjust_viewport_to_caret_horizontally(int p_caret = 0, bool p_maximize_selection = true);
571
572 // Minimap.
573 bool draw_minimap = false;
574
575 int minimap_width = 80;
576 Point2 minimap_char_size = Point2(1, 2);
577 int minimap_line_spacing = 1;
578
579 // Minimap scroll.
580 bool minimap_clicked = false;
581 bool hovering_minimap = false;
582 bool dragging_minimap = false;
583 bool can_drag_minimap = false;
584
585 double minimap_scroll_ratio = 0.0;
586 double minimap_scroll_click_pos = 0.0;
587
588 void _update_minimap_hover();
589 void _update_minimap_click();
590 void _update_minimap_drag();
591
592 /* Gutters. */
593 Vector<GutterInfo> gutters;
594 int gutters_width = 0;
595 int gutter_padding = 0;
596 Vector2i hovered_gutter = Vector2i(-1, -1); // X = gutter index, Y = row.
597
598 void _update_gutter_width();
599 Vector2i _get_hovered_gutter(const Point2 &p_mouse_pos) const;
600
601 /* Syntax highlighting. */
602 Ref<SyntaxHighlighter> syntax_highlighter;
603 HashMap<int, Vector<Pair<int64_t, Color>>> syntax_highlighting_cache;
604
605 Vector<Pair<int64_t, Color>> _get_line_syntax_highlighting(int p_line);
606 void _clear_syntax_highlighting_cache();
607
608 /* Visual. */
609 struct ThemeCache {
610 float base_scale = 1.0;
611
612 /* Search */
613 Color search_result_color = Color(1, 1, 1);
614 Color search_result_border_color = Color(1, 1, 1);
615
616 /* Caret */
617 int caret_width = 1;
618 Color caret_color = Color(1, 1, 1);
619 Color caret_background_color = Color(0, 0, 0);
620
621 /* Selection */
622 Color font_selected_color = Color(0, 0, 0, 0);
623 Color selection_color = Color(1, 1, 1);
624
625 /* Other visuals */
626 Ref<StyleBox> style_normal;
627 Ref<StyleBox> style_focus;
628 Ref<StyleBox> style_readonly;
629
630 Ref<Texture2D> tab_icon;
631 Ref<Texture2D> space_icon;
632
633 Ref<Font> font;
634 int font_size = 16;
635 Color font_color = Color(1, 1, 1);
636 Color font_readonly_color = Color(1, 1, 1);
637 Color font_placeholder_color = Color(1, 1, 1, 0.6);
638
639 int outline_size = 0;
640 Color outline_color = Color(1, 1, 1);
641
642 int line_spacing = 1;
643 int wrap_offset = 10;
644
645 Color current_line_color = Color(1, 1, 1);
646 Color word_highlighted_color = Color(1, 1, 1);
647
648 #ifndef DISABLE_DEPRECATED
649 Color background_color = Color(1, 1, 1);
650 #endif // DISABLE_DEPRECATED
651 } theme_cache;
652
653 bool window_has_focus = true;
654 bool first_draw = true;
655
656 bool highlight_current_line = false;
657 bool highlight_all_occurrences = false;
658 bool draw_control_chars = false;
659 bool draw_tabs = false;
660 bool draw_spaces = false;
661
662 // FIXME: Helper method to draw unfilled rects, should be moved to RenderingServer.
663 void _draw_rect_unfilled(RID p_canvas_item, const Rect2 &p_rect, const Color &p_color, real_t p_width = -1.0, bool p_antialiased = false) const;
664
665 /* Theme. */
666 Ref<StyleBox> _get_current_stylebox() const;
667
668 /*** Super internal Core API. Everything builds on it. ***/
669 bool text_changed_dirty = false;
670 void _text_changed();
671 void _emit_text_changed();
672
673 void _insert_text(int p_line, int p_char, const String &p_text, int *r_end_line = nullptr, int *r_end_char = nullptr);
674 void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
675
676 void _base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column);
677 String _base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const;
678 void _base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
679
680 /* Input actions. */
681 void _swap_current_input_direction();
682 void _new_line(bool p_split_current = true, bool p_above = false);
683 void _move_caret_left(bool p_select, bool p_move_by_word = false);
684 void _move_caret_right(bool p_select, bool p_move_by_word = false);
685 void _move_caret_up(bool p_select);
686 void _move_caret_down(bool p_select);
687 void _move_caret_to_line_start(bool p_select);
688 void _move_caret_to_line_end(bool p_select);
689 void _move_caret_page_up(bool p_select);
690 void _move_caret_page_down(bool p_select);
691 void _do_backspace(bool p_word = false, bool p_all_to_left = false);
692 void _delete(bool p_word = false, bool p_all_to_right = false);
693 void _move_caret_document_start(bool p_select);
694 void _move_caret_document_end(bool p_select);
695 bool _clear_carets_and_selection();
696
697 protected:
698 void _notification(int p_what);
699 static void _bind_methods();
700
701 #ifndef DISABLE_DEPRECATED
702 void _set_selection_mode_compat_86978(SelectionMode p_mode, int p_line = -1, int p_column = -1, int p_caret = 0);
703 Point2i _get_line_column_at_pos_bind_compat_100913(const Point2i &p_pos, bool p_allow_out_of_bounds = true) const;
704 static void _bind_compatibility_methods();
705 #endif // DISABLE_DEPRECATED
706
707 virtual void _draw_guidelines() {}
708 virtual void _update_theme_item_cache() override;
709
710 /* Internal API for CodeEdit, pending public API. */
711 // Brace matching.
712 struct BraceMatchingData {
713 int open_match_line = -1;
714 int open_match_column = -1;
715 bool open_matching = false;
716 bool open_mismatch = false;
717 int close_match_line = -1;
718 int close_match_column = -1;
719 bool close_matching = false;
720 bool close_mismatch = false;
721 };
722
723 bool highlight_matching_braces_enabled = false;
724
725 // Line hiding.
726 bool hiding_enabled = false;
727
728 void _set_hiding_enabled(bool p_enabled);
729 bool _is_hiding_enabled() const;
730
731 void _set_line_as_hidden(int p_line, bool p_hidden);
732 bool _is_line_hidden(int p_line) const;
733
734 void _unhide_all_lines();
735 virtual void _unhide_carets();
736
737 int _get_wrapped_indent_level(int p_line, int &r_first_wrap) const;
738 float _get_wrap_indent_offset(int p_line, int p_wrap_index, bool p_rtl) const;
739
740 // Symbol lookup.
741 String lookup_symbol_word;
742 void _set_symbol_lookup_word(const String &p_symbol);
743
744 // Theme items.
745 virtual Color _get_brace_mismatch_color() const { return Color(); }
746 virtual Color _get_code_folding_color() const { return Color(); }
747 virtual Ref<Texture2D> _get_folded_eol_icon() const { return Ref<Texture2D>(); }
748
749 /* Text manipulation */
750
751 // Overridable actions
752 virtual void _handle_unicode_input_internal(const uint32_t p_unicode, int p_caret);
753 virtual void _backspace_internal(int p_caret);
754
755 virtual void _cut_internal(int p_caret);
756 virtual void _copy_internal(int p_caret);
757 virtual void _paste_internal(int p_caret);
758 virtual void _paste_primary_clipboard_internal(int p_caret);
759
760 void _accessibility_action_set_selection(const Variant &p_data);
761 void _accessibility_action_replace_selected(const Variant &p_data);
762 void _accessibility_action_set_value(const Variant &p_data);
763 void _accessibility_action_menu(const Variant &p_data);
764 void _accessibility_scroll_down(const Variant &p_data);
765 void _accessibility_scroll_left(const Variant &p_data);
766 void _accessibility_scroll_right(const Variant &p_data);
767 void _accessibility_scroll_up(const Variant &p_data);
768 void _accessibility_scroll_set(const Variant &p_data);
769 void _accessibility_action_scroll_into_view(const Variant &p_data, int p_line, int p_wrap);
770
771 GDVIRTUAL2(_handle_unicode_input, int, int)
772 GDVIRTUAL1(_backspace, int)
773 GDVIRTUAL1(_cut, int)
774 GDVIRTUAL1(_copy, int)
775 GDVIRTUAL1(_paste, int)
776 GDVIRTUAL1(_paste_primary_clipboard, int)
777
778 public:
779 /* General overrides. */
780 virtual void unhandled_key_input(const Ref<InputEvent> &p_event) override;
781 virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
782 bool alt_input(const Ref<InputEvent> &p_gui_input);
783 virtual Size2 get_minimum_size() const override;
784 virtual bool is_text_field() const override;
785 virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
786 virtual Variant get_drag_data(const Point2 &p_point) override;
787 virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
788 virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
789 virtual String get_tooltip(const Point2 &p_pos) const override;
790 void set_tooltip_request_func(const Callable &p_tooltip_callback);
791
792 /* Text */
793 // Text properties.
794 RID get_text_canvas_item() const;
795
796 bool has_ime_text() const;
797 void cancel_ime();
798 void apply_ime();
799
800 void set_editable(bool p_editable);
801 bool is_editable() const;
802
803 void set_text_direction(TextDirection p_text_direction);
804 TextDirection get_text_direction() const;
805
806 void set_language(const String &p_language);
807 String get_language() const;
808
809 void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
810 TextServer::StructuredTextParser get_structured_text_bidi_override() const;
811 void set_structured_text_bidi_override_options(const Array &p_args);
812 Array get_structured_text_bidi_override_options() const;
813
814 void set_tab_size(const int p_size);
815 int get_tab_size() const;
816
817 void set_indent_wrapped_lines(bool p_enabled);
818 bool is_indent_wrapped_lines() const;
819
820 void set_tab_input_mode(bool p_enabled);
821 bool get_tab_input_mode() const;
822
823 // User controls
824 void set_overtype_mode_enabled(bool p_enabled);
825 bool is_overtype_mode_enabled() const;
826
827 void set_context_menu_enabled(bool p_enabled);
828 bool is_context_menu_enabled() const;
829
830 void show_emoji_and_symbol_picker();
831
832 void set_emoji_menu_enabled(bool p_enabled);
833 bool is_emoji_menu_enabled() const;
834
835 void set_backspace_deletes_composite_character_enabled(bool p_enabled);
836 bool is_backspace_deletes_composite_character_enabled() const;
837
838 void set_shortcut_keys_enabled(bool p_enabled);
839 bool is_shortcut_keys_enabled() const;
840
841 void set_virtual_keyboard_enabled(bool p_enabled);
842 bool is_virtual_keyboard_enabled() const;
843
844 void set_virtual_keyboard_show_on_focus(bool p_show_on_focus);
845 bool get_virtual_keyboard_show_on_focus() const;
846
847 void set_middle_mouse_paste_enabled(bool p_enabled);
848 bool is_middle_mouse_paste_enabled() const;
849
850 void set_empty_selection_clipboard_enabled(bool p_enabled);
851 bool is_empty_selection_clipboard_enabled() const;
852
853 // Text manipulation
854 void clear();
855
856 void _set_text(const String &p_text, bool p_emit_signal = false);
857 void set_text(const String &p_text);
858 String get_text() const;
859
860 int get_line_count() const;
861
862 void set_placeholder(const String &p_text);
863 String get_placeholder() const;
864
865 void set_line(int p_line, const String &p_new_text);
866 String get_line(int p_line) const;
867 String get_line_with_ime(int p_line) const;
868
869 int get_line_width(int p_line, int p_wrap_index = -1) const;
870 int get_line_height() const;
871
872 int get_indent_level(int p_line) const;
873 int get_first_non_whitespace_column(int p_line) const;
874
875 void swap_lines(int p_from_line, int p_to_line);
876
877 void insert_line_at(int p_line, const String &p_text);
878 void remove_line_at(int p_line, bool p_move_carets_down = true);
879
880 void insert_text_at_caret(const String &p_text, int p_caret = -1);
881 void insert_text(const String &p_text, int p_line, int p_column, bool p_before_selection_begin = true, bool p_before_selection_end = false);
882 void remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
883
884 int get_last_unhidden_line() const;
885 int get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const;
886 Point2i get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const;
887
888 void set_inline_object_handlers(const Callable &p_parser, const Callable &p_drawer, const Callable &p_click_handler);
889
890 // Overridable actions
891 void handle_unicode_input(const uint32_t p_unicode, int p_caret = -1);
892 void backspace(int p_caret = -1);
893
894 void cut(int p_caret = -1);
895 void copy(int p_caret = -1);
896 void paste(int p_caret = -1);
897 void paste_primary_clipboard(int p_caret = -1);
898
899 // Context menu.
900 PopupMenu *get_menu() const;
901 bool is_menu_visible() const;
902 void menu_option(int p_option);
903
904 /* Versioning */
905 void start_action(EditAction p_action);
906 void end_action();
907 EditAction get_current_action() const;
908
909 void begin_complex_operation();
910 void end_complex_operation();
911
912 bool has_undo() const;
913 bool has_redo() const;
914 void undo();
915 void redo();
916 void clear_undo_history();
917
918 bool is_insert_text_operation() const;
919
920 void tag_saved_version();
921
922 uint32_t get_version() const;
923 uint32_t get_saved_version() const;
924
925 /* Search */
926 void set_search_text(const String &p_search_text);
927 void set_search_flags(uint32_t p_flags);
928
929 Point2i search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const;
930
931 /* Mouse */
932 Point2 get_local_mouse_pos() const;
933
934 String get_word_at_pos(const Vector2 &p_pos) const;
935 String get_word(int p_line, int p_column) const;
936
937 Point2i get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line = true, bool p_clamp_column = true) const;
938 Point2i get_pos_at_line_column(int p_line, int p_column) const;
939 Rect2i get_rect_at_line_column(int p_line, int p_column) const;
940
941 int get_minimap_line_at_pos(const Point2i &p_pos) const;
942
943 bool is_dragging_cursor() const;
944 bool is_mouse_over_selection(bool p_edges = true, int p_caret = -1) const;
945
946 /* Caret */
947 void set_caret_type(CaretType p_type);
948 CaretType get_caret_type() const;
949
950 void set_caret_blink_enabled(bool p_enabled);
951 bool is_caret_blink_enabled() const;
952
953 void set_caret_blink_interval(const float p_interval);
954 float get_caret_blink_interval() const;
955
956 void set_draw_caret_when_editable_disabled(bool p_enable);
957 bool is_drawing_caret_when_editable_disabled() const;
958
959 void set_move_caret_on_right_click_enabled(bool p_enabled);
960 bool is_move_caret_on_right_click_enabled() const;
961
962 void set_caret_mid_grapheme_enabled(bool p_enabled);
963 bool is_caret_mid_grapheme_enabled() const;
964
965 void set_multiple_carets_enabled(bool p_enabled);
966 bool is_multiple_carets_enabled() const;
967
968 int add_caret(int p_line, int p_column);
969 void remove_caret(int p_caret);
970 void remove_drag_caret();
971 void remove_secondary_carets();
972 int get_caret_count() const;
973 void add_caret_at_carets(bool p_below);
974
975 Vector<int> get_sorted_carets(bool p_include_ignored_carets = false) const;
976 void collapse_carets(int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_inclusive = false);
977
978 void merge_overlapping_carets();
979 void begin_multicaret_edit();
980 void end_multicaret_edit();
981 bool is_in_mulitcaret_edit() const;
982 bool multicaret_edit_ignore_caret(int p_caret) const;
983
984 bool is_caret_visible(int p_caret = 0) const;
985 Point2 get_caret_draw_pos(int p_caret = 0) const;
986
987 void set_caret_line(int p_line, bool p_adjust_viewport = true, bool p_can_be_hidden = true, int p_wrap_index = 0, int p_caret = 0);
988 int get_caret_line(int p_caret = 0) const;
989
990 void set_caret_column(int p_column, bool p_adjust_viewport = true, int p_caret = 0);
991 int get_caret_column(int p_caret = 0) const;
992 int get_next_composite_character_column(int p_line, int p_column) const;
993 int get_previous_composite_character_column(int p_line, int p_column) const;
994
995 int get_caret_wrap_index(int p_caret = 0) const;
996
997 String get_word_under_caret(int p_caret = -1) const;
998
999 /* Selection. */
1000 void set_selecting_enabled(bool p_enabled);
1001 bool is_selecting_enabled() const;
1002
1003 void set_deselect_on_focus_loss_enabled(bool p_enabled);
1004 bool is_deselect_on_focus_loss_enabled() const;
1005
1006 void set_drag_and_drop_selection_enabled(bool p_enabled);
1007 bool is_drag_and_drop_selection_enabled() const;
1008
1009 void set_selection_mode(SelectionMode p_mode);
1010 SelectionMode get_selection_mode() const;
1011
1012 void select_all();
1013 void select_word_under_caret(int p_caret = -1);
1014 void add_selection_for_next_occurrence();
1015 void skip_selection_for_next_occurrence();
1016 void select(int p_origin_line, int p_origin_column, int p_caret_line, int p_caret_column, int p_caret = 0);
1017
1018 bool has_selection(int p_caret = -1) const;
1019
1020 String get_selected_text(int p_caret = -1);
1021 int get_selection_at_line_column(int p_line, int p_column, bool p_include_edges = true, bool p_only_selections = true) const;
1022 Vector<Point2i> get_line_ranges_from_carets(bool p_only_selections = false, bool p_merge_adjacent = true) const;
1023 TypedArray<Vector2i> get_line_ranges_from_carets_typed_array(bool p_only_selections = false, bool p_merge_adjacent = true) const;
1024
1025 void set_selection_origin_line(int p_line, bool p_can_be_hidden = true, int p_wrap_index = -1, int p_caret = 0);
1026 void set_selection_origin_column(int p_column, int p_caret = 0);
1027 int get_selection_origin_line(int p_caret = 0) const;
1028 int get_selection_origin_column(int p_caret = 0) const;
1029
1030 int get_selection_from_line(int p_caret = 0) const;
1031 int get_selection_from_column(int p_caret = 0) const;
1032 int get_selection_to_line(int p_caret = 0) const;
1033 int get_selection_to_column(int p_caret = 0) const;
1034
1035 bool is_caret_after_selection_origin(int p_caret = 0) const;
1036
1037 void deselect(int p_caret = -1);
1038 void delete_selection(int p_caret = -1);
1039
1040 /* Line wrapping. */
1041 void set_line_wrapping_mode(LineWrappingMode p_wrapping_mode);
1042 LineWrappingMode get_line_wrapping_mode() const;
1043
1044 void set_autowrap_mode(TextServer::AutowrapMode p_mode);
1045 TextServer::AutowrapMode get_autowrap_mode() const;
1046
1047 bool is_line_wrapped(int p_line) const;
1048 int get_line_wrap_count(int p_line) const;
1049 int get_line_wrap_index_at_column(int p_line, int p_column) const;
1050
1051 Vector<String> get_line_wrapped_text(int p_line) const;
1052
1053 /* Viewport. */
1054 // Scrolling.
1055 void set_smooth_scroll_enabled(bool p_enabled);
1056 bool is_smooth_scroll_enabled() const;
1057
1058 void set_scroll_past_end_of_file_enabled(bool p_enabled);
1059 bool is_scroll_past_end_of_file_enabled() const;
1060
1061 VScrollBar *get_v_scroll_bar() const;
1062 HScrollBar *get_h_scroll_bar() const;
1063
1064 void set_v_scroll(double p_scroll);
1065 double get_v_scroll() const;
1066
1067 void set_h_scroll(int p_scroll);
1068 int get_h_scroll() const;
1069
1070 void set_v_scroll_speed(float p_speed);
1071 float get_v_scroll_speed() const;
1072
1073 void set_fit_content_height_enabled(bool p_enabled);
1074 bool is_fit_content_height_enabled() const;
1075
1076 void set_fit_content_width_enabled(bool p_enabled);
1077 bool is_fit_content_width_enabled() const;
1078
1079 double get_scroll_pos_for_line(int p_line, int p_wrap_index = 0) const;
1080
1081 // Visible lines.
1082 void set_line_as_first_visible(int p_line, int p_wrap_index = 0);
1083 int get_first_visible_line() const;
1084
1085 void set_line_as_center_visible(int p_line, int p_wrap_index = 0);
1086
1087 void set_line_as_last_visible(int p_line, int p_wrap_index = 0);
1088 int get_last_full_visible_line() const;
1089 int get_last_full_visible_line_wrap_index() const;
1090
1091 int get_visible_line_count() const;
1092 int get_visible_line_count_in_range(int p_from, int p_to) const;
1093 int get_total_visible_line_count() const;
1094
1095 // Auto Adjust
1096 void adjust_viewport_to_caret(int p_caret = 0);
1097 void center_viewport_to_caret(int p_caret = 0);
1098
1099 // Minimap
1100 void set_draw_minimap(bool p_enabled);
1101 bool is_drawing_minimap() const;
1102
1103 void set_minimap_width(int p_minimap_width);
1104 int get_minimap_width() const;
1105
1106 int get_minimap_visible_lines() const;
1107
1108 /* Gutters. */
1109 void add_gutter(int p_at = -1);
1110 void remove_gutter(int p_gutter);
1111 int get_gutter_count() const;
1112 Vector2i get_hovered_gutter() const { return hovered_gutter; }
1113
1114 void set_gutter_name(int p_gutter, const String &p_name);
1115 String get_gutter_name(int p_gutter) const;
1116
1117 void set_gutter_type(int p_gutter, GutterType p_type);
1118 GutterType get_gutter_type(int p_gutter) const;
1119
1120 void set_gutter_width(int p_gutter, int p_width);
1121 int get_gutter_width(int p_gutter) const;
1122 int get_total_gutter_width() const;
1123
1124 void set_gutter_draw(int p_gutter, bool p_draw);
1125 bool is_gutter_drawn(int p_gutter) const;
1126
1127 void set_gutter_clickable(int p_gutter, bool p_clickable);
1128 bool is_gutter_clickable(int p_gutter) const;
1129
1130 void set_gutter_overwritable(int p_gutter, bool p_overwritable);
1131 bool is_gutter_overwritable(int p_gutter) const;
1132
1133 void merge_gutters(int p_from_line, int p_to_line);
1134
1135 void set_gutter_custom_draw(int p_gutter, const Callable &p_draw_callback);
1136
1137 // Line gutters.
1138 void set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata);
1139 Variant get_line_gutter_metadata(int p_line, int p_gutter) const;
1140
1141 void set_line_gutter_text(int p_line, int p_gutter, const String &p_text);
1142 String get_line_gutter_text(int p_line, int p_gutter) const;
1143
1144 void set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon);
1145 Ref<Texture2D> get_line_gutter_icon(int p_line, int p_gutter) const;
1146
1147 void set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color);
1148 Color get_line_gutter_item_color(int p_line, int p_gutter) const;
1149
1150 void set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable);
1151 bool is_line_gutter_clickable(int p_line, int p_gutter) const;
1152
1153 // Line style
1154 void set_line_background_color(int p_line, const Color &p_color);
1155 Color get_line_background_color(int p_line) const;
1156
1157 /* Syntax Highlighting. */
1158 void set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter);
1159 Ref<SyntaxHighlighter> get_syntax_highlighter() const;
1160
1161 /* Visual. */
1162 void set_highlight_current_line(bool p_enabled);
1163 bool is_highlight_current_line_enabled() const;
1164
1165 void set_highlight_all_occurrences(bool p_enabled);
1166 bool is_highlight_all_occurrences_enabled() const;
1167
1168 void set_draw_control_chars(bool p_enabled);
1169 bool get_draw_control_chars() const;
1170
1171 void set_draw_tabs(bool p_enabled);
1172 bool is_drawing_tabs() const;
1173
1174 void set_draw_spaces(bool p_enabled);
1175 bool is_drawing_spaces() const;
1176
1177 Color get_font_color() const;
1178
1179 /* Behavior */
1180
1181 String get_default_word_separators() const;
1182
1183 void set_use_default_word_separators(bool p_enabled);
1184 bool is_default_word_separators_enabled() const;
1185
1186 void set_custom_word_separators(const String &p_separators);
1187 void set_use_custom_word_separators(bool p_enabled);
1188 bool is_custom_word_separators_enabled() const;
1189
1190 String get_custom_word_separators() const;
1191
1192 /* Deprecated. */
1193 #ifndef DISABLE_DEPRECATED
1194 Vector<int> get_caret_index_edit_order();
1195 void adjust_carets_after_edit(int p_caret, int p_from_line, int p_from_col, int p_to_line, int p_to_col);
1196
1197 int get_selection_line(int p_caret = 0) const;
1198 int get_selection_column(int p_caret = 0) const;
1199 #endif
1200
1201 TextEdit(const String &p_placeholder = String());
1202 ~TextEdit();
1203 };
1204
1205 VARIANT_ENUM_CAST(TextEdit::EditAction);
1206 VARIANT_ENUM_CAST(TextEdit::CaretType);
1207 VARIANT_ENUM_CAST(TextEdit::LineWrappingMode);
1208 VARIANT_ENUM_CAST(TextEdit::SelectionMode);
1209 VARIANT_ENUM_CAST(TextEdit::GutterType);
1210 VARIANT_ENUM_CAST(TextEdit::MenuItems);
1211 VARIANT_ENUM_CAST(TextEdit::SearchFlags);