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();
119 //! Sets another skin independent font.
120 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
122 if (OverrideFont == font)
126 OverrideFont->drop();
131 OverrideFont->grab();
136 IGUIFont * intlGUIEditBox::getOverrideFont() const
141 //! Get the font which is used right now for drawing
142 IGUIFont* intlGUIEditBox::getActiveFont() const
146 IGUISkin* skin = Environment->getSkin();
148 return skin->getFont();
152 //! Sets another color for the text.
153 void intlGUIEditBox::setOverrideColor(video::SColor color)
155 OverrideColor = color;
156 OverrideColorEnabled = true;
159 video::SColor intlGUIEditBox::getOverrideColor() const
161 return OverrideColor;
164 //! Turns the border on or off
165 void intlGUIEditBox::setDrawBorder(bool border)
170 //! Sets whether to draw the background
171 void intlGUIEditBox::setDrawBackground(bool draw)
175 //! Sets if the text should use the overide color or the color in the gui skin.
176 void intlGUIEditBox::enableOverrideColor(bool enable)
178 OverrideColorEnabled = enable;
181 bool intlGUIEditBox::isOverrideColorEnabled() const
183 return OverrideColorEnabled;
186 //! Enables or disables word wrap
187 void intlGUIEditBox::setWordWrap(bool enable)
194 void intlGUIEditBox::updateAbsolutePosition()
196 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
197 IGUIElement::updateAbsolutePosition();
198 if ( oldAbsoluteRect != AbsoluteRect )
205 //! Checks if word wrap is enabled
206 bool intlGUIEditBox::isWordWrapEnabled() const
212 //! Enables or disables newlines.
213 void intlGUIEditBox::setMultiLine(bool enable)
219 //! Checks if multi line editing is enabled
220 bool intlGUIEditBox::isMultiLineEnabled() const
226 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
228 PasswordBox = passwordBox;
231 PasswordChar = passwordChar;
239 bool intlGUIEditBox::isPasswordBox() const
245 //! Sets text justification
246 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
253 //! called if an event happened.
254 bool intlGUIEditBox::OnEvent(const SEvent& event)
259 switch(event.EventType)
262 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
264 if (event.GUIEvent.Caller == this)
266 MouseMarking = false;
271 case EET_KEY_INPUT_EVENT:
273 #if (defined(__linux__) || defined(__FreeBSD__))
274 // ################################################################
276 // This part is the difference from the original intlGUIEditBox
277 // It converts UTF-8 character into a UCS-2 (wchar_t)
279 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
281 //printf( "char: %lc (%u) \r\n", wc, wc );
283 SEvent irrevent(event);
284 irrevent.KeyInput.Char = wc;
285 // ################################################################
287 if (processKey(irrevent))
290 if (processKey(event))
292 #endif // defined(linux)
296 case EET_MOUSE_INPUT_EVENT:
297 if (processMouse(event))
305 return IGUIElement::OnEvent(event);
309 bool intlGUIEditBox::processKey(const SEvent& event)
311 if (!event.KeyInput.PressedDown)
314 bool textChanged = false;
315 s32 newMarkBegin = MarkBegin;
316 s32 newMarkEnd = MarkEnd;
318 // control shortcut handling
320 if (event.KeyInput.Control)
322 // german backlash '\' entered with control + '?'
323 if ( event.KeyInput.Char == '\\' )
325 inputChar(event.KeyInput.Char);
329 switch(event.KeyInput.Key)
334 newMarkEnd = Text.size();
338 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
340 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
341 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
344 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
345 Operator->copyToClipboard(s.c_str());
349 // cut to the clipboard
350 if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
351 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
352 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
356 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
357 Operator->copyToClipboard(sc.c_str());
359 if (IsEnabled && m_writable) {
362 s = Text.subString(0, realmbgn);
363 s.append( Text.subString(realmend, Text.size()-realmend) );
366 CursorPos = realmbgn;
374 if (!IsEnabled || !m_writable)
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 || !m_writable)
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 || !m_writable)
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;
775 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
776 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;
786 core::rect<s32> localClipRect = FrameRect;
787 localClipRect.clipAgainst(AbsoluteClippingRect);
791 IGUIFont* font = OverrideFont;
793 font = skin->getFont();
796 s32 charcursorpos = 0;
800 if (LastBreakFont != font)
805 // calculate cursor pos
807 core::stringw *txtLine = &Text;
813 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
814 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
815 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
816 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
817 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
818 const s32 lineCount = ml ? BrokenText.size() : 1;
820 // Save the override color information.
821 // Then, alter it if the edit box is disabled.
822 const bool prevOver = OverrideColorEnabled;
823 const video::SColor prevColor = OverrideColor;
826 if (!IsEnabled && !OverrideColorEnabled)
828 OverrideColorEnabled = true;
829 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
832 for (s32 i=0; i < lineCount; ++i)
836 // clipping test - don't draw anything outside the visible area
837 core::rect<s32> c = localClipRect;
838 c.clipAgainst(CurrentTextRect);
845 if (BrokenText.size() != 1)
848 BrokenText.push_back(core::stringw());
850 if (BrokenText[0].size() != Text.size())
852 BrokenText[0] = Text;
853 for (u32 q = 0; q < Text.size(); ++q)
855 BrokenText[0] [q] = PasswordChar;
858 txtLine = &BrokenText[0];
863 txtLine = ml ? &BrokenText[i] : &Text;
864 startPos = ml ? BrokenTextPositions[i] : 0;
869 font->draw(txtLine->c_str(), CurrentTextRect,
870 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
871 false, true, &localClipRect);
873 // draw mark and marked text
874 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
877 s32 mbegin = 0, mend = 0;
878 s32 lineStartPos = 0, lineEndPos = txtLine->size();
882 // highlight start is on this line
883 s = txtLine->subString(0, realmbgn - startPos);
884 mbegin = font->getDimension(s.c_str()).Width;
887 mbegin += font->getKerningWidth(
888 &((*txtLine)[realmbgn - startPos]),
889 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
891 lineStartPos = realmbgn - startPos;
893 if (i == hlineStart + hlineCount - 1)
895 // highlight end is on this line
896 s2 = txtLine->subString(0, realmend - startPos);
897 mend = font->getDimension(s2.c_str()).Width;
898 lineEndPos = (s32)s2.size();
901 mend = font->getDimension(txtLine->c_str()).Width;
903 CurrentTextRect.UpperLeftCorner.X += mbegin;
904 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
907 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
910 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
913 font->draw(s.c_str(), CurrentTextRect,
914 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
915 false, true, &localClipRect);
920 // Return the override color information to its previous settings.
921 OverrideColorEnabled = prevOver;
922 OverrideColor = prevColor;
927 if (WordWrap || MultiLine)
929 cursorLine = getLineFromPos(CursorPos);
930 txtLine = &BrokenText[cursorLine];
931 startPos = BrokenTextPositions[cursorLine];
933 s = txtLine->subString(0,CursorPos-startPos);
934 charcursorpos = font->getDimension(s.c_str()).Width +
935 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
938 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350) {
939 setTextRect(cursorLine);
940 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
942 font->draw(L"_", CurrentTextRect,
943 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
944 false, true, &localClipRect);
954 //! Sets the new caption of this element.
955 void intlGUIEditBox::setText(const wchar_t* text)
958 if (u32(CursorPos) > Text.size())
959 CursorPos = Text.size();
965 //! Enables or disables automatic scrolling with cursor position
966 //! \param enable: If set to true, the text will move around with the cursor position
967 void intlGUIEditBox::setAutoScroll(bool enable)
973 //! Checks to see if automatic scrolling is enabled
974 //! \return true if automatic scrolling is enabled, false if not
975 bool intlGUIEditBox::isAutoScrollEnabled() const
981 //! Gets the area of the text in the edit box
982 //! \return Returns the size in pixels of the text
983 core::dimension2du intlGUIEditBox::getTextDimension()
988 ret = CurrentTextRect;
990 for (u32 i=1; i < BrokenText.size(); ++i)
993 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
994 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
997 return core::dimension2du(ret.getSize());
1001 //! Sets the maximum amount of characters which may be entered in the box.
1002 //! \param max: Maximum amount of characters. If 0, the character amount is
1004 void intlGUIEditBox::setMax(u32 max)
1008 if (Text.size() > Max && Max != 0)
1009 Text = Text.subString(0, Max);
1013 //! Returns maximum amount of characters, previously set by setMax();
1014 u32 intlGUIEditBox::getMax() const
1020 bool intlGUIEditBox::processMouse(const SEvent& event)
1022 switch(event.MouseInput.Event)
1024 case irr::EMIE_LMOUSE_LEFT_UP:
1025 if (Environment->hasFocus(this))
1027 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1030 setTextMarkers( MarkBegin, CursorPos );
1032 MouseMarking = false;
1033 calculateScrollPos();
1037 case irr::EMIE_MOUSE_MOVED:
1041 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1042 setTextMarkers( MarkBegin, CursorPos );
1043 calculateScrollPos();
1048 case EMIE_LMOUSE_PRESSED_DOWN:
1049 if (!Environment->hasFocus(this))
1051 BlinkStartTime = porting::getTimeMs();
1052 MouseMarking = true;
1053 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1054 setTextMarkers(CursorPos, CursorPos );
1055 calculateScrollPos();
1060 if (!AbsoluteClippingRect.isPointInside(
1061 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();
1079 case EMIE_MOUSE_WHEEL:
1080 if (m_vscrollbar && m_vscrollbar->isVisible()) {
1081 s32 pos = m_vscrollbar->getPos();
1082 s32 step = m_vscrollbar->getSmallStep();
1083 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
1094 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1096 IGUIFont* font = OverrideFont;
1097 IGUISkin* skin = Environment->getSkin();
1099 font = skin->getFont();
1101 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1103 core::stringw *txtLine = NULL;
1105 u32 curr_line_idx = 0;
1108 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1109 setTextRect(curr_line_idx);
1110 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1111 y = CurrentTextRect.UpperLeftCorner.Y;
1112 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1113 y = CurrentTextRect.LowerRightCorner.Y;
1115 // is it inside this region?
1116 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1117 // we've found the clicked line
1118 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1119 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1124 if (x < CurrentTextRect.UpperLeftCorner.X)
1125 x = CurrentTextRect.UpperLeftCorner.X;
1126 else if (x > CurrentTextRect.LowerRightCorner.X)
1127 x = CurrentTextRect.LowerRightCorner.X;
1129 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1130 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1131 // will be the last char, not null char of the wstring
1132 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1135 return rangelim(idx + startPos, 0, S32_MAX);
1139 //! Breaks the single text line.
1140 void intlGUIEditBox::breakText()
1142 IGUISkin* skin = Environment->getSkin();
1144 if ((!WordWrap && !MultiLine) || !skin)
1147 BrokenText.clear(); // need to reallocate :/
1148 BrokenTextPositions.set_used(0);
1150 IGUIFont* font = OverrideFont;
1152 font = skin->getFont();
1157 LastBreakFont = font;
1161 core::stringw whitespace;
1162 s32 lastLineStart = 0;
1163 s32 size = Text.size();
1165 s32 elWidth = RelativeRect.getWidth() - 6;
1168 for (s32 i=0; i<size; ++i)
1171 bool lineBreak = false;
1173 if (c == L'\r') // Mac or Windows breaks
1177 if (Text[i+1] == L'\n') // Windows breaks
1183 else if (c == L'\n') // Unix breaks
1189 // don't break if we're not a multi-line edit box
1193 if (c == L' ' || c == 0 || i == (size-1))
1195 if (!word.empty()) {
1196 // here comes the next whitespace, look if
1197 // we can break the last word to the next line.
1198 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1199 s32 worldlgth = font->getDimension(word.c_str()).Width;
1201 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1203 // break to next line
1205 BrokenText.push_back(line);
1206 BrokenTextPositions.push_back(lastLineStart);
1207 lastLineStart = i - (s32)word.size();
1215 length += whitelgth + worldlgth;
1224 // compute line break
1229 BrokenText.push_back(line);
1230 BrokenTextPositions.push_back(lastLineStart);
1231 lastLineStart = i+1;
1240 // yippee this is a word..
1247 BrokenText.push_back(line);
1248 BrokenTextPositions.push_back(lastLineStart);
1252 void intlGUIEditBox::setTextRect(s32 line)
1254 core::dimension2du d;
1256 IGUISkin* skin = Environment->getSkin();
1260 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1265 // get text dimension
1266 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1267 if (WordWrap || MultiLine)
1269 d = font->getDimension(BrokenText[line].c_str());
1273 d = font->getDimension(Text.c_str());
1274 d.Height = AbsoluteRect.getHeight();
1276 d.Height += font->getKerningHeight();
1282 // align to h centre
1283 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1284 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1286 case EGUIA_LOWERRIGHT:
1287 // align to right edge
1288 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1289 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1292 // align to left edge
1293 CurrentTextRect.UpperLeftCorner.X = 0;
1294 CurrentTextRect.LowerRightCorner.X = d.Width;
1301 // align to v centre
1302 CurrentTextRect.UpperLeftCorner.Y =
1303 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1305 case EGUIA_LOWERRIGHT:
1306 // align to bottom edge
1307 CurrentTextRect.UpperLeftCorner.Y =
1308 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1311 // align to top edge
1312 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1316 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1317 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1318 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1319 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1321 CurrentTextRect += FrameRect.UpperLeftCorner;
1326 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1328 if (!WordWrap && !MultiLine)
1332 while (i < (s32)BrokenTextPositions.size())
1334 if (BrokenTextPositions[i] > pos)
1338 return (s32)BrokenTextPositions.size() - 1;
1342 void intlGUIEditBox::inputChar(wchar_t c)
1344 if (!IsEnabled || !m_writable)
1349 if (Text.size() < Max || Max == 0)
1353 if (MarkBegin != MarkEnd)
1355 // replace marked text
1356 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1357 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1359 s = Text.subString(0, realmbgn);
1361 s.append( Text.subString(realmend, Text.size()-realmend) );
1363 CursorPos = realmbgn+1;
1367 // add new character
1368 s = Text.subString(0, CursorPos);
1370 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1375 BlinkStartTime = porting::getTimeMs();
1376 setTextMarkers(0, 0);
1380 sendGuiEvent(EGET_EDITBOX_CHANGED);
1381 calculateScrollPos();
1385 void intlGUIEditBox::calculateScrollPos()
1390 // calculate horizontal scroll position
1391 s32 cursLine = getLineFromPos(CursorPos);
1392 setTextRect(cursLine);
1394 // don't do horizontal scrolling when wordwrap is enabled.
1397 // get cursor position
1398 IGUISkin* skin = Environment->getSkin();
1401 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1405 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1406 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1408 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1409 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1411 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1413 if (FrameRect.LowerRightCorner.X < cEnd)
1414 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1415 else if (FrameRect.UpperLeftCorner.X > cStart)
1416 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1420 // todo: adjust scrollbar
1423 if (!WordWrap && !MultiLine)
1426 // vertical scroll position
1427 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y)
1428 VScrollPos += CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y; // scrolling downwards
1429 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y)
1430 VScrollPos += CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y; // scrolling upwards
1432 // todo: adjust scrollbar
1434 m_vscrollbar->setPos(VScrollPos);
1437 //! set text markers
1438 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1440 if ( begin != MarkBegin || end != MarkEnd )
1444 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1448 //! send some gui event to parent
1449 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1454 e.EventType = EET_GUI_EVENT;
1455 e.GUIEvent.Caller = this;
1456 e.GUIEvent.Element = 0;
1457 e.GUIEvent.EventType = type;
1463 //! Create a vertical scrollbar
1464 void intlGUIEditBox::createVScrollBar()
1469 fontHeight = OverrideFont->getDimension(L"").Height;
1471 if (IGUISkin* skin = Environment->getSkin()) {
1472 if (IGUIFont* font = skin->getFont()) {
1473 fontHeight = font->getDimension(L"").Height;
1478 RelativeRect.LowerRightCorner.X -= m_scrollbar_width + 4;
1480 irr::core::rect<s32> scrollbarrect = FrameRect;
1481 scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
1482 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1483 m_vscrollbar->setVisible(false);
1484 m_vscrollbar->setSmallStep(3 * fontHeight);
1485 m_vscrollbar->setLargeStep(10 * fontHeight);
1488 //! Update the vertical scrollbar (visibilty & scroll position)
1489 void intlGUIEditBox::updateVScrollBar()
1494 // OnScrollBarChanged(...)
1495 if (m_vscrollbar->getPos() != VScrollPos) {
1496 s32 deltaScrollY = m_vscrollbar->getPos() - VScrollPos;
1497 CurrentTextRect.UpperLeftCorner.Y -= deltaScrollY;
1498 CurrentTextRect.LowerRightCorner.Y -= deltaScrollY;
1500 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1501 if (scrollymax != m_vscrollbar->getMax()) {
1502 // manage a newline or a deleted line
1503 m_vscrollbar->setMax(scrollymax);
1504 calculateScrollPos();
1506 // manage a newline or a deleted line
1507 VScrollPos = m_vscrollbar->getPos();
1511 // check if a vertical scrollbar is needed ?
1512 if (getTextDimension().Height > (u32) FrameRect.getHeight()) {
1513 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1514 if (scrollymax != m_vscrollbar->getMax()) {
1515 m_vscrollbar->setMax(scrollymax);
1518 if (!m_vscrollbar->isVisible() && MultiLine) {
1519 AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
1521 m_vscrollbar->setVisible(true);
1524 if (m_vscrollbar->isVisible()) {
1525 AbsoluteRect.LowerRightCorner.X += m_scrollbar_width;
1528 m_vscrollbar->setPos(0);
1529 m_vscrollbar->setMax(1);
1530 m_vscrollbar->setVisible(false);
1535 void intlGUIEditBox::setWritable(bool can_write_text)
1537 m_writable = can_write_text;
1540 //! Writes attributes of the element.
1541 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1543 // IGUIEditBox::serializeAttributes(out,options);
1545 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1546 out->addColor ("OverrideColor", OverrideColor);
1547 // out->addFont("OverrideFont",OverrideFont);
1548 out->addInt ("MaxChars", Max);
1549 out->addBool ("WordWrap", WordWrap);
1550 out->addBool ("MultiLine", MultiLine);
1551 out->addBool ("AutoScroll", AutoScroll);
1552 out->addBool ("PasswordBox", PasswordBox);
1553 core::stringw ch = L" ";
1554 ch[0] = PasswordChar;
1555 out->addString("PasswordChar", ch.c_str());
1556 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1557 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1558 out->addBool ("Writable", m_writable);
1560 IGUIEditBox::serializeAttributes(out,options);
1564 //! Reads attributes of the element
1565 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1567 IGUIEditBox::deserializeAttributes(in,options);
1569 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1570 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1571 setMax(in->getAttributeAsInt("MaxChars"));
1572 setWordWrap(in->getAttributeAsBool("WordWrap"));
1573 setMultiLine(in->getAttributeAsBool("MultiLine"));
1574 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1575 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1578 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1580 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1582 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1583 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1585 setWritable(in->getAttributeAsBool("Writable"));
1586 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1590 } // end namespace gui
1591 } // end namespace irr