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 )
636 if (MarkBegin != MarkEnd)
638 // delete marked text
639 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
640 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
642 s = Text.subString(0, realmbgn);
643 s.append( Text.subString(realmend, Text.size()-realmend) );
646 CursorPos = realmbgn;
650 // delete text behind cursor
652 s = Text.subString(0, CursorPos-1);
655 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
662 BlinkStartTime = porting::getTimeMs();
669 if ( !this->IsEnabled )
675 if (MarkBegin != MarkEnd)
677 // delete marked text
678 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
679 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
681 s = Text.subString(0, realmbgn);
682 s.append( Text.subString(realmend, Text.size()-realmend) );
685 CursorPos = realmbgn;
689 // delete text before cursor
690 s = Text.subString(0, CursorPos);
691 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
695 if (CursorPos > (s32)Text.size())
696 CursorPos = (s32)Text.size();
698 BlinkStartTime = porting::getTimeMs();
736 inputChar(event.KeyInput.Char);
740 // Set new text markers
741 setTextMarkers( newMarkBegin, newMarkEnd );
743 // break the text if it has changed
747 sendGuiEvent(EGET_EDITBOX_CHANGED);
750 calculateScrollPos();
756 //! draws the element and its children
757 void intlGUIEditBox::draw()
762 const bool focus = Environment->hasFocus(this);
764 IGUISkin* skin = Environment->getSkin();
768 FrameRect = AbsoluteRect;
774 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
775 false, true, FrameRect, &AbsoluteClippingRect);
777 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
778 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
779 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
780 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
782 core::rect<s32> localClipRect = FrameRect;
783 localClipRect.clipAgainst(AbsoluteClippingRect);
787 IGUIFont* font = OverrideFont;
789 font = skin->getFont();
792 s32 charcursorpos = 0;
796 if (LastBreakFont != font)
801 // calculate cursor pos
803 core::stringw *txtLine = &Text;
809 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
810 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
811 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
812 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
813 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
814 const s32 lineCount = ml ? BrokenText.size() : 1;
816 // Save the override color information.
817 // Then, alter it if the edit box is disabled.
818 const bool prevOver = OverrideColorEnabled;
819 const video::SColor prevColor = OverrideColor;
822 if (!IsEnabled && !OverrideColorEnabled)
824 OverrideColorEnabled = true;
825 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
828 for (s32 i=0; i < lineCount; ++i)
832 // clipping test - don't draw anything outside the visible area
833 core::rect<s32> c = localClipRect;
834 c.clipAgainst(CurrentTextRect);
841 if (BrokenText.size() != 1)
844 BrokenText.push_back(core::stringw());
846 if (BrokenText[0].size() != Text.size())
848 BrokenText[0] = Text;
849 for (u32 q = 0; q < Text.size(); ++q)
851 BrokenText[0] [q] = PasswordChar;
854 txtLine = &BrokenText[0];
859 txtLine = ml ? &BrokenText[i] : &Text;
860 startPos = ml ? BrokenTextPositions[i] : 0;
865 font->draw(txtLine->c_str(), CurrentTextRect,
866 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
867 false, true, &localClipRect);
869 // draw mark and marked text
870 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
873 s32 mbegin = 0, mend = 0;
874 s32 lineStartPos = 0, lineEndPos = txtLine->size();
878 // highlight start is on this line
879 s = txtLine->subString(0, realmbgn - startPos);
880 mbegin = font->getDimension(s.c_str()).Width;
883 mbegin += font->getKerningWidth(
884 &((*txtLine)[realmbgn - startPos]),
885 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
887 lineStartPos = realmbgn - startPos;
889 if (i == hlineStart + hlineCount - 1)
891 // highlight end is on this line
892 s2 = txtLine->subString(0, realmend - startPos);
893 mend = font->getDimension(s2.c_str()).Width;
894 lineEndPos = (s32)s2.size();
897 mend = font->getDimension(txtLine->c_str()).Width;
899 CurrentTextRect.UpperLeftCorner.X += mbegin;
900 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
903 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
906 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
909 font->draw(s.c_str(), CurrentTextRect,
910 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
911 false, true, &localClipRect);
916 // Return the override color information to its previous settings.
917 OverrideColorEnabled = prevOver;
918 OverrideColor = prevColor;
923 if (WordWrap || MultiLine)
925 cursorLine = getLineFromPos(CursorPos);
926 txtLine = &BrokenText[cursorLine];
927 startPos = BrokenTextPositions[cursorLine];
929 s = txtLine->subString(0,CursorPos-startPos);
930 charcursorpos = font->getDimension(s.c_str()).Width +
931 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
933 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350)
935 setTextRect(cursorLine);
936 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
938 font->draw(L"_", CurrentTextRect,
939 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
940 false, true, &localClipRect);
949 //! Sets the new caption of this element.
950 void intlGUIEditBox::setText(const wchar_t* text)
953 if (u32(CursorPos) > Text.size())
954 CursorPos = Text.size();
960 //! Enables or disables automatic scrolling with cursor position
961 //! \param enable: If set to true, the text will move around with the cursor position
962 void intlGUIEditBox::setAutoScroll(bool enable)
968 //! Checks to see if automatic scrolling is enabled
969 //! \return true if automatic scrolling is enabled, false if not
970 bool intlGUIEditBox::isAutoScrollEnabled() const
972 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
977 //! Gets the area of the text in the edit box
978 //! \return Returns the size in pixels of the text
979 core::dimension2du intlGUIEditBox::getTextDimension()
984 ret = CurrentTextRect;
986 for (u32 i=1; i < BrokenText.size(); ++i)
989 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
990 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
993 return core::dimension2du(ret.getSize());
997 //! Sets the maximum amount of characters which may be entered in the box.
998 //! \param max: Maximum amount of characters. If 0, the character amount is
1000 void intlGUIEditBox::setMax(u32 max)
1004 if (Text.size() > Max && Max != 0)
1005 Text = Text.subString(0, Max);
1009 //! Returns maximum amount of characters, previously set by setMax();
1010 u32 intlGUIEditBox::getMax() const
1016 bool intlGUIEditBox::processMouse(const SEvent& event)
1018 switch(event.MouseInput.Event)
1020 case irr::EMIE_LMOUSE_LEFT_UP:
1021 if (Environment->hasFocus(this))
1023 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1026 setTextMarkers( MarkBegin, CursorPos );
1028 MouseMarking = false;
1029 calculateScrollPos();
1033 case irr::EMIE_MOUSE_MOVED:
1037 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1038 setTextMarkers( MarkBegin, CursorPos );
1039 calculateScrollPos();
1044 case EMIE_LMOUSE_PRESSED_DOWN:
1045 if (!Environment->hasFocus(this))
1047 BlinkStartTime = porting::getTimeMs();
1048 MouseMarking = true;
1049 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1050 setTextMarkers(CursorPos, CursorPos );
1051 calculateScrollPos();
1056 if (!AbsoluteClippingRect.isPointInside(
1057 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
1063 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1065 s32 newMarkBegin = MarkBegin;
1067 newMarkBegin = CursorPos;
1069 MouseMarking = true;
1070 setTextMarkers( newMarkBegin, CursorPos);
1071 calculateScrollPos();
1082 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1084 IGUIFont* font = OverrideFont;
1085 IGUISkin* skin = Environment->getSkin();
1087 font = skin->getFont();
1089 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1091 core::stringw *txtLine = NULL;
1093 u32 curr_line_idx = 0;
1096 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1097 setTextRect(curr_line_idx);
1098 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1099 y = CurrentTextRect.UpperLeftCorner.Y;
1100 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1101 y = CurrentTextRect.LowerRightCorner.Y;
1103 // is it inside this region?
1104 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1105 // we've found the clicked line
1106 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1107 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1112 if (x < CurrentTextRect.UpperLeftCorner.X)
1113 x = CurrentTextRect.UpperLeftCorner.X;
1114 else if (x > CurrentTextRect.LowerRightCorner.X)
1115 x = CurrentTextRect.LowerRightCorner.X;
1117 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1118 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1119 // will be the last char, not null char of the wstring
1120 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1123 return rangelim(idx + startPos, 0, S32_MAX);
1127 //! Breaks the single text line.
1128 void intlGUIEditBox::breakText()
1130 IGUISkin* skin = Environment->getSkin();
1132 if ((!WordWrap && !MultiLine) || !skin)
1135 BrokenText.clear(); // need to reallocate :/
1136 BrokenTextPositions.set_used(0);
1138 IGUIFont* font = OverrideFont;
1140 font = skin->getFont();
1145 LastBreakFont = font;
1149 core::stringw whitespace;
1150 s32 lastLineStart = 0;
1151 s32 size = Text.size();
1153 s32 elWidth = RelativeRect.getWidth() - 6;
1156 for (s32 i=0; i<size; ++i)
1159 bool lineBreak = false;
1161 if (c == L'\r') // Mac or Windows breaks
1165 if (Text[i+1] == L'\n') // Windows breaks
1171 else if (c == L'\n') // Unix breaks
1177 // don't break if we're not a multi-line edit box
1181 if (c == L' ' || c == 0 || i == (size-1))
1183 if (!word.empty()) {
1184 // here comes the next whitespace, look if
1185 // we can break the last word to the next line.
1186 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1187 s32 worldlgth = font->getDimension(word.c_str()).Width;
1189 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1191 // break to next line
1193 BrokenText.push_back(line);
1194 BrokenTextPositions.push_back(lastLineStart);
1195 lastLineStart = i - (s32)word.size();
1203 length += whitelgth + worldlgth;
1212 // compute line break
1217 BrokenText.push_back(line);
1218 BrokenTextPositions.push_back(lastLineStart);
1219 lastLineStart = i+1;
1228 // yippee this is a word..
1235 BrokenText.push_back(line);
1236 BrokenTextPositions.push_back(lastLineStart);
1240 void intlGUIEditBox::setTextRect(s32 line)
1242 core::dimension2du d;
1244 IGUISkin* skin = Environment->getSkin();
1248 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1253 // get text dimension
1254 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1255 if (WordWrap || MultiLine)
1257 d = font->getDimension(BrokenText[line].c_str());
1261 d = font->getDimension(Text.c_str());
1262 d.Height = AbsoluteRect.getHeight();
1264 d.Height += font->getKerningHeight();
1270 // align to h centre
1271 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1272 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1274 case EGUIA_LOWERRIGHT:
1275 // align to right edge
1276 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1277 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1280 // align to left edge
1281 CurrentTextRect.UpperLeftCorner.X = 0;
1282 CurrentTextRect.LowerRightCorner.X = d.Width;
1289 // align to v centre
1290 CurrentTextRect.UpperLeftCorner.Y =
1291 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1293 case EGUIA_LOWERRIGHT:
1294 // align to bottom edge
1295 CurrentTextRect.UpperLeftCorner.Y =
1296 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1299 // align to top edge
1300 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1304 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1305 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1306 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1307 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1309 CurrentTextRect += FrameRect.UpperLeftCorner;
1314 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1316 if (!WordWrap && !MultiLine)
1320 while (i < (s32)BrokenTextPositions.size())
1322 if (BrokenTextPositions[i] > pos)
1326 return (s32)BrokenTextPositions.size() - 1;
1330 void intlGUIEditBox::inputChar(wchar_t c)
1337 if (Text.size() < Max || Max == 0)
1341 if (MarkBegin != MarkEnd)
1343 // replace marked text
1344 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1345 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1347 s = Text.subString(0, realmbgn);
1349 s.append( Text.subString(realmend, Text.size()-realmend) );
1351 CursorPos = realmbgn+1;
1355 // add new character
1356 s = Text.subString(0, CursorPos);
1358 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1363 BlinkStartTime = porting::getTimeMs();
1364 setTextMarkers(0, 0);
1368 sendGuiEvent(EGET_EDITBOX_CHANGED);
1369 calculateScrollPos();
1373 void intlGUIEditBox::calculateScrollPos()
1378 // calculate horizontal scroll position
1379 s32 cursLine = getLineFromPos(CursorPos);
1380 setTextRect(cursLine);
1382 // don't do horizontal scrolling when wordwrap is enabled.
1385 // get cursor position
1386 IGUISkin* skin = Environment->getSkin();
1389 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1393 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1394 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1396 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1397 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1399 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1401 if (FrameRect.LowerRightCorner.X < cEnd)
1402 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1403 else if (FrameRect.UpperLeftCorner.X > cStart)
1404 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1408 // todo: adjust scrollbar
1411 // vertical scroll position
1412 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1413 VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1415 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1416 VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1420 // todo: adjust scrollbar
1423 //! set text markers
1424 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1426 if ( begin != MarkBegin || end != MarkEnd )
1430 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1434 //! send some gui event to parent
1435 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1440 e.EventType = EET_GUI_EVENT;
1441 e.GUIEvent.Caller = this;
1442 e.GUIEvent.Element = 0;
1443 e.GUIEvent.EventType = type;
1449 //! Writes attributes of the element.
1450 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1452 // IGUIEditBox::serializeAttributes(out,options);
1454 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1455 out->addColor ("OverrideColor", OverrideColor);
1456 // out->addFont("OverrideFont",OverrideFont);
1457 out->addInt ("MaxChars", Max);
1458 out->addBool ("WordWrap", WordWrap);
1459 out->addBool ("MultiLine", MultiLine);
1460 out->addBool ("AutoScroll", AutoScroll);
1461 out->addBool ("PasswordBox", PasswordBox);
1462 core::stringw ch = L" ";
1463 ch[0] = PasswordChar;
1464 out->addString("PasswordChar", ch.c_str());
1465 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1466 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1468 IGUIEditBox::serializeAttributes(out,options);
1472 //! Reads attributes of the element
1473 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1475 IGUIEditBox::deserializeAttributes(in,options);
1477 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1478 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1479 setMax(in->getAttributeAsInt("MaxChars"));
1480 setWordWrap(in->getAttributeAsBool("WordWrap"));
1481 setMultiLine(in->getAttributeAsBool("MultiLine"));
1482 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1483 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1486 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1488 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1490 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1491 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1493 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1497 } // end namespace gui
1498 } // end namespace irr
1500 #endif // _IRR_COMPILE_WITH_GUI_