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
1146 s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
\r
1148 IGUIFont* font = getActiveFont();
\r
1150 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
\r
1152 core::stringw *txtLine=0;
\r
1156 for (u32 i=0; i < lineCount; ++i)
\r
1159 if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
\r
1160 y = CurrentTextRect.UpperLeftCorner.Y;
\r
1161 if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
\r
1162 y = CurrentTextRect.LowerRightCorner.Y;
\r
1164 // is it inside this region?
\r
1165 if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
\r
1167 // we've found the clicked line
\r
1168 txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
\r
1169 startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
\r
1174 if (x < CurrentTextRect.UpperLeftCorner.X)
\r
1175 x = CurrentTextRect.UpperLeftCorner.X;
\r
1180 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
\r
1182 // click was on or left of the line
\r
1184 return idx + startPos;
\r
1186 // click was off the right edge of the line, go to end.
\r
1187 return txtLine->size() + startPos;
\r
1191 //! Breaks the single text line.
\r
1192 void CGUIEditBox::breakText()
\r
1194 if ((!WordWrap && !MultiLine))
\r
1197 BrokenText.clear(); // need to reallocate :/
\r
1198 BrokenTextPositions.set_used(0);
\r
1200 IGUIFont* font = getActiveFont();
\r
1204 LastBreakFont = font;
\r
1206 core::stringw line;
\r
1207 core::stringw word;
\r
1208 core::stringw whitespace;
\r
1209 s32 lastLineStart = 0;
\r
1210 s32 size = Text.size();
\r
1212 s32 elWidth = RelativeRect.getWidth() - 6;
\r
1215 for (s32 i=0; i<size; ++i)
\r
1218 bool lineBreak = false;
\r
1220 if (c == L'\r') // Mac or Windows breaks
\r
1224 if (Text[i+1] == L'\n') // Windows breaks
\r
1226 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
\r
1227 // Instead rework the cursor positioning to be able to handle this (but not in stable release
\r
1228 // branch as users might already expect this behavior).
\r
1231 if ( CursorPos > i )
\r
1235 else if (c == L'\n') // Unix breaks
\r
1241 // don't break if we're not a multi-line edit box
\r
1243 lineBreak = false;
\r
1245 if (c == L' ' || c == 0 || i == (size-1))
\r
1247 // here comes the next whitespace, look if
\r
1248 // we can break the last word to the next line
\r
1249 // We also break whitespace, otherwise cursor would vanish beside the right border.
\r
1250 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
1251 s32 worldlgth = font->getDimension(word.c_str()).Width;
\r
1253 if (WordWrap && length + worldlgth + whitelgth > elWidth && line.size() > 0)
\r
1255 // break to next line
\r
1256 length = worldlgth;
\r
1257 BrokenText.push_back(line);
\r
1258 BrokenTextPositions.push_back(lastLineStart);
\r
1259 lastLineStart = i - (s32)word.size();
\r
1264 // add word to line
\r
1265 line += whitespace;
\r
1267 length += whitelgth + worldlgth;
\r
1277 // compute line break
\r
1280 line += whitespace;
\r
1282 BrokenText.push_back(line);
\r
1283 BrokenTextPositions.push_back(lastLineStart);
\r
1284 lastLineStart = i+1;
\r
1293 // yippee this is a word..
\r
1298 line += whitespace;
\r
1300 BrokenText.push_back(line);
\r
1301 BrokenTextPositions.push_back(lastLineStart);
\r
1304 // TODO: that function does interpret VAlign according to line-index (indexed line is placed on top-center-bottom)
\r
1305 // but HAlign according to line-width (pixels) and not by row.
\r
1306 // Intuitively I suppose HAlign handling is better as VScrollPos should handle the line-scrolling.
\r
1307 // But please no one change this without also rewriting (and this time fucking testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
\r
1308 void CGUIEditBox::setTextRect(s32 line)
\r
1313 IGUIFont* font = getActiveFont();
\r
1317 core::dimension2du d;
\r
1319 // get text dimension
\r
1320 const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
\r
1321 if (WordWrap || MultiLine)
\r
1323 d = font->getDimension(BrokenText[line].c_str());
\r
1327 d = font->getDimension(Text.c_str());
\r
1328 d.Height = AbsoluteRect.getHeight();
\r
1330 d.Height += font->getKerningHeight();
\r
1335 case EGUIA_CENTER:
\r
1336 // align to h centre
\r
1337 CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
\r
1338 CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
\r
1340 case EGUIA_LOWERRIGHT:
\r
1341 // align to right edge
\r
1342 CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
\r
1343 CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
\r
1346 // align to left edge
\r
1347 CurrentTextRect.UpperLeftCorner.X = 0;
\r
1348 CurrentTextRect.LowerRightCorner.X = d.Width;
\r
1354 case EGUIA_CENTER:
\r
1355 // align to v centre
\r
1356 CurrentTextRect.UpperLeftCorner.Y =
\r
1357 (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
\r
1359 case EGUIA_LOWERRIGHT:
\r
1360 // align to bottom edge
\r
1361 CurrentTextRect.UpperLeftCorner.Y =
\r
1362 FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
\r
1365 // align to top edge
\r
1366 CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
\r
1370 CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
\r
1371 CurrentTextRect.LowerRightCorner.X -= HScrollPos;
\r
1372 CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
\r
1373 CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
\r
1375 CurrentTextRect += FrameRect.UpperLeftCorner;
\r
1380 s32 CGUIEditBox::getLineFromPos(s32 pos)
\r
1382 if (!WordWrap && !MultiLine)
\r
1386 while (i < (s32)BrokenTextPositions.size())
\r
1388 if (BrokenTextPositions[i] > pos)
\r
1392 return (s32)BrokenTextPositions.size() - 1;
\r
1396 void CGUIEditBox::inputChar(wchar_t c)
\r
1400 core::stringw s(&c, 1);
\r
1404 void CGUIEditBox::inputString(const core::stringw &str)
\r
1410 u32 len = str.size();
\r
1412 if (MarkBegin != MarkEnd)
\r
1414 // replace marked text
\r
1415 const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
\r
1416 const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
\r
1418 s = Text.subString(0, realmbgn);
\r
1420 s.append( Text.subString(realmend, Text.size()-realmend) );
\r
1422 CursorPos = realmbgn+len;
\r
1424 else if ( OverwriteMode )
\r
1426 //check to see if we are at the end of the text
\r
1427 if ( (u32)CursorPos+len < Text.size())
\r
1429 bool isEOL = false;
\r
1431 for (u32 i = CursorPos; i < CursorPos+len && i < Max; i++)
\r
1433 if (Text[i] == L'\n' || Text[i] == L'\r')
\r
1440 if (!isEOL || Text.size()+len <= Max || Max == 0)
\r
1442 s = Text.subString(0, CursorPos);
\r
1446 //just keep appending to the current line
\r
1447 //This follows the behavior of other gui libraries behaviors
\r
1448 s.append( Text.subString(EOLPos, Text.size()-EOLPos) );
\r
1452 //replace the next character
\r
1453 s.append( Text.subString(CursorPos + len,Text.size() - CursorPos - len));
\r
1459 else if (Text.size()+len <= Max || Max == 0)
\r
1461 // add new character because we are at the end of the string
\r
1462 s = Text.subString(0, CursorPos);
\r
1464 s.append( Text.subString(CursorPos+len, Text.size()-CursorPos-len) );
\r
1469 else if (Text.size()+len <= Max || Max == 0)
\r
1471 // add new character
\r
1472 s = Text.subString(0, CursorPos);
\r
1474 s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
\r
1479 BlinkStartTime = os::Timer::getTime();
\r
1480 setTextMarkers(0, 0);
\r
1483 calculateScrollPos();
\r
1484 sendGuiEvent(EGET_EDITBOX_CHANGED);
\r
1487 // calculate autoscroll
\r
1488 void CGUIEditBox::calculateScrollPos()
\r
1493 IGUIFont* font = getActiveFont();
\r
1497 s32 cursLine = getLineFromPos(CursorPos);
\r
1498 if ( cursLine < 0 )
\r
1500 setTextRect(cursLine);
\r
1501 const bool hasBrokenText = MultiLine || WordWrap;
\r
1503 // Check horizonal scrolling
\r
1504 // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
\r
1506 // get cursor position
\r
1507 // get cursor area
\r
1508 irr::u32 cursorWidth = font->getDimension(CursorChar.c_str()).Width;
\r
1509 core::stringw *txtLine = hasBrokenText ? &BrokenText[cursLine] : &Text;
\r
1510 s32 cPos = hasBrokenText ? CursorPos - BrokenTextPositions[cursLine] : CursorPos; // column
\r
1511 s32 cStart = font->getDimension(txtLine->subString(0, cPos).c_str()).Width; // pixels from text-start
\r
1512 s32 cEnd = cStart + cursorWidth;
\r
1513 s32 txtWidth = font->getDimension(txtLine->c_str()).Width;
\r
1515 if ( txtWidth < FrameRect.getWidth() )
\r
1517 // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
\r
1518 // This check just fixes the case where it was most noticable (text smaller than clipping area).
\r
1521 setTextRect(cursLine);
\r
1524 if ( CurrentTextRect.UpperLeftCorner.X+cStart < FrameRect.UpperLeftCorner.X )
\r
1526 // cursor to the left of the clipping area
\r
1527 HScrollPos -= FrameRect.UpperLeftCorner.X-(CurrentTextRect.UpperLeftCorner.X+cStart);
\r
1528 setTextRect(cursLine);
\r
1530 // TODO: should show more characters to the left when we're scrolling left
\r
1531 // and the cursor reaches the border.
\r
1533 else if ( CurrentTextRect.UpperLeftCorner.X+cEnd > FrameRect.LowerRightCorner.X)
\r
1535 // cursor to the right of the clipping area
\r
1536 HScrollPos += (CurrentTextRect.UpperLeftCorner.X+cEnd)-FrameRect.LowerRightCorner.X;
\r
1537 setTextRect(cursLine);
\r
1541 // calculate vertical scrolling
\r
1542 if (hasBrokenText)
\r
1544 irr::u32 lineHeight = font->getDimension(L"A").Height + font->getKerningHeight();
\r
1545 // only up to 1 line fits?
\r
1546 if ( lineHeight >= (irr::u32)FrameRect.getHeight() )
\r
1549 setTextRect(cursLine);
\r
1550 s32 unscrolledPos = CurrentTextRect.UpperLeftCorner.Y;
\r
1551 s32 pivot = FrameRect.UpperLeftCorner.Y;
\r
1554 case EGUIA_CENTER:
\r
1555 pivot += FrameRect.getHeight()/2;
\r
1556 unscrolledPos += lineHeight/2;
\r
1558 case EGUIA_LOWERRIGHT:
\r
1559 pivot += FrameRect.getHeight();
\r
1560 unscrolledPos += lineHeight;
\r
1565 VScrollPos = unscrolledPos-pivot;
\r
1566 setTextRect(cursLine);
\r
1570 // First 2 checks are necessary when people delete lines
\r
1572 if ( CurrentTextRect.UpperLeftCorner.Y > FrameRect.UpperLeftCorner.Y && VAlign != EGUIA_LOWERRIGHT)
\r
1574 // first line is leaving a gap on top
\r
1577 else if (VAlign != EGUIA_UPPERLEFT)
\r
1579 u32 lastLine = BrokenTextPositions.empty() ? 0 : BrokenTextPositions.size()-1;
\r
1580 setTextRect(lastLine);
\r
1581 if ( CurrentTextRect.LowerRightCorner.Y < FrameRect.LowerRightCorner.Y)
\r
1583 // last line is leaving a gap on bottom
\r
1584 VScrollPos -= FrameRect.LowerRightCorner.Y-CurrentTextRect.LowerRightCorner.Y;
\r
1588 setTextRect(cursLine);
\r
1589 if ( CurrentTextRect.UpperLeftCorner.Y < FrameRect.UpperLeftCorner.Y )
\r
1591 // text above valid area
\r
1592 VScrollPos -= FrameRect.UpperLeftCorner.Y-CurrentTextRect.UpperLeftCorner.Y;
\r
1593 setTextRect(cursLine);
\r
1595 else if ( CurrentTextRect.LowerRightCorner.Y > FrameRect.LowerRightCorner.Y)
\r
1597 // text below valid area
\r
1598 VScrollPos += CurrentTextRect.LowerRightCorner.Y-FrameRect.LowerRightCorner.Y;
\r
1599 setTextRect(cursLine);
\r
1605 void CGUIEditBox::calculateFrameRect()
\r
1607 FrameRect = AbsoluteRect;
\r
1608 IGUISkin *skin = 0;
\r
1610 skin = Environment->getSkin();
\r
1611 if (Border && skin)
\r
1613 FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
\r
1614 FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
\r
1615 FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
\r
1616 FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
\r
1620 //! set text markers
\r
1621 void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
\r
1623 if ( begin != MarkBegin || end != MarkEnd )
\r
1625 MarkBegin = begin;
\r
1627 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
\r
1631 //! send some gui event to parent
\r
1632 void CGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
\r
1637 e.EventType = EET_GUI_EVENT;
\r
1638 e.GUIEvent.Caller = this;
\r
1639 e.GUIEvent.Element = 0;
\r
1640 e.GUIEvent.EventType = type;
\r
1642 Parent->OnEvent(e);
\r
1646 //! Returns whether the element takes input from the IME
\r
1647 bool CGUIEditBox::acceptsIME()
\r
1649 return isEnabled();
\r
1652 } // end namespace gui
\r
1653 } // end namespace irr
\r