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)
235 if (!event.KeyInput.PressedDown)
238 bool text_changed = false;
239 s32 new_mark_begin = m_mark_begin;
240 s32 new_mark_end = m_mark_end;
242 // control shortcut handling
243 if (event.KeyInput.Control) {
245 // german backlash '\' entered with control + '?'
246 if (event.KeyInput.Char == '\\') {
247 inputChar(event.KeyInput.Char);
251 switch (event.KeyInput.Key) {
255 new_mark_end = Text.size();
258 onKeyControlC(event);
261 text_changed = onKeyControlX(event, new_mark_begin, new_mark_end);
264 text_changed = onKeyControlV(event, new_mark_begin, new_mark_end);
267 // move/highlight to start of text
268 if (event.KeyInput.Shift) {
269 new_mark_end = m_cursor_pos;
279 // move/highlight to end of text
280 if (event.KeyInput.Shift) {
281 new_mark_begin = m_cursor_pos;
282 new_mark_end = Text.size();
285 m_cursor_pos = Text.size();
294 switch (event.KeyInput.Key) {
297 if (m_word_wrap || m_multiline) {
298 p = getLineFromPos(m_cursor_pos);
299 p = m_broken_text_positions[p] +
300 (s32)m_broken_text[p].size();
301 if (p > 0 && (Text[p - 1] == L'\r' ||
302 Text[p - 1] == L'\n'))
306 if (event.KeyInput.Shift) {
307 if (m_mark_begin == m_mark_end)
308 new_mark_begin = m_cursor_pos;
316 m_blink_start_time = porting::getTimeMs();
321 if (m_word_wrap || m_multiline) {
322 p = getLineFromPos(m_cursor_pos);
323 p = m_broken_text_positions[p];
326 if (event.KeyInput.Shift) {
327 if (m_mark_begin == m_mark_end)
328 new_mark_begin = m_cursor_pos;
335 m_blink_start_time = porting::getTimeMs();
341 calculateScrollPos();
342 sendGuiEvent(EGET_EDITBOX_ENTER);
346 if (event.KeyInput.Shift) {
347 if (m_cursor_pos > 0) {
348 if (m_mark_begin == m_mark_end)
349 new_mark_begin = m_cursor_pos;
351 new_mark_end = m_cursor_pos - 1;
358 if (m_cursor_pos > 0)
360 m_blink_start_time = porting::getTimeMs();
363 if (event.KeyInput.Shift) {
364 if (Text.size() > (u32)m_cursor_pos) {
365 if (m_mark_begin == m_mark_end)
366 new_mark_begin = m_cursor_pos;
368 new_mark_end = m_cursor_pos + 1;
375 if (Text.size() > (u32)m_cursor_pos)
377 m_blink_start_time = porting::getTimeMs();
380 if (!onKeyUp(event, new_mark_begin, new_mark_end)) {
385 if (!onKeyDown(event, new_mark_begin, new_mark_end)) {
390 text_changed = onKeyBack(event, new_mark_begin, new_mark_end);
394 text_changed = onKeyDelete(event, new_mark_begin, new_mark_end);
428 inputChar(event.KeyInput.Char);
433 // Set new text markers
434 setTextMarkers(new_mark_begin, new_mark_end);
436 // break the text if it has changed
439 sendGuiEvent(EGET_EDITBOX_CHANGED);
442 calculateScrollPos();
447 bool GUIEditBox::onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end)
450 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
451 s32 lineNo = getLineFromPos(m_cursor_pos);
452 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
453 (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
455 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
456 if ((s32)m_broken_text[lineNo - 1].size() < cp) {
457 m_cursor_pos = m_broken_text_positions[lineNo - 1] +
458 core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
461 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
464 if (event.KeyInput.Shift) {
466 mark_end = m_cursor_pos;
479 bool GUIEditBox::onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end)
482 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
483 s32 lineNo = getLineFromPos(m_cursor_pos);
484 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
485 (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
486 if (lineNo < (s32)m_broken_text.size() - 1) {
487 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
488 if ((s32)m_broken_text[lineNo + 1].size() < cp) {
489 m_cursor_pos = m_broken_text_positions[lineNo + 1] +
490 core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
493 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
496 if (event.KeyInput.Shift) {
498 mark_end = m_cursor_pos;
511 void GUIEditBox::onKeyControlC(const SEvent &event)
514 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
517 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
518 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
521 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
522 m_operator->copyToClipboard(s.c_str());
525 bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end)
527 // First copy to clipboard
528 onKeyControlC(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;
539 // Now remove from box if enabled
543 s = Text.subString(0, realmbgn);
544 s.append(Text.subString(realmend, Text.size() - realmend));
547 m_cursor_pos = realmbgn;
556 bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end)
558 if (!isEnabled() || !m_writable)
561 // paste from the clipboard
565 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
566 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
569 if (const c8 *p = m_operator->getTextFromClipboard()) {
570 if (m_mark_begin == m_mark_end) {
572 core::stringw s = Text.subString(0, m_cursor_pos);
574 s.append(Text.subString(
575 m_cursor_pos, Text.size() - m_cursor_pos));
577 if (!m_max || s.size() <= m_max) {
580 m_cursor_pos += s.size();
585 core::stringw s = Text.subString(0, realmbgn);
587 s.append(Text.subString(realmend, Text.size() - realmend));
589 if (!m_max || s.size() <= m_max) {
592 m_cursor_pos = realmbgn + s.size();
602 bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
604 if (!isEnabled() || Text.empty() || !m_writable)
609 if (m_mark_begin != m_mark_end) {
610 // delete marked text
612 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
614 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
616 s = Text.subString(0, realmbgn);
617 s.append(Text.subString(realmend, Text.size() - realmend));
620 m_cursor_pos = realmbgn;
622 // delete text behind cursor
623 if (m_cursor_pos > 0)
624 s = Text.subString(0, m_cursor_pos - 1);
627 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
632 if (m_cursor_pos < 0)
634 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
640 bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
642 if (!isEnabled() || Text.empty() || !m_writable)
647 if (m_mark_begin != m_mark_end) {
648 // delete marked text
650 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
652 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
654 s = Text.subString(0, realmbgn);
655 s.append(Text.subString(realmend, Text.size() - realmend));
658 m_cursor_pos = realmbgn;
660 // delete text before cursor
661 s = Text.subString(0, m_cursor_pos);
662 s.append(Text.subString(
663 m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
667 if (m_cursor_pos > (s32)Text.size())
668 m_cursor_pos = (s32)Text.size();
670 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
676 void GUIEditBox::inputChar(wchar_t c)
680 core::stringw s(&c, 1);
684 void GUIEditBox::inputString(const core::stringw &str)
686 if (!isEnabled() || !m_writable)
689 u32 len = str.size();
690 if (Text.size()+len <= m_max || m_max == 0) {
692 if (m_mark_begin != m_mark_end) {
693 // replace marked text
694 s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
695 s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
697 s = Text.subString(0, real_begin);
699 s.append(Text.subString(real_end, Text.size() - real_end));
701 m_cursor_pos = real_begin + len;
704 s = Text.subString(0, m_cursor_pos);
706 s.append(Text.subString(m_cursor_pos,
707 Text.size() - m_cursor_pos));
712 m_blink_start_time = porting::getTimeMs();
713 setTextMarkers(0, 0);
717 sendGuiEvent(EGET_EDITBOX_CHANGED);
718 calculateScrollPos();
721 bool GUIEditBox::processMouse(const SEvent &event)
723 switch (event.MouseInput.Event) {
724 case irr::EMIE_LMOUSE_LEFT_UP:
725 if (Environment->hasFocus(this)) {
726 m_cursor_pos = getCursorPos(
727 event.MouseInput.X, event.MouseInput.Y);
728 if (m_mouse_marking) {
729 setTextMarkers(m_mark_begin, m_cursor_pos);
731 m_mouse_marking = false;
732 calculateScrollPos();
736 case irr::EMIE_MOUSE_MOVED: {
737 if (m_mouse_marking) {
738 m_cursor_pos = getCursorPos(
739 event.MouseInput.X, event.MouseInput.Y);
740 setTextMarkers(m_mark_begin, m_cursor_pos);
741 calculateScrollPos();
745 case EMIE_LMOUSE_PRESSED_DOWN:
747 if (!Environment->hasFocus(this)) {
748 m_blink_start_time = porting::getTimeMs();
749 m_mouse_marking = true;
750 m_cursor_pos = getCursorPos(
751 event.MouseInput.X, event.MouseInput.Y);
752 setTextMarkers(m_cursor_pos, m_cursor_pos);
753 calculateScrollPos();
756 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
757 event.MouseInput.X, event.MouseInput.Y))) {
761 m_cursor_pos = getCursorPos(
762 event.MouseInput.X, event.MouseInput.Y);
764 s32 newMarkBegin = m_mark_begin;
765 if (!m_mouse_marking)
766 newMarkBegin = m_cursor_pos;
768 m_mouse_marking = true;
769 setTextMarkers(newMarkBegin, m_cursor_pos);
770 calculateScrollPos();
774 case EMIE_MOUSE_WHEEL:
775 if (m_vscrollbar && m_vscrollbar->isVisible()) {
776 s32 pos = m_vscrollbar->getPos();
777 s32 step = m_vscrollbar->getSmallStep();
778 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
789 s32 GUIEditBox::getLineFromPos(s32 pos)
791 if (!m_word_wrap && !m_multiline)
795 while (i < (s32)m_broken_text_positions.size()) {
796 if (m_broken_text_positions[i] > pos)
800 return (s32)m_broken_text_positions.size() - 1;
803 void GUIEditBox::updateVScrollBar()
809 // OnScrollBarChanged(...)
810 if (m_vscrollbar->getPos() != m_vscroll_pos) {
811 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
812 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
813 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
815 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
816 if (scrollymax != m_vscrollbar->getMax()) {
817 // manage a newline or a deleted line
818 m_vscrollbar->setMax(scrollymax);
819 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
820 calculateScrollPos();
822 // manage a newline or a deleted line
823 m_vscroll_pos = m_vscrollbar->getPos();
827 // check if a vertical scrollbar is needed ?
828 if (getTextDimension().Height > (u32)m_frame_rect.getHeight()) {
829 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
831 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
832 if (scrollymax != m_vscrollbar->getMax()) {
833 m_vscrollbar->setMax(scrollymax);
834 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
837 if (!m_vscrollbar->isVisible()) {
838 m_vscrollbar->setVisible(true);
841 if (m_vscrollbar->isVisible()) {
842 m_vscrollbar->setVisible(false);
844 m_vscrollbar->setPos(0);
845 m_vscrollbar->setMax(1);
846 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
851 void GUIEditBox::deserializeAttributes(
852 io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0)
854 IGUIEditBox::deserializeAttributes(in, options);
856 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
857 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
858 setMax(in->getAttributeAsInt("MaxChars"));
859 setWordWrap(in->getAttributeAsBool("WordWrap"));
860 setMultiLine(in->getAttributeAsBool("MultiLine"));
861 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
862 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
865 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
867 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
869 setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
870 "HTextAlign", GUIAlignmentNames),
871 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
872 "VTextAlign", GUIAlignmentNames));
874 setWritable(in->getAttributeAsBool("Writable"));
875 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
878 //! Writes attributes of the element.
879 void GUIEditBox::serializeAttributes(
880 io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const
882 // IGUIEditBox::serializeAttributes(out,options);
884 out->addBool("OverrideColorEnabled", m_override_color_enabled);
885 out->addColor("OverrideColor", m_override_color);
886 // out->addFont("OverrideFont",m_override_font);
887 out->addInt("MaxChars", m_max);
888 out->addBool("WordWrap", m_word_wrap);
889 out->addBool("MultiLine", m_multiline);
890 out->addBool("AutoScroll", m_autoscroll);
891 out->addBool("PasswordBox", m_passwordbox);
892 core::stringw ch = L" ";
893 ch[0] = m_passwordchar;
894 out->addString("PasswordChar", ch.c_str());
895 out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
896 out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
897 out->addBool("Writable", m_writable);
899 IGUIEditBox::serializeAttributes(out, options);