code_editor.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* code_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 CODE_EDITOR_H
  31. #define CODE_EDITOR_H
  32. #include "editor/editor_plugin.h"
  33. #include "scene/gui/check_box.h"
  34. #include "scene/gui/check_button.h"
  35. #include "scene/gui/dialogs.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/text_edit.h"
  38. #include "scene/gui/tool_button.h"
  39. #include "scene/main/timer.h"
  40. class GotoLineDialog : public ConfirmationDialog {
  41. GDCLASS(GotoLineDialog, ConfirmationDialog);
  42. Label *line_label;
  43. LineEdit *line;
  44. TextEdit *text_editor;
  45. virtual void ok_pressed();
  46. public:
  47. void popup_find_line(TextEdit *p_edit);
  48. int get_line() const;
  49. void set_text_editor(TextEdit *p_text_editor);
  50. GotoLineDialog();
  51. };
  52. class FindReplaceBar : public HBoxContainer {
  53. GDCLASS(FindReplaceBar, HBoxContainer);
  54. LineEdit *search_text;
  55. ToolButton *find_prev;
  56. ToolButton *find_next;
  57. CheckBox *case_sensitive;
  58. CheckBox *whole_words;
  59. TextureButton *hide_button;
  60. LineEdit *replace_text;
  61. Button *replace;
  62. Button *replace_all;
  63. CheckBox *selection_only;
  64. VBoxContainer *vbc_lineedit;
  65. HBoxContainer *hbc_button_replace;
  66. HBoxContainer *hbc_option_replace;
  67. TextEdit *text_edit;
  68. int result_line;
  69. int result_col;
  70. bool replace_all_mode;
  71. bool preserve_cursor;
  72. void _get_search_from(int &r_line, int &r_col);
  73. void _show_search();
  74. void _hide_bar();
  75. void _editor_text_changed();
  76. void _search_options_changed(bool p_pressed);
  77. void _search_text_changed(const String &p_text);
  78. void _search_text_entered(const String &p_text);
  79. void _replace_text_entered(const String &p_text);
  80. void _update_size();
  81. protected:
  82. void _notification(int p_what);
  83. void _unhandled_input(const Ref<InputEvent> &p_event);
  84. bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
  85. void _replace();
  86. void _replace_all();
  87. static void _bind_methods();
  88. public:
  89. String get_search_text() const;
  90. String get_replace_text() const;
  91. bool is_case_sensitive() const;
  92. bool is_whole_words() const;
  93. bool is_selection_only() const;
  94. void set_error(const String &p_label);
  95. void set_text_edit(TextEdit *p_text_edit);
  96. void popup_search();
  97. void popup_replace();
  98. bool search_current();
  99. bool search_prev();
  100. bool search_next();
  101. FindReplaceBar();
  102. };
  103. typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<String> *r_options, bool &r_forced);
  104. class CodeTextEditor : public VBoxContainer {
  105. GDCLASS(CodeTextEditor, VBoxContainer);
  106. TextEdit *text_editor;
  107. FindReplaceBar *find_replace_bar;
  108. HBoxContainer *status_bar;
  109. ToolButton *warning_button;
  110. Label *warning_count_label;
  111. Label *line_and_col_txt;
  112. Label *info;
  113. Timer *idle;
  114. Timer *code_complete_timer;
  115. Timer *font_resize_timer;
  116. int font_resize_val;
  117. real_t font_size;
  118. Label *error;
  119. int error_line;
  120. int error_column;
  121. void _on_settings_change();
  122. void _update_font();
  123. void _complete_request();
  124. void _font_resize_timeout();
  125. bool _add_font_size(int p_delta);
  126. void _text_editor_gui_input(const Ref<InputEvent> &p_event);
  127. void _zoom_in();
  128. void _zoom_out();
  129. void _zoom_changed();
  130. void _reset_zoom();
  131. CodeTextEditorCodeCompleteFunc code_complete_func;
  132. void *code_complete_ud;
  133. void _warning_label_gui_input(const Ref<InputEvent> &p_event);
  134. void _warning_button_pressed();
  135. void _set_show_warnings_panel(bool p_show);
  136. void _error_pressed(const Ref<InputEvent> &p_event);
  137. protected:
  138. virtual void _load_theme_settings() {}
  139. virtual void _validate_script() {}
  140. virtual void _code_complete_script(const String &p_code, List<String> *r_options) {}
  141. void _text_changed_idle_timeout();
  142. void _code_complete_timer_timeout();
  143. void _text_changed();
  144. void _line_col_changed();
  145. void _notification(int);
  146. static void _bind_methods();
  147. bool is_warnings_panel_opened;
  148. public:
  149. void trim_trailing_whitespace();
  150. void convert_indent_to_spaces();
  151. void convert_indent_to_tabs();
  152. enum CaseStyle {
  153. UPPER,
  154. LOWER,
  155. CAPITALIZE,
  156. };
  157. void convert_case(CaseStyle p_case);
  158. void move_lines_up();
  159. void move_lines_down();
  160. void delete_lines();
  161. void clone_lines_down();
  162. void goto_line(int p_line);
  163. void goto_line_selection(int p_line, int p_begin, int p_end);
  164. Variant get_edit_state();
  165. void set_edit_state(const Variant &p_state);
  166. void set_warning_nb(int p_warning_nb);
  167. void update_editor_settings();
  168. void set_error(const String &p_error);
  169. void set_error_pos(int p_line, int p_column);
  170. void update_line_and_column() { _line_col_changed(); }
  171. TextEdit *get_text_edit() { return text_editor; }
  172. FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
  173. virtual void apply_code() {}
  174. void goto_error();
  175. void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);
  176. CodeTextEditor();
  177. };
  178. #endif // CODE_EDITOR_H