2
0

text_edit.h 35 KB

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