text_edit.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*************************************************************************/
  2. /* text_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. /* Caret. */
  42. enum CaretType {
  43. CARET_TYPE_LINE,
  44. CARET_TYPE_BLOCK
  45. };
  46. /* Selection */
  47. enum SelectionMode {
  48. SELECTION_MODE_NONE,
  49. SELECTION_MODE_SHIFT,
  50. SELECTION_MODE_POINTER,
  51. SELECTION_MODE_WORD,
  52. SELECTION_MODE_LINE
  53. };
  54. /* Line Wrapping.*/
  55. enum LineWrappingMode {
  56. LINE_WRAPPING_NONE,
  57. LINE_WRAPPING_BOUNDARY
  58. };
  59. /* Gutters. */
  60. enum GutterType {
  61. GUTTER_TYPE_STRING,
  62. GUTTER_TYPE_ICON,
  63. GUTTER_TYPE_CUSTOM
  64. };
  65. /* Contex Menu. */
  66. enum MenuItems {
  67. MENU_CUT,
  68. MENU_COPY,
  69. MENU_PASTE,
  70. MENU_CLEAR,
  71. MENU_SELECT_ALL,
  72. MENU_UNDO,
  73. MENU_REDO,
  74. MENU_DIR_INHERITED,
  75. MENU_DIR_AUTO,
  76. MENU_DIR_LTR,
  77. MENU_DIR_RTL,
  78. MENU_DISPLAY_UCC,
  79. MENU_INSERT_LRM,
  80. MENU_INSERT_RLM,
  81. MENU_INSERT_LRE,
  82. MENU_INSERT_RLE,
  83. MENU_INSERT_LRO,
  84. MENU_INSERT_RLO,
  85. MENU_INSERT_PDF,
  86. MENU_INSERT_ALM,
  87. MENU_INSERT_LRI,
  88. MENU_INSERT_RLI,
  89. MENU_INSERT_FSI,
  90. MENU_INSERT_PDI,
  91. MENU_INSERT_ZWJ,
  92. MENU_INSERT_ZWNJ,
  93. MENU_INSERT_WJ,
  94. MENU_INSERT_SHY,
  95. MENU_MAX
  96. };
  97. /* Search. */
  98. enum SearchFlags {
  99. SEARCH_MATCH_CASE = 1,
  100. SEARCH_WHOLE_WORDS = 2,
  101. SEARCH_BACKWARDS = 4
  102. };
  103. private:
  104. struct GutterInfo {
  105. GutterType type = GutterType::GUTTER_TYPE_STRING;
  106. String name = "";
  107. int width = 24;
  108. bool draw = true;
  109. bool clickable = false;
  110. bool overwritable = false;
  111. ObjectID custom_draw_obj = ObjectID();
  112. StringName custom_draw_callback;
  113. };
  114. class Text {
  115. public:
  116. struct Gutter {
  117. Variant metadata;
  118. bool clickable = false;
  119. Ref<Texture2D> icon = Ref<Texture2D>();
  120. String text = "";
  121. Color color = Color(1, 1, 1);
  122. };
  123. struct Line {
  124. Vector<Gutter> gutters;
  125. String data;
  126. Array bidi_override;
  127. Ref<TextParagraph> data_buf;
  128. Color background_color = Color(0, 0, 0, 0);
  129. bool hidden = false;
  130. int height = 0;
  131. int width = 0;
  132. Line() {
  133. data_buf.instantiate();
  134. }
  135. };
  136. private:
  137. bool is_dirty = false;
  138. bool tab_size_dirty = false;
  139. mutable Vector<Line> text;
  140. Ref<Font> font;
  141. int font_size = -1;
  142. Dictionary opentype_features;
  143. String language;
  144. TextServer::Direction direction = TextServer::DIRECTION_AUTO;
  145. bool draw_control_chars = false;
  146. int line_height = -1;
  147. int max_width = -1;
  148. int width = -1;
  149. int tab_size = 4;
  150. int gutter_count = 0;
  151. void _calculate_line_height();
  152. void _calculate_max_line_width();
  153. public:
  154. void set_tab_size(int p_tab_size);
  155. int get_tab_size() const;
  156. void set_font(const Ref<Font> &p_font);
  157. void set_font_size(int p_font_size);
  158. void set_font_features(const Dictionary &p_features);
  159. void set_direction_and_language(TextServer::Direction p_direction, const String &p_language);
  160. void set_draw_control_chars(bool p_draw_control_chars);
  161. int get_line_height() const;
  162. int get_line_width(int p_line, int p_wrap_index = -1) const;
  163. int get_max_width() const;
  164. void set_width(float p_width);
  165. int get_line_wrap_amount(int p_line) const;
  166. Vector<Vector2i> get_line_wrap_ranges(int p_line) const;
  167. const Ref<TextParagraph> get_line_data(int p_line) const;
  168. void set(int p_line, const String &p_text, const Array &p_bidi_override);
  169. void set_hidden(int p_line, bool p_hidden) {
  170. text.write[p_line].hidden = p_hidden;
  171. if (!p_hidden && text[p_line].width > max_width) {
  172. max_width = text[p_line].width;
  173. } else if (p_hidden && text[p_line].width == max_width) {
  174. _calculate_max_line_width();
  175. }
  176. }
  177. bool is_hidden(int p_line) const { return text[p_line].hidden; }
  178. void insert(int p_at, const String &p_text, const Array &p_bidi_override);
  179. void remove(int p_at);
  180. int size() const { return text.size(); }
  181. void clear();
  182. void invalidate_cache(int p_line, int p_column = -1, const String &p_ime_text = String(), const Array &p_bidi_override = Array());
  183. void invalidate_all();
  184. void invalidate_all_lines();
  185. _FORCE_INLINE_ const String &operator[](int p_line) const;
  186. /* Gutters. */
  187. void add_gutter(int p_at);
  188. void remove_gutter(int p_gutter);
  189. void move_gutters(int p_from_line, int p_to_line);
  190. 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; }
  191. const Variant &get_line_gutter_metadata(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].metadata; }
  192. 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; }
  193. const String &get_line_gutter_text(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].text; }
  194. 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; }
  195. const Ref<Texture2D> &get_line_gutter_icon(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].icon; }
  196. 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; }
  197. const Color &get_line_gutter_item_color(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].color; }
  198. 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; }
  199. bool is_line_gutter_clickable(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].clickable; }
  200. /* Line style. */
  201. void set_line_background_color(int p_line, const Color &p_color) { text.write[p_line].background_color = p_color; }
  202. const Color get_line_background_color(int p_line) const { return text[p_line].background_color; }
  203. };
  204. /* Text */
  205. Text text;
  206. bool setting_text = false;
  207. // Text properties.
  208. String ime_text = "";
  209. Point2 ime_selection;
  210. /* Initialise to opposite first, so we get past the early-out in set_editable. */
  211. bool editable = false;
  212. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  213. TextDirection input_direction = TEXT_DIRECTION_LTR;
  214. Dictionary opentype_features;
  215. String language = "";
  216. Control::StructuredTextParser st_parser = STRUCTURED_TEXT_DEFAULT;
  217. Array st_args;
  218. void _clear();
  219. void _update_caches();
  220. // User control.
  221. bool overtype_mode = false;
  222. bool context_menu_enabled = true;
  223. bool shortcut_keys_enabled = true;
  224. bool virtual_keyboard_enabled = true;
  225. // Overridable actions
  226. String cut_copy_line = "";
  227. // Context menu.
  228. PopupMenu *menu = nullptr;
  229. PopupMenu *menu_dir = nullptr;
  230. PopupMenu *menu_ctl = nullptr;
  231. void _generate_context_menu();
  232. int _get_menu_action_accelerator(const String &p_action);
  233. /* Versioning */
  234. struct TextOperation {
  235. enum Type {
  236. TYPE_NONE,
  237. TYPE_INSERT,
  238. TYPE_REMOVE
  239. };
  240. Type type = TYPE_NONE;
  241. int from_line = 0;
  242. int from_column = 0;
  243. int to_line = 0;
  244. int to_column = 0;
  245. String text;
  246. uint32_t prev_version = 0;
  247. uint32_t version = 0;
  248. bool chain_forward = false;
  249. bool chain_backward = false;
  250. };
  251. bool undo_enabled = true;
  252. int undo_stack_max_size = 50;
  253. int complex_operation_count = 0;
  254. bool next_operation_is_complex = false;
  255. TextOperation current_op;
  256. List<TextOperation> undo_stack;
  257. List<TextOperation>::Element *undo_stack_pos = nullptr;
  258. Timer *idle_detect;
  259. uint32_t version = 0;
  260. uint32_t saved_version = 0;
  261. void _push_current_op();
  262. void _do_text_op(const TextOperation &p_op, bool p_reverse);
  263. void _clear_redo();
  264. /* Search */
  265. Color search_result_color = Color(1, 1, 1);
  266. Color search_result_border_color = Color(1, 1, 1);
  267. String search_text = "";
  268. uint32_t search_flags = 0;
  269. int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) const;
  270. /* Tooltip. */
  271. ObjectID tooltip_obj_id;
  272. StringName tooltip_func;
  273. Variant tooltip_ud;
  274. /* Mouse */
  275. int _get_char_pos_for_line(int p_px, int p_line, int p_wrap_index = 0) const;
  276. /* Caret. */
  277. struct Caret {
  278. Point2 draw_pos;
  279. bool visible = false;
  280. int last_fit_x = 0;
  281. int line = 0;
  282. int column = 0;
  283. int x_ofs = 0;
  284. int line_ofs = 0;
  285. int wrap_ofs = 0;
  286. } caret;
  287. bool setting_caret_line = false;
  288. bool caret_pos_dirty = false;
  289. Color caret_color = Color(1, 1, 1);
  290. Color caret_background_color = Color(0, 0, 0);
  291. CaretType caret_type = CaretType::CARET_TYPE_LINE;
  292. bool draw_caret = true;
  293. bool caret_blink_enabled = false;
  294. Timer *caret_blink_timer;
  295. bool move_caret_on_right_click = true;
  296. bool caret_mid_grapheme_enabled = false;
  297. void _emit_caret_changed();
  298. void _reset_caret_blink_timer();
  299. void _toggle_draw_caret();
  300. int _get_column_x_offset_for_line(int p_char, int p_line) const;
  301. /* Selection. */
  302. struct Selection {
  303. SelectionMode selecting_mode = SelectionMode::SELECTION_MODE_NONE;
  304. int selecting_line = 0;
  305. int selecting_column = 0;
  306. int selected_word_beg = 0;
  307. int selected_word_end = 0;
  308. int selected_word_origin = 0;
  309. bool selecting_text = false;
  310. bool active = false;
  311. int from_line = 0;
  312. int from_column = 0;
  313. int to_line = 0;
  314. int to_column = 0;
  315. bool shiftclick_left = false;
  316. } selection;
  317. bool selecting_enabled = true;
  318. Color font_selected_color = Color(1, 1, 1);
  319. Color selection_color = Color(1, 1, 1);
  320. bool override_selected_font_color = false;
  321. bool dragging_selection = false;
  322. Timer *click_select_held;
  323. uint64_t last_dblclk = 0;
  324. Vector2 last_dblclk_pos;
  325. void _click_selection_held();
  326. void _update_selection_mode_pointer();
  327. void _update_selection_mode_word();
  328. void _update_selection_mode_line();
  329. void _pre_shift_selection();
  330. void _post_shift_selection();
  331. /* line wrapping. */
  332. LineWrappingMode line_wrapping_mode = LineWrappingMode::LINE_WRAPPING_NONE;
  333. int wrap_at_column = 0;
  334. int wrap_right_offset = 10;
  335. void _update_wrap_at_column(bool p_force = false);
  336. void _update_caret_wrap_offset();
  337. /* Viewport. */
  338. HScrollBar *h_scroll;
  339. VScrollBar *v_scroll;
  340. bool scroll_past_end_of_file_enabled = false;
  341. // Smooth scrolling.
  342. bool smooth_scroll_enabled = false;
  343. float target_v_scroll = 0.0;
  344. float v_scroll_speed = 80.0;
  345. // Scrolling.
  346. bool scrolling = false;
  347. bool updating_scrolls = false;
  348. void _update_scrollbars();
  349. int _get_control_height() const;
  350. void _v_scroll_input();
  351. void _scroll_moved(double p_to_val);
  352. double _get_visible_lines_offset() const;
  353. double _get_v_scroll_offset() const;
  354. void _scroll_up(real_t p_delta);
  355. void _scroll_down(real_t p_delta);
  356. void _scroll_lines_up();
  357. void _scroll_lines_down();
  358. // Minimap
  359. bool draw_minimap = false;
  360. int minimap_width = 80;
  361. Point2 minimap_char_size = Point2(1, 2);
  362. int minimap_line_spacing = 1;
  363. // minimap scroll
  364. bool minimap_clicked = false;
  365. bool hovering_minimap = false;
  366. bool dragging_minimap = false;
  367. bool can_drag_minimap = false;
  368. double minimap_scroll_ratio = 0.0;
  369. double minimap_scroll_click_pos = 0.0;
  370. void _update_minimap_hover();
  371. void _update_minimap_click();
  372. void _update_minimap_drag();
  373. /* Gutters. */
  374. Vector<GutterInfo> gutters;
  375. int gutters_width = 0;
  376. int gutter_padding = 0;
  377. Vector2i hovered_gutter = Vector2i(-1, -1); // X = gutter index, Y = row.
  378. void _update_gutter_width();
  379. /* Syntax highlighting. */
  380. Ref<SyntaxHighlighter> syntax_highlighter;
  381. Map<int, Dictionary> syntax_highlighting_cache;
  382. Dictionary _get_line_syntax_highlighting(int p_line);
  383. /* Visual. */
  384. Ref<StyleBox> style_normal;
  385. Ref<StyleBox> style_focus;
  386. Ref<StyleBox> style_readonly;
  387. Ref<Texture2D> tab_icon;
  388. Ref<Texture2D> space_icon;
  389. Ref<Font> font;
  390. int font_size = 16;
  391. Color font_color = Color(1, 1, 1);
  392. Color font_readonly_color = Color(1, 1, 1);
  393. int outline_size = 0;
  394. Color outline_color = Color(1, 1, 1);
  395. int line_spacing = 1;
  396. Color background_color = Color(1, 1, 1);
  397. Color current_line_color = Color(1, 1, 1);
  398. Color word_highlighted_color = Color(1, 1, 1);
  399. bool window_has_focus = true;
  400. bool first_draw = true;
  401. bool highlight_current_line = false;
  402. bool highlight_all_occurrences = false;
  403. bool draw_control_chars = false;
  404. bool draw_tabs = false;
  405. bool draw_spaces = false;
  406. /*** Super internal Core API. Everything builds on it. ***/
  407. bool text_changed_dirty = false;
  408. void _text_changed_emit();
  409. void _insert_text(int p_line, int p_char, const String &p_text, int *r_end_line = nullptr, int *r_end_char = nullptr);
  410. void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  411. void _base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column);
  412. String _base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const;
  413. void _base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  414. /* Input actions. */
  415. void _swap_current_input_direction();
  416. void _new_line(bool p_split_current = true, bool p_above = false);
  417. void _move_caret_left(bool p_select, bool p_move_by_word = false);
  418. void _move_caret_right(bool p_select, bool p_move_by_word = false);
  419. void _move_caret_up(bool p_select);
  420. void _move_caret_down(bool p_select);
  421. void _move_caret_to_line_start(bool p_select);
  422. void _move_caret_to_line_end(bool p_select);
  423. void _move_caret_page_up(bool p_select);
  424. void _move_caret_page_down(bool p_select);
  425. void _do_backspace(bool p_word = false, bool p_all_to_left = false);
  426. void _delete(bool p_word = false, bool p_all_to_right = false);
  427. void _move_caret_document_start(bool p_select);
  428. void _move_caret_document_end(bool p_select);
  429. protected:
  430. void _notification(int p_what);
  431. static void _bind_methods();
  432. bool _set(const StringName &p_name, const Variant &p_value);
  433. bool _get(const StringName &p_name, Variant &r_ret) const;
  434. void _get_property_list(List<PropertyInfo> *p_list) const;
  435. /* Internal API for CodeEdit, pending public API. */
  436. // brace matching
  437. bool highlight_matching_braces_enabled = false;
  438. Color brace_mismatch_color;
  439. // Line hiding.
  440. Color code_folding_color = Color(1, 1, 1);
  441. Ref<Texture2D> folded_eol_icon;
  442. bool hiding_enabled = false;
  443. void _set_hiding_enabled(bool p_enabled);
  444. bool _is_hiding_enabled() const;
  445. void _set_line_as_hidden(int p_line, bool p_hidden);
  446. bool _is_line_hidden(int p_line) const;
  447. void _unhide_all_lines();
  448. // Symbol lookup.
  449. String lookup_symbol_word;
  450. void _set_symbol_lookup_word(const String &p_symbol);
  451. /* Text manipulation */
  452. // Overridable actions
  453. virtual void _handle_unicode_input_internal(const uint32_t p_unicode);
  454. virtual void _backspace_internal();
  455. virtual void _cut_internal();
  456. virtual void _copy_internal();
  457. virtual void _paste_internal();
  458. GDVIRTUAL1(_handle_unicode_input, int)
  459. GDVIRTUAL0(_backspace)
  460. GDVIRTUAL0(_cut)
  461. GDVIRTUAL0(_copy)
  462. GDVIRTUAL0(_paste)
  463. public:
  464. /* General overrides. */
  465. virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
  466. virtual Size2 get_minimum_size() const override;
  467. virtual bool is_text_field() const override;
  468. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  469. virtual String get_tooltip(const Point2 &p_pos) const override;
  470. void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata);
  471. /* Text */
  472. // Text properties.
  473. bool has_ime_text() const;
  474. void set_editable(const bool p_editable);
  475. bool is_editable() const;
  476. void set_text_direction(TextDirection p_text_direction);
  477. TextDirection get_text_direction() const;
  478. void set_opentype_feature(const String &p_name, int p_value);
  479. int get_opentype_feature(const String &p_name) const;
  480. void clear_opentype_features();
  481. void set_language(const String &p_language);
  482. String get_language() const;
  483. void set_structured_text_bidi_override(Control::StructuredTextParser p_parser);
  484. Control::StructuredTextParser get_structured_text_bidi_override() const;
  485. void set_structured_text_bidi_override_options(Array p_args);
  486. Array get_structured_text_bidi_override_options() const;
  487. void set_tab_size(const int p_size);
  488. int get_tab_size() const;
  489. // User controls
  490. void set_overtype_mode_enabled(const bool p_enabled);
  491. bool is_overtype_mode_enabled() const;
  492. void set_context_menu_enabled(bool p_enable);
  493. bool is_context_menu_enabled() const;
  494. void set_shortcut_keys_enabled(bool p_enabled);
  495. bool is_shortcut_keys_enabled() const;
  496. void set_virtual_keyboard_enabled(bool p_enable);
  497. bool is_virtual_keyboard_enabled() const;
  498. // Text manipulation
  499. void clear();
  500. void set_text(const String &p_text);
  501. String get_text() const;
  502. int get_line_count() const;
  503. void set_line(int p_line, const String &p_new_text);
  504. String get_line(int p_line) const;
  505. int get_line_width(int p_line, int p_wrap_index = -1) const;
  506. int get_line_height() const;
  507. int get_indent_level(int p_line) const;
  508. int get_first_non_whitespace_column(int p_line) const;
  509. void swap_lines(int p_from_line, int p_to_line);
  510. void insert_line_at(int p_at, const String &p_text);
  511. void insert_text_at_caret(const String &p_text);
  512. void remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  513. int get_last_unhidden_line() const;
  514. int get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const;
  515. Point2i get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const;
  516. // Overridable actions
  517. void handle_unicode_input(const uint32_t p_unicode);
  518. void backspace();
  519. void cut();
  520. void copy();
  521. void paste();
  522. // Context menu.
  523. PopupMenu *get_menu() const;
  524. bool is_menu_visible() const;
  525. void menu_option(int p_option);
  526. /* Versioning */
  527. void begin_complex_operation();
  528. void end_complex_operation();
  529. bool has_undo() const;
  530. bool has_redo() const;
  531. void undo();
  532. void redo();
  533. void clear_undo_history();
  534. bool is_insert_text_operation() const;
  535. void tag_saved_version();
  536. uint32_t get_version() const;
  537. uint32_t get_saved_version() const;
  538. /* Search */
  539. void set_search_text(const String &p_search_text);
  540. void set_search_flags(uint32_t p_flags);
  541. Point2i search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const;
  542. /* Mouse */
  543. Point2 get_local_mouse_pos() const;
  544. String get_word_at_pos(const Vector2 &p_pos) const;
  545. Point2i get_line_column_at_pos(const Point2i &p_pos) const;
  546. int get_minimap_line_at_pos(const Point2i &p_pos) const;
  547. bool is_dragging_cursor() const;
  548. /* Caret */
  549. void set_caret_type(CaretType p_type);
  550. CaretType get_caret_type() const;
  551. void set_caret_blink_enabled(const bool p_enabled);
  552. bool is_caret_blink_enabled() const;
  553. void set_caret_blink_speed(const float p_speed);
  554. float get_caret_blink_speed() const;
  555. void set_move_caret_on_right_click_enabled(const bool p_enable);
  556. bool is_move_caret_on_right_click_enabled() const;
  557. void set_caret_mid_grapheme_enabled(const bool p_enabled);
  558. bool is_caret_mid_grapheme_enabled() const;
  559. bool is_caret_visible() const;
  560. Point2 get_caret_draw_pos() const;
  561. void set_caret_line(int p_line, bool p_adjust_viewport = true, bool p_can_be_hidden = true, int p_wrap_index = 0);
  562. int get_caret_line() const;
  563. void set_caret_column(int p_col, bool p_adjust_viewport = true);
  564. int get_caret_column() const;
  565. int get_caret_wrap_index() const;
  566. String get_word_under_caret() const;
  567. /* Selection. */
  568. void set_selecting_enabled(const bool p_enabled);
  569. bool is_selecting_enabled() const;
  570. void set_override_selected_font_color(bool p_override_selected_font_color);
  571. bool is_overriding_selected_font_color() const;
  572. void set_selection_mode(SelectionMode p_mode, int p_line = -1, int p_column = -1);
  573. SelectionMode get_selection_mode() const;
  574. void select_all();
  575. void select_word_under_caret();
  576. void select(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  577. bool has_selection() const;
  578. String get_selected_text() const;
  579. int get_selection_line() const;
  580. int get_selection_column() const;
  581. int get_selection_from_line() const;
  582. int get_selection_from_column() const;
  583. int get_selection_to_line() const;
  584. int get_selection_to_column() const;
  585. void deselect();
  586. void delete_selection();
  587. /* line wrapping. */
  588. void set_line_wrapping_mode(LineWrappingMode p_wrapping_mode);
  589. LineWrappingMode get_line_wrapping_mode() const;
  590. bool is_line_wrapped(int p_line) const;
  591. int get_line_wrap_count(int p_line) const;
  592. int get_line_wrap_index_at_column(int p_line, int p_column) const;
  593. Vector<String> get_line_wrapped_text(int p_line) const;
  594. /* Viewport. */
  595. // Scrolling.
  596. void set_smooth_scroll_enabled(const bool p_enable);
  597. bool is_smooth_scroll_enabled() const;
  598. void set_scroll_past_end_of_file_enabled(const bool p_enabled);
  599. bool is_scroll_past_end_of_file_enabled() const;
  600. void set_v_scroll(double p_scroll);
  601. double get_v_scroll() const;
  602. void set_h_scroll(int p_scroll);
  603. int get_h_scroll() const;
  604. void set_v_scroll_speed(float p_speed);
  605. float get_v_scroll_speed() const;
  606. double get_scroll_pos_for_line(int p_line, int p_wrap_index = 0) const;
  607. // Visible lines.
  608. void set_line_as_first_visible(int p_line, int p_wrap_index = 0);
  609. int get_first_visible_line() const;
  610. void set_line_as_center_visible(int p_line, int p_wrap_index = 0);
  611. void set_line_as_last_visible(int p_line, int p_wrap_index = 0);
  612. int get_last_full_visible_line() const;
  613. int get_last_full_visible_line_wrap_index() const;
  614. int get_visible_line_count() const;
  615. int get_total_visible_line_count() const;
  616. // Auto Adjust
  617. void adjust_viewport_to_caret();
  618. void center_viewport_to_caret();
  619. // Minimap
  620. void set_draw_minimap(bool p_draw);
  621. bool is_drawing_minimap() const;
  622. void set_minimap_width(int p_minimap_width);
  623. int get_minimap_width() const;
  624. int get_minimap_visible_lines() const;
  625. /* Gutters. */
  626. void add_gutter(int p_at = -1);
  627. void remove_gutter(int p_gutter);
  628. int get_gutter_count() const;
  629. void set_gutter_name(int p_gutter, const String &p_name);
  630. String get_gutter_name(int p_gutter) const;
  631. void set_gutter_type(int p_gutter, GutterType p_type);
  632. GutterType get_gutter_type(int p_gutter) const;
  633. void set_gutter_width(int p_gutter, int p_width);
  634. int get_gutter_width(int p_gutter) const;
  635. int get_total_gutter_width() const;
  636. void set_gutter_draw(int p_gutter, bool p_draw);
  637. bool is_gutter_drawn(int p_gutter) const;
  638. void set_gutter_clickable(int p_gutter, bool p_clickable);
  639. bool is_gutter_clickable(int p_gutter) const;
  640. void set_gutter_overwritable(int p_gutter, bool p_overwritable);
  641. bool is_gutter_overwritable(int p_gutter) const;
  642. void merge_gutters(int p_from_line, int p_to_line);
  643. void set_gutter_custom_draw(int p_gutter, Object *p_object, const StringName &p_callback);
  644. // Line gutters.
  645. void set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata);
  646. Variant get_line_gutter_metadata(int p_line, int p_gutter) const;
  647. void set_line_gutter_text(int p_line, int p_gutter, const String &p_text);
  648. String get_line_gutter_text(int p_line, int p_gutter) const;
  649. void set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon);
  650. Ref<Texture2D> get_line_gutter_icon(int p_line, int p_gutter) const;
  651. void set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color);
  652. Color get_line_gutter_item_color(int p_line, int p_gutter) const;
  653. void set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable);
  654. bool is_line_gutter_clickable(int p_line, int p_gutter) const;
  655. // Line style
  656. void set_line_background_color(int p_line, const Color &p_color);
  657. Color get_line_background_color(int p_line) const;
  658. /* Syntax Highlighting. */
  659. void set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter);
  660. Ref<SyntaxHighlighter> get_syntax_highlighter() const;
  661. /* Visual. */
  662. void set_highlight_current_line(bool p_enabled);
  663. bool is_highlight_current_line_enabled() const;
  664. void set_highlight_all_occurrences(const bool p_enabled);
  665. bool is_highlight_all_occurrences_enabled() const;
  666. void set_draw_control_chars(bool p_draw_control_chars);
  667. bool get_draw_control_chars() const;
  668. void set_draw_tabs(bool p_draw);
  669. bool is_drawing_tabs() const;
  670. void set_draw_spaces(bool p_draw);
  671. bool is_drawing_spaces() const;
  672. TextEdit();
  673. };
  674. VARIANT_ENUM_CAST(TextEdit::CaretType);
  675. VARIANT_ENUM_CAST(TextEdit::LineWrappingMode);
  676. VARIANT_ENUM_CAST(TextEdit::SelectionMode);
  677. VARIANT_ENUM_CAST(TextEdit::GutterType);
  678. VARIANT_ENUM_CAST(TextEdit::MenuItems);
  679. VARIANT_ENUM_CAST(TextEdit::SearchFlags);
  680. #endif // TEXT_EDIT_H