text_edit.h 16 KB

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