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-2010 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 "intlGUIEditBox.h"
34 #ifdef _IRR_COMPILE_WITH_GUI_
37 #include "IGUIEnvironment.h"
39 #include "IVideoDriver.h"
41 //#include "irrlicht/os.cpp"
43 //#include "Keycodes.h"
48 ctrl+left/right to select word
49 double click/ctrl click: word select + drag to select whole words, triple click to select line
50 optional? dragging selected text
60 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
61 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
62 const core::rect<s32>& rectangle)
63 : IGUIEditBox(environment, parent, id, rectangle), MouseMarking(false),
64 Border(border), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
65 OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
66 Operator(0), BlinkStartTime(0), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
67 WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
68 PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
69 CurrentTextRect(0,0,1,1), FrameRect(rectangle)
72 setDebugName("intlintlGUIEditBox");
78 Operator = Environment->getOSOperator();
83 // this element can be tabbed to
89 skin = Environment->getSkin();
92 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
93 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
94 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
95 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
100 calculateScrollPos();
105 intlGUIEditBox::~intlGUIEditBox()
108 OverrideFont->drop();
115 //! Sets another skin independent font.
116 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
118 if (OverrideFont == font)
122 OverrideFont->drop();
127 OverrideFont->grab();
132 IGUIFont * intlGUIEditBox::getOverrideFont() const
137 //! Get the font which is used right now for drawing
138 IGUIFont* intlGUIEditBox::getActiveFont() const
142 IGUISkin* skin = Environment->getSkin();
144 return skin->getFont();
148 //! Sets another color for the text.
149 void intlGUIEditBox::setOverrideColor(video::SColor color)
151 OverrideColor = color;
152 OverrideColorEnabled = true;
155 video::SColor intlGUIEditBox::getOverrideColor() const
157 return OverrideColor;
160 //! Turns the border on or off
161 void intlGUIEditBox::setDrawBorder(bool border)
166 //! Sets whether to draw the background
167 void intlGUIEditBox::setDrawBackground(bool draw)
171 //! Sets if the text should use the overide color or the color in the gui skin.
172 void intlGUIEditBox::enableOverrideColor(bool enable)
174 OverrideColorEnabled = enable;
177 bool intlGUIEditBox::isOverrideColorEnabled() const
179 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
180 return OverrideColorEnabled;
183 //! Enables or disables word wrap
184 void intlGUIEditBox::setWordWrap(bool enable)
191 void intlGUIEditBox::updateAbsolutePosition()
193 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
194 IGUIElement::updateAbsolutePosition();
195 if ( oldAbsoluteRect != AbsoluteRect )
202 //! Checks if word wrap is enabled
203 bool intlGUIEditBox::isWordWrapEnabled() const
205 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
210 //! Enables or disables newlines.
211 void intlGUIEditBox::setMultiLine(bool enable)
217 //! Checks if multi line editing is enabled
218 bool intlGUIEditBox::isMultiLineEnabled() const
220 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
225 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
227 PasswordBox = passwordBox;
230 PasswordChar = passwordChar;
238 bool intlGUIEditBox::isPasswordBox() const
240 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
245 //! Sets text justification
246 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
253 //! called if an event happened.
254 bool intlGUIEditBox::OnEvent(const SEvent& event)
259 switch(event.EventType)
262 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
264 if (event.GUIEvent.Caller == this)
266 MouseMarking = false;
271 case EET_KEY_INPUT_EVENT:
274 // ################################################################
276 // This part is the difference from the original intlGUIEditBox
277 // It converts UTF-8 character into a UCS-2 (wchar_t)
279 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
281 //printf( "char: %lc (%u) \r\n", wc, wc );
283 SEvent irrevent(event);
284 irrevent.KeyInput.Char = wc;
285 // ################################################################
287 if (processKey(irrevent))
290 if (processKey(event))
292 #endif // defined(linux)
296 case EET_MOUSE_INPUT_EVENT:
297 if (processMouse(event))
305 return IGUIElement::OnEvent(event);
309 bool intlGUIEditBox::processKey(const SEvent& event)
311 if (!event.KeyInput.PressedDown)
314 bool textChanged = false;
315 s32 newMarkBegin = MarkBegin;
316 s32 newMarkEnd = MarkEnd;
318 // control shortcut handling
320 if (event.KeyInput.Control)
322 // german backlash '\' entered with control + '?'
323 if ( event.KeyInput.Char == '\\' )
325 inputChar(event.KeyInput.Char);
329 switch(event.KeyInput.Key)
334 newMarkEnd = Text.size();
338 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
340 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
341 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
344 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
345 Operator->copyToClipboard(s.c_str());
349 // cut to the clipboard
350 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
352 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
353 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
357 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
358 Operator->copyToClipboard(sc.c_str());
364 s = Text.subString(0, realmbgn);
365 s.append( Text.subString(realmend, Text.size()-realmend) );
368 CursorPos = realmbgn;
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 )
639 if (MarkBegin != MarkEnd)
641 // delete marked text
642 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
643 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
645 s = Text.subString(0, realmbgn);
646 s.append( Text.subString(realmend, Text.size()-realmend) );
649 CursorPos = realmbgn;
653 // delete text behind cursor
655 s = Text.subString(0, CursorPos-1);
658 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
665 BlinkStartTime = porting::getTimeMs();
672 if ( !this->IsEnabled )
675 if (Text.size() != 0)
679 if (MarkBegin != MarkEnd)
681 // delete marked text
682 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
683 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
685 s = Text.subString(0, realmbgn);
686 s.append( Text.subString(realmend, Text.size()-realmend) );
689 CursorPos = realmbgn;
693 // delete text before cursor
694 s = Text.subString(0, CursorPos);
695 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
699 if (CursorPos > (s32)Text.size())
700 CursorPos = (s32)Text.size();
702 BlinkStartTime = porting::getTimeMs();
740 inputChar(event.KeyInput.Char);
744 // Set new text markers
745 setTextMarkers( newMarkBegin, newMarkEnd );
747 // break the text if it has changed
751 sendGuiEvent(EGET_EDITBOX_CHANGED);
754 calculateScrollPos();
760 //! draws the element and its children
761 void intlGUIEditBox::draw()
766 const bool focus = Environment->hasFocus(this);
768 IGUISkin* skin = Environment->getSkin();
772 FrameRect = AbsoluteRect;
778 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
779 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;
786 core::rect<s32> localClipRect = FrameRect;
787 localClipRect.clipAgainst(AbsoluteClippingRect);
791 IGUIFont* font = OverrideFont;
793 font = skin->getFont();
796 s32 charcursorpos = 0;
800 if (LastBreakFont != font)
805 // calculate cursor pos
807 core::stringw *txtLine = &Text;
813 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
814 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
815 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
816 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
817 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
818 const s32 lineCount = ml ? BrokenText.size() : 1;
820 // Save the override color information.
821 // Then, alter it if the edit box is disabled.
822 const bool prevOver = OverrideColorEnabled;
823 const video::SColor prevColor = OverrideColor;
827 if (!IsEnabled && !OverrideColorEnabled)
829 OverrideColorEnabled = true;
830 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
833 for (s32 i=0; i < lineCount; ++i)
837 // clipping test - don't draw anything outside the visible area
838 core::rect<s32> c = localClipRect;
839 c.clipAgainst(CurrentTextRect);
846 if (BrokenText.size() != 1)
849 BrokenText.push_back(core::stringw());
851 if (BrokenText[0].size() != Text.size())
853 BrokenText[0] = Text;
854 for (u32 q = 0; q < Text.size(); ++q)
856 BrokenText[0] [q] = PasswordChar;
859 txtLine = &BrokenText[0];
864 txtLine = ml ? &BrokenText[i] : &Text;
865 startPos = ml ? BrokenTextPositions[i] : 0;
870 font->draw(txtLine->c_str(), CurrentTextRect,
871 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
872 false, true, &localClipRect);
874 // draw mark and marked text
875 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
878 s32 mbegin = 0, mend = 0;
879 s32 lineStartPos = 0, lineEndPos = txtLine->size();
883 // highlight start is on this line
884 s = txtLine->subString(0, realmbgn - startPos);
885 mbegin = font->getDimension(s.c_str()).Width;
888 mbegin += font->getKerningWidth(
889 &((*txtLine)[realmbgn - startPos]),
890 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
892 lineStartPos = realmbgn - startPos;
894 if (i == hlineStart + hlineCount - 1)
896 // highlight end is on this line
897 s2 = txtLine->subString(0, realmend - startPos);
898 mend = font->getDimension(s2.c_str()).Width;
899 lineEndPos = (s32)s2.size();
902 mend = font->getDimension(txtLine->c_str()).Width;
904 CurrentTextRect.UpperLeftCorner.X += mbegin;
905 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
908 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
911 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
914 font->draw(s.c_str(), CurrentTextRect,
915 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
916 false, true, &localClipRect);
921 // Return the override color information to its previous settings.
922 OverrideColorEnabled = prevOver;
923 OverrideColor = prevColor;
928 if (WordWrap || MultiLine)
930 cursorLine = getLineFromPos(CursorPos);
931 txtLine = &BrokenText[cursorLine];
932 startPos = BrokenTextPositions[cursorLine];
934 s = txtLine->subString(0,CursorPos-startPos);
935 charcursorpos = font->getDimension(s.c_str()).Width +
936 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
938 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350)
940 setTextRect(cursorLine);
941 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
943 font->draw(L"_", CurrentTextRect,
944 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
945 false, true, &localClipRect);
954 //! Sets the new caption of this element.
955 void intlGUIEditBox::setText(const wchar_t* text)
958 if (u32(CursorPos) > Text.size())
959 CursorPos = Text.size();
965 //! Enables or disables automatic scrolling with cursor position
966 //! \param enable: If set to true, the text will move around with the cursor position
967 void intlGUIEditBox::setAutoScroll(bool enable)
973 //! Checks to see if automatic scrolling is enabled
974 //! \return true if automatic scrolling is enabled, false if not
975 bool intlGUIEditBox::isAutoScrollEnabled() const
977 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
982 //! Gets the area of the text in the edit box
983 //! \return Returns the size in pixels of the text
984 core::dimension2du intlGUIEditBox::getTextDimension()
989 ret = CurrentTextRect;
991 for (u32 i=1; i < BrokenText.size(); ++i)
994 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
995 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
998 return core::dimension2du(ret.getSize());
1002 //! Sets the maximum amount of characters which may be entered in the box.
1003 //! \param max: Maximum amount of characters. If 0, the character amount is
1005 void intlGUIEditBox::setMax(u32 max)
1009 if (Text.size() > Max && Max != 0)
1010 Text = Text.subString(0, Max);
1014 //! Returns maximum amount of characters, previously set by setMax();
1015 u32 intlGUIEditBox::getMax() const
1021 bool intlGUIEditBox::processMouse(const SEvent& event)
1023 switch(event.MouseInput.Event)
1025 case irr::EMIE_LMOUSE_LEFT_UP:
1026 if (Environment->hasFocus(this))
1028 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1031 setTextMarkers( MarkBegin, CursorPos );
1033 MouseMarking = false;
1034 calculateScrollPos();
1038 case irr::EMIE_MOUSE_MOVED:
1042 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1043 setTextMarkers( MarkBegin, CursorPos );
1044 calculateScrollPos();
1049 case EMIE_LMOUSE_PRESSED_DOWN:
1050 if (!Environment->hasFocus(this))
1052 BlinkStartTime = porting::getTimeMs();
1053 MouseMarking = true;
1054 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1055 setTextMarkers(CursorPos, CursorPos );
1056 calculateScrollPos();
1061 if (!AbsoluteClippingRect.isPointInside(
1062 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();
1089 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1091 IGUIFont* font = OverrideFont;
1092 IGUISkin* skin = Environment->getSkin();
1094 font = skin->getFont();
1096 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1098 core::stringw *txtLine=0;
1102 for (u32 i=0; i < lineCount; ++i)
1105 if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1106 y = CurrentTextRect.UpperLeftCorner.Y;
1107 if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
1108 y = CurrentTextRect.LowerRightCorner.Y;
1110 // is it inside this region?
1111 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
1113 // we've found the clicked line
1114 txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
1115 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
1120 if (x < CurrentTextRect.UpperLeftCorner.X)
1121 x = CurrentTextRect.UpperLeftCorner.X;
1123 s32 idx = font->getCharacterFromPos(Text.c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1125 // click was on or left of the line
1127 return idx + startPos;
1129 // click was off the right edge of the line, go to end.
1130 return txtLine->size() + startPos;
1134 //! Breaks the single text line.
1135 void intlGUIEditBox::breakText()
1137 IGUISkin* skin = Environment->getSkin();
1139 if ((!WordWrap && !MultiLine) || !skin)
1142 BrokenText.clear(); // need to reallocate :/
1143 BrokenTextPositions.set_used(0);
1145 IGUIFont* font = OverrideFont;
1147 font = skin->getFont();
1152 LastBreakFont = font;
1156 core::stringw whitespace;
1157 s32 lastLineStart = 0;
1158 s32 size = Text.size();
1160 s32 elWidth = RelativeRect.getWidth() - 6;
1163 for (s32 i=0; i<size; ++i)
1166 bool lineBreak = false;
1168 if (c == L'\r') // Mac or Windows breaks
1172 if (Text[i+1] == L'\n') // Windows breaks
1178 else if (c == L'\n') // Unix breaks
1184 // don't break if we're not a multi-line edit box
1188 if (c == L' ' || c == 0 || i == (size-1))
1192 // here comes the next whitespace, look if
1193 // we can break the last word to the next line.
1194 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1195 s32 worldlgth = font->getDimension(word.c_str()).Width;
1197 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1199 // break to next line
1201 BrokenText.push_back(line);
1202 BrokenTextPositions.push_back(lastLineStart);
1203 lastLineStart = i - (s32)word.size();
1211 length += whitelgth + worldlgth;
1220 // compute line break
1225 BrokenText.push_back(line);
1226 BrokenTextPositions.push_back(lastLineStart);
1227 lastLineStart = i+1;
1236 // yippee this is a word..
1243 BrokenText.push_back(line);
1244 BrokenTextPositions.push_back(lastLineStart);
1248 void intlGUIEditBox::setTextRect(s32 line)
1250 core::dimension2du d;
1252 IGUISkin* skin = Environment->getSkin();
1256 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1261 // get text dimension
1262 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1263 if (WordWrap || MultiLine)
1265 d = font->getDimension(BrokenText[line].c_str());
1269 d = font->getDimension(Text.c_str());
1270 d.Height = AbsoluteRect.getHeight();
1272 d.Height += font->getKerningHeight();
1278 // align to h centre
1279 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1280 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1282 case EGUIA_LOWERRIGHT:
1283 // align to right edge
1284 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1285 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1288 // align to left edge
1289 CurrentTextRect.UpperLeftCorner.X = 0;
1290 CurrentTextRect.LowerRightCorner.X = d.Width;
1297 // align to v centre
1298 CurrentTextRect.UpperLeftCorner.Y =
1299 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1301 case EGUIA_LOWERRIGHT:
1302 // align to bottom edge
1303 CurrentTextRect.UpperLeftCorner.Y =
1304 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1307 // align to top edge
1308 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1312 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1313 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1314 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1315 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1317 CurrentTextRect += FrameRect.UpperLeftCorner;
1322 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1324 if (!WordWrap && !MultiLine)
1328 while (i < (s32)BrokenTextPositions.size())
1330 if (BrokenTextPositions[i] > pos)
1334 return (s32)BrokenTextPositions.size() - 1;
1338 void intlGUIEditBox::inputChar(wchar_t c)
1345 if (Text.size() < Max || Max == 0)
1349 if (MarkBegin != MarkEnd)
1351 // replace marked text
1352 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1353 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1355 s = Text.subString(0, realmbgn);
1357 s.append( Text.subString(realmend, Text.size()-realmend) );
1359 CursorPos = realmbgn+1;
1363 // add new character
1364 s = Text.subString(0, CursorPos);
1366 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1371 BlinkStartTime = porting::getTimeMs();
1372 setTextMarkers(0, 0);
1376 sendGuiEvent(EGET_EDITBOX_CHANGED);
1377 calculateScrollPos();
1381 void intlGUIEditBox::calculateScrollPos()
1386 // calculate horizontal scroll position
1387 s32 cursLine = getLineFromPos(CursorPos);
1388 setTextRect(cursLine);
1390 // don't do horizontal scrolling when wordwrap is enabled.
1393 // get cursor position
1394 IGUISkin* skin = Environment->getSkin();
1397 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1401 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1402 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1404 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1405 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1407 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1409 if (FrameRect.LowerRightCorner.X < cEnd)
1410 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1411 else if (FrameRect.UpperLeftCorner.X > cStart)
1412 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1416 // todo: adjust scrollbar
1419 // vertical scroll position
1420 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1421 VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1423 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1424 VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1428 // todo: adjust scrollbar
1431 //! set text markers
1432 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1434 if ( begin != MarkBegin || end != MarkEnd )
1438 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1442 //! send some gui event to parent
1443 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1448 e.EventType = EET_GUI_EVENT;
1449 e.GUIEvent.Caller = this;
1450 e.GUIEvent.Element = 0;
1451 e.GUIEvent.EventType = type;
1457 //! Writes attributes of the element.
1458 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1460 // IGUIEditBox::serializeAttributes(out,options);
1462 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1463 out->addColor ("OverrideColor", OverrideColor);
1464 // out->addFont("OverrideFont",OverrideFont);
1465 out->addInt ("MaxChars", Max);
1466 out->addBool ("WordWrap", WordWrap);
1467 out->addBool ("MultiLine", MultiLine);
1468 out->addBool ("AutoScroll", AutoScroll);
1469 out->addBool ("PasswordBox", PasswordBox);
1470 core::stringw ch = L" ";
1471 ch[0] = PasswordChar;
1472 out->addString("PasswordChar", ch.c_str());
1473 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1474 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1476 IGUIEditBox::serializeAttributes(out,options);
1480 //! Reads attributes of the element
1481 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1483 IGUIEditBox::deserializeAttributes(in,options);
1485 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1486 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1487 setMax(in->getAttributeAsInt("MaxChars"));
1488 setWordWrap(in->getAttributeAsBool("WordWrap"));
1489 setMultiLine(in->getAttributeAsBool("MultiLine"));
1490 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1491 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1494 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1496 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1498 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1499 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1501 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1505 } // end namespace gui
1506 } // end namespace irr
1508 #endif // _IRR_COMPILE_WITH_GUI_