text_edit.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*************************************************************************/
  2. /* text_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. class TextEdit : public Control {
  37. OBJ_TYPE(TextEdit, Control);
  38. struct Cursor {
  39. int last_fit_x;
  40. int line, column; ///< cursor
  41. int x_ofs, line_ofs;
  42. } cursor;
  43. struct Selection {
  44. enum Mode {
  45. MODE_NONE,
  46. MODE_SHIFT,
  47. MODE_POINTER
  48. };
  49. Mode selecting_mode;
  50. int selecting_line, selecting_column;
  51. bool selecting_text;
  52. bool active;
  53. int from_line, from_column;
  54. int to_line, to_column;
  55. bool shiftclick_left;
  56. } selection;
  57. struct Cache {
  58. Ref<Texture> tab_icon;
  59. Ref<StyleBox> style_normal;
  60. Ref<StyleBox> style_focus;
  61. Ref<Font> font;
  62. Color completion_background_color;
  63. Color completion_selected_color;
  64. Color completion_existing_color;
  65. Color completion_font_color;
  66. Color caret_color;
  67. Color caret_background_color;
  68. Color line_number_color;
  69. Color font_color;
  70. Color font_selected_color;
  71. Color keyword_color;
  72. Color number_color;
  73. Color function_color;
  74. Color member_variable_color;
  75. Color selection_color;
  76. Color mark_color;
  77. Color breakpoint_color;
  78. Color current_line_color;
  79. Color line_length_guideline_color;
  80. Color brace_mismatch_color;
  81. Color word_highlighted_color;
  82. Color search_result_color;
  83. Color search_result_border_color;
  84. int row_height;
  85. int line_spacing;
  86. int line_number_w;
  87. int breakpoint_gutter_width;
  88. Size2 size;
  89. } cache;
  90. struct ColorRegion {
  91. Color color;
  92. String begin_key;
  93. String end_key;
  94. bool line_only;
  95. bool eq;
  96. ColorRegion(const String &p_begin_key = "", const String &p_end_key = "", const Color &p_color = Color(), bool p_line_only = false) {
  97. begin_key = p_begin_key;
  98. end_key = p_end_key;
  99. color = p_color;
  100. line_only = p_line_only || p_end_key == "";
  101. eq = begin_key == end_key;
  102. }
  103. };
  104. class Text {
  105. public:
  106. struct ColorRegionInfo {
  107. int region;
  108. bool end;
  109. };
  110. struct Line {
  111. int width_cache : 24;
  112. bool marked : 1;
  113. bool breakpoint : 1;
  114. Map<int, ColorRegionInfo> region_info;
  115. String data;
  116. };
  117. private:
  118. const Vector<ColorRegion> *color_regions;
  119. mutable Vector<Line> text;
  120. Ref<Font> font;
  121. int tab_size;
  122. void _update_line_cache(int p_line) const;
  123. public:
  124. void set_tab_size(int p_tab_size);
  125. void set_font(const Ref<Font> &p_font);
  126. void set_color_regions(const Vector<ColorRegion> *p_regions) { color_regions = p_regions; }
  127. int get_line_width(int p_line) const;
  128. int get_max_width() const;
  129. const Map<int, ColorRegionInfo> &get_color_region_info(int p_line);
  130. void set(int p_line, const String &p_string);
  131. void set_marked(int p_line, bool p_marked) { text[p_line].marked = p_marked; }
  132. bool is_marked(int p_line) const { return text[p_line].marked; }
  133. void set_breakpoint(int p_line, bool p_breakpoint) { text[p_line].breakpoint = p_breakpoint; }
  134. bool is_breakpoint(int p_line) const { return text[p_line].breakpoint; }
  135. void insert(int p_at, const String &p_text);
  136. void remove(int p_at);
  137. int size() const { return text.size(); }
  138. void clear();
  139. void clear_caches();
  140. _FORCE_INLINE_ const String &operator[](int p_line) const { return text[p_line].data; }
  141. Text() { tab_size = 4; }
  142. };
  143. struct TextOperation {
  144. enum Type {
  145. TYPE_NONE,
  146. TYPE_INSERT,
  147. TYPE_REMOVE,
  148. TYPE_CLEAR
  149. };
  150. Type type;
  151. int from_line, from_column;
  152. int to_line, to_column;
  153. String text;
  154. uint32_t prev_version;
  155. uint32_t version;
  156. bool chain_forward;
  157. bool chain_backward;
  158. };
  159. TextOperation current_op;
  160. List<TextOperation> undo_stack;
  161. List<TextOperation>::Element *undo_stack_pos;
  162. void _clear_redo();
  163. void _do_text_op(const TextOperation &p_op, bool p_reverse);
  164. //syntax coloring
  165. Color symbol_color;
  166. HashMap<String, Color> keywords;
  167. Color custom_bg_color;
  168. Vector<ColorRegion> color_regions;
  169. Set<String> completion_prefixes;
  170. bool completion_enabled;
  171. Vector<String> completion_strings;
  172. Vector<String> completion_options;
  173. bool completion_active;
  174. String completion_current;
  175. String completion_base;
  176. int completion_index;
  177. Rect2i completion_rect;
  178. int completion_line_ofs;
  179. String completion_hint;
  180. int completion_hint_offset;
  181. bool setting_text;
  182. // data
  183. Text text;
  184. uint32_t version;
  185. uint32_t saved_version;
  186. int max_chars;
  187. bool readonly;
  188. bool syntax_coloring;
  189. int tab_size;
  190. Timer *caret_blink_timer;
  191. bool caret_blink_enabled;
  192. bool draw_caret;
  193. bool window_has_focus;
  194. bool block_caret;
  195. bool setting_row;
  196. bool wrap;
  197. bool draw_tabs;
  198. bool cursor_changed_dirty;
  199. bool text_changed_dirty;
  200. bool undo_enabled;
  201. bool line_numbers;
  202. bool line_numbers_zero_padded;
  203. bool line_length_guideline;
  204. int line_length_guideline_col;
  205. bool draw_breakpoint_gutter;
  206. int breakpoint_gutter_width;
  207. bool highlight_all_occurrences;
  208. bool scroll_past_end_of_file_enabled;
  209. bool auto_brace_completion_enabled;
  210. bool brace_matching_enabled;
  211. bool auto_indent;
  212. bool cut_copy_line;
  213. bool insert_mode;
  214. bool raised_from_completion;
  215. uint64_t last_dblclk;
  216. Timer *idle_detect;
  217. Timer *click_select_held;
  218. HScrollBar *h_scroll;
  219. VScrollBar *v_scroll;
  220. bool updating_scrolls;
  221. Object *tooltip_obj;
  222. StringName tooltip_func;
  223. Variant tooltip_ud;
  224. bool next_operation_is_complex;
  225. bool callhint_below;
  226. Vector2 callhint_offset;
  227. String search_text;
  228. uint32_t search_flags;
  229. int search_result_line;
  230. int search_result_col;
  231. int get_visible_rows() const;
  232. int get_char_count();
  233. int get_char_pos_for(int p_px, String p_pos) const;
  234. int get_column_x_offset(int p_column, String p_pos);
  235. void adjust_viewport_to_cursor();
  236. void _scroll_moved(double);
  237. void _update_scrollbars();
  238. void _click_selection_held();
  239. void _pre_shift_selection();
  240. void _post_shift_selection();
  241. void _scroll_lines_up();
  242. void _scroll_lines_down();
  243. // void mouse_motion(const Point& p_pos, const Point& p_rel, int p_button_mask);
  244. Size2 get_minimum_size() const;
  245. int get_row_height() const;
  246. void _reset_caret_blink_timer();
  247. void _toggle_draw_caret();
  248. void _update_caches();
  249. void _cursor_changed_emit();
  250. void _text_changed_emit();
  251. void _push_current_op();
  252. /* super internal api, undo/redo builds on it */
  253. void _base_insert_text(int p_line, int p_column, const String &p_text, int &r_end_line, int &r_end_column);
  254. String _base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const;
  255. void _base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  256. int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column);
  257. DVector<int> _search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const;
  258. PopupMenu *menu;
  259. void _clear();
  260. void _cancel_completion();
  261. void _cancel_code_hint();
  262. void _confirm_completion();
  263. void _update_completion_candidates();
  264. void _get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const;
  265. protected:
  266. virtual String get_tooltip(const Point2 &p_pos) const;
  267. void _insert_text(int p_line, int p_column, const String &p_text, int *r_end_line = NULL, int *r_end_char = NULL);
  268. void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  269. void _insert_text_at_cursor(const String &p_text);
  270. void _input_event(const InputEvent &p_input);
  271. void _notification(int p_what);
  272. void _consume_pair_symbol(CharType ch);
  273. void _consume_backspace_for_pair_symbol(int prev_line, int prev_column);
  274. static void _bind_methods();
  275. public:
  276. enum MenuItems {
  277. MENU_CUT,
  278. MENU_COPY,
  279. MENU_PASTE,
  280. MENU_CLEAR,
  281. MENU_UPPERCASE,
  282. MENU_LOWERCASE,
  283. MENU_SELECT_ALL,
  284. MENU_UNDO,
  285. MENU_MAX
  286. };
  287. enum Case {
  288. UPPERCASE,
  289. LOWERCASE
  290. };
  291. enum SearchFlags {
  292. SEARCH_MATCH_CASE = 1,
  293. SEARCH_WHOLE_WORDS = 2,
  294. SEARCH_BACKWARDS = 4
  295. };
  296. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const;
  297. //void delete_char();
  298. //void delete_line();
  299. void begin_complex_operation();
  300. void end_complex_operation();
  301. void set_text(String p_text);
  302. void insert_text_at_cursor(const String &p_text);
  303. void insert_at(const String &p_text, int at);
  304. int get_line_count() const;
  305. void set_line_as_marked(int p_line, bool p_marked);
  306. void set_line_as_breakpoint(int p_line, bool p_breakpoint);
  307. bool is_line_set_as_breakpoint(int p_line) const;
  308. void get_breakpoints(List<int> *p_breakpoints) const;
  309. String get_text();
  310. String get_line(int line) const;
  311. void set_line(int line, String new_text);
  312. void backspace_at_cursor();
  313. void indent_selection_left();
  314. void indent_selection_right();
  315. inline void set_scroll_pass_end_of_file(bool p_enabled) {
  316. scroll_past_end_of_file_enabled = p_enabled;
  317. update();
  318. }
  319. inline void set_auto_brace_completion(bool p_enabled) {
  320. auto_brace_completion_enabled = p_enabled;
  321. }
  322. inline void set_brace_matching(bool p_enabled) {
  323. brace_matching_enabled = p_enabled;
  324. update();
  325. }
  326. inline void set_callhint_settings(bool below, Vector2 offset) {
  327. callhint_below = below;
  328. callhint_offset = offset;
  329. }
  330. void set_auto_indent(bool p_auto_indent);
  331. void center_viewport_to_cursor();
  332. void cursor_set_column(int p_col, bool p_adjust_viewport = true);
  333. void cursor_set_line(int p_row, bool p_adjust_viewport = true);
  334. int cursor_get_column() const;
  335. int cursor_get_line() const;
  336. bool cursor_get_blink_enabled() const;
  337. void cursor_set_blink_enabled(const bool p_enabled);
  338. float cursor_get_blink_speed() const;
  339. void cursor_set_blink_speed(const float p_speed);
  340. void cursor_set_block_mode(const bool p_enable);
  341. bool cursor_is_block_mode() const;
  342. void set_readonly(bool p_readonly);
  343. void set_max_chars(int p_max_chars);
  344. void set_wrap(bool p_wrap);
  345. void clear();
  346. void set_syntax_coloring(bool p_enabled);
  347. bool is_syntax_coloring_enabled() const;
  348. void cut();
  349. void convert_case(int p_case);
  350. void copy();
  351. void paste();
  352. void select_all();
  353. void select(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
  354. void deselect();
  355. void set_search_text(const String &p_search_text);
  356. void set_search_flags(uint32_t p_flags);
  357. void set_current_search_result(int line, int col);
  358. void set_highlight_all_occurrences(const bool p_enabled);
  359. bool is_highlight_all_occurrences_enabled() const;
  360. bool is_selection_active() const;
  361. int get_selection_from_line() const;
  362. int get_selection_from_column() const;
  363. int get_selection_to_line() const;
  364. int get_selection_to_column() const;
  365. String get_selection_text() const;
  366. String get_word_under_cursor() const;
  367. bool search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column, int &r_line, int &r_column) const;
  368. void undo();
  369. void redo();
  370. void clear_undo_history();
  371. void set_tab_size(const int p_size);
  372. void set_draw_tabs(bool p_draw);
  373. bool is_drawing_tabs() const;
  374. void set_insert_mode(bool p_enabled);
  375. bool is_insert_mode() const;
  376. void add_keyword_color(const String &p_keyword, const Color &p_color);
  377. void add_color_region(const String &p_begin_key = String(), const String &p_end_key = String(), const Color &p_color = Color(), bool p_line_only = false);
  378. void set_symbol_color(const Color &p_color);
  379. void set_custom_bg_color(const Color &p_color);
  380. void clear_colors();
  381. int get_v_scroll() const;
  382. void set_v_scroll(int p_scroll);
  383. int get_h_scroll() const;
  384. void set_h_scroll(int p_scroll);
  385. uint32_t get_version() const;
  386. uint32_t get_saved_version() const;
  387. void tag_saved_version();
  388. void menu_option(int p_option);
  389. void set_show_line_numbers(bool p_show);
  390. bool is_show_line_numbers_enabled() const;
  391. void set_line_numbers_zero_padded(bool p_zero_padded);
  392. void set_show_line_length_guideline(bool p_show);
  393. void set_line_length_guideline_column(int p_column);
  394. void set_draw_breakpoint_gutter(bool p_draw);
  395. bool is_drawing_breakpoint_gutter() const;
  396. void set_breakpoint_gutter_width(int p_gutter_width);
  397. int get_breakpoint_gutter_width() const;
  398. void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata);
  399. void set_completion(bool p_enabled, const Vector<String> &p_prefixes);
  400. void code_complete(const Vector<String> &p_strings);
  401. void set_code_hint(const String &p_hint);
  402. void query_code_comple();
  403. PopupMenu *get_menu() const;
  404. String get_text_for_completion();
  405. virtual bool is_text_field() const;
  406. TextEdit();
  407. ~TextEdit();
  408. };
  409. #endif // TEXT_EDIT_H