code_editor.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. MarginContainer *container;
  55. LineEdit *search_text;
  56. ToolButton *find_prev;
  57. ToolButton *find_next;
  58. CheckBox *case_sensitive;
  59. CheckBox *whole_words;
  60. TextureButton *hide_button;
  61. LineEdit *replace_text;
  62. Button *replace;
  63. Button *replace_all;
  64. CheckBox *selection_only;
  65. HBoxContainer *hbc;
  66. VBoxContainer *vbc_lineedit;
  67. HBoxContainer *hbc_button_replace;
  68. HBoxContainer *hbc_option_replace;
  69. TextEdit *text_edit;
  70. int result_line;
  71. int result_col;
  72. bool replace_all_mode;
  73. bool preserve_cursor;
  74. void _get_search_from(int &r_line, int &r_col);
  75. void _show_search();
  76. void _hide_bar();
  77. void _editor_text_changed();
  78. void _search_options_changed(bool p_pressed);
  79. void _search_text_changed(const String &p_text);
  80. void _search_text_entered(const String &p_text);
  81. void _replace_text_entered(const String &p_text);
  82. void _update_size();
  83. protected:
  84. void _notification(int p_what);
  85. void _unhandled_input(const Ref<InputEvent> &p_event);
  86. bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
  87. void _replace();
  88. void _replace_all();
  89. static void _bind_methods();
  90. public:
  91. String get_search_text() const;
  92. String get_replace_text() const;
  93. bool is_case_sensitive() const;
  94. bool is_whole_words() const;
  95. bool is_selection_only() const;
  96. void set_error(const String &p_label);
  97. void set_text_edit(TextEdit *p_text_edit);
  98. void popup_search();
  99. void popup_replace();
  100. bool search_current();
  101. bool search_prev();
  102. bool search_next();
  103. FindReplaceBar();
  104. };
  105. typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<String> *r_options, bool &r_forced);
  106. class CodeTextEditor : public VBoxContainer {
  107. GDCLASS(CodeTextEditor, VBoxContainer);
  108. TextEdit *text_editor;
  109. FindReplaceBar *find_replace_bar;
  110. HBoxContainer *status_bar;
  111. Label *warning_label;
  112. Label *warning_count_label;
  113. Label *line_nb;
  114. Label *col_nb;
  115. Label *font_size_nb;
  116. Label *info;
  117. Timer *idle;
  118. Timer *code_complete_timer;
  119. Timer *font_resize_timer;
  120. int font_resize_val;
  121. real_t font_size;
  122. Label *error;
  123. int error_line;
  124. int error_column;
  125. void _on_settings_change();
  126. void _update_font();
  127. void _complete_request();
  128. void _font_resize_timeout();
  129. bool _add_font_size(int p_delta);
  130. void _text_editor_gui_input(const Ref<InputEvent> &p_event);
  131. void _zoom_in();
  132. void _zoom_out();
  133. void _zoom_changed();
  134. void _reset_zoom();
  135. CodeTextEditorCodeCompleteFunc code_complete_func;
  136. void *code_complete_ud;
  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. public:
  148. void trim_trailing_whitespace();
  149. void convert_indent_to_spaces();
  150. void convert_indent_to_tabs();
  151. enum CaseStyle {
  152. UPPER,
  153. LOWER,
  154. CAPITALIZE,
  155. };
  156. void convert_case(CaseStyle p_case);
  157. void move_lines_up();
  158. void move_lines_down();
  159. void delete_lines();
  160. void clone_lines_down();
  161. void goto_line(int p_line);
  162. void goto_line_selection(int p_line, int p_begin, int p_end);
  163. Variant get_edit_state();
  164. void set_edit_state(const Variant &p_state);
  165. void update_editor_settings();
  166. void set_error(const String &p_error);
  167. void set_error_pos(int p_line, int p_column);
  168. void update_line_and_column() { _line_col_changed(); }
  169. TextEdit *get_text_edit() { return text_editor; }
  170. FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
  171. Label *get_error_label() const { return error; }
  172. Label *get_warning_label() const { return warning_label; }
  173. Label *get_warning_count_label() const { return warning_count_label; }
  174. virtual void apply_code() {}
  175. void goto_error();
  176. void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);
  177. CodeTextEditor();
  178. };
  179. #endif // CODE_EDITOR_H