line_edit.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*************************************************************************/
  2. /* line_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 LINE_EDIT_H
  31. #define LINE_EDIT_H
  32. #include "scene/gui/control.h"
  33. #include "scene/gui/popup_menu.h"
  34. class LineEdit : public Control {
  35. GDCLASS(LineEdit, Control);
  36. public:
  37. enum Align {
  38. ALIGN_LEFT,
  39. ALIGN_CENTER,
  40. ALIGN_RIGHT,
  41. ALIGN_FILL
  42. };
  43. enum MenuItems {
  44. MENU_CUT,
  45. MENU_COPY,
  46. MENU_PASTE,
  47. MENU_CLEAR,
  48. MENU_SELECT_ALL,
  49. MENU_UNDO,
  50. MENU_REDO,
  51. MENU_DIR_INHERITED,
  52. MENU_DIR_AUTO,
  53. MENU_DIR_LTR,
  54. MENU_DIR_RTL,
  55. MENU_DISPLAY_UCC,
  56. MENU_INSERT_LRM,
  57. MENU_INSERT_RLM,
  58. MENU_INSERT_LRE,
  59. MENU_INSERT_RLE,
  60. MENU_INSERT_LRO,
  61. MENU_INSERT_RLO,
  62. MENU_INSERT_PDF,
  63. MENU_INSERT_ALM,
  64. MENU_INSERT_LRI,
  65. MENU_INSERT_RLI,
  66. MENU_INSERT_FSI,
  67. MENU_INSERT_PDI,
  68. MENU_INSERT_ZWJ,
  69. MENU_INSERT_ZWNJ,
  70. MENU_INSERT_WJ,
  71. MENU_INSERT_SHY,
  72. MENU_MAX
  73. };
  74. private:
  75. Align align = ALIGN_LEFT;
  76. bool editable = false;
  77. bool pass = false;
  78. bool text_changed_dirty = false;
  79. String undo_text;
  80. String text;
  81. String placeholder;
  82. String placeholder_translated;
  83. String secret_character = "*";
  84. float placeholder_alpha = 0.6;
  85. String ime_text;
  86. Point2 ime_selection;
  87. RID text_rid;
  88. float full_width = 0.0;
  89. bool selecting_enabled = true;
  90. bool deselect_on_focus_loss_enabled = true;
  91. bool context_menu_enabled = true;
  92. PopupMenu *menu = nullptr;
  93. PopupMenu *menu_dir = nullptr;
  94. PopupMenu *menu_ctl = nullptr;
  95. bool caret_mid_grapheme_enabled = false;
  96. int caret_column = 0;
  97. int scroll_offset = 0;
  98. int max_length = 0; // 0 for no maximum.
  99. Dictionary opentype_features;
  100. String language;
  101. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  102. TextDirection input_direction = TEXT_DIRECTION_LTR;
  103. Control::StructuredTextParser st_parser = STRUCTURED_TEXT_DEFAULT;
  104. Array st_args;
  105. bool draw_control_chars = false;
  106. bool expand_to_text_length = false;
  107. bool window_has_focus = true;
  108. bool clear_button_enabled = false;
  109. bool shortcut_keys_enabled = true;
  110. bool virtual_keyboard_enabled = true;
  111. bool middle_mouse_paste_enabled = true;
  112. Ref<Texture2D> right_icon;
  113. struct Selection {
  114. int begin = 0;
  115. int end = 0;
  116. int start_column = 0;
  117. bool enabled = false;
  118. bool creating = false;
  119. bool double_click = false;
  120. bool drag_attempt = false;
  121. } selection;
  122. struct TextOperation {
  123. int caret_column = 0;
  124. int scroll_offset = 0;
  125. int cached_width = 0;
  126. String text;
  127. };
  128. List<TextOperation> undo_stack;
  129. List<TextOperation>::Element *undo_stack_pos = nullptr;
  130. struct ClearButtonStatus {
  131. bool press_attempt = false;
  132. bool pressing_inside = false;
  133. } clear_button_status;
  134. uint64_t last_dblclk = 0;
  135. Vector2 last_dblclk_pos;
  136. bool caret_blink_enabled = false;
  137. bool caret_force_displayed = false;
  138. bool draw_caret = true;
  139. Timer *caret_blink_timer = nullptr;
  140. bool _is_over_clear_button(const Point2 &p_pos) const;
  141. void _clear_undo_stack();
  142. void _clear_redo();
  143. void _create_undo_state();
  144. int _get_menu_action_accelerator(const String &p_action);
  145. void _shape();
  146. void _fit_to_width();
  147. void _text_changed();
  148. void _emit_text_change();
  149. void shift_selection_check_pre(bool);
  150. void shift_selection_check_post(bool);
  151. void selection_fill_at_caret();
  152. void set_scroll_offset(int p_pos);
  153. int get_scroll_offset() const;
  154. void set_caret_at_pixel_pos(int p_x);
  155. Vector2i get_caret_pixel_pos();
  156. void _reset_caret_blink_timer();
  157. void _toggle_draw_caret();
  158. void clear_internal();
  159. void _editor_settings_changed();
  160. void _swap_current_input_direction();
  161. void _move_caret_left(bool p_select, bool p_move_by_word = false);
  162. void _move_caret_right(bool p_select, bool p_move_by_word = false);
  163. void _move_caret_start(bool p_select);
  164. void _move_caret_end(bool p_select);
  165. void _backspace(bool p_word = false, bool p_all_to_left = false);
  166. void _delete(bool p_word = false, bool p_all_to_right = false);
  167. void _ensure_menu();
  168. protected:
  169. void _notification(int p_what);
  170. static void _bind_methods();
  171. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  172. bool _set(const StringName &p_name, const Variant &p_value);
  173. bool _get(const StringName &p_name, Variant &r_ret) const;
  174. void _get_property_list(List<PropertyInfo> *p_list) const;
  175. void _validate_property(PropertyInfo &property) const override;
  176. public:
  177. void set_align(Align p_align);
  178. Align get_align() const;
  179. virtual Variant get_drag_data(const Point2 &p_point) override;
  180. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  181. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
  182. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  183. void menu_option(int p_option);
  184. void set_context_menu_enabled(bool p_enable);
  185. bool is_context_menu_enabled();
  186. PopupMenu *get_menu() const;
  187. bool is_menu_visible() const;
  188. void select(int p_from = 0, int p_to = -1);
  189. void select_all();
  190. void selection_delete();
  191. void deselect();
  192. bool has_selection() const;
  193. int get_selection_from_column() const;
  194. int get_selection_to_column() const;
  195. void delete_char();
  196. void delete_text(int p_from_column, int p_to_column);
  197. void set_text(String p_text);
  198. String get_text() const;
  199. void set_text_direction(TextDirection p_text_direction);
  200. TextDirection get_text_direction() const;
  201. void set_opentype_feature(const String &p_name, int p_value);
  202. int get_opentype_feature(const String &p_name) const;
  203. void clear_opentype_features();
  204. void set_language(const String &p_language);
  205. String get_language() const;
  206. void set_draw_control_chars(bool p_draw_control_chars);
  207. bool get_draw_control_chars() const;
  208. void set_structured_text_bidi_override(Control::StructuredTextParser p_parser);
  209. Control::StructuredTextParser get_structured_text_bidi_override() const;
  210. void set_structured_text_bidi_override_options(Array p_args);
  211. Array get_structured_text_bidi_override_options() const;
  212. void set_placeholder(String p_text);
  213. String get_placeholder() const;
  214. void set_placeholder_alpha(float p_alpha);
  215. float get_placeholder_alpha() const;
  216. void set_caret_column(int p_column);
  217. int get_caret_column() const;
  218. void set_max_length(int p_max_length);
  219. int get_max_length() const;
  220. void insert_text_at_caret(String p_text);
  221. void clear();
  222. void set_caret_mid_grapheme_enabled(const bool p_enabled);
  223. bool is_caret_mid_grapheme_enabled() const;
  224. bool is_caret_blink_enabled() const;
  225. void set_caret_blink_enabled(const bool p_enabled);
  226. float get_caret_blink_speed() const;
  227. void set_caret_blink_speed(const float p_speed);
  228. void set_caret_force_displayed(const bool p_enabled);
  229. bool is_caret_force_displayed() const;
  230. void copy_text();
  231. void cut_text();
  232. void paste_text();
  233. bool has_undo() const;
  234. bool has_redo() const;
  235. void undo();
  236. void redo();
  237. void set_editable(bool p_editable);
  238. bool is_editable() const;
  239. void set_secret(bool p_secret);
  240. bool is_secret() const;
  241. void set_secret_character(const String &p_string);
  242. String get_secret_character() const;
  243. virtual Size2 get_minimum_size() const override;
  244. void set_expand_to_text_length_enabled(bool p_enabled);
  245. bool is_expand_to_text_length_enabled() const;
  246. void set_clear_button_enabled(bool p_enabled);
  247. bool is_clear_button_enabled() const;
  248. void set_shortcut_keys_enabled(bool p_enabled);
  249. bool is_shortcut_keys_enabled() const;
  250. void set_virtual_keyboard_enabled(bool p_enable);
  251. bool is_virtual_keyboard_enabled() const;
  252. void set_middle_mouse_paste_enabled(bool p_enabled);
  253. bool is_middle_mouse_paste_enabled() const;
  254. void set_selecting_enabled(bool p_enabled);
  255. bool is_selecting_enabled() const;
  256. void set_deselect_on_focus_loss_enabled(const bool p_enabled);
  257. bool is_deselect_on_focus_loss_enabled() const;
  258. void set_right_icon(const Ref<Texture2D> &p_icon);
  259. Ref<Texture2D> get_right_icon();
  260. virtual bool is_text_field() const override;
  261. void show_virtual_keyboard();
  262. LineEdit();
  263. ~LineEdit();
  264. };
  265. VARIANT_ENUM_CAST(LineEdit::Align);
  266. VARIANT_ENUM_CAST(LineEdit::MenuItems);
  267. #endif