code_editor.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*************************************************************************/
  2. /* code_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. OBJ_TYPE(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. OBJ_TYPE(FindReplaceBar, HBoxContainer);
  54. LineEdit *search_text;
  55. ToolButton *find_prev;
  56. ToolButton *find_next;
  57. CheckBox *case_sensitive;
  58. CheckBox *whole_words;
  59. Label *error_label;
  60. TextureButton *hide_button;
  61. LineEdit *replace_text;
  62. ToolButton *replace;
  63. ToolButton *replace_all;
  64. CheckBox *selection_only;
  65. VBoxContainer *text_vbc;
  66. HBoxContainer *replace_hbc;
  67. HBoxContainer *replace_options_hbc;
  68. TextEdit *text_edit;
  69. int result_line;
  70. int result_col;
  71. bool replace_all_mode;
  72. bool preserve_cursor;
  73. void _get_search_from(int &r_line, int &r_col);
  74. void _show_search();
  75. void _hide_bar();
  76. void _editor_text_changed();
  77. void _search_options_changed(bool p_pressed);
  78. void _search_text_changed(const String &p_text);
  79. void _search_text_entered(const String &p_text);
  80. protected:
  81. void _notification(int p_what);
  82. void _unhandled_input(const InputEvent &p_event);
  83. bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
  84. void _replace();
  85. void _replace_all();
  86. static void _bind_methods();
  87. public:
  88. String get_search_text() const;
  89. String get_replace_text() const;
  90. bool is_case_sensitive() const;
  91. bool is_whole_words() const;
  92. bool is_selection_only() const;
  93. void set_error(const String &p_label);
  94. void set_text_edit(TextEdit *p_text_edit);
  95. void popup_search();
  96. void popup_replace();
  97. bool search_current();
  98. bool search_prev();
  99. bool search_next();
  100. FindReplaceBar();
  101. };
  102. class FindReplaceDialog : public ConfirmationDialog {
  103. OBJ_TYPE(FindReplaceDialog, ConfirmationDialog);
  104. LineEdit *search_text;
  105. LineEdit *replace_text;
  106. CheckButton *whole_words;
  107. CheckButton *case_sensitive;
  108. CheckButton *backwards;
  109. CheckButton *prompt;
  110. CheckButton *selection_only;
  111. Button *skip;
  112. Label *error_label;
  113. MarginContainer *replace_mc;
  114. Label *replace_label;
  115. VBoxContainer *replace_vb;
  116. void _search_text_entered(const String &p_text);
  117. void _replace_text_entered(const String &p_text);
  118. void _prompt_changed();
  119. void _skip_pressed();
  120. TextEdit *text_edit;
  121. protected:
  122. void _search_callback();
  123. void _replace_skip_callback();
  124. bool _search();
  125. void _replace();
  126. virtual void ok_pressed();
  127. static void _bind_methods();
  128. public:
  129. String get_search_text() const;
  130. String get_replace_text() const;
  131. bool is_whole_words() const;
  132. bool is_case_sensitive() const;
  133. bool is_backwards() const;
  134. bool is_replace_mode() const;
  135. bool is_replace_all_mode() const;
  136. bool is_replace_selection_only() const;
  137. void set_replace_selection_only(bool p_enable);
  138. void set_error(const String &p_error);
  139. void popup_search();
  140. void popup_replace();
  141. void set_text_edit(TextEdit *p_text_edit);
  142. void search_next();
  143. FindReplaceDialog();
  144. };
  145. class CodeTextEditor : public VBoxContainer {
  146. OBJ_TYPE(CodeTextEditor, VBoxContainer);
  147. TextEdit *text_editor;
  148. FindReplaceBar *find_replace_bar;
  149. Label *line_nb;
  150. Label *col_nb;
  151. Label *info;
  152. Timer *idle;
  153. Timer *code_complete_timer;
  154. bool enable_complete_timer;
  155. Timer *font_resize_timer;
  156. int font_resize_val;
  157. Label *error;
  158. void _on_settings_change();
  159. void _update_font();
  160. void _complete_request();
  161. void _font_resize_timeout();
  162. void _text_editor_input_event(const InputEvent &p_event);
  163. void _zoom_in();
  164. void _zoom_out();
  165. void _reset_zoom();
  166. protected:
  167. void set_error(const String &p_error);
  168. virtual void _load_theme_settings() {}
  169. virtual void _validate_script() = 0;
  170. virtual void _code_complete_script(const String &p_code, List<String> *r_options){};
  171. void _text_changed_idle_timeout();
  172. void _code_complete_timer_timeout();
  173. void _text_changed();
  174. void _line_col_changed();
  175. void _notification(int);
  176. static void _bind_methods();
  177. public:
  178. void update_editor_settings();
  179. TextEdit *get_text_edit() { return text_editor; }
  180. FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
  181. virtual void apply_code() {}
  182. CodeTextEditor();
  183. };
  184. #endif // CODE_EDITOR_H