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"
27 #include "util/string.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)
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 (processKey(event))
215 case EET_MOUSE_INPUT_EVENT:
216 if (processMouse(event))
219 case EET_STRING_INPUT_EVENT:
220 inputString(*event.StringInput.Str);
227 return IGUIElement::OnEvent(event);
230 bool GUIEditBox::processKey(const SEvent &event)
232 if (!event.KeyInput.PressedDown)
235 bool text_changed = false;
236 s32 new_mark_begin = m_mark_begin;
237 s32 new_mark_end = m_mark_end;
239 // control shortcut handling
240 if (event.KeyInput.Control) {
242 // german backlash '\' entered with control + '?'
243 if (event.KeyInput.Char == '\\') {
244 inputChar(event.KeyInput.Char);
248 switch (event.KeyInput.Key) {
252 new_mark_end = Text.size();
255 onKeyControlC(event);
258 text_changed = onKeyControlX(event, new_mark_begin, new_mark_end);
261 text_changed = onKeyControlV(event, new_mark_begin, new_mark_end);
264 // move/highlight to start of text
265 if (event.KeyInput.Shift) {
266 new_mark_end = m_cursor_pos;
276 // move/highlight to end of text
277 if (event.KeyInput.Shift) {
278 new_mark_begin = m_cursor_pos;
279 new_mark_end = Text.size();
282 m_cursor_pos = Text.size();
291 switch (event.KeyInput.Key) {
294 if (m_word_wrap || m_multiline) {
295 p = getLineFromPos(m_cursor_pos);
296 p = m_broken_text_positions[p] +
297 (s32)m_broken_text[p].size();
298 if (p > 0 && (Text[p - 1] == L'\r' ||
299 Text[p - 1] == L'\n'))
303 if (event.KeyInput.Shift) {
304 if (m_mark_begin == m_mark_end)
305 new_mark_begin = m_cursor_pos;
313 m_blink_start_time = porting::getTimeMs();
318 if (m_word_wrap || m_multiline) {
319 p = getLineFromPos(m_cursor_pos);
320 p = m_broken_text_positions[p];
323 if (event.KeyInput.Shift) {
324 if (m_mark_begin == m_mark_end)
325 new_mark_begin = m_cursor_pos;
332 m_blink_start_time = porting::getTimeMs();
338 calculateScrollPos();
339 sendGuiEvent(EGET_EDITBOX_ENTER);
343 if (event.KeyInput.Shift) {
344 if (m_cursor_pos > 0) {
345 if (m_mark_begin == m_mark_end)
346 new_mark_begin = m_cursor_pos;
348 new_mark_end = m_cursor_pos - 1;
355 if (m_cursor_pos > 0)
357 m_blink_start_time = porting::getTimeMs();
360 if (event.KeyInput.Shift) {
361 if (Text.size() > (u32)m_cursor_pos) {
362 if (m_mark_begin == m_mark_end)
363 new_mark_begin = m_cursor_pos;
365 new_mark_end = m_cursor_pos + 1;
372 if (Text.size() > (u32)m_cursor_pos)
374 m_blink_start_time = porting::getTimeMs();
377 if (!onKeyUp(event, new_mark_begin, new_mark_end)) {
382 if (!onKeyDown(event, new_mark_begin, new_mark_end)) {
387 text_changed = onKeyBack(event, new_mark_begin, new_mark_end);
391 text_changed = onKeyDelete(event, new_mark_begin, new_mark_end);
425 inputChar(event.KeyInput.Char);
430 // Set new text markers
431 setTextMarkers(new_mark_begin, new_mark_end);
433 // break the text if it has changed
436 sendGuiEvent(EGET_EDITBOX_CHANGED);
439 calculateScrollPos();
444 bool GUIEditBox::onKeyUp(const SEvent &event, s32 &mark_begin, s32 &mark_end)
447 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
448 s32 lineNo = getLineFromPos(m_cursor_pos);
449 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
450 (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
452 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
453 if ((s32)m_broken_text[lineNo - 1].size() < cp) {
454 m_cursor_pos = m_broken_text_positions[lineNo - 1] +
455 core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
458 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
461 if (event.KeyInput.Shift) {
463 mark_end = m_cursor_pos;
476 bool GUIEditBox::onKeyDown(const SEvent &event, s32 &mark_begin, s32 &mark_end)
479 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
480 s32 lineNo = getLineFromPos(m_cursor_pos);
481 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos :
482 (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
483 if (lineNo < (s32)m_broken_text.size() - 1) {
484 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
485 if ((s32)m_broken_text[lineNo + 1].size() < cp) {
486 m_cursor_pos = m_broken_text_positions[lineNo + 1] +
487 core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
490 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
493 if (event.KeyInput.Shift) {
495 mark_end = m_cursor_pos;
508 void GUIEditBox::onKeyControlC(const SEvent &event)
511 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
514 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
515 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
517 std::string s = stringw_to_utf8(Text.subString(realmbgn, realmend - realmbgn));
518 m_operator->copyToClipboard(s.c_str());
521 bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_end)
523 // First copy to clipboard
524 onKeyControlC(event);
529 if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
532 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
533 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
535 // Now remove from box if enabled
539 s = Text.subString(0, realmbgn);
540 s.append(Text.subString(realmend, Text.size() - realmend));
543 m_cursor_pos = realmbgn;
552 bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end)
554 if (!isEnabled() || !m_writable)
557 // paste from the clipboard
561 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
562 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
565 if (const c8 *p = m_operator->getTextFromClipboard()) {
566 core::stringw inserted_text = utf8_to_stringw(p);
567 if (m_mark_begin == m_mark_end) {
569 core::stringw s = Text.subString(0, m_cursor_pos);
570 s.append(inserted_text);
571 s.append(Text.subString(
572 m_cursor_pos, Text.size() - m_cursor_pos));
574 if (!m_max || s.size() <= m_max) {
576 m_cursor_pos += inserted_text.size();
581 core::stringw s = Text.subString(0, realmbgn);
582 s.append(inserted_text);
583 s.append(Text.subString(realmend, Text.size() - realmend));
585 if (!m_max || s.size() <= m_max) {
587 m_cursor_pos = realmbgn + inserted_text.size();
597 bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
599 if (!isEnabled() || Text.empty() || !m_writable)
604 if (m_mark_begin != m_mark_end) {
605 // delete marked text
607 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
609 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
611 s = Text.subString(0, realmbgn);
612 s.append(Text.subString(realmend, Text.size() - realmend));
615 m_cursor_pos = realmbgn;
617 // delete text behind cursor
618 if (m_cursor_pos > 0)
619 s = Text.subString(0, m_cursor_pos - 1);
622 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
627 if (m_cursor_pos < 0)
629 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
635 bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
637 if (!isEnabled() || Text.empty() || !m_writable)
642 if (m_mark_begin != m_mark_end) {
643 // delete marked text
645 m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
647 m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
649 s = Text.subString(0, realmbgn);
650 s.append(Text.subString(realmend, Text.size() - realmend));
653 m_cursor_pos = realmbgn;
655 // delete text before cursor
656 s = Text.subString(0, m_cursor_pos);
657 s.append(Text.subString(
658 m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
662 if (m_cursor_pos > (s32)Text.size())
663 m_cursor_pos = (s32)Text.size();
665 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
671 void GUIEditBox::inputChar(wchar_t c)
675 core::stringw s(&c, 1);
679 void GUIEditBox::inputString(const core::stringw &str)
681 if (!isEnabled() || !m_writable)
684 u32 len = str.size();
685 if (Text.size()+len <= m_max || m_max == 0) {
687 if (m_mark_begin != m_mark_end) {
688 // replace marked text
689 s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
690 s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
692 s = Text.subString(0, real_begin);
694 s.append(Text.subString(real_end, Text.size() - real_end));
696 m_cursor_pos = real_begin + len;
699 s = Text.subString(0, m_cursor_pos);
701 s.append(Text.subString(m_cursor_pos,
702 Text.size() - m_cursor_pos));
707 m_blink_start_time = porting::getTimeMs();
708 setTextMarkers(0, 0);
712 sendGuiEvent(EGET_EDITBOX_CHANGED);
713 calculateScrollPos();
716 bool GUIEditBox::processMouse(const SEvent &event)
718 switch (event.MouseInput.Event) {
719 case irr::EMIE_LMOUSE_LEFT_UP:
720 if (Environment->hasFocus(this)) {
721 m_cursor_pos = getCursorPos(
722 event.MouseInput.X, event.MouseInput.Y);
723 if (m_mouse_marking) {
724 setTextMarkers(m_mark_begin, m_cursor_pos);
726 m_mouse_marking = false;
727 calculateScrollPos();
731 case irr::EMIE_MOUSE_MOVED: {
732 if (m_mouse_marking) {
733 m_cursor_pos = getCursorPos(
734 event.MouseInput.X, event.MouseInput.Y);
735 setTextMarkers(m_mark_begin, m_cursor_pos);
736 calculateScrollPos();
740 case EMIE_LMOUSE_PRESSED_DOWN:
742 if (!Environment->hasFocus(this)) {
743 m_blink_start_time = porting::getTimeMs();
744 m_mouse_marking = true;
745 m_cursor_pos = getCursorPos(
746 event.MouseInput.X, event.MouseInput.Y);
747 setTextMarkers(m_cursor_pos, m_cursor_pos);
748 calculateScrollPos();
751 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
752 event.MouseInput.X, event.MouseInput.Y))) {
756 m_cursor_pos = getCursorPos(
757 event.MouseInput.X, event.MouseInput.Y);
759 s32 newMarkBegin = m_mark_begin;
760 if (!m_mouse_marking)
761 newMarkBegin = m_cursor_pos;
763 m_mouse_marking = true;
764 setTextMarkers(newMarkBegin, m_cursor_pos);
765 calculateScrollPos();
769 case EMIE_MOUSE_WHEEL:
770 if (m_vscrollbar && m_vscrollbar->isVisible()) {
771 s32 pos = m_vscrollbar->getPos();
772 s32 step = m_vscrollbar->getSmallStep();
773 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
784 s32 GUIEditBox::getLineFromPos(s32 pos)
786 if (!m_word_wrap && !m_multiline)
790 while (i < (s32)m_broken_text_positions.size()) {
791 if (m_broken_text_positions[i] > pos)
795 return (s32)m_broken_text_positions.size() - 1;
798 void GUIEditBox::updateVScrollBar()
804 // OnScrollBarChanged(...)
805 if (m_vscrollbar->getPos() != m_vscroll_pos) {
806 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
807 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
808 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
810 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
811 if (scrollymax != m_vscrollbar->getMax()) {
812 // manage a newline or a deleted line
813 m_vscrollbar->setMax(scrollymax);
814 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
815 calculateScrollPos();
817 // manage a newline or a deleted line
818 m_vscroll_pos = m_vscrollbar->getPos();
822 // check if a vertical scrollbar is needed ?
823 if (getTextDimension().Height > (u32)m_frame_rect.getHeight()) {
824 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
826 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
827 if (scrollymax != m_vscrollbar->getMax()) {
828 m_vscrollbar->setMax(scrollymax);
829 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
832 if (!m_vscrollbar->isVisible()) {
833 m_vscrollbar->setVisible(true);
836 if (m_vscrollbar->isVisible()) {
837 m_vscrollbar->setVisible(false);
839 m_vscrollbar->setPos(0);
840 m_vscrollbar->setMax(1);
841 m_vscrollbar->setPageSize(s32(getTextDimension().Height));