text_edit.h 36 KB

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