text_edit.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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_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 = Ref<Texture2D>();
  128. String text = "";
  129. Color color = Color(1, 1, 1);
  130. };
  131. struct Line {
  132. Vector<Gutter> gutters;
  133. String data;
  134. Array bidi_override;
  135. Ref<TextParagraph> data_buf;
  136. Color background_color = Color(0, 0, 0, 0);
  137. bool hidden = false;
  138. int height = 0;
  139. int width = 0;
  140. Line() {
  141. data_buf.instantiate();
  142. }
  143. };
  144. private:
  145. bool is_dirty = false;
  146. bool tab_size_dirty = false;
  147. mutable Vector<Line> text;
  148. Ref<Font> font;
  149. int font_size = -1;
  150. int font_height = 0;
  151. String language;
  152. TextServer::Direction direction = TextServer::DIRECTION_AUTO;
  153. bool draw_control_chars = false;
  154. int line_height = -1;
  155. int max_width = -1;
  156. int width = -1;
  157. int tab_size = 4;
  158. int gutter_count = 0;
  159. void _calculate_line_height();
  160. void _calculate_max_line_width();
  161. public:
  162. void set_tab_size(int p_tab_size);
  163. int get_tab_size() const;
  164. void set_font(const Ref<Font> &p_font);
  165. void set_font_size(int p_font_size);
  166. void set_direction_and_language(TextServer::Direction p_direction, const String &p_language);
  167. void set_draw_control_chars(bool p_enabled);
  168. int get_line_height() const;
  169. int get_line_width(int p_line, int p_wrap_index = -1) const;
  170. int get_max_width() const;
  171. void set_width(float p_width);
  172. float get_width() const;
  173. int get_line_wrap_amount(int p_line) const;
  174. Vector<Vector2i> get_line_wrap_ranges(int p_line) const;
  175. const Ref<TextParagraph> get_line_data(int p_line) const;
  176. void set(int p_line, const String &p_text, const Array &p_bidi_override);
  177. void set_hidden(int p_line, bool p_hidden) {
  178. if (text[p_line].hidden == p_hidden) {
  179. return;
  180. }
  181. text.write[p_line].hidden = p_hidden;
  182. if (!p_hidden && text[p_line].width > max_width) {
  183. max_width = text[p_line].width;
  184. } else if (p_hidden && text[p_line].width == max_width) {
  185. _calculate_max_line_width();
  186. }
  187. }
  188. bool is_hidden(int p_line) const { return text[p_line].hidden; }
  189. void insert(int p_at, const Vector<String> &p_text, const Vector<Array> &p_bidi_override);
  190. void remove_range(int p_from_line, int p_to_line);
  191. int size() const { return text.size(); }
  192. void clear();
  193. 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());
  194. void invalidate_font();
  195. void invalidate_all();
  196. void invalidate_all_lines();
  197. _FORCE_INLINE_ const String &operator[](int p_line) const;
  198. /* Gutters. */
  199. void add_gutter(int p_at);
  200. void remove_gutter(int p_gutter);
  201. void move_gutters(int p_from_line, int p_to_line);
  202. 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; }
  203. const Variant &get_line_gutter_metadata(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].metadata; }
  204. 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; }
  205. const String &get_line_gutter_text(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].text; }
  206. 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; }
  207. const Ref<Texture2D> &get_line_gutter_icon(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].icon; }
  208. 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; }
  209. const Color &get_line_gutter_item_color(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].color; }
  210. 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; }
  211. bool is_line_gutter_clickable(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].clickable; }
  212. /* Line style. */
  213. void set_line_background_color(int p_line, const Color &p_color) { text.write[p_line].background_color = p_color; }
  214. const Color get_line_background_color(int p_line) const { return text[p_line].background_color; }
  215. };
  216. /* Text */
  217. Text text;
  218. bool setting_text = false;
  219. bool alt_start = false;
  220. uint32_t alt_code = 0;
  221. // Text properties.
  222. String ime_text = "";
  223. Point2 ime_selection;
  224. // Placeholder
  225. String placeholder_text = "";
  226. Array placeholder_bidi_override;
  227. Ref<TextParagraph> placeholder_data_buf;
  228. int placeholder_line_height = -1;
  229. int placeholder_max_width = -1;
  230. Vector<String> placeholder_wraped_rows;
  231. void _update_placeholder();
  232. /* Initialize to opposite first, so we get past the early-out in set_editable. */
  233. bool editable = false;
  234. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  235. TextDirection input_direction = TEXT_DIRECTION_LTR;
  236. String language = "";
  237. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  238. Array st_args;
  239. void _clear();
  240. void _update_caches();
  241. // User control.
  242. bool overtype_mode = false;
  243. bool context_menu_enabled = true;
  244. bool shortcut_keys_enabled = true;
  245. bool virtual_keyboard_enabled = true;
  246. bool middle_mouse_paste_enabled = true;
  247. // Overridable actions.
  248. String cut_copy_line = "";
  249. // Context menu.
  250. PopupMenu *menu = nullptr;
  251. PopupMenu *menu_dir = nullptr;
  252. PopupMenu *menu_ctl = nullptr;
  253. Key _get_menu_action_accelerator(const String &p_action);
  254. void _generate_context_menu();
  255. void _update_context_menu();
  256. /* Versioning */
  257. struct Caret;
  258. struct TextOperation {
  259. enum Type {
  260. TYPE_NONE,
  261. TYPE_INSERT,
  262. TYPE_REMOVE
  263. };
  264. Vector<Caret> start_carets;
  265. Vector<Caret> end_carets;
  266. Type type = TYPE_NONE;
  267. int from_line = 0;
  268. int from_column = 0;
  269. int to_line = 0;
  270. int to_column = 0;
  271. String text;
  272. uint32_t prev_version = 0;
  273. uint32_t version = 0;
  274. bool chain_forward = false;
  275. bool chain_backward = false;
  276. };
  277. bool undo_enabled = true;
  278. int undo_stack_max_size = 50;
  279. EditAction current_action = EditAction::ACTION_NONE;
  280. bool pending_action_end = false;
  281. bool in_action = false;
  282. int complex_operation_count = 0;
  283. bool next_operation_is_complex = false;
  284. TextOperation current_op;
  285. List<TextOperation> undo_stack;
  286. List<TextOperation>::Element *undo_stack_pos = nullptr;
  287. Timer *idle_detect = nullptr;
  288. uint32_t version = 0;
  289. uint32_t saved_version = 0;
  290. void _push_current_op();
  291. void _do_text_op(const TextOperation &p_op, bool p_reverse);
  292. void _clear_redo();
  293. /* Search */
  294. Color search_result_color = Color(1, 1, 1);
  295. Color search_result_border_color = Color(1, 1, 1);
  296. String search_text = "";
  297. uint32_t search_flags = 0;
  298. int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) const;
  299. /* Tooltip. */
  300. Callable tooltip_callback;
  301. /* Mouse */
  302. struct LineDrawingCache {
  303. int y_offset = 0;
  304. Vector<int> first_visible_chars;
  305. Vector<int> last_visible_chars;
  306. };
  307. HashMap<int, LineDrawingCache> line_drawing_cache;
  308. int _get_char_pos_for_line(int p_px, int p_line, int p_wrap_index = 0) const;
  309. /* Caret. */
  310. struct Selection {
  311. bool active = false;
  312. bool shiftclick_left = false;
  313. int selecting_line = 0;
  314. int selecting_column = 0;
  315. int selected_word_beg = 0;
  316. int selected_word_end = 0;
  317. int selected_word_origin = 0;
  318. int from_line = 0;
  319. int from_column = 0;
  320. int to_line = 0;
  321. int to_column = 0;
  322. };
  323. struct Caret {
  324. Selection selection;
  325. Point2 draw_pos;
  326. bool visible = false;
  327. int last_fit_x = 0;
  328. int line = 0;
  329. int column = 0;
  330. };
  331. // Vector containing all the carets, index '0' is the "main caret" and should never be removed.
  332. Vector<Caret> carets;
  333. Vector<int> caret_index_edit_order;
  334. bool setting_caret_line = false;
  335. bool caret_pos_dirty = false;
  336. bool caret_index_edit_dirty = true;
  337. Color caret_color = Color(1, 1, 1);
  338. Color caret_background_color = Color(0, 0, 0);
  339. CaretType caret_type = CaretType::CARET_TYPE_LINE;
  340. bool draw_caret = true;
  341. bool caret_blink_enabled = false;
  342. Timer *caret_blink_timer = nullptr;
  343. bool move_caret_on_right_click = true;
  344. bool caret_mid_grapheme_enabled = true;
  345. bool multi_carets_enabled = true;
  346. bool drag_action = false;
  347. bool drag_caret_force_displayed = false;
  348. void _emit_caret_changed();
  349. void _reset_caret_blink_timer();
  350. void _toggle_draw_caret();
  351. int _get_column_x_offset_for_line(int p_char, int p_line, int p_column) const;
  352. /* Selection. */
  353. SelectionMode selecting_mode = SelectionMode::SELECTION_MODE_NONE;
  354. bool selecting_enabled = true;
  355. bool deselect_on_focus_loss_enabled = true;
  356. bool drag_and_drop_selection_enabled = true;
  357. Color font_selected_color = Color(0, 0, 0, 0);
  358. Color selection_color = Color(1, 1, 1);
  359. bool use_selected_font_color = false;
  360. bool selection_drag_attempt = false;
  361. bool dragging_selection = false;
  362. Timer *click_select_held = nullptr;
  363. uint64_t last_dblclk = 0;
  364. Vector2 last_dblclk_pos;
  365. void _click_selection_held();
  366. void _update_selection_mode_pointer();
  367. void _update_selection_mode_word();
  368. void _update_selection_mode_line();
  369. void _pre_shift_selection(int p_caret);
  370. void _post_shift_selection(int p_caret);
  371. /* Line wrapping. */
  372. LineWrappingMode line_wrapping_mode = LineWrappingMode::LINE_WRAPPING_NONE;
  373. int wrap_at_column = 0;
  374. int wrap_right_offset = 10;
  375. void _update_wrap_at_column(bool p_force = false);
  376. /* Viewport. */
  377. HScrollBar *h_scroll = nullptr;
  378. VScrollBar *v_scroll = nullptr;
  379. float content_height_cache = 0.0;
  380. bool fit_content_height = false;
  381. bool scroll_past_end_of_file_enabled = false;
  382. // Smooth scrolling.
  383. bool smooth_scroll_enabled = false;
  384. float target_v_scroll = 0.0;
  385. float v_scroll_speed = 80.0;
  386. // Scrolling.
  387. int first_visible_line = 0;
  388. int first_visible_line_wrap_ofs = 0;
  389. int first_visible_col = 0;
  390. bool scrolling = false;
  391. bool updating_scrolls = false;
  392. void _update_scrollbars();
  393. int _get_control_height() const;
  394. void _v_scroll_input();
  395. void _scroll_moved(double p_to_val);
  396. double _get_visible_lines_offset() const;
  397. double _get_v_scroll_offset() const;
  398. void _scroll_up(real_t p_delta);
  399. void _scroll_down(real_t p_delta);
  400. void _scroll_lines_up();
  401. void _scroll_lines_down();
  402. // Minimap.
  403. bool draw_minimap = false;
  404. int minimap_width = 80;
  405. Point2 minimap_char_size = Point2(1, 2);
  406. int minimap_line_spacing = 1;
  407. // Minimap scroll.
  408. bool minimap_clicked = false;
  409. bool hovering_minimap = false;
  410. bool dragging_minimap = false;
  411. bool can_drag_minimap = false;
  412. double minimap_scroll_ratio = 0.0;
  413. double minimap_scroll_click_pos = 0.0;
  414. void _update_minimap_hover();
  415. void _update_minimap_click();
  416. void _update_minimap_drag();
  417. /* Gutters. */
  418. Vector<GutterInfo> gutters;
  419. int gutters_width = 0;
  420. int gutter_padding = 0;
  421. Vector2i hovered_gutter = Vector2i(-1, -1); // X = gutter index, Y = row.
  422. void _update_gutter_width();
  423. /* Syntax highlighting. */
  424. Ref<SyntaxHighlighter> syntax_highlighter;
  425. Dictionary _get_line_syntax_highlighting(int p_line);
  426. /* Visual. */
  427. Ref<StyleBox> style_normal;
  428. Ref<StyleBox> style_focus;
  429. Ref<StyleBox> style_readonly;
  430. Ref<Texture2D> tab_icon;
  431. Ref<Texture2D> space_icon;
  432. Ref<Font> font;
  433. int font_size = 16;
  434. Color font_color = Color(1, 1, 1);
  435. Color font_readonly_color = Color(1, 1, 1);
  436. Color font_placeholder_color = Color(1, 1, 1, 0.6);
  437. int outline_size = 0;
  438. Color outline_color = Color(1, 1, 1);
  439. int line_spacing = 1;
  440. Color background_color = Color(1, 1, 1);
  441. Color current_line_color = Color(1, 1, 1);
  442. Color word_highlighted_color = Color(1, 1, 1);
  443. bool window_has_focus = true;
  444. bool first_draw = true;
  445. bool highlight_current_line = false;
  446. bool highlight_all_occurrences = false;
  447. bool draw_control_chars = false;
  448. bool draw_tabs = false;
  449. bool draw_spaces = false;
  450. /*** Super internal Core API. Everything builds on it. ***/
  451. bool text_changed_dirty = false;
  452. void _text_changed_emit();
  453. void _insert_text(int p_line, int p_char, const String &p_text, int *r_end_line = nullptr, int *r_end_char = nullptr);
  454. void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  455. void _base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column);
  456. String _base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const;
  457. void _base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  458. /* Input actions. */
  459. void _swap_current_input_direction();
  460. void _new_line(bool p_split_current = true, bool p_above = false);
  461. void _move_caret_left(bool p_select, bool p_move_by_word = false);
  462. void _move_caret_right(bool p_select, bool p_move_by_word = false);
  463. void _move_caret_up(bool p_select);
  464. void _move_caret_down(bool p_select);
  465. void _move_caret_to_line_start(bool p_select);
  466. void _move_caret_to_line_end(bool p_select);
  467. void _move_caret_page_up(bool p_select);
  468. void _move_caret_page_down(bool p_select);
  469. void _do_backspace(bool p_word = false, bool p_all_to_left = false);
  470. void _delete(bool p_word = false, bool p_all_to_right = false);
  471. void _move_caret_document_start(bool p_select);
  472. void _move_caret_document_end(bool p_select);
  473. bool _clear_carets_and_selection();
  474. // Used in add_caret_at_carets
  475. void _get_above_below_caret_line_column(int p_old_line, int p_old_wrap_index, int p_old_column, bool p_below, int &p_new_line, int &p_new_column, int p_last_fit_x = -1) const;
  476. protected:
  477. void _notification(int p_what);
  478. static void _bind_methods();
  479. /* Internal API for CodeEdit, pending public API. */
  480. // brace matching
  481. struct BraceMatchingData {
  482. int open_match_line = -1;
  483. int open_match_column = -1;
  484. bool open_matching = false;
  485. bool open_mismatch = false;
  486. int close_match_line = -1;
  487. int close_match_column = -1;
  488. bool close_matching = false;
  489. bool close_mismatch = false;
  490. };
  491. bool highlight_matching_braces_enabled = false;
  492. Color brace_mismatch_color;
  493. // Line hiding.
  494. Color code_folding_color = Color(1, 1, 1);
  495. Ref<Texture2D> folded_eol_icon;
  496. bool hiding_enabled = false;
  497. void _set_hiding_enabled(bool p_enabled);
  498. bool _is_hiding_enabled() const;
  499. void _set_line_as_hidden(int p_line, bool p_hidden);
  500. bool _is_line_hidden(int p_line) const;
  501. void _unhide_all_lines();
  502. // Symbol lookup.
  503. String lookup_symbol_word;
  504. void _set_symbol_lookup_word(const String &p_symbol);
  505. /* Text manipulation */
  506. // Overridable actions
  507. virtual void _handle_unicode_input_internal(const uint32_t p_unicode, int p_caret);
  508. virtual void _backspace_internal(int p_caret);
  509. virtual void _cut_internal(int p_caret);
  510. virtual void _copy_internal(int p_caret);
  511. virtual void _paste_internal(int p_caret);
  512. virtual void _paste_primary_clipboard_internal(int p_caret);
  513. GDVIRTUAL2(_handle_unicode_input, int, int)
  514. GDVIRTUAL1(_backspace, int)
  515. GDVIRTUAL1(_cut, int)
  516. GDVIRTUAL1(_copy, int)
  517. GDVIRTUAL1(_paste, int)
  518. GDVIRTUAL1(_paste_primary_clipboard, int)
  519. public:
  520. /* General overrides. */
  521. virtual void unhandled_key_input(const Ref<InputEvent> &p_event) override;
  522. virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
  523. bool alt_input(const Ref<InputEvent> &p_gui_input);
  524. virtual Size2 get_minimum_size() const override;
  525. virtual bool is_text_field() const override;
  526. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  527. virtual Variant get_drag_data(const Point2 &p_point) override;
  528. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  529. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
  530. virtual String get_tooltip(const Point2 &p_pos) const override;
  531. void set_tooltip_request_func(const Callable &p_tooltip_callback);
  532. /* Text */
  533. // Text properties.
  534. bool has_ime_text() const;
  535. void set_editable(const bool p_editable);
  536. bool is_editable() const;
  537. void set_text_direction(TextDirection p_text_direction);
  538. TextDirection get_text_direction() const;
  539. void set_language(const String &p_language);
  540. String get_language() const;
  541. void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
  542. TextServer::StructuredTextParser get_structured_text_bidi_override() const;
  543. void set_structured_text_bidi_override_options(Array p_args);
  544. Array get_structured_text_bidi_override_options() const;
  545. void set_tab_size(const int p_size);
  546. int get_tab_size() const;
  547. // User controls
  548. void set_overtype_mode_enabled(const bool p_enabled);
  549. bool is_overtype_mode_enabled() const;
  550. void set_context_menu_enabled(bool p_enabled);
  551. bool is_context_menu_enabled() const;
  552. void set_shortcut_keys_enabled(bool p_enabled);
  553. bool is_shortcut_keys_enabled() const;
  554. void set_virtual_keyboard_enabled(bool p_enabled);
  555. bool is_virtual_keyboard_enabled() const;
  556. void set_middle_mouse_paste_enabled(bool p_enabled);
  557. bool is_middle_mouse_paste_enabled() const;
  558. // Text manipulation
  559. void clear();
  560. void set_text(const String &p_text);
  561. String get_text() const;
  562. int get_line_count() const;
  563. void set_placeholder(const String &p_text);
  564. String get_placeholder() const;
  565. void set_line(int p_line, const String &p_new_text);
  566. String get_line(int p_line) const;
  567. int get_line_width(int p_line, int p_wrap_index = -1) const;
  568. int get_line_height() const;
  569. int get_indent_level(int p_line) const;
  570. int get_first_non_whitespace_column(int p_line) const;
  571. void swap_lines(int p_from_line, int p_to_line);
  572. void insert_line_at(int p_at, const String &p_text);
  573. void insert_text_at_caret(const String &p_text, int p_caret = -1);
  574. void remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  575. int get_last_unhidden_line() const;
  576. int get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const;
  577. Point2i get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const;
  578. // Overridable actions
  579. void handle_unicode_input(const uint32_t p_unicode, int p_caret = -1);
  580. void backspace(int p_caret = -1);
  581. void cut(int p_caret = -1);
  582. void copy(int p_caret = -1);
  583. void paste(int p_caret = -1);
  584. void paste_primary_clipboard(int p_caret = -1);
  585. // Context menu.
  586. PopupMenu *get_menu() const;
  587. bool is_menu_visible() const;
  588. void menu_option(int p_option);
  589. /* Versioning */
  590. void start_action(EditAction p_action);
  591. void end_action();
  592. EditAction get_current_action() const;
  593. void begin_complex_operation();
  594. void end_complex_operation();
  595. bool has_undo() const;
  596. bool has_redo() const;
  597. void undo();
  598. void redo();
  599. void clear_undo_history();
  600. bool is_insert_text_operation() const;
  601. void tag_saved_version();
  602. uint32_t get_version() const;
  603. uint32_t get_saved_version() const;
  604. /* Search */
  605. void set_search_text(const String &p_search_text);
  606. void set_search_flags(uint32_t p_flags);
  607. Point2i search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const;
  608. /* Mouse */
  609. Point2 get_local_mouse_pos() const;
  610. String get_word_at_pos(const Vector2 &p_pos) const;
  611. Point2i get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_of_bounds = true) const;
  612. Point2i get_pos_at_line_column(int p_line, int p_column) const;
  613. Rect2i get_rect_at_line_column(int p_line, int p_column) const;
  614. int get_minimap_line_at_pos(const Point2i &p_pos) const;
  615. bool is_dragging_cursor() const;
  616. bool is_mouse_over_selection(bool p_edges = true, int p_caret = -1) const;
  617. /* Caret */
  618. void set_caret_type(CaretType p_type);
  619. CaretType get_caret_type() const;
  620. void set_caret_blink_enabled(const bool p_enabled);
  621. bool is_caret_blink_enabled() const;
  622. void set_caret_blink_interval(const float p_interval);
  623. float get_caret_blink_interval() const;
  624. void set_move_caret_on_right_click_enabled(const bool p_enabled);
  625. bool is_move_caret_on_right_click_enabled() const;
  626. void set_caret_mid_grapheme_enabled(const bool p_enabled);
  627. bool is_caret_mid_grapheme_enabled() const;
  628. void set_multiple_carets_enabled(bool p_enabled);
  629. bool is_multiple_carets_enabled() const;
  630. int add_caret(int p_line, int p_col);
  631. void remove_caret(int p_caret);
  632. void remove_secondary_carets();
  633. void merge_overlapping_carets();
  634. int get_caret_count() const;
  635. void add_caret_at_carets(bool p_below);
  636. Vector<int> get_caret_index_edit_order();
  637. void adjust_carets_after_edit(int p_caret, int p_from_line, int p_from_col, int p_to_line, int p_to_col);
  638. bool is_caret_visible(int p_caret = 0) const;
  639. Point2 get_caret_draw_pos(int p_caret = 0) const;
  640. 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);
  641. int get_caret_line(int p_caret = 0) const;
  642. void set_caret_column(int p_col, bool p_adjust_viewport = true, int p_caret = 0);
  643. int get_caret_column(int p_caret = 0) const;
  644. int get_caret_wrap_index(int p_caret = 0) const;
  645. String get_word_under_caret(int p_caret = -1) const;
  646. /* Selection. */
  647. void set_selecting_enabled(const bool p_enabled);
  648. bool is_selecting_enabled() const;
  649. void set_deselect_on_focus_loss_enabled(const bool p_enabled);
  650. bool is_deselect_on_focus_loss_enabled() const;
  651. void set_drag_and_drop_selection_enabled(const bool p_enabled);
  652. bool is_drag_and_drop_selection_enabled() const;
  653. void set_override_selected_font_color(bool p_override_selected_font_color);
  654. bool is_overriding_selected_font_color() const;
  655. void set_selection_mode(SelectionMode p_mode, int p_line = -1, int p_column = -1, int p_caret = 0);
  656. SelectionMode get_selection_mode() const;
  657. void select_all();
  658. void select_word_under_caret(int p_caret = -1);
  659. void add_selection_for_next_occurrence();
  660. void select(int p_from_line, int p_from_column, int p_to_line, int p_to_column, int p_caret = 0);
  661. bool has_selection(int p_caret = -1) const;
  662. String get_selected_text(int p_caret = -1);
  663. int get_selection_line(int p_caret = 0) const;
  664. int get_selection_column(int p_caret = 0) const;
  665. int get_selection_from_line(int p_caret = 0) const;
  666. int get_selection_from_column(int p_caret = 0) const;
  667. int get_selection_to_line(int p_caret = 0) const;
  668. int get_selection_to_column(int p_caret = 0) const;
  669. void deselect(int p_caret = -1);
  670. void delete_selection(int p_caret = -1);
  671. /* Line wrapping. */
  672. void set_line_wrapping_mode(LineWrappingMode p_wrapping_mode);
  673. LineWrappingMode get_line_wrapping_mode() const;
  674. bool is_line_wrapped(int p_line) const;
  675. int get_line_wrap_count(int p_line) const;
  676. int get_line_wrap_index_at_column(int p_line, int p_column) const;
  677. Vector<String> get_line_wrapped_text(int p_line) const;
  678. /* Viewport. */
  679. // Scrolling.
  680. void set_smooth_scroll_enabled(const bool p_enabled);
  681. bool is_smooth_scroll_enabled() const;
  682. void set_scroll_past_end_of_file_enabled(const bool p_enabled);
  683. bool is_scroll_past_end_of_file_enabled() const;
  684. VScrollBar *get_v_scroll_bar() const;
  685. HScrollBar *get_h_scroll_bar() const;
  686. void set_v_scroll(double p_scroll);
  687. double get_v_scroll() const;
  688. void set_h_scroll(int p_scroll);
  689. int get_h_scroll() const;
  690. void set_v_scroll_speed(float p_speed);
  691. float get_v_scroll_speed() const;
  692. void set_fit_content_height_enabled(const bool p_enabled);
  693. bool is_fit_content_height_enabled() const;
  694. double get_scroll_pos_for_line(int p_line, int p_wrap_index = 0) const;
  695. // Visible lines.
  696. void set_line_as_first_visible(int p_line, int p_wrap_index = 0);
  697. int get_first_visible_line() const;
  698. void set_line_as_center_visible(int p_line, int p_wrap_index = 0);
  699. void set_line_as_last_visible(int p_line, int p_wrap_index = 0);
  700. int get_last_full_visible_line() const;
  701. int get_last_full_visible_line_wrap_index() const;
  702. int get_visible_line_count() const;
  703. int get_visible_line_count_in_range(int p_from, int p_to) const;
  704. int get_total_visible_line_count() const;
  705. // Auto Adjust
  706. void adjust_viewport_to_caret(int p_caret = 0);
  707. void center_viewport_to_caret(int p_caret = 0);
  708. // Minimap
  709. void set_draw_minimap(bool p_enabled);
  710. bool is_drawing_minimap() const;
  711. void set_minimap_width(int p_minimap_width);
  712. int get_minimap_width() const;
  713. int get_minimap_visible_lines() const;
  714. /* Gutters. */
  715. void add_gutter(int p_at = -1);
  716. void remove_gutter(int p_gutter);
  717. int get_gutter_count() const;
  718. void set_gutter_name(int p_gutter, const String &p_name);
  719. String get_gutter_name(int p_gutter) const;
  720. void set_gutter_type(int p_gutter, GutterType p_type);
  721. GutterType get_gutter_type(int p_gutter) const;
  722. void set_gutter_width(int p_gutter, int p_width);
  723. int get_gutter_width(int p_gutter) const;
  724. int get_total_gutter_width() const;
  725. void set_gutter_draw(int p_gutter, bool p_draw);
  726. bool is_gutter_drawn(int p_gutter) const;
  727. void set_gutter_clickable(int p_gutter, bool p_clickable);
  728. bool is_gutter_clickable(int p_gutter) const;
  729. void set_gutter_overwritable(int p_gutter, bool p_overwritable);
  730. bool is_gutter_overwritable(int p_gutter) const;
  731. void merge_gutters(int p_from_line, int p_to_line);
  732. void set_gutter_custom_draw(int p_gutter, const Callable &p_draw_callback);
  733. // Line gutters.
  734. void set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata);
  735. Variant get_line_gutter_metadata(int p_line, int p_gutter) const;
  736. void set_line_gutter_text(int p_line, int p_gutter, const String &p_text);
  737. String get_line_gutter_text(int p_line, int p_gutter) const;
  738. void set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon);
  739. Ref<Texture2D> get_line_gutter_icon(int p_line, int p_gutter) const;
  740. void set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color);
  741. Color get_line_gutter_item_color(int p_line, int p_gutter) const;
  742. void set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable);
  743. bool is_line_gutter_clickable(int p_line, int p_gutter) const;
  744. // Line style
  745. void set_line_background_color(int p_line, const Color &p_color);
  746. Color get_line_background_color(int p_line) const;
  747. /* Syntax Highlighting. */
  748. void set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter);
  749. Ref<SyntaxHighlighter> get_syntax_highlighter() const;
  750. /* Visual. */
  751. void set_highlight_current_line(bool p_enabled);
  752. bool is_highlight_current_line_enabled() const;
  753. void set_highlight_all_occurrences(const bool p_enabled);
  754. bool is_highlight_all_occurrences_enabled() const;
  755. void set_draw_control_chars(bool p_enabled);
  756. bool get_draw_control_chars() const;
  757. void set_draw_tabs(bool p_enabled);
  758. bool is_drawing_tabs() const;
  759. void set_draw_spaces(bool p_enabled);
  760. bool is_drawing_spaces() const;
  761. TextEdit(const String &p_placeholder = String());
  762. };
  763. VARIANT_ENUM_CAST(TextEdit::EditAction);
  764. VARIANT_ENUM_CAST(TextEdit::CaretType);
  765. VARIANT_ENUM_CAST(TextEdit::LineWrappingMode);
  766. VARIANT_ENUM_CAST(TextEdit::SelectionMode);
  767. VARIANT_ENUM_CAST(TextEdit::GutterType);
  768. VARIANT_ENUM_CAST(TextEdit::MenuItems);
  769. VARIANT_ENUM_CAST(TextEdit::SearchFlags);
  770. #endif // TEXT_EDIT_H