1 // 11.11.2011 11:11 ValkaTR
3 // This is a copy of intlGUIEditBox from the irrlicht, but with a
4 // fix in the OnEvent function, which doesn't allowed input of
5 // other keyboard layouts than latin-1
7 // Characters like: ä ö ü õ ы й ю я ъ № € ° ...
9 // This fix is only needed for linux, because of a bug
10 // in the CIrrDeviceLinux.cpp:1014-1015 of the irrlicht
12 // Also locale in the programm should not be changed to
13 // a "C", "POSIX" or whatever, it should be set to "",
14 // or XLookupString will return nothing for the international
17 // From the "man setlocale":
19 // On startup of the main program, the portable "C" locale
20 // is selected as default. A program may be made
21 // portable to all locales by calling:
23 // setlocale(LC_ALL, "");
25 // after program initialization....
28 // Copyright (C) 2002-2013 Nikolaus Gebhardt
29 // This file is part of the "Irrlicht Engine".
30 // For conditions of distribution and use, see copyright notice in irrlicht.h
32 #include <util/numeric.h>
33 #include "intlGUIEditBox.h"
35 #if defined(_IRR_COMPILE_WITH_GUI_) && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9 || defined(__ANDROID__)
38 #include "IGUIEnvironment.h"
40 #include "IVideoDriver.h"
41 //#include "irrlicht/os.cpp"
43 //#include "Keycodes.h"
49 ctrl+left/right to select word
50 double click/ctrl click: word select + drag to select whole words, triple click to select line
51 optional? dragging selected text
61 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
62 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
63 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
64 : IGUIEditBox(environment, parent, id, rectangle),
65 Border(border), FrameRect(rectangle),
66 m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable)
69 setDebugName("intlintlGUIEditBox");
75 Operator = Environment->getOSOperator();
80 // this element can be tabbed to
86 skin = Environment->getSkin();
89 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
90 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
91 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
92 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
95 if (skin && has_vscrollbar) {
96 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
98 if (m_scrollbar_width > 0) {
105 calculateScrollPos();
106 setWritable(writable);
111 intlGUIEditBox::~intlGUIEditBox()
114 OverrideFont->drop();
121 //! Sets another skin independent font.
122 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
124 if (OverrideFont == font)
128 OverrideFont->drop();
133 OverrideFont->grab();
138 IGUIFont * intlGUIEditBox::getOverrideFont() const
143 //! Get the font which is used right now for drawing
144 IGUIFont* intlGUIEditBox::getActiveFont() const
148 IGUISkin* skin = Environment->getSkin();
150 return skin->getFont();
154 //! Sets another color for the text.
155 void intlGUIEditBox::setOverrideColor(video::SColor color)
157 OverrideColor = color;
158 OverrideColorEnabled = true;
161 video::SColor intlGUIEditBox::getOverrideColor() const
163 return OverrideColor;
166 //! Turns the border on or off
167 void intlGUIEditBox::setDrawBorder(bool border)
172 //! Sets whether to draw the background
173 void intlGUIEditBox::setDrawBackground(bool draw)
177 //! Sets if the text should use the overide color or the color in the gui skin.
178 void intlGUIEditBox::enableOverrideColor(bool enable)
180 OverrideColorEnabled = enable;
183 bool intlGUIEditBox::isOverrideColorEnabled() const
185 return OverrideColorEnabled;
188 //! Enables or disables word wrap
189 void intlGUIEditBox::setWordWrap(bool enable)
196 void intlGUIEditBox::updateAbsolutePosition()
198 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
199 IGUIElement::updateAbsolutePosition();
200 if ( oldAbsoluteRect != AbsoluteRect )
207 //! Checks if word wrap is enabled
208 bool intlGUIEditBox::isWordWrapEnabled() const
214 //! Enables or disables newlines.
215 void intlGUIEditBox::setMultiLine(bool enable)
221 //! Checks if multi line editing is enabled
222 bool intlGUIEditBox::isMultiLineEnabled() const
228 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
230 PasswordBox = passwordBox;
233 PasswordChar = passwordChar;
241 bool intlGUIEditBox::isPasswordBox() const
247 //! Sets text justification
248 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
255 //! called if an event happened.
256 bool intlGUIEditBox::OnEvent(const SEvent& event)
261 switch(event.EventType)
264 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
266 if (event.GUIEvent.Caller == this)
268 MouseMarking = false;
273 case EET_KEY_INPUT_EVENT:
275 #if (defined(__linux__) || defined(__FreeBSD__))
276 // ################################################################
278 // This part is the difference from the original intlGUIEditBox
279 // It converts UTF-8 character into a UCS-2 (wchar_t)
281 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
283 //printf( "char: %lc (%u) \r\n", wc, wc );
285 SEvent irrevent(event);
286 irrevent.KeyInput.Char = wc;
287 // ################################################################
289 if (processKey(irrevent))
292 if (processKey(event))
294 #endif // defined(linux)
298 case EET_MOUSE_INPUT_EVENT:
299 if (processMouse(event))
307 return IGUIElement::OnEvent(event);
311 bool intlGUIEditBox::processKey(const SEvent& event)
313 if (!event.KeyInput.PressedDown)
316 bool textChanged = false;
317 s32 newMarkBegin = MarkBegin;
318 s32 newMarkEnd = MarkEnd;
320 // control shortcut handling
322 if (event.KeyInput.Control)
324 // german backlash '\' entered with control + '?'
325 if ( event.KeyInput.Char == '\\' )
327 inputChar(event.KeyInput.Char);
331 switch(event.KeyInput.Key)
336 newMarkEnd = Text.size();
340 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
342 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
343 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
346 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
347 Operator->copyToClipboard(s.c_str());
351 // cut to the clipboard
352 if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
353 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
354 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
358 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
359 Operator->copyToClipboard(sc.c_str());
361 if (IsEnabled && m_writable) {
364 s = Text.subString(0, realmbgn);
365 s.append( Text.subString(realmend, Text.size()-realmend) );
368 CursorPos = realmbgn;
376 if (!IsEnabled || !m_writable)
379 // paste from the clipboard
382 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
383 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
386 const c8* p = Operator->getTextFromClipboard();
389 if (MarkBegin == MarkEnd)
392 core::stringw s = Text.subString(0, CursorPos);
394 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
396 if (!Max || s.size()<=Max) // thx to Fish FH for fix
400 CursorPos += s.size();
407 core::stringw s = Text.subString(0, realmbgn);
409 s.append( Text.subString(realmend, Text.size()-realmend) );
411 if (!Max || s.size()<=Max) // thx to Fish FH for fix
415 CursorPos = realmbgn + s.size();
426 // move/highlight to start of text
427 if (event.KeyInput.Shift)
429 newMarkEnd = CursorPos;
441 // move/highlight to end of text
442 if (event.KeyInput.Shift)
444 newMarkBegin = CursorPos;
445 newMarkEnd = Text.size();
450 CursorPos = Text.size();
459 // default keyboard handling
461 switch(event.KeyInput.Key)
466 if (WordWrap || MultiLine)
468 p = getLineFromPos(CursorPos);
469 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
470 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
474 if (event.KeyInput.Shift)
476 if (MarkBegin == MarkEnd)
477 newMarkBegin = CursorPos;
487 BlinkStartTime = porting::getTimeMs();
494 if (WordWrap || MultiLine)
496 p = getLineFromPos(CursorPos);
497 p = BrokenTextPositions[p];
500 if (event.KeyInput.Shift)
502 if (MarkBegin == MarkEnd)
503 newMarkBegin = CursorPos;
512 BlinkStartTime = porting::getTimeMs();
523 sendGuiEvent( EGET_EDITBOX_ENTER );
528 if (event.KeyInput.Shift)
532 if (MarkBegin == MarkEnd)
533 newMarkBegin = CursorPos;
535 newMarkEnd = CursorPos-1;
544 if (CursorPos > 0) CursorPos--;
545 BlinkStartTime = porting::getTimeMs();
549 if (event.KeyInput.Shift)
551 if (Text.size() > (u32)CursorPos)
553 if (MarkBegin == MarkEnd)
554 newMarkBegin = CursorPos;
556 newMarkEnd = CursorPos+1;
565 if (Text.size() > (u32)CursorPos) CursorPos++;
566 BlinkStartTime = porting::getTimeMs();
569 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
571 s32 lineNo = getLineFromPos(CursorPos);
572 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
575 s32 cp = CursorPos - BrokenTextPositions[lineNo];
576 if ((s32)BrokenText[lineNo-1].size() < cp)
577 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
579 CursorPos = BrokenTextPositions[lineNo-1] + cp;
582 if (event.KeyInput.Shift)
585 newMarkEnd = CursorPos;
600 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
602 s32 lineNo = getLineFromPos(CursorPos);
603 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
604 if (lineNo < (s32)BrokenText.size()-1)
606 s32 cp = CursorPos - BrokenTextPositions[lineNo];
607 if ((s32)BrokenText[lineNo+1].size() < cp)
608 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
610 CursorPos = BrokenTextPositions[lineNo+1] + cp;
613 if (event.KeyInput.Shift)
616 newMarkEnd = CursorPos;
632 if (!this->IsEnabled || !m_writable)
638 if (MarkBegin != MarkEnd)
640 // delete marked text
641 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
642 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
644 s = Text.subString(0, realmbgn);
645 s.append( Text.subString(realmend, Text.size()-realmend) );
648 CursorPos = realmbgn;
652 // delete text behind cursor
654 s = Text.subString(0, CursorPos-1);
657 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
664 BlinkStartTime = porting::getTimeMs();
671 if (!this->IsEnabled || !m_writable)
677 if (MarkBegin != MarkEnd)
679 // delete marked text
680 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
681 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
683 s = Text.subString(0, realmbgn);
684 s.append( Text.subString(realmend, Text.size()-realmend) );
687 CursorPos = realmbgn;
691 // delete text before cursor
692 s = Text.subString(0, CursorPos);
693 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
697 if (CursorPos > (s32)Text.size())
698 CursorPos = (s32)Text.size();
700 BlinkStartTime = porting::getTimeMs();
738 inputChar(event.KeyInput.Char);
742 // Set new text markers
743 setTextMarkers( newMarkBegin, newMarkEnd );
745 // break the text if it has changed
749 sendGuiEvent(EGET_EDITBOX_CHANGED);
752 calculateScrollPos();
758 //! draws the element and its children
759 void intlGUIEditBox::draw()
764 const bool focus = Environment->hasFocus(this);
766 IGUISkin* skin = Environment->getSkin();
770 FrameRect = AbsoluteRect;
777 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
778 false, true, FrameRect, &AbsoluteClippingRect);
781 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
782 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
783 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
784 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
788 core::rect<s32> localClipRect = FrameRect;
789 localClipRect.clipAgainst(AbsoluteClippingRect);
793 IGUIFont* font = OverrideFont;
795 font = skin->getFont();
798 s32 charcursorpos = 0;
802 if (LastBreakFont != font)
807 // calculate cursor pos
809 core::stringw *txtLine = &Text;
815 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
816 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
817 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
818 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
819 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
820 const s32 lineCount = ml ? BrokenText.size() : 1;
822 // Save the override color information.
823 // Then, alter it if the edit box is disabled.
824 const bool prevOver = OverrideColorEnabled;
825 const video::SColor prevColor = OverrideColor;
828 if (!IsEnabled && !OverrideColorEnabled)
830 OverrideColorEnabled = true;
831 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
834 for (s32 i=0; i < lineCount; ++i)
838 // clipping test - don't draw anything outside the visible area
839 core::rect<s32> c = localClipRect;
840 c.clipAgainst(CurrentTextRect);
847 if (BrokenText.size() != 1)
850 BrokenText.push_back(core::stringw());
852 if (BrokenText[0].size() != Text.size())
854 BrokenText[0] = Text;
855 for (u32 q = 0; q < Text.size(); ++q)
857 BrokenText[0] [q] = PasswordChar;
860 txtLine = &BrokenText[0];
865 txtLine = ml ? &BrokenText[i] : &Text;
866 startPos = ml ? BrokenTextPositions[i] : 0;
871 font->draw(txtLine->c_str(), CurrentTextRect,
872 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
873 false, true, &localClipRect);
875 // draw mark and marked text
876 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
879 s32 mbegin = 0, mend = 0;
880 s32 lineStartPos = 0, lineEndPos = txtLine->size();
884 // highlight start is on this line
885 s = txtLine->subString(0, realmbgn - startPos);
886 mbegin = font->getDimension(s.c_str()).Width;
889 mbegin += font->getKerningWidth(
890 &((*txtLine)[realmbgn - startPos]),
891 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
893 lineStartPos = realmbgn - startPos;
895 if (i == hlineStart + hlineCount - 1)
897 // highlight end is on this line
898 s2 = txtLine->subString(0, realmend - startPos);
899 mend = font->getDimension(s2.c_str()).Width;
900 lineEndPos = (s32)s2.size();
903 mend = font->getDimension(txtLine->c_str()).Width;
905 CurrentTextRect.UpperLeftCorner.X += mbegin;
906 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
909 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
912 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
915 font->draw(s.c_str(), CurrentTextRect,
916 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
917 false, true, &localClipRect);
922 // Return the override color information to its previous settings.
923 OverrideColorEnabled = prevOver;
924 OverrideColor = prevColor;
929 if (WordWrap || MultiLine)
931 cursorLine = getLineFromPos(CursorPos);
932 txtLine = &BrokenText[cursorLine];
933 startPos = BrokenTextPositions[cursorLine];
935 s = txtLine->subString(0,CursorPos-startPos);
936 charcursorpos = font->getDimension(s.c_str()).Width +
937 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
940 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350) {
941 setTextRect(cursorLine);
942 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
944 font->draw(L"_", CurrentTextRect,
945 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
946 false, true, &localClipRect);
956 //! Sets the new caption of this element.
957 void intlGUIEditBox::setText(const wchar_t* text)
960 if (u32(CursorPos) > Text.size())
961 CursorPos = Text.size();
967 //! Enables or disables automatic scrolling with cursor position
968 //! \param enable: If set to true, the text will move around with the cursor position
969 void intlGUIEditBox::setAutoScroll(bool enable)
975 //! Checks to see if automatic scrolling is enabled
976 //! \return true if automatic scrolling is enabled, false if not
977 bool intlGUIEditBox::isAutoScrollEnabled() const
983 //! Gets the area of the text in the edit box
984 //! \return Returns the size in pixels of the text
985 core::dimension2du intlGUIEditBox::getTextDimension()
990 ret = CurrentTextRect;
992 for (u32 i=1; i < BrokenText.size(); ++i)
995 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
996 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
999 return core::dimension2du(ret.getSize());
1003 //! Sets the maximum amount of characters which may be entered in the box.
1004 //! \param max: Maximum amount of characters. If 0, the character amount is
1006 void intlGUIEditBox::setMax(u32 max)
1010 if (Text.size() > Max && Max != 0)
1011 Text = Text.subString(0, Max);
1015 //! Returns maximum amount of characters, previously set by setMax();
1016 u32 intlGUIEditBox::getMax() const
1022 bool intlGUIEditBox::processMouse(const SEvent& event)
1024 switch(event.MouseInput.Event)
1026 case irr::EMIE_LMOUSE_LEFT_UP:
1027 if (Environment->hasFocus(this))
1029 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1032 setTextMarkers( MarkBegin, CursorPos );
1034 MouseMarking = false;
1035 calculateScrollPos();
1039 case irr::EMIE_MOUSE_MOVED:
1043 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1044 setTextMarkers( MarkBegin, CursorPos );
1045 calculateScrollPos();
1050 case EMIE_LMOUSE_PRESSED_DOWN:
1051 if (!Environment->hasFocus(this))
1053 BlinkStartTime = porting::getTimeMs();
1054 MouseMarking = true;
1055 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1056 setTextMarkers(CursorPos, CursorPos );
1057 calculateScrollPos();
1062 if (!AbsoluteClippingRect.isPointInside(
1063 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
1069 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1071 s32 newMarkBegin = MarkBegin;
1073 newMarkBegin = CursorPos;
1075 MouseMarking = true;
1076 setTextMarkers( newMarkBegin, CursorPos);
1077 calculateScrollPos();
1081 case EMIE_MOUSE_WHEEL:
1082 if (m_vscrollbar && m_vscrollbar->isVisible()) {
1083 s32 pos = m_vscrollbar->getPos();
1084 s32 step = m_vscrollbar->getSmallStep();
1085 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
1096 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1098 IGUIFont* font = OverrideFont;
1099 IGUISkin* skin = Environment->getSkin();
1101 font = skin->getFont();
1103 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1105 core::stringw *txtLine = NULL;
1107 u32 curr_line_idx = 0;
1110 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1111 setTextRect(curr_line_idx);
1112 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1113 y = CurrentTextRect.UpperLeftCorner.Y;
1114 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1115 y = CurrentTextRect.LowerRightCorner.Y;
1117 // is it inside this region?
1118 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1119 // we've found the clicked line
1120 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1121 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1126 if (x < CurrentTextRect.UpperLeftCorner.X)
1127 x = CurrentTextRect.UpperLeftCorner.X;
1128 else if (x > CurrentTextRect.LowerRightCorner.X)
1129 x = CurrentTextRect.LowerRightCorner.X;
1131 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1132 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1133 // will be the last char, not null char of the wstring
1134 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1137 return rangelim(idx + startPos, 0, S32_MAX);
1141 //! Breaks the single text line.
1142 void intlGUIEditBox::breakText()
1144 IGUISkin* skin = Environment->getSkin();
1146 if ((!WordWrap && !MultiLine) || !skin)
1149 BrokenText.clear(); // need to reallocate :/
1150 BrokenTextPositions.set_used(0);
1152 IGUIFont* font = OverrideFont;
1154 font = skin->getFont();
1159 LastBreakFont = font;
1163 core::stringw whitespace;
1164 s32 lastLineStart = 0;
1165 s32 size = Text.size();
1167 s32 elWidth = RelativeRect.getWidth() - 6;
1170 for (s32 i=0; i<size; ++i)
1173 bool lineBreak = false;
1175 if (c == L'\r') // Mac or Windows breaks
1179 if (Text[i+1] == L'\n') // Windows breaks
1185 else if (c == L'\n') // Unix breaks
1191 // don't break if we're not a multi-line edit box
1195 if (c == L' ' || c == 0 || i == (size-1))
1197 if (!word.empty()) {
1198 // here comes the next whitespace, look if
1199 // we can break the last word to the next line.
1200 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1201 s32 worldlgth = font->getDimension(word.c_str()).Width;
1203 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1205 // break to next line
1207 BrokenText.push_back(line);
1208 BrokenTextPositions.push_back(lastLineStart);
1209 lastLineStart = i - (s32)word.size();
1217 length += whitelgth + worldlgth;
1226 // compute line break
1231 BrokenText.push_back(line);
1232 BrokenTextPositions.push_back(lastLineStart);
1233 lastLineStart = i+1;
1242 // yippee this is a word..
1249 BrokenText.push_back(line);
1250 BrokenTextPositions.push_back(lastLineStart);
1254 void intlGUIEditBox::setTextRect(s32 line)
1256 core::dimension2du d;
1258 IGUISkin* skin = Environment->getSkin();
1262 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1267 // get text dimension
1268 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1269 if (WordWrap || MultiLine)
1271 d = font->getDimension(BrokenText[line].c_str());
1275 d = font->getDimension(Text.c_str());
1276 d.Height = AbsoluteRect.getHeight();
1278 d.Height += font->getKerningHeight();
1284 // align to h centre
1285 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1286 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1288 case EGUIA_LOWERRIGHT:
1289 // align to right edge
1290 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1291 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1294 // align to left edge
1295 CurrentTextRect.UpperLeftCorner.X = 0;
1296 CurrentTextRect.LowerRightCorner.X = d.Width;
1303 // align to v centre
1304 CurrentTextRect.UpperLeftCorner.Y =
1305 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1307 case EGUIA_LOWERRIGHT:
1308 // align to bottom edge
1309 CurrentTextRect.UpperLeftCorner.Y =
1310 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1313 // align to top edge
1314 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1318 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1319 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1320 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1321 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1323 CurrentTextRect += FrameRect.UpperLeftCorner;
1328 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1330 if (!WordWrap && !MultiLine)
1334 while (i < (s32)BrokenTextPositions.size())
1336 if (BrokenTextPositions[i] > pos)
1340 return (s32)BrokenTextPositions.size() - 1;
1344 void intlGUIEditBox::inputChar(wchar_t c)
1346 if (!IsEnabled || !m_writable)
1351 if (Text.size() < Max || Max == 0)
1355 if (MarkBegin != MarkEnd)
1357 // replace marked text
1358 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1359 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1361 s = Text.subString(0, realmbgn);
1363 s.append( Text.subString(realmend, Text.size()-realmend) );
1365 CursorPos = realmbgn+1;
1369 // add new character
1370 s = Text.subString(0, CursorPos);
1372 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1377 BlinkStartTime = porting::getTimeMs();
1378 setTextMarkers(0, 0);
1382 sendGuiEvent(EGET_EDITBOX_CHANGED);
1383 calculateScrollPos();
1387 void intlGUIEditBox::calculateScrollPos()
1392 // calculate horizontal scroll position
1393 s32 cursLine = getLineFromPos(CursorPos);
1394 setTextRect(cursLine);
1396 // don't do horizontal scrolling when wordwrap is enabled.
1399 // get cursor position
1400 IGUISkin* skin = Environment->getSkin();
1403 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1407 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1408 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1410 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1411 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1413 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1415 if (FrameRect.LowerRightCorner.X < cEnd)
1416 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1417 else if (FrameRect.UpperLeftCorner.X > cStart)
1418 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1422 // todo: adjust scrollbar
1425 if (!WordWrap && !MultiLine)
1428 // vertical scroll position
1429 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y)
1430 VScrollPos += CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y; // scrolling downwards
1431 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y)
1432 VScrollPos += CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y; // scrolling upwards
1434 // todo: adjust scrollbar
1436 m_vscrollbar->setPos(VScrollPos);
1439 //! set text markers
1440 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1442 if ( begin != MarkBegin || end != MarkEnd )
1446 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1450 //! send some gui event to parent
1451 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1456 e.EventType = EET_GUI_EVENT;
1457 e.GUIEvent.Caller = this;
1458 e.GUIEvent.Element = 0;
1459 e.GUIEvent.EventType = type;
1465 //! Create a vertical scrollbar
1466 void intlGUIEditBox::createVScrollBar()
1471 fontHeight = OverrideFont->getDimension(L"").Height;
1473 if (IGUISkin* skin = Environment->getSkin()) {
1474 if (IGUIFont* font = skin->getFont()) {
1475 fontHeight = font->getDimension(L"").Height;
1480 RelativeRect.LowerRightCorner.X -= m_scrollbar_width + 4;
1482 irr::core::rect<s32> scrollbarrect = FrameRect;
1483 scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
1484 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1485 m_vscrollbar->setVisible(false);
1486 m_vscrollbar->setSmallStep(3 * fontHeight);
1487 m_vscrollbar->setLargeStep(10 * fontHeight);
1490 //! Update the vertical scrollbar (visibilty & scroll position)
1491 void intlGUIEditBox::updateVScrollBar()
1496 // OnScrollBarChanged(...)
1497 if (m_vscrollbar->getPos() != VScrollPos) {
1498 s32 deltaScrollY = m_vscrollbar->getPos() - VScrollPos;
1499 CurrentTextRect.UpperLeftCorner.Y -= deltaScrollY;
1500 CurrentTextRect.LowerRightCorner.Y -= deltaScrollY;
1502 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1503 if (scrollymax != m_vscrollbar->getMax()) {
1504 // manage a newline or a deleted line
1505 m_vscrollbar->setMax(scrollymax);
1506 calculateScrollPos();
1508 // manage a newline or a deleted line
1509 VScrollPos = m_vscrollbar->getPos();
1513 // check if a vertical scrollbar is needed ?
1514 if (getTextDimension().Height > (u32) FrameRect.getHeight()) {
1515 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1516 if (scrollymax != m_vscrollbar->getMax()) {
1517 m_vscrollbar->setMax(scrollymax);
1520 if (!m_vscrollbar->isVisible() && MultiLine) {
1521 AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
1523 m_vscrollbar->setVisible(true);
1526 if (m_vscrollbar->isVisible()) {
1527 AbsoluteRect.LowerRightCorner.X += m_scrollbar_width;
1530 m_vscrollbar->setPos(0);
1531 m_vscrollbar->setMax(1);
1532 m_vscrollbar->setVisible(false);
1537 void intlGUIEditBox::setWritable(bool can_write_text)
1539 m_writable = can_write_text;
1542 //! Writes attributes of the element.
1543 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1545 // IGUIEditBox::serializeAttributes(out,options);
1547 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1548 out->addColor ("OverrideColor", OverrideColor);
1549 // out->addFont("OverrideFont",OverrideFont);
1550 out->addInt ("MaxChars", Max);
1551 out->addBool ("WordWrap", WordWrap);
1552 out->addBool ("MultiLine", MultiLine);
1553 out->addBool ("AutoScroll", AutoScroll);
1554 out->addBool ("PasswordBox", PasswordBox);
1555 core::stringw ch = L" ";
1556 ch[0] = PasswordChar;
1557 out->addString("PasswordChar", ch.c_str());
1558 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1559 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1560 out->addBool ("Writable", m_writable);
1562 IGUIEditBox::serializeAttributes(out,options);
1566 //! Reads attributes of the element
1567 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1569 IGUIEditBox::deserializeAttributes(in,options);
1571 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1572 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1573 setMax(in->getAttributeAsInt("MaxChars"));
1574 setWordWrap(in->getAttributeAsBool("WordWrap"));
1575 setMultiLine(in->getAttributeAsBool("MultiLine"));
1576 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1577 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1580 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1582 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1584 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1585 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1587 setWritable(in->getAttributeAsBool("Writable"));
1588 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1592 } // end namespace gui
1593 } // end namespace irr
1595 #endif // _IRR_COMPILE_WITH_GUI_