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"
28 #include "util/string.h"
30 GUIEditBox::~GUIEditBox()
33 m_override_font->drop();
42 void GUIEditBox::setOverrideFont(IGUIFont *font)
44 if (m_override_font == font)
48 m_override_font->drop();
50 m_override_font = font;
53 m_override_font->grab();
58 //! Get the font which is used right now for drawing
59 IGUIFont *GUIEditBox::getActiveFont() const
62 return m_override_font;
63 IGUISkin *skin = Environment->getSkin();
65 return skin->getFont();
69 //! Sets another color for the text.
70 void GUIEditBox::setOverrideColor(video::SColor color)
72 m_override_color = color;
73 m_override_color_enabled = true;
76 video::SColor GUIEditBox::getOverrideColor() const
78 return m_override_color;
81 //! Sets if the text should use the overide color or the color in the gui skin.
82 void GUIEditBox::enableOverrideColor(bool enable)
84 m_override_color_enabled = enable;
87 //! Enables or disables word wrap
88 void GUIEditBox::setWordWrap(bool enable)
94 //! Enables or disables newlines.
95 void GUIEditBox::setMultiLine(bool enable)
100 //! Enables or disables automatic scrolling with cursor position
101 //! \param enable: If set to true, the text will move around with the cursor position
102 void GUIEditBox::setAutoScroll(bool enable)
104 m_autoscroll = enable;
107 void GUIEditBox::setPasswordBox(bool password_box, wchar_t password_char)
109 m_passwordbox = password_box;
111 m_passwordchar = password_char;
114 m_broken_text.clear();
118 //! Sets text justification
119 void GUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
121 m_halign = horizontal;
125 //! Sets the new caption of this element.
126 void GUIEditBox::setText(const wchar_t *text)
129 if (u32(m_cursor_pos) > Text.size())
130 m_cursor_pos = Text.size();
135 //! Sets the maximum amount of characters which may be entered in the box.
136 //! \param max: Maximum amount of characters. If 0, the character amount is
138 void GUIEditBox::setMax(u32 max)
142 if (Text.size() > m_max && m_max != 0)
143 Text = Text.subString(0, m_max);
146 //! Gets the area of the text in the edit box
147 //! \return Returns the size in pixels of the text
148 core::dimension2du GUIEditBox::getTextDimension()
153 ret = m_current_text_rect;
155 for (u32 i = 1; i < m_broken_text.size(); ++i) {
157 ret.addInternalPoint(m_current_text_rect.UpperLeftCorner);
158 ret.addInternalPoint(m_current_text_rect.LowerRightCorner);
161 return core::dimension2du(ret.getSize());
164 //! Turns the border on or off
165 void GUIEditBox::setDrawBorder(bool border)
170 void GUIEditBox::setWritable(bool can_write_text)
172 m_writable = can_write_text;
176 void GUIEditBox::setTextMarkers(s32 begin, s32 end)
178 if (begin != m_mark_begin || end != m_mark_end) {
179 m_mark_begin = begin;
181 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
185 //! send some gui event to parent
186 void GUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
190 e.EventType = EET_GUI_EVENT;
191 e.GUIEvent.Caller = this;
192 e.GUIEvent.Element = 0;
193 e.GUIEvent.EventType = type;
199 //! called if an event happened.
200 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 case EET_STRING_INPUT_EVENT:
221 inputString(*event.StringInput.Str);
228 return IGUIElement::OnEvent(event);
231 bool GUIEditBox::processKey(const SEvent &event)
233 if (!event.KeyInput.PressedDown)
236 bool text_changed = false;
237 s32 new_mark_begin = m_mark_begin;
238 s32 new_mark_end = m_mark_end;
240 // control shortcut handling
241 if (event.KeyInput.Control) {
243 // german backlash '\' entered with control + '?'
244 if (event.KeyInput.Char == '\\') {
245 inputChar(event.KeyInput.Char);
249 switch (event.KeyInput.Key) {
253 new_mark_end = Text.size();
256 onKeyControlC(event);
259 text_changed = onKeyControlX(event, new_mark_begin, new_mark_end);
262 text_changed = onKeyControlV(event, new_mark_begin, new_mark_end);
265 // move/highlight to start of text
266 if (event.KeyInput.Shift) {
267 new_mark_end = m_cursor_pos;
277 // move/highlight to end of text
278 if (event.KeyInput.Shift) {
279 new_mark_begin = m_cursor_pos;
280 new_mark_end = Text.size();
283 m_cursor_pos = Text.size();
292 switch (event.KeyInput.Key) {
295 if (m_word_wrap || m_multiline) {
296 p = getLineFromPos(m_cursor_pos);
297 p = m_broken_text_positions[p] +
298 (s32)m_broken_text[p].size();
299 if (p > 0 && (Text[p - 1] == L'\r' ||
300 Text[p - 1] == L'\n'))
304 if (event.KeyInput.Shift) {
305 if (m_mark_begin == m_mark_end)
306 new_mark_begin = m_cursor_pos;
314 m_blink_start_time = porting::getTimeMs();
319 if (m_word_wrap || m_multiline) {
320 p = getLineFromPos(m_cursor_pos);
321 p = m_broken_text_positions[p];
324 if (event.KeyInput.Shift) {
325 if (m_mark_begin == m_mark_end)
326 new_mark_begin = m_cursor_pos;
333 m_blink_start_time = porting::getTimeMs();
339 calculateScrollPos();
340 sendGuiEvent(EGET_EDITBOX_ENTER);
344 if (event.KeyInput.Shift) {
345 if (m_cursor_pos > 0) {
346 if (m_mark_begin == m_mark_end)
347 new_mark_begin = m_cursor_pos;
349 new_mark_end = m_cursor_pos - 1;
356 if (m_cursor_pos > 0)
358 m_blink_start_time = porting::getTimeMs();
361 if (event.KeyInput.Shift) {
362 if (Text.size() > (u32)m_cursor_pos) {
363 if (m_mark_begin == m_mark_end)
364 new_mark_begin = m_cursor_pos;
366 new_mark_end = m_cursor_pos + 1;
373 if (Text.size() > (u32)m_cursor_pos)
375 m_blink_start_time = porting::getTimeMs();
378 if (!onKeyUp(event, new_mark_begin, new_mark_end)) {
383 if (!onKeyDown(event, new_mark_begin, new_mark_end)) {
388 text_changed = onKeyBack(event, new_mark_begin, new_mark_end);
392 text_changed = onKeyDelete(event, new_mark_begin, new_mark_end);
426 inputChar(event.KeyInput.Char);
431 // Set new text markers
432 setTextMarkers(new_mark_begin, new_mark_end);
434 // break the text if it has changed
437 sendGuiEvent(EGET_EDITBOX_CHANGED);
440 calculateScrollPos();
445 bool GUIEditBox::onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end)
448 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
449 s32 lineNo = getLineFromPos(m_cursor_pos);
450 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
451 (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
453 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
454 if ((s32)m_broken_text[lineNo - 1].size() < cp) {
455 m_cursor_pos = m_broken_text_positions[lineNo - 1] +
456 core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
459 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
462 if (event.KeyInput.Shift) {
464 mark_end = m_cursor_pos;
477 bool GUIEditBox::onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end)
480 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
481 s32 lineNo = getLineFromPos(m_cursor_pos);
482 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
483 (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
484 if (lineNo < (s32)m_broken_text.size() - 1) {
485 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
486 if ((s32)m_broken_text[lineNo + 1].size() < cp) {
487 m_cursor_pos = m_broken_text_positions[lineNo + 1] +
488 core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
491 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
494 if (event.KeyInput.Shift) {
496 mark_end = m_cursor_pos;
509 void GUIEditBox::onKeyControlC(const SEvent &event)
512 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
515 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
516 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
518 std::string s = stringw_to_utf8(Text.subString(realmbgn, realmend - realmbgn));
519 m_operator->copyToClipboard(s.c_str());
522 bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end)
524 // First copy to clipboard
525 onKeyControlC(event);
530 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
533 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
534 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
536 // Now remove from box if enabled
540 s = Text.subString(0, realmbgn);
541 s.append(Text.subString(realmend, Text.size() - realmend));
544 m_cursor_pos = realmbgn;
553 bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end)
555 if (!isEnabled() || !m_writable)
558 // paste from the clipboard
562 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
563 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
566 if (const c8 *p = m_operator->getTextFromClipboard()) {
567 core::stringw inserted_text = utf8_to_stringw(p);
568 if (m_mark_begin == m_mark_end) {
570 core::stringw s = Text.subString(0, m_cursor_pos);
571 s.append(inserted_text);
572 s.append(Text.subString(
573 m_cursor_pos, Text.size() - m_cursor_pos));
575 if (!m_max || s.size() <= m_max) {
577 m_cursor_pos += inserted_text.size();
582 core::stringw s = Text.subString(0, realmbgn);
583 s.append(inserted_text);
584 s.append(Text.subString(realmend, Text.size() - realmend));
586 if (!m_max || s.size() <= m_max) {
588 m_cursor_pos = realmbgn + inserted_text.size();
598 bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
600 if (!isEnabled() || Text.empty() || !m_writable)
605 if (m_mark_begin != m_mark_end) {
606 // delete marked text
608 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
610 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
612 s = Text.subString(0, realmbgn);
613 s.append(Text.subString(realmend, Text.size() - realmend));
616 m_cursor_pos = realmbgn;
618 // delete text behind cursor
619 if (m_cursor_pos > 0)
620 s = Text.subString(0, m_cursor_pos - 1);
623 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
628 if (m_cursor_pos < 0)
630 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
636 bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
638 if (!isEnabled() || Text.empty() || !m_writable)
643 if (m_mark_begin != m_mark_end) {
644 // delete marked text
646 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
648 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
650 s = Text.subString(0, realmbgn);
651 s.append(Text.subString(realmend, Text.size() - realmend));
654 m_cursor_pos = realmbgn;
656 // delete text before cursor
657 s = Text.subString(0, m_cursor_pos);
658 s.append(Text.subString(
659 m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
663 if (m_cursor_pos > (s32)Text.size())
664 m_cursor_pos = (s32)Text.size();
666 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
672 void GUIEditBox::inputChar(wchar_t c)
676 core::stringw s(&c, 1);
680 void GUIEditBox::inputString(const core::stringw &str)
682 if (!isEnabled() || !m_writable)
685 u32 len = str.size();
686 if (Text.size()+len <= m_max || m_max == 0) {
688 if (m_mark_begin != m_mark_end) {
689 // replace marked text
690 s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
691 s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
693 s = Text.subString(0, real_begin);
695 s.append(Text.subString(real_end, Text.size() - real_end));
697 m_cursor_pos = real_begin + len;
700 s = Text.subString(0, m_cursor_pos);
702 s.append(Text.subString(m_cursor_pos,
703 Text.size() - m_cursor_pos));
708 m_blink_start_time = porting::getTimeMs();
709 setTextMarkers(0, 0);
713 sendGuiEvent(EGET_EDITBOX_CHANGED);
714 calculateScrollPos();
717 bool GUIEditBox::processMouse(const SEvent &event)
719 switch (event.MouseInput.Event) {
720 case irr::EMIE_LMOUSE_LEFT_UP:
721 if (Environment->hasFocus(this)) {
722 m_cursor_pos = getCursorPos(
723 event.MouseInput.X, event.MouseInput.Y);
724 if (m_mouse_marking) {
725 setTextMarkers(m_mark_begin, m_cursor_pos);
727 m_mouse_marking = false;
728 calculateScrollPos();
732 case irr::EMIE_MOUSE_MOVED: {
733 if (m_mouse_marking) {
734 m_cursor_pos = getCursorPos(
735 event.MouseInput.X, event.MouseInput.Y);
736 setTextMarkers(m_mark_begin, m_cursor_pos);
737 calculateScrollPos();
741 case EMIE_LMOUSE_PRESSED_DOWN:
743 if (!Environment->hasFocus(this)) {
744 m_blink_start_time = porting::getTimeMs();
745 m_mouse_marking = true;
746 m_cursor_pos = getCursorPos(
747 event.MouseInput.X, event.MouseInput.Y);
748 setTextMarkers(m_cursor_pos, m_cursor_pos);
749 calculateScrollPos();
752 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
753 event.MouseInput.X, event.MouseInput.Y))) {
757 m_cursor_pos = getCursorPos(
758 event.MouseInput.X, event.MouseInput.Y);
760 s32 newMarkBegin = m_mark_begin;
761 if (!m_mouse_marking)
762 newMarkBegin = m_cursor_pos;
764 m_mouse_marking = true;
765 setTextMarkers(newMarkBegin, m_cursor_pos);
766 calculateScrollPos();
770 case EMIE_MOUSE_WHEEL:
771 if (m_vscrollbar && m_vscrollbar->isVisible()) {
772 s32 pos = m_vscrollbar->getPos();
773 s32 step = m_vscrollbar->getSmallStep();
774 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
785 s32 GUIEditBox::getLineFromPos(s32 pos)
787 if (!m_word_wrap && !m_multiline)
791 while (i < (s32)m_broken_text_positions.size()) {
792 if (m_broken_text_positions[i] > pos)
796 return (s32)m_broken_text_positions.size() - 1;
799 void GUIEditBox::updateVScrollBar()
805 // OnScrollBarChanged(...)
806 if (m_vscrollbar->getPos() != m_vscroll_pos) {
807 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
808 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
809 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
811 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
812 if (scrollymax != m_vscrollbar->getMax()) {
813 // manage a newline or a deleted line
814 m_vscrollbar->setMax(scrollymax);
815 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
816 calculateScrollPos();
818 // manage a newline or a deleted line
819 m_vscroll_pos = m_vscrollbar->getPos();
823 // check if a vertical scrollbar is needed ?
824 if (getTextDimension().Height > (u32)m_frame_rect.getHeight()) {
825 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
827 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
828 if (scrollymax != m_vscrollbar->getMax()) {
829 m_vscrollbar->setMax(scrollymax);
830 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
833 if (!m_vscrollbar->isVisible()) {
834 m_vscrollbar->setVisible(true);
837 if (m_vscrollbar->isVisible()) {
838 m_vscrollbar->setVisible(false);
840 m_vscrollbar->setPos(0);
841 m_vscrollbar->setMax(1);
842 m_vscrollbar->setPageSize(s32(getTextDimension().Height));