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 return m_override_color_enabled;
154 //! Enables or disables word wrap
155 void GUIEditBoxWithScrollBar::setWordWrap(bool enable)
157 m_word_wrap = enable;
162 void GUIEditBoxWithScrollBar::updateAbsolutePosition()
164 core::rect<s32> old_absolute_rect(AbsoluteRect);
165 IGUIElement::updateAbsolutePosition();
166 if (old_absolute_rect != AbsoluteRect) {
167 calculateFrameRect();
169 calculateScrollPos();
173 //! Checks if word wrap is enabled
174 bool GUIEditBoxWithScrollBar::isWordWrapEnabled() const
180 //! Enables or disables newlines.
181 void GUIEditBoxWithScrollBar::setMultiLine(bool enable)
183 m_multiline = enable;
187 //! Checks if multi line editing is enabled
188 bool GUIEditBoxWithScrollBar::isMultiLineEnabled() const
194 void GUIEditBoxWithScrollBar::setPasswordBox(bool password_box, wchar_t password_char)
196 m_passwordbox = password_box;
198 m_passwordchar = password_char;
201 m_broken_text.clear();
206 bool GUIEditBoxWithScrollBar::isPasswordBox() const
208 return m_passwordbox;
212 //! Sets text justification
213 void GUIEditBoxWithScrollBar::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
215 m_halign = horizontal;
220 //! called if an event happened.
221 bool GUIEditBoxWithScrollBar::OnEvent(const SEvent& event)
224 switch (event.EventType)
227 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
228 if (event.GUIEvent.Caller == this) {
229 m_mouse_marking = false;
230 setTextMarkers(0, 0);
234 case EET_KEY_INPUT_EVENT:
235 if (processKey(event))
238 case EET_MOUSE_INPUT_EVENT:
239 if (processMouse(event))
247 return IGUIElement::OnEvent(event);
251 bool GUIEditBoxWithScrollBar::processKey(const SEvent& event)
257 if (!event.KeyInput.PressedDown)
260 bool text_changed = false;
261 s32 new_mark_begin = m_mark_begin;
262 s32 new_mark_end = m_mark_end;
264 // control shortcut handling
266 if (event.KeyInput.Control) {
268 // german backlash '\' entered with control + '?'
269 if (event.KeyInput.Char == '\\') {
270 inputChar(event.KeyInput.Char);
274 switch (event.KeyInput.Key) {
278 new_mark_end = Text.size();
282 if (!m_passwordbox && m_operator && m_mark_begin != m_mark_end)
284 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
285 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
288 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
289 m_operator->copyToClipboard(s.c_str());
293 // cut to the clipboard
294 if (!m_passwordbox && m_operator && m_mark_begin != m_mark_end) {
295 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
296 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
300 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
301 m_operator->copyToClipboard(sc.c_str());
307 s = Text.subString(0, realmbgn);
308 s.append(Text.subString(realmend, Text.size() - realmend));
311 m_cursor_pos = realmbgn;
322 // paste from the clipboard
324 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
325 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
328 const c8* p = m_operator->getTextFromClipboard();
330 if (m_mark_begin == m_mark_end) {
332 core::stringw s = Text.subString(0, m_cursor_pos);
334 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
336 if (!m_max || s.size() <= m_max) // thx to Fish FH for fix
340 m_cursor_pos += s.size();
345 core::stringw s = Text.subString(0, realmbgn);
347 s.append(Text.subString(realmend, Text.size() - realmend));
349 if (!m_max || s.size() <= m_max) // thx to Fish FH for fix
353 m_cursor_pos = realmbgn + s.size();
364 // move/highlight to start of text
365 if (event.KeyInput.Shift) {
366 new_mark_end = m_cursor_pos;
376 // move/highlight to end of text
377 if (event.KeyInput.Shift) {
378 new_mark_begin = m_cursor_pos;
379 new_mark_end = Text.size();
382 m_cursor_pos = Text.size();
391 // default keyboard handling
393 switch (event.KeyInput.Key) {
397 if (m_word_wrap || m_multiline) {
398 p = getLineFromPos(m_cursor_pos);
399 p = m_broken_text_positions[p] + (s32)m_broken_text[p].size();
400 if (p > 0 && (Text[p - 1] == L'\r' || Text[p - 1] == L'\n'))
404 if (event.KeyInput.Shift) {
405 if (m_mark_begin == m_mark_end)
406 new_mark_begin = m_cursor_pos;
414 m_blink_start_time = porting::getTimeMs();
421 if (m_word_wrap || m_multiline) {
422 p = getLineFromPos(m_cursor_pos);
423 p = m_broken_text_positions[p];
426 if (event.KeyInput.Shift) {
427 if (m_mark_begin == m_mark_end)
428 new_mark_begin = m_cursor_pos;
435 m_blink_start_time = porting::getTimeMs();
442 calculateScrollPos();
443 sendGuiEvent(EGET_EDITBOX_ENTER);
448 if (event.KeyInput.Shift) {
449 if (m_cursor_pos > 0) {
450 if (m_mark_begin == m_mark_end)
451 new_mark_begin = m_cursor_pos;
453 new_mark_end = m_cursor_pos - 1;
460 if (m_cursor_pos > 0)
462 m_blink_start_time = porting::getTimeMs();
466 if (event.KeyInput.Shift) {
467 if (Text.size() > (u32)m_cursor_pos) {
468 if (m_mark_begin == m_mark_end)
469 new_mark_begin = m_cursor_pos;
471 new_mark_end = m_cursor_pos + 1;
478 if (Text.size() > (u32)m_cursor_pos)
480 m_blink_start_time = porting::getTimeMs();
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 : (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
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] + 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 new_mark_end = m_cursor_pos;
506 if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
507 s32 lineNo = getLineFromPos(m_cursor_pos);
508 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos : (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
509 if (lineNo < (s32)m_broken_text.size() - 1)
511 s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
512 if ((s32)m_broken_text[lineNo + 1].size() < cp)
513 m_cursor_pos = m_broken_text_positions[lineNo + 1] + core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
515 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
518 if (event.KeyInput.Shift) {
520 new_mark_end = m_cursor_pos;
538 if (m_mark_begin != m_mark_end) {
539 // delete marked text
540 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
541 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
543 s = Text.subString(0, realmbgn);
544 s.append(Text.subString(realmend, Text.size() - realmend));
547 m_cursor_pos = realmbgn;
549 // delete text behind cursor
550 if (m_cursor_pos > 0)
551 s = Text.subString(0, m_cursor_pos - 1);
554 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
559 if (m_cursor_pos < 0)
561 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
571 if (Text.size() != 0) {
574 if (m_mark_begin != m_mark_end) {
575 // delete marked text
576 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
577 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
579 s = Text.subString(0, realmbgn);
580 s.append(Text.subString(realmend, Text.size() - realmend));
583 m_cursor_pos = realmbgn;
585 // delete text before cursor
586 s = Text.subString(0, m_cursor_pos);
587 s.append(Text.subString(m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
591 if (m_cursor_pos > (s32)Text.size())
592 m_cursor_pos = (s32)Text.size();
594 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
632 inputChar(event.KeyInput.Char);
636 // Set new text markers
637 setTextMarkers(new_mark_begin, new_mark_end);
639 // break the text if it has changed
642 calculateScrollPos();
643 sendGuiEvent(EGET_EDITBOX_CHANGED);
647 calculateScrollPos();
654 //! draws the element and its children
655 void GUIEditBoxWithScrollBar::draw()
660 const bool focus = Environment->hasFocus(this);
662 IGUISkin* skin = Environment->getSkin();
666 video::SColor default_bg_color;
667 video::SColor bg_color;
669 default_bg_color = m_writable ? skin->getColor(EGDC_WINDOW) : video::SColor(0);
670 bg_color = m_bg_color_used ? m_bg_color : default_bg_color;
672 if (!m_border && m_background) {
673 skin->draw2DRectangle(this, bg_color, AbsoluteRect, &AbsoluteClippingRect);
681 skin->draw3DSunkenPane(this, bg_color, false, m_background,
682 AbsoluteRect, &AbsoluteClippingRect);
685 calculateFrameRect();
688 core::rect<s32> local_clip_rect = m_frame_rect;
689 local_clip_rect.clipAgainst(AbsoluteClippingRect);
693 IGUIFont* font = getActiveFont();
696 s32 charcursorpos = 0;
699 if (m_last_break_font != font) {
703 // calculate cursor pos
705 core::stringw *txt_line = &Text;
711 const bool ml = (!m_passwordbox && (m_word_wrap || m_multiline));
712 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
713 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
714 const s32 hline_start = ml ? getLineFromPos(realmbgn) : 0;
715 const s32 hline_count = ml ? getLineFromPos(realmend) - hline_start + 1 : 1;
716 const s32 line_count = ml ? m_broken_text.size() : 1;
718 // Save the override color information.
719 // Then, alter it if the edit box is disabled.
720 const bool prevOver = m_override_color_enabled;
721 const video::SColor prevColor = m_override_color;
724 if (!isEnabled() && !m_override_color_enabled) {
725 m_override_color_enabled = true;
726 m_override_color = skin->getColor(EGDC_GRAY_TEXT);
729 for (s32 i = 0; i < line_count; ++i) {
732 // clipping test - don't draw anything outside the visible area
733 core::rect<s32> c = local_clip_rect;
734 c.clipAgainst(m_current_text_rect);
740 if (m_broken_text.size() != 1) {
741 m_broken_text.clear();
742 m_broken_text.emplace_back();
744 if (m_broken_text[0].size() != Text.size()){
745 m_broken_text[0] = Text;
746 for (u32 q = 0; q < Text.size(); ++q)
748 m_broken_text[0][q] = m_passwordchar;
751 txt_line = &m_broken_text[0];
754 txt_line = ml ? &m_broken_text[i] : &Text;
755 start_pos = ml ? m_broken_text_positions[i] : 0;
760 font->draw(txt_line->c_str(), m_current_text_rect,
761 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
762 false, true, &local_clip_rect);
764 // draw mark and marked text
765 if (focus && m_mark_begin != m_mark_end && i >= hline_start && i < hline_start + hline_count) {
767 s32 mbegin = 0, mend = 0;
768 s32 lineStartPos = 0, lineEndPos = txt_line->size();
770 if (i == hline_start) {
771 // highlight start is on this line
772 s = txt_line->subString(0, realmbgn - start_pos);
773 mbegin = font->getDimension(s.c_str()).Width;
776 mbegin += font->getKerningWidth(
777 &((*txt_line)[realmbgn - start_pos]),
778 realmbgn - start_pos > 0 ? &((*txt_line)[realmbgn - start_pos - 1]) : 0);
780 lineStartPos = realmbgn - start_pos;
782 if (i == hline_start + hline_count - 1) {
783 // highlight end is on this line
784 s2 = txt_line->subString(0, realmend - start_pos);
785 mend = font->getDimension(s2.c_str()).Width;
786 lineEndPos = (s32)s2.size();
788 mend = font->getDimension(txt_line->c_str()).Width;
792 m_current_text_rect.UpperLeftCorner.X += mbegin;
793 m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
797 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &local_clip_rect);
800 s = txt_line->subString(lineStartPos, lineEndPos - lineStartPos);
803 font->draw(s.c_str(), m_current_text_rect,
804 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
805 false, true, &local_clip_rect);
810 // Return the override color information to its previous settings.
811 m_override_color_enabled = prevOver;
812 m_override_color = prevColor;
816 if (IsEnabled && m_writable) {
817 if (m_word_wrap || m_multiline) {
818 cursor_line = getLineFromPos(m_cursor_pos);
819 txt_line = &m_broken_text[cursor_line];
820 start_pos = m_broken_text_positions[cursor_line];
822 s = txt_line->subString(0, m_cursor_pos - start_pos);
823 charcursorpos = font->getDimension(s.c_str()).Width +
824 font->getKerningWidth(L"_", m_cursor_pos - start_pos > 0 ? &((*txt_line)[m_cursor_pos - start_pos - 1]) : 0);
826 if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
827 setTextRect(cursor_line);
828 m_current_text_rect.UpperLeftCorner.X += charcursorpos;
830 font->draw(L"_", m_current_text_rect,
831 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
832 false, true, &local_clip_rect);
842 //! Sets the new caption of this element.
843 void GUIEditBoxWithScrollBar::setText(const wchar_t* text)
846 if (u32(m_cursor_pos) > Text.size())
847 m_cursor_pos = Text.size();
853 //! Enables or disables automatic scrolling with cursor position
854 //! \param enable: If set to true, the text will move around with the cursor position
855 void GUIEditBoxWithScrollBar::setAutoScroll(bool enable)
857 m_autoscroll = enable;
861 //! Checks to see if automatic scrolling is enabled
862 //! \return true if automatic scrolling is enabled, false if not
863 bool GUIEditBoxWithScrollBar::isAutoScrollEnabled() const
869 //! Gets the area of the text in the edit box
870 //! \return Returns the size in pixels of the text
871 core::dimension2du GUIEditBoxWithScrollBar::getTextDimension()
876 ret = m_current_text_rect;
878 for (u32 i = 1; i < m_broken_text.size(); ++i) {
880 ret.addInternalPoint(m_current_text_rect.UpperLeftCorner);
881 ret.addInternalPoint(m_current_text_rect.LowerRightCorner);
884 return core::dimension2du(ret.getSize());
888 //! Sets the maximum amount of characters which may be entered in the box.
889 //! \param max: Maximum amount of characters. If 0, the character amount is
891 void GUIEditBoxWithScrollBar::setMax(u32 max)
895 if (Text.size() > m_max && m_max != 0)
896 Text = Text.subString(0, m_max);
900 //! Returns maximum amount of characters, previously set by setMax();
901 u32 GUIEditBoxWithScrollBar::getMax() const
907 bool GUIEditBoxWithScrollBar::processMouse(const SEvent& event)
909 switch (event.MouseInput.Event)
911 case irr::EMIE_LMOUSE_LEFT_UP:
912 if (Environment->hasFocus(this)) {
913 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
914 if (m_mouse_marking) {
915 setTextMarkers(m_mark_begin, m_cursor_pos);
917 m_mouse_marking = false;
918 calculateScrollPos();
922 case irr::EMIE_MOUSE_MOVED:
924 if (m_mouse_marking) {
925 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
926 setTextMarkers(m_mark_begin, m_cursor_pos);
927 calculateScrollPos();
932 case EMIE_LMOUSE_PRESSED_DOWN:
934 if (!Environment->hasFocus(this)) {
935 m_blink_start_time = porting::getTimeMs();
936 m_mouse_marking = true;
937 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
938 setTextMarkers(m_cursor_pos, m_cursor_pos);
939 calculateScrollPos();
942 if (!AbsoluteClippingRect.isPointInside(
943 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
947 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
949 s32 newMarkBegin = m_mark_begin;
950 if (!m_mouse_marking)
951 newMarkBegin = m_cursor_pos;
953 m_mouse_marking = true;
954 setTextMarkers(newMarkBegin, m_cursor_pos);
955 calculateScrollPos();
967 s32 GUIEditBoxWithScrollBar::getCursorPos(s32 x, s32 y)
969 IGUIFont* font = getActiveFont();
971 const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
973 core::stringw *txt_line = 0;
977 for (u32 i = 0; i < line_count; ++i) {
979 if (i == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
980 y = m_current_text_rect.UpperLeftCorner.Y;
981 if (i == line_count - 1 && y > m_current_text_rect.LowerRightCorner.Y)
982 y = m_current_text_rect.LowerRightCorner.Y;
984 // is it inside this region?
985 if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
986 // we've found the clicked line
987 txt_line = (m_word_wrap || m_multiline) ? &m_broken_text[i] : &Text;
988 start_pos = (m_word_wrap || m_multiline) ? m_broken_text_positions[i] : 0;
993 if (x < m_current_text_rect.UpperLeftCorner.X)
994 x = m_current_text_rect.UpperLeftCorner.X;
999 s32 idx = font->getCharacterFromPos(txt_line->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
1001 // click was on or left of the line
1003 return idx + start_pos;
1005 // click was off the right edge of the line, go to end.
1006 return txt_line->size() + start_pos;
1010 //! Breaks the single text line.
1011 void GUIEditBoxWithScrollBar::breakText()
1013 if ((!m_word_wrap && !m_multiline))
1016 m_broken_text.clear(); // need to reallocate :/
1017 m_broken_text_positions.clear();
1019 IGUIFont* font = getActiveFont();
1023 m_last_break_font = font;
1027 core::stringw whitespace;
1028 s32 last_line_start = 0;
1029 s32 size = Text.size();
1031 s32 el_width = RelativeRect.getWidth() - 6;
1034 for (s32 i = 0; i < size; ++i) {
1036 bool line_break = false;
1038 if (c == L'\r') { // Mac or Windows breaks
1042 if (Text[i + 1] == L'\n') { // Windows breaks
1043 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
1044 // Instead rework the cursor positioning to be able to handle this (but not in stable release
1045 // branch as users might already expect this behavior).
1048 if (m_cursor_pos > i)
1051 } else if (c == L'\n') { // Unix breaks
1056 // don't break if we're not a multi-line edit box
1060 if (c == L' ' || c == 0 || i == (size - 1)) {
1061 // here comes the next whitespace, look if
1062 // we can break the last word to the next line
1063 // We also break whitespace, otherwise cursor would vanish beside the right border.
1064 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1065 s32 worldlgth = font->getDimension(word.c_str()).Width;
1067 if (m_word_wrap && length + worldlgth + whitelgth > el_width && line.size() > 0) {
1068 // break to next line
1070 m_broken_text.push_back(line);
1071 m_broken_text_positions.push_back(last_line_start);
1072 last_line_start = i - (s32)word.size();
1078 length += whitelgth + worldlgth;
1088 // compute line break
1092 m_broken_text.push_back(line);
1093 m_broken_text_positions.push_back(last_line_start);
1094 last_line_start = i + 1;
1101 // yippee this is a word..
1108 m_broken_text.push_back(line);
1109 m_broken_text_positions.push_back(last_line_start);
1112 // TODO: that function does interpret VAlign according to line-index (indexed line is placed on top-center-bottom)
1113 // but HAlign according to line-width (pixels) and not by row.
1114 // Intuitively I suppose HAlign handling is better as VScrollPos should handle the line-scrolling.
1115 // But please no one change this without also rewriting (and this time fucking testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
1116 void GUIEditBoxWithScrollBar::setTextRect(s32 line)
1121 IGUIFont* font = getActiveFont();
1125 core::dimension2du d;
1127 // get text dimension
1128 const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
1129 if (m_word_wrap || m_multiline) {
1130 d = font->getDimension(m_broken_text[line].c_str());
1132 d = font->getDimension(Text.c_str());
1133 d.Height = AbsoluteRect.getHeight();
1135 d.Height += font->getKerningHeight();
1140 // align to h centre
1141 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth() / 2) - (d.Width / 2);
1142 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth() / 2) + (d.Width / 2);
1144 case EGUIA_LOWERRIGHT:
1145 // align to right edge
1146 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
1147 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
1150 // align to left edge
1151 m_current_text_rect.UpperLeftCorner.X = 0;
1152 m_current_text_rect.LowerRightCorner.X = d.Width;
1158 // align to v centre
1159 m_current_text_rect.UpperLeftCorner.Y =
1160 (m_frame_rect.getHeight() / 2) - (line_count*d.Height) / 2 + d.Height*line;
1162 case EGUIA_LOWERRIGHT:
1163 // align to bottom edge
1164 m_current_text_rect.UpperLeftCorner.Y =
1165 m_frame_rect.getHeight() - line_count*d.Height + d.Height*line;
1168 // align to top edge
1169 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
1173 m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
1174 m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
1175 m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
1176 m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
1178 m_current_text_rect += m_frame_rect.UpperLeftCorner;
1182 s32 GUIEditBoxWithScrollBar::getLineFromPos(s32 pos)
1184 if (!m_word_wrap && !m_multiline)
1188 while (i < (s32)m_broken_text_positions.size()) {
1189 if (m_broken_text_positions[i] > pos)
1193 return (s32)m_broken_text_positions.size() - 1;
1197 void GUIEditBoxWithScrollBar::inputChar(wchar_t c)
1203 if (Text.size() < m_max || m_max == 0) {
1206 if (m_mark_begin != m_mark_end) {
1207 // replace marked text
1208 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
1209 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
1211 s = Text.subString(0, realmbgn);
1213 s.append(Text.subString(realmend, Text.size() - realmend));
1215 m_cursor_pos = realmbgn + 1;
1217 // add new character
1218 s = Text.subString(0, m_cursor_pos);
1220 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
1225 m_blink_start_time = porting::getTimeMs();
1226 setTextMarkers(0, 0);
1230 calculateScrollPos();
1231 sendGuiEvent(EGET_EDITBOX_CHANGED);
1234 // calculate autoscroll
1235 void GUIEditBoxWithScrollBar::calculateScrollPos()
1240 IGUISkin* skin = Environment->getSkin();
1243 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
1247 s32 curs_line = getLineFromPos(m_cursor_pos);
1250 setTextRect(curs_line);
1251 const bool has_broken_text = m_multiline || m_word_wrap;
1253 // Check horizonal scrolling
1254 // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
1256 // get cursor position
1257 IGUIFont* font = getActiveFont();
1262 irr::u32 cursor_width = font->getDimension(L"_").Width;
1263 core::stringw *txt_line = has_broken_text ? &m_broken_text[curs_line] : &Text;
1264 s32 cpos = has_broken_text ? m_cursor_pos - m_broken_text_positions[curs_line] : m_cursor_pos; // column
1265 s32 cstart = font->getDimension(txt_line->subString(0, cpos).c_str()).Width; // pixels from text-start
1266 s32 cend = cstart + cursor_width;
1267 s32 txt_width = font->getDimension(txt_line->c_str()).Width;
1269 if (txt_width < m_frame_rect.getWidth()) {
1270 // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
1271 // This check just fixes the case where it was most noticable (text smaller than clipping area).
1274 setTextRect(curs_line);
1277 if (m_current_text_rect.UpperLeftCorner.X + cstart < m_frame_rect.UpperLeftCorner.X) {
1278 // cursor to the left of the clipping area
1279 m_hscroll_pos -= m_frame_rect.UpperLeftCorner.X - (m_current_text_rect.UpperLeftCorner.X + cstart);
1280 setTextRect(curs_line);
1282 // TODO: should show more characters to the left when we're scrolling left
1283 // and the cursor reaches the border.
1284 } else if (m_current_text_rect.UpperLeftCorner.X + cend > m_frame_rect.LowerRightCorner.X) {
1285 // cursor to the right of the clipping area
1286 m_hscroll_pos += (m_current_text_rect.UpperLeftCorner.X + cend) - m_frame_rect.LowerRightCorner.X;
1287 setTextRect(curs_line);
1291 // calculate vertical scrolling
1292 if (has_broken_text) {
1293 irr::u32 line_height = font->getDimension(L"A").Height + font->getKerningHeight();
1294 // only up to 1 line fits?
1295 if (line_height >= (irr::u32)m_frame_rect.getHeight()) {
1297 setTextRect(curs_line);
1298 s32 unscrolledPos = m_current_text_rect.UpperLeftCorner.Y;
1299 s32 pivot = m_frame_rect.UpperLeftCorner.Y;
1302 pivot += m_frame_rect.getHeight() / 2;
1303 unscrolledPos += line_height / 2;
1305 case EGUIA_LOWERRIGHT:
1306 pivot += m_frame_rect.getHeight();
1307 unscrolledPos += line_height;
1312 m_vscroll_pos = unscrolledPos - pivot;
1313 setTextRect(curs_line);
1315 // First 2 checks are necessary when people delete lines
1317 if (m_current_text_rect.UpperLeftCorner.Y > m_frame_rect.UpperLeftCorner.Y && m_valign != EGUIA_LOWERRIGHT) {
1318 // first line is leaving a gap on top
1320 } else if (m_valign != EGUIA_UPPERLEFT) {
1321 u32 lastLine = m_broken_text_positions.empty() ? 0 : m_broken_text_positions.size() - 1;
1322 setTextRect(lastLine);
1323 if (m_current_text_rect.LowerRightCorner.Y < m_frame_rect.LowerRightCorner.Y)
1325 // last line is leaving a gap on bottom
1326 m_vscroll_pos -= m_frame_rect.LowerRightCorner.Y - m_current_text_rect.LowerRightCorner.Y;
1330 setTextRect(curs_line);
1331 if (m_current_text_rect.UpperLeftCorner.Y < m_frame_rect.UpperLeftCorner.Y) {
1332 // text above valid area
1333 m_vscroll_pos -= m_frame_rect.UpperLeftCorner.Y - m_current_text_rect.UpperLeftCorner.Y;
1334 setTextRect(curs_line);
1335 } else if (m_current_text_rect.LowerRightCorner.Y > m_frame_rect.LowerRightCorner.Y){
1336 // text below valid area
1337 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y;
1338 setTextRect(curs_line);
1344 m_vscrollbar->setPos(m_vscroll_pos);
1348 void GUIEditBoxWithScrollBar::calculateFrameRect()
1350 m_frame_rect = AbsoluteRect;
1355 skin = Environment->getSkin();
1356 if (m_border && skin) {
1357 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1358 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1359 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1360 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1366 //! set text markers
1367 void GUIEditBoxWithScrollBar::setTextMarkers(s32 begin, s32 end)
1369 if (begin != m_mark_begin || end != m_mark_end) {
1370 m_mark_begin = begin;
1372 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1376 //! send some gui event to parent
1377 void GUIEditBoxWithScrollBar::sendGuiEvent(EGUI_EVENT_TYPE type)
1381 e.EventType = EET_GUI_EVENT;
1382 e.GUIEvent.Caller = this;
1383 e.GUIEvent.Element = 0;
1384 e.GUIEvent.EventType = type;
1390 //! create a vertical scroll bar
1391 void GUIEditBoxWithScrollBar::createVScrollBar()
1395 skin = Environment->getSkin();
1397 m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
1399 RelativeRect.LowerRightCorner.X -= m_scrollbar_width + 4;
1401 irr::core::rect<s32> scrollbarrect = m_frame_rect;
1402 scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
1403 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1404 m_vscrollbar->setVisible(false);
1405 m_vscrollbar->setSmallStep(1);
1406 m_vscrollbar->setLargeStep(1);
1409 void GUIEditBoxWithScrollBar::updateVScrollBar()
1411 if (!m_vscrollbar) {
1415 // OnScrollBarChanged(...)
1416 if (m_vscrollbar->getPos() != m_vscroll_pos) {
1417 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
1418 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
1419 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
1421 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1422 if (scrollymax != m_vscrollbar->getMax()) {
1423 // manage a newline or a deleted line
1424 m_vscrollbar->setMax(scrollymax);
1425 calculateScrollPos();
1427 // manage a newline or a deleted line
1428 m_vscroll_pos = m_vscrollbar->getPos();
1432 // check if a vertical scrollbar is needed ?
1433 if (getTextDimension().Height > (u32) m_frame_rect.getHeight()) {
1434 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
1436 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1437 if (scrollymax != m_vscrollbar->getMax()) {
1438 m_vscrollbar->setMax(scrollymax);
1441 if (!m_vscrollbar->isVisible()) {
1442 m_vscrollbar->setVisible(true);
1445 if (m_vscrollbar->isVisible())
1447 m_vscrollbar->setVisible(false);
1449 m_vscrollbar->setPos(0);
1450 m_vscrollbar->setMax(1);
1457 //! set true if this editbox is writable
1458 void GUIEditBoxWithScrollBar::setWritable(bool writable)
1460 m_writable = writable;
1463 //! Change the background color
1464 void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
1466 m_bg_color = bg_color;
1467 m_bg_color_used = true;
1470 //! Writes attributes of the element.
1471 void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
1473 // IGUIEditBox::serializeAttributes(out,options);
1475 out->addBool("Border", m_border);
1476 out->addBool("Background", m_background);
1477 out->addBool("OverrideColorEnabled", m_override_color_enabled);
1478 out->addColor("OverrideColor", m_override_color);
1479 // out->addFont("OverrideFont", OverrideFont);
1480 out->addInt("MaxChars", m_max);
1481 out->addBool("WordWrap", m_word_wrap);
1482 out->addBool("MultiLine", m_multiline);
1483 out->addBool("AutoScroll", m_autoscroll);
1484 out->addBool("PasswordBox", m_passwordbox);
1485 core::stringw ch = L" ";
1486 ch[0] = m_passwordchar;
1487 out->addString("PasswordChar", ch.c_str());
1488 out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
1489 out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
1490 out->addBool("Writable", m_writable);
1492 IGUIEditBox::serializeAttributes(out, options);
1496 //! Reads attributes of the element
1497 void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
1499 IGUIEditBox::deserializeAttributes(in, options);
1501 setDrawBorder(in->getAttributeAsBool("Border"));
1502 setDrawBackground(in->getAttributeAsBool("Background"));
1503 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1504 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1505 setMax(in->getAttributeAsInt("MaxChars"));
1506 setWordWrap(in->getAttributeAsBool("WordWrap"));
1507 setMultiLine(in->getAttributeAsBool("MultiLine"));
1508 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1509 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1512 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1514 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1516 setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1517 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1519 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1520 setWritable(in->getAttributeAsBool("Writable"));
1523 bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }
1524 bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; }
1525 void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { }
1526 wchar_t GUIEditBoxWithScrollBar::getCursorChar() const { return '|'; }
1527 void GUIEditBoxWithScrollBar::setCursorBlinkTime(irr::u32 timeMs) { }
1528 irr::u32 GUIEditBoxWithScrollBar::getCursorBlinkTime() const { return 500; }