text_edit.h 35 KB

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