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"
36 #include "IGUIEnvironment.h"
38 #include "IVideoDriver.h"
39 //#include "irrlicht/os.cpp"
41 //#include "Keycodes.h"
47 ctrl+left/right to select word
48 double click/ctrl click: word select + drag to select whole words, triple click to select line
49 optional? dragging selected text
59 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
60 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
61 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
62 : IGUIEditBox(environment, parent, id, rectangle),
63 Border(border), FrameRect(rectangle),
64 m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable)
67 setDebugName("intlintlGUIEditBox");
73 Operator = Environment->getOSOperator();
78 // this element can be tabbed to
84 skin = Environment->getSkin();
87 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
88 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
89 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
90 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
93 if (skin && has_vscrollbar) {
94 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
96 if (m_scrollbar_width > 0) {
103 calculateScrollPos();
104 setWritable(writable);
109 intlGUIEditBox::~intlGUIEditBox()
112 OverrideFont->drop();
118 m_vscrollbar->drop();
122 //! Sets another skin independent font.
123 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
125 if (OverrideFont == font)
129 OverrideFont->drop();
134 OverrideFont->grab();
139 IGUIFont * intlGUIEditBox::getOverrideFont() const
144 //! Get the font which is used right now for drawing
145 IGUIFont* intlGUIEditBox::getActiveFont() const
149 IGUISkin* skin = Environment->getSkin();
151 return skin->getFont();
155 //! Sets another color for the text.
156 void intlGUIEditBox::setOverrideColor(video::SColor color)
158 OverrideColor = color;
159 OverrideColorEnabled = true;
162 video::SColor intlGUIEditBox::getOverrideColor() const
164 return OverrideColor;
167 //! Turns the border on or off
168 void intlGUIEditBox::setDrawBorder(bool border)
173 //! Sets whether to draw the background
174 void intlGUIEditBox::setDrawBackground(bool draw)
178 //! Sets if the text should use the overide color or the color in the gui skin.
179 void intlGUIEditBox::enableOverrideColor(bool enable)
181 OverrideColorEnabled = enable;
184 bool intlGUIEditBox::isOverrideColorEnabled() const
186 return OverrideColorEnabled;
189 //! Enables or disables word wrap
190 void intlGUIEditBox::setWordWrap(bool enable)
197 void intlGUIEditBox::updateAbsolutePosition()
199 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
200 IGUIElement::updateAbsolutePosition();
201 if ( oldAbsoluteRect != AbsoluteRect )
208 //! Checks if word wrap is enabled
209 bool intlGUIEditBox::isWordWrapEnabled() const
215 //! Enables or disables newlines.
216 void intlGUIEditBox::setMultiLine(bool enable)
222 //! Checks if multi line editing is enabled
223 bool intlGUIEditBox::isMultiLineEnabled() const
229 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
231 PasswordBox = passwordBox;
234 PasswordChar = passwordChar;
242 bool intlGUIEditBox::isPasswordBox() const
248 //! Sets text justification
249 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
256 //! called if an event happened.
257 bool intlGUIEditBox::OnEvent(const SEvent& event)
262 switch(event.EventType)
265 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
267 if (event.GUIEvent.Caller == this)
269 MouseMarking = false;
274 case EET_KEY_INPUT_EVENT:
276 #if (defined(__linux__) || defined(__FreeBSD__)) || defined(__DragonFly__)
277 // ################################################################
279 // This part is the difference from the original intlGUIEditBox
280 // It converts UTF-8 character into a UCS-2 (wchar_t)
282 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
284 //printf( "char: %lc (%u) \r\n", wc, wc );
286 SEvent irrevent(event);
287 irrevent.KeyInput.Char = wc;
288 // ################################################################
290 if (processKey(irrevent))
293 if (processKey(event))
295 #endif // defined(linux)
299 case EET_MOUSE_INPUT_EVENT:
300 if (processMouse(event))
308 return IGUIElement::OnEvent(event);
312 bool intlGUIEditBox::processKey(const SEvent& event)
314 if (!event.KeyInput.PressedDown)
317 bool textChanged = false;
318 s32 newMarkBegin = MarkBegin;
319 s32 newMarkEnd = MarkEnd;
321 // control shortcut handling
323 if (event.KeyInput.Control)
325 // german backlash '\' entered with control + '?'
326 if ( event.KeyInput.Char == '\\' )
328 inputChar(event.KeyInput.Char);
332 switch(event.KeyInput.Key)
337 newMarkEnd = Text.size();
341 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
343 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
344 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
347 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
348 Operator->copyToClipboard(s.c_str());
352 // cut to the clipboard
353 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());
362 if (IsEnabled && m_writable) {
365 s = Text.subString(0, realmbgn);
366 s.append( Text.subString(realmend, Text.size()-realmend) );
369 CursorPos = realmbgn;
377 if (!IsEnabled || !m_writable)
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 || !m_writable)
639 if (MarkBegin != MarkEnd)
641 // delete marked text
642 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
643 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
645 s = Text.subString(0, realmbgn);
646 s.append( Text.subString(realmend, Text.size()-realmend) );
649 CursorPos = realmbgn;
653 // delete text behind cursor
655 s = Text.subString(0, CursorPos-1);
658 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
665 BlinkStartTime = porting::getTimeMs();
672 if (!this->IsEnabled || !m_writable)
678 if (MarkBegin != MarkEnd)
680 // delete marked text
681 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
682 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
684 s = Text.subString(0, realmbgn);
685 s.append( Text.subString(realmend, Text.size()-realmend) );
688 CursorPos = realmbgn;
692 // delete text before cursor
693 s = Text.subString(0, CursorPos);
694 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
698 if (CursorPos > (s32)Text.size())
699 CursorPos = (s32)Text.size();
701 BlinkStartTime = porting::getTimeMs();
739 inputChar(event.KeyInput.Char);
743 // Set new text markers
744 setTextMarkers( newMarkBegin, newMarkEnd );
746 // break the text if it has changed
750 sendGuiEvent(EGET_EDITBOX_CHANGED);
753 calculateScrollPos();
759 //! draws the element and its children
760 void intlGUIEditBox::draw()
765 const bool focus = Environment->hasFocus(this);
767 IGUISkin* skin = Environment->getSkin();
771 FrameRect = AbsoluteRect;
778 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
779 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;
789 core::rect<s32> localClipRect = FrameRect;
790 localClipRect.clipAgainst(AbsoluteClippingRect);
794 IGUIFont* font = OverrideFont;
796 font = skin->getFont();
799 s32 charcursorpos = 0;
803 if (LastBreakFont != font)
808 // calculate cursor pos
810 core::stringw *txtLine = &Text;
816 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
817 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
818 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
819 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
820 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
821 const s32 lineCount = ml ? BrokenText.size() : 1;
823 // Save the override color information.
824 // Then, alter it if the edit box is disabled.
825 const bool prevOver = OverrideColorEnabled;
826 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);
941 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);
957 //! Sets the new caption of this element.
958 void intlGUIEditBox::setText(const wchar_t* text)
961 if (u32(CursorPos) > Text.size())
962 CursorPos = Text.size();
968 //! Enables or disables automatic scrolling with cursor position
969 //! \param enable: If set to true, the text will move around with the cursor position
970 void intlGUIEditBox::setAutoScroll(bool enable)
976 //! Checks to see if automatic scrolling is enabled
977 //! \return true if automatic scrolling is enabled, false if not
978 bool intlGUIEditBox::isAutoScrollEnabled() const
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))) {
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();
1082 case EMIE_MOUSE_WHEEL:
1083 if (m_vscrollbar && m_vscrollbar->isVisible()) {
1084 s32 pos = m_vscrollbar->getPos();
1085 s32 step = m_vscrollbar->getSmallStep();
1086 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
1097 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1099 IGUIFont* font = OverrideFont;
1100 IGUISkin* skin = Environment->getSkin();
1102 font = skin->getFont();
1104 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1106 core::stringw *txtLine = NULL;
1108 u32 curr_line_idx = 0;
1111 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1112 setTextRect(curr_line_idx);
1113 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1114 y = CurrentTextRect.UpperLeftCorner.Y;
1115 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1116 y = CurrentTextRect.LowerRightCorner.Y;
1118 // is it inside this region?
1119 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1120 // we've found the clicked line
1121 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1122 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1127 if (x < CurrentTextRect.UpperLeftCorner.X)
1128 x = CurrentTextRect.UpperLeftCorner.X;
1129 else if (x > CurrentTextRect.LowerRightCorner.X)
1130 x = CurrentTextRect.LowerRightCorner.X;
1132 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1133 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1134 // will be the last char, not null char of the wstring
1135 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1138 return rangelim(idx + startPos, 0, S32_MAX);
1142 //! Breaks the single text line.
1143 void intlGUIEditBox::breakText()
1145 IGUISkin* skin = Environment->getSkin();
1147 if ((!WordWrap && !MultiLine) || !skin)
1150 BrokenText.clear(); // need to reallocate :/
1151 BrokenTextPositions.set_used(0);
1153 IGUIFont* font = OverrideFont;
1155 font = skin->getFont();
1160 LastBreakFont = font;
1164 core::stringw whitespace;
1165 s32 lastLineStart = 0;
1166 s32 size = Text.size();
1168 s32 elWidth = RelativeRect.getWidth() - 6;
1171 for (s32 i=0; i<size; ++i)
1174 bool lineBreak = false;
1176 if (c == L'\r') // Mac or Windows breaks
1180 if (Text[i+1] == L'\n') // Windows breaks
1186 else if (c == L'\n') // Unix breaks
1192 // don't break if we're not a multi-line edit box
1196 if (c == L' ' || c == 0 || i == (size-1))
1198 if (!word.empty()) {
1199 // here comes the next whitespace, look if
1200 // we can break the last word to the next line.
1201 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1202 s32 worldlgth = font->getDimension(word.c_str()).Width;
1204 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1206 // break to next line
1208 BrokenText.push_back(line);
1209 BrokenTextPositions.push_back(lastLineStart);
1210 lastLineStart = i - (s32)word.size();
1218 length += whitelgth + worldlgth;
1227 // compute line break
1232 BrokenText.push_back(line);
1233 BrokenTextPositions.push_back(lastLineStart);
1234 lastLineStart = i+1;
1243 // yippee this is a word..
1250 BrokenText.push_back(line);
1251 BrokenTextPositions.push_back(lastLineStart);
1255 void intlGUIEditBox::setTextRect(s32 line)
1257 core::dimension2du d;
1259 IGUISkin* skin = Environment->getSkin();
1263 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1268 // get text dimension
1269 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1270 if (WordWrap || MultiLine)
1272 d = font->getDimension(BrokenText[line].c_str());
1276 d = font->getDimension(Text.c_str());
1277 d.Height = AbsoluteRect.getHeight();
1279 d.Height += font->getKerningHeight();
1285 // align to h centre
1286 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1287 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1289 case EGUIA_LOWERRIGHT:
1290 // align to right edge
1291 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1292 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1295 // align to left edge
1296 CurrentTextRect.UpperLeftCorner.X = 0;
1297 CurrentTextRect.LowerRightCorner.X = d.Width;
1304 // align to v centre
1305 CurrentTextRect.UpperLeftCorner.Y =
1306 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1308 case EGUIA_LOWERRIGHT:
1309 // align to bottom edge
1310 CurrentTextRect.UpperLeftCorner.Y =
1311 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1314 // align to top edge
1315 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1319 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1320 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1321 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1322 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1324 CurrentTextRect += FrameRect.UpperLeftCorner;
1329 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1331 if (!WordWrap && !MultiLine)
1335 while (i < (s32)BrokenTextPositions.size())
1337 if (BrokenTextPositions[i] > pos)
1341 return (s32)BrokenTextPositions.size() - 1;
1345 void intlGUIEditBox::inputChar(wchar_t c)
1347 if (!IsEnabled || !m_writable)
1352 if (Text.size() < Max || Max == 0)
1356 if (MarkBegin != MarkEnd)
1358 // replace marked text
1359 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1360 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1362 s = Text.subString(0, realmbgn);
1364 s.append( Text.subString(realmend, Text.size()-realmend) );
1366 CursorPos = realmbgn+1;
1370 // add new character
1371 s = Text.subString(0, CursorPos);
1373 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1378 BlinkStartTime = porting::getTimeMs();
1379 setTextMarkers(0, 0);
1383 sendGuiEvent(EGET_EDITBOX_CHANGED);
1384 calculateScrollPos();
1388 void intlGUIEditBox::calculateScrollPos()
1393 // calculate horizontal scroll position
1394 s32 cursLine = getLineFromPos(CursorPos);
1395 setTextRect(cursLine);
1397 // don't do horizontal scrolling when wordwrap is enabled.
1400 // get cursor position
1401 IGUISkin* skin = Environment->getSkin();
1404 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1408 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1409 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1411 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1412 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1414 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1416 if (FrameRect.LowerRightCorner.X < cEnd)
1417 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1418 else if (FrameRect.UpperLeftCorner.X > cStart)
1419 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1423 // todo: adjust scrollbar
1426 if (!WordWrap && !MultiLine)
1429 // vertical scroll position
1430 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y)
1431 VScrollPos += CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y; // scrolling downwards
1432 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y)
1433 VScrollPos += CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y; // scrolling upwards
1435 // todo: adjust scrollbar
1437 m_vscrollbar->setPos(VScrollPos);
1440 //! set text markers
1441 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1443 if ( begin != MarkBegin || end != MarkEnd )
1447 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1451 //! send some gui event to parent
1452 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1457 e.EventType = EET_GUI_EVENT;
1458 e.GUIEvent.Caller = this;
1459 e.GUIEvent.Element = 0;
1460 e.GUIEvent.EventType = type;
1466 //! Create a vertical scrollbar
1467 void intlGUIEditBox::createVScrollBar()
1472 fontHeight = OverrideFont->getDimension(L"").Height;
1474 if (IGUISkin* skin = Environment->getSkin()) {
1475 if (IGUIFont* font = skin->getFont()) {
1476 fontHeight = font->getDimension(L"").Height;
1481 RelativeRect.LowerRightCorner.X -= m_scrollbar_width + 4;
1483 irr::core::rect<s32> scrollbarrect = FrameRect;
1484 scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
1485 m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
1486 scrollbarrect, false, true);
1488 m_vscrollbar->setVisible(false);
1489 m_vscrollbar->setSmallStep(3 * fontHeight);
1490 m_vscrollbar->setLargeStep(10 * fontHeight);
1493 //! Update the vertical scrollbar (visibilty & scroll position)
1494 void intlGUIEditBox::updateVScrollBar()
1499 // OnScrollBarChanged(...)
1500 if (m_vscrollbar->getPos() != VScrollPos) {
1501 s32 deltaScrollY = m_vscrollbar->getPos() - VScrollPos;
1502 CurrentTextRect.UpperLeftCorner.Y -= deltaScrollY;
1503 CurrentTextRect.LowerRightCorner.Y -= deltaScrollY;
1505 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1506 if (scrollymax != m_vscrollbar->getMax()) {
1507 // manage a newline or a deleted line
1508 m_vscrollbar->setMax(scrollymax);
1509 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
1510 calculateScrollPos();
1512 // manage a newline or a deleted line
1513 VScrollPos = m_vscrollbar->getPos();
1517 // check if a vertical scrollbar is needed ?
1518 if (getTextDimension().Height > (u32) FrameRect.getHeight()) {
1519 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1520 if (scrollymax != m_vscrollbar->getMax()) {
1521 m_vscrollbar->setMax(scrollymax);
1522 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
1525 if (!m_vscrollbar->isVisible() && MultiLine) {
1526 AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
1528 m_vscrollbar->setVisible(true);
1531 if (m_vscrollbar->isVisible()) {
1532 AbsoluteRect.LowerRightCorner.X += m_scrollbar_width;
1535 m_vscrollbar->setPos(0);
1536 m_vscrollbar->setMax(1);
1537 m_vscrollbar->setPageSize(s32(getTextDimension().Height));
1538 m_vscrollbar->setVisible(false);
1543 void intlGUIEditBox::setWritable(bool can_write_text)
1545 m_writable = can_write_text;
1548 //! Writes attributes of the element.
1549 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1551 // IGUIEditBox::serializeAttributes(out,options);
1553 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1554 out->addColor ("OverrideColor", OverrideColor);
1555 // out->addFont("OverrideFont",OverrideFont);
1556 out->addInt ("MaxChars", Max);
1557 out->addBool ("WordWrap", WordWrap);
1558 out->addBool ("MultiLine", MultiLine);
1559 out->addBool ("AutoScroll", AutoScroll);
1560 out->addBool ("PasswordBox", PasswordBox);
1561 core::stringw ch = L" ";
1562 ch[0] = PasswordChar;
1563 out->addString("PasswordChar", ch.c_str());
1564 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1565 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1566 out->addBool ("Writable", m_writable);
1568 IGUIEditBox::serializeAttributes(out,options);
1572 //! Reads attributes of the element
1573 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1575 IGUIEditBox::deserializeAttributes(in,options);
1577 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1578 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1579 setMax(in->getAttributeAsInt("MaxChars"));
1580 setWordWrap(in->getAttributeAsBool("WordWrap"));
1581 setMultiLine(in->getAttributeAsBool("MultiLine"));
1582 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1583 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1586 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1588 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1590 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1591 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1593 setWritable(in->getAttributeAsBool("Writable"));
1594 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1598 } // end namespace gui
1599 } // end namespace irr