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