line_edit.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*************************************************************************/
  2. /* line_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class LineEdit : public Control {
  38. GDCLASS(LineEdit, Control);
  39. public:
  40. enum Align {
  41. ALIGN_LEFT,
  42. ALIGN_CENTER,
  43. ALIGN_RIGHT,
  44. ALIGN_FILL
  45. };
  46. enum MenuItems {
  47. MENU_CUT,
  48. MENU_COPY,
  49. MENU_PASTE,
  50. MENU_CLEAR,
  51. MENU_SELECT_ALL,
  52. MENU_UNDO,
  53. MENU_REDO,
  54. MENU_MAX
  55. };
  56. private:
  57. Align align;
  58. bool editable;
  59. bool pass;
  60. bool text_changed_dirty;
  61. String undo_text;
  62. String text;
  63. String placeholder;
  64. String secret_character;
  65. float placeholder_alpha;
  66. String ime_text;
  67. Point2 ime_selection;
  68. bool context_menu_enabled;
  69. PopupMenu *menu;
  70. int cursor_pos;
  71. int window_pos;
  72. int max_length; // 0 for no maximum
  73. int cached_width;
  74. int cached_placeholder_width;
  75. bool clear_button_enabled;
  76. Ref<Texture> right_icon;
  77. struct Selection {
  78. int begin;
  79. int end;
  80. int cursor_start;
  81. bool enabled;
  82. bool creating;
  83. bool doubleclick;
  84. bool drag_attempt;
  85. } selection;
  86. struct TextOperation {
  87. int cursor_pos;
  88. String text;
  89. };
  90. List<TextOperation> undo_stack;
  91. List<TextOperation>::Element *undo_stack_pos;
  92. struct ClearButtonStatus {
  93. bool press_attempt;
  94. bool pressing_inside;
  95. } clear_button_status;
  96. bool _is_over_clear_button(const Point2 &p_pos) const;
  97. void _clear_undo_stack();
  98. void _clear_redo();
  99. void _create_undo_state();
  100. Timer *caret_blink_timer;
  101. void _text_changed();
  102. void _emit_text_change();
  103. bool expand_to_text_length;
  104. bool caret_blink_enabled;
  105. bool draw_caret;
  106. bool window_has_focus;
  107. void shift_selection_check_pre(bool);
  108. void shift_selection_check_post(bool);
  109. void selection_fill_at_cursor();
  110. void set_window_pos(int p_pos);
  111. void set_cursor_at_pixel_pos(int p_x);
  112. void _reset_caret_blink_timer();
  113. void _toggle_draw_caret();
  114. void clear_internal();
  115. void changed_internal();
  116. void _editor_settings_changed();
  117. void _gui_input(Ref<InputEvent> p_event);
  118. void _notification(int p_what);
  119. protected:
  120. static void _bind_methods();
  121. public:
  122. void set_align(Align p_align);
  123. Align get_align() const;
  124. virtual Variant get_drag_data(const Point2 &p_point);
  125. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  126. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  127. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const;
  128. void menu_option(int p_option);
  129. void set_context_menu_enabled(bool p_enable);
  130. bool is_context_menu_enabled();
  131. PopupMenu *get_menu() const;
  132. void select(int p_from = 0, int p_to = -1);
  133. void select_all();
  134. void selection_delete();
  135. void deselect();
  136. void delete_char();
  137. void delete_text(int p_from_column, int p_to_column);
  138. void set_text(String p_text);
  139. String get_text() const;
  140. void set_placeholder(String p_text);
  141. String get_placeholder() const;
  142. void set_placeholder_alpha(float p_alpha);
  143. float get_placeholder_alpha() const;
  144. void set_cursor_position(int p_pos);
  145. int get_cursor_position() const;
  146. void set_max_length(int p_max_length);
  147. int get_max_length() const;
  148. void append_at_cursor(String p_text);
  149. void clear();
  150. bool cursor_get_blink_enabled() const;
  151. void cursor_set_blink_enabled(const bool p_enabled);
  152. float cursor_get_blink_speed() const;
  153. void cursor_set_blink_speed(const float p_speed);
  154. void copy_text();
  155. void cut_text();
  156. void paste_text();
  157. void undo();
  158. void redo();
  159. void set_editable(bool p_editable);
  160. bool is_editable() const;
  161. void set_secret(bool p_secret);
  162. bool is_secret() const;
  163. void set_secret_character(const String &p_string);
  164. String get_secret_character() const;
  165. virtual Size2 get_minimum_size() const;
  166. void set_expand_to_text_length(bool p_enabled);
  167. bool get_expand_to_text_length() const;
  168. void set_clear_button_enabled(bool p_enabled);
  169. bool is_clear_button_enabled() const;
  170. void set_right_icon(const Ref<Texture> &p_icon);
  171. virtual bool is_text_field() const;
  172. LineEdit();
  173. ~LineEdit();
  174. };
  175. VARIANT_ENUM_CAST(LineEdit::Align);
  176. VARIANT_ENUM_CAST(LineEdit::MenuItems);
  177. #endif