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, bool writable, bool has_vscrollbar)
65 : IGUIEditBox(environment, parent, id, rectangle),
66 Border(border), FrameRect(rectangle),
67 m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable)
70 setDebugName("intlintlGUIEditBox");
76 Operator = Environment->getOSOperator();
81 // this element can be tabbed to
87 skin = Environment->getSkin();
90 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
91 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
92 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
93 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
96 if (skin && has_vscrollbar) {
97 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
99 if (m_scrollbar_width > 0) {
106 calculateScrollPos();
107 setWritable(writable);
112 intlGUIEditBox::~intlGUIEditBox()
115 OverrideFont->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 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
187 return OverrideColorEnabled;
190 //! Enables or disables word wrap
191 void intlGUIEditBox::setWordWrap(bool enable)
198 void intlGUIEditBox::updateAbsolutePosition()
200 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
201 IGUIElement::updateAbsolutePosition();
202 if ( oldAbsoluteRect != AbsoluteRect )
209 //! Checks if word wrap is enabled
210 bool intlGUIEditBox::isWordWrapEnabled() const
212 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
217 //! Enables or disables newlines.
218 void intlGUIEditBox::setMultiLine(bool enable)
224 //! Checks if multi line editing is enabled
225 bool intlGUIEditBox::isMultiLineEnabled() const
227 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
232 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
234 PasswordBox = passwordBox;
237 PasswordChar = passwordChar;
245 bool intlGUIEditBox::isPasswordBox() const
247 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
252 //! Sets text justification
253 void intlGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
260 //! called if an event happened.
261 bool intlGUIEditBox::OnEvent(const SEvent& event)
266 switch(event.EventType)
269 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
271 if (event.GUIEvent.Caller == this)
273 MouseMarking = false;
278 case EET_KEY_INPUT_EVENT:
280 #if (defined(__linux__) || defined(__FreeBSD__))
281 // ################################################################
283 // This part is the difference from the original intlGUIEditBox
284 // It converts UTF-8 character into a UCS-2 (wchar_t)
286 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
288 //printf( "char: %lc (%u) \r\n", wc, wc );
290 SEvent irrevent(event);
291 irrevent.KeyInput.Char = wc;
292 // ################################################################
294 if (processKey(irrevent))
297 if (processKey(event))
299 #endif // defined(linux)
303 case EET_MOUSE_INPUT_EVENT:
304 if (processMouse(event))
312 return IGUIElement::OnEvent(event);
316 bool intlGUIEditBox::processKey(const SEvent& event)
318 if (!event.KeyInput.PressedDown)
321 bool textChanged = false;
322 s32 newMarkBegin = MarkBegin;
323 s32 newMarkEnd = MarkEnd;
325 // control shortcut handling
327 if (event.KeyInput.Control)
329 // german backlash '\' entered with control + '?'
330 if ( event.KeyInput.Char == '\\' )
332 inputChar(event.KeyInput.Char);
336 switch(event.KeyInput.Key)
341 newMarkEnd = Text.size();
345 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
347 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
348 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
351 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
352 Operator->copyToClipboard(s.c_str());
356 // cut to the clipboard
357 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
359 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
360 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
364 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
365 Operator->copyToClipboard(sc.c_str());
371 s = Text.subString(0, realmbgn);
372 s.append( Text.subString(realmend, Text.size()-realmend) );
375 CursorPos = realmbgn;
386 // paste from the clipboard
389 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
390 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
393 const c8* p = Operator->getTextFromClipboard();
396 if (MarkBegin == MarkEnd)
399 core::stringw s = Text.subString(0, CursorPos);
401 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
403 if (!Max || s.size()<=Max) // thx to Fish FH for fix
407 CursorPos += s.size();
414 core::stringw s = Text.subString(0, realmbgn);
416 s.append( Text.subString(realmend, Text.size()-realmend) );
418 if (!Max || s.size()<=Max) // thx to Fish FH for fix
422 CursorPos = realmbgn + s.size();
433 // move/highlight to start of text
434 if (event.KeyInput.Shift)
436 newMarkEnd = CursorPos;
448 // move/highlight to end of text
449 if (event.KeyInput.Shift)
451 newMarkBegin = CursorPos;
452 newMarkEnd = Text.size();
457 CursorPos = Text.size();
466 // default keyboard handling
468 switch(event.KeyInput.Key)
473 if (WordWrap || MultiLine)
475 p = getLineFromPos(CursorPos);
476 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
477 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
481 if (event.KeyInput.Shift)
483 if (MarkBegin == MarkEnd)
484 newMarkBegin = CursorPos;
494 BlinkStartTime = porting::getTimeMs();
501 if (WordWrap || MultiLine)
503 p = getLineFromPos(CursorPos);
504 p = BrokenTextPositions[p];
507 if (event.KeyInput.Shift)
509 if (MarkBegin == MarkEnd)
510 newMarkBegin = CursorPos;
519 BlinkStartTime = porting::getTimeMs();
530 sendGuiEvent( EGET_EDITBOX_ENTER );
535 if (event.KeyInput.Shift)
539 if (MarkBegin == MarkEnd)
540 newMarkBegin = CursorPos;
542 newMarkEnd = CursorPos-1;
551 if (CursorPos > 0) CursorPos--;
552 BlinkStartTime = porting::getTimeMs();
556 if (event.KeyInput.Shift)
558 if (Text.size() > (u32)CursorPos)
560 if (MarkBegin == MarkEnd)
561 newMarkBegin = CursorPos;
563 newMarkEnd = CursorPos+1;
572 if (Text.size() > (u32)CursorPos) CursorPos++;
573 BlinkStartTime = porting::getTimeMs();
576 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
578 s32 lineNo = getLineFromPos(CursorPos);
579 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
582 s32 cp = CursorPos - BrokenTextPositions[lineNo];
583 if ((s32)BrokenText[lineNo-1].size() < cp)
584 CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
586 CursorPos = BrokenTextPositions[lineNo-1] + cp;
589 if (event.KeyInput.Shift)
592 newMarkEnd = CursorPos;
607 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
609 s32 lineNo = getLineFromPos(CursorPos);
610 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
611 if (lineNo < (s32)BrokenText.size()-1)
613 s32 cp = CursorPos - BrokenTextPositions[lineNo];
614 if ((s32)BrokenText[lineNo+1].size() < cp)
615 CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
617 CursorPos = BrokenTextPositions[lineNo+1] + cp;
620 if (event.KeyInput.Shift)
623 newMarkEnd = CursorPos;
639 if ( !this->IsEnabled )
645 if (MarkBegin != MarkEnd)
647 // delete marked text
648 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
649 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
651 s = Text.subString(0, realmbgn);
652 s.append( Text.subString(realmend, Text.size()-realmend) );
655 CursorPos = realmbgn;
659 // delete text behind cursor
661 s = Text.subString(0, CursorPos-1);
664 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
671 BlinkStartTime = porting::getTimeMs();
678 if ( !this->IsEnabled )
684 if (MarkBegin != MarkEnd)
686 // delete marked text
687 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
688 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
690 s = Text.subString(0, realmbgn);
691 s.append( Text.subString(realmend, Text.size()-realmend) );
694 CursorPos = realmbgn;
698 // delete text before cursor
699 s = Text.subString(0, CursorPos);
700 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
704 if (CursorPos > (s32)Text.size())
705 CursorPos = (s32)Text.size();
707 BlinkStartTime = porting::getTimeMs();
745 inputChar(event.KeyInput.Char);
749 // Set new text markers
750 setTextMarkers( newMarkBegin, newMarkEnd );
752 // break the text if it has changed
756 sendGuiEvent(EGET_EDITBOX_CHANGED);
759 calculateScrollPos();
765 //! draws the element and its children
766 void intlGUIEditBox::draw()
771 const bool focus = Environment->hasFocus(this);
773 IGUISkin* skin = Environment->getSkin();
777 FrameRect = AbsoluteRect;
784 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
785 false, true, FrameRect, &AbsoluteClippingRect);
788 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
789 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
790 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
791 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
795 core::rect<s32> localClipRect = FrameRect;
796 localClipRect.clipAgainst(AbsoluteClippingRect);
800 IGUIFont* font = OverrideFont;
802 font = skin->getFont();
805 s32 charcursorpos = 0;
809 if (LastBreakFont != font)
814 // calculate cursor pos
816 core::stringw *txtLine = &Text;
822 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
823 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
824 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
825 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
826 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
827 const s32 lineCount = ml ? BrokenText.size() : 1;
829 // Save the override color information.
830 // Then, alter it if the edit box is disabled.
831 const bool prevOver = OverrideColorEnabled;
832 const video::SColor prevColor = OverrideColor;
835 if (!IsEnabled && !OverrideColorEnabled)
837 OverrideColorEnabled = true;
838 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
841 for (s32 i=0; i < lineCount; ++i)
845 // clipping test - don't draw anything outside the visible area
846 core::rect<s32> c = localClipRect;
847 c.clipAgainst(CurrentTextRect);
854 if (BrokenText.size() != 1)
857 BrokenText.push_back(core::stringw());
859 if (BrokenText[0].size() != Text.size())
861 BrokenText[0] = Text;
862 for (u32 q = 0; q < Text.size(); ++q)
864 BrokenText[0] [q] = PasswordChar;
867 txtLine = &BrokenText[0];
872 txtLine = ml ? &BrokenText[i] : &Text;
873 startPos = ml ? BrokenTextPositions[i] : 0;
878 font->draw(txtLine->c_str(), CurrentTextRect,
879 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
880 false, true, &localClipRect);
882 // draw mark and marked text
883 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
886 s32 mbegin = 0, mend = 0;
887 s32 lineStartPos = 0, lineEndPos = txtLine->size();
891 // highlight start is on this line
892 s = txtLine->subString(0, realmbgn - startPos);
893 mbegin = font->getDimension(s.c_str()).Width;
896 mbegin += font->getKerningWidth(
897 &((*txtLine)[realmbgn - startPos]),
898 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
900 lineStartPos = realmbgn - startPos;
902 if (i == hlineStart + hlineCount - 1)
904 // highlight end is on this line
905 s2 = txtLine->subString(0, realmend - startPos);
906 mend = font->getDimension(s2.c_str()).Width;
907 lineEndPos = (s32)s2.size();
910 mend = font->getDimension(txtLine->c_str()).Width;
912 CurrentTextRect.UpperLeftCorner.X += mbegin;
913 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
916 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
919 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
922 font->draw(s.c_str(), CurrentTextRect,
923 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
924 false, true, &localClipRect);
929 // Return the override color information to its previous settings.
930 OverrideColorEnabled = prevOver;
931 OverrideColor = prevColor;
936 if (WordWrap || MultiLine)
938 cursorLine = getLineFromPos(CursorPos);
939 txtLine = &BrokenText[cursorLine];
940 startPos = BrokenTextPositions[cursorLine];
942 s = txtLine->subString(0,CursorPos-startPos);
943 charcursorpos = font->getDimension(s.c_str()).Width +
944 font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
947 if (focus && (porting::getTimeMs() - BlinkStartTime) % 700 < 350) {
948 setTextRect(cursorLine);
949 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
951 font->draw(L"_", CurrentTextRect,
952 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
953 false, true, &localClipRect);
963 //! Sets the new caption of this element.
964 void intlGUIEditBox::setText(const wchar_t* text)
967 if (u32(CursorPos) > Text.size())
968 CursorPos = Text.size();
974 //! Enables or disables automatic scrolling with cursor position
975 //! \param enable: If set to true, the text will move around with the cursor position
976 void intlGUIEditBox::setAutoScroll(bool enable)
982 //! Checks to see if automatic scrolling is enabled
983 //! \return true if automatic scrolling is enabled, false if not
984 bool intlGUIEditBox::isAutoScrollEnabled() const
986 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
991 //! Gets the area of the text in the edit box
992 //! \return Returns the size in pixels of the text
993 core::dimension2du intlGUIEditBox::getTextDimension()
998 ret = CurrentTextRect;
1000 for (u32 i=1; i < BrokenText.size(); ++i)
1003 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
1004 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
1007 return core::dimension2du(ret.getSize());
1011 //! Sets the maximum amount of characters which may be entered in the box.
1012 //! \param max: Maximum amount of characters. If 0, the character amount is
1014 void intlGUIEditBox::setMax(u32 max)
1018 if (Text.size() > Max && Max != 0)
1019 Text = Text.subString(0, Max);
1023 //! Returns maximum amount of characters, previously set by setMax();
1024 u32 intlGUIEditBox::getMax() const
1030 bool intlGUIEditBox::processMouse(const SEvent& event)
1032 switch(event.MouseInput.Event)
1034 case irr::EMIE_LMOUSE_LEFT_UP:
1035 if (Environment->hasFocus(this))
1037 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1040 setTextMarkers( MarkBegin, CursorPos );
1042 MouseMarking = false;
1043 calculateScrollPos();
1047 case irr::EMIE_MOUSE_MOVED:
1051 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1052 setTextMarkers( MarkBegin, CursorPos );
1053 calculateScrollPos();
1058 case EMIE_LMOUSE_PRESSED_DOWN:
1059 if (!Environment->hasFocus(this))
1061 BlinkStartTime = porting::getTimeMs();
1062 MouseMarking = true;
1063 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1064 setTextMarkers(CursorPos, CursorPos );
1065 calculateScrollPos();
1070 if (!AbsoluteClippingRect.isPointInside(
1071 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
1077 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1079 s32 newMarkBegin = MarkBegin;
1081 newMarkBegin = CursorPos;
1083 MouseMarking = true;
1084 setTextMarkers( newMarkBegin, CursorPos);
1085 calculateScrollPos();
1089 case EMIE_MOUSE_WHEEL:
1090 if (m_vscrollbar && m_vscrollbar->isVisible()) {
1091 s32 pos = m_vscrollbar->getPos();
1092 s32 step = m_vscrollbar->getSmallStep();
1093 m_vscrollbar->setPos(pos - event.MouseInput.Wheel * step);
1104 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
1106 IGUIFont* font = OverrideFont;
1107 IGUISkin* skin = Environment->getSkin();
1109 font = skin->getFont();
1111 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1113 core::stringw *txtLine = NULL;
1115 u32 curr_line_idx = 0;
1118 for (; curr_line_idx < lineCount; ++curr_line_idx) {
1119 setTextRect(curr_line_idx);
1120 if (curr_line_idx == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1121 y = CurrentTextRect.UpperLeftCorner.Y;
1122 if (curr_line_idx == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y)
1123 y = CurrentTextRect.LowerRightCorner.Y;
1125 // is it inside this region?
1126 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
1127 // we've found the clicked line
1128 txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
1129 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
1134 if (x < CurrentTextRect.UpperLeftCorner.X)
1135 x = CurrentTextRect.UpperLeftCorner.X;
1136 else if (x > CurrentTextRect.LowerRightCorner.X)
1137 x = CurrentTextRect.LowerRightCorner.X;
1139 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1140 // Special handling for last line, if we are on limits, add 1 extra shift because idx
1141 // will be the last char, not null char of the wstring
1142 if (curr_line_idx == lineCount - 1 && x == CurrentTextRect.LowerRightCorner.X)
1145 return rangelim(idx + startPos, 0, S32_MAX);
1149 //! Breaks the single text line.
1150 void intlGUIEditBox::breakText()
1152 IGUISkin* skin = Environment->getSkin();
1154 if ((!WordWrap && !MultiLine) || !skin)
1157 BrokenText.clear(); // need to reallocate :/
1158 BrokenTextPositions.set_used(0);
1160 IGUIFont* font = OverrideFont;
1162 font = skin->getFont();
1167 LastBreakFont = font;
1171 core::stringw whitespace;
1172 s32 lastLineStart = 0;
1173 s32 size = Text.size();
1175 s32 elWidth = RelativeRect.getWidth() - 6;
1178 for (s32 i=0; i<size; ++i)
1181 bool lineBreak = false;
1183 if (c == L'\r') // Mac or Windows breaks
1187 if (Text[i+1] == L'\n') // Windows breaks
1193 else if (c == L'\n') // Unix breaks
1199 // don't break if we're not a multi-line edit box
1203 if (c == L' ' || c == 0 || i == (size-1))
1205 if (!word.empty()) {
1206 // here comes the next whitespace, look if
1207 // we can break the last word to the next line.
1208 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1209 s32 worldlgth = font->getDimension(word.c_str()).Width;
1211 if (WordWrap && length + worldlgth + whitelgth > elWidth)
1213 // break to next line
1215 BrokenText.push_back(line);
1216 BrokenTextPositions.push_back(lastLineStart);
1217 lastLineStart = i - (s32)word.size();
1225 length += whitelgth + worldlgth;
1234 // compute line break
1239 BrokenText.push_back(line);
1240 BrokenTextPositions.push_back(lastLineStart);
1241 lastLineStart = i+1;
1250 // yippee this is a word..
1257 BrokenText.push_back(line);
1258 BrokenTextPositions.push_back(lastLineStart);
1262 void intlGUIEditBox::setTextRect(s32 line)
1264 core::dimension2du d;
1266 IGUISkin* skin = Environment->getSkin();
1270 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1275 // get text dimension
1276 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1277 if (WordWrap || MultiLine)
1279 d = font->getDimension(BrokenText[line].c_str());
1283 d = font->getDimension(Text.c_str());
1284 d.Height = AbsoluteRect.getHeight();
1286 d.Height += font->getKerningHeight();
1292 // align to h centre
1293 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1294 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1296 case EGUIA_LOWERRIGHT:
1297 // align to right edge
1298 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1299 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1302 // align to left edge
1303 CurrentTextRect.UpperLeftCorner.X = 0;
1304 CurrentTextRect.LowerRightCorner.X = d.Width;
1311 // align to v centre
1312 CurrentTextRect.UpperLeftCorner.Y =
1313 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1315 case EGUIA_LOWERRIGHT:
1316 // align to bottom edge
1317 CurrentTextRect.UpperLeftCorner.Y =
1318 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1321 // align to top edge
1322 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1326 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1327 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1328 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1329 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1331 CurrentTextRect += FrameRect.UpperLeftCorner;
1336 s32 intlGUIEditBox::getLineFromPos(s32 pos)
1338 if (!WordWrap && !MultiLine)
1342 while (i < (s32)BrokenTextPositions.size())
1344 if (BrokenTextPositions[i] > pos)
1348 return (s32)BrokenTextPositions.size() - 1;
1352 void intlGUIEditBox::inputChar(wchar_t c)
1359 if (Text.size() < Max || Max == 0)
1363 if (MarkBegin != MarkEnd)
1365 // replace marked text
1366 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1367 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1369 s = Text.subString(0, realmbgn);
1371 s.append( Text.subString(realmend, Text.size()-realmend) );
1373 CursorPos = realmbgn+1;
1377 // add new character
1378 s = Text.subString(0, CursorPos);
1380 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1385 BlinkStartTime = porting::getTimeMs();
1386 setTextMarkers(0, 0);
1390 sendGuiEvent(EGET_EDITBOX_CHANGED);
1391 calculateScrollPos();
1395 void intlGUIEditBox::calculateScrollPos()
1400 // calculate horizontal scroll position
1401 s32 cursLine = getLineFromPos(CursorPos);
1402 setTextRect(cursLine);
1404 // don't do horizontal scrolling when wordwrap is enabled.
1407 // get cursor position
1408 IGUISkin* skin = Environment->getSkin();
1411 IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
1415 core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1416 s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1418 s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1419 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1421 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1423 if (FrameRect.LowerRightCorner.X < cEnd)
1424 HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1425 else if (FrameRect.UpperLeftCorner.X > cStart)
1426 HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1430 // todo: adjust scrollbar
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_