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)
65 : IGUIEditBox(environment, parent, id, rectangle),
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;
103 intlGUIEditBox::~intlGUIEditBox()
106 OverrideFont->drop();
113 //! Sets another skin independent font.
114 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
116 if (OverrideFont == font)
120 OverrideFont->drop();
125 OverrideFont->grab();
130 IGUIFont * intlGUIEditBox::getOverrideFont() const
135 //! Get the font which is used right now for drawing
136 IGUIFont* intlGUIEditBox::getActiveFont() const
140 IGUISkin* skin = Environment->getSkin();
142 return skin->getFont();
146 //! Sets another color for the text.
147 void intlGUIEditBox::setOverrideColor(video::SColor color)
149 OverrideColor = color;
150 OverrideColorEnabled = true;
153 video::SColor intlGUIEditBox::getOverrideColor() const
155 return OverrideColor;
158 //! Turns the border on or off
159 void intlGUIEditBox::setDrawBorder(bool border)
164 //! Sets whether to draw the background
165 void intlGUIEditBox::setDrawBackground(bool draw)
169 //! Sets if the text should use the overide color or the color in the gui skin.
170 void intlGUIEditBox::enableOverrideColor(bool enable)
172 OverrideColorEnabled = enable;
175 bool intlGUIEditBox::isOverrideColorEnabled() const
177 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
178 return OverrideColorEnabled;
181 //! Enables or disables word wrap
182 void intlGUIEditBox::setWordWrap(bool enable)
189 void intlGUIEditBox::updateAbsolutePosition()
191 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
192 IGUIElement::updateAbsolutePosition();
193 if ( oldAbsoluteRect != AbsoluteRect )
200 //! Checks if word wrap is enabled
201 bool intlGUIEditBox::isWordWrapEnabled() const
203 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
208 //! Enables or disables newlines.
209 void intlGUIEditBox::setMultiLine(bool enable)
215 //! Checks if multi line editing is enabled
216 bool intlGUIEditBox::isMultiLineEnabled() const
218 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
223 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
225 PasswordBox = passwordBox;
228 PasswordChar = passwordChar;
236 bool intlGUIEditBox::isPasswordBox() const
238 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
243 //! Sets text justification
244 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
251 //! called if an event happened.
252 bool intlGUIEditBox::OnEvent(const SEvent& event)
257 switch(event.EventType)
260 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
262 if (event.GUIEvent.Caller == this)
264 MouseMarking = false;
269 case EET_KEY_INPUT_EVENT:
271 #if (defined(__linux__) || defined(__FreeBSD__))
272 // ################################################################
274 // This part is the difference from the original intlGUIEditBox
275 // It converts UTF-8 character into a UCS-2 (wchar_t)
277 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
279 //printf( "char: %lc (%u) \r\n", wc, wc );
281 SEvent irrevent(event);
282 irrevent.KeyInput.Char = wc;
283 // ################################################################
285 if (processKey(irrevent))
288 if (processKey(event))
290 #endif // defined(linux)
294 case EET_MOUSE_INPUT_EVENT:
295 if (processMouse(event))
303 return IGUIElement::OnEvent(event);
307 bool intlGUIEditBox::processKey(const SEvent& event)
309 if (!event.KeyInput.PressedDown)
312 bool textChanged = false;
313 s32 newMarkBegin = MarkBegin;
314 s32 newMarkEnd = MarkEnd;
316 // control shortcut handling
318 if (event.KeyInput.Control)
320 // german backlash '\' entered with control + '?'
321 if ( event.KeyInput.Char == '\\' )
323 inputChar(event.KeyInput.Char);
327 switch(event.KeyInput.Key)
332 newMarkEnd = Text.size();
336 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
338 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
339 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
342 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
343 Operator->copyToClipboard(s.c_str());
347 // cut to the clipboard
348 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
350 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
351 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
355 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
356 Operator->copyToClipboard(sc.c_str());
362 s = Text.subString(0, realmbgn);
363 s.append( Text.subString(realmend, Text.size()-realmend) );
366 CursorPos = realmbgn;
377 // paste from the clipboard
380 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
381 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
384 const c8* p = Operator->getTextFromClipboard();
387 if (MarkBegin == MarkEnd)
390 core::stringw s = Text.subString(0, CursorPos);
392 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
394 if (!Max || s.size()<=Max) // thx to Fish FH for fix
398 CursorPos += s.size();
405 core::stringw s = Text.subString(0, realmbgn);
407 s.append( Text.subString(realmend, Text.size()-realmend) );
409 if (!Max || s.size()<=Max) // thx to Fish FH for fix
413 CursorPos = realmbgn + s.size();
424 // move/highlight to start of text
425 if (event.KeyInput.Shift)
427 newMarkEnd = CursorPos;
439 // move/highlight to end of text
440 if (event.KeyInput.Shift)
442 newMarkBegin = CursorPos;
443 newMarkEnd = Text.size();
448 CursorPos = Text.size();
457 // default keyboard handling
459 switch(event.KeyInput.Key)
464 if (WordWrap || MultiLine)
466 p = getLineFromPos(CursorPos);
467 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
468 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
472 if (event.KeyInput.Shift)
474 if (MarkBegin == MarkEnd)
475 newMarkBegin = CursorPos;
485 BlinkStartTime = porting::getTimeMs();
492 if (WordWrap || MultiLine)
494 p = getLineFromPos(CursorPos);
495 p = BrokenTextPositions[p];
498 if (event.KeyInput.Shift)
500 if (MarkBegin == MarkEnd)
501 newMarkBegin = CursorPos;
510 BlinkStartTime = porting::getTimeMs();
521 sendGuiEvent( EGET_EDITBOX_ENTER );
526 if (event.KeyInput.Shift)
530 if (MarkBegin == MarkEnd)
531 newMarkBegin = CursorPos;
533 newMarkEnd = CursorPos-1;
542 if (CursorPos > 0) CursorPos--;
543 BlinkStartTime = porting::getTimeMs();
547 if (event.KeyInput.Shift)
549 if (Text.size() > (u32)CursorPos)
551 if (MarkBegin == MarkEnd)
552 newMarkBegin = CursorPos;
554 newMarkEnd = CursorPos+1;
563 if (Text.size() > (u32)CursorPos) CursorPos++;
564 BlinkStartTime = porting::getTimeMs();
567 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
569 s32 lineNo = getLineFromPos(CursorPos);
570 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
573 s32 cp = CursorPos - BrokenTextPositions[lineNo];
574 if ((s32)BrokenText[lineNo-1].size() < cp)
575 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
577 CursorPos = BrokenTextPositions[lineNo-1] + cp;
580 if (event.KeyInput.Shift)
583 newMarkEnd = CursorPos;
598 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
600 s32 lineNo = getLineFromPos(CursorPos);
601 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
602 if (lineNo < (s32)BrokenText.size()-1)
604 s32 cp = CursorPos - BrokenTextPositions[lineNo];
605 if ((s32)BrokenText[lineNo+1].size() < cp)
606 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
608 CursorPos = BrokenTextPositions[lineNo+1] + cp;
611 if (event.KeyInput.Shift)
614 newMarkEnd = CursorPos;
630 if ( !this->IsEnabled )
637 if (MarkBegin != MarkEnd)
639 // delete marked text
640 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
641 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
643 s = Text.subString(0, realmbgn);
644 s.append( Text.subString(realmend, Text.size()-realmend) );
647 CursorPos = realmbgn;
651 // delete text behind cursor
653 s = Text.subString(0, CursorPos-1);
656 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
663 BlinkStartTime = porting::getTimeMs();
670 if ( !this->IsEnabled )
673 if (Text.size() != 0)
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;
776 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
777 false, true, FrameRect, &AbsoluteClippingRect);
779 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
780 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
781 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
782 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
784 core::rect<s32> localClipRect = FrameRect;
785 localClipRect.clipAgainst(AbsoluteClippingRect);
789 IGUIFont* font = OverrideFont;
791 font = skin->getFont();
794 s32 charcursorpos = 0;
798 if (LastBreakFont != font)
803 // calculate cursor pos
805 core::stringw *txtLine = &Text;
811 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
812 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
813 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
814 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
815 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
816 const s32 lineCount = ml ? BrokenText.size() : 1;
818 // Save the override color information.
819 // Then, alter it if the edit box is disabled.
820 const bool prevOver = OverrideColorEnabled;
821 const video::SColor prevColor = OverrideColor;
825 if (!IsEnabled && !OverrideColorEnabled)
827 OverrideColorEnabled = true;
828 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
831 for (s32 i=0; i < lineCount; ++i)
835 // clipping test - don't draw anything outside the visible area
836 core::rect<s32> c = localClipRect;
837 c.clipAgainst(CurrentTextRect);
844 if (BrokenText.size() != 1)
847 BrokenText.push_back(core::stringw());
849 if (BrokenText[0].size() != Text.size())
851 BrokenText[0] = Text;
852 for (u32 q = 0; q < Text.size(); ++q)
854 BrokenText[0] [q] = PasswordChar;
857 txtLine = &BrokenText[0];
862 txtLine = ml ? &BrokenText[i] : &Text;
863 startPos = ml ? BrokenTextPositions[i] : 0;
868 font->draw(txtLine->c_str(), CurrentTextRect,
869 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
870 false, true, &localClipRect);
872 // draw mark and marked text
873 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
876 s32 mbegin = 0, mend = 0;
877 s32 lineStartPos = 0, lineEndPos = txtLine->size();
881 // highlight start is on this line
882 s = txtLine->subString(0, realmbgn - startPos);
883 mbegin = font->getDimension(s.c_str()).Width;
886 mbegin += font->getKerningWidth(
887 &((*txtLine)[realmbgn - startPos]),
888 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
890 lineStartPos = realmbgn - startPos;
892 if (i == hlineStart + hlineCount - 1)
894 // highlight end is on this line
895 s2 = txtLine->subString(0, realmend - startPos);
896 mend = font->getDimension(s2.c_str()).Width;
897 lineEndPos = (s32)s2.size();
900 mend = font->getDimension(txtLine->c_str()).Width;
902 CurrentTextRect.UpperLeftCorner.X += mbegin;
903 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
906 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
909 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
912 font->draw(s.c_str(), CurrentTextRect,
913 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
914 false, true, &localClipRect);
919 // Return the override color information to its previous settings.
920 OverrideColorEnabled = prevOver;
921 OverrideColor = prevColor;
926 if (WordWrap || MultiLine)
928 cursorLine = getLineFromPos(CursorPos);
929 txtLine = &BrokenText[cursorLine];
930 startPos = BrokenTextPositions[cursorLine];
932 s = txtLine->subString(0,CursorPos-startPos);
933 charcursorpos = font->getDimension(s.c_str()).Width +
934 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
936 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350)
938 setTextRect(cursorLine);
939 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
941 font->draw(L"_", CurrentTextRect,
942 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
943 false, true, &localClipRect);
952 //! Sets the new caption of this element.
953 void intlGUIEditBox::setText(const wchar_t* text)
956 if (u32(CursorPos) > Text.size())
957 CursorPos = Text.size();
963 //! Enables or disables automatic scrolling with cursor position
964 //! \param enable: If set to true, the text will move around with the cursor position
965 void intlGUIEditBox::setAutoScroll(bool enable)
971 //! Checks to see if automatic scrolling is enabled
972 //! \return true if automatic scrolling is enabled, false if not
973 bool intlGUIEditBox::isAutoScrollEnabled() const
975 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
980 //! Gets the area of the text in the edit box
981 //! \return Returns the size in pixels of the text
982 core::dimension2du intlGUIEditBox::getTextDimension()
987 ret = CurrentTextRect;
989 for (u32 i=1; i < BrokenText.size(); ++i)
992 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
993 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
996 return core::dimension2du(ret.getSize());
1000 //! Sets the maximum amount of characters which may be entered in the box.
1001 //! \param max: Maximum amount of characters. If 0, the character amount is
1003 void intlGUIEditBox::setMax(u32 max)
1007 if (Text.size() > Max && Max != 0)
1008 Text = Text.subString(0, Max);
1012 //! Returns maximum amount of characters, previously set by setMax();
1013 u32 intlGUIEditBox::getMax() const
1019 bool intlGUIEditBox::processMouse(const SEvent& event)
1021 switch(event.MouseInput.Event)
1023 case irr::EMIE_LMOUSE_LEFT_UP:
1024 if (Environment->hasFocus(this))
1026 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1029 setTextMarkers( MarkBegin, CursorPos );
1031 MouseMarking = false;
1032 calculateScrollPos();
1036 case irr::EMIE_MOUSE_MOVED:
1040 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1041 setTextMarkers( MarkBegin, CursorPos );
1042 calculateScrollPos();
1047 case EMIE_LMOUSE_PRESSED_DOWN:
1048 if (!Environment->hasFocus(this))
1050 BlinkStartTime = porting::getTimeMs();
1051 MouseMarking = true;
1052 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1053 setTextMarkers(CursorPos, CursorPos );
1054 calculateScrollPos();
1059 if (!AbsoluteClippingRect.isPointInside(
1060 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
1067 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1069 s32 newMarkBegin = MarkBegin;
1071 newMarkBegin = CursorPos;
1073 MouseMarking = true;
1074 setTextMarkers( newMarkBegin, CursorPos);
1075 calculateScrollPos();
1087 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1089 IGUIFont* font = OverrideFont;
1090 IGUISkin* skin = Environment->getSkin();
1092 font = skin->getFont();
1094 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1096 core::stringw *txtLine = NULL;
1098 u32 curr_line_idx = 0;
1101 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1102 setTextRect(curr_line_idx);
1103 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1104 y = CurrentTextRect.UpperLeftCorner.Y;
1105 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1106 y = CurrentTextRect.LowerRightCorner.Y;
1108 // is it inside this region?
1109 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1110 // we've found the clicked line
1111 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1112 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1117 if (x < CurrentTextRect.UpperLeftCorner.X)
1118 x = CurrentTextRect.UpperLeftCorner.X;
1119 else if (x > CurrentTextRect.LowerRightCorner.X)
1120 x = CurrentTextRect.LowerRightCorner.X;
1122 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1123 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1124 // will be the last char, not null char of the wstring
1125 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1128 return rangelim(idx + startPos, 0, S32_MAX);
1132 //! Breaks the single text line.
1133 void intlGUIEditBox::breakText()
1135 IGUISkin* skin = Environment->getSkin();
1137 if ((!WordWrap && !MultiLine) || !skin)
1140 BrokenText.clear(); // need to reallocate :/
1141 BrokenTextPositions.set_used(0);
1143 IGUIFont* font = OverrideFont;
1145 font = skin->getFont();
1150 LastBreakFont = font;
1154 core::stringw whitespace;
1155 s32 lastLineStart = 0;
1156 s32 size = Text.size();
1158 s32 elWidth = RelativeRect.getWidth() - 6;
1161 for (s32 i=0; i<size; ++i)
1164 bool lineBreak = false;
1166 if (c == L'\r') // Mac or Windows breaks
1170 if (Text[i+1] == L'\n') // Windows breaks
1176 else if (c == L'\n') // Unix breaks
1182 // don't break if we're not a multi-line edit box
1186 if (c == L' ' || c == 0 || i == (size-1))
1190 // here comes the next whitespace, look if
1191 // we can break the last word to the next line.
1192 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1193 s32 worldlgth = font->getDimension(word.c_str()).Width;
1195 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1197 // break to next line
1199 BrokenText.push_back(line);
1200 BrokenTextPositions.push_back(lastLineStart);
1201 lastLineStart = i - (s32)word.size();
1209 length += whitelgth + worldlgth;
1218 // compute line break
1223 BrokenText.push_back(line);
1224 BrokenTextPositions.push_back(lastLineStart);
1225 lastLineStart = i+1;
1234 // yippee this is a word..
1241 BrokenText.push_back(line);
1242 BrokenTextPositions.push_back(lastLineStart);
1246 void intlGUIEditBox::setTextRect(s32 line)
1248 core::dimension2du d;
1250 IGUISkin* skin = Environment->getSkin();
1254 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1259 // get text dimension
1260 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1261 if (WordWrap || MultiLine)
1263 d = font->getDimension(BrokenText[line].c_str());
1267 d = font->getDimension(Text.c_str());
1268 d.Height = AbsoluteRect.getHeight();
1270 d.Height += font->getKerningHeight();
1276 // align to h centre
1277 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1278 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1280 case EGUIA_LOWERRIGHT:
1281 // align to right edge
1282 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1283 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1286 // align to left edge
1287 CurrentTextRect.UpperLeftCorner.X = 0;
1288 CurrentTextRect.LowerRightCorner.X = d.Width;
1295 // align to v centre
1296 CurrentTextRect.UpperLeftCorner.Y =
1297 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1299 case EGUIA_LOWERRIGHT:
1300 // align to bottom edge
1301 CurrentTextRect.UpperLeftCorner.Y =
1302 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1305 // align to top edge
1306 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1310 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1311 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1312 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1313 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1315 CurrentTextRect += FrameRect.UpperLeftCorner;
1320 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1322 if (!WordWrap && !MultiLine)
1326 while (i < (s32)BrokenTextPositions.size())
1328 if (BrokenTextPositions[i] > pos)
1332 return (s32)BrokenTextPositions.size() - 1;
1336 void intlGUIEditBox::inputChar(wchar_t c)
1343 if (Text.size() < Max || Max == 0)
1347 if (MarkBegin != MarkEnd)
1349 // replace marked text
1350 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1351 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1353 s = Text.subString(0, realmbgn);
1355 s.append( Text.subString(realmend, Text.size()-realmend) );
1357 CursorPos = realmbgn+1;
1361 // add new character
1362 s = Text.subString(0, CursorPos);
1364 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1369 BlinkStartTime = porting::getTimeMs();
1370 setTextMarkers(0, 0);
1374 sendGuiEvent(EGET_EDITBOX_CHANGED);
1375 calculateScrollPos();
1379 void intlGUIEditBox::calculateScrollPos()
1384 // calculate horizontal scroll position
1385 s32 cursLine = getLineFromPos(CursorPos);
1386 setTextRect(cursLine);
1388 // don't do horizontal scrolling when wordwrap is enabled.
1391 // get cursor position
1392 IGUISkin* skin = Environment->getSkin();
1395 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1399 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1400 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1402 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1403 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1405 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1407 if (FrameRect.LowerRightCorner.X < cEnd)
1408 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1409 else if (FrameRect.UpperLeftCorner.X > cStart)
1410 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1414 // todo: adjust scrollbar
1417 // vertical scroll position
1418 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1419 VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1421 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1422 VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1426 // todo: adjust scrollbar
1429 //! set text markers
1430 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1432 if ( begin != MarkBegin || end != MarkEnd )
1436 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1440 //! send some gui event to parent
1441 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1446 e.EventType = EET_GUI_EVENT;
1447 e.GUIEvent.Caller = this;
1448 e.GUIEvent.Element = 0;
1449 e.GUIEvent.EventType = type;
1455 //! Writes attributes of the element.
1456 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1458 // IGUIEditBox::serializeAttributes(out,options);
1460 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1461 out->addColor ("OverrideColor", OverrideColor);
1462 // out->addFont("OverrideFont",OverrideFont);
1463 out->addInt ("MaxChars", Max);
1464 out->addBool ("WordWrap", WordWrap);
1465 out->addBool ("MultiLine", MultiLine);
1466 out->addBool ("AutoScroll", AutoScroll);
1467 out->addBool ("PasswordBox", PasswordBox);
1468 core::stringw ch = L" ";
1469 ch[0] = PasswordChar;
1470 out->addString("PasswordChar", ch.c_str());
1471 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1472 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1474 IGUIEditBox::serializeAttributes(out,options);
1478 //! Reads attributes of the element
1479 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1481 IGUIEditBox::deserializeAttributes(in,options);
1483 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1484 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1485 setMax(in->getAttributeAsInt("MaxChars"));
1486 setWordWrap(in->getAttributeAsBool("WordWrap"));
1487 setMultiLine(in->getAttributeAsBool("MultiLine"));
1488 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1489 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1492 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1494 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1496 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1497 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1499 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1503 } // end namespace gui
1504 } // end namespace irr
1506 #endif // _IRR_COMPILE_WITH_GUI_