line_edit.h 13 KB

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