text_edit.h 37 KB

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