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)
204 switch (event.EventType) {
206 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
207 if (event.GUIEvent.Caller == this) {
208 m_mouse_marking = false;
209 setTextMarkers(0, 0);
213 case EET_KEY_INPUT_EVENT:
214 if (processKey(event))
217 case EET_MOUSE_INPUT_EVENT:
218 if (processMouse(event))
221 #if (IRRLICHT_VERSION_MT_REVISION >= 2)
222 case EET_STRING_INPUT_EVENT:
223 inputString(*event.StringInput.Str);
231 return IGUIElement::OnEvent(event);
234 bool GUIEditBox::processKey(const SEvent &event)
236 if (!event.KeyInput.PressedDown)
239 bool text_changed = false;
240 s32 new_mark_begin = m_mark_begin;
241 s32 new_mark_end = m_mark_end;
243 // control shortcut handling
244 if (event.KeyInput.Control) {
246 // german backlash '\' entered with control + '?'
247 if (event.KeyInput.Char == '\\') {
248 inputChar(event.KeyInput.Char);
252 switch (event.KeyInput.Key) {
256 new_mark_end = Text.size();
259 onKeyControlC(event);
262 text_changed = onKeyControlX(event, new_mark_begin, new_mark_end);
265 text_changed = onKeyControlV(event, new_mark_begin, new_mark_end);
268 // move/highlight to start of text
269 if (event.KeyInput.Shift) {
270 new_mark_end = m_cursor_pos;
280 // move/highlight to end of text
281 if (event.KeyInput.Shift) {
282 new_mark_begin = m_cursor_pos;
283 new_mark_end = Text.size();
286 m_cursor_pos = Text.size();
295 switch (event.KeyInput.Key) {
298 if (m_word_wrap || m_multiline) {
299 p = getLineFromPos(m_cursor_pos);
300 p = m_broken_text_positions[p] +
301 (s32)m_broken_text[p].size();
302 if (p > 0 && (Text[p - 1] == L'\r' ||
303 Text[p - 1] == L'\n'))
307 if (event.KeyInput.Shift) {
308 if (m_mark_begin == m_mark_end)
309 new_mark_begin = m_cursor_pos;
317 m_blink_start_time = porting::getTimeMs();
322 if (m_word_wrap || m_multiline) {
323 p = getLineFromPos(m_cursor_pos);
324 p = m_broken_text_positions[p];
327 if (event.KeyInput.Shift) {
328 if (m_mark_begin == m_mark_end)
329 new_mark_begin = m_cursor_pos;
336 m_blink_start_time = porting::getTimeMs();
342 calculateScrollPos();
343 sendGuiEvent(EGET_EDITBOX_ENTER);
347 if (event.KeyInput.Shift) {
348 if (m_cursor_pos > 0) {
349 if (m_mark_begin == m_mark_end)
350 new_mark_begin = m_cursor_pos;
352 new_mark_end = m_cursor_pos - 1;
359 if (m_cursor_pos > 0)
361 m_blink_start_time = porting::getTimeMs();
364 if (event.KeyInput.Shift) {
365 if (Text.size() > (u32)m_cursor_pos) {
366 if (m_mark_begin == m_mark_end)
367 new_mark_begin = m_cursor_pos;
369 new_mark_end = m_cursor_pos + 1;
376 if (Text.size() > (u32)m_cursor_pos)
378 m_blink_start_time = porting::getTimeMs();
381 if (!onKeyUp(event, new_mark_begin, new_mark_end)) {
386 if (!onKeyDown(event, new_mark_begin, new_mark_end)) {
391 text_changed = onKeyBack(event, new_mark_begin, new_mark_end);
395 text_changed = onKeyDelete(event, new_mark_begin, new_mark_end);
429 inputChar(event.KeyInput.Char);
434 // Set new text markers
435 setTextMarkers(new_mark_begin, new_mark_end);
437 // break the text if it has changed
440 sendGuiEvent(EGET_EDITBOX_CHANGED);
443 calculateScrollPos();
448 bool GUIEditBox::onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end)
451 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
452 s32 lineNo = getLineFromPos(m_cursor_pos);
453 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
454 (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
456 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
457 if ((s32)m_broken_text[lineNo - 1].size() < cp) {
458 m_cursor_pos = m_broken_text_positions[lineNo - 1] +
459 core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
462 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
465 if (event.KeyInput.Shift) {
467 mark_end = m_cursor_pos;
480 bool GUIEditBox::onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end)
483 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
484 s32 lineNo = getLineFromPos(m_cursor_pos);
485 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
486 (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
487 if (lineNo < (s32)m_broken_text.size() - 1) {
488 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
489 if ((s32)m_broken_text[lineNo + 1].size() < cp) {
490 m_cursor_pos = m_broken_text_positions[lineNo + 1] +
491 core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
494 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
497 if (event.KeyInput.Shift) {
499 mark_end = m_cursor_pos;
512 void GUIEditBox::onKeyControlC(const SEvent &event)
515 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
518 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
519 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
521 std::string s = stringw_to_utf8(Text.subString(realmbgn, realmend - realmbgn));
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 core::stringw inserted_text = utf8_to_stringw(p);
571 if (m_mark_begin == m_mark_end) {
573 core::stringw s = Text.subString(0, m_cursor_pos);
574 s.append(inserted_text);
575 s.append(Text.subString(
576 m_cursor_pos, Text.size() - m_cursor_pos));
578 if (!m_max || s.size() <= m_max) {
580 m_cursor_pos += inserted_text.size();
585 core::stringw s = Text.subString(0, realmbgn);
586 s.append(inserted_text);
587 s.append(Text.subString(realmend, Text.size() - realmend));
589 if (!m_max || s.size() <= m_max) {
591 m_cursor_pos = realmbgn + inserted_text.size();
601 bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
603 if (!isEnabled() || Text.empty() || !m_writable)
608 if (m_mark_begin != m_mark_end) {
609 // delete marked text
611 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
613 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
615 s = Text.subString(0, realmbgn);
616 s.append(Text.subString(realmend, Text.size() - realmend));
619 m_cursor_pos = realmbgn;
621 // delete text behind cursor
622 if (m_cursor_pos > 0)
623 s = Text.subString(0, m_cursor_pos - 1);
626 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
631 if (m_cursor_pos < 0)
633 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
639 bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
641 if (!isEnabled() || Text.empty() || !m_writable)
646 if (m_mark_begin != m_mark_end) {
647 // delete marked text
649 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
651 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
653 s = Text.subString(0, realmbgn);
654 s.append(Text.subString(realmend, Text.size() - realmend));
657 m_cursor_pos = realmbgn;
659 // delete text before cursor
660 s = Text.subString(0, m_cursor_pos);
661 s.append(Text.subString(
662 m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
666 if (m_cursor_pos > (s32)Text.size())
667 m_cursor_pos = (s32)Text.size();
669 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
675 void GUIEditBox::inputChar(wchar_t c)
679 core::stringw s(&c, 1);
683 void GUIEditBox::inputString(const core::stringw &str)
685 if (!isEnabled() || !m_writable)
688 u32 len = str.size();
689 if (Text.size()+len <= m_max || m_max == 0) {
691 if (m_mark_begin != m_mark_end) {
692 // replace marked text
693 s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
694 s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
696 s = Text.subString(0, real_begin);
698 s.append(Text.subString(real_end, Text.size() - real_end));
700 m_cursor_pos = real_begin + len;
703 s = Text.subString(0, m_cursor_pos);
705 s.append(Text.subString(m_cursor_pos,
706 Text.size() - m_cursor_pos));
711 m_blink_start_time = porting::getTimeMs();
712 setTextMarkers(0, 0);
716 sendGuiEvent(EGET_EDITBOX_CHANGED);
717 calculateScrollPos();
720 bool GUIEditBox::processMouse(const SEvent &event)
722 switch (event.MouseInput.Event) {
723 case irr::EMIE_LMOUSE_LEFT_UP:
724 if (Environment->hasFocus(this)) {
725 m_cursor_pos = getCursorPos(
726 event.MouseInput.X, event.MouseInput.Y);
727 if (m_mouse_marking) {
728 setTextMarkers(m_mark_begin, m_cursor_pos);
730 m_mouse_marking = false;
731 calculateScrollPos();
735 case irr::EMIE_MOUSE_MOVED: {
736 if (m_mouse_marking) {
737 m_cursor_pos = getCursorPos(
738 event.MouseInput.X, event.MouseInput.Y);
739 setTextMarkers(m_mark_begin, m_cursor_pos);
740 calculateScrollPos();
744 case EMIE_LMOUSE_PRESSED_DOWN:
746 if (!Environment->hasFocus(this)) {
747 m_blink_start_time = porting::getTimeMs();
748 m_mouse_marking = true;
749 m_cursor_pos = getCursorPos(
750 event.MouseInput.X, event.MouseInput.Y);
751 setTextMarkers(m_cursor_pos, m_cursor_pos);
752 calculateScrollPos();
755 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
756 event.MouseInput.X, event.MouseInput.Y))) {
760 m_cursor_pos = getCursorPos(
761 event.MouseInput.X, event.MouseInput.Y);
763 s32 newMarkBegin = m_mark_begin;
764 if (!m_mouse_marking)
765 newMarkBegin = m_cursor_pos;
767 m_mouse_marking = true;
768 setTextMarkers(newMarkBegin, m_cursor_pos);
769 calculateScrollPos();
773 case EMIE_MOUSE_WHEEL:
774 if (m_vscrollbar && m_vscrollbar->isVisible()) {
775 s32 pos = m_vscrollbar->getPos();
776 s32 step = m_vscrollbar->getSmallStep();
777 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
788 s32 GUIEditBox::getLineFromPos(s32 pos)
790 if (!m_word_wrap && !m_multiline)
794 while (i < (s32)m_broken_text_positions.size()) {
795 if (m_broken_text_positions[i] > pos)
799 return (s32)m_broken_text_positions.size() - 1;
802 void GUIEditBox::updateVScrollBar()
808 // OnScrollBarChanged(...)
809 if (m_vscrollbar->getPos() != m_vscroll_pos) {
810 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
811 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
812 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
814 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
815 if (scrollymax != m_vscrollbar->getMax()) {
816 // manage a newline or a deleted line
817 m_vscrollbar->setMax(scrollymax);
818 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
819 calculateScrollPos();
821 // manage a newline or a deleted line
822 m_vscroll_pos = m_vscrollbar->getPos();
826 // check if a vertical scrollbar is needed ?
827 if (getTextDimension().Height > (u32)m_frame_rect.getHeight()) {
828 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
830 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
831 if (scrollymax != m_vscrollbar->getMax()) {
832 m_vscrollbar->setMax(scrollymax);
833 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
836 if (!m_vscrollbar->isVisible()) {
837 m_vscrollbar->setVisible(true);
840 if (m_vscrollbar->isVisible()) {
841 m_vscrollbar->setVisible(false);
843 m_vscrollbar->setPos(0);
844 m_vscrollbar->setMax(1);
845 m_vscrollbar->setPageSize(s32(getTextDimension().Height));