line_edit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*************************************************************************/
  2. /* line_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 MenuItems {
  38. MENU_CUT,
  39. MENU_COPY,
  40. MENU_PASTE,
  41. MENU_CLEAR,
  42. MENU_SELECT_ALL,
  43. MENU_UNDO,
  44. MENU_REDO,
  45. MENU_DIR_INHERITED,
  46. MENU_DIR_AUTO,
  47. MENU_DIR_LTR,
  48. MENU_DIR_RTL,
  49. MENU_DISPLAY_UCC,
  50. MENU_INSERT_LRM,
  51. MENU_INSERT_RLM,
  52. MENU_INSERT_LRE,
  53. MENU_INSERT_RLE,
  54. MENU_INSERT_LRO,
  55. MENU_INSERT_RLO,
  56. MENU_INSERT_PDF,
  57. MENU_INSERT_ALM,
  58. MENU_INSERT_LRI,
  59. MENU_INSERT_RLI,
  60. MENU_INSERT_FSI,
  61. MENU_INSERT_PDI,
  62. MENU_INSERT_ZWJ,
  63. MENU_INSERT_ZWNJ,
  64. MENU_INSERT_WJ,
  65. MENU_INSERT_SHY,
  66. MENU_MAX
  67. };
  68. enum VirtualKeyboardType {
  69. KEYBOARD_TYPE_DEFAULT,
  70. KEYBOARD_TYPE_MULTILINE,
  71. KEYBOARD_TYPE_NUMBER,
  72. KEYBOARD_TYPE_NUMBER_DECIMAL,
  73. KEYBOARD_TYPE_PHONE,
  74. KEYBOARD_TYPE_EMAIL_ADDRESS,
  75. KEYBOARD_TYPE_PASSWORD,
  76. KEYBOARD_TYPE_URL
  77. };
  78. private:
  79. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
  80. bool editable = false;
  81. bool pass = false;
  82. bool text_changed_dirty = false;
  83. bool alt_start = false;
  84. uint32_t alt_code = 0;
  85. String undo_text;
  86. String text;
  87. String placeholder;
  88. String placeholder_translated;
  89. String secret_character = U"•";
  90. String ime_text;
  91. Point2 ime_selection;
  92. RID text_rid;
  93. float full_width = 0.0;
  94. bool selecting_enabled = true;
  95. bool deselect_on_focus_loss_enabled = true;
  96. bool context_menu_enabled = true;
  97. PopupMenu *menu = nullptr;
  98. PopupMenu *menu_dir = nullptr;
  99. PopupMenu *menu_ctl = nullptr;
  100. bool caret_mid_grapheme_enabled = true;
  101. int caret_column = 0;
  102. float scroll_offset = 0.0;
  103. int max_length = 0; // 0 for no maximum.
  104. String language;
  105. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  106. TextDirection input_direction = TEXT_DIRECTION_LTR;
  107. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  108. Array st_args;
  109. bool draw_control_chars = false;
  110. bool expand_to_text_length = false;
  111. bool window_has_focus = true;
  112. bool clear_button_enabled = false;
  113. bool shortcut_keys_enabled = true;
  114. bool virtual_keyboard_enabled = true;
  115. VirtualKeyboardType virtual_keyboard_type = KEYBOARD_TYPE_DEFAULT;
  116. bool middle_mouse_paste_enabled = true;
  117. bool drag_action = false;
  118. bool drag_caret_force_displayed = false;
  119. Ref<Texture2D> right_icon;
  120. bool flat = false;
  121. struct Selection {
  122. int begin = 0;
  123. int end = 0;
  124. int start_column = 0;
  125. bool enabled = false;
  126. bool creating = false;
  127. bool double_click = false;
  128. bool drag_attempt = false;
  129. } selection;
  130. struct TextOperation {
  131. int caret_column = 0;
  132. float scroll_offset = 0.0;
  133. String text;
  134. };
  135. List<TextOperation> undo_stack;
  136. List<TextOperation>::Element *undo_stack_pos = nullptr;
  137. struct ClearButtonStatus {
  138. bool press_attempt = false;
  139. bool pressing_inside = false;
  140. } clear_button_status;
  141. uint64_t last_dblclk = 0;
  142. Vector2 last_dblclk_pos;
  143. bool caret_blink_enabled = false;
  144. bool caret_force_displayed = false;
  145. bool draw_caret = true;
  146. float caret_blink_speed = 0.65;
  147. double caret_blink_timer = 0.0;
  148. bool caret_blinking = false;
  149. struct ThemeCache {
  150. Ref<StyleBox> normal;
  151. Ref<StyleBox> read_only;
  152. Ref<StyleBox> focus;
  153. Ref<Font> font;
  154. int font_size = 0;
  155. Color font_color;
  156. Color font_uneditable_color;
  157. Color font_selected_color;
  158. int font_outline_size;
  159. Color font_outline_color;
  160. Color font_placeholder_color;
  161. int caret_width = 0;
  162. Color caret_color;
  163. int minimum_character_width = 0;
  164. Color selection_color;
  165. Ref<Texture2D> clear_icon;
  166. Color clear_button_color;
  167. Color clear_button_color_pressed;
  168. float base_scale = 1.0;
  169. } theme_cache;
  170. bool _is_over_clear_button(const Point2 &p_pos) const;
  171. void _clear_undo_stack();
  172. void _clear_redo();
  173. void _create_undo_state();
  174. Key _get_menu_action_accelerator(const String &p_action);
  175. void _shape();
  176. void _fit_to_width();
  177. void _text_changed();
  178. void _emit_text_change();
  179. void shift_selection_check_pre(bool);
  180. void shift_selection_check_post(bool);
  181. void selection_fill_at_caret();
  182. void set_scroll_offset(float p_pos);
  183. float get_scroll_offset() const;
  184. void set_caret_at_pixel_pos(int p_x);
  185. Vector2 get_caret_pixel_pos();
  186. void _reset_caret_blink_timer();
  187. void _toggle_draw_caret();
  188. void clear_internal();
  189. void _editor_settings_changed();
  190. void _swap_current_input_direction();
  191. void _move_caret_left(bool p_select, bool p_move_by_word = false);
  192. void _move_caret_right(bool p_select, bool p_move_by_word = false);
  193. void _move_caret_start(bool p_select);
  194. void _move_caret_end(bool p_select);
  195. void _backspace(bool p_word = false, bool p_all_to_left = false);
  196. void _delete(bool p_word = false, bool p_all_to_right = false);
  197. void _ensure_menu();
  198. protected:
  199. virtual void _update_theme_item_cache() override;
  200. void _notification(int p_what);
  201. static void _bind_methods();
  202. virtual void unhandled_key_input(const Ref<InputEvent> &p_event) override;
  203. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  204. void _validate_property(PropertyInfo &p_property) const;
  205. public:
  206. void set_horizontal_alignment(HorizontalAlignment p_alignment);
  207. HorizontalAlignment get_horizontal_alignment() const;
  208. virtual Variant get_drag_data(const Point2 &p_point) override;
  209. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  210. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
  211. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  212. void menu_option(int p_option);
  213. void set_context_menu_enabled(bool p_enable);
  214. bool is_context_menu_enabled();
  215. PopupMenu *get_menu() const;
  216. bool is_menu_visible() const;
  217. void select(int p_from = 0, int p_to = -1);
  218. void select_all();
  219. void selection_delete();
  220. void deselect();
  221. bool has_selection() const;
  222. int get_selection_from_column() const;
  223. int get_selection_to_column() const;
  224. void delete_char();
  225. void delete_text(int p_from_column, int p_to_column);
  226. void set_text(String p_text);
  227. String get_text() const;
  228. void set_text_direction(TextDirection p_text_direction);
  229. TextDirection get_text_direction() const;
  230. void set_language(const String &p_language);
  231. String get_language() const;
  232. void set_draw_control_chars(bool p_draw_control_chars);
  233. bool get_draw_control_chars() const;
  234. void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
  235. TextServer::StructuredTextParser get_structured_text_bidi_override() const;
  236. void set_structured_text_bidi_override_options(Array p_args);
  237. Array get_structured_text_bidi_override_options() const;
  238. void set_placeholder(String p_text);
  239. String get_placeholder() const;
  240. void set_caret_column(int p_column);
  241. int get_caret_column() const;
  242. void set_max_length(int p_max_length);
  243. int get_max_length() const;
  244. void insert_text_at_caret(String p_text);
  245. void clear();
  246. void set_caret_mid_grapheme_enabled(const bool p_enabled);
  247. bool is_caret_mid_grapheme_enabled() const;
  248. bool is_caret_blink_enabled() const;
  249. void set_caret_blink_enabled(const bool p_enabled);
  250. float get_caret_blink_speed() const;
  251. void set_caret_blink_speed(const float p_speed);
  252. void set_caret_force_displayed(const bool p_enabled);
  253. bool is_caret_force_displayed() const;
  254. void copy_text();
  255. void cut_text();
  256. void paste_text();
  257. bool has_undo() const;
  258. bool has_redo() const;
  259. void undo();
  260. void redo();
  261. void set_editable(bool p_editable);
  262. bool is_editable() const;
  263. void set_secret(bool p_secret);
  264. bool is_secret() const;
  265. void set_secret_character(const String &p_string);
  266. String get_secret_character() const;
  267. virtual Size2 get_minimum_size() const override;
  268. void set_expand_to_text_length_enabled(bool p_enabled);
  269. bool is_expand_to_text_length_enabled() const;
  270. void set_clear_button_enabled(bool p_enabled);
  271. bool is_clear_button_enabled() const;
  272. void set_shortcut_keys_enabled(bool p_enabled);
  273. bool is_shortcut_keys_enabled() const;
  274. void set_virtual_keyboard_enabled(bool p_enable);
  275. bool is_virtual_keyboard_enabled() const;
  276. void set_virtual_keyboard_type(VirtualKeyboardType p_type);
  277. VirtualKeyboardType get_virtual_keyboard_type() const;
  278. void set_middle_mouse_paste_enabled(bool p_enabled);
  279. bool is_middle_mouse_paste_enabled() const;
  280. void set_selecting_enabled(bool p_enabled);
  281. bool is_selecting_enabled() const;
  282. void set_deselect_on_focus_loss_enabled(const bool p_enabled);
  283. bool is_deselect_on_focus_loss_enabled() const;
  284. void set_right_icon(const Ref<Texture2D> &p_icon);
  285. Ref<Texture2D> get_right_icon();
  286. void set_flat(bool p_enabled);
  287. bool is_flat() const;
  288. virtual bool is_text_field() const override;
  289. void show_virtual_keyboard();
  290. LineEdit(const String &p_placeholder = String());
  291. ~LineEdit();
  292. };
  293. VARIANT_ENUM_CAST(LineEdit::MenuItems);
  294. VARIANT_ENUM_CAST(LineEdit::VirtualKeyboardType);
  295. #endif // LINE_EDIT_H