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
7 #include "IGUISkin.h"
\r
8 #include "IGUIEnvironment.h"
\r
9 #include "IGUIFont.h"
\r
10 #include "IVideoDriver.h"
\r
13 #include "Keycodes.h"
\r
18 ctrl+left/right to select word
\r
19 double click/ctrl click: word select + drag to select whole words, triple click to select line
\r
20 optional? dragging selected text
\r
30 CGUIEditBox::CGUIEditBox(const wchar_t* text, bool border,
\r
31 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
\r
32 const core::rect<s32>& rectangle)
\r
33 : IGUIEditBox(environment, parent, id, rectangle), OverwriteMode(false), MouseMarking(false),
\r
34 Border(border), Background(true), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
\r
35 OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
\r
36 Operator(0), BlinkStartTime(0), CursorBlinkTime(350), CursorChar(L"_"), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
\r
37 WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
\r
38 PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
\r
39 CurrentTextRect(0,0,1,1), FrameRect(rectangle)
\r
42 setDebugName("CGUIEditBox");
\r
48 Operator = Environment->getOSOperator();
\r
53 // this element can be tabbed to
\r
57 calculateFrameRect();
\r
60 calculateScrollPos();
\r
65 CGUIEditBox::~CGUIEditBox()
\r
68 OverrideFont->drop();
\r
75 //! Sets another skin independent font.
\r
76 void CGUIEditBox::setOverrideFont(IGUIFont* font)
\r
78 if (OverrideFont == font)
\r
82 OverrideFont->drop();
\r
84 OverrideFont = font;
\r
87 OverrideFont->grab();
\r
92 //! Gets the override font (if any)
\r
93 IGUIFont * CGUIEditBox::getOverrideFont() const
\r
95 return OverrideFont;
\r
98 //! Get the font which is used right now for drawing
\r
99 IGUIFont* CGUIEditBox::getActiveFont() const
\r
101 if ( OverrideFont )
\r
102 return OverrideFont;
\r
103 IGUISkin* skin = Environment->getSkin();
\r
105 return skin->getFont();
\r
109 //! Sets another color for the text.
\r
110 void CGUIEditBox::setOverrideColor(video::SColor color)
\r
112 OverrideColor = color;
\r
113 OverrideColorEnabled = true;
\r
117 video::SColor CGUIEditBox::getOverrideColor() const
\r
119 return OverrideColor;
\r
123 //! Turns the border on or off
\r
124 void CGUIEditBox::setDrawBorder(bool border)
\r
129 //! Checks if border drawing is enabled
\r
130 bool CGUIEditBox::isDrawBorderEnabled() const
\r
135 //! Sets whether to draw the background
\r
136 void CGUIEditBox::setDrawBackground(bool draw)
\r
141 //! Checks if background drawing is enabled
\r
142 bool CGUIEditBox::isDrawBackgroundEnabled() const
\r
147 //! Sets if the text should use the override color or the color in the gui skin.
\r
148 void CGUIEditBox::enableOverrideColor(bool enable)
\r
150 OverrideColorEnabled = enable;
\r
153 bool CGUIEditBox::isOverrideColorEnabled() const
\r
155 return OverrideColorEnabled;
\r
158 //! Enables or disables word wrap
\r
159 void CGUIEditBox::setWordWrap(bool enable)
\r
166 void CGUIEditBox::updateAbsolutePosition()
\r
168 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
\r
169 IGUIElement::updateAbsolutePosition();
\r
170 if ( oldAbsoluteRect != AbsoluteRect )
\r
172 calculateFrameRect();
\r
174 calculateScrollPos();
\r
179 //! Checks if word wrap is enabled
\r
180 bool CGUIEditBox::isWordWrapEnabled() const
\r
186 //! Enables or disables newlines.
\r
187 void CGUIEditBox::setMultiLine(bool enable)
\r
189 MultiLine = enable;
\r
194 //! Checks if multi line editing is enabled
\r
195 bool CGUIEditBox::isMultiLineEnabled() const
\r
201 void CGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
\r
203 PasswordBox = passwordBox;
\r
206 PasswordChar = passwordChar;
\r
207 setMultiLine(false);
\r
208 setWordWrap(false);
\r
209 BrokenText.clear();
\r
214 bool CGUIEditBox::isPasswordBox() const
\r
216 return PasswordBox;
\r
220 //! Sets text justification
\r
221 void CGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
\r
223 HAlign = horizontal;
\r
228 //! called if an event happened.
\r
229 bool CGUIEditBox::OnEvent(const SEvent& event)
\r
234 switch(event.EventType)
\r
236 case EET_GUI_EVENT:
\r
237 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
\r
239 if (event.GUIEvent.Caller == this)
\r
241 MouseMarking = false;
\r
242 setTextMarkers(0,0);
\r
246 case EET_KEY_INPUT_EVENT:
\r
247 if (processKey(event))
\r
250 case EET_MOUSE_INPUT_EVENT:
\r
251 if (processMouse(event))
\r
254 case EET_STRING_INPUT_EVENT:
\r
255 inputString(*event.StringInput.Str);
\r
263 return IGUIElement::OnEvent(event);
\r
267 bool CGUIEditBox::processKey(const SEvent& event)
\r
269 if (!event.KeyInput.PressedDown)
\r
272 bool textChanged = false;
\r
273 s32 newMarkBegin = MarkBegin;
\r
274 s32 newMarkEnd = MarkEnd;
\r
276 // control shortcut handling
\r
278 if (event.KeyInput.Control)
\r
280 // german backlash '\' entered with control + '?'
\r
281 if ( event.KeyInput.Char == '\\' )
\r
283 inputChar(event.KeyInput.Char);
\r
287 switch(event.KeyInput.Key)
\r
292 newMarkEnd = Text.size();
\r
295 // copy to clipboard
\r
296 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
\r
298 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
299 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
302 wStringToMultibyte(s, Text.subString(realmbgn, realmend - realmbgn));
\r
303 Operator->copyToClipboard(s.c_str());
\r
307 // cut to the clipboard
\r
308 if (!PasswordBox && Operator && MarkBegin != MarkEnd)
\r
310 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
311 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
315 wStringToMultibyte(sc, Text.subString(realmbgn, realmend - realmbgn));
\r
316 Operator->copyToClipboard(sc.c_str());
\r
322 s = Text.subString(0, realmbgn);
\r
323 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
326 CursorPos = realmbgn;
\r
329 textChanged = true;
\r
334 if ( !isEnabled() )
\r
337 // paste from the clipboard
\r
340 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
341 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
344 const c8 *p = Operator->getTextFromClipboard();
\r
347 irr::core::stringw widep;
\r
348 core::multibyteToWString(widep, p);
\r
350 if (MarkBegin == MarkEnd)
\r
353 core::stringw s = Text.subString(0, CursorPos);
\r
355 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
357 if (!Max || s.size()<=Max) // thx to Fish FH for fix
\r
361 CursorPos += s.size();
\r
368 core::stringw s = Text.subString(0, realmbgn);
\r
370 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
372 if (!Max || s.size()<=Max) // thx to Fish FH for fix
\r
376 CursorPos = realmbgn + s.size();
\r
383 textChanged = true;
\r
387 // move/highlight to start of text
\r
388 if (event.KeyInput.Shift)
\r
390 newMarkEnd = CursorPos;
\r
402 // move/highlight to end of text
\r
403 if (event.KeyInput.Shift)
\r
405 newMarkBegin = CursorPos;
\r
406 newMarkEnd = Text.size();
\r
411 CursorPos = Text.size();
\r
420 // 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
421 else if (event.KeyInput.Char == 0)
\r
423 switch(event.KeyInput.Key)
\r
427 s32 p = Text.size();
\r
428 if (WordWrap || MultiLine)
\r
430 p = getLineFromPos(CursorPos);
\r
431 p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
\r
432 if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
\r
436 if (event.KeyInput.Shift)
\r
438 if (MarkBegin == MarkEnd)
\r
439 newMarkBegin = CursorPos;
\r
449 BlinkStartTime = os::Timer::getTime();
\r
456 if (WordWrap || MultiLine)
\r
458 p = getLineFromPos(CursorPos);
\r
459 p = BrokenTextPositions[p];
\r
462 if (event.KeyInput.Shift)
\r
464 if (MarkBegin == MarkEnd)
\r
465 newMarkBegin = CursorPos;
\r
474 BlinkStartTime = os::Timer::getTime();
\r
479 if (event.KeyInput.Shift)
\r
483 if (MarkBegin == MarkEnd)
\r
484 newMarkBegin = CursorPos;
\r
486 newMarkEnd = CursorPos-1;
\r
495 if (CursorPos > 0) CursorPos--;
\r
496 BlinkStartTime = os::Timer::getTime();
\r
500 if (event.KeyInput.Shift)
\r
502 if (Text.size() > (u32)CursorPos)
\r
504 if (MarkBegin == MarkEnd)
\r
505 newMarkBegin = CursorPos;
\r
507 newMarkEnd = CursorPos+1;
\r
516 if (Text.size() > (u32)CursorPos) CursorPos++;
\r
517 BlinkStartTime = os::Timer::getTime();
\r
520 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
\r
522 s32 lineNo = getLineFromPos(CursorPos);
\r
523 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
\r
526 s32 cp = CursorPos - BrokenTextPositions[lineNo];
\r
527 if ((s32)BrokenText[lineNo-1].size() < cp)
\r
528 CursorPos = BrokenTextPositions[lineNo-1] + core::max_((u32)1, BrokenText[lineNo-1].size())-1;
\r
530 CursorPos = BrokenTextPositions[lineNo-1] + cp;
\r
533 if (event.KeyInput.Shift)
\r
536 newMarkEnd = CursorPos;
\r
551 if (MultiLine || (WordWrap && BrokenText.size() > 1) )
\r
553 s32 lineNo = getLineFromPos(CursorPos);
\r
554 s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
\r
555 if (lineNo < (s32)BrokenText.size()-1)
\r
557 s32 cp = CursorPos - BrokenTextPositions[lineNo];
\r
558 if ((s32)BrokenText[lineNo+1].size() < cp)
\r
559 CursorPos = BrokenTextPositions[lineNo+1] + core::max_((u32)1, BrokenText[lineNo+1].size())-1;
\r
561 CursorPos = BrokenTextPositions[lineNo+1] + cp;
\r
564 if (event.KeyInput.Shift)
\r
567 newMarkEnd = CursorPos;
\r
582 if ( !isEnabled() )
\r
585 OverwriteMode = !OverwriteMode;
\r
588 if ( !isEnabled() )
\r
593 BlinkStartTime = os::Timer::getTime();
\r
596 textChanged = true;
\r
605 // default keyboard handling
\r
606 switch(event.KeyInput.Key)
\r
615 calculateScrollPos();
\r
616 sendGuiEvent( EGET_EDITBOX_ENTER );
\r
621 if ( !isEnabled() )
\r
628 if (MarkBegin != MarkEnd)
\r
630 // delete marked text
\r
631 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
632 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
634 s = Text.subString(0, realmbgn);
\r
635 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
638 CursorPos = realmbgn;
\r
642 // delete text behind cursor
\r
644 s = Text.subString(0, CursorPos-1);
\r
647 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
654 BlinkStartTime = os::Timer::getTime();
\r
657 textChanged = true;
\r
663 // At least on X11 we get a char with 127 when the delete key is pressed.
\r
664 // 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
665 // We get a keykode != 127 when delete key on numlock is pressed with numlock on.
\r
666 if (event.KeyInput.Char == 127)
\r
668 if ( !isEnabled() )
\r
673 BlinkStartTime = os::Timer::getTime();
\r
676 textChanged = true;
\r
682 inputChar(event.KeyInput.Char);
\r
713 // ignore these keys
\r
717 inputChar(event.KeyInput.Char);
\r
722 // Set new text markers
\r
723 setTextMarkers( newMarkBegin, newMarkEnd );
\r
725 // break the text if it has changed
\r
729 calculateScrollPos();
\r
730 sendGuiEvent(EGET_EDITBOX_CHANGED);
\r
734 calculateScrollPos();
\r
740 bool CGUIEditBox::keyDelete()
\r
742 if (Text.size() != 0)
\r
746 if (MarkBegin != MarkEnd)
\r
748 // delete marked text
\r
749 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
750 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
752 s = Text.subString(0, realmbgn);
\r
753 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
756 CursorPos = realmbgn;
\r
760 // delete text before cursor
\r
761 s = Text.subString(0, CursorPos);
\r
762 s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
\r
766 if (CursorPos > (s32)Text.size())
\r
767 CursorPos = (s32)Text.size();
\r
775 //! draws the element and its children
\r
776 void CGUIEditBox::draw()
\r
781 const bool focus = Environment->hasFocus(this);
\r
783 IGUISkin* skin = Environment->getSkin();
\r
787 EGUI_DEFAULT_COLOR bgCol = EGDC_GRAY_EDITABLE;
\r
789 bgCol = focus ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;
\r
791 if (!Border && Background)
\r
793 skin->draw2DRectangle(this, skin->getColor(bgCol), AbsoluteRect, &AbsoluteClippingRect);
\r
799 skin->draw3DSunkenPane(this, skin->getColor(bgCol), false, Background, AbsoluteRect, &AbsoluteClippingRect);
\r
801 calculateFrameRect();
\r
804 core::rect<s32> localClipRect = FrameRect;
\r
805 localClipRect.clipAgainst(AbsoluteClippingRect);
\r
809 IGUIFont* font = getActiveFont();
\r
811 s32 cursorLine = 0;
\r
812 s32 charcursorpos = 0;
\r
816 if (LastBreakFont != font)
\r
821 // calculate cursor pos
\r
823 core::stringw *txtLine = &Text;
\r
826 core::stringw s, s2;
\r
828 // get mark position
\r
829 const bool ml = (!PasswordBox && (WordWrap || MultiLine));
\r
830 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
831 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
832 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
\r
833 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
\r
834 const s32 lineCount = ml ? BrokenText.size() : 1;
\r
836 // Save the override color information.
\r
837 // Then, alter it if the edit box is disabled.
\r
838 const bool prevOver = OverrideColorEnabled;
\r
839 const video::SColor prevColor = OverrideColor;
\r
843 if (!isEnabled() && !OverrideColorEnabled)
\r
845 OverrideColorEnabled = true;
\r
846 OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
\r
849 for (s32 i=0; i < lineCount; ++i)
\r
853 // clipping test - don't draw anything outside the visible area
\r
854 core::rect<s32> c = localClipRect;
\r
855 c.clipAgainst(CurrentTextRect);
\r
859 // get current line
\r
862 if (BrokenText.size() != 1)
\r
864 BrokenText.clear();
\r
865 BrokenText.push_back(core::stringw());
\r
867 if (BrokenText[0].size() != Text.size())
\r
869 BrokenText[0] = Text;
\r
870 for (u32 q = 0; q < Text.size(); ++q)
\r
872 BrokenText[0] [q] = PasswordChar;
\r
875 txtLine = &BrokenText[0];
\r
880 txtLine = ml ? &BrokenText[i] : &Text;
\r
881 startPos = ml ? BrokenTextPositions[i] : 0;
\r
885 // draw normal text
\r
886 font->draw(txtLine->c_str(), CurrentTextRect,
\r
887 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
\r
888 false, true, &localClipRect);
\r
890 // draw mark and marked text
\r
891 if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
\r
894 s32 mbegin = 0, mend = 0;
\r
895 s32 lineStartPos = 0, lineEndPos = txtLine->size();
\r
897 if (i == hlineStart)
\r
899 // highlight start is on this line
\r
900 s = txtLine->subString(0, realmbgn - startPos);
\r
901 mbegin = font->getDimension(s.c_str()).Width;
\r
903 // deal with kerning
\r
904 mbegin += font->getKerningWidth(
\r
905 &((*txtLine)[realmbgn - startPos]),
\r
906 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
\r
908 lineStartPos = realmbgn - startPos;
\r
910 if (i == hlineStart + hlineCount - 1)
\r
912 // highlight end is on this line
\r
913 s2 = txtLine->subString(0, realmend - startPos);
\r
914 mend = font->getDimension(s2.c_str()).Width;
\r
915 lineEndPos = (s32)s2.size();
\r
918 mend = font->getDimension(txtLine->c_str()).Width;
\r
920 CurrentTextRect.UpperLeftCorner.X += mbegin;
\r
921 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
\r
924 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
\r
926 // draw marked text
\r
927 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
\r
930 font->draw(s.c_str(), CurrentTextRect,
\r
931 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
\r
932 false, true, &localClipRect);
\r
937 // Return the override color information to its previous settings.
\r
938 OverrideColorEnabled = prevOver;
\r
939 OverrideColor = prevColor;
\r
945 if (WordWrap || MultiLine)
\r
947 cursorLine = getLineFromPos(CursorPos);
\r
948 txtLine = &BrokenText[cursorLine];
\r
949 startPos = BrokenTextPositions[cursorLine];
\r
951 s = txtLine->subString(0,CursorPos-startPos);
\r
952 charcursorpos = font->getDimension(s.c_str()).Width +
\r
953 font->getKerningWidth(CursorChar.c_str(), CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
\r
955 if (focus && (CursorBlinkTime == 0 || (os::Timer::getTime() - BlinkStartTime) % (2*CursorBlinkTime) < CursorBlinkTime))
\r
957 setTextRect(cursorLine);
\r
958 CurrentTextRect.UpperLeftCorner.X += charcursorpos;
\r
960 if ( OverwriteMode )
\r
962 core::stringw character = Text.subString(CursorPos,1);
\r
963 s32 mend = font->getDimension(character.c_str()).Width;
\r
964 //Make sure the cursor box has at least some width to it
\r
966 mend = font->getDimension(CursorChar.c_str()).Width;
\r
967 CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend;
\r
968 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
\r
969 font->draw(character.c_str(), CurrentTextRect,
\r
970 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
\r
971 false, true, &localClipRect);
\r
975 font->draw(CursorChar, CurrentTextRect,
\r
976 OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
\r
977 false, true, &localClipRect);
\r
984 IGUIElement::draw();
\r
988 //! Sets the new caption of this element.
\r
989 void CGUIEditBox::setText(const wchar_t* text)
\r
992 if (u32(CursorPos) > Text.size())
\r
993 CursorPos = Text.size();
\r
999 //! Enables or disables automatic scrolling with cursor position
\r
1000 //! \param enable: If set to true, the text will move around with the cursor position
\r
1001 void CGUIEditBox::setAutoScroll(bool enable)
\r
1003 AutoScroll = enable;
\r
1007 //! Checks to see if automatic scrolling is enabled
\r
1008 //! \return true if automatic scrolling is enabled, false if not
\r
1009 bool CGUIEditBox::isAutoScrollEnabled() const
\r
1011 return AutoScroll;
\r
1015 //! Gets the area of the text in the edit box
\r
1016 //! \return Returns the size in pixels of the text
\r
1017 core::dimension2du CGUIEditBox::getTextDimension()
\r
1019 core::rect<s32> ret;
\r
1022 ret = CurrentTextRect;
\r
1024 for (u32 i=1; i < BrokenText.size(); ++i)
\r
1027 ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
\r
1028 ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
\r
1031 return core::dimension2du(ret.getSize());
\r
1035 //! Sets the maximum amount of characters which may be entered in the box.
\r
1036 //! \param max: Maximum amount of characters. If 0, the character amount is
\r
1038 void CGUIEditBox::setMax(u32 max)
\r
1042 if (Text.size() > Max && Max != 0)
\r
1043 Text = Text.subString(0, Max);
\r
1047 //! Returns maximum amount of characters, previously set by setMax();
\r
1048 u32 CGUIEditBox::getMax() const
\r
1053 //! Set the character used for the cursor.
\r
1054 /** By default it's "_" */
\r
1055 void CGUIEditBox::setCursorChar(const wchar_t cursorChar)
\r
1057 CursorChar[0] = cursorChar;
\r
1060 //! Get the character used for the cursor.
\r
1061 wchar_t CGUIEditBox::getCursorChar() const
\r
1063 return CursorChar[0];
\r
1066 //! Set the blinktime for the cursor. 2x blinktime is one full cycle.
\r
1067 void CGUIEditBox::setCursorBlinkTime(irr::u32 timeMs)
\r
1069 CursorBlinkTime = timeMs;
\r
1072 //! Get the cursor blinktime
\r
1073 irr::u32 CGUIEditBox::getCursorBlinkTime() const
\r
1075 return CursorBlinkTime;
\r
1078 bool CGUIEditBox::processMouse(const SEvent& event)
\r
1080 switch(event.MouseInput.Event)
\r
1082 case irr::EMIE_LMOUSE_LEFT_UP:
\r
1083 if (Environment->hasFocus(this))
\r
1085 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1088 setTextMarkers( MarkBegin, CursorPos );
\r
1090 MouseMarking = false;
\r
1091 calculateScrollPos();
\r
1095 case irr::EMIE_MOUSE_MOVED:
\r
1099 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1100 setTextMarkers( MarkBegin, CursorPos );
\r
1101 calculateScrollPos();
\r
1106 case EMIE_LMOUSE_PRESSED_DOWN:
\r
1107 if (!Environment->hasFocus(this)) // can happen when events are manually send to the element
\r
1109 BlinkStartTime = os::Timer::getTime();
\r
1110 MouseMarking = true;
\r
1111 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1112 setTextMarkers(CursorPos, CursorPos );
\r
1113 calculateScrollPos();
\r
1118 if (!AbsoluteClippingRect.isPointInside(
\r
1119 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
\r
1126 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1128 s32 newMarkBegin = MarkBegin;
\r
1129 if (!MouseMarking)
\r
1130 newMarkBegin = CursorPos;
\r
1132 MouseMarking = true;
\r
1133 setTextMarkers( newMarkBegin, CursorPos);
\r
1134 calculateScrollPos();
\r
1138 case EMIE_MMOUSE_PRESSED_DOWN: {
\r
1139 if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(
\r
1140 event.MouseInput.X, event.MouseInput.Y)))
\r
1143 if (!Environment->hasFocus(this)) {
\r
1144 BlinkStartTime = os::Timer::getTime();
\r
1147 // move cursor and disable marking
\r
1148 CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
\r
1149 MouseMarking = false;
\r
1150 setTextMarkers(CursorPos, CursorPos);
\r
1152 // paste from the primary selection
\r
1154 irr::core::stringw inserted_text;
\r
1156 return inserted_text;
\r
1157 const c8 *inserted_text_utf8 = Operator->getTextFromPrimarySelection();
\r
1158 if (!inserted_text_utf8)
\r
1159 return inserted_text;
\r
1160 core::multibyteToWString(inserted_text, inserted_text_utf8);
\r
1161 return inserted_text;
\r
1174 s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
\r
1176 IGUIFont* font = getActiveFont();
\r
1178 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
\r
1180 core::stringw *txtLine=0;
\r
1184 for (u32 i=0; i < lineCount; ++i)
\r
1187 if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
\r
1188 y = CurrentTextRect.UpperLeftCorner.Y;
\r
1189 if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
\r
1190 y = CurrentTextRect.LowerRightCorner.Y;
\r
1192 // is it inside this region?
\r
1193 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
\r
1195 // we've found the clicked line
\r
1196 txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
\r
1197 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
\r
1202 if (x < CurrentTextRect.UpperLeftCorner.X)
\r
1203 x = CurrentTextRect.UpperLeftCorner.X;
\r
1208 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
\r
1210 // click was on or left of the line
\r
1212 return idx + startPos;
\r
1214 // click was off the right edge of the line, go to end.
\r
1215 return txtLine->size() + startPos;
\r
1219 //! Breaks the single text line.
\r
1220 void CGUIEditBox::breakText()
\r
1222 if ((!WordWrap && !MultiLine))
\r
1225 BrokenText.clear(); // need to reallocate :/
\r
1226 BrokenTextPositions.set_used(0);
\r
1228 IGUIFont* font = getActiveFont();
\r
1232 LastBreakFont = font;
\r
1234 core::stringw line;
\r
1235 core::stringw word;
\r
1236 core::stringw whitespace;
\r
1237 s32 lastLineStart = 0;
\r
1238 s32 size = Text.size();
\r
1240 s32 elWidth = RelativeRect.getWidth() - 6;
\r
1243 for (s32 i=0; i<size; ++i)
\r
1246 bool lineBreak = false;
\r
1248 if (c == L'\r') // Mac or Windows breaks
\r
1252 if (Text[i+1] == L'\n') // Windows breaks
\r
1254 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
\r
1255 // Instead rework the cursor positioning to be able to handle this (but not in stable release
\r
1256 // branch as users might already expect this behavior).
\r
1259 if ( CursorPos > i )
\r
1263 else if (c == L'\n') // Unix breaks
\r
1269 // don't break if we're not a multi-line edit box
\r
1271 lineBreak = false;
\r
1273 if (c == L' ' || c == 0 || i == (size-1))
\r
1275 // here comes the next whitespace, look if
\r
1276 // we can break the last word to the next line
\r
1277 // We also break whitespace, otherwise cursor would vanish beside the right border.
\r
1278 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
1279 s32 worldlgth = font->getDimension(word.c_str()).Width;
\r
1281 if (WordWrap && length + worldlgth + whitelgth > elWidth && line.size() > 0)
\r
1283 // break to next line
\r
1284 length = worldlgth;
\r
1285 BrokenText.push_back(line);
\r
1286 BrokenTextPositions.push_back(lastLineStart);
\r
1287 lastLineStart = i - (s32)word.size();
\r
1292 // add word to line
\r
1293 line += whitespace;
\r
1295 length += whitelgth + worldlgth;
\r
1305 // compute line break
\r
1308 line += whitespace;
\r
1310 BrokenText.push_back(line);
\r
1311 BrokenTextPositions.push_back(lastLineStart);
\r
1312 lastLineStart = i+1;
\r
1321 // yippee this is a word..
\r
1326 line += whitespace;
\r
1328 BrokenText.push_back(line);
\r
1329 BrokenTextPositions.push_back(lastLineStart);
\r
1332 // TODO: that function does interpret VAlign according to line-index (indexed line is placed on top-center-bottom)
\r
1333 // but HAlign according to line-width (pixels) and not by row.
\r
1334 // Intuitively I suppose HAlign handling is better as VScrollPos should handle the line-scrolling.
\r
1335 // But please no one change this without also rewriting (and this time fucking testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
\r
1336 void CGUIEditBox::setTextRect(s32 line)
\r
1341 IGUIFont* font = getActiveFont();
\r
1345 core::dimension2du d;
\r
1347 // get text dimension
\r
1348 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
\r
1349 if (WordWrap || MultiLine)
\r
1351 d = font->getDimension(BrokenText[line].c_str());
\r
1355 d = font->getDimension(Text.c_str());
\r
1356 d.Height = AbsoluteRect.getHeight();
\r
1358 d.Height += font->getKerningHeight();
\r
1363 case EGUIA_CENTER:
\r
1364 // align to h centre
\r
1365 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
\r
1366 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
\r
1368 case EGUIA_LOWERRIGHT:
\r
1369 // align to right edge
\r
1370 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
\r
1371 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
\r
1374 // align to left edge
\r
1375 CurrentTextRect.UpperLeftCorner.X = 0;
\r
1376 CurrentTextRect.LowerRightCorner.X = d.Width;
\r
1382 case EGUIA_CENTER:
\r
1383 // align to v centre
\r
1384 CurrentTextRect.UpperLeftCorner.Y =
\r
1385 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
\r
1387 case EGUIA_LOWERRIGHT:
\r
1388 // align to bottom edge
\r
1389 CurrentTextRect.UpperLeftCorner.Y =
\r
1390 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
\r
1393 // align to top edge
\r
1394 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
\r
1398 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
\r
1399 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
\r
1400 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
\r
1401 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
\r
1403 CurrentTextRect += FrameRect.UpperLeftCorner;
\r
1408 s32 CGUIEditBox::getLineFromPos(s32 pos)
\r
1410 if (!WordWrap && !MultiLine)
\r
1414 while (i < (s32)BrokenTextPositions.size())
\r
1416 if (BrokenTextPositions[i] > pos)
\r
1420 return (s32)BrokenTextPositions.size() - 1;
\r
1424 void CGUIEditBox::inputChar(wchar_t c)
\r
1428 core::stringw s(&c, 1);
\r
1432 void CGUIEditBox::inputString(const core::stringw &str)
\r
1438 u32 len = str.size();
\r
1440 if (MarkBegin != MarkEnd)
\r
1442 // replace marked text
\r
1443 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
1444 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
1446 s = Text.subString(0, realmbgn);
\r
1448 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
1450 CursorPos = realmbgn+len;
\r
1452 else if ( OverwriteMode )
\r
1454 //check to see if we are at the end of the text
\r
1455 if ( (u32)CursorPos+len < Text.size())
\r
1457 bool isEOL = false;
\r
1459 for (u32 i = CursorPos; i < CursorPos+len && i < Max; i++)
\r
1461 if (Text[i] == L'\n' || Text[i] == L'\r')
\r
1468 if (!isEOL || Text.size()+len <= Max || Max == 0)
\r
1470 s = Text.subString(0, CursorPos);
\r
1474 //just keep appending to the current line
\r
1475 //This follows the behavior of other gui libraries behaviors
\r
1476 s.append( Text.subString(EOLPos, Text.size()-EOLPos) );
\r
1480 //replace the next character
\r
1481 s.append( Text.subString(CursorPos + len,Text.size() - CursorPos - len));
\r
1487 else if (Text.size()+len <= Max || Max == 0)
\r
1489 // add new character because we are at the end of the string
\r
1490 s = Text.subString(0, CursorPos);
\r
1492 s.append( Text.subString(CursorPos+len, Text.size()-CursorPos-len) );
\r
1497 else if (Text.size()+len <= Max || Max == 0)
\r
1499 // add new character
\r
1500 s = Text.subString(0, CursorPos);
\r
1502 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
1507 BlinkStartTime = os::Timer::getTime();
\r
1508 setTextMarkers(0, 0);
\r
1511 calculateScrollPos();
\r
1512 sendGuiEvent(EGET_EDITBOX_CHANGED);
\r
1515 // calculate autoscroll
\r
1516 void CGUIEditBox::calculateScrollPos()
\r
1521 IGUIFont* font = getActiveFont();
\r
1525 s32 cursLine = getLineFromPos(CursorPos);
\r
1526 if ( cursLine < 0 )
\r
1528 setTextRect(cursLine);
\r
1529 const bool hasBrokenText = MultiLine || WordWrap;
\r
1531 // Check horizonal scrolling
\r
1532 // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
\r
1534 // get cursor position
\r
1535 // get cursor area
\r
1536 irr::u32 cursorWidth = font->getDimension(CursorChar.c_str()).Width;
\r
1537 core::stringw *txtLine = hasBrokenText ? &BrokenText[cursLine] : &Text;
\r
1538 s32 cPos = hasBrokenText ? CursorPos - BrokenTextPositions[cursLine] : CursorPos; // column
\r
1539 s32 cStart = font->getDimension(txtLine->subString(0, cPos).c_str()).Width; // pixels from text-start
\r
1540 s32 cEnd = cStart + cursorWidth;
\r
1541 s32 txtWidth = font->getDimension(txtLine->c_str()).Width;
\r
1543 if ( txtWidth < FrameRect.getWidth() )
\r
1545 // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
\r
1546 // This check just fixes the case where it was most noticable (text smaller than clipping area).
\r
1549 setTextRect(cursLine);
\r
1552 if ( CurrentTextRect.UpperLeftCorner.X+cStart < FrameRect.UpperLeftCorner.X )
\r
1554 // cursor to the left of the clipping area
\r
1555 HScrollPos -= FrameRect.UpperLeftCorner.X-(CurrentTextRect.UpperLeftCorner.X+cStart);
\r
1556 setTextRect(cursLine);
\r
1558 // TODO: should show more characters to the left when we're scrolling left
\r
1559 // and the cursor reaches the border.
\r
1561 else if ( CurrentTextRect.UpperLeftCorner.X+cEnd > FrameRect.LowerRightCorner.X)
\r
1563 // cursor to the right of the clipping area
\r
1564 HScrollPos += (CurrentTextRect.UpperLeftCorner.X+cEnd)-FrameRect.LowerRightCorner.X;
\r
1565 setTextRect(cursLine);
\r
1569 // calculate vertical scrolling
\r
1570 if (hasBrokenText)
\r
1572 irr::u32 lineHeight = font->getDimension(L"A").Height + font->getKerningHeight();
\r
1573 // only up to 1 line fits?
\r
1574 if ( lineHeight >= (irr::u32)FrameRect.getHeight() )
\r
1577 setTextRect(cursLine);
\r
1578 s32 unscrolledPos = CurrentTextRect.UpperLeftCorner.Y;
\r
1579 s32 pivot = FrameRect.UpperLeftCorner.Y;
\r
1582 case EGUIA_CENTER:
\r
1583 pivot += FrameRect.getHeight()/2;
\r
1584 unscrolledPos += lineHeight/2;
\r
1586 case EGUIA_LOWERRIGHT:
\r
1587 pivot += FrameRect.getHeight();
\r
1588 unscrolledPos += lineHeight;
\r
1593 VScrollPos = unscrolledPos-pivot;
\r
1594 setTextRect(cursLine);
\r
1598 // First 2 checks are necessary when people delete lines
\r
1600 if ( CurrentTextRect.UpperLeftCorner.Y > FrameRect.UpperLeftCorner.Y && VAlign != EGUIA_LOWERRIGHT)
\r
1602 // first line is leaving a gap on top
\r
1605 else if (VAlign != EGUIA_UPPERLEFT)
\r
1607 u32 lastLine = BrokenTextPositions.empty() ? 0 : BrokenTextPositions.size()-1;
\r
1608 setTextRect(lastLine);
\r
1609 if ( CurrentTextRect.LowerRightCorner.Y < FrameRect.LowerRightCorner.Y)
\r
1611 // last line is leaving a gap on bottom
\r
1612 VScrollPos -= FrameRect.LowerRightCorner.Y-CurrentTextRect.LowerRightCorner.Y;
\r
1616 setTextRect(cursLine);
\r
1617 if ( CurrentTextRect.UpperLeftCorner.Y < FrameRect.UpperLeftCorner.Y )
\r
1619 // text above valid area
\r
1620 VScrollPos -= FrameRect.UpperLeftCorner.Y-CurrentTextRect.UpperLeftCorner.Y;
\r
1621 setTextRect(cursLine);
\r
1623 else if ( CurrentTextRect.LowerRightCorner.Y > FrameRect.LowerRightCorner.Y)
\r
1625 // text below valid area
\r
1626 VScrollPos += CurrentTextRect.LowerRightCorner.Y-FrameRect.LowerRightCorner.Y;
\r
1627 setTextRect(cursLine);
\r
1633 void CGUIEditBox::calculateFrameRect()
\r
1635 FrameRect = AbsoluteRect;
\r
1636 IGUISkin *skin = 0;
\r
1638 skin = Environment->getSkin();
\r
1639 if (Border && skin)
\r
1641 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
\r
1642 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
\r
1643 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
\r
1644 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
\r
1648 //! set text markers
\r
1649 void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
\r
1651 if ( begin != MarkBegin || end != MarkEnd )
\r
1653 MarkBegin = begin;
\r
1656 if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
\r
1657 // copy to primary selection
\r
1658 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
1659 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
1662 wStringToMultibyte(s, Text.subString(realmbgn, realmend - realmbgn));
\r
1663 Operator->copyToPrimarySelection(s.c_str());
\r
1666 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
\r
1670 //! send some gui event to parent
\r
1671 void CGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
\r
1676 e.EventType = EET_GUI_EVENT;
\r
1677 e.GUIEvent.Caller = this;
\r
1678 e.GUIEvent.Element = 0;
\r
1679 e.GUIEvent.EventType = type;
\r
1681 Parent->OnEvent(e);
\r
1685 //! Returns whether the element takes input from the IME
\r
1686 bool CGUIEditBox::acceptsIME()
\r
1688 return isEnabled();
\r
1691 } // end namespace gui
\r
1692 } // end namespace irr
\r