2
0

line_edit.h 12 KB

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