text_edit.h 31 KB

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