text_edit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*************************************************************************/
  2. /* text_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef TEXT_EDIT_H
  30. #define TEXT_EDIT_H
  31. #include "scene/gui/control.h"
  32. #include "scene/gui/scroll_bar.h"
  33. #include "scene/main/timer.h"
  34. class TextEdit : public Control {
  35. OBJ_TYPE( TextEdit, Control );
  36. struct Cursor {
  37. int last_fit_x;
  38. int line,column; ///< cursor
  39. int x_ofs,line_ofs;
  40. } cursor;
  41. struct Selection {
  42. enum Mode {
  43. MODE_NONE,
  44. MODE_SHIFT,
  45. MODE_POINTER
  46. };
  47. Mode selecting_mode;
  48. int selecting_line,selecting_column;
  49. bool selecting_test;
  50. bool active;
  51. int from_line,from_column;
  52. int to_line,to_column;
  53. } selection;
  54. struct Cache {
  55. Ref<Texture> tab_icon;
  56. Ref<StyleBox> style_normal;
  57. Ref<StyleBox> style_focus;
  58. Ref<Font> font;
  59. Color font_color;
  60. Color font_selected_color;
  61. Color keyword_color;
  62. Color selection_color;
  63. Color mark_color;
  64. Color breakpoint_color;
  65. Color current_line_color;
  66. int row_height;
  67. int line_spacing;
  68. int line_number_w;
  69. Size2 size;
  70. } cache;
  71. struct ColorRegion {
  72. Color color;
  73. String begin_key;
  74. String end_key;
  75. bool line_only;
  76. bool eq;
  77. ColorRegion(const String& p_begin_key="",const String& p_end_key="",const Color &p_color=Color(),bool p_line_only=false) { begin_key=p_begin_key; end_key=p_end_key; color=p_color; line_only=p_line_only || p_end_key==""; eq=begin_key==end_key; }
  78. };
  79. class Text {
  80. public:
  81. struct ColorRegionInfo {
  82. int region;
  83. bool end;
  84. };
  85. struct Line {
  86. int width_cache : 24;
  87. bool marked : 1;
  88. bool breakpoint : 1;
  89. Map<int,ColorRegionInfo> region_info;
  90. String data;
  91. };
  92. private:
  93. const Vector<ColorRegion> *color_regions;
  94. mutable Vector<Line> text;
  95. Ref<Font> font;
  96. int tab_size;
  97. void _update_line_cache(int p_line) const;
  98. public:
  99. void set_tab_size(int p_tab_size);
  100. void set_font(const Ref<Font>& p_font);
  101. void set_color_regions(const Vector<ColorRegion>*p_regions) { color_regions=p_regions; }
  102. int get_line_width(int p_line) const;
  103. int get_max_width() const;
  104. const Map<int,ColorRegionInfo>& get_color_region_info(int p_line);
  105. void set(int p_line,const String& p_string);
  106. void set_marked(int p_line,bool p_marked) { text[p_line].marked=p_marked; }
  107. bool is_marked(int p_line) const { return text[p_line].marked; }
  108. void set_breakpoint(int p_line,bool p_breakpoint) { text[p_line].breakpoint=p_breakpoint; }
  109. bool is_breakpoint(int p_line) const { return text[p_line].breakpoint; }
  110. void insert(int p_at,const String& p_text);
  111. void remove(int p_at);
  112. int size() const { return text.size(); }
  113. void clear();
  114. void clear_caches();
  115. _FORCE_INLINE_ const String& operator[](int p_line) const { return text[p_line].data; }
  116. Text() { tab_size=4; }
  117. };
  118. struct TextOperation {
  119. enum Type {
  120. TYPE_NONE,
  121. TYPE_INSERT,
  122. TYPE_REMOVE
  123. };
  124. Type type;
  125. int from_line,from_column;
  126. int to_line, to_column;
  127. String text;
  128. uint32_t version;
  129. bool chain_forward;
  130. bool chain_backward;
  131. };
  132. TextOperation current_op;
  133. List<TextOperation> undo_stack;
  134. List<TextOperation>::Element *undo_stack_pos;
  135. void _clear_redo();
  136. void _do_text_op(const TextOperation& p_op, bool p_reverse);
  137. //syntax coloring
  138. Color symbol_color;
  139. HashMap<String,Color> keywords;
  140. Color custom_bg_color;
  141. Vector<ColorRegion> color_regions;
  142. Set<String> completion_prefixes;
  143. bool completion_enabled;
  144. Vector<String> completion_strings;
  145. Vector<String> completion_options;
  146. bool completion_active;
  147. String completion_current;
  148. String completion_base;
  149. int completion_index;
  150. Rect2i completion_rect;
  151. int completion_line_ofs;
  152. bool setting_text;
  153. // data
  154. Text text;
  155. uint32_t version;
  156. uint32_t saved_version;
  157. int max_chars;
  158. bool readonly;
  159. bool syntax_coloring;
  160. int tab_size;
  161. bool setting_row;
  162. bool wrap;
  163. bool draw_tabs;
  164. bool cursor_changed_dirty;
  165. bool text_changed_dirty;
  166. bool undo_enabled;
  167. bool line_numbers;
  168. bool auto_brace_completion_enabled;
  169. bool cut_copy_line;
  170. uint64_t last_dblclk;
  171. Timer *idle_detect;
  172. HScrollBar *h_scroll;
  173. VScrollBar *v_scroll;
  174. bool updating_scrolls;
  175. Object *tooltip_obj;
  176. StringName tooltip_func;
  177. Variant tooltip_ud;
  178. bool next_operation_is_complex;
  179. int get_visible_rows() const;
  180. int get_char_count();
  181. int get_char_pos_for(int p_px,String p_pos) const;
  182. int get_column_x_offset(int p_column,String p_pos);
  183. void adjust_viewport_to_cursor();
  184. void _scroll_moved(double);
  185. void _update_scrollbars();
  186. void _pre_shift_selection();
  187. void _post_shift_selection();
  188. // void mouse_motion(const Point& p_pos, const Point& p_rel, int p_button_mask);
  189. Size2 get_minimum_size();
  190. int get_row_height() const;
  191. void _update_caches();
  192. void _cursor_changed_emit();
  193. void _text_changed_emit();
  194. void _begin_compex_operation();
  195. void _end_compex_operation();
  196. void _push_current_op();
  197. /* super internal api, undo/redo builds on it */
  198. void _base_insert_text(int p_line, int p_column,const String& p_text,int &r_end_line,int &r_end_column);
  199. String _base_get_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) const;
  200. void _base_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column);
  201. DVector<int> _search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const;
  202. void _clear();
  203. void _cancel_completion();
  204. void _confirm_completion();
  205. void _update_completion_candidates();
  206. bool _get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const;
  207. protected:
  208. virtual String get_tooltip(const Point2& p_pos) const;
  209. void _insert_text(int p_line, int p_column,const String& p_text,int *r_end_line=NULL,int *r_end_char=NULL);
  210. void _remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column);
  211. void _insert_text_at_cursor(const String& p_text);
  212. void _input_event(const InputEvent& p_input);
  213. void _notification(int p_what);
  214. void _consume_pair_symbol(CharType ch);
  215. void _consume_backspace_for_pair_symbol(int prev_line, int prev_column);
  216. static void _bind_methods();
  217. public:
  218. enum SearchFlags {
  219. SEARCH_MATCH_CASE=1,
  220. SEARCH_WHOLE_WORDS=2,
  221. SEARCH_BACKWARDS=4
  222. };
  223. virtual CursorShape get_cursor_shape(const Point2& p_pos=Point2i()) const;
  224. //void delete_char();
  225. //void delete_line();
  226. void set_text(String p_text);
  227. void insert_text_at_cursor(const String& p_text);
  228. int get_line_count() const;
  229. void set_line_as_marked(int p_line,bool p_marked);
  230. void set_line_as_breakpoint(int p_line,bool p_breakpoint);
  231. bool is_line_set_as_breakpoint(int p_line) const;
  232. void get_breakpoints(List<int> *p_breakpoints) const;
  233. String get_text();
  234. String get_line(int line) const;
  235. void backspace_at_cursor();
  236. inline void set_auto_brace_completion(bool p_enabled) {
  237. auto_brace_completion_enabled = p_enabled;
  238. }
  239. void cursor_set_column(int p_col);
  240. void cursor_set_line(int p_row);
  241. int cursor_get_column() const;
  242. int cursor_get_line() const;
  243. void set_readonly(bool p_readonly);
  244. void set_max_chars(int p_max_chars);
  245. void set_wrap(bool p_wrap);
  246. void clear();
  247. void set_syntax_coloring(bool p_enabled);
  248. bool is_syntax_coloring_enabled() const;
  249. void cut();
  250. void copy();
  251. void paste();
  252. void select_all();
  253. void select(int p_from_line,int p_from_column,int p_to_line,int p_to_column);
  254. void deselect();
  255. bool is_selection_active() const;
  256. int get_selection_from_line() const;
  257. int get_selection_from_column() const;
  258. int get_selection_to_line() const;
  259. int get_selection_to_column() const;
  260. String get_selection_text() const;
  261. String get_word_under_cursor() const;
  262. 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;
  263. void undo();
  264. void redo();
  265. void clear_undo_history();
  266. void set_draw_tabs(bool p_draw);
  267. bool is_drawing_tabs() const;
  268. void add_keyword_color(const String& p_keyword,const Color& p_color);
  269. 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);
  270. void set_symbol_color(const Color& p_color);
  271. void set_custom_bg_color(const Color& p_color);
  272. void clear_colors();
  273. int get_v_scroll() const;
  274. void set_v_scroll(int p_scroll);
  275. int get_h_scroll() const;
  276. void set_h_scroll(int p_scroll);
  277. uint32_t get_version() const;
  278. uint32_t get_saved_version() const;
  279. void tag_saved_version();
  280. void set_show_line_numbers(bool p_show);
  281. void set_tooltip_request_func(Object *p_obj, const StringName& p_function, const Variant& p_udata);
  282. void set_completion(bool p_enabled,const Vector<String>& p_prefixes);
  283. void code_complete(const Vector<String> &p_strings);
  284. void query_code_comple();
  285. TextEdit();
  286. ~TextEdit();
  287. };
  288. #endif // TEXT_EDIT_H