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), MouseMarking(false),
66 Border(border), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
67 OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
68 Operator(0), BlinkStartTime(0), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
69 WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
70 PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
71 CurrentTextRect(0,0,1,1), FrameRect(rectangle)
74 setDebugName("intlintlGUIEditBox");
80 Operator = Environment->getOSOperator();
85 // this element can be tabbed to
91 skin = Environment->getSkin();
94 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
95 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
96 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
97 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
102 calculateScrollPos();
107 intlGUIEditBox::~intlGUIEditBox()
110 OverrideFont->drop();
117 //! Sets another skin independent font.
118 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
120 if (OverrideFont == font)
124 OverrideFont->drop();
129 OverrideFont->grab();
134 IGUIFont * intlGUIEditBox::getOverrideFont() const
139 //! Get the font which is used right now for drawing
140 IGUIFont* intlGUIEditBox::getActiveFont() const
144 IGUISkin* skin = Environment->getSkin();
146 return skin->getFont();
150 //! Sets another color for the text.
151 void intlGUIEditBox::setOverrideColor(video::SColor color)
153 OverrideColor = color;
154 OverrideColorEnabled = true;
157 video::SColor intlGUIEditBox::getOverrideColor() const
159 return OverrideColor;
162 //! Turns the border on or off
163 void intlGUIEditBox::setDrawBorder(bool border)
168 //! Sets whether to draw the background
169 void intlGUIEditBox::setDrawBackground(bool draw)
173 //! Sets if the text should use the overide color or the color in the gui skin.
174 void intlGUIEditBox::enableOverrideColor(bool enable)
176 OverrideColorEnabled = enable;
179 bool intlGUIEditBox::isOverrideColorEnabled() const
181 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
182 return OverrideColorEnabled;
185 //! Enables or disables word wrap
186 void intlGUIEditBox::setWordWrap(bool enable)
193 void intlGUIEditBox::updateAbsolutePosition()
195 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
196 IGUIElement::updateAbsolutePosition();
197 if ( oldAbsoluteRect != AbsoluteRect )
204 //! Checks if word wrap is enabled
205 bool intlGUIEditBox::isWordWrapEnabled() const
207 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
212 //! Enables or disables newlines.
213 void intlGUIEditBox::setMultiLine(bool enable)
219 //! Checks if multi line editing is enabled
220 bool intlGUIEditBox::isMultiLineEnabled() const
222 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
227 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
229 PasswordBox = passwordBox;
232 PasswordChar = passwordChar;
240 bool intlGUIEditBox::isPasswordBox() const
242 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
247 //! Sets text justification
248 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
255 //! called if an event happened.
256 bool intlGUIEditBox::OnEvent(const SEvent& event)
261 switch(event.EventType)
264 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
266 if (event.GUIEvent.Caller == this)
268 MouseMarking = false;
273 case EET_KEY_INPUT_EVENT:
275 #if (defined(__linux__) || defined(__FreeBSD__))
276 // ################################################################
278 // This part is the difference from the original intlGUIEditBox
279 // It converts UTF-8 character into a UCS-2 (wchar_t)
281 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
283 //printf( "char: %lc (%u) \r\n", wc, wc );
285 SEvent irrevent(event);
286 irrevent.KeyInput.Char = wc;
287 // ################################################################
289 if (processKey(irrevent))
292 if (processKey(event))
294 #endif // defined(linux)
298 case EET_MOUSE_INPUT_EVENT:
299 if (processMouse(event))
307 return IGUIElement::OnEvent(event);
311 bool intlGUIEditBox::processKey(const SEvent& event)
313 if (!event.KeyInput.PressedDown)
316 bool textChanged = false;
317 s32 newMarkBegin = MarkBegin;
318 s32 newMarkEnd = MarkEnd;
320 // control shortcut handling
322 if (event.KeyInput.Control)
324 // german backlash '\' entered with control + '?'
325 if ( event.KeyInput.Char == '\\' )
327 inputChar(event.KeyInput.Char);
331 switch(event.KeyInput.Key)
336 newMarkEnd = Text.size();
340 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
342 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
343 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
346 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
347 Operator->copyToClipboard(s.c_str());
351 // cut to the clipboard
352 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
354 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
355 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
359 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
360 Operator->copyToClipboard(sc.c_str());
366 s = Text.subString(0, realmbgn);
367 s.append( Text.subString(realmend, Text.size()-realmend) );
370 CursorPos = realmbgn;
381 // paste from the clipboard
384 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
385 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
388 const c8* p = Operator->getTextFromClipboard();
391 if (MarkBegin == MarkEnd)
394 core::stringw s = Text.subString(0, CursorPos);
396 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
398 if (!Max || s.size()<=Max) // thx to Fish FH for fix
402 CursorPos += s.size();
409 core::stringw s = Text.subString(0, realmbgn);
411 s.append( Text.subString(realmend, Text.size()-realmend) );
413 if (!Max || s.size()<=Max) // thx to Fish FH for fix
417 CursorPos = realmbgn + s.size();
428 // move/highlight to start of text
429 if (event.KeyInput.Shift)
431 newMarkEnd = CursorPos;
443 // move/highlight to end of text
444 if (event.KeyInput.Shift)
446 newMarkBegin = CursorPos;
447 newMarkEnd = Text.size();
452 CursorPos = Text.size();
461 // default keyboard handling
463 switch(event.KeyInput.Key)
468 if (WordWrap || MultiLine)
470 p = getLineFromPos(CursorPos);
471 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
472 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
476 if (event.KeyInput.Shift)
478 if (MarkBegin == MarkEnd)
479 newMarkBegin = CursorPos;
489 BlinkStartTime = porting::getTimeMs();
496 if (WordWrap || MultiLine)
498 p = getLineFromPos(CursorPos);
499 p = BrokenTextPositions[p];
502 if (event.KeyInput.Shift)
504 if (MarkBegin == MarkEnd)
505 newMarkBegin = CursorPos;
514 BlinkStartTime = porting::getTimeMs();
525 sendGuiEvent( EGET_EDITBOX_ENTER );
530 if (event.KeyInput.Shift)
534 if (MarkBegin == MarkEnd)
535 newMarkBegin = CursorPos;
537 newMarkEnd = CursorPos-1;
546 if (CursorPos > 0) CursorPos--;
547 BlinkStartTime = porting::getTimeMs();
551 if (event.KeyInput.Shift)
553 if (Text.size() > (u32)CursorPos)
555 if (MarkBegin == MarkEnd)
556 newMarkBegin = CursorPos;
558 newMarkEnd = CursorPos+1;
567 if (Text.size() > (u32)CursorPos) CursorPos++;
568 BlinkStartTime = porting::getTimeMs();
571 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
573 s32 lineNo = getLineFromPos(CursorPos);
574 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
577 s32 cp = CursorPos - BrokenTextPositions[lineNo];
578 if ((s32)BrokenText[lineNo-1].size() < cp)
579 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
581 CursorPos = BrokenTextPositions[lineNo-1] + cp;
584 if (event.KeyInput.Shift)
587 newMarkEnd = CursorPos;
602 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
604 s32 lineNo = getLineFromPos(CursorPos);
605 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
606 if (lineNo < (s32)BrokenText.size()-1)
608 s32 cp = CursorPos - BrokenTextPositions[lineNo];
609 if ((s32)BrokenText[lineNo+1].size() < cp)
610 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
612 CursorPos = BrokenTextPositions[lineNo+1] + cp;
615 if (event.KeyInput.Shift)
618 newMarkEnd = CursorPos;
634 if ( !this->IsEnabled )
641 if (MarkBegin != MarkEnd)
643 // delete marked text
644 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
645 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
647 s = Text.subString(0, realmbgn);
648 s.append( Text.subString(realmend, Text.size()-realmend) );
651 CursorPos = realmbgn;
655 // delete text behind cursor
657 s = Text.subString(0, CursorPos-1);
660 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
667 BlinkStartTime = porting::getTimeMs();
674 if ( !this->IsEnabled )
677 if (Text.size() != 0)
681 if (MarkBegin != MarkEnd)
683 // delete marked text
684 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
685 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
687 s = Text.subString(0, realmbgn);
688 s.append( Text.subString(realmend, Text.size()-realmend) );
691 CursorPos = realmbgn;
695 // delete text before cursor
696 s = Text.subString(0, CursorPos);
697 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
701 if (CursorPos > (s32)Text.size())
702 CursorPos = (s32)Text.size();
704 BlinkStartTime = porting::getTimeMs();
742 inputChar(event.KeyInput.Char);
746 // Set new text markers
747 setTextMarkers( newMarkBegin, newMarkEnd );
749 // break the text if it has changed
753 sendGuiEvent(EGET_EDITBOX_CHANGED);
756 calculateScrollPos();
762 //! draws the element and its children
763 void intlGUIEditBox::draw()
768 const bool focus = Environment->hasFocus(this);
770 IGUISkin* skin = Environment->getSkin();
774 FrameRect = AbsoluteRect;
780 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
781 false, true, FrameRect, &AbsoluteClippingRect);
783 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
784 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
785 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
786 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
788 core::rect<s32> localClipRect = FrameRect;
789 localClipRect.clipAgainst(AbsoluteClippingRect);
793 IGUIFont* font = OverrideFont;
795 font = skin->getFont();
798 s32 charcursorpos = 0;
802 if (LastBreakFont != font)
807 // calculate cursor pos
809 core::stringw *txtLine = &Text;
815 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
816 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
817 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
818 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
819 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
820 const s32 lineCount = ml ? BrokenText.size() : 1;
822 // Save the override color information.
823 // Then, alter it if the edit box is disabled.
824 const bool prevOver = OverrideColorEnabled;
825 const video::SColor prevColor = OverrideColor;
829 if (!IsEnabled && !OverrideColorEnabled)
831 OverrideColorEnabled = true;
832 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
835 for (s32 i=0; i < lineCount; ++i)
839 // clipping test - don't draw anything outside the visible area
840 core::rect<s32> c = localClipRect;
841 c.clipAgainst(CurrentTextRect);
848 if (BrokenText.size() != 1)
851 BrokenText.push_back(core::stringw());
853 if (BrokenText[0].size() != Text.size())
855 BrokenText[0] = Text;
856 for (u32 q = 0; q < Text.size(); ++q)
858 BrokenText[0] [q] = PasswordChar;
861 txtLine = &BrokenText[0];
866 txtLine = ml ? &BrokenText[i] : &Text;
867 startPos = ml ? BrokenTextPositions[i] : 0;
872 font->draw(txtLine->c_str(), CurrentTextRect,
873 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
874 false, true, &localClipRect);
876 // draw mark and marked text
877 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
880 s32 mbegin = 0, mend = 0;
881 s32 lineStartPos = 0, lineEndPos = txtLine->size();
885 // highlight start is on this line
886 s = txtLine->subString(0, realmbgn - startPos);
887 mbegin = font->getDimension(s.c_str()).Width;
890 mbegin += font->getKerningWidth(
891 &((*txtLine)[realmbgn - startPos]),
892 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
894 lineStartPos = realmbgn - startPos;
896 if (i == hlineStart + hlineCount - 1)
898 // highlight end is on this line
899 s2 = txtLine->subString(0, realmend - startPos);
900 mend = font->getDimension(s2.c_str()).Width;
901 lineEndPos = (s32)s2.size();
904 mend = font->getDimension(txtLine->c_str()).Width;
906 CurrentTextRect.UpperLeftCorner.X += mbegin;
907 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
910 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
913 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
916 font->draw(s.c_str(), CurrentTextRect,
917 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
918 false, true, &localClipRect);
923 // Return the override color information to its previous settings.
924 OverrideColorEnabled = prevOver;
925 OverrideColor = prevColor;
930 if (WordWrap || MultiLine)
932 cursorLine = getLineFromPos(CursorPos);
933 txtLine = &BrokenText[cursorLine];
934 startPos = BrokenTextPositions[cursorLine];
936 s = txtLine->subString(0,CursorPos-startPos);
937 charcursorpos = font->getDimension(s.c_str()).Width +
938 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
940 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350)
942 setTextRect(cursorLine);
943 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
945 font->draw(L"_", CurrentTextRect,
946 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
947 false, true, &localClipRect);
956 //! Sets the new caption of this element.
957 void intlGUIEditBox::setText(const wchar_t* text)
960 if (u32(CursorPos) > Text.size())
961 CursorPos = Text.size();
967 //! Enables or disables automatic scrolling with cursor position
968 //! \param enable: If set to true, the text will move around with the cursor position
969 void intlGUIEditBox::setAutoScroll(bool enable)
975 //! Checks to see if automatic scrolling is enabled
976 //! \return true if automatic scrolling is enabled, false if not
977 bool intlGUIEditBox::isAutoScrollEnabled() const
979 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
984 //! Gets the area of the text in the edit box
985 //! \return Returns the size in pixels of the text
986 core::dimension2du intlGUIEditBox::getTextDimension()
991 ret = CurrentTextRect;
993 for (u32 i=1; i < BrokenText.size(); ++i)
996 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
997 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
1000 return core::dimension2du(ret.getSize());
1004 //! Sets the maximum amount of characters which may be entered in the box.
1005 //! \param max: Maximum amount of characters. If 0, the character amount is
1007 void intlGUIEditBox::setMax(u32 max)
1011 if (Text.size() > Max && Max != 0)
1012 Text = Text.subString(0, Max);
1016 //! Returns maximum amount of characters, previously set by setMax();
1017 u32 intlGUIEditBox::getMax() const
1023 bool intlGUIEditBox::processMouse(const SEvent& event)
1025 switch(event.MouseInput.Event)
1027 case irr::EMIE_LMOUSE_LEFT_UP:
1028 if (Environment->hasFocus(this))
1030 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1033 setTextMarkers( MarkBegin, CursorPos );
1035 MouseMarking = false;
1036 calculateScrollPos();
1040 case irr::EMIE_MOUSE_MOVED:
1044 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1045 setTextMarkers( MarkBegin, CursorPos );
1046 calculateScrollPos();
1051 case EMIE_LMOUSE_PRESSED_DOWN:
1052 if (!Environment->hasFocus(this))
1054 BlinkStartTime = porting::getTimeMs();
1055 MouseMarking = true;
1056 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1057 setTextMarkers(CursorPos, CursorPos );
1058 calculateScrollPos();
1063 if (!AbsoluteClippingRect.isPointInside(
1064 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
1071 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1073 s32 newMarkBegin = MarkBegin;
1075 newMarkBegin = CursorPos;
1077 MouseMarking = true;
1078 setTextMarkers( newMarkBegin, CursorPos);
1079 calculateScrollPos();
1091 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1093 IGUIFont* font = OverrideFont;
1094 IGUISkin* skin = Environment->getSkin();
1096 font = skin->getFont();
1098 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1100 core::stringw *txtLine = NULL;
1102 u32 curr_line_idx = 0;
1105 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1106 setTextRect(curr_line_idx);
1107 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1108 y = CurrentTextRect.UpperLeftCorner.Y;
1109 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1110 y = CurrentTextRect.LowerRightCorner.Y;
1112 // is it inside this region?
1113 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1114 // we've found the clicked line
1115 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1116 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1121 if (x < CurrentTextRect.UpperLeftCorner.X)
1122 x = CurrentTextRect.UpperLeftCorner.X;
1123 else if (x > CurrentTextRect.LowerRightCorner.X)
1124 x = CurrentTextRect.LowerRightCorner.X;
1126 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1127 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1128 // will be the last char, not null char of the wstring
1129 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1132 return rangelim(idx + startPos, 0, S32_MAX);
1136 //! Breaks the single text line.
1137 void intlGUIEditBox::breakText()
1139 IGUISkin* skin = Environment->getSkin();
1141 if ((!WordWrap && !MultiLine) || !skin)
1144 BrokenText.clear(); // need to reallocate :/
1145 BrokenTextPositions.set_used(0);
1147 IGUIFont* font = OverrideFont;
1149 font = skin->getFont();
1154 LastBreakFont = font;
1158 core::stringw whitespace;
1159 s32 lastLineStart = 0;
1160 s32 size = Text.size();
1162 s32 elWidth = RelativeRect.getWidth() - 6;
1165 for (s32 i=0; i<size; ++i)
1168 bool lineBreak = false;
1170 if (c == L'\r') // Mac or Windows breaks
1174 if (Text[i+1] == L'\n') // Windows breaks
1180 else if (c == L'\n') // Unix breaks
1186 // don't break if we're not a multi-line edit box
1190 if (c == L' ' || c == 0 || i == (size-1))
1194 // here comes the next whitespace, look if
1195 // we can break the last word to the next line.
1196 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1197 s32 worldlgth = font->getDimension(word.c_str()).Width;
1199 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1201 // break to next line
1203 BrokenText.push_back(line);
1204 BrokenTextPositions.push_back(lastLineStart);
1205 lastLineStart = i - (s32)word.size();
1213 length += whitelgth + worldlgth;
1222 // compute line break
1227 BrokenText.push_back(line);
1228 BrokenTextPositions.push_back(lastLineStart);
1229 lastLineStart = i+1;
1238 // yippee this is a word..
1245 BrokenText.push_back(line);
1246 BrokenTextPositions.push_back(lastLineStart);
1250 void intlGUIEditBox::setTextRect(s32 line)
1252 core::dimension2du d;
1254 IGUISkin* skin = Environment->getSkin();
1258 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1263 // get text dimension
1264 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1265 if (WordWrap || MultiLine)
1267 d = font->getDimension(BrokenText[line].c_str());
1271 d = font->getDimension(Text.c_str());
1272 d.Height = AbsoluteRect.getHeight();
1274 d.Height += font->getKerningHeight();
1280 // align to h centre
1281 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1282 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1284 case EGUIA_LOWERRIGHT:
1285 // align to right edge
1286 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1287 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1290 // align to left edge
1291 CurrentTextRect.UpperLeftCorner.X = 0;
1292 CurrentTextRect.LowerRightCorner.X = d.Width;
1299 // align to v centre
1300 CurrentTextRect.UpperLeftCorner.Y =
1301 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1303 case EGUIA_LOWERRIGHT:
1304 // align to bottom edge
1305 CurrentTextRect.UpperLeftCorner.Y =
1306 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1309 // align to top edge
1310 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1314 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1315 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1316 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1317 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1319 CurrentTextRect += FrameRect.UpperLeftCorner;
1324 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1326 if (!WordWrap && !MultiLine)
1330 while (i < (s32)BrokenTextPositions.size())
1332 if (BrokenTextPositions[i] > pos)
1336 return (s32)BrokenTextPositions.size() - 1;
1340 void intlGUIEditBox::inputChar(wchar_t c)
1347 if (Text.size() < Max || Max == 0)
1351 if (MarkBegin != MarkEnd)
1353 // replace marked text
1354 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1355 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1357 s = Text.subString(0, realmbgn);
1359 s.append( Text.subString(realmend, Text.size()-realmend) );
1361 CursorPos = realmbgn+1;
1365 // add new character
1366 s = Text.subString(0, CursorPos);
1368 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1373 BlinkStartTime = porting::getTimeMs();
1374 setTextMarkers(0, 0);
1378 sendGuiEvent(EGET_EDITBOX_CHANGED);
1379 calculateScrollPos();
1383 void intlGUIEditBox::calculateScrollPos()
1388 // calculate horizontal scroll position
1389 s32 cursLine = getLineFromPos(CursorPos);
1390 setTextRect(cursLine);
1392 // don't do horizontal scrolling when wordwrap is enabled.
1395 // get cursor position
1396 IGUISkin* skin = Environment->getSkin();
1399 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1403 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1404 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1406 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1407 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1409 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1411 if (FrameRect.LowerRightCorner.X < cEnd)
1412 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1413 else if (FrameRect.UpperLeftCorner.X > cStart)
1414 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1418 // todo: adjust scrollbar
1421 // vertical scroll position
1422 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1423 VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1425 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1426 VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1430 // todo: adjust scrollbar
1433 //! set text markers
1434 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1436 if ( begin != MarkBegin || end != MarkEnd )
1440 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1444 //! send some gui event to parent
1445 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1450 e.EventType = EET_GUI_EVENT;
1451 e.GUIEvent.Caller = this;
1452 e.GUIEvent.Element = 0;
1453 e.GUIEvent.EventType = type;
1459 //! Writes attributes of the element.
1460 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1462 // IGUIEditBox::serializeAttributes(out,options);
1464 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1465 out->addColor ("OverrideColor", OverrideColor);
1466 // out->addFont("OverrideFont",OverrideFont);
1467 out->addInt ("MaxChars", Max);
1468 out->addBool ("WordWrap", WordWrap);
1469 out->addBool ("MultiLine", MultiLine);
1470 out->addBool ("AutoScroll", AutoScroll);
1471 out->addBool ("PasswordBox", PasswordBox);
1472 core::stringw ch = L" ";
1473 ch[0] = PasswordChar;
1474 out->addString("PasswordChar", ch.c_str());
1475 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1476 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1478 IGUIEditBox::serializeAttributes(out,options);
1482 //! Reads attributes of the element
1483 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1485 IGUIEditBox::deserializeAttributes(in,options);
1487 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1488 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1489 setMax(in->getAttributeAsInt("MaxChars"));
1490 setWordWrap(in->getAttributeAsBool("WordWrap"));
1491 setMultiLine(in->getAttributeAsBool("MultiLine"));
1492 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1493 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1496 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1498 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1500 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1501 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1503 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1507 } // end namespace gui
1508 } // end namespace irr
1510 #endif // _IRR_COMPILE_WITH_GUI_