]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiEditBox.h
Translated using Weblate (Malay)
[minetest.git] / src / gui / guiEditBox.h
1 /*
2 Minetest
3 Copyright (C) 2021 Minetest
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes.h"
23 #include "IGUIEditBox.h"
24 #include "IOSOperator.h"
25 #include "guiScrollBar.h"
26 #include <vector>
27
28 using namespace irr;
29 using namespace irr::gui;
30
31 class GUIEditBox : public IGUIEditBox
32 {
33 public:
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)
38         {
39         }
40
41         virtual ~GUIEditBox();
42
43         //! Sets another skin independent font.
44         virtual void setOverrideFont(IGUIFont *font = 0);
45
46         virtual IGUIFont *getOverrideFont() const { return m_override_font; }
47
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;
52
53         //! Sets another color for the text.
54         virtual void setOverrideColor(video::SColor color);
55
56         //! Gets the override color
57         virtual video::SColor getOverrideColor() const;
58
59         //! Sets if the text should use the overide color or the
60         //! color in the gui skin.
61         virtual void enableOverrideColor(bool enable);
62
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
66         {
67                 return m_override_color_enabled;
68         }
69
70         //! Enables or disables word wrap for using the edit box as multiline text editor.
71         virtual void setWordWrap(bool enable);
72
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; }
76
77         //! Turns the border on or off
78         virtual void setDrawBorder(bool border);
79
80         virtual bool isDrawBorderEnabled() const { return m_border; }
81
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);
86
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; }
90
91         //! Enables or disables automatic scrolling with cursor position
92         //! \param enable: If set to true, the text will move around with the cursor
93         //! position
94         virtual void setAutoScroll(bool enable);
95
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; }
99
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'*');
105
106         //! Returns true if the edit box is currently a password box.
107         virtual bool isPasswordBox() const { return m_passwordbox; }
108
109         //! Sets text justification
110         virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
111
112         //! Sets the new caption of this element.
113         virtual void setText(const wchar_t *text);
114
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
117         //! infinity.
118         virtual void setMax(u32 max);
119
120         //! Returns maximum amount of characters, previously set by setMax();
121         virtual u32 getMax() const { return m_max; }
122
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();
126
127         //! set true if this EditBox is writable
128         virtual void setWritable(bool can_write_text);
129
130         //! called if an event happened.
131         virtual bool OnEvent(const SEvent &event);
132
133         virtual bool acceptsIME() { return isEnabled() && m_writable; };
134
135 protected:
136         virtual void breakText() = 0;
137
138         //! sets the area of the given line
139         virtual void setTextRect(s32 line) = 0;
140
141         //! set text markers
142         void setTextMarkers(s32 begin, s32 end);
143
144         //! send some gui event to parent
145         void sendGuiEvent(EGUI_EVENT_TYPE type);
146
147         //! calculates the current scroll position
148         virtual void calculateScrollPos() = 0;
149
150         virtual s32 getCursorPos(s32 x, s32 y) = 0;
151
152         bool processKey(const SEvent &event);
153         virtual void inputString(const core::stringw &str);
154         virtual void inputChar(wchar_t c);
155
156         //! returns the line number that the cursor is on
157         s32 getLineFromPos(s32 pos);
158
159         //! update the vertical scrollBar (visibilty & position)
160         void updateVScrollBar();
161
162         gui::IGUIFont *m_override_font = nullptr;
163
164         bool m_override_color_enabled = false;
165         bool m_word_wrap = false;
166         bool m_multiline = false;
167         bool m_autoscroll = true;
168
169         bool m_border;
170
171         bool m_passwordbox = false;
172         wchar_t m_passwordchar = L'*';
173
174         std::vector<core::stringw> m_broken_text;
175         std::vector<s32> m_broken_text_positions;
176
177         EGUI_ALIGNMENT m_halign = EGUIA_UPPERLEFT;
178         EGUI_ALIGNMENT m_valign = EGUIA_CENTER;
179
180         u32 m_blink_start_time = 0;
181         s32 m_cursor_pos = 0;
182         s32 m_hscroll_pos = 0;
183         s32 m_vscroll_pos = 0; // scroll position in characters
184         u32 m_max = 0;
185
186         video::SColor m_override_color = video::SColor(101, 255, 255, 255);
187
188         core::rect<s32> m_current_text_rect = core::rect<s32>(0, 0, 1, 1);
189
190         bool m_writable;
191
192         bool m_mouse_marking = false;
193
194         s32 m_mark_begin = 0;
195         s32 m_mark_end = 0;
196
197         gui::IGUIFont *m_last_break_font = nullptr;
198         IOSOperator *m_operator = nullptr;
199
200         core::rect<s32> m_frame_rect; // temporary values
201
202         u32 m_scrollbar_width = 0;
203         GUIScrollBar *m_vscrollbar = nullptr;
204
205 private:
206         bool processMouse(const SEvent &event);
207
208         bool onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end);
209         bool onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end);
210         void onKeyControlC(const SEvent &event);
211         bool onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end);
212         bool onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end);
213         bool onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end);
214         bool onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end);
215 };