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)
263 if (IsEnabled && m_writable)
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)
949 setTextRect(cursorLine);
950 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
952 font->draw(L"_", CurrentTextRect,
953 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
954 false, true, &localClipRect);
964 //! Sets the new caption of this element.
965 void intlGUIEditBox::setText(const wchar_t* text)
968 if (u32(CursorPos) > Text.size())
969 CursorPos = Text.size();
975 //! Enables or disables automatic scrolling with cursor position
976 //! \param enable: If set to true, the text will move around with the cursor position
977 void intlGUIEditBox::setAutoScroll(bool enable)
983 //! Checks to see if automatic scrolling is enabled
984 //! \return true if automatic scrolling is enabled, false if not
985 bool intlGUIEditBox::isAutoScrollEnabled() const
987 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
992 //! Gets the area of the text in the edit box
993 //! \return Returns the size in pixels of the text
994 core::dimension2du intlGUIEditBox::getTextDimension()
999 ret = CurrentTextRect;
1001 for (u32 i=1; i < BrokenText.size(); ++i)
1004 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
1005 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
1008 return core::dimension2du(ret.getSize());
1012 //! Sets the maximum amount of characters which may be entered in the box.
1013 //! \param max: Maximum amount of characters. If 0, the character amount is
1015 void intlGUIEditBox::setMax(u32 max)
1019 if (Text.size() > Max && Max != 0)
1020 Text = Text.subString(0, Max);
1024 //! Returns maximum amount of characters, previously set by setMax();
1025 u32 intlGUIEditBox::getMax() const
1031 bool intlGUIEditBox::processMouse(const SEvent& event)
1033 switch(event.MouseInput.Event)
1035 case irr::EMIE_LMOUSE_LEFT_UP:
1036 if (Environment->hasFocus(this))
1038 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1041 setTextMarkers( MarkBegin, CursorPos );
1043 MouseMarking = false;
1044 calculateScrollPos();
1048 case irr::EMIE_MOUSE_MOVED:
1052 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1053 setTextMarkers( MarkBegin, CursorPos );
1054 calculateScrollPos();
1059 case EMIE_LMOUSE_PRESSED_DOWN:
1060 if (!Environment->hasFocus(this))
1062 BlinkStartTime = porting::getTimeMs();
1063 MouseMarking = true;
1064 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1065 setTextMarkers(CursorPos, CursorPos );
1066 calculateScrollPos();
1071 if (!AbsoluteClippingRect.isPointInside(
1072 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
1078 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1080 s32 newMarkBegin = MarkBegin;
1082 newMarkBegin = CursorPos;
1084 MouseMarking = true;
1085 setTextMarkers( newMarkBegin, CursorPos);
1086 calculateScrollPos();
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)
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 // vertical scroll position
1427 if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1428 VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1430 else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1431 VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1435 // todo: adjust scrollbar
1437 m_vscrollbar->setPos(VScrollPos);
1441 //! set text markers
1442 void intlGUIEditBox::setTextMarkers(s32 begin, s32 end)
1444 if ( begin != MarkBegin || end != MarkEnd )
1448 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1452 //! send some gui event to parent
1453 void intlGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1458 e.EventType = EET_GUI_EVENT;
1459 e.GUIEvent.Caller = this;
1460 e.GUIEvent.Element = 0;
1461 e.GUIEvent.EventType = type;
1467 //! Create a vertical scrollbar
1468 void intlGUIEditBox::createVScrollBar()
1470 irr::core::rect<s32> scrollbarrect = FrameRect;
1471 scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
1472 m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1473 m_vscrollbar->setVisible(false);
1474 m_vscrollbar->setSmallStep(1);
1475 m_vscrollbar->setLargeStep(1);
1478 //! Update the vertical scrollbar (visibilty & scroll position)
1479 void intlGUIEditBox::updateVScrollBar()
1481 if (!m_vscrollbar) {
1485 // OnScrollBarChanged(...)
1486 if (m_vscrollbar->getPos() != VScrollPos) {
1487 s32 deltaScrollY = m_vscrollbar->getPos() - VScrollPos;
1488 CurrentTextRect.UpperLeftCorner.Y -= deltaScrollY;
1489 CurrentTextRect.LowerRightCorner.Y -= deltaScrollY;
1491 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1492 if (scrollymax != m_vscrollbar->getMax()) {
1493 // manage a newline or a deleted line
1494 m_vscrollbar->setMax(scrollymax);
1495 calculateScrollPos();
1497 // manage a newline or a deleted line
1498 VScrollPos = m_vscrollbar->getPos();
1502 // check if a vertical scrollbar is needed ?
1503 if (getTextDimension().Height > FrameRect.getHeight()) {
1504 s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
1505 if (scrollymax != m_vscrollbar->getMax()) {
1506 m_vscrollbar->setMax(scrollymax);
1509 if (!m_vscrollbar->isVisible() && MultiLine) {
1510 AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
1512 m_vscrollbar->setVisible(true);
1515 if (m_vscrollbar->isVisible()) {
1516 AbsoluteRect.LowerRightCorner.X += m_scrollbar_width;
1519 m_vscrollbar->setPos(0);
1520 m_vscrollbar->setMax(1);
1521 m_vscrollbar->setVisible(false);
1526 void intlGUIEditBox::setWritable(bool can_write_text)
1528 m_writable = can_write_text;
1531 //! Writes attributes of the element.
1532 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1534 // IGUIEditBox::serializeAttributes(out,options);
1536 out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1537 out->addColor ("OverrideColor", OverrideColor);
1538 // out->addFont("OverrideFont",OverrideFont);
1539 out->addInt ("MaxChars", Max);
1540 out->addBool ("WordWrap", WordWrap);
1541 out->addBool ("MultiLine", MultiLine);
1542 out->addBool ("AutoScroll", AutoScroll);
1543 out->addBool ("PasswordBox", PasswordBox);
1544 core::stringw ch = L" ";
1545 ch[0] = PasswordChar;
1546 out->addString("PasswordChar", ch.c_str());
1547 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1548 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1549 out->addBool ("Writable", m_writable);
1551 IGUIEditBox::serializeAttributes(out,options);
1555 //! Reads attributes of the element
1556 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1558 IGUIEditBox::deserializeAttributes(in,options);
1560 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1561 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1562 setMax(in->getAttributeAsInt("MaxChars"));
1563 setWordWrap(in->getAttributeAsBool("WordWrap"));
1564 setMultiLine(in->getAttributeAsBool("MultiLine"));
1565 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1566 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1569 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1571 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1573 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1574 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1576 setWritable(in->getAttributeAsBool("Writable"));
1577 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1581 } // end namespace gui
1582 } // end namespace irr
1584 #endif // _IRR_COMPILE_WITH_GUI_