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