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.
20 #include "guiEditBox.h"
23 #include "IGUIEnvironment.h"
28 GUIEditBox::~GUIEditBox()
31 m_override_font->drop();
40 void GUIEditBox::setOverrideFont(IGUIFont *font)
42 if (m_override_font == font)
46 m_override_font->drop();
48 m_override_font = font;
51 m_override_font->grab();
56 //! Get the font which is used right now for drawing
57 IGUIFont *GUIEditBox::getActiveFont() const
60 return m_override_font;
61 IGUISkin *skin = Environment->getSkin();
63 return skin->getFont();
67 //! Sets another color for the text.
68 void GUIEditBox::setOverrideColor(video::SColor color)
70 m_override_color = color;
71 m_override_color_enabled = true;
74 video::SColor GUIEditBox::getOverrideColor() const
76 return m_override_color;
79 //! Sets if the text should use the overide color or the color in the gui skin.
80 void GUIEditBox::enableOverrideColor(bool enable)
82 m_override_color_enabled = enable;
85 //! Enables or disables word wrap
86 void GUIEditBox::setWordWrap(bool enable)
92 //! Enables or disables newlines.
93 void GUIEditBox::setMultiLine(bool enable)
98 //! Enables or disables automatic scrolling with cursor position
99 //! \param enable: If set to true, the text will move around with the cursor position
100 void GUIEditBox::setAutoScroll(bool enable)
102 m_autoscroll = enable;
105 void GUIEditBox::setPasswordBox(bool password_box, wchar_t password_char)
107 m_passwordbox = password_box;
109 m_passwordchar = password_char;
112 m_broken_text.clear();
116 //! Sets text justification
117 void GUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
119 m_halign = horizontal;
123 //! Sets the new caption of this element.
124 void GUIEditBox::setText(const wchar_t *text)
127 if (u32(m_cursor_pos) > Text.size())
128 m_cursor_pos = Text.size();
133 //! Sets the maximum amount of characters which may be entered in the box.
134 //! \param max: Maximum amount of characters. If 0, the character amount is
136 void GUIEditBox::setMax(u32 max)
140 if (Text.size() > m_max && m_max != 0)
141 Text = Text.subString(0, m_max);
144 //! Gets the area of the text in the edit box
145 //! \return Returns the size in pixels of the text
146 core::dimension2du GUIEditBox::getTextDimension()
151 ret = m_current_text_rect;
153 for (u32 i = 1; i < m_broken_text.size(); ++i) {
155 ret.addInternalPoint(m_current_text_rect.UpperLeftCorner);
156 ret.addInternalPoint(m_current_text_rect.LowerRightCorner);
159 return core::dimension2du(ret.getSize());
162 //! Turns the border on or off
163 void GUIEditBox::setDrawBorder(bool border)
168 void GUIEditBox::setWritable(bool can_write_text)
170 m_writable = can_write_text;
174 void GUIEditBox::setTextMarkers(s32 begin, s32 end)
176 if (begin != m_mark_begin || end != m_mark_end) {
177 m_mark_begin = begin;
179 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
183 //! send some gui event to parent
184 void GUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
188 e.EventType = EET_GUI_EVENT;
189 e.GUIEvent.Caller = this;
190 e.GUIEvent.Element = 0;
191 e.GUIEvent.EventType = type;
197 //! called if an event happened.
198 bool GUIEditBox::OnEvent(const SEvent &event)
202 switch (event.EventType) {
204 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
205 if (event.GUIEvent.Caller == this) {
206 m_mouse_marking = false;
207 setTextMarkers(0, 0);
211 case EET_KEY_INPUT_EVENT: {
212 #if (defined(__linux__) || defined(__FreeBSD__)) || defined(__DragonFly__)
213 // ################################################################
215 // This part is the difference from the original intlGUIEditBox
216 // It converts UTF-8 character into a UCS-2 (wchar_t)
218 mbtowc(&wc, (char *)&event.KeyInput.Char,
219 sizeof(event.KeyInput.Char));
221 // printf( "char: %lc (%u) \r\n", wc, wc );
223 SEvent irrevent(event);
224 irrevent.KeyInput.Char = wc;
225 // ################################################################
227 if (processKey(irrevent))
230 if (processKey(event))
232 #endif // defined(linux)
236 case EET_MOUSE_INPUT_EVENT:
237 if (processMouse(event))
245 return IGUIElement::OnEvent(event);
248 bool GUIEditBox::processKey(const SEvent &event)
254 if (!event.KeyInput.PressedDown)
257 bool text_changed = false;
258 s32 new_mark_begin = m_mark_begin;
259 s32 new_mark_end = m_mark_end;
261 // control shortcut handling
262 if (event.KeyInput.Control) {
264 // german backlash '\' entered with control + '?'
265 if (event.KeyInput.Char == '\\') {
266 inputChar(event.KeyInput.Char);
270 switch (event.KeyInput.Key) {
274 new_mark_end = Text.size();
277 onKeyControlC(event);
280 text_changed = onKeyControlX(event, new_mark_begin, new_mark_end);
283 text_changed = onKeyControlV(event, new_mark_begin, new_mark_end);
286 // move/highlight to start of text
287 if (event.KeyInput.Shift) {
288 new_mark_end = m_cursor_pos;
298 // move/highlight to end of text
299 if (event.KeyInput.Shift) {
300 new_mark_begin = m_cursor_pos;
301 new_mark_end = Text.size();
304 m_cursor_pos = Text.size();
313 switch (event.KeyInput.Key) {
316 if (m_word_wrap || m_multiline) {
317 p = getLineFromPos(m_cursor_pos);
318 p = m_broken_text_positions[p] +
319 (s32)m_broken_text[p].size();
320 if (p > 0 && (Text[p - 1] == L'\r' ||
321 Text[p - 1] == L'\n'))
325 if (event.KeyInput.Shift) {
326 if (m_mark_begin == m_mark_end)
327 new_mark_begin = m_cursor_pos;
335 m_blink_start_time = porting::getTimeMs();
340 if (m_word_wrap || m_multiline) {
341 p = getLineFromPos(m_cursor_pos);
342 p = m_broken_text_positions[p];
345 if (event.KeyInput.Shift) {
346 if (m_mark_begin == m_mark_end)
347 new_mark_begin = m_cursor_pos;
354 m_blink_start_time = porting::getTimeMs();
360 calculateScrollPos();
361 sendGuiEvent(EGET_EDITBOX_ENTER);
365 if (event.KeyInput.Shift) {
366 if (m_cursor_pos > 0) {
367 if (m_mark_begin == m_mark_end)
368 new_mark_begin = m_cursor_pos;
370 new_mark_end = m_cursor_pos - 1;
377 if (m_cursor_pos > 0)
379 m_blink_start_time = porting::getTimeMs();
382 if (event.KeyInput.Shift) {
383 if (Text.size() > (u32)m_cursor_pos) {
384 if (m_mark_begin == m_mark_end)
385 new_mark_begin = m_cursor_pos;
387 new_mark_end = m_cursor_pos + 1;
394 if (Text.size() > (u32)m_cursor_pos)
396 m_blink_start_time = porting::getTimeMs();
399 if (!onKeyUp(event, new_mark_begin, new_mark_end)) {
404 if (!onKeyDown(event, new_mark_begin, new_mark_end)) {
409 text_changed = onKeyBack(event, new_mark_begin, new_mark_end);
413 text_changed = onKeyDelete(event, new_mark_begin, new_mark_end);
447 inputChar(event.KeyInput.Char);
452 // Set new text markers
453 setTextMarkers(new_mark_begin, new_mark_end);
455 // break the text if it has changed
458 sendGuiEvent(EGET_EDITBOX_CHANGED);
461 calculateScrollPos();
466 bool GUIEditBox::onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end)
469 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
470 s32 lineNo = getLineFromPos(m_cursor_pos);
471 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
472 (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
474 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
475 if ((s32)m_broken_text[lineNo - 1].size() < cp) {
476 m_cursor_pos = m_broken_text_positions[lineNo - 1] +
477 core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
480 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
483 if (event.KeyInput.Shift) {
485 mark_end = m_cursor_pos;
498 bool GUIEditBox::onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end)
501 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
502 s32 lineNo = getLineFromPos(m_cursor_pos);
503 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
504 (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
505 if (lineNo < (s32)m_broken_text.size() - 1) {
506 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
507 if ((s32)m_broken_text[lineNo + 1].size() < cp) {
508 m_cursor_pos = m_broken_text_positions[lineNo + 1] +
509 core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
512 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
515 if (event.KeyInput.Shift) {
517 mark_end = m_cursor_pos;
530 void GUIEditBox::onKeyControlC(const SEvent &event)
533 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
536 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
537 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
540 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
541 m_operator->copyToClipboard(s.c_str());
544 bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end)
546 // First copy to clipboard
547 onKeyControlC(event);
549 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
552 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
553 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
555 // Now remove from box if enabled
559 s = Text.subString(0, realmbgn);
560 s.append(Text.subString(realmend, Text.size() - realmend));
563 m_cursor_pos = realmbgn;
572 bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end)
577 // paste from the clipboard
581 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
582 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
585 if (const c8 *p = m_operator->getTextFromClipboard()) {
586 if (m_mark_begin == m_mark_end) {
588 core::stringw s = Text.subString(0, m_cursor_pos);
590 s.append(Text.subString(
591 m_cursor_pos, Text.size() - m_cursor_pos));
593 if (!m_max || s.size() <= m_max) {
596 m_cursor_pos += s.size();
601 core::stringw s = Text.subString(0, realmbgn);
603 s.append(Text.subString(realmend, Text.size() - realmend));
605 if (!m_max || s.size() <= m_max) {
608 m_cursor_pos = realmbgn + s.size();
618 bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
620 if (!isEnabled() || Text.empty())
625 if (m_mark_begin != m_mark_end) {
626 // delete marked text
628 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
630 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
632 s = Text.subString(0, realmbgn);
633 s.append(Text.subString(realmend, Text.size() - realmend));
636 m_cursor_pos = realmbgn;
638 // delete text behind cursor
639 if (m_cursor_pos > 0)
640 s = Text.subString(0, m_cursor_pos - 1);
643 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
648 if (m_cursor_pos < 0)
650 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
656 bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
658 if (!isEnabled() || Text.empty())
663 if (m_mark_begin != m_mark_end) {
664 // delete marked text
666 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
668 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
670 s = Text.subString(0, realmbgn);
671 s.append(Text.subString(realmend, Text.size() - realmend));
674 m_cursor_pos = realmbgn;
676 // delete text before cursor
677 s = Text.subString(0, m_cursor_pos);
678 s.append(Text.subString(
679 m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
683 if (m_cursor_pos > (s32)Text.size())
684 m_cursor_pos = (s32)Text.size();
686 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
692 void GUIEditBox::inputChar(wchar_t c)
694 if (!isEnabled() || !m_writable)
698 if (Text.size() < m_max || m_max == 0) {
701 if (m_mark_begin != m_mark_end) {
703 // replace marked text
704 s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
705 s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
707 s = Text.subString(0, real_begin);
709 s.append(Text.subString(real_end, Text.size() - real_end));
711 m_cursor_pos = real_begin + 1;
715 s = Text.subString(0, m_cursor_pos);
717 s.append(Text.subString(m_cursor_pos,
718 Text.size() - m_cursor_pos));
723 m_blink_start_time = porting::getTimeMs();
724 setTextMarkers(0, 0);
728 sendGuiEvent(EGET_EDITBOX_CHANGED);
729 calculateScrollPos();
732 bool GUIEditBox::processMouse(const SEvent &event)
734 switch (event.MouseInput.Event) {
735 case irr::EMIE_LMOUSE_LEFT_UP:
736 if (Environment->hasFocus(this)) {
737 m_cursor_pos = getCursorPos(
738 event.MouseInput.X, event.MouseInput.Y);
739 if (m_mouse_marking) {
740 setTextMarkers(m_mark_begin, m_cursor_pos);
742 m_mouse_marking = false;
743 calculateScrollPos();
747 case irr::EMIE_MOUSE_MOVED: {
748 if (m_mouse_marking) {
749 m_cursor_pos = getCursorPos(
750 event.MouseInput.X, event.MouseInput.Y);
751 setTextMarkers(m_mark_begin, m_cursor_pos);
752 calculateScrollPos();
756 case EMIE_LMOUSE_PRESSED_DOWN:
758 if (!Environment->hasFocus(this)) {
759 m_blink_start_time = porting::getTimeMs();
760 m_mouse_marking = true;
761 m_cursor_pos = getCursorPos(
762 event.MouseInput.X, event.MouseInput.Y);
763 setTextMarkers(m_cursor_pos, m_cursor_pos);
764 calculateScrollPos();
767 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
768 event.MouseInput.X, event.MouseInput.Y))) {
772 m_cursor_pos = getCursorPos(
773 event.MouseInput.X, event.MouseInput.Y);
775 s32 newMarkBegin = m_mark_begin;
776 if (!m_mouse_marking)
777 newMarkBegin = m_cursor_pos;
779 m_mouse_marking = true;
780 setTextMarkers(newMarkBegin, m_cursor_pos);
781 calculateScrollPos();
785 case EMIE_MOUSE_WHEEL:
786 if (m_vscrollbar && m_vscrollbar->isVisible()) {
787 s32 pos = m_vscrollbar->getPos();
788 s32 step = m_vscrollbar->getSmallStep();
789 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
800 s32 GUIEditBox::getLineFromPos(s32 pos)
802 if (!m_word_wrap && !m_multiline)
806 while (i < (s32)m_broken_text_positions.size()) {
807 if (m_broken_text_positions[i] > pos)
811 return (s32)m_broken_text_positions.size() - 1;
814 void GUIEditBox::updateVScrollBar()
820 // OnScrollBarChanged(...)
821 if (m_vscrollbar->getPos() != m_vscroll_pos) {
822 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
823 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
824 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
826 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
827 if (scrollymax != m_vscrollbar->getMax()) {
828 // manage a newline or a deleted line
829 m_vscrollbar->setMax(scrollymax);
830 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
831 calculateScrollPos();
833 // manage a newline or a deleted line
834 m_vscroll_pos = m_vscrollbar->getPos();
838 // check if a vertical scrollbar is needed ?
839 if (getTextDimension().Height > (u32)m_frame_rect.getHeight()) {
840 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
842 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
843 if (scrollymax != m_vscrollbar->getMax()) {
844 m_vscrollbar->setMax(scrollymax);
845 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
848 if (!m_vscrollbar->isVisible()) {
849 m_vscrollbar->setVisible(true);
852 if (m_vscrollbar->isVisible()) {
853 m_vscrollbar->setVisible(false);
855 m_vscrollbar->setPos(0);
856 m_vscrollbar->setMax(1);
857 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
862 void GUIEditBox::deserializeAttributes(
863 io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0)
865 IGUIEditBox::deserializeAttributes(in, options);
867 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
868 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
869 setMax(in->getAttributeAsInt("MaxChars"));
870 setWordWrap(in->getAttributeAsBool("WordWrap"));
871 setMultiLine(in->getAttributeAsBool("MultiLine"));
872 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
873 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
876 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
878 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
880 setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
881 "HTextAlign", GUIAlignmentNames),
882 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
883 "VTextAlign", GUIAlignmentNames));
885 setWritable(in->getAttributeAsBool("Writable"));
886 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
889 //! Writes attributes of the element.
890 void GUIEditBox::serializeAttributes(
891 io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const
893 // IGUIEditBox::serializeAttributes(out,options);
895 out->addBool("OverrideColorEnabled", m_override_color_enabled);
896 out->addColor("OverrideColor", m_override_color);
897 // out->addFont("OverrideFont",m_override_font);
898 out->addInt("MaxChars", m_max);
899 out->addBool("WordWrap", m_word_wrap);
900 out->addBool("MultiLine", m_multiline);
901 out->addBool("AutoScroll", m_autoscroll);
902 out->addBool("PasswordBox", m_passwordbox);
903 core::stringw ch = L" ";
904 ch[0] = m_passwordchar;
905 out->addString("PasswordChar", ch.c_str());
906 out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
907 out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
908 out->addBool("Writable", m_writable);
910 IGUIEditBox::serializeAttributes(out, options);