1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // Modified by Mustapha T.
3 // This file is part of the "Irrlicht Engine".
4 // For conditions of distribution and use, see copyright notice in irrlicht.h
6 #include "guiEditBoxWithScrollbar.h"
9 #include "IGUIEnvironment.h"
11 #include "IVideoDriver.h"
19 optional scrollbars [done]
20 ctrl+left/right to select word
21 double click/ctrl click: word select + drag to select whole words, triple click to select line
22 optional? dragging selected text
28 GUIEditBoxWithScrollBar::GUIEditBoxWithScrollBar(const wchar_t* text, bool border,
29 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
30 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
31 : IGUIEditBox(environment, parent, id, rectangle), m_mouse_marking(false),
32 m_border(border), m_background(true), m_override_color_enabled(false), m_mark_begin(0), m_mark_end(0),
33 m_override_color(video::SColor(101, 255, 255, 255)), m_override_font(0), m_last_break_font(0),
34 m_operator(0), m_blink_start_time(0), m_cursor_pos(0), m_hscroll_pos(0), m_vscroll_pos(0), m_max(0),
35 m_word_wrap(false), m_multiline(false), m_autoscroll(true), m_passwordbox(false),
36 m_passwordchar(L'*'), m_halign(EGUIA_UPPERLEFT), m_valign(EGUIA_CENTER),
37 m_current_text_rect(0, 0, 1, 1), m_frame_rect(rectangle),
38 m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable),
39 m_bg_color_used(false)
42 setDebugName("GUIEditBoxWithScrollBar");
49 m_operator = Environment->getOSOperator();
54 // this element can be tabbed to
66 setWritable(writable);
71 GUIEditBoxWithScrollBar::~GUIEditBoxWithScrollBar()
74 m_override_font->drop();
79 m_vscrollbar->remove();
83 //! Sets another skin independent font.
84 void GUIEditBoxWithScrollBar::setOverrideFont(IGUIFont* font)
86 if (m_override_font == font)
90 m_override_font->drop();
92 m_override_font = font;
95 m_override_font->grab();
100 //! Gets the override font (if any)
101 IGUIFont * GUIEditBoxWithScrollBar::getOverrideFont() const
103 return m_override_font;
106 //! Get the font which is used right now for drawing
107 IGUIFont* GUIEditBoxWithScrollBar::getActiveFont() const
110 return m_override_font;
111 IGUISkin* skin = Environment->getSkin();
113 return skin->getFont();
117 //! Sets another color for the text.
118 void GUIEditBoxWithScrollBar::setOverrideColor(video::SColor color)
120 m_override_color = color;
121 m_override_color_enabled = true;
125 video::SColor GUIEditBoxWithScrollBar::getOverrideColor() const
127 return m_override_color;
131 //! Turns the border on or off
132 void GUIEditBoxWithScrollBar::setDrawBorder(bool border)
137 //! Sets whether to draw the background
138 void GUIEditBoxWithScrollBar::setDrawBackground(bool draw)
143 //! Sets if the text should use the overide color or the color in the gui skin.
144 void GUIEditBoxWithScrollBar::enableOverrideColor(bool enable)
146 m_override_color_enabled = enable;
149 bool GUIEditBoxWithScrollBar::isOverrideColorEnabled() const
151 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
152 return m_override_color_enabled;
155 //! Enables or disables word wrap
156 void GUIEditBoxWithScrollBar::setWordWrap(bool enable)
158 m_word_wrap = enable;
163 void GUIEditBoxWithScrollBar::updateAbsolutePosition()
165 core::rect<s32> old_absolute_rect(AbsoluteRect);
166 IGUIElement::updateAbsolutePosition();
167 if (old_absolute_rect != AbsoluteRect) {
168 calculateFrameRect();
170 calculateScrollPos();
174 //! Checks if word wrap is enabled
175 bool GUIEditBoxWithScrollBar::isWordWrapEnabled() const
177 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
182 //! Enables or disables newlines.
183 void GUIEditBoxWithScrollBar::setMultiLine(bool enable)
185 m_multiline = enable;
189 //! Checks if multi line editing is enabled
190 bool GUIEditBoxWithScrollBar::isMultiLineEnabled() const
192 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
197 void GUIEditBoxWithScrollBar::setPasswordBox(bool password_box, wchar_t password_char)
199 m_passwordbox = password_box;
201 m_passwordchar = password_char;
204 m_broken_text.clear();
209 bool GUIEditBoxWithScrollBar::isPasswordBox() const
211 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
212 return m_passwordbox;
216 //! Sets text justification
217 void GUIEditBoxWithScrollBar::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
219 m_halign = horizontal;
224 //! called if an event happened.
225 bool GUIEditBoxWithScrollBar::OnEvent(const SEvent& event)
228 switch (event.EventType)
231 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
232 if (event.GUIEvent.Caller == this) {
233 m_mouse_marking = false;
234 setTextMarkers(0, 0);
238 case EET_KEY_INPUT_EVENT:
239 if (processKey(event))
242 case EET_MOUSE_INPUT_EVENT:
243 if (processMouse(event))
251 return IGUIElement::OnEvent(event);
255 bool GUIEditBoxWithScrollBar::processKey(const SEvent& event)
261 if (!event.KeyInput.PressedDown)
264 bool text_changed = false;
265 s32 new_mark_begin = m_mark_begin;
266 s32 new_mark_end = m_mark_end;
268 // control shortcut handling
270 if (event.KeyInput.Control) {
272 // german backlash '\' entered with control + '?'
273 if (event.KeyInput.Char == '\\') {
274 inputChar(event.KeyInput.Char);
278 switch (event.KeyInput.Key) {
282 new_mark_end = Text.size();
286 if (!m_passwordbox && m_operator && m_mark_begin != m_mark_end)
288 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
289 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
292 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
293 m_operator->copyToClipboard(s.c_str());
297 // cut to the clipboard
298 if (!m_passwordbox && m_operator && m_mark_begin != m_mark_end) {
299 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
300 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
304 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
305 m_operator->copyToClipboard(sc.c_str());
311 s = Text.subString(0, realmbgn);
312 s.append(Text.subString(realmend, Text.size() - realmend));
315 m_cursor_pos = realmbgn;
326 // paste from the clipboard
328 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
329 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
332 const c8* p = m_operator->getTextFromClipboard();
334 if (m_mark_begin == m_mark_end) {
336 core::stringw s = Text.subString(0, m_cursor_pos);
338 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
340 if (!m_max || s.size() <= m_max) // thx to Fish FH for fix
344 m_cursor_pos += s.size();
349 core::stringw s = Text.subString(0, realmbgn);
351 s.append(Text.subString(realmend, Text.size() - realmend));
353 if (!m_max || s.size() <= m_max) // thx to Fish FH for fix
357 m_cursor_pos = realmbgn + s.size();
368 // move/highlight to start of text
369 if (event.KeyInput.Shift) {
370 new_mark_end = m_cursor_pos;
380 // move/highlight to end of text
381 if (event.KeyInput.Shift) {
382 new_mark_begin = m_cursor_pos;
383 new_mark_end = Text.size();
386 m_cursor_pos = Text.size();
395 // default keyboard handling
397 switch (event.KeyInput.Key) {
401 if (m_word_wrap || m_multiline) {
402 p = getLineFromPos(m_cursor_pos);
403 p = m_broken_text_positions[p] + (s32)m_broken_text[p].size();
404 if (p > 0 && (Text[p - 1] == L'\r' || Text[p - 1] == L'\n'))
408 if (event.KeyInput.Shift) {
409 if (m_mark_begin == m_mark_end)
410 new_mark_begin = m_cursor_pos;
418 m_blink_start_time = porting::getTimeMs();
425 if (m_word_wrap || m_multiline) {
426 p = getLineFromPos(m_cursor_pos);
427 p = m_broken_text_positions[p];
430 if (event.KeyInput.Shift) {
431 if (m_mark_begin == m_mark_end)
432 new_mark_begin = m_cursor_pos;
439 m_blink_start_time = porting::getTimeMs();
446 calculateScrollPos();
447 sendGuiEvent(EGET_EDITBOX_ENTER);
452 if (event.KeyInput.Shift) {
453 if (m_cursor_pos > 0) {
454 if (m_mark_begin == m_mark_end)
455 new_mark_begin = m_cursor_pos;
457 new_mark_end = m_cursor_pos - 1;
464 if (m_cursor_pos > 0)
466 m_blink_start_time = porting::getTimeMs();
470 if (event.KeyInput.Shift) {
471 if (Text.size() > (u32)m_cursor_pos) {
472 if (m_mark_begin == m_mark_end)
473 new_mark_begin = m_cursor_pos;
475 new_mark_end = m_cursor_pos + 1;
482 if (Text.size() > (u32)m_cursor_pos)
484 m_blink_start_time = porting::getTimeMs();
487 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
488 s32 lineNo = getLineFromPos(m_cursor_pos);
489 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos : (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
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] + core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
495 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
498 if (event.KeyInput.Shift) {
500 new_mark_end = m_cursor_pos;
510 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
511 s32 lineNo = getLineFromPos(m_cursor_pos);
512 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos : (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
513 if (lineNo < (s32)m_broken_text.size() - 1)
515 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
516 if ((s32)m_broken_text[lineNo + 1].size() < cp)
517 m_cursor_pos = m_broken_text_positions[lineNo + 1] + core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
519 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
522 if (event.KeyInput.Shift) {
524 new_mark_end = m_cursor_pos;
542 if (m_mark_begin != m_mark_end) {
543 // delete marked text
544 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
545 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
547 s = Text.subString(0, realmbgn);
548 s.append(Text.subString(realmend, Text.size() - realmend));
551 m_cursor_pos = realmbgn;
553 // delete text behind cursor
554 if (m_cursor_pos > 0)
555 s = Text.subString(0, m_cursor_pos - 1);
558 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
563 if (m_cursor_pos < 0)
565 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
575 if (Text.size() != 0) {
578 if (m_mark_begin != m_mark_end) {
579 // delete marked text
580 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
581 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
583 s = Text.subString(0, realmbgn);
584 s.append(Text.subString(realmend, Text.size() - realmend));
587 m_cursor_pos = realmbgn;
589 // delete text before cursor
590 s = Text.subString(0, m_cursor_pos);
591 s.append(Text.subString(m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
595 if (m_cursor_pos > (s32)Text.size())
596 m_cursor_pos = (s32)Text.size();
598 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
636 inputChar(event.KeyInput.Char);
640 // Set new text markers
641 setTextMarkers(new_mark_begin, new_mark_end);
643 // break the text if it has changed
646 calculateScrollPos();
647 sendGuiEvent(EGET_EDITBOX_CHANGED);
651 calculateScrollPos();
658 //! draws the element and its children
659 void GUIEditBoxWithScrollBar::draw()
664 const bool focus = Environment->hasFocus(this);
666 IGUISkin* skin = Environment->getSkin();
670 video::SColor default_bg_color;
671 video::SColor bg_color;
673 default_bg_color = m_writable ? skin->getColor(EGDC_WINDOW) : video::SColor(0);
674 bg_color = m_bg_color_used ? m_bg_color : default_bg_color;
676 if (!m_border && m_background) {
677 skin->draw2DRectangle(this, bg_color, AbsoluteRect, &AbsoluteClippingRect);
685 skin->draw3DSunkenPane(this, bg_color, false, m_background,
686 AbsoluteRect, &AbsoluteClippingRect);
689 calculateFrameRect();
692 core::rect<s32> local_clip_rect = m_frame_rect;
693 local_clip_rect.clipAgainst(AbsoluteClippingRect);
697 IGUIFont* font = getActiveFont();
700 s32 charcursorpos = 0;
703 if (m_last_break_font != font) {
707 // calculate cursor pos
709 core::stringw *txt_line = &Text;
715 const bool ml = (!m_passwordbox && (m_word_wrap || m_multiline));
716 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
717 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
718 const s32 hline_start = ml ? getLineFromPos(realmbgn) : 0;
719 const s32 hline_count = ml ? getLineFromPos(realmend) - hline_start + 1 : 1;
720 const s32 line_count = ml ? m_broken_text.size() : 1;
722 // Save the override color information.
723 // Then, alter it if the edit box is disabled.
724 const bool prevOver = m_override_color_enabled;
725 const video::SColor prevColor = m_override_color;
728 if (!isEnabled() && !m_override_color_enabled) {
729 m_override_color_enabled = true;
730 m_override_color = skin->getColor(EGDC_GRAY_TEXT);
733 for (s32 i = 0; i < line_count; ++i) {
736 // clipping test - don't draw anything outside the visible area
737 core::rect<s32> c = local_clip_rect;
738 c.clipAgainst(m_current_text_rect);
744 if (m_broken_text.size() != 1) {
745 m_broken_text.clear();
746 m_broken_text.push_back(core::stringw());
748 if (m_broken_text[0].size() != Text.size()){
749 m_broken_text[0] = Text;
750 for (u32 q = 0; q < Text.size(); ++q)
752 m_broken_text[0][q] = m_passwordchar;
755 txt_line = &m_broken_text[0];
758 txt_line = ml ? &m_broken_text[i] : &Text;
759 start_pos = ml ? m_broken_text_positions[i] : 0;
764 font->draw(txt_line->c_str(), m_current_text_rect,
765 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
766 false, true, &local_clip_rect);
768 // draw mark and marked text
769 if (focus && m_mark_begin != m_mark_end && i >= hline_start && i < hline_start + hline_count) {
771 s32 mbegin = 0, mend = 0;
772 s32 lineStartPos = 0, lineEndPos = txt_line->size();
774 if (i == hline_start) {
775 // highlight start is on this line
776 s = txt_line->subString(0, realmbgn - start_pos);
777 mbegin = font->getDimension(s.c_str()).Width;
780 mbegin += font->getKerningWidth(
781 &((*txt_line)[realmbgn - start_pos]),
782 realmbgn - start_pos > 0 ? &((*txt_line)[realmbgn - start_pos - 1]) : 0);
784 lineStartPos = realmbgn - start_pos;
786 if (i == hline_start + hline_count - 1) {
787 // highlight end is on this line
788 s2 = txt_line->subString(0, realmend - start_pos);
789 mend = font->getDimension(s2.c_str()).Width;
790 lineEndPos = (s32)s2.size();
792 mend = font->getDimension(txt_line->c_str()).Width;
796 m_current_text_rect.UpperLeftCorner.X += mbegin;
797 m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
801 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &local_clip_rect);
804 s = txt_line->subString(lineStartPos, lineEndPos - lineStartPos);
807 font->draw(s.c_str(), m_current_text_rect,
808 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
809 false, true, &local_clip_rect);
814 // Return the override color information to its previous settings.
815 m_override_color_enabled = prevOver;
816 m_override_color = prevColor;
820 if (IsEnabled && m_writable) {
821 if (m_word_wrap || m_multiline) {
822 cursor_line = getLineFromPos(m_cursor_pos);
823 txt_line = &m_broken_text[cursor_line];
824 start_pos = m_broken_text_positions[cursor_line];
826 s = txt_line->subString(0, m_cursor_pos - start_pos);
827 charcursorpos = font->getDimension(s.c_str()).Width +
828 font->getKerningWidth(L"_", m_cursor_pos - start_pos > 0 ? &((*txt_line)[m_cursor_pos - start_pos - 1]) : 0);
830 if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
831 setTextRect(cursor_line);
832 m_current_text_rect.UpperLeftCorner.X += charcursorpos;
834 font->draw(L"_", m_current_text_rect,
835 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
836 false, true, &local_clip_rect);
846 //! Sets the new caption of this element.
847 void GUIEditBoxWithScrollBar::setText(const wchar_t* text)
850 if (u32(m_cursor_pos) > Text.size())
851 m_cursor_pos = Text.size();
857 //! Enables or disables automatic scrolling with cursor position
858 //! \param enable: If set to true, the text will move around with the cursor position
859 void GUIEditBoxWithScrollBar::setAutoScroll(bool enable)
861 m_autoscroll = enable;
865 //! Checks to see if automatic scrolling is enabled
866 //! \return true if automatic scrolling is enabled, false if not
867 bool GUIEditBoxWithScrollBar::isAutoScrollEnabled() const
869 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
874 //! Gets the area of the text in the edit box
875 //! \return Returns the size in pixels of the text
876 core::dimension2du GUIEditBoxWithScrollBar::getTextDimension()
881 ret = m_current_text_rect;
883 for (u32 i = 1; i < m_broken_text.size(); ++i) {
885 ret.addInternalPoint(m_current_text_rect.UpperLeftCorner);
886 ret.addInternalPoint(m_current_text_rect.LowerRightCorner);
889 return core::dimension2du(ret.getSize());
893 //! Sets the maximum amount of characters which may be entered in the box.
894 //! \param max: Maximum amount of characters. If 0, the character amount is
896 void GUIEditBoxWithScrollBar::setMax(u32 max)
900 if (Text.size() > m_max && m_max != 0)
901 Text = Text.subString(0, m_max);
905 //! Returns maximum amount of characters, previously set by setMax();
906 u32 GUIEditBoxWithScrollBar::getMax() const
912 bool GUIEditBoxWithScrollBar::processMouse(const SEvent& event)
914 switch (event.MouseInput.Event)
916 case irr::EMIE_LMOUSE_LEFT_UP:
917 if (Environment->hasFocus(this)) {
918 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
919 if (m_mouse_marking) {
920 setTextMarkers(m_mark_begin, m_cursor_pos);
922 m_mouse_marking = false;
923 calculateScrollPos();
927 case irr::EMIE_MOUSE_MOVED:
929 if (m_mouse_marking) {
930 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
931 setTextMarkers(m_mark_begin, m_cursor_pos);
932 calculateScrollPos();
937 case EMIE_LMOUSE_PRESSED_DOWN:
939 if (!Environment->hasFocus(this)) {
940 m_blink_start_time = porting::getTimeMs();
941 m_mouse_marking = true;
942 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
943 setTextMarkers(m_cursor_pos, m_cursor_pos);
944 calculateScrollPos();
947 if (!AbsoluteClippingRect.isPointInside(
948 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
952 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
954 s32 newMarkBegin = m_mark_begin;
955 if (!m_mouse_marking)
956 newMarkBegin = m_cursor_pos;
958 m_mouse_marking = true;
959 setTextMarkers(newMarkBegin, m_cursor_pos);
960 calculateScrollPos();
972 s32 GUIEditBoxWithScrollBar::getCursorPos(s32 x, s32 y)
974 IGUIFont* font = getActiveFont();
976 const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
978 core::stringw *txt_line = 0;
982 for (u32 i = 0; i < line_count; ++i) {
984 if (i == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
985 y = m_current_text_rect.UpperLeftCorner.Y;
986 if (i == line_count - 1 && y > m_current_text_rect.LowerRightCorner.Y)
987 y = m_current_text_rect.LowerRightCorner.Y;
989 // is it inside this region?
990 if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
991 // we've found the clicked line
992 txt_line = (m_word_wrap || m_multiline) ? &m_broken_text[i] : &Text;
993 start_pos = (m_word_wrap || m_multiline) ? m_broken_text_positions[i] : 0;
998 if (x < m_current_text_rect.UpperLeftCorner.X)
999 x = m_current_text_rect.UpperLeftCorner.X;
1004 s32 idx = font->getCharacterFromPos(txt_line->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
1006 // click was on or left of the line
1008 return idx + start_pos;
1010 // click was off the right edge of the line, go to end.
1011 return txt_line->size() + start_pos;
1015 //! Breaks the single text line.
1016 void GUIEditBoxWithScrollBar::breakText()
1018 if ((!m_word_wrap && !m_multiline))
1021 m_broken_text.clear(); // need to reallocate :/
1022 m_broken_text_positions.clear();
1024 IGUIFont* font = getActiveFont();
1028 m_last_break_font = font;
1032 core::stringw whitespace;
1033 s32 last_line_start = 0;
1034 s32 size = Text.size();
1036 s32 el_width = RelativeRect.getWidth() - 6;
1039 for (s32 i = 0; i < size; ++i) {
1041 bool line_break = false;
1043 if (c == L'\r') { // Mac or Windows breaks
1047 if (Text[i + 1] == L'\n') { // Windows breaks
1048 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
1049 // Instead rework the cursor positioning to be able to handle this (but not in stable release
1050 // branch as users might already expect this behavior).
1053 if (m_cursor_pos > i)
1056 } else if (c == L'\n') { // Unix breaks
1061 // don't break if we're not a multi-line edit box
1065 if (c == L' ' || c == 0 || i == (size - 1)) {
1066 // here comes the next whitespace, look if
1067 // we can break the last word to the next line
1068 // We also break whitespace, otherwise cursor would vanish beside the right border.
1069 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1070 s32 worldlgth = font->getDimension(word.c_str()).Width;
1072 if (m_word_wrap && length + worldlgth + whitelgth > el_width && line.size() > 0) {
1073 // break to next line
1075 m_broken_text.push_back(line);
1076 m_broken_text_positions.push_back(last_line_start);
1077 last_line_start = i - (s32)word.size();
1083 length += whitelgth + worldlgth;
1093 // compute line break
1097 m_broken_text.push_back(line);
1098 m_broken_text_positions.push_back(last_line_start);
1099 last_line_start = i + 1;
1106 // yippee this is a word..
1113 m_broken_text.push_back(line);
1114 m_broken_text_positions.push_back(last_line_start);
1117 // TODO: that function does interpret VAlign according to line-index (indexed line is placed on top-center-bottom)
1118 // but HAlign according to line-width (pixels) and not by row.
1119 // Intuitively I suppose HAlign handling is better as VScrollPos should handle the line-scrolling.
1120 // But please no one change this without also rewriting (and this time fucking testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
1121 void GUIEditBoxWithScrollBar::setTextRect(s32 line)
1126 IGUIFont* font = getActiveFont();
1130 core::dimension2du d;
1132 // get text dimension
1133 const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
1134 if (m_word_wrap || m_multiline) {
1135 d = font->getDimension(m_broken_text[line].c_str());
1137 d = font->getDimension(Text.c_str());
1138 d.Height = AbsoluteRect.getHeight();
1140 d.Height += font->getKerningHeight();
1145 // align to h centre
1146 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth() / 2) - (d.Width / 2);
1147 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth() / 2) + (d.Width / 2);
1149 case EGUIA_LOWERRIGHT:
1150 // align to right edge
1151 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
1152 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
1155 // align to left edge
1156 m_current_text_rect.UpperLeftCorner.X = 0;
1157 m_current_text_rect.LowerRightCorner.X = d.Width;
1163 // align to v centre
1164 m_current_text_rect.UpperLeftCorner.Y =
1165 (m_frame_rect.getHeight() / 2) - (line_count*d.Height) / 2 + d.Height*line;
1167 case EGUIA_LOWERRIGHT:
1168 // align to bottom edge
1169 m_current_text_rect.UpperLeftCorner.Y =
1170 m_frame_rect.getHeight() - line_count*d.Height + d.Height*line;
1173 // align to top edge
1174 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
1178 m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
1179 m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
1180 m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
1181 m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
1183 m_current_text_rect += m_frame_rect.UpperLeftCorner;
1187 s32 GUIEditBoxWithScrollBar::getLineFromPos(s32 pos)
1189 if (!m_word_wrap && !m_multiline)
1193 while (i < (s32)m_broken_text_positions.size()) {
1194 if (m_broken_text_positions[i] > pos)
1198 return (s32)m_broken_text_positions.size() - 1;
1202 void GUIEditBoxWithScrollBar::inputChar(wchar_t c)
1208 if (Text.size() < m_max || m_max == 0) {
1211 if (m_mark_begin != m_mark_end) {
1212 // replace marked text
1213 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
1214 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
1216 s = Text.subString(0, realmbgn);
1218 s.append(Text.subString(realmend, Text.size() - realmend));
1220 m_cursor_pos = realmbgn + 1;
1222 // add new character
1223 s = Text.subString(0, m_cursor_pos);
1225 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
1230 m_blink_start_time = porting::getTimeMs();
1231 setTextMarkers(0, 0);
1235 calculateScrollPos();
1236 sendGuiEvent(EGET_EDITBOX_CHANGED);
1239 // calculate autoscroll
1240 void GUIEditBoxWithScrollBar::calculateScrollPos()
1245 IGUISkin* skin = Environment->getSkin();
1248 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
1252 s32 curs_line = getLineFromPos(m_cursor_pos);
1255 setTextRect(curs_line);
1256 const bool has_broken_text = m_multiline || m_word_wrap;
1258 // Check horizonal scrolling
1259 // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
1261 // get cursor position
1262 IGUIFont* font = getActiveFont();
1267 irr::u32 cursor_width = font->getDimension(L"_").Width;
1268 core::stringw *txt_line = has_broken_text ? &m_broken_text[curs_line] : &Text;
1269 s32 cpos = has_broken_text ? m_cursor_pos - m_broken_text_positions[curs_line] : m_cursor_pos; // column
1270 s32 cstart = font->getDimension(txt_line->subString(0, cpos).c_str()).Width; // pixels from text-start
1271 s32 cend = cstart + cursor_width;
1272 s32 txt_width = font->getDimension(txt_line->c_str()).Width;
1274 if (txt_width < m_frame_rect.getWidth()) {
1275 // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
1276 // This check just fixes the case where it was most noticable (text smaller than clipping area).
1279 setTextRect(curs_line);
1282 if (m_current_text_rect.UpperLeftCorner.X + cstart < m_frame_rect.UpperLeftCorner.X) {
1283 // cursor to the left of the clipping area
1284 m_hscroll_pos -= m_frame_rect.UpperLeftCorner.X - (m_current_text_rect.UpperLeftCorner.X + cstart);
1285 setTextRect(curs_line);
1287 // TODO: should show more characters to the left when we're scrolling left
1288 // and the cursor reaches the border.
1289 } else if (m_current_text_rect.UpperLeftCorner.X + cend > m_frame_rect.LowerRightCorner.X) {
1290 // cursor to the right of the clipping area
1291 m_hscroll_pos += (m_current_text_rect.UpperLeftCorner.X + cend) - m_frame_rect.LowerRightCorner.X;
1292 setTextRect(curs_line);
1296 // calculate vertical scrolling
1297 if (has_broken_text) {
1298 irr::u32 line_height = font->getDimension(L"A").Height + font->getKerningHeight();
1299 // only up to 1 line fits?
1300 if (line_height >= (irr::u32)m_frame_rect.getHeight()) {
1302 setTextRect(curs_line);
1303 s32 unscrolledPos = m_current_text_rect.UpperLeftCorner.Y;
1304 s32 pivot = m_frame_rect.UpperLeftCorner.Y;
1307 pivot += m_frame_rect.getHeight() / 2;
1308 unscrolledPos += line_height / 2;
1310 case EGUIA_LOWERRIGHT:
1311 pivot += m_frame_rect.getHeight();
1312 unscrolledPos += line_height;
1317 m_vscroll_pos = unscrolledPos - pivot;
1318 setTextRect(curs_line);
1320 // First 2 checks are necessary when people delete lines
1322 if (m_current_text_rect.UpperLeftCorner.Y > m_frame_rect.UpperLeftCorner.Y && m_valign != EGUIA_LOWERRIGHT) {
1323 // first line is leaving a gap on top
1325 } else if (m_valign != EGUIA_UPPERLEFT) {
1326 u32 lastLine = m_broken_text_positions.empty() ? 0 : m_broken_text_positions.size() - 1;
1327 setTextRect(lastLine);
1328 if (m_current_text_rect.LowerRightCorner.Y < m_frame_rect.LowerRightCorner.Y)
1330 // last line is leaving a gap on bottom
1331 m_vscroll_pos -= m_frame_rect.LowerRightCorner.Y - m_current_text_rect.LowerRightCorner.Y;
1335 setTextRect(curs_line);
1336 if (m_current_text_rect.UpperLeftCorner.Y < m_frame_rect.UpperLeftCorner.Y) {
1337 // text above valid area
1338 m_vscroll_pos -= m_frame_rect.UpperLeftCorner.Y - m_current_text_rect.UpperLeftCorner.Y;
1339 setTextRect(curs_line);
1340 } else if (m_current_text_rect.LowerRightCorner.Y > m_frame_rect.LowerRightCorner.Y){
1341 // text below valid area
1342 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y;
1343 setTextRect(curs_line);
1349 m_vscrollbar->setPos(m_vscroll_pos);
1353 void GUIEditBoxWithScrollBar::calculateFrameRect()
1355 m_frame_rect = AbsoluteRect;
1360 skin = Environment->getSkin();
1361 if (m_border && skin) {
1362 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1363 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1364 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1365 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1371 //! set text markers
1372 void GUIEditBoxWithScrollBar::setTextMarkers(s32 begin, s32 end)
1374 if (begin != m_mark_begin || end != m_mark_end) {
1375 m_mark_begin = begin;
1377 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1381 //! send some gui event to parent
1382 void GUIEditBoxWithScrollBar::sendGuiEvent(EGUI_EVENT_TYPE type)
1386 e.EventType = EET_GUI_EVENT;
1387 e.GUIEvent.Caller = this;
1388 e.GUIEvent.Element = 0;
1389 e.GUIEvent.EventType = type;
1395 //! create a vertical scroll bar
1396 void GUIEditBoxWithScrollBar::createVScrollBar()
1400 skin = Environment->getSkin();
1402 m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
1404 irr::core::rect<s32> scrollbarrect = m_frame_rect;
1405 scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
1406 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1407 m_vscrollbar->setVisible(false);
1408 m_vscrollbar->setSmallStep(1);
1409 m_vscrollbar->setLargeStep(1);
1412 void GUIEditBoxWithScrollBar::updateVScrollBar()
1414 if (!m_vscrollbar) {
1418 // OnScrollBarChanged(...)
1419 if (m_vscrollbar->getPos() != m_vscroll_pos) {
1420 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
1421 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
1422 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
1424 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1425 if (scrollymax != m_vscrollbar->getMax()) {
1426 // manage a newline or a deleted line
1427 m_vscrollbar->setMax(scrollymax);
1428 calculateScrollPos();
1430 // manage a newline or a deleted line
1431 m_vscroll_pos = m_vscrollbar->getPos();
1435 // check if a vertical scrollbar is needed ?
1436 if (getTextDimension().Height > (u32) m_frame_rect.getHeight()) {
1437 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
1439 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1440 if (scrollymax != m_vscrollbar->getMax()) {
1441 m_vscrollbar->setMax(scrollymax);
1444 if (!m_vscrollbar->isVisible()) {
1445 m_vscrollbar->setVisible(true);
1448 if (m_vscrollbar->isVisible())
1450 m_vscrollbar->setVisible(false);
1452 m_vscrollbar->setPos(0);
1453 m_vscrollbar->setMax(1);
1460 //! set true if this editbox is writable
1461 void GUIEditBoxWithScrollBar::setWritable(bool writable)
1463 m_writable = writable;
1466 //! Change the background color
1467 void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
1469 m_bg_color = bg_color;
1470 m_bg_color_used = true;
1473 //! Writes attributes of the element.
1474 void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
1476 // IGUIEditBox::serializeAttributes(out,options);
1478 out->addBool("Border", m_border);
1479 out->addBool("Background", m_background);
1480 out->addBool("OverrideColorEnabled", m_override_color_enabled);
1481 out->addColor("OverrideColor", m_override_color);
1482 // out->addFont("OverrideFont", OverrideFont);
1483 out->addInt("MaxChars", m_max);
1484 out->addBool("WordWrap", m_word_wrap);
1485 out->addBool("MultiLine", m_multiline);
1486 out->addBool("AutoScroll", m_autoscroll);
1487 out->addBool("PasswordBox", m_passwordbox);
1488 core::stringw ch = L" ";
1489 ch[0] = m_passwordchar;
1490 out->addString("PasswordChar", ch.c_str());
1491 out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
1492 out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
1493 out->addBool("Writable", m_writable);
1495 IGUIEditBox::serializeAttributes(out, options);
1499 //! Reads attributes of the element
1500 void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
1502 IGUIEditBox::deserializeAttributes(in, options);
1504 setDrawBorder(in->getAttributeAsBool("Border"));
1505 setDrawBackground(in->getAttributeAsBool("Background"));
1506 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1507 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1508 setMax(in->getAttributeAsInt("MaxChars"));
1509 setWordWrap(in->getAttributeAsBool("WordWrap"));
1510 setMultiLine(in->getAttributeAsBool("MultiLine"));
1511 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1512 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1515 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1517 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1519 setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1520 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1522 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1523 setWritable(in->getAttributeAsBool("Writable"));
1526 bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }
1527 bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; }
1528 void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { }
1529 wchar_t GUIEditBoxWithScrollBar::getCursorChar() const { return '|'; }
1530 void GUIEditBoxWithScrollBar::setCursorBlinkTime(irr::u32 timeMs) { }
1531 irr::u32 GUIEditBoxWithScrollBar::getCursorBlinkTime() const { return 500; }