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
38 #include "IGUIEnvironment.h"
40 #include "IVideoDriver.h"
42 //#include "irrlicht/os.cpp"
44 //#include "Keycodes.h"
50 ctrl+left/right to select word
51 double click/ctrl click: word select + drag to select whole words, triple click to select line
52 optional? dragging selected text
62 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
63 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
64 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
65 : IGUIEditBox(environment, parent, id, rectangle),
66 Border(border), FrameRect(rectangle),
67 m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable)
70 setDebugName("intlintlGUIEditBox");
76 Operator = Environment->getOSOperator();
81 // this element can be tabbed to
87 skin = Environment->getSkin();
90 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
91 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
92 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
93 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
96 if (skin && has_vscrollbar) {
97 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
99 if (m_scrollbar_width > 0) {
106 calculateScrollPos();
107 setWritable(writable);
112 intlGUIEditBox::~intlGUIEditBox()
115 OverrideFont->drop();
122 //! Sets another skin independent font.
123 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
125 if (OverrideFont == font)
129 OverrideFont->drop();
134 OverrideFont->grab();
139 IGUIFont * intlGUIEditBox::getOverrideFont() const
144 //! Get the font which is used right now for drawing
145 IGUIFont* intlGUIEditBox::getActiveFont() const
149 IGUISkin* skin = Environment->getSkin();
151 return skin->getFont();
155 //! Sets another color for the text.
156 void intlGUIEditBox::setOverrideColor(video::SColor color)
158 OverrideColor = color;
159 OverrideColorEnabled = true;
162 video::SColor intlGUIEditBox::getOverrideColor() const
164 return OverrideColor;
167 //! Turns the border on or off
168 void intlGUIEditBox::setDrawBorder(bool border)
173 //! Sets whether to draw the background
174 void intlGUIEditBox::setDrawBackground(bool draw)
178 //! Sets if the text should use the overide color or the color in the gui skin.
179 void intlGUIEditBox::enableOverrideColor(bool enable)
181 OverrideColorEnabled = enable;
184 bool intlGUIEditBox::isOverrideColorEnabled() const
186 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
187 return OverrideColorEnabled;
190 //! Enables or disables word wrap
191 void intlGUIEditBox::setWordWrap(bool enable)
198 void intlGUIEditBox::updateAbsolutePosition()
200 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
201 IGUIElement::updateAbsolutePosition();
202 if ( oldAbsoluteRect != AbsoluteRect )
209 //! Checks if word wrap is enabled
210 bool intlGUIEditBox::isWordWrapEnabled() const
212 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
217 //! Enables or disables newlines.
218 void intlGUIEditBox::setMultiLine(bool enable)
224 //! Checks if multi line editing is enabled
225 bool intlGUIEditBox::isMultiLineEnabled() const
227 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
232 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
234 PasswordBox = passwordBox;
237 PasswordChar = passwordChar;
245 bool intlGUIEditBox::isPasswordBox() const
247 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
252 //! Sets text justification
253 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
260 //! called if an event happened.
261 bool intlGUIEditBox::OnEvent(const SEvent& event)
266 switch(event.EventType)
269 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
271 if (event.GUIEvent.Caller == this)
273 MouseMarking = false;
278 case EET_KEY_INPUT_EVENT:
280 #if (defined(__linux__) || defined(__FreeBSD__))
281 // ################################################################
283 // This part is the difference from the original intlGUIEditBox
284 // It converts UTF-8 character into a UCS-2 (wchar_t)
286 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
288 //printf( "char: %lc (%u) \r\n", wc, wc );
290 SEvent irrevent(event);
291 irrevent.KeyInput.Char = wc;
292 // ################################################################
294 if (processKey(irrevent))
297 if (processKey(event))
299 #endif // defined(linux)
303 case EET_MOUSE_INPUT_EVENT:
304 if (processMouse(event))
312 return IGUIElement::OnEvent(event);
316 bool intlGUIEditBox::processKey(const SEvent& event)
318 if (!event.KeyInput.PressedDown)
321 bool textChanged = false;
322 s32 newMarkBegin = MarkBegin;
323 s32 newMarkEnd = MarkEnd;
325 // control shortcut handling
327 if (event.KeyInput.Control)
329 // german backlash '\' entered with control + '?'
330 if ( event.KeyInput.Char == '\\' )
332 inputChar(event.KeyInput.Char);
336 switch(event.KeyInput.Key)
341 newMarkEnd = Text.size();
345 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
347 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
348 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
351 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
352 Operator->copyToClipboard(s.c_str());
356 // cut to the clipboard
357 if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
358 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
359 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
363 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
364 Operator->copyToClipboard(sc.c_str());
366 if (IsEnabled && m_writable) {
369 s = Text.subString(0, realmbgn);
370 s.append( Text.subString(realmend, Text.size()-realmend) );
373 CursorPos = realmbgn;
381 if (!IsEnabled || !m_writable)
384 // paste from the clipboard
387 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
388 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
391 const c8* p = Operator->getTextFromClipboard();
394 if (MarkBegin == MarkEnd)
397 core::stringw s = Text.subString(0, CursorPos);
399 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
401 if (!Max || s.size()<=Max) // thx to Fish FH for fix
405 CursorPos += s.size();
412 core::stringw s = Text.subString(0, realmbgn);
414 s.append( Text.subString(realmend, Text.size()-realmend) );
416 if (!Max || s.size()<=Max) // thx to Fish FH for fix
420 CursorPos = realmbgn + s.size();
431 // move/highlight to start of text
432 if (event.KeyInput.Shift)
434 newMarkEnd = CursorPos;
446 // move/highlight to end of text
447 if (event.KeyInput.Shift)
449 newMarkBegin = CursorPos;
450 newMarkEnd = Text.size();
455 CursorPos = Text.size();
464 // default keyboard handling
466 switch(event.KeyInput.Key)
471 if (WordWrap || MultiLine)
473 p = getLineFromPos(CursorPos);
474 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
475 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
479 if (event.KeyInput.Shift)
481 if (MarkBegin == MarkEnd)
482 newMarkBegin = CursorPos;
492 BlinkStartTime = porting::getTimeMs();
499 if (WordWrap || MultiLine)
501 p = getLineFromPos(CursorPos);
502 p = BrokenTextPositions[p];
505 if (event.KeyInput.Shift)
507 if (MarkBegin == MarkEnd)
508 newMarkBegin = CursorPos;
517 BlinkStartTime = porting::getTimeMs();
528 sendGuiEvent( EGET_EDITBOX_ENTER );
533 if (event.KeyInput.Shift)
537 if (MarkBegin == MarkEnd)
538 newMarkBegin = CursorPos;
540 newMarkEnd = CursorPos-1;
549 if (CursorPos > 0) CursorPos--;
550 BlinkStartTime = porting::getTimeMs();
554 if (event.KeyInput.Shift)
556 if (Text.size() > (u32)CursorPos)
558 if (MarkBegin == MarkEnd)
559 newMarkBegin = CursorPos;
561 newMarkEnd = CursorPos+1;
570 if (Text.size() > (u32)CursorPos) CursorPos++;
571 BlinkStartTime = porting::getTimeMs();
574 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
576 s32 lineNo = getLineFromPos(CursorPos);
577 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
580 s32 cp = CursorPos - BrokenTextPositions[lineNo];
581 if ((s32)BrokenText[lineNo-1].size() < cp)
582 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
584 CursorPos = BrokenTextPositions[lineNo-1] + cp;
587 if (event.KeyInput.Shift)
590 newMarkEnd = CursorPos;
605 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
607 s32 lineNo = getLineFromPos(CursorPos);
608 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
609 if (lineNo < (s32)BrokenText.size()-1)
611 s32 cp = CursorPos - BrokenTextPositions[lineNo];
612 if ((s32)BrokenText[lineNo+1].size() < cp)
613 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
615 CursorPos = BrokenTextPositions[lineNo+1] + cp;
618 if (event.KeyInput.Shift)
621 newMarkEnd = CursorPos;
637 if (!this->IsEnabled || !m_writable)
643 if (MarkBegin != MarkEnd)
645 // delete marked text
646 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
647 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
649 s = Text.subString(0, realmbgn);
650 s.append( Text.subString(realmend, Text.size()-realmend) );
653 CursorPos = realmbgn;
657 // delete text behind cursor
659 s = Text.subString(0, CursorPos-1);
662 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
669 BlinkStartTime = porting::getTimeMs();
676 if (!this->IsEnabled || !m_writable)
682 if (MarkBegin != MarkEnd)
684 // delete marked text
685 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
686 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
688 s = Text.subString(0, realmbgn);
689 s.append( Text.subString(realmend, Text.size()-realmend) );
692 CursorPos = realmbgn;
696 // delete text before cursor
697 s = Text.subString(0, CursorPos);
698 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
702 if (CursorPos > (s32)Text.size())
703 CursorPos = (s32)Text.size();
705 BlinkStartTime = porting::getTimeMs();
743 inputChar(event.KeyInput.Char);
747 // Set new text markers
748 setTextMarkers( newMarkBegin, newMarkEnd );
750 // break the text if it has changed
754 sendGuiEvent(EGET_EDITBOX_CHANGED);
757 calculateScrollPos();
763 //! draws the element and its children
764 void intlGUIEditBox::draw()
769 const bool focus = Environment->hasFocus(this);
771 IGUISkin* skin = Environment->getSkin();
775 FrameRect = AbsoluteRect;
782 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
783 false, true, FrameRect, &AbsoluteClippingRect);
786 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
787 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
788 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
789 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
793 core::rect<s32> localClipRect = FrameRect;
794 localClipRect.clipAgainst(AbsoluteClippingRect);
798 IGUIFont* font = OverrideFont;
800 font = skin->getFont();
803 s32 charcursorpos = 0;
807 if (LastBreakFont != font)
812 // calculate cursor pos
814 core::stringw *txtLine = &Text;
820 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
821 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
822 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
823 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
824 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
825 const s32 lineCount = ml ? BrokenText.size() : 1;
827 // Save the override color information.
828 // Then, alter it if the edit box is disabled.
829 const bool prevOver = OverrideColorEnabled;
830 const video::SColor prevColor = OverrideColor;
833 if (!IsEnabled && !OverrideColorEnabled)
835 OverrideColorEnabled = true;
836 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
839 for (s32 i=0; i < lineCount; ++i)
843 // clipping test - don't draw anything outside the visible area
844 core::rect<s32> c = localClipRect;
845 c.clipAgainst(CurrentTextRect);
852 if (BrokenText.size() != 1)
855 BrokenText.push_back(core::stringw());
857 if (BrokenText[0].size() != Text.size())
859 BrokenText[0] = Text;
860 for (u32 q = 0; q < Text.size(); ++q)
862 BrokenText[0] [q] = PasswordChar;
865 txtLine = &BrokenText[0];
870 txtLine = ml ? &BrokenText[i] : &Text;
871 startPos = ml ? BrokenTextPositions[i] : 0;
876 font->draw(txtLine->c_str(), CurrentTextRect,
877 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
878 false, true, &localClipRect);
880 // draw mark and marked text
881 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
884 s32 mbegin = 0, mend = 0;
885 s32 lineStartPos = 0, lineEndPos = txtLine->size();
889 // highlight start is on this line
890 s = txtLine->subString(0, realmbgn - startPos);
891 mbegin = font->getDimension(s.c_str()).Width;
894 mbegin += font->getKerningWidth(
895 &((*txtLine)[realmbgn - startPos]),
896 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
898 lineStartPos = realmbgn - startPos;
900 if (i == hlineStart + hlineCount - 1)
902 // highlight end is on this line
903 s2 = txtLine->subString(0, realmend - startPos);
904 mend = font->getDimension(s2.c_str()).Width;
905 lineEndPos = (s32)s2.size();
908 mend = font->getDimension(txtLine->c_str()).Width;
910 CurrentTextRect.UpperLeftCorner.X += mbegin;
911 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
914 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
917 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
920 font->draw(s.c_str(), CurrentTextRect,
921 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
922 false, true, &localClipRect);
927 // Return the override color information to its previous settings.
928 OverrideColorEnabled = prevOver;
929 OverrideColor = prevColor;
934 if (WordWrap || MultiLine)
936 cursorLine = getLineFromPos(CursorPos);
937 txtLine = &BrokenText[cursorLine];
938 startPos = BrokenTextPositions[cursorLine];
940 s = txtLine->subString(0,CursorPos-startPos);
941 charcursorpos = font->getDimension(s.c_str()).Width +
942 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
945 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350) {
946 setTextRect(cursorLine);
947 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
949 font->draw(L"_", CurrentTextRect,
950 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
951 false, true, &localClipRect);
961 //! Sets the new caption of this element.
962 void intlGUIEditBox::setText(const wchar_t* text)
965 if (u32(CursorPos) > Text.size())
966 CursorPos = Text.size();
972 //! Enables or disables automatic scrolling with cursor position
973 //! \param enable: If set to true, the text will move around with the cursor position
974 void intlGUIEditBox::setAutoScroll(bool enable)
980 //! Checks to see if automatic scrolling is enabled
981 //! \return true if automatic scrolling is enabled, false if not
982 bool intlGUIEditBox::isAutoScrollEnabled() const
984 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
989 //! Gets the area of the text in the edit box
990 //! \return Returns the size in pixels of the text
991 core::dimension2du intlGUIEditBox::getTextDimension()
996 ret = CurrentTextRect;
998 for (u32 i=1; i < BrokenText.size(); ++i)
1001 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
1002 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
1005 return core::dimension2du(ret.getSize());
1009 //! Sets the maximum amount of characters which may be entered in the box.
1010 //! \param max: Maximum amount of characters. If 0, the character amount is
1012 void intlGUIEditBox::setMax(u32 max)
1016 if (Text.size() > Max && Max != 0)
1017 Text = Text.subString(0, Max);
1021 //! Returns maximum amount of characters, previously set by setMax();
1022 u32 intlGUIEditBox::getMax() const
1028 bool intlGUIEditBox::processMouse(const SEvent& event)
1030 switch(event.MouseInput.Event)
1032 case irr::EMIE_LMOUSE_LEFT_UP:
1033 if (Environment->hasFocus(this))
1035 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1038 setTextMarkers( MarkBegin, CursorPos );
1040 MouseMarking = false;
1041 calculateScrollPos();
1045 case irr::EMIE_MOUSE_MOVED:
1049 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1050 setTextMarkers( MarkBegin, CursorPos );
1051 calculateScrollPos();
1056 case EMIE_LMOUSE_PRESSED_DOWN:
1057 if (!Environment->hasFocus(this))
1059 BlinkStartTime = porting::getTimeMs();
1060 MouseMarking = true;
1061 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1062 setTextMarkers(CursorPos, CursorPos );
1063 calculateScrollPos();
1068 if (!AbsoluteClippingRect.isPointInside(
1069 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
1075 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1077 s32 newMarkBegin = MarkBegin;
1079 newMarkBegin = CursorPos;
1081 MouseMarking = true;
1082 setTextMarkers( newMarkBegin, CursorPos);
1083 calculateScrollPos();
1087 case EMIE_MOUSE_WHEEL:
1088 if (m_vscrollbar && m_vscrollbar->isVisible()) {
1089 s32 pos = m_vscrollbar->getPos();
1090 s32 step = m_vscrollbar->getSmallStep();
1091 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
1102 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1104 IGUIFont* font = OverrideFont;
1105 IGUISkin* skin = Environment->getSkin();
1107 font = skin->getFont();
1109 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1111 core::stringw *txtLine = NULL;
1113 u32 curr_line_idx = 0;
1116 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1117 setTextRect(curr_line_idx);
1118 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1119 y = CurrentTextRect.UpperLeftCorner.Y;
1120 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1121 y = CurrentTextRect.LowerRightCorner.Y;
1123 // is it inside this region?
1124 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1125 // we've found the clicked line
1126 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1127 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1132 if (x < CurrentTextRect.UpperLeftCorner.X)
1133 x = CurrentTextRect.UpperLeftCorner.X;
1134 else if (x > CurrentTextRect.LowerRightCorner.X)
1135 x = CurrentTextRect.LowerRightCorner.X;
1137 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1138 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1139 // will be the last char, not null char of the wstring
1140 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1143 return rangelim(idx + startPos, 0, S32_MAX);
1147 //! Breaks the single text line.
1148 void intlGUIEditBox::breakText()
1150 IGUISkin* skin = Environment->getSkin();
1152 if ((!WordWrap && !MultiLine) || !skin)
1155 BrokenText.clear(); // need to reallocate :/
1156 BrokenTextPositions.set_used(0);
1158 IGUIFont* font = OverrideFont;
1160 font = skin->getFont();
1165 LastBreakFont = font;
1169 core::stringw whitespace;
1170 s32 lastLineStart = 0;
1171 s32 size = Text.size();
1173 s32 elWidth = RelativeRect.getWidth() - 6;
1176 for (s32 i=0; i<size; ++i)
1179 bool lineBreak = false;
1181 if (c == L'\r') // Mac or Windows breaks
1185 if (Text[i+1] == L'\n') // Windows breaks
1191 else if (c == L'\n') // Unix breaks
1197 // don't break if we're not a multi-line edit box
1201 if (c == L' ' || c == 0 || i == (size-1))
1203 if (!word.empty()) {
1204 // here comes the next whitespace, look if
1205 // we can break the last word to the next line.
1206 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1207 s32 worldlgth = font->getDimension(word.c_str()).Width;
1209 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1211 // break to next line
1213 BrokenText.push_back(line);
1214 BrokenTextPositions.push_back(lastLineStart);
1215 lastLineStart = i - (s32)word.size();
1223 length += whitelgth + worldlgth;
1232 // compute line break
1237 BrokenText.push_back(line);
1238 BrokenTextPositions.push_back(lastLineStart);
1239 lastLineStart = i+1;
1248 // yippee this is a word..
1255 BrokenText.push_back(line);
1256 BrokenTextPositions.push_back(lastLineStart);
1260 void intlGUIEditBox::setTextRect(s32 line)
1262 core::dimension2du d;
1264 IGUISkin* skin = Environment->getSkin();
1268 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1273 // get text dimension
1274 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1275 if (WordWrap || MultiLine)
1277 d = font->getDimension(BrokenText[line].c_str());
1281 d = font->getDimension(Text.c_str());
1282 d.Height = AbsoluteRect.getHeight();
1284 d.Height += font->getKerningHeight();
1290 // align to h centre
1291 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1292 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1294 case EGUIA_LOWERRIGHT:
1295 // align to right edge
1296 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1297 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1300 // align to left edge
1301 CurrentTextRect.UpperLeftCorner.X = 0;
1302 CurrentTextRect.LowerRightCorner.X = d.Width;
1309 // align to v centre
1310 CurrentTextRect.UpperLeftCorner.Y =
1311 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1313 case EGUIA_LOWERRIGHT:
1314 // align to bottom edge
1315 CurrentTextRect.UpperLeftCorner.Y =
1316 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1319 // align to top edge
1320 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1324 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1325 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1326 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1327 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1329 CurrentTextRect += FrameRect.UpperLeftCorner;
1334 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1336 if (!WordWrap && !MultiLine)
1340 while (i < (s32)BrokenTextPositions.size())
1342 if (BrokenTextPositions[i] > pos)
1346 return (s32)BrokenTextPositions.size() - 1;
1350 void intlGUIEditBox::inputChar(wchar_t c)
1352 if (!IsEnabled || !m_writable)
1357 if (Text.size() < Max || Max == 0)
1361 if (MarkBegin != MarkEnd)
1363 // replace marked text
1364 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1365 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1367 s = Text.subString(0, realmbgn);
1369 s.append( Text.subString(realmend, Text.size()-realmend) );
1371 CursorPos = realmbgn+1;
1375 // add new character
1376 s = Text.subString(0, CursorPos);
1378 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1383 BlinkStartTime = porting::getTimeMs();
1384 setTextMarkers(0, 0);
1388 sendGuiEvent(EGET_EDITBOX_CHANGED);
1389 calculateScrollPos();
1393 void intlGUIEditBox::calculateScrollPos()
1398 // calculate horizontal scroll position
1399 s32 cursLine = getLineFromPos(CursorPos);
1400 setTextRect(cursLine);
1402 // don't do horizontal scrolling when wordwrap is enabled.
1405 // get cursor position
1406 IGUISkin* skin = Environment->getSkin();
1409 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1413 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1414 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1416 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1417 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1419 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1421 if (FrameRect.LowerRightCorner.X < cEnd)
1422 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1423 else if (FrameRect.UpperLeftCorner.X > cStart)
1424 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1428 // todo: adjust scrollbar
1431 if (!WordWrap && !MultiLine)
1434 // vertical scroll position
1435 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y)
1436 VScrollPos += CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y; // scrolling downwards
1437 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y)
1438 VScrollPos += CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y; // scrolling upwards
1440 // todo: adjust scrollbar
1442 m_vscrollbar->setPos(VScrollPos);
1445 //! set text markers
1446 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1448 if ( begin != MarkBegin || end != MarkEnd )
1452 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1456 //! send some gui event to parent
1457 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1462 e.EventType = EET_GUI_EVENT;
1463 e.GUIEvent.Caller = this;
1464 e.GUIEvent.Element = 0;
1465 e.GUIEvent.EventType = type;
1471 //! Create a vertical scrollbar
1472 void intlGUIEditBox::createVScrollBar()
1477 fontHeight = OverrideFont->getDimension(L"").Height;
1479 if (IGUISkin* skin = Environment->getSkin()) {
1480 if (IGUIFont* font = skin->getFont()) {
1481 fontHeight = font->getDimension(L"").Height;
1486 irr::core::rect<s32> scrollbarrect = FrameRect;
1487 scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
1488 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1489 m_vscrollbar->setVisible(false);
1490 m_vscrollbar->setSmallStep(3 * fontHeight);
1491 m_vscrollbar->setLargeStep(10 * fontHeight);
1494 //! Update the vertical scrollbar (visibilty & scroll position)
1495 void intlGUIEditBox::updateVScrollBar()
1500 // OnScrollBarChanged(...)
1501 if (m_vscrollbar->getPos() != VScrollPos) {
1502 s32 deltaScrollY = m_vscrollbar->getPos() - VScrollPos;
1503 CurrentTextRect.UpperLeftCorner.Y -= deltaScrollY;
1504 CurrentTextRect.LowerRightCorner.Y -= deltaScrollY;
1506 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1507 if (scrollymax != m_vscrollbar->getMax()) {
1508 // manage a newline or a deleted line
1509 m_vscrollbar->setMax(scrollymax);
1510 calculateScrollPos();
1512 // manage a newline or a deleted line
1513 VScrollPos = m_vscrollbar->getPos();
1517 // check if a vertical scrollbar is needed ?
1518 if (getTextDimension().Height > (u32) FrameRect.getHeight()) {
1519 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1520 if (scrollymax != m_vscrollbar->getMax()) {
1521 m_vscrollbar->setMax(scrollymax);
1524 if (!m_vscrollbar->isVisible() && MultiLine) {
1525 AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
1527 m_vscrollbar->setVisible(true);
1530 if (m_vscrollbar->isVisible()) {
1531 AbsoluteRect.LowerRightCorner.X += m_scrollbar_width;
1534 m_vscrollbar->setPos(0);
1535 m_vscrollbar->setMax(1);
1536 m_vscrollbar->setVisible(false);
1541 void intlGUIEditBox::setWritable(bool can_write_text)
1543 m_writable = can_write_text;
1546 //! Writes attributes of the element.
1547 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1549 // IGUIEditBox::serializeAttributes(out,options);
1551 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1552 out->addColor ("OverrideColor", OverrideColor);
1553 // out->addFont("OverrideFont",OverrideFont);
1554 out->addInt ("MaxChars", Max);
1555 out->addBool ("WordWrap", WordWrap);
1556 out->addBool ("MultiLine", MultiLine);
1557 out->addBool ("AutoScroll", AutoScroll);
1558 out->addBool ("PasswordBox", PasswordBox);
1559 core::stringw ch = L" ";
1560 ch[0] = PasswordChar;
1561 out->addString("PasswordChar", ch.c_str());
1562 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1563 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1564 out->addBool ("Writable", m_writable);
1566 IGUIEditBox::serializeAttributes(out,options);
1570 //! Reads attributes of the element
1571 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1573 IGUIEditBox::deserializeAttributes(in,options);
1575 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1576 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1577 setMax(in->getAttributeAsInt("MaxChars"));
1578 setWordWrap(in->getAttributeAsBool("WordWrap"));
1579 setMultiLine(in->getAttributeAsBool("MultiLine"));
1580 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1581 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1584 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1586 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1588 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1589 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1591 setWritable(in->getAttributeAsBool("Writable"));
1592 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1596 } // end namespace gui
1597 } // end namespace irr
1599 #endif // _IRR_COMPILE_WITH_GUI_