line_edit.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*************************************************************************/
  2. /* line_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. String undo_text;
  61. String text;
  62. String placeholder;
  63. float placeholder_alpha;
  64. String ime_text;
  65. Point2 ime_selection;
  66. PopupMenu *menu;
  67. int cursor_pos;
  68. int window_pos;
  69. int max_length; // 0 for no maximum
  70. int cached_width;
  71. struct Selection {
  72. int begin;
  73. int end;
  74. int cursor_start;
  75. bool enabled;
  76. bool creating;
  77. bool doubleclick;
  78. bool drag_attempt;
  79. } selection;
  80. struct TextOperation {
  81. int cursor_pos;
  82. String text;
  83. };
  84. List<TextOperation> undo_stack;
  85. List<TextOperation>::Element *undo_stack_pos;
  86. void _clear_undo_stack();
  87. void _clear_redo();
  88. void _create_undo_state();
  89. Timer *caret_blink_timer;
  90. static void _ime_text_callback(void *p_self, String p_text, Point2 p_selection);
  91. void _text_changed();
  92. void _emit_text_change();
  93. bool expand_to_text_length;
  94. bool caret_blink_enabled;
  95. bool draw_caret;
  96. bool window_has_focus;
  97. void shift_selection_check_pre(bool);
  98. void shift_selection_check_post(bool);
  99. void selection_clear();
  100. void selection_fill_at_cursor();
  101. void selection_delete();
  102. void set_window_pos(int p_pos);
  103. void set_cursor_at_pixel_pos(int p_x);
  104. void _reset_caret_blink_timer();
  105. void _toggle_draw_caret();
  106. void clear_internal();
  107. void changed_internal();
  108. #ifdef TOOLS_ENABLED
  109. void _editor_settings_changed();
  110. #endif
  111. void _gui_input(Ref<InputEvent> p_event);
  112. void _notification(int p_what);
  113. protected:
  114. static void _bind_methods();
  115. public:
  116. void set_align(Align p_align);
  117. Align get_align() const;
  118. virtual Variant get_drag_data(const Point2 &p_point);
  119. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  120. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  121. void menu_option(int p_option);
  122. PopupMenu *get_menu() const;
  123. void select_all();
  124. void delete_char();
  125. void delete_text(int p_from_column, int p_to_column);
  126. void set_text(String p_text);
  127. String get_text() const;
  128. void set_placeholder(String p_text);
  129. String get_placeholder() const;
  130. void set_placeholder_alpha(float p_alpha);
  131. float get_placeholder_alpha() const;
  132. void set_cursor_position(int p_pos);
  133. int get_cursor_position() const;
  134. void set_max_length(int p_max_length);
  135. int get_max_length() const;
  136. void append_at_cursor(String p_text);
  137. void clear();
  138. bool cursor_get_blink_enabled() const;
  139. void cursor_set_blink_enabled(const bool p_enabled);
  140. float cursor_get_blink_speed() const;
  141. void cursor_set_blink_speed(const float p_speed);
  142. void copy_text();
  143. void cut_text();
  144. void paste_text();
  145. void undo();
  146. void redo();
  147. void set_editable(bool p_editable);
  148. bool is_editable() const;
  149. void set_secret(bool p_secret);
  150. bool is_secret() const;
  151. void select(int p_from = 0, int p_to = -1);
  152. virtual Size2 get_minimum_size() const;
  153. void set_expand_to_text_length(bool p_enabled);
  154. bool get_expand_to_text_length() const;
  155. virtual bool is_text_field() const;
  156. LineEdit();
  157. ~LineEdit();
  158. };
  159. VARIANT_ENUM_CAST(LineEdit::Align);
  160. VARIANT_ENUM_CAST(LineEdit::MenuItems);
  161. #endif