1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CGUIEditBox.h"
\r
6 #ifdef _IRR_COMPILE_WITH_GUI_
\r
8 #include "IGUISkin.h"
\r
9 #include "IGUIEnvironment.h"
\r
10 #include "IGUIFont.h"
\r
11 #include "IVideoDriver.h"
\r
14 #include "Keycodes.h"
\r
19 ctrl+left/right to select word
\r
20 double click/ctrl click: word select + drag to select whole words, triple click to select line
\r
21 optional? dragging selected text
\r
31 CGUIEditBox::CGUIEditBox(const wchar_t* text, bool border,
\r
32 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
\r
33 const core::rect<s32>& rectangle)
\r
34 : IGUIEditBox(environment, parent, id, rectangle), OverwriteMode(false), MouseMarking(false),
\r
35 Border(border), Background(true), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
\r
36 OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
\r
37 Operator(0), BlinkStartTime(0), CursorBlinkTime(350), CursorChar(L"_"), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
\r
38 WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
\r
39 PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
\r
40 CurrentTextRect(0,0,1,1), FrameRect(rectangle)
\r
43 setDebugName("CGUIEditBox");
\r
49 Operator = Environment->getOSOperator();
\r
54 // this element can be tabbed to
\r
58 calculateFrameRect();
\r
61 calculateScrollPos();
\r
66 CGUIEditBox::~CGUIEditBox()
\r
69 OverrideFont->drop();
\r
76 //! Sets another skin independent font.
\r
77 void CGUIEditBox::setOverrideFont(IGUIFont* font)
\r
79 if (OverrideFont == font)
\r
83 OverrideFont->drop();
\r
85 OverrideFont = font;
\r
88 OverrideFont->grab();
\r
93 //! Gets the override font (if any)
\r
94 IGUIFont * CGUIEditBox::getOverrideFont() const
\r
96 return OverrideFont;
\r
99 //! Get the font which is used right now for drawing
\r
100 IGUIFont* CGUIEditBox::getActiveFont() const
\r
102 if ( OverrideFont )
\r
103 return OverrideFont;
\r
104 IGUISkin* skin = Environment->getSkin();
\r
106 return skin->getFont();
\r
110 //! Sets another color for the text.
\r
111 void CGUIEditBox::setOverrideColor(video::SColor color)
\r
113 OverrideColor = color;
\r
114 OverrideColorEnabled = true;
\r
118 video::SColor CGUIEditBox::getOverrideColor() const
\r
120 return OverrideColor;
\r
124 //! Turns the border on or off
\r
125 void CGUIEditBox::setDrawBorder(bool border)
\r
130 //! Checks if border drawing is enabled
\r
131 bool CGUIEditBox::isDrawBorderEnabled() const
\r
136 //! Sets whether to draw the background
\r
137 void CGUIEditBox::setDrawBackground(bool draw)
\r
142 //! Checks if background drawing is enabled
\r
143 bool CGUIEditBox::isDrawBackgroundEnabled() const
\r
148 //! Sets if the text should use the override color or the color in the gui skin.
\r
149 void CGUIEditBox::enableOverrideColor(bool enable)
\r
151 OverrideColorEnabled = enable;
\r
154 bool CGUIEditBox::isOverrideColorEnabled() const
\r
156 return OverrideColorEnabled;
\r
159 //! Enables or disables word wrap
\r
160 void CGUIEditBox::setWordWrap(bool enable)
\r
167 void CGUIEditBox::updateAbsolutePosition()
\r
169 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
\r
170 IGUIElement::updateAbsolutePosition();
\r
171 if ( oldAbsoluteRect != AbsoluteRect )
\r
173 calculateFrameRect();
\r
175 calculateScrollPos();
\r
180 //! Checks if word wrap is enabled
\r
181 bool CGUIEditBox::isWordWrapEnabled() const
\r
187 //! Enables or disables newlines.
\r
188 void CGUIEditBox::setMultiLine(bool enable)
\r
190 MultiLine = enable;
\r
195 //! Checks if multi line editing is enabled
\r
196 bool CGUIEditBox::isMultiLineEnabled() const
\r
202 void CGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
\r
204 PasswordBox = passwordBox;
\r
207 PasswordChar = passwordChar;
\r
208 setMultiLine(false);
\r
209 setWordWrap(false);
\r
210 BrokenText.clear();
\r
215 bool CGUIEditBox::isPasswordBox() const
\r
217 return PasswordBox;
\r
221 //! Sets text justification
\r
222 void CGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
\r
224 HAlign = horizontal;
\r
229 //! called if an event happened.
\r
230 bool CGUIEditBox::OnEvent(const SEvent& event)
\r
235 switch(event.EventType)
\r
237 case EET_GUI_EVENT:
\r
238 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
\r
240 if (event.GUIEvent.Caller == this)
\r
242 MouseMarking = false;
\r
243 setTextMarkers(0,0);
\r
247 case EET_KEY_INPUT_EVENT:
\r
248 if (processKey(event))
\r
251 case EET_MOUSE_INPUT_EVENT:
\r
252 if (processMouse(event))
\r
255 case EET_STRING_INPUT_EVENT:
\r
256 inputString(*event.StringInput.Str);
\r
264 return IGUIElement::OnEvent(event);
\r
268 bool CGUIEditBox::processKey(const SEvent& event)
\r
270 if (!event.KeyInput.PressedDown)
\r
273 bool textChanged = false;
\r
274 s32 newMarkBegin = MarkBegin;
\r
275 s32 newMarkEnd = MarkEnd;
\r
277 // control shortcut handling
\r
279 if (event.KeyInput.Control)
\r
281 // german backlash '\' entered with control + '?'
\r
282 if ( event.KeyInput.Char == '\\' )
\r
284 inputChar(event.KeyInput.Char);
\r
288 switch(event.KeyInput.Key)
\r
293 newMarkEnd = Text.size();
\r
296 // copy to clipboard
\r
297 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
\r
299 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
300 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
303 wStringToMultibyte(s, Text.subString(realmbgn, realmend - realmbgn));
\r
304 Operator->copyToClipboard(s.c_str());
\r
308 // cut to the clipboard
\r
309 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
\r
311 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
312 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
316 wStringToMultibyte(sc, Text.subString(realmbgn, realmend - realmbgn));
\r
317 Operator->copyToClipboard(sc.c_str());
\r
323 s = Text.subString(0, realmbgn);
\r
324 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
327 CursorPos = realmbgn;
\r
330 textChanged = true;
\r
335 if ( !isEnabled() )
\r
338 // paste from the clipboard
\r
341 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
342 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
345 const c8 *p = Operator->getTextFromClipboard();
\r
348 irr::core::stringw widep;
\r
349 core::multibyteToWString(widep, p);
\r
351 if (MarkBegin == MarkEnd)
\r
354 core::stringw s = Text.subString(0, CursorPos);
\r
356 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
358 if (!Max || s.size()<=Max) // thx to Fish FH for fix
\r
362 CursorPos += s.size();
\r
369 core::stringw s = Text.subString(0, realmbgn);
\r
371 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
373 if (!Max || s.size()<=Max) // thx to Fish FH for fix
\r
377 CursorPos = realmbgn + s.size();
\r
384 textChanged = true;
\r
388 // move/highlight to start of text
\r
389 if (event.KeyInput.Shift)
\r
391 newMarkEnd = CursorPos;
\r
403 // move/highlight to end of text
\r
404 if (event.KeyInput.Shift)
\r
406 newMarkBegin = CursorPos;
\r
407 newMarkEnd = Text.size();
\r
412 CursorPos = Text.size();
\r
421 // Some special keys - but only handle them if KeyInput.Char is null as on some systems (X11) they might have same key-code as ansi-keys otherwise
\r
422 else if (event.KeyInput.Char == 0)
\r
424 switch(event.KeyInput.Key)
\r
428 s32 p = Text.size();
\r
429 if (WordWrap || MultiLine)
\r
431 p = getLineFromPos(CursorPos);
\r
432 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
\r
433 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
\r
437 if (event.KeyInput.Shift)
\r
439 if (MarkBegin == MarkEnd)
\r
440 newMarkBegin = CursorPos;
\r
450 BlinkStartTime = os::Timer::getTime();
\r
457 if (WordWrap || MultiLine)
\r
459 p = getLineFromPos(CursorPos);
\r
460 p = BrokenTextPositions[p];
\r
463 if (event.KeyInput.Shift)
\r
465 if (MarkBegin == MarkEnd)
\r
466 newMarkBegin = CursorPos;
\r
475 BlinkStartTime = os::Timer::getTime();
\r
480 if (event.KeyInput.Shift)
\r
484 if (MarkBegin == MarkEnd)
\r
485 newMarkBegin = CursorPos;
\r
487 newMarkEnd = CursorPos-1;
\r
496 if (CursorPos > 0) CursorPos--;
\r
497 BlinkStartTime = os::Timer::getTime();
\r
501 if (event.KeyInput.Shift)
\r
503 if (Text.size() > (u32)CursorPos)
\r
505 if (MarkBegin == MarkEnd)
\r
506 newMarkBegin = CursorPos;
\r
508 newMarkEnd = CursorPos+1;
\r
517 if (Text.size() > (u32)CursorPos) CursorPos++;
\r
518 BlinkStartTime = os::Timer::getTime();
\r
521 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
\r
523 s32 lineNo = getLineFromPos(CursorPos);
\r
524 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
\r
527 s32 cp = CursorPos - BrokenTextPositions[lineNo];
\r
528 if ((s32)BrokenText[lineNo-1].size() < cp)
\r
529 CursorPos = BrokenTextPositions[lineNo-1] + core::max_((u32)1, BrokenText[lineNo-1].size())-1;
\r
531 CursorPos = BrokenTextPositions[lineNo-1] + cp;
\r
534 if (event.KeyInput.Shift)
\r
537 newMarkEnd = CursorPos;
\r
552 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
\r
554 s32 lineNo = getLineFromPos(CursorPos);
\r
555 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
\r
556 if (lineNo < (s32)BrokenText.size()-1)
\r
558 s32 cp = CursorPos - BrokenTextPositions[lineNo];
\r
559 if ((s32)BrokenText[lineNo+1].size() < cp)
\r
560 CursorPos = BrokenTextPositions[lineNo+1] + core::max_((u32)1, BrokenText[lineNo+1].size())-1;
\r
562 CursorPos = BrokenTextPositions[lineNo+1] + cp;
\r
565 if (event.KeyInput.Shift)
\r
568 newMarkEnd = CursorPos;
\r
583 if ( !isEnabled() )
\r
586 OverwriteMode = !OverwriteMode;
\r
589 if ( !isEnabled() )
\r
594 BlinkStartTime = os::Timer::getTime();
\r
597 textChanged = true;
\r
606 // default keyboard handling
\r
607 switch(event.KeyInput.Key)
\r
616 calculateScrollPos();
\r
617 sendGuiEvent( EGET_EDITBOX_ENTER );
\r
622 if ( !isEnabled() )
\r
629 if (MarkBegin != MarkEnd)
\r
631 // delete marked text
\r
632 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
633 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
635 s = Text.subString(0, realmbgn);
\r
636 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
639 CursorPos = realmbgn;
\r
643 // delete text behind cursor
\r
645 s = Text.subString(0, CursorPos-1);
\r
648 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
655 BlinkStartTime = os::Timer::getTime();
\r
658 textChanged = true;
\r
664 // At least on X11 we get a char with 127 when the delete key is pressed.
\r
665 // We get no char when the delete key on numkeys is pressed with numlock off (handled in the other case calling keyDelete as Char is then 0).
\r
666 // We get a keykode != 127 when delete key on numlock is pressed with numlock on.
\r
667 if (event.KeyInput.Char == 127)
\r
669 if ( !isEnabled() )
\r
674 BlinkStartTime = os::Timer::getTime();
\r
677 textChanged = true;
\r
683 inputChar(event.KeyInput.Char);
\r
714 // ignore these keys
\r
718 inputChar(event.KeyInput.Char);
\r
723 // Set new text markers
\r
724 setTextMarkers( newMarkBegin, newMarkEnd );
\r
726 // break the text if it has changed
\r
730 calculateScrollPos();
\r
731 sendGuiEvent(EGET_EDITBOX_CHANGED);
\r
735 calculateScrollPos();
\r
741 bool CGUIEditBox::keyDelete()
\r
743 if (Text.size() != 0)
\r
747 if (MarkBegin != MarkEnd)
\r
749 // delete marked text
\r
750 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
751 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
753 s = Text.subString(0, realmbgn);
\r
754 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
757 CursorPos = realmbgn;
\r
761 // delete text before cursor
\r
762 s = Text.subString(0, CursorPos);
\r
763 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
\r
767 if (CursorPos > (s32)Text.size())
\r
768 CursorPos = (s32)Text.size();
\r
776 //! draws the element and its children
\r
777 void CGUIEditBox::draw()
\r
782 const bool focus = Environment->hasFocus(this);
\r
784 IGUISkin* skin = Environment->getSkin();
\r
788 EGUI_DEFAULT_COLOR bgCol = EGDC_GRAY_EDITABLE;
\r
790 bgCol = focus ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;
\r
792 if (!Border && Background)
\r
794 skin->draw2DRectangle(this, skin->getColor(bgCol), AbsoluteRect, &AbsoluteClippingRect);
\r
800 skin->draw3DSunkenPane(this, skin->getColor(bgCol), false, Background, AbsoluteRect, &AbsoluteClippingRect);
\r
802 calculateFrameRect();
\r
805 core::rect<s32> localClipRect = FrameRect;
\r
806 localClipRect.clipAgainst(AbsoluteClippingRect);
\r
810 IGUIFont* font = getActiveFont();
\r
812 s32 cursorLine = 0;
\r
813 s32 charcursorpos = 0;
\r
817 if (LastBreakFont != font)
\r
822 // calculate cursor pos
\r
824 core::stringw *txtLine = &Text;
\r
827 core::stringw s, s2;
\r
829 // get mark position
\r
830 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
\r
831 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
832 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
833 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
\r
834 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
\r
835 const s32 lineCount = ml ? BrokenText.size() : 1;
\r
837 // Save the override color information.
\r
838 // Then, alter it if the edit box is disabled.
\r
839 const bool prevOver = OverrideColorEnabled;
\r
840 const video::SColor prevColor = OverrideColor;
\r
844 if (!isEnabled() && !OverrideColorEnabled)
\r
846 OverrideColorEnabled = true;
\r
847 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
\r
850 for (s32 i=0; i < lineCount; ++i)
\r
854 // clipping test - don't draw anything outside the visible area
\r
855 core::rect<s32> c = localClipRect;
\r
856 c.clipAgainst(CurrentTextRect);
\r
860 // get current line
\r
863 if (BrokenText.size() != 1)
\r
865 BrokenText.clear();
\r
866 BrokenText.push_back(core::stringw());
\r
868 if (BrokenText[0].size() != Text.size())
\r
870 BrokenText[0] = Text;
\r
871 for (u32 q = 0; q < Text.size(); ++q)
\r
873 BrokenText[0] [q] = PasswordChar;
\r
876 txtLine = &BrokenText[0];
\r
881 txtLine = ml ? &BrokenText[i] : &Text;
\r
882 startPos = ml ? BrokenTextPositions[i] : 0;
\r
886 // draw normal text
\r
887 font->draw(txtLine->c_str(), CurrentTextRect,
\r
888 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
\r
889 false, true, &localClipRect);
\r
891 // draw mark and marked text
\r
892 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
\r
895 s32 mbegin = 0, mend = 0;
\r
896 s32 lineStartPos = 0, lineEndPos = txtLine->size();
\r
898 if (i == hlineStart)
\r
900 // highlight start is on this line
\r
901 s = txtLine->subString(0, realmbgn - startPos);
\r
902 mbegin = font->getDimension(s.c_str()).Width;
\r
904 // deal with kerning
\r
905 mbegin += font->getKerningWidth(
\r
906 &((*txtLine)[realmbgn - startPos]),
\r
907 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
\r
909 lineStartPos = realmbgn - startPos;
\r
911 if (i == hlineStart + hlineCount - 1)
\r
913 // highlight end is on this line
\r
914 s2 = txtLine->subString(0, realmend - startPos);
\r
915 mend = font->getDimension(s2.c_str()).Width;
\r
916 lineEndPos = (s32)s2.size();
\r
919 mend = font->getDimension(txtLine->c_str()).Width;
\r
921 CurrentTextRect.UpperLeftCorner.X += mbegin;
\r
922 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
\r
925 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
\r
927 // draw marked text
\r
928 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
\r
931 font->draw(s.c_str(), CurrentTextRect,
\r
932 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
\r
933 false, true, &localClipRect);
\r
938 // Return the override color information to its previous settings.
\r
939 OverrideColorEnabled = prevOver;
\r
940 OverrideColor = prevColor;
\r
946 if (WordWrap || MultiLine)
\r
948 cursorLine = getLineFromPos(CursorPos);
\r
949 txtLine = &BrokenText[cursorLine];
\r
950 startPos = BrokenTextPositions[cursorLine];
\r
952 s = txtLine->subString(0,CursorPos-startPos);
\r
953 charcursorpos = font->getDimension(s.c_str()).Width +
\r
954 font->getKerningWidth(CursorChar.c_str(), CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
\r
956 if (focus && (CursorBlinkTime == 0 || (os::Timer::getTime() - BlinkStartTime) % (2*CursorBlinkTime) < CursorBlinkTime))
\r
958 setTextRect(cursorLine);
\r
959 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
\r
961 if ( OverwriteMode )
\r
963 core::stringw character = Text.subString(CursorPos,1);
\r
964 s32 mend = font->getDimension(character.c_str()).Width;
\r
965 //Make sure the cursor box has at least some width to it
\r
967 mend = font->getDimension(CursorChar.c_str()).Width;
\r
968 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend;
\r
969 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
\r
970 font->draw(character.c_str(), CurrentTextRect,
\r
971 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
\r
972 false, true, &localClipRect);
\r
976 font->draw(CursorChar, CurrentTextRect,
\r
977 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
\r
978 false, true, &localClipRect);
\r
985 IGUIElement::draw();
\r
989 //! Sets the new caption of this element.
\r
990 void CGUIEditBox::setText(const wchar_t* text)
\r
993 if (u32(CursorPos) > Text.size())
\r
994 CursorPos = Text.size();
\r
1000 //! Enables or disables automatic scrolling with cursor position
\r
1001 //! \param enable: If set to true, the text will move around with the cursor position
\r
1002 void CGUIEditBox::setAutoScroll(bool enable)
\r
1004 AutoScroll = enable;
\r
1008 //! Checks to see if automatic scrolling is enabled
\r
1009 //! \return true if automatic scrolling is enabled, false if not
\r
1010 bool CGUIEditBox::isAutoScrollEnabled() const
\r
1012 return AutoScroll;
\r
1016 //! Gets the area of the text in the edit box
\r
1017 //! \return Returns the size in pixels of the text
\r
1018 core::dimension2du CGUIEditBox::getTextDimension()
\r
1020 core::rect<s32> ret;
\r
1023 ret = CurrentTextRect;
\r
1025 for (u32 i=1; i < BrokenText.size(); ++i)
\r
1028 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
\r
1029 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
\r
1032 return core::dimension2du(ret.getSize());
\r
1036 //! Sets the maximum amount of characters which may be entered in the box.
\r
1037 //! \param max: Maximum amount of characters. If 0, the character amount is
\r
1039 void CGUIEditBox::setMax(u32 max)
\r
1043 if (Text.size() > Max && Max != 0)
\r
1044 Text = Text.subString(0, Max);
\r
1048 //! Returns maximum amount of characters, previously set by setMax();
\r
1049 u32 CGUIEditBox::getMax() const
\r
1054 //! Set the character used for the cursor.
\r
1055 /** By default it's "_" */
\r
1056 void CGUIEditBox::setCursorChar(const wchar_t cursorChar)
\r
1058 CursorChar[0] = cursorChar;
\r
1061 //! Get the character used for the cursor.
\r
1062 wchar_t CGUIEditBox::getCursorChar() const
\r
1064 return CursorChar[0];
\r
1067 //! Set the blinktime for the cursor. 2x blinktime is one full cycle.
\r
1068 void CGUIEditBox::setCursorBlinkTime(irr::u32 timeMs)
\r
1070 CursorBlinkTime = timeMs;
\r
1073 //! Get the cursor blinktime
\r
1074 irr::u32 CGUIEditBox::getCursorBlinkTime() const
\r
1076 return CursorBlinkTime;
\r
1079 bool CGUIEditBox::processMouse(const SEvent& event)
\r
1081 switch(event.MouseInput.Event)
\r
1083 case irr::EMIE_LMOUSE_LEFT_UP:
\r
1084 if (Environment->hasFocus(this))
\r
1086 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1089 setTextMarkers( MarkBegin, CursorPos );
\r
1091 MouseMarking = false;
\r
1092 calculateScrollPos();
\r
1096 case irr::EMIE_MOUSE_MOVED:
\r
1100 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1101 setTextMarkers( MarkBegin, CursorPos );
\r
1102 calculateScrollPos();
\r
1107 case EMIE_LMOUSE_PRESSED_DOWN:
\r
1108 if (!Environment->hasFocus(this)) // can happen when events are manually send to the element
\r
1110 BlinkStartTime = os::Timer::getTime();
\r
1111 MouseMarking = true;
\r
1112 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1113 setTextMarkers(CursorPos, CursorPos );
\r
1114 calculateScrollPos();
\r
1119 if (!AbsoluteClippingRect.isPointInside(
\r
1120 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
\r
1127 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1129 s32 newMarkBegin = MarkBegin;
\r
1130 if (!MouseMarking)
\r
1131 newMarkBegin = CursorPos;
\r
1133 MouseMarking = true;
\r
1134 setTextMarkers( newMarkBegin, CursorPos);
\r
1135 calculateScrollPos();
\r
1147 s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
\r
1149 IGUIFont* font = getActiveFont();
\r
1151 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
\r
1153 core::stringw *txtLine=0;
\r
1157 for (u32 i=0; i < lineCount; ++i)
\r
1160 if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
\r
1161 y = CurrentTextRect.UpperLeftCorner.Y;
\r
1162 if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
\r
1163 y = CurrentTextRect.LowerRightCorner.Y;
\r
1165 // is it inside this region?
\r
1166 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
\r
1168 // we've found the clicked line
\r
1169 txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
\r
1170 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
\r
1175 if (x < CurrentTextRect.UpperLeftCorner.X)
\r
1176 x = CurrentTextRect.UpperLeftCorner.X;
\r
1181 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
\r
1183 // click was on or left of the line
\r
1185 return idx + startPos;
\r
1187 // click was off the right edge of the line, go to end.
\r
1188 return txtLine->size() + startPos;
\r
1192 //! Breaks the single text line.
\r
1193 void CGUIEditBox::breakText()
\r
1195 if ((!WordWrap && !MultiLine))
\r
1198 BrokenText.clear(); // need to reallocate :/
\r
1199 BrokenTextPositions.set_used(0);
\r
1201 IGUIFont* font = getActiveFont();
\r
1205 LastBreakFont = font;
\r
1207 core::stringw line;
\r
1208 core::stringw word;
\r
1209 core::stringw whitespace;
\r
1210 s32 lastLineStart = 0;
\r
1211 s32 size = Text.size();
\r
1213 s32 elWidth = RelativeRect.getWidth() - 6;
\r
1216 for (s32 i=0; i<size; ++i)
\r
1219 bool lineBreak = false;
\r
1221 if (c == L'\r') // Mac or Windows breaks
\r
1225 if (Text[i+1] == L'\n') // Windows breaks
\r
1227 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
\r
1228 // Instead rework the cursor positioning to be able to handle this (but not in stable release
\r
1229 // branch as users might already expect this behavior).
\r
1232 if ( CursorPos > i )
\r
1236 else if (c == L'\n') // Unix breaks
\r
1242 // don't break if we're not a multi-line edit box
\r
1244 lineBreak = false;
\r
1246 if (c == L' ' || c == 0 || i == (size-1))
\r
1248 // here comes the next whitespace, look if
\r
1249 // we can break the last word to the next line
\r
1250 // We also break whitespace, otherwise cursor would vanish beside the right border.
\r
1251 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
1252 s32 worldlgth = font->getDimension(word.c_str()).Width;
\r
1254 if (WordWrap && length + worldlgth + whitelgth > elWidth && line.size() > 0)
\r
1256 // break to next line
\r
1257 length = worldlgth;
\r
1258 BrokenText.push_back(line);
\r
1259 BrokenTextPositions.push_back(lastLineStart);
\r
1260 lastLineStart = i - (s32)word.size();
\r
1265 // add word to line
\r
1266 line += whitespace;
\r
1268 length += whitelgth + worldlgth;
\r
1278 // compute line break
\r
1281 line += whitespace;
\r
1283 BrokenText.push_back(line);
\r
1284 BrokenTextPositions.push_back(lastLineStart);
\r
1285 lastLineStart = i+1;
\r
1294 // yippee this is a word..
\r
1299 line += whitespace;
\r
1301 BrokenText.push_back(line);
\r
1302 BrokenTextPositions.push_back(lastLineStart);
\r
1305 // TODO: that function does interpret VAlign according to line-index (indexed line is placed on top-center-bottom)
\r
1306 // but HAlign according to line-width (pixels) and not by row.
\r
1307 // Intuitively I suppose HAlign handling is better as VScrollPos should handle the line-scrolling.
\r
1308 // But please no one change this without also rewriting (and this time fucking testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
\r
1309 void CGUIEditBox::setTextRect(s32 line)
\r
1314 IGUIFont* font = getActiveFont();
\r
1318 core::dimension2du d;
\r
1320 // get text dimension
\r
1321 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
\r
1322 if (WordWrap || MultiLine)
\r
1324 d = font->getDimension(BrokenText[line].c_str());
\r
1328 d = font->getDimension(Text.c_str());
\r
1329 d.Height = AbsoluteRect.getHeight();
\r
1331 d.Height += font->getKerningHeight();
\r
1336 case EGUIA_CENTER:
\r
1337 // align to h centre
\r
1338 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
\r
1339 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
\r
1341 case EGUIA_LOWERRIGHT:
\r
1342 // align to right edge
\r
1343 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
\r
1344 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
\r
1347 // align to left edge
\r
1348 CurrentTextRect.UpperLeftCorner.X = 0;
\r
1349 CurrentTextRect.LowerRightCorner.X = d.Width;
\r
1355 case EGUIA_CENTER:
\r
1356 // align to v centre
\r
1357 CurrentTextRect.UpperLeftCorner.Y =
\r
1358 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
\r
1360 case EGUIA_LOWERRIGHT:
\r
1361 // align to bottom edge
\r
1362 CurrentTextRect.UpperLeftCorner.Y =
\r
1363 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
\r
1366 // align to top edge
\r
1367 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
\r
1371 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
\r
1372 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
\r
1373 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
\r
1374 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
\r
1376 CurrentTextRect += FrameRect.UpperLeftCorner;
\r
1381 s32 CGUIEditBox::getLineFromPos(s32 pos)
\r
1383 if (!WordWrap && !MultiLine)
\r
1387 while (i < (s32)BrokenTextPositions.size())
\r
1389 if (BrokenTextPositions[i] > pos)
\r
1393 return (s32)BrokenTextPositions.size() - 1;
\r
1397 void CGUIEditBox::inputChar(wchar_t c)
\r
1401 core::stringw s(&c, 1);
\r
1405 void CGUIEditBox::inputString(const core::stringw &str)
\r
1411 u32 len = str.size();
\r
1413 if (MarkBegin != MarkEnd)
\r
1415 // replace marked text
\r
1416 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
1417 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
1419 s = Text.subString(0, realmbgn);
\r
1421 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
1423 CursorPos = realmbgn+len;
\r
1425 else if ( OverwriteMode )
\r
1427 //check to see if we are at the end of the text
\r
1428 if ( (u32)CursorPos+len < Text.size())
\r
1430 bool isEOL = false;
\r
1432 for (u32 i = CursorPos; i < CursorPos+len && i < Max; i++)
\r
1434 if (Text[i] == L'\n' || Text[i] == L'\r')
\r
1441 if (!isEOL || Text.size()+len <= Max || Max == 0)
\r
1443 s = Text.subString(0, CursorPos);
\r
1447 //just keep appending to the current line
\r
1448 //This follows the behavior of other gui libraries behaviors
\r
1449 s.append( Text.subString(EOLPos, Text.size()-EOLPos) );
\r
1453 //replace the next character
\r
1454 s.append( Text.subString(CursorPos + len,Text.size() - CursorPos - len));
\r
1460 else if (Text.size()+len <= Max || Max == 0)
\r
1462 // add new character because we are at the end of the string
\r
1463 s = Text.subString(0, CursorPos);
\r
1465 s.append( Text.subString(CursorPos+len, Text.size()-CursorPos-len) );
\r
1470 else if (Text.size()+len <= Max || Max == 0)
\r
1472 // add new character
\r
1473 s = Text.subString(0, CursorPos);
\r
1475 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
1480 BlinkStartTime = os::Timer::getTime();
\r
1481 setTextMarkers(0, 0);
\r
1484 calculateScrollPos();
\r
1485 sendGuiEvent(EGET_EDITBOX_CHANGED);
\r
1488 // calculate autoscroll
\r
1489 void CGUIEditBox::calculateScrollPos()
\r
1494 IGUIFont* font = getActiveFont();
\r
1498 s32 cursLine = getLineFromPos(CursorPos);
\r
1499 if ( cursLine < 0 )
\r
1501 setTextRect(cursLine);
\r
1502 const bool hasBrokenText = MultiLine || WordWrap;
\r
1504 // Check horizonal scrolling
\r
1505 // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
\r
1507 // get cursor position
\r
1508 // get cursor area
\r
1509 irr::u32 cursorWidth = font->getDimension(CursorChar.c_str()).Width;
\r
1510 core::stringw *txtLine = hasBrokenText ? &BrokenText[cursLine] : &Text;
\r
1511 s32 cPos = hasBrokenText ? CursorPos - BrokenTextPositions[cursLine] : CursorPos; // column
\r
1512 s32 cStart = font->getDimension(txtLine->subString(0, cPos).c_str()).Width; // pixels from text-start
\r
1513 s32 cEnd = cStart + cursorWidth;
\r
1514 s32 txtWidth = font->getDimension(txtLine->c_str()).Width;
\r
1516 if ( txtWidth < FrameRect.getWidth() )
\r
1518 // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
\r
1519 // This check just fixes the case where it was most noticable (text smaller than clipping area).
\r
1522 setTextRect(cursLine);
\r
1525 if ( CurrentTextRect.UpperLeftCorner.X+cStart < FrameRect.UpperLeftCorner.X )
\r
1527 // cursor to the left of the clipping area
\r
1528 HScrollPos -= FrameRect.UpperLeftCorner.X-(CurrentTextRect.UpperLeftCorner.X+cStart);
\r
1529 setTextRect(cursLine);
\r
1531 // TODO: should show more characters to the left when we're scrolling left
\r
1532 // and the cursor reaches the border.
\r
1534 else if ( CurrentTextRect.UpperLeftCorner.X+cEnd > FrameRect.LowerRightCorner.X)
\r
1536 // cursor to the right of the clipping area
\r
1537 HScrollPos += (CurrentTextRect.UpperLeftCorner.X+cEnd)-FrameRect.LowerRightCorner.X;
\r
1538 setTextRect(cursLine);
\r
1542 // calculate vertical scrolling
\r
1543 if (hasBrokenText)
\r
1545 irr::u32 lineHeight = font->getDimension(L"A").Height + font->getKerningHeight();
\r
1546 // only up to 1 line fits?
\r
1547 if ( lineHeight >= (irr::u32)FrameRect.getHeight() )
\r
1550 setTextRect(cursLine);
\r
1551 s32 unscrolledPos = CurrentTextRect.UpperLeftCorner.Y;
\r
1552 s32 pivot = FrameRect.UpperLeftCorner.Y;
\r
1555 case EGUIA_CENTER:
\r
1556 pivot += FrameRect.getHeight()/2;
\r
1557 unscrolledPos += lineHeight/2;
\r
1559 case EGUIA_LOWERRIGHT:
\r
1560 pivot += FrameRect.getHeight();
\r
1561 unscrolledPos += lineHeight;
\r
1566 VScrollPos = unscrolledPos-pivot;
\r
1567 setTextRect(cursLine);
\r
1571 // First 2 checks are necessary when people delete lines
\r
1573 if ( CurrentTextRect.UpperLeftCorner.Y > FrameRect.UpperLeftCorner.Y && VAlign != EGUIA_LOWERRIGHT)
\r
1575 // first line is leaving a gap on top
\r
1578 else if (VAlign != EGUIA_UPPERLEFT)
\r
1580 u32 lastLine = BrokenTextPositions.empty() ? 0 : BrokenTextPositions.size()-1;
\r
1581 setTextRect(lastLine);
\r
1582 if ( CurrentTextRect.LowerRightCorner.Y < FrameRect.LowerRightCorner.Y)
\r
1584 // last line is leaving a gap on bottom
\r
1585 VScrollPos -= FrameRect.LowerRightCorner.Y-CurrentTextRect.LowerRightCorner.Y;
\r
1589 setTextRect(cursLine);
\r
1590 if ( CurrentTextRect.UpperLeftCorner.Y < FrameRect.UpperLeftCorner.Y )
\r
1592 // text above valid area
\r
1593 VScrollPos -= FrameRect.UpperLeftCorner.Y-CurrentTextRect.UpperLeftCorner.Y;
\r
1594 setTextRect(cursLine);
\r
1596 else if ( CurrentTextRect.LowerRightCorner.Y > FrameRect.LowerRightCorner.Y)
\r
1598 // text below valid area
\r
1599 VScrollPos += CurrentTextRect.LowerRightCorner.Y-FrameRect.LowerRightCorner.Y;
\r
1600 setTextRect(cursLine);
\r
1606 void CGUIEditBox::calculateFrameRect()
\r
1608 FrameRect = AbsoluteRect;
\r
1609 IGUISkin *skin = 0;
\r
1611 skin = Environment->getSkin();
\r
1612 if (Border && skin)
\r
1614 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
\r
1615 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
\r
1616 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
\r
1617 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
\r
1621 //! set text markers
\r
1622 void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
\r
1624 if ( begin != MarkBegin || end != MarkEnd )
\r
1626 MarkBegin = begin;
\r
1628 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
\r
1632 //! send some gui event to parent
\r
1633 void CGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
\r
1638 e.EventType = EET_GUI_EVENT;
\r
1639 e.GUIEvent.Caller = this;
\r
1640 e.GUIEvent.Element = 0;
\r
1641 e.GUIEvent.EventType = type;
\r
1643 Parent->OnEvent(e);
\r
1647 //! Returns whether the element takes input from the IME
\r
1648 bool CGUIEditBox::acceptsIME()
\r
1650 return isEnabled();
\r
1653 } // end namespace gui
\r
1654 } // end namespace irr
\r
1656 #endif // _IRR_COMPILE_WITH_GUI_
\r