guiEditBox.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Minetest
  3. Copyright (C) 2021 Minetest
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "IGUIEditBox.h"
  19. #include "IOSOperator.h"
  20. #include "guiScrollBar.h"
  21. #include <vector>
  22. using namespace irr;
  23. using namespace irr::gui;
  24. class GUIEditBox : public IGUIEditBox
  25. {
  26. public:
  27. GUIEditBox(IGUIEnvironment *environment, IGUIElement *parent, s32 id,
  28. core::rect<s32> rectangle, bool border, bool writable) :
  29. IGUIEditBox(environment, parent, id, rectangle),
  30. m_border(border), m_writable(writable), m_frame_rect(rectangle)
  31. {
  32. }
  33. virtual ~GUIEditBox();
  34. //! Sets another skin independent font.
  35. virtual void setOverrideFont(IGUIFont *font = 0);
  36. virtual IGUIFont *getOverrideFont() const { return m_override_font; }
  37. //! Get the font which is used right now for drawing
  38. /** Currently this is the override font when one is set and the
  39. font of the active skin otherwise */
  40. virtual IGUIFont *getActiveFont() const;
  41. //! Sets another color for the text.
  42. virtual void setOverrideColor(video::SColor color);
  43. //! Gets the override color
  44. virtual video::SColor getOverrideColor() const;
  45. //! Sets if the text should use the overide color or the
  46. //! color in the gui skin.
  47. virtual void enableOverrideColor(bool enable);
  48. //! Checks if an override color is enabled
  49. /** \return true if the override color is enabled, false otherwise */
  50. virtual bool isOverrideColorEnabled(void) const
  51. {
  52. return m_override_color_enabled;
  53. }
  54. //! Enables or disables word wrap for using the edit box as multiline text editor.
  55. virtual void setWordWrap(bool enable);
  56. //! Checks if word wrap is enabled
  57. //! \return true if word wrap is enabled, false otherwise
  58. virtual bool isWordWrapEnabled() const { return m_word_wrap; }
  59. //! Turns the border on or off
  60. virtual void setDrawBorder(bool border);
  61. virtual bool isDrawBorderEnabled() const { return m_border; }
  62. //! Enables or disables newlines.
  63. /** \param enable: If set to true, the EGET_EDITBOX_ENTER event will not be fired,
  64. instead a newline character will be inserted. */
  65. virtual void setMultiLine(bool enable);
  66. //! Checks if multi line editing is enabled
  67. //! \return true if mult-line is enabled, false otherwise
  68. virtual bool isMultiLineEnabled() const { return m_multiline; }
  69. //! Enables or disables automatic scrolling with cursor position
  70. //! \param enable: If set to true, the text will move around with the cursor
  71. //! position
  72. virtual void setAutoScroll(bool enable);
  73. //! Checks to see if automatic scrolling is enabled
  74. //! \return true if automatic scrolling is enabled, false if not
  75. virtual bool isAutoScrollEnabled() const { return m_autoscroll; }
  76. //! Sets whether the edit box is a password box. Setting this to true will
  77. /** disable MultiLine, WordWrap and the ability to copy with ctrl+c or ctrl+x
  78. \param passwordBox: true to enable password, false to disable
  79. \param passwordChar: the character that is displayed instead of letters */
  80. virtual void setPasswordBox(bool passwordBox, wchar_t passwordChar = L'*');
  81. //! Returns true if the edit box is currently a password box.
  82. virtual bool isPasswordBox() const { return m_passwordbox; }
  83. //! Sets text justification
  84. virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
  85. //! Sets the new caption of this element.
  86. virtual void setText(const wchar_t *text);
  87. //! Sets the maximum amount of characters which may be entered in the box.
  88. //! \param max: Maximum amount of characters. If 0, the character amount is
  89. //! infinity.
  90. virtual void setMax(u32 max);
  91. //! Returns maximum amount of characters, previously set by setMax();
  92. virtual u32 getMax() const { return m_max; }
  93. //! Gets the size area of the text in the edit box
  94. //! \return Returns the size in pixels of the text
  95. virtual core::dimension2du getTextDimension();
  96. //! set true if this EditBox is writable
  97. virtual void setWritable(bool can_write_text);
  98. //! called if an event happened.
  99. virtual bool OnEvent(const SEvent &event);
  100. //! Writes attributes of the element.
  101. virtual void serializeAttributes(io::IAttributes *out,
  102. io::SAttributeReadWriteOptions *options) const;
  103. //! Reads attributes of the element
  104. virtual void deserializeAttributes(
  105. io::IAttributes *in, io::SAttributeReadWriteOptions *options);
  106. virtual bool acceptsIME() { return isEnabled() && m_writable; };
  107. protected:
  108. virtual void breakText() = 0;
  109. //! sets the area of the given line
  110. virtual void setTextRect(s32 line) = 0;
  111. //! set text markers
  112. void setTextMarkers(s32 begin, s32 end);
  113. //! send some gui event to parent
  114. void sendGuiEvent(EGUI_EVENT_TYPE type);
  115. //! calculates the current scroll position
  116. virtual void calculateScrollPos() = 0;
  117. virtual s32 getCursorPos(s32 x, s32 y) = 0;
  118. bool processKey(const SEvent &event);
  119. virtual void inputString(const core::stringw &str);
  120. virtual void inputChar(wchar_t c);
  121. //! returns the line number that the cursor is on
  122. s32 getLineFromPos(s32 pos);
  123. //! update the vertical scrollBar (visibilty & position)
  124. void updateVScrollBar();
  125. gui::IGUIFont *m_override_font = nullptr;
  126. bool m_override_color_enabled = false;
  127. bool m_word_wrap = false;
  128. bool m_multiline = false;
  129. bool m_autoscroll = true;
  130. bool m_border;
  131. bool m_passwordbox = false;
  132. wchar_t m_passwordchar = L'*';
  133. std::vector<core::stringw> m_broken_text;
  134. std::vector<s32> m_broken_text_positions;
  135. EGUI_ALIGNMENT m_halign = EGUIA_UPPERLEFT;
  136. EGUI_ALIGNMENT m_valign = EGUIA_CENTER;
  137. u32 m_blink_start_time = 0;
  138. s32 m_cursor_pos = 0;
  139. s32 m_hscroll_pos = 0;
  140. s32 m_vscroll_pos = 0; // scroll position in characters
  141. u32 m_max = 0;
  142. video::SColor m_override_color = video::SColor(101, 255, 255, 255);
  143. core::rect<s32> m_current_text_rect = core::rect<s32>(0, 0, 1, 1);
  144. bool m_writable;
  145. bool m_mouse_marking = false;
  146. s32 m_mark_begin = 0;
  147. s32 m_mark_end = 0;
  148. gui::IGUIFont *m_last_break_font = nullptr;
  149. IOSOperator *m_operator = nullptr;
  150. core::rect<s32> m_frame_rect; // temporary values
  151. u32 m_scrollbar_width = 0;
  152. GUIScrollBar *m_vscrollbar = nullptr;
  153. private:
  154. bool processMouse(const SEvent &event);
  155. bool onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end);
  156. bool onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end);
  157. void onKeyControlC(const SEvent &event);
  158. bool onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end);
  159. bool onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end);
  160. bool onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end);
  161. bool onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end);
  162. };