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 "intlGUIEditBox.h"
34 #if defined(_IRR_COMPILE_WITH_GUI_) && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
37 #include "IGUIEnvironment.h"
39 #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)
64 : IGUIEditBox(environment, parent, id, rectangle), MouseMarking(false),
65 Border(border), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
66 OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
67 Operator(0), BlinkStartTime(0), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
68 WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
69 PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
70 CurrentTextRect(0,0,1,1), FrameRect(rectangle)
73 setDebugName("intlintlGUIEditBox");
79 Operator = Environment->getOSOperator();
84 // this element can be tabbed to
90 skin = Environment->getSkin();
93 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
94 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
95 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
96 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
101 calculateScrollPos();
106 intlGUIEditBox::~intlGUIEditBox()
109 OverrideFont->drop();
116 //! Sets another skin independent font.
117 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
119 if (OverrideFont == font)
123 OverrideFont->drop();
128 OverrideFont->grab();
133 IGUIFont * intlGUIEditBox::getOverrideFont() const
138 //! Get the font which is used right now for drawing
139 IGUIFont* intlGUIEditBox::getActiveFont() const
143 IGUISkin* skin = Environment->getSkin();
145 return skin->getFont();
149 //! Sets another color for the text.
150 void intlGUIEditBox::setOverrideColor(video::SColor color)
152 OverrideColor = color;
153 OverrideColorEnabled = true;
156 video::SColor intlGUIEditBox::getOverrideColor() const
158 return OverrideColor;
161 //! Turns the border on or off
162 void intlGUIEditBox::setDrawBorder(bool border)
167 //! Sets whether to draw the background
168 void intlGUIEditBox::setDrawBackground(bool draw)
172 //! Sets if the text should use the overide color or the color in the gui skin.
173 void intlGUIEditBox::enableOverrideColor(bool enable)
175 OverrideColorEnabled = enable;
178 bool intlGUIEditBox::isOverrideColorEnabled() const
180 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
181 return OverrideColorEnabled;
184 //! Enables or disables word wrap
185 void intlGUIEditBox::setWordWrap(bool enable)
192 void intlGUIEditBox::updateAbsolutePosition()
194 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
195 IGUIElement::updateAbsolutePosition();
196 if ( oldAbsoluteRect != AbsoluteRect )
203 //! Checks if word wrap is enabled
204 bool intlGUIEditBox::isWordWrapEnabled() const
206 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
211 //! Enables or disables newlines.
212 void intlGUIEditBox::setMultiLine(bool enable)
218 //! Checks if multi line editing is enabled
219 bool intlGUIEditBox::isMultiLineEnabled() const
221 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
226 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
228 PasswordBox = passwordBox;
231 PasswordChar = passwordChar;
239 bool intlGUIEditBox::isPasswordBox() const
241 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
246 //! Sets text justification
247 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
254 //! called if an event happened.
255 bool intlGUIEditBox::OnEvent(const SEvent& event)
260 switch(event.EventType)
263 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
265 if (event.GUIEvent.Caller == this)
267 MouseMarking = false;
272 case EET_KEY_INPUT_EVENT:
274 #if (defined(linux) || defined(__linux) || defined(__FreeBSD__))
275 // ################################################################
277 // This part is the difference from the original intlGUIEditBox
278 // It converts UTF-8 character into a UCS-2 (wchar_t)
280 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
282 //printf( "char: %lc (%u) \r\n", wc, wc );
284 SEvent irrevent(event);
285 irrevent.KeyInput.Char = wc;
286 // ################################################################
288 if (processKey(irrevent))
291 if (processKey(event))
293 #endif // defined(linux)
297 case EET_MOUSE_INPUT_EVENT:
298 if (processMouse(event))
306 return IGUIElement::OnEvent(event);
310 bool intlGUIEditBox::processKey(const SEvent& event)
312 if (!event.KeyInput.PressedDown)
315 bool textChanged = false;
316 s32 newMarkBegin = MarkBegin;
317 s32 newMarkEnd = MarkEnd;
319 // control shortcut handling
321 if (event.KeyInput.Control)
323 // german backlash '\' entered with control + '?'
324 if ( event.KeyInput.Char == '\\' )
326 inputChar(event.KeyInput.Char);
330 switch(event.KeyInput.Key)
335 newMarkEnd = Text.size();
339 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
341 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
342 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
345 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
346 Operator->copyToClipboard(s.c_str());
350 // cut to the clipboard
351 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());
365 s = Text.subString(0, realmbgn);
366 s.append( Text.subString(realmend, Text.size()-realmend) );
369 CursorPos = realmbgn;
380 // paste from the clipboard
383 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
384 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
387 const c8* p = Operator->getTextFromClipboard();
390 if (MarkBegin == MarkEnd)
393 core::stringw s = Text.subString(0, CursorPos);
395 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
397 if (!Max || s.size()<=Max) // thx to Fish FH for fix
401 CursorPos += s.size();
408 core::stringw s = Text.subString(0, realmbgn);
410 s.append( Text.subString(realmend, Text.size()-realmend) );
412 if (!Max || s.size()<=Max) // thx to Fish FH for fix
416 CursorPos = realmbgn + s.size();
427 // move/highlight to start of text
428 if (event.KeyInput.Shift)
430 newMarkEnd = CursorPos;
442 // move/highlight to end of text
443 if (event.KeyInput.Shift)
445 newMarkBegin = CursorPos;
446 newMarkEnd = Text.size();
451 CursorPos = Text.size();
460 // default keyboard handling
462 switch(event.KeyInput.Key)
467 if (WordWrap || MultiLine)
469 p = getLineFromPos(CursorPos);
470 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
471 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
475 if (event.KeyInput.Shift)
477 if (MarkBegin == MarkEnd)
478 newMarkBegin = CursorPos;
488 BlinkStartTime = porting::getTimeMs();
495 if (WordWrap || MultiLine)
497 p = getLineFromPos(CursorPos);
498 p = BrokenTextPositions[p];
501 if (event.KeyInput.Shift)
503 if (MarkBegin == MarkEnd)
504 newMarkBegin = CursorPos;
513 BlinkStartTime = porting::getTimeMs();
524 sendGuiEvent( EGET_EDITBOX_ENTER );
529 if (event.KeyInput.Shift)
533 if (MarkBegin == MarkEnd)
534 newMarkBegin = CursorPos;
536 newMarkEnd = CursorPos-1;
545 if (CursorPos > 0) CursorPos--;
546 BlinkStartTime = porting::getTimeMs();
550 if (event.KeyInput.Shift)
552 if (Text.size() > (u32)CursorPos)
554 if (MarkBegin == MarkEnd)
555 newMarkBegin = CursorPos;
557 newMarkEnd = CursorPos+1;
566 if (Text.size() > (u32)CursorPos) CursorPos++;
567 BlinkStartTime = porting::getTimeMs();
570 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
572 s32 lineNo = getLineFromPos(CursorPos);
573 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
576 s32 cp = CursorPos - BrokenTextPositions[lineNo];
577 if ((s32)BrokenText[lineNo-1].size() < cp)
578 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
580 CursorPos = BrokenTextPositions[lineNo-1] + cp;
583 if (event.KeyInput.Shift)
586 newMarkEnd = CursorPos;
601 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
603 s32 lineNo = getLineFromPos(CursorPos);
604 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
605 if (lineNo < (s32)BrokenText.size()-1)
607 s32 cp = CursorPos - BrokenTextPositions[lineNo];
608 if ((s32)BrokenText[lineNo+1].size() < cp)
609 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
611 CursorPos = BrokenTextPositions[lineNo+1] + cp;
614 if (event.KeyInput.Shift)
617 newMarkEnd = CursorPos;
633 if ( !this->IsEnabled )
640 if (MarkBegin != MarkEnd)
642 // delete marked text
643 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
644 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
646 s = Text.subString(0, realmbgn);
647 s.append( Text.subString(realmend, Text.size()-realmend) );
650 CursorPos = realmbgn;
654 // delete text behind cursor
656 s = Text.subString(0, CursorPos-1);
659 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
666 BlinkStartTime = porting::getTimeMs();
673 if ( !this->IsEnabled )
676 if (Text.size() != 0)
680 if (MarkBegin != MarkEnd)
682 // delete marked text
683 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
684 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
686 s = Text.subString(0, realmbgn);
687 s.append( Text.subString(realmend, Text.size()-realmend) );
690 CursorPos = realmbgn;
694 // delete text before cursor
695 s = Text.subString(0, CursorPos);
696 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
700 if (CursorPos > (s32)Text.size())
701 CursorPos = (s32)Text.size();
703 BlinkStartTime = porting::getTimeMs();
741 inputChar(event.KeyInput.Char);
745 // Set new text markers
746 setTextMarkers( newMarkBegin, newMarkEnd );
748 // break the text if it has changed
752 sendGuiEvent(EGET_EDITBOX_CHANGED);
755 calculateScrollPos();
761 //! draws the element and its children
762 void intlGUIEditBox::draw()
767 const bool focus = Environment->hasFocus(this);
769 IGUISkin* skin = Environment->getSkin();
773 FrameRect = AbsoluteRect;
779 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
780 false, true, FrameRect, &AbsoluteClippingRect);
782 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
783 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
784 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
785 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
787 core::rect<s32> localClipRect = FrameRect;
788 localClipRect.clipAgainst(AbsoluteClippingRect);
792 IGUIFont* font = OverrideFont;
794 font = skin->getFont();
797 s32 charcursorpos = 0;
801 if (LastBreakFont != font)
806 // calculate cursor pos
808 core::stringw *txtLine = &Text;
814 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
815 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
816 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
817 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
818 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
819 const s32 lineCount = ml ? BrokenText.size() : 1;
821 // Save the override color information.
822 // Then, alter it if the edit box is disabled.
823 const bool prevOver = OverrideColorEnabled;
824 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);
939 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);
955 //! Sets the new caption of this element.
956 void intlGUIEditBox::setText(const wchar_t* text)
959 if (u32(CursorPos) > Text.size())
960 CursorPos = Text.size();
966 //! Enables or disables automatic scrolling with cursor position
967 //! \param enable: If set to true, the text will move around with the cursor position
968 void intlGUIEditBox::setAutoScroll(bool enable)
974 //! Checks to see if automatic scrolling is enabled
975 //! \return true if automatic scrolling is enabled, false if not
976 bool intlGUIEditBox::isAutoScrollEnabled() const
978 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
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)))
1070 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1072 s32 newMarkBegin = MarkBegin;
1074 newMarkBegin = CursorPos;
1076 MouseMarking = true;
1077 setTextMarkers( newMarkBegin, CursorPos);
1078 calculateScrollPos();
1090 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1092 IGUIFont* font = OverrideFont;
1093 IGUISkin* skin = Environment->getSkin();
1095 font = skin->getFont();
1097 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1099 core::stringw *txtLine=0;
1103 for (u32 i=0; i < lineCount; ++i)
1106 if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1107 y = CurrentTextRect.UpperLeftCorner.Y;
1108 if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
1109 y = CurrentTextRect.LowerRightCorner.Y;
1111 // is it inside this region?
1112 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
1114 // we've found the clicked line
1115 txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
1116 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
1121 if (x < CurrentTextRect.UpperLeftCorner.X)
1122 x = CurrentTextRect.UpperLeftCorner.X;
1124 s32 idx = font->getCharacterFromPos(Text.c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1126 // click was on or left of the line
1128 return idx + startPos;
1130 // click was off the right edge of the line, go to end.
1131 return txtLine->size() + startPos;
1135 //! Breaks the single text line.
1136 void intlGUIEditBox::breakText()
1138 IGUISkin* skin = Environment->getSkin();
1140 if ((!WordWrap && !MultiLine) || !skin)
1143 BrokenText.clear(); // need to reallocate :/
1144 BrokenTextPositions.set_used(0);
1146 IGUIFont* font = OverrideFont;
1148 font = skin->getFont();
1153 LastBreakFont = font;
1157 core::stringw whitespace;
1158 s32 lastLineStart = 0;
1159 s32 size = Text.size();
1161 s32 elWidth = RelativeRect.getWidth() - 6;
1164 for (s32 i=0; i<size; ++i)
1167 bool lineBreak = false;
1169 if (c == L'\r') // Mac or Windows breaks
1173 if (Text[i+1] == L'\n') // Windows breaks
1179 else if (c == L'\n') // Unix breaks
1185 // don't break if we're not a multi-line edit box
1189 if (c == L' ' || c == 0 || i == (size-1))
1193 // here comes the next whitespace, look if
1194 // we can break the last word to the next line.
1195 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1196 s32 worldlgth = font->getDimension(word.c_str()).Width;
1198 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1200 // break to next line
1202 BrokenText.push_back(line);
1203 BrokenTextPositions.push_back(lastLineStart);
1204 lastLineStart = i - (s32)word.size();
1212 length += whitelgth + worldlgth;
1221 // compute line break
1226 BrokenText.push_back(line);
1227 BrokenTextPositions.push_back(lastLineStart);
1228 lastLineStart = i+1;
1237 // yippee this is a word..
1244 BrokenText.push_back(line);
1245 BrokenTextPositions.push_back(lastLineStart);
1249 void intlGUIEditBox::setTextRect(s32 line)
1251 core::dimension2du d;
1253 IGUISkin* skin = Environment->getSkin();
1257 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1262 // get text dimension
1263 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1264 if (WordWrap || MultiLine)
1266 d = font->getDimension(BrokenText[line].c_str());
1270 d = font->getDimension(Text.c_str());
1271 d.Height = AbsoluteRect.getHeight();
1273 d.Height += font->getKerningHeight();
1279 // align to h centre
1280 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1281 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1283 case EGUIA_LOWERRIGHT:
1284 // align to right edge
1285 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1286 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1289 // align to left edge
1290 CurrentTextRect.UpperLeftCorner.X = 0;
1291 CurrentTextRect.LowerRightCorner.X = d.Width;
1298 // align to v centre
1299 CurrentTextRect.UpperLeftCorner.Y =
1300 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1302 case EGUIA_LOWERRIGHT:
1303 // align to bottom edge
1304 CurrentTextRect.UpperLeftCorner.Y =
1305 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1308 // align to top edge
1309 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1313 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1314 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1315 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1316 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1318 CurrentTextRect += FrameRect.UpperLeftCorner;
1323 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1325 if (!WordWrap && !MultiLine)
1329 while (i < (s32)BrokenTextPositions.size())
1331 if (BrokenTextPositions[i] > pos)
1335 return (s32)BrokenTextPositions.size() - 1;
1339 void intlGUIEditBox::inputChar(wchar_t c)
1346 if (Text.size() < Max || Max == 0)
1350 if (MarkBegin != MarkEnd)
1352 // replace marked text
1353 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1354 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1356 s = Text.subString(0, realmbgn);
1358 s.append( Text.subString(realmend, Text.size()-realmend) );
1360 CursorPos = realmbgn+1;
1364 // add new character
1365 s = Text.subString(0, CursorPos);
1367 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1372 BlinkStartTime = porting::getTimeMs();
1373 setTextMarkers(0, 0);
1377 sendGuiEvent(EGET_EDITBOX_CHANGED);
1378 calculateScrollPos();
1382 void intlGUIEditBox::calculateScrollPos()
1387 // calculate horizontal scroll position
1388 s32 cursLine = getLineFromPos(CursorPos);
1389 setTextRect(cursLine);
1391 // don't do horizontal scrolling when wordwrap is enabled.
1394 // get cursor position
1395 IGUISkin* skin = Environment->getSkin();
1398 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1402 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1403 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1405 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1406 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1408 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1410 if (FrameRect.LowerRightCorner.X < cEnd)
1411 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1412 else if (FrameRect.UpperLeftCorner.X > cStart)
1413 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1417 // todo: adjust scrollbar
1420 // vertical scroll position
1421 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1422 VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1424 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1425 VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1429 // todo: adjust scrollbar
1432 //! set text markers
1433 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1435 if ( begin != MarkBegin || end != MarkEnd )
1439 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1443 //! send some gui event to parent
1444 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1449 e.EventType = EET_GUI_EVENT;
1450 e.GUIEvent.Caller = this;
1451 e.GUIEvent.Element = 0;
1452 e.GUIEvent.EventType = type;
1458 //! Writes attributes of the element.
1459 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1461 // IGUIEditBox::serializeAttributes(out,options);
1463 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1464 out->addColor ("OverrideColor", OverrideColor);
1465 // out->addFont("OverrideFont",OverrideFont);
1466 out->addInt ("MaxChars", Max);
1467 out->addBool ("WordWrap", WordWrap);
1468 out->addBool ("MultiLine", MultiLine);
1469 out->addBool ("AutoScroll", AutoScroll);
1470 out->addBool ("PasswordBox", PasswordBox);
1471 core::stringw ch = L" ";
1472 ch[0] = PasswordChar;
1473 out->addString("PasswordChar", ch.c_str());
1474 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1475 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1477 IGUIEditBox::serializeAttributes(out,options);
1481 //! Reads attributes of the element
1482 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1484 IGUIEditBox::deserializeAttributes(in,options);
1486 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1487 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1488 setMax(in->getAttributeAsInt("MaxChars"));
1489 setWordWrap(in->getAttributeAsBool("WordWrap"));
1490 setMultiLine(in->getAttributeAsBool("MultiLine"));
1491 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1492 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1495 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1497 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1499 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1500 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1502 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1506 } // end namespace gui
1507 } // end namespace irr
1509 #endif // _IRR_COMPILE_WITH_GUI_