line_edit.h 12 KB

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