66 mChangeContentByResize =
true;
69 void EditBox::initialiseOverride()
71 Base::initialiseOverride();
73 mOriginalPointer = getPointer();
76 setNeedKeyFocus(
true);
78 assignWidget(mScrollViewClient,
"Client");
79 if (getClientWidget() !=
nullptr)
81 getClientWidget()->eventMouseSetFocus += newDelegate(
this, &EditBox::notifyMouseSetFocus);
82 getClientWidget()->eventMouseLostFocus += newDelegate(
this, &EditBox::notifyMouseLostFocus);
83 getClientWidget()->eventMouseButtonPressed += newDelegate(
this, &EditBox::notifyMousePressed);
84 getClientWidget()->eventMouseButtonReleased += newDelegate(
this, &EditBox::notifyMouseReleased);
85 getClientWidget()->eventMouseDrag += newDelegate(
this, &EditBox::notifyMouseDrag);
86 getClientWidget()->eventMouseButtonDoubleClick += newDelegate(
this, &EditBox::notifyMouseButtonDoubleClick);
87 getClientWidget()->eventMouseWheel += newDelegate(
this, &EditBox::notifyMouseWheel);
91 assignWidget(mVScroll,
"VScroll");
92 if (mVScroll !=
nullptr)
94 mVScroll->eventScrollChangePosition +=
newDelegate(
this, &EditBox::notifyScrollChangePosition);
98 assignWidget(mHScroll,
"HScroll");
99 if (mHScroll !=
nullptr)
101 mHScroll->eventScrollChangePosition +=
newDelegate(
this, &EditBox::notifyScrollChangePosition);
104 mClientText = getSubWidgetText();
105 if (getClientWidget() !=
nullptr)
107 ISubWidgetText* text = getClientWidget()->getSubWidgetText();
115 if (mClientText !=
nullptr)
116 mClientText->setCursorPosition(mCursorPosition);
121 void EditBox::shutdownOverride()
123 mScrollViewClient =
nullptr;
124 mClientText =
nullptr;
128 Base::shutdownOverride();
131 void EditBox::notifyMouseSetFocus(Widget* _sender, Widget* _old)
133 if ((_old == getClientWidget()) || (mIsFocus))
140 void EditBox::notifyMouseLostFocus(Widget* _sender, Widget* _new)
142 if ((_new == getClientWidget()) || (!mIsFocus))
149 void EditBox::notifyMousePressed(Widget* _sender,
int _left,
int _top, MouseButton _id)
151 if (mClientText ==
nullptr)
158 IntPoint point = InputManager::getInstance().getLastPressedPositionForWidget(MouseButton::Left, _sender);
159 mCursorPosition = mClientText->getCursorPosition(point);
160 mClientText->setCursorPosition(mCursorPosition);
161 mClientText->setVisibleCursor(
true);
165 if (_id == MouseButton::Left)
166 mMouseLeftPressed =
true;
169 void EditBox::notifyMouseReleased(Widget* _sender,
int _left,
int _top, MouseButton _id)
171 mMouseLeftPressed =
false;
174 void EditBox::notifyMouseDrag(Widget* _sender,
int _left,
int _top, MouseButton _id)
176 if (_id != MouseButton::Left)
179 if (mClientText ==
nullptr)
187 mClientText->setVisibleCursor(
true);
191 mActionMouseTimer = 0;
193 size_t old = mCursorPosition;
195 mCursorPosition = mClientText->getCursorPosition(point);
197 if (old != mCursorPosition)
199 mClientText->setCursorPosition(mCursorPosition);
201 if (mStartSelect == ITEM_NONE)
204 mEndSelect = (size_t)mCursorPosition;
205 if (mStartSelect > mEndSelect)
206 mClientText->setTextSelection(mEndSelect, mStartSelect);
208 mClientText->setTextSelection(mStartSelect, mEndSelect);
210 updateViewWithCursor();
214 void EditBox::notifyMouseButtonDoubleClick(Widget* _sender)
216 if (mClientText ==
nullptr)
224 InputManager::getInstance().getLastPressedPositionForWidget(MouseButton::Left, getClientWidget());
226 size_t cursorPosition = mClientText->getCursorPosition(lastPressed);
227 mStartSelect = cursorPosition;
228 mEndSelect = cursorPosition;
230 const UString text = getOnlyText();
231 auto iterBack = text.rend() - cursorPosition;
232 auto iterForw = text.begin() + cursorPosition;
234 while (iterBack != text.rend())
236 if (((*iterBack) < 256) && (ispunct(*iterBack) || isspace(*iterBack)))
241 while (iterForw != text.end())
243 if (((*iterForw) < 256) && (ispunct(*iterForw) || isspace(*iterForw)))
249 mClientText->setCursorPosition(mEndSelect);
250 mClientText->setTextSelection(mStartSelect, mEndSelect);
253 void EditBox::onMouseDrag(
int _left,
int _top, MouseButton _id)
255 notifyMouseDrag(
nullptr, _left, _top, _id);
257 Base::onMouseDrag(_left, _top, _id);
260 void EditBox::onKeySetFocus(Widget* _old)
269 if (mClientText !=
nullptr)
271 mCursorActive =
true;
272 Gui::getInstance().eventFrameStart +=
newDelegate(
this, &EditBox::frameEntered);
273 mClientText->setVisibleCursor(
true);
274 mClientText->setSelectBackground(
true);
280 Base::onKeySetFocus(_old);
283 void EditBox::onKeyLostFocus(Widget* _new)
290 if (mClientText !=
nullptr)
292 mCursorActive =
false;
293 Gui::getInstance().eventFrameStart -=
newDelegate(
this, &EditBox::frameEntered);
294 mClientText->setVisibleCursor(
false);
295 mClientText->setSelectBackground(
false);
299 Base::onKeyLostFocus(_new);
304 return c ==
' ' || c ==
'\t';
309#if MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX || MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
310 switch (key.getValue())
315 case KeyCode::Insert:
318 case KeyCode::Return:
330 if (input.isControlPressed())
332 switch (key.getValue())
342#elif MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
343 switch (key.getValue())
348 case KeyCode::Return:
350 case KeyCode::ArrowLeft:
352 if (input.isAltPressed())
354 if (input.isMetaPressed() || input.isControlPressed())
358 case KeyCode::ArrowRight:
360 if (input.isAltPressed())
362 if (input.isMetaPressed() || input.isControlPressed())
374 if (input.isMetaPressed())
376 switch (key.getValue())
389 void EditBox::onKeyButtonPressed(KeyCode _key, Char _char)
391 if (mClientText ==
nullptr || getClientWidget() ==
nullptr)
393 Base::onKeyButtonPressed(_key, _char);
400 Base::onKeyButtonPressed(_key, _char);
404 InputManager& input = InputManager::getInstance();
406 mClientText->setVisibleCursor(
true);
411 if (editCmd == EditCommand::LoseFocus)
413 InputManager::getInstance().setKeyFocusWidget(
nullptr);
415 else if (editCmd == EditCommand::ErasePrevious)
423 if (!deleteTextSelect(
true))
426 if (mCursorPosition != 0)
429 eraseText(mCursorPosition, 1,
true);
433 eventEditTextChange(
this);
436 else if (editCmd == EditCommand::EraseNext)
444 if (!deleteTextSelect(
true))
446 if (mCursorPosition != mTextLength)
448 eraseText(mCursorPosition, 1,
true);
452 eventEditTextChange(
this);
455 else if (editCmd == EditCommand::Paste)
462 else if (editCmd == EditCommand::Copy)
466 else if (editCmd == EditCommand::Cut)
472 else if (editCmd == EditCommand::Undo)
476 else if (editCmd == EditCommand::Redo)
480 else if (editCmd == EditCommand::SelectAll)
482 setTextSelection(0, mTextLength);
484 else if (editCmd == EditCommand::NewLine)
495 size_t size = mVectorUndoChangeInfo.size();
497 deleteTextSelect(
true);
498 insertText(TextIterator::getTextNewLine(), mCursorPosition,
true);
500 if ((size + 2) == mVectorUndoChangeInfo.size())
503 eventEditTextChange(
this);
508 eventEditSelectAccept(
this);
512 else if (editCmd == EditCommand::AcceptEntry)
514 eventEditSelectAccept(
this);
516 else if (editCmd == EditCommand::MoveRight || editCmd == EditCommand::MoveRightWord)
518 if ((mCursorPosition) < mTextLength)
520 if (editCmd == EditCommand::MoveRightWord)
524 mCursorPosition = mTextLength;
528 const UString text = getOnlyText();
529 while (mCursorPosition < mTextLength &&
isWhitespace(text[mCursorPosition]))
533 while (mCursorPosition < mTextLength && !
isWhitespace(text[mCursorPosition]))
543 mClientText->setCursorPosition(mCursorPosition);
547 else if (isTextSelection() && !input.isShiftPressed())
552 else if (editCmd == EditCommand::MoveLeft || editCmd == EditCommand::MoveLeftWord)
554 if (mCursorPosition != 0)
556 if (editCmd == EditCommand::MoveLeftWord)
564 const UString text = getOnlyText();
565 while (mCursorPosition > 0 &&
isWhitespace(text[mCursorPosition - 1]))
569 while (mCursorPosition > 0 && !
isWhitespace(text[mCursorPosition - 1]))
579 mClientText->setCursorPosition(mCursorPosition);
583 else if (isTextSelection() && !input.isShiftPressed())
588 else if (editCmd == EditCommand::MoveUp)
590 moveCursorVertical(-mClientText->getFontHeight(), 0);
592 else if (editCmd == EditCommand::MoveDown)
594 moveCursorVertical(mClientText->getFontHeight(), mTextLength);
596 else if (editCmd == EditCommand::MoveLineBeginning)
598 IntPoint point = mClientText->getCursorPoint(mCursorPosition);
600 size_t old = mCursorPosition;
601 mCursorPosition = mClientText->getCursorPosition(point);
602 if (old != mCursorPosition)
604 mClientText->setCursorPosition(mCursorPosition);
607 else if (isTextSelection() && !input.isShiftPressed())
612 else if (editCmd == EditCommand::MoveTextBeginning)
614 if (0 != mCursorPosition)
617 mClientText->setCursorPosition(mCursorPosition);
620 else if (isTextSelection() && !input.isShiftPressed())
625 else if (editCmd == EditCommand::MoveLineEnd)
627 IntPoint point = mClientText->getCursorPoint(mCursorPosition);
629 size_t old = mCursorPosition;
630 mCursorPosition = mClientText->getCursorPosition(point);
631 if (old != mCursorPosition)
633 mClientText->setCursorPosition(mCursorPosition);
636 else if (isTextSelection() && !input.isShiftPressed())
641 else if (editCmd == EditCommand::MoveTextEnd)
643 if (mTextLength != mCursorPosition)
645 mCursorPosition = mTextLength;
646 mClientText->setCursorPosition(mCursorPosition);
649 else if (isTextSelection() && !input.isShiftPressed())
654 else if (editCmd == EditCommand::MovePageUp)
656 int step = (getClientWidget()->getHeight() > mClientText->getFontHeight())
657 ? getClientWidget()->getHeight()
658 : mClientText->getFontHeight();
659 moveCursorVertical(-step, 0);
661 else if (editCmd == EditCommand::MovePageDown)
663 int step = (getClientWidget()->getHeight() > mClientText->getFontHeight())
664 ? getClientWidget()->getHeight()
665 : mClientText->getFontHeight();
666 moveCursorVertical(step, mTextLength);
668 else if ((_key == KeyCode::LeftShift) || (_key == KeyCode::RightShift))
671 if (mStartSelect == ITEM_NONE)
673 mStartSelect = mEndSelect = mCursorPosition;
679 if (!mModeReadOnly && _char != 0)
685 if (_char !=
'\t' || mTabPrinting)
688 size_t size = mVectorUndoChangeInfo.size();
690 deleteTextSelect(
true);
691 insertText(TextIterator::getTextCharInfo(_char), mCursorPosition,
true);
693 if ((size + 2) == mVectorUndoChangeInfo.size())
696 eventEditTextChange(
this);
701 Base::onKeyButtonPressed(_key, _char);
704 void EditBox::frameEntered(
float _frame)
706 if (mClientText ==
nullptr)
715 mCursorTimer += _frame;
717 if (mCursorTimer > EDIT_CURSOR_TIMER)
719 mClientText->setVisibleCursor(!mClientText->isVisibleCursor());
720 while (mCursorTimer > EDIT_CURSOR_TIMER)
726 if (mMouseLeftPressed)
728 mActionMouseTimer += _frame;
730 if (mActionMouseTimer > EDIT_ACTION_MOUSE_TIMER)
732 IntPoint mouse = InputManager::getInstance().getMousePositionForWidget(getClientWidget());
733 const IntRect& view = getClientWidget()->getAbsoluteRect();
734 mouse.
left -= view.left;
735 mouse.top -= view.top;
741 if ((mouse.top < 0) && (mouse.top > -EDIT_ACTION_MOUSE_ZONE))
743 if ((mouse.left > 0) && (mouse.left <= getClientWidget()->getWidth()))
745 point = mClientText->getCursorPoint(mCursorPosition);
746 point.top -= mClientText->getFontHeight();
752 (mouse.top > getClientWidget()->getHeight()) &&
753 (mouse.top < (getClientWidget()->getHeight() + EDIT_ACTION_MOUSE_ZONE)))
755 if ((mouse.left > 0) && (mouse.left <= getClientWidget()->getWidth()))
757 point = mClientText->getCursorPoint(mCursorPosition);
758 point.top += mClientText->getFontHeight();
764 if ((mouse.left < 0) && (mouse.left > -EDIT_ACTION_MOUSE_ZONE))
766 point = mClientText->getCursorPoint(mCursorPosition);
767 point.left -= (int)EDIT_OFFSET_HORZ_CURSOR;
772 (mouse.left > getClientWidget()->getWidth()) &&
773 (mouse.left < (getClientWidget()->getWidth() + EDIT_ACTION_MOUSE_ZONE)))
775 point = mClientText->getCursorPoint(mCursorPosition);
776 point.left += (int)EDIT_OFFSET_HORZ_CURSOR;
782 size_t old = mCursorPosition;
783 mCursorPosition = mClientText->getCursorPosition(point);
785 if (old != mCursorPosition)
787 mClientText->setCursorPosition(mCursorPosition);
789 if (mStartSelect == ITEM_NONE)
792 mEndSelect = (size_t)mCursorPosition;
793 if (mStartSelect > mEndSelect)
794 mClientText->setTextSelection(mEndSelect, mStartSelect);
796 mClientText->setTextSelection(mStartSelect, mEndSelect);
799 updateViewWithCursor();
805 mActionMouseTimer = 0;
808 while (mActionMouseTimer > EDIT_ACTION_MOUSE_TIMER)
815 void EditBox::setTextCursor(
size_t _index)
820 if (_index > mTextLength)
821 _index = mTextLength;
823 if (mCursorPosition == _index)
826 mCursorPosition = _index;
828 if (mClientText !=
nullptr)
829 mClientText->setCursorPosition(mCursorPosition);
834 void EditBox::setTextSelection(
size_t _start,
size_t _end)
836 if (_start > mTextLength)
837 _start = mTextLength;
838 if (_end > mTextLength)
841 mStartSelect = _start;
844 if (mClientText !=
nullptr)
846 if (mStartSelect > mEndSelect)
847 mClientText->setTextSelection(mEndSelect, mStartSelect);
849 mClientText->setTextSelection(mStartSelect, mEndSelect);
852 if (mCursorPosition == mEndSelect)
855 mCursorPosition = mEndSelect;
858 if (mClientText !=
nullptr)
859 mClientText->setCursorPosition(mCursorPosition);
862 bool EditBox::deleteTextSelect(
bool _history)
864 if (!isTextSelection())
868 size_t start = getTextSelectionStart();
869 size_t end = getTextSelectionEnd();
871 eraseText(start, end - start, _history);
876 void EditBox::resetSelect()
878 if (mStartSelect != ITEM_NONE)
881 if (mClientText !=
nullptr)
882 mClientText->setTextSelection(0, 0);
886 void EditBox::commandPosition(
size_t _undo,
size_t _redo,
size_t _length, VectorChangeInfo* _info)
888 if (_info !=
nullptr)
889 _info->push_back(TextCommandInfo(_undo, _redo, _length));
892 void EditBox::commandMerge()
894 if (mVectorUndoChangeInfo.size() < 2)
898 mVectorUndoChangeInfo.pop_back();
901 for (
const auto& iter : info)
903 mVectorUndoChangeInfo.back().push_back(iter);
907 bool EditBox::commandUndo()
909 if (mVectorUndoChangeInfo.empty())
917 mVectorUndoChangeInfo.pop_back();
918 mVectorRedoChangeInfo.push_back(info);
920 UString text = getRealString();
923 for (VectorChangeInfo::const_reverse_iterator iter = info.rbegin(); iter != info.rend(); ++iter)
925 const auto& change = *iter;
928 case TextCommandInfo::COMMAND_INSERT: text.erase(change.start, change.text.size());
break;
929 case TextCommandInfo::COMMAND_ERASE: text.insert(change.start, change.text);
break;
930 case TextCommandInfo::COMMAND_POSITION:
931 mCursorPosition = change.undo;
932 mTextLength = change.length;
940 if (mClientText !=
nullptr)
941 mClientText->setCursorPosition(mCursorPosition);
944 eventEditTextChange(
this);
949 bool EditBox::commandRedo()
951 if (mVectorRedoChangeInfo.empty())
960 mVectorRedoChangeInfo.pop_back();
961 mVectorUndoChangeInfo.push_back(info);
963 UString text = getRealString();
966 for (
const auto& change : info)
970 case TextCommandInfo::COMMAND_INSERT: text.insert(change.start, change.text);
break;
971 case TextCommandInfo::COMMAND_ERASE: text.erase(change.start, change.text.size());
break;
972 case TextCommandInfo::COMMAND_POSITION:
973 mCursorPosition = change.redo;
974 mTextLength = change.length;
982 if (mClientText !=
nullptr)
983 mClientText->setCursorPosition(mCursorPosition);
986 eventEditTextChange(
this);
991 void EditBox::saveInHistory(VectorChangeInfo* _info)
993 if (_info ==
nullptr)
995 commandResetHistory();
1001 if ((_info->size() == 1) && (_info->back().type == TextCommandInfo::COMMAND_POSITION))
1004 mVectorUndoChangeInfo.push_back(*_info);
1006 if (mVectorUndoChangeInfo.size() > EDIT_MAX_UNDO)
1007 mVectorUndoChangeInfo.pop_front();
1010 void EditBox::moveCursorVertical(
int _delta,
size_t _clampTarget)
1012 IntPoint point = mClientText->getCursorPoint(mCursorPosition);
1013 point.
top += _delta;
1014 size_t old = mCursorPosition;
1015 mCursorPosition = mClientText->getCursorPosition(point);
1016 if (old == mCursorPosition)
1018 if (mCursorPosition != _clampTarget)
1020 mCursorPosition = _clampTarget;
1021 mClientText->setCursorPosition(mCursorPosition);
1024 else if (isTextSelection() && !InputManager::getInstance().isShiftPressed())
1031 mClientText->setCursorPosition(mCursorPosition);
1037 UString EditBox::getTextInterval(
size_t _start,
size_t _count)
const
1039 if (_start > mTextLength)
1040 _start = mTextLength;
1041 size_t end = _start + _count;
1043 TextIterator iterator(getRealString());
1046 if (mClientText !=
nullptr)
1047 colour = TextIterator::convertTagColour(mClientText->getTextColour());
1049 bool need_colour =
true;
1051 while (iterator.moveNext())
1053 size_t pos = iterator.getPosition();
1057 iterator.getTagColour(colour);
1063 need_colour = !iterator.getTagColour(colour);
1064 iterator.saveStartPoint();
1067 else if (pos == end)
1072 return colour + iterator.getFromStart();
1073 return iterator.getFromStart();
1076 void EditBox::_setTextColour(
size_t _start,
size_t _count,
const Colour& _colour,
bool _history)
1078 if (_count == 0 || _start >= mTextLength)
1080 _count = std::min(_count, mTextLength - _start);
1086 size_t end = _start + _count;
1088 TextIterator iterator(getRealString(), history);
1091 if (mClientText !=
nullptr)
1092 colour = TextIterator::convertTagColour(mClientText->getTextColour());
1094 while (iterator.moveNext())
1096 size_t pos = iterator.getPosition();
1098 iterator.getTagColour(colour);
1104 iterator.setTagColour(_colour);
1107 iterator.clearTagColour();
1109 else if (pos == end)
1111 iterator.setTagColour(colour);
1116 commandPosition(_start, _start + _count, mTextLength, history);
1118 saveInHistory(history);
1121 setRealString(iterator.getText());
1124 void EditBox::setTextSelectColour(
const Colour& _colour,
bool _history)
1126 if (!isTextSelection())
1129 size_t start = getTextSelectionStart();
1130 size_t end = getTextSelectionEnd();
1131 _setTextColour(start, end - start, _colour, _history);
1134 UString EditBox::getTextSelection()
const
1136 if (!isTextSelection())
1138 size_t start = getTextSelectionStart();
1139 size_t end = getTextSelectionEnd();
1140 return getTextInterval(start, end - start);
1143 void EditBox::setEditPassword(
bool _password)
1145 if (mModePassword == _password)
1147 if (_password && mModeMultiline)
1149 MYGUI_LOG(Warning,
"EditBox password mode is single-line; disabling multiline mode");
1150 setEditMultiLine(
false);
1152 mModePassword = _password;
1156 if (mClientText !=
nullptr)
1158 mPasswordText = mClientText->getCaption();
1159 mClientText->setCaption(UString(mTextLength, mCharPassword));
1164 if (mClientText !=
nullptr)
1166 mClientText->setCaption(mPasswordText);
1167 mPasswordText.clear();
1171 commandResetHistory();
1174 void EditBox::setText(
const UString& _caption,
bool _history)
1182 TextIterator iterator(getRealString(), history);
1184 iterator.setText(_caption, mModeMultiline || mModeWordWrap);
1186 if (mOverflowToTheLeft)
1188 iterator.cutMaxLengthFromBeginning(mMaxTextLength);
1192 iterator.cutMaxLength(mMaxTextLength);
1195 size_t old = mTextLength;
1196 mCursorPosition = mTextLength = iterator.getSize();
1198 commandPosition(0, mTextLength, old, history);
1200 saveInHistory(history);
1203 setRealString(iterator.getText());
1205 if (mClientText !=
nullptr)
1206 mClientText->setCursorPosition(mCursorPosition);
1210 void EditBox::insertText(
const UString& _text,
size_t _start,
bool _history)
1218 if ((!mOverflowToTheLeft) && (mTextLength == mMaxTextLength))
1225 TextIterator iterator(getRealString(), history);
1228 if (mClientText !=
nullptr)
1229 colour = TextIterator::convertTagColour(mClientText->getTextColour());
1232 bool need_colour = ((_text.size() > 6) && (_text[0] == U
'#') && (_text[1] != U
'#'));
1234 while (iterator.moveNext())
1236 size_t pos = iterator.getPosition();
1239 iterator.getTagColour(colour);
1246 iterator.setTagColour(colour);
1248 iterator.insertText(_text, mModeMultiline || mModeWordWrap);
1250 if (mOverflowToTheLeft)
1252 iterator.cutMaxLengthFromBeginning(mMaxTextLength);
1256 iterator.cutMaxLength(mMaxTextLength);
1259 size_t old = mTextLength;
1260 mTextLength = iterator.getSize();
1261 mCursorPosition += mTextLength - old;
1263 commandPosition(_start, _start + mTextLength - old, old, history);
1265 saveInHistory(history);
1268 setRealString(iterator.getText());
1271 if (mClientText !=
nullptr)
1272 mClientText->setCursorPosition(mCursorPosition);
1276 void EditBox::eraseText(
size_t _start,
size_t _count,
bool _history)
1287 TextIterator iterator(getRealString(), history);
1290 size_t end = _start + _count;
1291 bool need_colour =
false;
1293 while (iterator.moveNext())
1295 size_t pos = iterator.getPosition();
1299 iterator.getTagColour(colour);
1305 if (!colour.empty())
1310 iterator.getTagColour(colour);
1311 iterator.saveStartPoint();
1316 iterator.getTagColour(colour);
1319 else if (pos == end)
1321 if (!colour.empty())
1323 if (iterator.getTagColour(colour))
1324 need_colour =
false;
1330 iterator.eraseFromStart();
1332 iterator.setTagColour(colour);
1334 commandPosition(_start + _count, _start, mTextLength, history);
1336 mCursorPosition = _start;
1337 mTextLength -= _count;
1339 saveInHistory(history);
1342 setRealString(iterator.getText());
1345 if (mClientText !=
nullptr)
1346 mClientText->setCursorPosition(mCursorPosition);
1350 void EditBox::commandCut()
1353 if (isTextSelection() && !mModePassword && !mModeReadOnly)
1355 deleteTextSelect(
true);
1356 eventEditTextChange(
this);
1360 void EditBox::commandCopy()
const
1362 if (isTextSelection() && (!mModePassword))
1363 ClipboardManager::getInstance().setClipboardData(EDIT_CLIPBOARD_TYPE_TEXT, getTextSelection());
1365 ClipboardManager::getInstance().clearClipboardData(EDIT_CLIPBOARD_TYPE_TEXT);
1368 void EditBox::commandPaste()
1370 std::string clipboard = ClipboardManager::getInstance().getClipboardData(EDIT_CLIPBOARD_TYPE_TEXT);
1371 if ((!mModeReadOnly) && (!clipboard.empty()))
1374 size_t size = mVectorUndoChangeInfo.size();
1375 deleteTextSelect(
true);
1376 insertText(clipboard, mCursorPosition,
true);
1378 if ((size + 2) == mVectorUndoChangeInfo.size())
1380 eventEditTextChange(
this);
1384 const UString& EditBox::getRealString()
const
1387 return mPasswordText;
1388 if (mClientText ==
nullptr)
1389 return mPasswordText;
1391 return mClientText->getCaption();
1394 void EditBox::setRealString(
const UString& _caption)
1398 mPasswordText = _caption;
1399 if (mClientText !=
nullptr)
1400 mClientText->setCaption(UString(mTextLength, mCharPassword));
1404 if (mClientText !=
nullptr)
1405 mClientText->setCaption(_caption);
1409 void EditBox::setPasswordChar(Char _char)
1411 mCharPassword = _char;
1414 if (mClientText !=
nullptr)
1415 mClientText->setCaption(UString(mTextLength, mCharPassword));
1419 void EditBox::updateEditState()
1421 if (!getInheritedEnabled())
1423 _setWidgetState(
"disabled");
1425 else if (mIsPressed)
1428 _setWidgetState(
"pushed");
1430 _setWidgetState(
"normal_checked");
1434 _setWidgetState(
"highlighted");
1438 _setWidgetState(
"normal");
1442 void EditBox::setPosition(
const IntPoint& _point)
1444 Base::setPosition(_point);
1447 void EditBox::eraseView()
1452 if (mClientText !=
nullptr)
1453 mClientText->_invalidateTextLayout();
1459 void EditBox::setSize(
const IntSize& _size)
1461 Base::setSize(_size);
1466 void EditBox::setCoord(
const IntCoord& _coord)
1468 Base::setCoord(_coord);
1473 void EditBox::setCaption(
const UString& _value)
1475 setText(_value,
false);
1478 const UString& EditBox::getCaption()
const
1480 return getRealString();
1483 void EditBox::updateSelectText()
1487 InputManager& input = InputManager::getInstance();
1488 if ((input.isShiftPressed()) && (mStartSelect != ITEM_NONE))
1491 mEndSelect = (size_t)mCursorPosition;
1492 if (mClientText !=
nullptr)
1494 if (mStartSelect > mEndSelect)
1495 mClientText->setTextSelection(mEndSelect, mStartSelect);
1497 mClientText->setTextSelection(mStartSelect, mEndSelect);
1500 else if (mStartSelect != ITEM_NONE)
1504 if (mClientText !=
nullptr)
1505 mClientText->setTextSelection(0, 0);
1510 updateViewWithCursor();
1513 void EditBox::setTextAlign(Align _value)
1515 Base::setTextAlign(_value);
1517 if (mClientText !=
nullptr)
1518 mClientText->setTextAlign(_value);
1524 void EditBox::setTextColour(
const Colour& _value)
1526 Base::setTextColour(_value);
1528 if (mClientText !=
nullptr)
1529 mClientText->setTextColour(_value);
1532 IntCoord EditBox::getTextRegion()
const
1534 if (mClientText !=
nullptr)
1535 return mClientText->getCoord();
1536 return Base::getTextRegion();
1539 IntSize EditBox::getTextSize()
const
1541 if (mClientText !=
nullptr)
1542 return mClientText->getTextSize();
1543 return Base::getTextSize();
1546 void EditBox::notifyScrollChangePosition(ScrollBar* _sender,
size_t _position)
1548 if (mClientText ==
nullptr)
1551 if (_sender == mVScroll)
1553 IntPoint point = mClientText->getViewOffset();
1554 point.
top = _position;
1555 mClientText->setViewOffset(point);
1557 else if (_sender == mHScroll)
1559 IntPoint point = mClientText->getViewOffset();
1560 point.
left = _position;
1561 mClientText->setViewOffset(point);
1565 void EditBox::notifyMouseWheel(Widget* _sender,
int _rel)
1567 if (mClientText ==
nullptr)
1572 IntPoint point = mClientText->getViewOffset();
1573 int offset = point.
top;
1579 offset = std::clamp(offset, 0,
int(mVRange));
1581 if (offset != point.top)
1584 if (mVScroll !=
nullptr)
1585 mVScroll->setScrollPosition(offset);
1586 mClientText->setViewOffset(point);
1589 else if (mHRange != 0)
1591 IntPoint point = mClientText->getViewOffset();
1592 int offset = point.
left;
1598 offset = std::clamp(offset, 0,
int(mHRange));
1600 if (offset != point.left)
1602 point.left = offset;
1603 if (mHScroll !=
nullptr)
1604 mHScroll->setScrollPosition(offset);
1605 mClientText->setViewOffset(point);
1610 void EditBox::setEditWordWrap(
bool _value)
1612 mModeWordWrap = _value;
1613 if (mClientText !=
nullptr)
1614 mClientText->setWordWrap(mModeWordWrap);
1619 void EditBox::setFontName(std::string_view _value)
1621 Base::setFontName(_value);
1623 if (mClientText !=
nullptr)
1624 mClientText->setFontName(_value);
1629 void EditBox::setFontHeight(
int _value)
1631 Base::setFontHeight(_value);
1633 if (mClientText !=
nullptr)
1634 mClientText->setFontHeight(_value);
1639 int EditBox::getFontHeight()
const
1641 return (
nullptr == mClientText) ? 0 : mClientText->getFontHeight();
1644 void EditBox::updateView()
1647 updateScrollPosition();
1650 void EditBox::updateViewWithCursor()
1653 updateCursorPosition();
1654 updateScrollPosition();
1657 void EditBox::updateCursorPosition()
1659 if (mClientText ==
nullptr || getClientWidget() ==
nullptr)
1663 IntSize textSize = mClientText->getTextSize();
1665 IntPoint point = mClientText->getViewOffset();
1668 IntRect cursor = mClientText->getCursorRect(mCursorPosition);
1671 const IntRect& view = getClientWidget()->getAbsoluteRect();
1674 if (!view.inside(cursor))
1677 if (textSize.width > view.width())
1679 if (cursor.left < view.left)
1681 offset.left = point.left - (view.left - cursor.left);
1683 if ((
float(view.width()) - EDIT_OFFSET_HORZ_CURSOR) > EDIT_OFFSET_HORZ_CURSOR)
1684 offset.left -= int(EDIT_OFFSET_HORZ_CURSOR);
1686 else if (cursor.right > view.right)
1688 offset.left = point.left + (cursor.right - view.right);
1690 if ((
float(view.width()) - EDIT_OFFSET_HORZ_CURSOR) > EDIT_OFFSET_HORZ_CURSOR)
1691 offset.left += int(EDIT_OFFSET_HORZ_CURSOR);
1696 if (textSize.height > view.height())
1699 if (cursor.height() > view.height())
1702 delta = ((cursor.bottom - view.bottom) - (view.top - cursor.top)) / 2;
1704 else if (cursor.top < view.top)
1706 delta = -(view.top - cursor.top);
1708 else if (cursor.bottom > view.bottom)
1710 delta = (cursor.bottom - view.bottom);
1712 offset.top = point.top + delta;
1716 if (offset != point)
1718 mClientText->setViewOffset(offset);
1719 if (mVScroll !=
nullptr)
1720 mVScroll->setScrollPosition(offset.top);
1721 if (mHScroll !=
nullptr)
1722 mHScroll->setScrollPosition(offset.left);
1726 void EditBox::setContentPosition(
const IntPoint& _point)
1728 if (mClientText !=
nullptr)
1729 mClientText->setViewOffset(_point);
1732 IntSize EditBox::getViewSize()
const
1734 if (mClientText !=
nullptr)
1735 return mClientText->getSize();
1736 return ScrollViewBase::getViewSize();
1739 IntSize EditBox::getContentSize()
const
1741 if (mClientText !=
nullptr)
1742 return mClientText->getTextSize();
1743 return ScrollViewBase::getContentSize();
1746 size_t EditBox::getVScrollPage()
const
1748 if (mClientText !=
nullptr)
1749 return (
size_t)mClientText->getFontHeight();
1750 return ScrollViewBase::getVScrollPage();
1753 size_t EditBox::getHScrollPage()
const
1755 if (mClientText !=
nullptr)
1756 return (
size_t)mClientText->getFontHeight();
1757 return ScrollViewBase::getHScrollPage();
1760 IntPoint EditBox::getContentPosition()
const
1762 if (mClientText !=
nullptr)
1763 return mClientText->getViewOffset();
1764 return ScrollViewBase::getContentPosition();
1767 Align EditBox::getContentAlign()
const
1769 if (mClientText !=
nullptr)
1770 return mClientText->getTextAlign();
1771 return ScrollViewBase::getContentAlign();
1774 void EditBox::setTextIntervalColour(
size_t _start,
size_t _count,
const Colour& _colour)
1776 _setTextColour(_start, _count, _colour,
false);
1779 size_t EditBox::getTextSelectionStart()
const
1781 return (mStartSelect == ITEM_NONE) ?
ITEM_NONE : (mStartSelect > mEndSelect ? mEndSelect : mStartSelect);
1784 size_t EditBox::getTextSelectionEnd()
const
1786 return (mStartSelect == ITEM_NONE) ?
ITEM_NONE : (mStartSelect > mEndSelect ? mStartSelect : mEndSelect);
1789 bool EditBox::isTextSelection()
const
1791 return (mStartSelect != ITEM_NONE) && (mStartSelect != mEndSelect);
1794 void EditBox::deleteTextSelection()
1796 deleteTextSelect(
false);
1799 void EditBox::setTextSelectionColour(
const Colour& _colour)
1801 setTextSelectColour(_colour,
false);
1804 size_t EditBox::getTextSelectionLength()
const
1806 if (!isTextSelection())
1808 return getTextSelectionEnd() - getTextSelectionStart();
1811 void EditBox::setOnlyText(
const UString& _text)
1813 setText(TextIterator::toTagsString(_text),
false);
1816 UString EditBox::getOnlyText()
const
1818 return TextIterator::getOnlyText(getRealString());
1821 void EditBox::insertText(
const UString& _text,
size_t _index)
1823 insertText(_text, _index,
false);
1826 void EditBox::addText(
const UString& _text)
1828 insertText(_text, ITEM_NONE,
false);
1831 void EditBox::eraseText(
size_t _start,
size_t _count)
1833 eraseText(_start, _count,
false);
1836 void EditBox::setEditReadOnly(
bool _value)
1838 mModeReadOnly = _value;
1839 commandResetHistory();
1842 void EditBox::setEditMultiLine(
bool _value)
1844 if (_value && mModePassword)
1845 MYGUI_LOG(Warning,
"EditBox multiline mode is ignored while password mode is enabled");
1846 mModeMultiline = _value && !mModePassword;
1848 if (!mModeMultiline)
1850 setText(getRealString(),
false);
1856 commandResetHistory();
1859 void EditBox::setEditStatic(
bool _value)
1861 mModeStatic = _value;
1864 if (getClientWidget() !=
nullptr)
1867 getClientWidget()->setPointer(std::string_view{});
1869 getClientWidget()->setPointer(mOriginalPointer);
1873 void EditBox::setPasswordChar(
const UString& _value)
1875 if (!_value.empty())
1876 setPasswordChar(_value[0]);
1879 void EditBox::setVisibleVScroll(
bool _value)
1881 mVisibleVScroll = _value;
1885 void EditBox::setVisibleHScroll(
bool _value)
1887 mVisibleHScroll = _value;
1891 size_t EditBox::getVScrollRange()
const
1896 size_t EditBox::getVScrollPosition()
const
1898 return mClientText ==
nullptr ? 0 : mClientText->getViewOffset().top;
1901 void EditBox::setVScrollPosition(
size_t _index)
1903 if (mClientText ==
nullptr)
1906 if (_index > mVRange)
1909 IntPoint point = mClientText->getViewOffset();
1912 mClientText->setViewOffset(point);
1913 if (mVScroll !=
nullptr)
1914 mVScroll->setScrollPosition(point.top);
1917 size_t EditBox::getHScrollRange()
const
1922 size_t EditBox::getHScrollPosition()
const
1924 return mClientText ==
nullptr ? 0 : mClientText->getViewOffset().left;
1927 void EditBox::setHScrollPosition(
size_t _index)
1929 if (mClientText ==
nullptr)
1932 if (_index > mHRange)
1935 IntPoint point = mClientText->getViewOffset();
1936 point.
left = _index;
1938 mClientText->setViewOffset(point);
1939 if (mHScroll !=
nullptr)
1940 mHScroll->setScrollPosition(point.left);
1943 bool EditBox::getInvertSelected()
const
1945 return mClientText ==
nullptr ? false : mClientText->getInvertSelected();
1948 void EditBox::setInvertSelected(
bool _value)
1950 if (mClientText !=
nullptr)
1951 mClientText->setInvertSelected(_value);
1954 void EditBox::setPropertyOverride(std::string_view _key, std::string_view _value)
1957 if (_key ==
"CursorPosition")
1958 setTextCursor(utility::parseValue<size_t>(_value));
1961 else if (_key ==
"TextSelect")
1963 utility::parseValue<types::TSize<size_t>>(_value).width,
1964 utility::parseValue<types::TSize<size_t>>(_value).height);
1967 else if (_key ==
"ReadOnly")
1968 setEditReadOnly(utility::parseValue<bool>(_value));
1971 else if (_key ==
"Password")
1972 setEditPassword(utility::parseValue<bool>(_value));
1975 else if (_key ==
"MultiLine")
1976 setEditMultiLine(utility::parseValue<bool>(_value));
1979 else if (_key ==
"PasswordChar")
1980 setPasswordChar(UString(_value));
1983 else if (_key ==
"MaxTextLength")
1984 setMaxTextLength(utility::parseValue<size_t>(_value));
1987 else if (_key ==
"OverflowToTheLeft")
1988 setOverflowToTheLeft(utility::parseValue<bool>(_value));
1991 else if (_key ==
"Static")
1992 setEditStatic(utility::parseValue<bool>(_value));
1995 else if (_key ==
"VisibleVScroll")
1996 setVisibleVScroll(utility::parseValue<bool>(_value));
1999 else if (_key ==
"VisibleHScroll")
2000 setVisibleHScroll(utility::parseValue<bool>(_value));
2003 else if (_key ==
"WordWrap")
2004 setEditWordWrap(utility::parseValue<bool>(_value));
2007 else if (_key ==
"TabPrinting")
2008 setTabPrinting(utility::parseValue<bool>(_value));
2011 else if (_key ==
"InvertSelected")
2012 setInvertSelected(utility::parseValue<bool>(_value));
2016 Base::setPropertyOverride(_key, _value);
2020 eventChangeProperty(
this, _key, _value);
2023 size_t EditBox::getTextCursor()
const
2025 return mCursorPosition;
2028 size_t EditBox::getTextLength()
const
2033 void EditBox::setOverflowToTheLeft(
bool _value)
2035 mOverflowToTheLeft = _value;
2038 bool EditBox::getOverflowToTheLeft()
const
2040 return mOverflowToTheLeft;
2043 void EditBox::setMaxTextLength(
size_t _value)
2045 mMaxTextLength = _value;
2048 size_t EditBox::getMaxTextLength()
const
2050 return mMaxTextLength;
2053 bool EditBox::getEditReadOnly()
const
2055 return mModeReadOnly;
2058 bool EditBox::getEditPassword()
const
2060 return mModePassword;
2063 bool EditBox::getEditMultiLine()
const
2065 return mModeMultiline;
2068 bool EditBox::getEditStatic()
const
2073 Char EditBox::getPasswordChar()
const
2075 return mCharPassword;
2078 bool EditBox::getEditWordWrap()
const
2080 return mModeWordWrap;
2083 void EditBox::setTabPrinting(
bool _value)
2085 mTabPrinting = _value;
2088 bool EditBox::getTabPrinting()
const
2090 return mTabPrinting;
2093 bool EditBox::isVisibleVScroll()
const
2095 return mVisibleVScroll;
2098 bool EditBox::isVisibleHScroll()
const
2100 return mVisibleHScroll;
2103 void EditBox::commandResetRedo()
2105 mVectorRedoChangeInfo.clear();
2108 void EditBox::commandResetHistory()
2110 mVectorRedoChangeInfo.clear();
2111 mVectorUndoChangeInfo.clear();
2114 void EditBox::setTextShadowColour(
const Colour& _value)
2116 Base::setTextShadowColour(_value);
2118 if (mClientText !=
nullptr)
2119 mClientText->setShadowColour(_value);
2122 void EditBox::setTextShadow(
bool _value)
2124 Base::setTextShadow(_value);
2126 if (mClientText !=
nullptr)
2127 mClientText->setShadow(_value);
#define MYGUI_LOG(level, text)
const int EDIT_MOUSE_WHEEL
types::TRect< int > IntRect
const int EDIT_CURSOR_MIN_POSITION
types::TPoint< int > IntPoint
std::vector< TextCommandInfo > VectorChangeInfo
const size_t EDIT_MAX_UNDO
const int EDIT_CURSOR_MAX_POSITION
constexpr size_t ITEM_NONE
const float EDIT_CURSOR_TIMER
static EditCommand ToEditCommand(KeyCode key, const InputManager &input)
types::TCoord< int > IntCoord
types::TSize< int > IntSize
const float EDIT_OFFSET_HORZ_CURSOR
static bool isWhitespace(const UString::code_point &c)
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
const std::string_view EDIT_CLIPBOARD_TYPE_TEXT
const float EDIT_ACTION_MOUSE_TIMER
const int EDIT_ACTION_MOUSE_ZONE
const size_t EDIT_DEFAULT_MAX_TEXT_LENGTH