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"
22 #include "IrrCompileConfig.h"
24 #include "IGUIEnvironment.h"
29 GUIEditBox::~GUIEditBox()
32 m_override_font->drop();
41 void GUIEditBox::setOverrideFont(IGUIFont *font)
43 if (m_override_font == font)
47 m_override_font->drop();
49 m_override_font = font;
52 m_override_font->grab();
57 //! Get the font which is used right now for drawing
58 IGUIFont *GUIEditBox::getActiveFont() const
61 return m_override_font;
62 IGUISkin *skin = Environment->getSkin();
64 return skin->getFont();
68 //! Sets another color for the text.
69 void GUIEditBox::setOverrideColor(video::SColor color)
71 m_override_color = color;
72 m_override_color_enabled = true;
75 video::SColor GUIEditBox::getOverrideColor() const
77 return m_override_color;
80 //! Sets if the text should use the overide color or the color in the gui skin.
81 void GUIEditBox::enableOverrideColor(bool enable)
83 m_override_color_enabled = enable;
86 //! Enables or disables word wrap
87 void GUIEditBox::setWordWrap(bool enable)
93 //! Enables or disables newlines.
94 void GUIEditBox::setMultiLine(bool enable)
99 //! Enables or disables automatic scrolling with cursor position
100 //! \param enable: If set to true, the text will move around with the cursor position
101 void GUIEditBox::setAutoScroll(bool enable)
103 m_autoscroll = enable;
106 void GUIEditBox::setPasswordBox(bool password_box, wchar_t password_char)
108 m_passwordbox = password_box;
110 m_passwordchar = password_char;
113 m_broken_text.clear();
117 //! Sets text justification
118 void GUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
120 m_halign = horizontal;
124 //! Sets the new caption of this element.
125 void GUIEditBox::setText(const wchar_t *text)
128 if (u32(m_cursor_pos) > Text.size())
129 m_cursor_pos = Text.size();
134 //! Sets the maximum amount of characters which may be entered in the box.
135 //! \param max: Maximum amount of characters. If 0, the character amount is
137 void GUIEditBox::setMax(u32 max)
141 if (Text.size() > m_max && m_max != 0)
142 Text = Text.subString(0, m_max);
145 //! Gets the area of the text in the edit box
146 //! \return Returns the size in pixels of the text
147 core::dimension2du GUIEditBox::getTextDimension()
152 ret = m_current_text_rect;
154 for (u32 i = 1; i < m_broken_text.size(); ++i) {
156 ret.addInternalPoint(m_current_text_rect.UpperLeftCorner);
157 ret.addInternalPoint(m_current_text_rect.LowerRightCorner);
160 return core::dimension2du(ret.getSize());
163 //! Turns the border on or off
164 void GUIEditBox::setDrawBorder(bool border)
169 void GUIEditBox::setWritable(bool can_write_text)
171 m_writable = can_write_text;
175 void GUIEditBox::setTextMarkers(s32 begin, s32 end)
177 if (begin != m_mark_begin || end != m_mark_end) {
178 m_mark_begin = begin;
180 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
184 //! send some gui event to parent
185 void GUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
189 e.EventType = EET_GUI_EVENT;
190 e.GUIEvent.Caller = this;
191 e.GUIEvent.Element = 0;
192 e.GUIEvent.EventType = type;
198 //! called if an event happened.
199 bool GUIEditBox::OnEvent(const SEvent &event)
203 switch (event.EventType) {
205 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
206 if (event.GUIEvent.Caller == this) {
207 m_mouse_marking = false;
208 setTextMarkers(0, 0);
212 case EET_KEY_INPUT_EVENT:
213 if (processKey(event))
216 case EET_MOUSE_INPUT_EVENT:
217 if (processMouse(event))
220 #if (IRRLICHT_VERSION_MT_REVISION >= 2)
221 case EET_STRING_INPUT_EVENT:
222 inputString(*event.StringInput.Str);
230 return IGUIElement::OnEvent(event);
233 bool GUIEditBox::processKey(const SEvent &event)
239 if (!event.KeyInput.PressedDown)
242 bool text_changed = false;
243 s32 new_mark_begin = m_mark_begin;
244 s32 new_mark_end = m_mark_end;
246 // control shortcut handling
247 if (event.KeyInput.Control) {
249 // german backlash '\' entered with control + '?'
250 if (event.KeyInput.Char == '\\') {
251 inputChar(event.KeyInput.Char);
255 switch (event.KeyInput.Key) {
259 new_mark_end = Text.size();
262 onKeyControlC(event);
265 text_changed = onKeyControlX(event, new_mark_begin, new_mark_end);
268 text_changed = onKeyControlV(event, new_mark_begin, new_mark_end);
271 // move/highlight to start of text
272 if (event.KeyInput.Shift) {
273 new_mark_end = m_cursor_pos;
283 // move/highlight to end of text
284 if (event.KeyInput.Shift) {
285 new_mark_begin = m_cursor_pos;
286 new_mark_end = Text.size();
289 m_cursor_pos = Text.size();
298 switch (event.KeyInput.Key) {
301 if (m_word_wrap || m_multiline) {
302 p = getLineFromPos(m_cursor_pos);
303 p = m_broken_text_positions[p] +
304 (s32)m_broken_text[p].size();
305 if (p > 0 && (Text[p - 1] == L'\r' ||
306 Text[p - 1] == L'\n'))
310 if (event.KeyInput.Shift) {
311 if (m_mark_begin == m_mark_end)
312 new_mark_begin = m_cursor_pos;
320 m_blink_start_time = porting::getTimeMs();
325 if (m_word_wrap || m_multiline) {
326 p = getLineFromPos(m_cursor_pos);
327 p = m_broken_text_positions[p];
330 if (event.KeyInput.Shift) {
331 if (m_mark_begin == m_mark_end)
332 new_mark_begin = m_cursor_pos;
339 m_blink_start_time = porting::getTimeMs();
345 calculateScrollPos();
346 sendGuiEvent(EGET_EDITBOX_ENTER);
350 if (event.KeyInput.Shift) {
351 if (m_cursor_pos > 0) {
352 if (m_mark_begin == m_mark_end)
353 new_mark_begin = m_cursor_pos;
355 new_mark_end = m_cursor_pos - 1;
362 if (m_cursor_pos > 0)
364 m_blink_start_time = porting::getTimeMs();
367 if (event.KeyInput.Shift) {
368 if (Text.size() > (u32)m_cursor_pos) {
369 if (m_mark_begin == m_mark_end)
370 new_mark_begin = m_cursor_pos;
372 new_mark_end = m_cursor_pos + 1;
379 if (Text.size() > (u32)m_cursor_pos)
381 m_blink_start_time = porting::getTimeMs();
384 if (!onKeyUp(event, new_mark_begin, new_mark_end)) {
389 if (!onKeyDown(event, new_mark_begin, new_mark_end)) {
394 text_changed = onKeyBack(event, new_mark_begin, new_mark_end);
398 text_changed = onKeyDelete(event, new_mark_begin, new_mark_end);
432 inputChar(event.KeyInput.Char);
437 // Set new text markers
438 setTextMarkers(new_mark_begin, new_mark_end);
440 // break the text if it has changed
443 sendGuiEvent(EGET_EDITBOX_CHANGED);
446 calculateScrollPos();
451 bool GUIEditBox::onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end)
454 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
455 s32 lineNo = getLineFromPos(m_cursor_pos);
456 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
457 (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
459 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
460 if ((s32)m_broken_text[lineNo - 1].size() < cp) {
461 m_cursor_pos = m_broken_text_positions[lineNo - 1] +
462 core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
465 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
468 if (event.KeyInput.Shift) {
470 mark_end = m_cursor_pos;
483 bool GUIEditBox::onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end)
486 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
487 s32 lineNo = getLineFromPos(m_cursor_pos);
488 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
489 (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
490 if (lineNo < (s32)m_broken_text.size() - 1) {
491 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
492 if ((s32)m_broken_text[lineNo + 1].size() < cp) {
493 m_cursor_pos = m_broken_text_positions[lineNo + 1] +
494 core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
497 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
500 if (event.KeyInput.Shift) {
502 mark_end = m_cursor_pos;
515 void GUIEditBox::onKeyControlC(const SEvent &event)
518 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
521 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
522 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
525 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
526 m_operator->copyToClipboard(s.c_str());
529 bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end)
531 // First copy to clipboard
532 onKeyControlC(event);
534 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
537 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
538 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
540 // Now remove from box if enabled
544 s = Text.subString(0, realmbgn);
545 s.append(Text.subString(realmend, Text.size() - realmend));
548 m_cursor_pos = realmbgn;
557 bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end)
562 // paste from the clipboard
566 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
567 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
570 if (const c8 *p = m_operator->getTextFromClipboard()) {
571 if (m_mark_begin == m_mark_end) {
573 core::stringw s = Text.subString(0, m_cursor_pos);
575 s.append(Text.subString(
576 m_cursor_pos, Text.size() - m_cursor_pos));
578 if (!m_max || s.size() <= m_max) {
581 m_cursor_pos += s.size();
586 core::stringw s = Text.subString(0, realmbgn);
588 s.append(Text.subString(realmend, Text.size() - realmend));
590 if (!m_max || s.size() <= m_max) {
593 m_cursor_pos = realmbgn + s.size();
603 bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
605 if (!isEnabled() || Text.empty())
610 if (m_mark_begin != m_mark_end) {
611 // delete marked text
613 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
615 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
617 s = Text.subString(0, realmbgn);
618 s.append(Text.subString(realmend, Text.size() - realmend));
621 m_cursor_pos = realmbgn;
623 // delete text behind cursor
624 if (m_cursor_pos > 0)
625 s = Text.subString(0, m_cursor_pos - 1);
628 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
633 if (m_cursor_pos < 0)
635 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
641 bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
643 if (!isEnabled() || Text.empty())
648 if (m_mark_begin != m_mark_end) {
649 // delete marked text
651 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
653 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
655 s = Text.subString(0, realmbgn);
656 s.append(Text.subString(realmend, Text.size() - realmend));
659 m_cursor_pos = realmbgn;
661 // delete text before cursor
662 s = Text.subString(0, m_cursor_pos);
663 s.append(Text.subString(
664 m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
668 if (m_cursor_pos > (s32)Text.size())
669 m_cursor_pos = (s32)Text.size();
671 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
677 void GUIEditBox::inputChar(wchar_t c)
681 core::stringw s(&c, 1);
685 void GUIEditBox::inputString(const core::stringw &str)
687 if (!isEnabled() || !m_writable)
690 u32 len = str.size();
691 if (Text.size()+len <= m_max || m_max == 0) {
693 if (m_mark_begin != m_mark_end) {
694 // replace marked text
695 s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
696 s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
698 s = Text.subString(0, real_begin);
700 s.append(Text.subString(real_end, Text.size() - real_end));
702 m_cursor_pos = real_begin + len;
705 s = Text.subString(0, m_cursor_pos);
707 s.append(Text.subString(m_cursor_pos,
708 Text.size() - m_cursor_pos));
713 m_blink_start_time = porting::getTimeMs();
714 setTextMarkers(0, 0);
718 sendGuiEvent(EGET_EDITBOX_CHANGED);
719 calculateScrollPos();
722 bool GUIEditBox::processMouse(const SEvent &event)
724 switch (event.MouseInput.Event) {
725 case irr::EMIE_LMOUSE_LEFT_UP:
726 if (Environment->hasFocus(this)) {
727 m_cursor_pos = getCursorPos(
728 event.MouseInput.X, event.MouseInput.Y);
729 if (m_mouse_marking) {
730 setTextMarkers(m_mark_begin, m_cursor_pos);
732 m_mouse_marking = false;
733 calculateScrollPos();
737 case irr::EMIE_MOUSE_MOVED: {
738 if (m_mouse_marking) {
739 m_cursor_pos = getCursorPos(
740 event.MouseInput.X, event.MouseInput.Y);
741 setTextMarkers(m_mark_begin, m_cursor_pos);
742 calculateScrollPos();
746 case EMIE_LMOUSE_PRESSED_DOWN:
748 if (!Environment->hasFocus(this)) {
749 m_blink_start_time = porting::getTimeMs();
750 m_mouse_marking = true;
751 m_cursor_pos = getCursorPos(
752 event.MouseInput.X, event.MouseInput.Y);
753 setTextMarkers(m_cursor_pos, m_cursor_pos);
754 calculateScrollPos();
757 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
758 event.MouseInput.X, event.MouseInput.Y))) {
762 m_cursor_pos = getCursorPos(
763 event.MouseInput.X, event.MouseInput.Y);
765 s32 newMarkBegin = m_mark_begin;
766 if (!m_mouse_marking)
767 newMarkBegin = m_cursor_pos;
769 m_mouse_marking = true;
770 setTextMarkers(newMarkBegin, m_cursor_pos);
771 calculateScrollPos();
775 case EMIE_MOUSE_WHEEL:
776 if (m_vscrollbar && m_vscrollbar->isVisible()) {
777 s32 pos = m_vscrollbar->getPos();
778 s32 step = m_vscrollbar->getSmallStep();
779 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
790 s32 GUIEditBox::getLineFromPos(s32 pos)
792 if (!m_word_wrap && !m_multiline)
796 while (i < (s32)m_broken_text_positions.size()) {
797 if (m_broken_text_positions[i] > pos)
801 return (s32)m_broken_text_positions.size() - 1;
804 void GUIEditBox::updateVScrollBar()
810 // OnScrollBarChanged(...)
811 if (m_vscrollbar->getPos() != m_vscroll_pos) {
812 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
813 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
814 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
816 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
817 if (scrollymax != m_vscrollbar->getMax()) {
818 // manage a newline or a deleted line
819 m_vscrollbar->setMax(scrollymax);
820 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
821 calculateScrollPos();
823 // manage a newline or a deleted line
824 m_vscroll_pos = m_vscrollbar->getPos();
828 // check if a vertical scrollbar is needed ?
829 if (getTextDimension().Height > (u32)m_frame_rect.getHeight()) {
830 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
832 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
833 if (scrollymax != m_vscrollbar->getMax()) {
834 m_vscrollbar->setMax(scrollymax);
835 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
838 if (!m_vscrollbar->isVisible()) {
839 m_vscrollbar->setVisible(true);
842 if (m_vscrollbar->isVisible()) {
843 m_vscrollbar->setVisible(false);
845 m_vscrollbar->setPos(0);
846 m_vscrollbar->setMax(1);
847 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
852 void GUIEditBox::deserializeAttributes(
853 io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0)
855 IGUIEditBox::deserializeAttributes(in, options);
857 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
858 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
859 setMax(in->getAttributeAsInt("MaxChars"));
860 setWordWrap(in->getAttributeAsBool("WordWrap"));
861 setMultiLine(in->getAttributeAsBool("MultiLine"));
862 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
863 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
866 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
868 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
870 setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
871 "HTextAlign", GUIAlignmentNames),
872 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
873 "VTextAlign", GUIAlignmentNames));
875 setWritable(in->getAttributeAsBool("Writable"));
876 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
879 //! Writes attributes of the element.
880 void GUIEditBox::serializeAttributes(
881 io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const
883 // IGUIEditBox::serializeAttributes(out,options);
885 out->addBool("OverrideColorEnabled", m_override_color_enabled);
886 out->addColor("OverrideColor", m_override_color);
887 // out->addFont("OverrideFont",m_override_font);
888 out->addInt("MaxChars", m_max);
889 out->addBool("WordWrap", m_word_wrap);
890 out->addBool("MultiLine", m_multiline);
891 out->addBool("AutoScroll", m_autoscroll);
892 out->addBool("PasswordBox", m_passwordbox);
893 core::stringw ch = L" ";
894 ch[0] = m_passwordchar;
895 out->addString("PasswordChar", ch.c_str());
896 out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
897 out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
898 out->addBool("Writable", m_writable);
900 IGUIEditBox::serializeAttributes(out, options);