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 || defined(__ANDROID__)
38 #include "IGUIEnvironment.h"
40 #include "IVideoDriver.h"
41 //#include "irrlicht/os.cpp"
43 //#include "Keycodes.h"
49 ctrl+left/right to select word
50 double click/ctrl click: word select + drag to select whole words, triple click to select line
51 optional? dragging selected text
61 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
62 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
63 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
64 : IGUIEditBox(environment, parent, id, rectangle),
65 Border(border), FrameRect(rectangle),
66 m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable)
69 setDebugName("intlintlGUIEditBox");
75 Operator = Environment->getOSOperator();
80 // this element can be tabbed to
86 skin = Environment->getSkin();
89 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
90 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
91 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
92 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
95 if (skin && has_vscrollbar) {
96 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
98 if (m_scrollbar_width > 0) {
105 calculateScrollPos();
106 setWritable(writable);
111 intlGUIEditBox::~intlGUIEditBox()
114 OverrideFont->drop();
121 //! Sets another skin independent font.
122 void intlGUIEditBox::setOverrideFont(IGUIFont* font)
124 if (OverrideFont == font)
128 OverrideFont->drop();
133 OverrideFont->grab();
138 IGUIFont * intlGUIEditBox::getOverrideFont() const
143 //! Get the font which is used right now for drawing
144 IGUIFont* intlGUIEditBox::getActiveFont() const
148 IGUISkin* skin = Environment->getSkin();
150 return skin->getFont();
154 //! Sets another color for the text.
155 void intlGUIEditBox::setOverrideColor(video::SColor color)
157 OverrideColor = color;
158 OverrideColorEnabled = true;
161 video::SColor intlGUIEditBox::getOverrideColor() const
163 return OverrideColor;
166 //! Turns the border on or off
167 void intlGUIEditBox::setDrawBorder(bool border)
172 //! Sets whether to draw the background
173 void intlGUIEditBox::setDrawBackground(bool draw)
177 //! Sets if the text should use the overide color or the color in the gui skin.
178 void intlGUIEditBox::enableOverrideColor(bool enable)
180 OverrideColorEnabled = enable;
183 bool intlGUIEditBox::isOverrideColorEnabled() const
185 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
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
211 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
216 //! Enables or disables newlines.
217 void intlGUIEditBox::setMultiLine(bool enable)
223 //! Checks if multi line editing is enabled
224 bool intlGUIEditBox::isMultiLineEnabled() const
226 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
231 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
233 PasswordBox = passwordBox;
236 PasswordChar = passwordChar;
244 bool intlGUIEditBox::isPasswordBox() const
246 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
251 //! Sets text justification
252 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
259 //! called if an event happened.
260 bool intlGUIEditBox::OnEvent(const SEvent& event)
265 switch(event.EventType)
268 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
270 if (event.GUIEvent.Caller == this)
272 MouseMarking = false;
277 case EET_KEY_INPUT_EVENT:
279 #if (defined(__linux__) || defined(__FreeBSD__))
280 // ################################################################
282 // This part is the difference from the original intlGUIEditBox
283 // It converts UTF-8 character into a UCS-2 (wchar_t)
285 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
287 //printf( "char: %lc (%u) \r\n", wc, wc );
289 SEvent irrevent(event);
290 irrevent.KeyInput.Char = wc;
291 // ################################################################
293 if (processKey(irrevent))
296 if (processKey(event))
298 #endif // defined(linux)
302 case EET_MOUSE_INPUT_EVENT:
303 if (processMouse(event))
311 return IGUIElement::OnEvent(event);
315 bool intlGUIEditBox::processKey(const SEvent& event)
317 if (!event.KeyInput.PressedDown)
320 bool textChanged = false;
321 s32 newMarkBegin = MarkBegin;
322 s32 newMarkEnd = MarkEnd;
324 // control shortcut handling
326 if (event.KeyInput.Control)
328 // german backlash '\' entered with control + '?'
329 if ( event.KeyInput.Char == '\\' )
331 inputChar(event.KeyInput.Char);
335 switch(event.KeyInput.Key)
340 newMarkEnd = Text.size();
344 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
346 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
347 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
350 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
351 Operator->copyToClipboard(s.c_str());
355 // cut to the clipboard
356 if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
357 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
358 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
362 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
363 Operator->copyToClipboard(sc.c_str());
365 if (IsEnabled && m_writable) {
368 s = Text.subString(0, realmbgn);
369 s.append( Text.subString(realmend, Text.size()-realmend) );
372 CursorPos = realmbgn;
380 if (!IsEnabled || !m_writable)
383 // paste from the clipboard
386 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
387 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
390 const c8* p = Operator->getTextFromClipboard();
393 if (MarkBegin == MarkEnd)
396 core::stringw s = Text.subString(0, CursorPos);
398 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
400 if (!Max || s.size()<=Max) // thx to Fish FH for fix
404 CursorPos += s.size();
411 core::stringw s = Text.subString(0, realmbgn);
413 s.append( Text.subString(realmend, Text.size()-realmend) );
415 if (!Max || s.size()<=Max) // thx to Fish FH for fix
419 CursorPos = realmbgn + s.size();
430 // move/highlight to start of text
431 if (event.KeyInput.Shift)
433 newMarkEnd = CursorPos;
445 // move/highlight to end of text
446 if (event.KeyInput.Shift)
448 newMarkBegin = CursorPos;
449 newMarkEnd = Text.size();
454 CursorPos = Text.size();
463 // default keyboard handling
465 switch(event.KeyInput.Key)
470 if (WordWrap || MultiLine)
472 p = getLineFromPos(CursorPos);
473 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
474 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
478 if (event.KeyInput.Shift)
480 if (MarkBegin == MarkEnd)
481 newMarkBegin = CursorPos;
491 BlinkStartTime = porting::getTimeMs();
498 if (WordWrap || MultiLine)
500 p = getLineFromPos(CursorPos);
501 p = BrokenTextPositions[p];
504 if (event.KeyInput.Shift)
506 if (MarkBegin == MarkEnd)
507 newMarkBegin = CursorPos;
516 BlinkStartTime = porting::getTimeMs();
527 sendGuiEvent( EGET_EDITBOX_ENTER );
532 if (event.KeyInput.Shift)
536 if (MarkBegin == MarkEnd)
537 newMarkBegin = CursorPos;
539 newMarkEnd = CursorPos-1;
548 if (CursorPos > 0) CursorPos--;
549 BlinkStartTime = porting::getTimeMs();
553 if (event.KeyInput.Shift)
555 if (Text.size() > (u32)CursorPos)
557 if (MarkBegin == MarkEnd)
558 newMarkBegin = CursorPos;
560 newMarkEnd = CursorPos+1;
569 if (Text.size() > (u32)CursorPos) CursorPos++;
570 BlinkStartTime = porting::getTimeMs();
573 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
575 s32 lineNo = getLineFromPos(CursorPos);
576 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
579 s32 cp = CursorPos - BrokenTextPositions[lineNo];
580 if ((s32)BrokenText[lineNo-1].size() < cp)
581 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
583 CursorPos = BrokenTextPositions[lineNo-1] + cp;
586 if (event.KeyInput.Shift)
589 newMarkEnd = CursorPos;
604 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
606 s32 lineNo = getLineFromPos(CursorPos);
607 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
608 if (lineNo < (s32)BrokenText.size()-1)
610 s32 cp = CursorPos - BrokenTextPositions[lineNo];
611 if ((s32)BrokenText[lineNo+1].size() < cp)
612 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
614 CursorPos = BrokenTextPositions[lineNo+1] + cp;
617 if (event.KeyInput.Shift)
620 newMarkEnd = CursorPos;
636 if (!this->IsEnabled || !m_writable)
642 if (MarkBegin != MarkEnd)
644 // delete marked text
645 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
646 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
648 s = Text.subString(0, realmbgn);
649 s.append( Text.subString(realmend, Text.size()-realmend) );
652 CursorPos = realmbgn;
656 // delete text behind cursor
658 s = Text.subString(0, CursorPos-1);
661 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
668 BlinkStartTime = porting::getTimeMs();
675 if (!this->IsEnabled || !m_writable)
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;
781 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
782 false, true, FrameRect, &AbsoluteClippingRect);
785 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
786 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
787 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
788 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
792 core::rect<s32> localClipRect = FrameRect;
793 localClipRect.clipAgainst(AbsoluteClippingRect);
797 IGUIFont* font = OverrideFont;
799 font = skin->getFont();
802 s32 charcursorpos = 0;
806 if (LastBreakFont != font)
811 // calculate cursor pos
813 core::stringw *txtLine = &Text;
819 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
820 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
821 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
822 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
823 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
824 const s32 lineCount = ml ? BrokenText.size() : 1;
826 // Save the override color information.
827 // Then, alter it if the edit box is disabled.
828 const bool prevOver = OverrideColorEnabled;
829 const video::SColor prevColor = OverrideColor;
832 if (!IsEnabled && !OverrideColorEnabled)
834 OverrideColorEnabled = true;
835 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
838 for (s32 i=0; i < lineCount; ++i)
842 // clipping test - don't draw anything outside the visible area
843 core::rect<s32> c = localClipRect;
844 c.clipAgainst(CurrentTextRect);
851 if (BrokenText.size() != 1)
854 BrokenText.push_back(core::stringw());
856 if (BrokenText[0].size() != Text.size())
858 BrokenText[0] = Text;
859 for (u32 q = 0; q < Text.size(); ++q)
861 BrokenText[0] [q] = PasswordChar;
864 txtLine = &BrokenText[0];
869 txtLine = ml ? &BrokenText[i] : &Text;
870 startPos = ml ? BrokenTextPositions[i] : 0;
875 font->draw(txtLine->c_str(), CurrentTextRect,
876 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
877 false, true, &localClipRect);
879 // draw mark and marked text
880 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
883 s32 mbegin = 0, mend = 0;
884 s32 lineStartPos = 0, lineEndPos = txtLine->size();
888 // highlight start is on this line
889 s = txtLine->subString(0, realmbgn - startPos);
890 mbegin = font->getDimension(s.c_str()).Width;
893 mbegin += font->getKerningWidth(
894 &((*txtLine)[realmbgn - startPos]),
895 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
897 lineStartPos = realmbgn - startPos;
899 if (i == hlineStart + hlineCount - 1)
901 // highlight end is on this line
902 s2 = txtLine->subString(0, realmend - startPos);
903 mend = font->getDimension(s2.c_str()).Width;
904 lineEndPos = (s32)s2.size();
907 mend = font->getDimension(txtLine->c_str()).Width;
909 CurrentTextRect.UpperLeftCorner.X += mbegin;
910 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
913 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
916 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
919 font->draw(s.c_str(), CurrentTextRect,
920 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
921 false, true, &localClipRect);
926 // Return the override color information to its previous settings.
927 OverrideColorEnabled = prevOver;
928 OverrideColor = prevColor;
933 if (WordWrap || MultiLine)
935 cursorLine = getLineFromPos(CursorPos);
936 txtLine = &BrokenText[cursorLine];
937 startPos = BrokenTextPositions[cursorLine];
939 s = txtLine->subString(0,CursorPos-startPos);
940 charcursorpos = font->getDimension(s.c_str()).Width +
941 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
944 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350) {
945 setTextRect(cursorLine);
946 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
948 font->draw(L"_", CurrentTextRect,
949 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
950 false, true, &localClipRect);
960 //! Sets the new caption of this element.
961 void intlGUIEditBox::setText(const wchar_t* text)
964 if (u32(CursorPos) > Text.size())
965 CursorPos = Text.size();
971 //! Enables or disables automatic scrolling with cursor position
972 //! \param enable: If set to true, the text will move around with the cursor position
973 void intlGUIEditBox::setAutoScroll(bool enable)
979 //! Checks to see if automatic scrolling is enabled
980 //! \return true if automatic scrolling is enabled, false if not
981 bool intlGUIEditBox::isAutoScrollEnabled() const
983 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
988 //! Gets the area of the text in the edit box
989 //! \return Returns the size in pixels of the text
990 core::dimension2du intlGUIEditBox::getTextDimension()
995 ret = CurrentTextRect;
997 for (u32 i=1; i < BrokenText.size(); ++i)
1000 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
1001 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
1004 return core::dimension2du(ret.getSize());
1008 //! Sets the maximum amount of characters which may be entered in the box.
1009 //! \param max: Maximum amount of characters. If 0, the character amount is
1011 void intlGUIEditBox::setMax(u32 max)
1015 if (Text.size() > Max && Max != 0)
1016 Text = Text.subString(0, Max);
1020 //! Returns maximum amount of characters, previously set by setMax();
1021 u32 intlGUIEditBox::getMax() const
1027 bool intlGUIEditBox::processMouse(const SEvent& event)
1029 switch(event.MouseInput.Event)
1031 case irr::EMIE_LMOUSE_LEFT_UP:
1032 if (Environment->hasFocus(this))
1034 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1037 setTextMarkers( MarkBegin, CursorPos );
1039 MouseMarking = false;
1040 calculateScrollPos();
1044 case irr::EMIE_MOUSE_MOVED:
1048 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1049 setTextMarkers( MarkBegin, CursorPos );
1050 calculateScrollPos();
1055 case EMIE_LMOUSE_PRESSED_DOWN:
1056 if (!Environment->hasFocus(this))
1058 BlinkStartTime = porting::getTimeMs();
1059 MouseMarking = true;
1060 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1061 setTextMarkers(CursorPos, CursorPos );
1062 calculateScrollPos();
1067 if (!AbsoluteClippingRect.isPointInside(
1068 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
1074 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1076 s32 newMarkBegin = MarkBegin;
1078 newMarkBegin = CursorPos;
1080 MouseMarking = true;
1081 setTextMarkers( newMarkBegin, CursorPos);
1082 calculateScrollPos();
1086 case EMIE_MOUSE_WHEEL:
1087 if (m_vscrollbar && m_vscrollbar->isVisible()) {
1088 s32 pos = m_vscrollbar->getPos();
1089 s32 step = m_vscrollbar->getSmallStep();
1090 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
1101 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1103 IGUIFont* font = OverrideFont;
1104 IGUISkin* skin = Environment->getSkin();
1106 font = skin->getFont();
1108 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1110 core::stringw *txtLine = NULL;
1112 u32 curr_line_idx = 0;
1115 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1116 setTextRect(curr_line_idx);
1117 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1118 y = CurrentTextRect.UpperLeftCorner.Y;
1119 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1120 y = CurrentTextRect.LowerRightCorner.Y;
1122 // is it inside this region?
1123 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1124 // we've found the clicked line
1125 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1126 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1131 if (x < CurrentTextRect.UpperLeftCorner.X)
1132 x = CurrentTextRect.UpperLeftCorner.X;
1133 else if (x > CurrentTextRect.LowerRightCorner.X)
1134 x = CurrentTextRect.LowerRightCorner.X;
1136 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1137 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1138 // will be the last char, not null char of the wstring
1139 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1142 return rangelim(idx + startPos, 0, S32_MAX);
1146 //! Breaks the single text line.
1147 void intlGUIEditBox::breakText()
1149 IGUISkin* skin = Environment->getSkin();
1151 if ((!WordWrap && !MultiLine) || !skin)
1154 BrokenText.clear(); // need to reallocate :/
1155 BrokenTextPositions.set_used(0);
1157 IGUIFont* font = OverrideFont;
1159 font = skin->getFont();
1164 LastBreakFont = font;
1168 core::stringw whitespace;
1169 s32 lastLineStart = 0;
1170 s32 size = Text.size();
1172 s32 elWidth = RelativeRect.getWidth() - 6;
1175 for (s32 i=0; i<size; ++i)
1178 bool lineBreak = false;
1180 if (c == L'\r') // Mac or Windows breaks
1184 if (Text[i+1] == L'\n') // Windows breaks
1190 else if (c == L'\n') // Unix breaks
1196 // don't break if we're not a multi-line edit box
1200 if (c == L' ' || c == 0 || i == (size-1))
1202 if (!word.empty()) {
1203 // here comes the next whitespace, look if
1204 // we can break the last word to the next line.
1205 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1206 s32 worldlgth = font->getDimension(word.c_str()).Width;
1208 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1210 // break to next line
1212 BrokenText.push_back(line);
1213 BrokenTextPositions.push_back(lastLineStart);
1214 lastLineStart = i - (s32)word.size();
1222 length += whitelgth + worldlgth;
1231 // compute line break
1236 BrokenText.push_back(line);
1237 BrokenTextPositions.push_back(lastLineStart);
1238 lastLineStart = i+1;
1247 // yippee this is a word..
1254 BrokenText.push_back(line);
1255 BrokenTextPositions.push_back(lastLineStart);
1259 void intlGUIEditBox::setTextRect(s32 line)
1261 core::dimension2du d;
1263 IGUISkin* skin = Environment->getSkin();
1267 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1272 // get text dimension
1273 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1274 if (WordWrap || MultiLine)
1276 d = font->getDimension(BrokenText[line].c_str());
1280 d = font->getDimension(Text.c_str());
1281 d.Height = AbsoluteRect.getHeight();
1283 d.Height += font->getKerningHeight();
1289 // align to h centre
1290 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1291 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1293 case EGUIA_LOWERRIGHT:
1294 // align to right edge
1295 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1296 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1299 // align to left edge
1300 CurrentTextRect.UpperLeftCorner.X = 0;
1301 CurrentTextRect.LowerRightCorner.X = d.Width;
1308 // align to v centre
1309 CurrentTextRect.UpperLeftCorner.Y =
1310 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1312 case EGUIA_LOWERRIGHT:
1313 // align to bottom edge
1314 CurrentTextRect.UpperLeftCorner.Y =
1315 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1318 // align to top edge
1319 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1323 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1324 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1325 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1326 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1328 CurrentTextRect += FrameRect.UpperLeftCorner;
1333 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1335 if (!WordWrap && !MultiLine)
1339 while (i < (s32)BrokenTextPositions.size())
1341 if (BrokenTextPositions[i] > pos)
1345 return (s32)BrokenTextPositions.size() - 1;
1349 void intlGUIEditBox::inputChar(wchar_t c)
1351 if (!IsEnabled || !m_writable)
1356 if (Text.size() < Max || Max == 0)
1360 if (MarkBegin != MarkEnd)
1362 // replace marked text
1363 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1364 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1366 s = Text.subString(0, realmbgn);
1368 s.append( Text.subString(realmend, Text.size()-realmend) );
1370 CursorPos = realmbgn+1;
1374 // add new character
1375 s = Text.subString(0, CursorPos);
1377 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1382 BlinkStartTime = porting::getTimeMs();
1383 setTextMarkers(0, 0);
1387 sendGuiEvent(EGET_EDITBOX_CHANGED);
1388 calculateScrollPos();
1392 void intlGUIEditBox::calculateScrollPos()
1397 // calculate horizontal scroll position
1398 s32 cursLine = getLineFromPos(CursorPos);
1399 setTextRect(cursLine);
1401 // don't do horizontal scrolling when wordwrap is enabled.
1404 // get cursor position
1405 IGUISkin* skin = Environment->getSkin();
1408 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1412 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1413 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1415 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1416 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1418 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1420 if (FrameRect.LowerRightCorner.X < cEnd)
1421 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1422 else if (FrameRect.UpperLeftCorner.X > cStart)
1423 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1427 // todo: adjust scrollbar
1430 if (!WordWrap && !MultiLine)
1433 // vertical scroll position
1434 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y)
1435 VScrollPos += CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y; // scrolling downwards
1436 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y)
1437 VScrollPos += CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y; // scrolling upwards
1439 // todo: adjust scrollbar
1441 m_vscrollbar->setPos(VScrollPos);
1444 //! set text markers
1445 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1447 if ( begin != MarkBegin || end != MarkEnd )
1451 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1455 //! send some gui event to parent
1456 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1461 e.EventType = EET_GUI_EVENT;
1462 e.GUIEvent.Caller = this;
1463 e.GUIEvent.Element = 0;
1464 e.GUIEvent.EventType = type;
1470 //! Create a vertical scrollbar
1471 void intlGUIEditBox::createVScrollBar()
1476 fontHeight = OverrideFont->getDimension(L"").Height;
1478 if (IGUISkin* skin = Environment->getSkin()) {
1479 if (IGUIFont* font = skin->getFont()) {
1480 fontHeight = font->getDimension(L"").Height;
1485 irr::core::rect<s32> scrollbarrect = FrameRect;
1486 scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
1487 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
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 calculateScrollPos();
1511 // manage a newline or a deleted line
1512 VScrollPos = m_vscrollbar->getPos();
1516 // check if a vertical scrollbar is needed ?
1517 if (getTextDimension().Height > (u32) FrameRect.getHeight()) {
1518 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1519 if (scrollymax != m_vscrollbar->getMax()) {
1520 m_vscrollbar->setMax(scrollymax);
1523 if (!m_vscrollbar->isVisible() && MultiLine) {
1524 AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
1526 m_vscrollbar->setVisible(true);
1529 if (m_vscrollbar->isVisible()) {
1530 AbsoluteRect.LowerRightCorner.X += m_scrollbar_width;
1533 m_vscrollbar->setPos(0);
1534 m_vscrollbar->setMax(1);
1535 m_vscrollbar->setVisible(false);
1540 void intlGUIEditBox::setWritable(bool can_write_text)
1542 m_writable = can_write_text;
1545 //! Writes attributes of the element.
1546 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1548 // IGUIEditBox::serializeAttributes(out,options);
1550 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1551 out->addColor ("OverrideColor", OverrideColor);
1552 // out->addFont("OverrideFont",OverrideFont);
1553 out->addInt ("MaxChars", Max);
1554 out->addBool ("WordWrap", WordWrap);
1555 out->addBool ("MultiLine", MultiLine);
1556 out->addBool ("AutoScroll", AutoScroll);
1557 out->addBool ("PasswordBox", PasswordBox);
1558 core::stringw ch = L" ";
1559 ch[0] = PasswordChar;
1560 out->addString("PasswordChar", ch.c_str());
1561 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1562 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1563 out->addBool ("Writable", m_writable);
1565 IGUIEditBox::serializeAttributes(out,options);
1569 //! Reads attributes of the element
1570 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1572 IGUIEditBox::deserializeAttributes(in,options);
1574 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1575 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1576 setMax(in->getAttributeAsInt("MaxChars"));
1577 setWordWrap(in->getAttributeAsBool("WordWrap"));
1578 setMultiLine(in->getAttributeAsBool("MultiLine"));
1579 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1580 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1583 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1585 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1587 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1588 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1590 setWritable(in->getAttributeAsBool("Writable"));
1591 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1595 } // end namespace gui
1596 } // end namespace irr
1598 #endif // _IRR_COMPILE_WITH_GUI_