MyGUI 3.5.3
MyGUI_EditBox.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_EditBox.h"
9#include "MyGUI_Gui.h"
10#include "MyGUI_ResourceSkin.h"
11#include "MyGUI_SkinManager.h"
12#include "MyGUI_InputManager.h"
16#include "MyGUI_ScrollBar.h"
17
18#include <algorithm>
19#include <cctype>
20
21namespace MyGUI
22{
23
24 enum class EditCommand
25 {
50 };
51
52 const float EDIT_CURSOR_TIMER = 0.7f;
53 const float EDIT_ACTION_MOUSE_TIMER = 0.05f;
54 const int EDIT_CURSOR_MAX_POSITION = 100000;
55 const int EDIT_CURSOR_MIN_POSITION = -100000;
56 const size_t EDIT_MAX_UNDO = 128;
57 const size_t EDIT_DEFAULT_MAX_TEXT_LENGTH = 2048;
58 const float EDIT_OFFSET_HORZ_CURSOR = 10.0f; // additional offset for cursor
59 const int EDIT_ACTION_MOUSE_ZONE = 1500; // mouse interaction zone outside the edit box
60 const std::string_view EDIT_CLIPBOARD_TYPE_TEXT = "Text";
61 const int EDIT_MOUSE_WHEEL = 50; // mouse interaction zone outside the edit box
62
63 EditBox::EditBox() :
64 mMaxTextLength(EDIT_DEFAULT_MAX_TEXT_LENGTH)
65 {
66 mChangeContentByResize = true;
67 }
68
69 void EditBox::initialiseOverride()
70 {
71 Base::initialiseOverride();
72
73 mOriginalPointer = getPointer();
74
75 // FIXME we need keyboard focus
76 setNeedKeyFocus(true);
77
78 assignWidget(mScrollViewClient, "Client");
79 if (getClientWidget() != nullptr)
80 {
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);
88 }
89
91 assignWidget(mVScroll, "VScroll");
92 if (mVScroll != nullptr)
93 {
94 mVScroll->eventScrollChangePosition += newDelegate(this, &EditBox::notifyScrollChangePosition);
95 }
96
98 assignWidget(mHScroll, "HScroll");
99 if (mHScroll != nullptr)
100 {
101 mHScroll->eventScrollChangePosition += newDelegate(this, &EditBox::notifyScrollChangePosition);
102 }
103
104 mClientText = getSubWidgetText();
105 if (getClientWidget() != nullptr)
106 {
107 ISubWidgetText* text = getClientWidget()->getSubWidgetText();
108 if (text)
109 mClientText = text;
110 }
111
112 updateScrollSize();
113
114 // initial cursor initialization
115 if (mClientText != nullptr)
116 mClientText->setCursorPosition(mCursorPosition);
117
118 updateSelectText();
119 }
120
121 void EditBox::shutdownOverride()
122 {
123 mScrollViewClient = nullptr;
124 mClientText = nullptr;
125 mVScroll = nullptr;
126 mHScroll = nullptr;
127
128 Base::shutdownOverride();
129 }
130
131 void EditBox::notifyMouseSetFocus(Widget* _sender, Widget* _old)
132 {
133 if ((_old == getClientWidget()) || (mIsFocus))
134 return;
135
136 mIsFocus = true;
137 updateEditState();
138 }
139
140 void EditBox::notifyMouseLostFocus(Widget* _sender, Widget* _new)
141 {
142 if ((_new == getClientWidget()) || (!mIsFocus))
143 return;
144
145 mIsFocus = false;
146 updateEditState();
147 }
148
149 void EditBox::notifyMousePressed(Widget* _sender, int _left, int _top, MouseButton _id)
150 {
151 if (mClientText == nullptr)
152 return;
153
154 // in static mode nothing is available
155 if (mModeStatic)
156 return;
157
158 IntPoint point = InputManager::getInstance().getLastPressedPositionForWidget(MouseButton::Left, _sender);
159 mCursorPosition = mClientText->getCursorPosition(point);
160 mClientText->setCursorPosition(mCursorPosition);
161 mClientText->setVisibleCursor(true);
162 mCursorTimer = 0;
163 updateSelectText();
164
165 if (_id == MouseButton::Left)
166 mMouseLeftPressed = true;
167 }
168
169 void EditBox::notifyMouseReleased(Widget* _sender, int _left, int _top, MouseButton _id)
170 {
171 mMouseLeftPressed = false;
172 }
173
174 void EditBox::notifyMouseDrag(Widget* _sender, int _left, int _top, MouseButton _id)
175 {
176 if (_id != MouseButton::Left)
177 return;
178
179 if (mClientText == nullptr)
180 return;
181
182 // in static mode nothing is available
183 if (mModeStatic)
184 return;
185
186 // stop cursor
187 mClientText->setVisibleCursor(true);
188
189 // reset all timers
190 mCursorTimer = 0;
191 mActionMouseTimer = 0;
192
193 size_t old = mCursorPosition;
194 IntPoint point(_left, _top);
195 mCursorPosition = mClientText->getCursorPosition(point);
196
197 if (old != mCursorPosition)
198 {
199 mClientText->setCursorPosition(mCursorPosition);
200
201 if (mStartSelect == ITEM_NONE)
202 mStartSelect = old;
203
204 mEndSelect = (size_t)mCursorPosition;
205 if (mStartSelect > mEndSelect)
206 mClientText->setTextSelection(mEndSelect, mStartSelect);
207 else
208 mClientText->setTextSelection(mStartSelect, mEndSelect);
209
210 updateViewWithCursor();
211 }
212 }
213
214 void EditBox::notifyMouseButtonDoubleClick(Widget* _sender)
215 {
216 if (mClientText == nullptr)
217 return;
218
219 // in static mode nothing is available
220 if (mModeStatic)
221 return;
222
223 const IntPoint lastPressed =
224 InputManager::getInstance().getLastPressedPositionForWidget(MouseButton::Left, getClientWidget());
225
226 size_t cursorPosition = mClientText->getCursorPosition(lastPressed);
227 mStartSelect = cursorPosition;
228 mEndSelect = cursorPosition;
229
230 const UString text = getOnlyText();
231 auto iterBack = text.rend() - cursorPosition;
232 auto iterForw = text.begin() + cursorPosition;
233
234 while (iterBack != text.rend())
235 {
236 if (((*iterBack) < 256) && (ispunct(*iterBack) || isspace(*iterBack)))
237 break;
238 ++iterBack;
239 mStartSelect--;
240 }
241 while (iterForw != text.end())
242 {
243 if (((*iterForw) < 256) && (ispunct(*iterForw) || isspace(*iterForw)))
244 break;
245 ++iterForw;
246 mEndSelect++;
247 }
248
249 mClientText->setCursorPosition(mEndSelect);
250 mClientText->setTextSelection(mStartSelect, mEndSelect);
251 }
252
253 void EditBox::onMouseDrag(int _left, int _top, MouseButton _id)
254 {
255 notifyMouseDrag(nullptr, _left, _top, _id);
256
257 Base::onMouseDrag(_left, _top, _id);
258 }
259
260 void EditBox::onKeySetFocus(Widget* _old)
261 {
262 if (!mIsPressed)
263 {
264 mIsPressed = true;
265 updateEditState();
266
267 if (!mModeStatic)
268 {
269 if (mClientText != nullptr)
270 {
271 mCursorActive = true;
272 Gui::getInstance().eventFrameStart += newDelegate(this, &EditBox::frameEntered);
273 mClientText->setVisibleCursor(true);
274 mClientText->setSelectBackground(true);
275 mCursorTimer = 0;
276 }
277 }
278 }
279
280 Base::onKeySetFocus(_old);
281 }
282
283 void EditBox::onKeyLostFocus(Widget* _new)
284 {
285 if (mIsPressed)
286 {
287 mIsPressed = false;
288 updateEditState();
289
290 if (mClientText != nullptr)
291 {
292 mCursorActive = false;
293 Gui::getInstance().eventFrameStart -= newDelegate(this, &EditBox::frameEntered);
294 mClientText->setVisibleCursor(false);
295 mClientText->setSelectBackground(false);
296 }
297 }
298
299 Base::onKeyLostFocus(_new);
300 }
301
302 static bool isWhitespace(const UString::code_point& c)
303 {
304 return c == ' ' || c == '\t';
305 }
306
307 static EditCommand ToEditCommand(KeyCode key, const InputManager& input)
308 {
309#if MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX || MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
310 switch (key.getValue())
311 {
312 case KeyCode::Escape: return EditCommand::LoseFocus;
313 case KeyCode::Backspace: return EditCommand::ErasePrevious;
314 case KeyCode::Delete: return input.isShiftPressed() ? EditCommand::Cut : EditCommand::EraseNext;
315 case KeyCode::Insert:
316 return input.isShiftPressed() ? EditCommand::Paste
317 : (input.isControlPressed() ? EditCommand::Copy : EditCommand::Unknown);
318 case KeyCode::Return:
319 case KeyCode::NumpadEnter: return input.isControlPressed() ? EditCommand::AcceptEntry : EditCommand::NewLine;
320 case KeyCode::ArrowLeft: return input.isControlPressed() ? EditCommand::MoveLeftWord : EditCommand::MoveLeft;
321 case KeyCode::ArrowRight: return input.isControlPressed() ? EditCommand::MoveRightWord : EditCommand::MoveRight;
322 case KeyCode::ArrowUp: return EditCommand::MoveUp;
323 case KeyCode::ArrowDown: return EditCommand::MoveDown;
324 case KeyCode::Home:
325 return input.isControlPressed() ? EditCommand::MoveTextBeginning : EditCommand::MoveLineBeginning;
326 case KeyCode::End: return input.isControlPressed() ? EditCommand::MoveTextEnd : EditCommand::MoveLineEnd;
327 case KeyCode::PageUp: return EditCommand::MovePageUp;
328 case KeyCode::PageDown: return EditCommand::MovePageDown;
329 }
330 if (input.isControlPressed())
331 {
332 switch (key.getValue())
333 {
334 case KeyCode::C: return EditCommand::Copy;
335 case KeyCode::V: return EditCommand::Paste;
336 case KeyCode::X: return EditCommand::Cut;
337 case KeyCode::Z: return EditCommand::Undo;
338 case KeyCode::Y: return EditCommand::Redo;
339 case KeyCode::A: return EditCommand::SelectAll;
340 }
341 }
342#elif MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
343 switch (key.getValue())
344 {
345 case KeyCode::Escape: return EditCommand::LoseFocus;
346 case KeyCode::Backspace: return EditCommand::ErasePrevious;
347 case KeyCode::Delete: return EditCommand::EraseNext;
348 case KeyCode::Return:
349 case KeyCode::NumpadEnter: return input.isMetaPressed() ? EditCommand::AcceptEntry : EditCommand::NewLine;
350 case KeyCode::ArrowLeft:
351 {
352 if (input.isAltPressed())
354 if (input.isMetaPressed() || input.isControlPressed())
357 }
358 case KeyCode::ArrowRight:
359 {
360 if (input.isAltPressed())
362 if (input.isMetaPressed() || input.isControlPressed())
365 }
366 case KeyCode::ArrowUp: return input.isMetaPressed() ? EditCommand::MoveTextBeginning : EditCommand::MoveUp;
367 case KeyCode::ArrowDown: return input.isMetaPressed() ? EditCommand::MoveTextEnd : EditCommand::MoveDown;
368 case KeyCode::Home:
369 return input.isMetaPressed() ? EditCommand::MoveTextBeginning : EditCommand::MoveLineBeginning;
370 case KeyCode::End: return input.isMetaPressed() ? EditCommand::MoveTextEnd : EditCommand::MoveLineEnd;
371 case KeyCode::PageUp: return EditCommand::MovePageUp;
372 case KeyCode::PageDown: return EditCommand::MovePageDown;
373 }
374 if (input.isMetaPressed())
375 {
376 switch (key.getValue())
377 {
378 case KeyCode::C: return EditCommand::Copy;
379 case KeyCode::V: return EditCommand::Paste;
380 case KeyCode::X: return EditCommand::Cut;
381 case KeyCode::Z: return input.isShiftPressed() ? EditCommand::Redo : EditCommand::Undo;
382 case KeyCode::A: return EditCommand::SelectAll;
383 }
384 }
385#endif
387 }
388
389 void EditBox::onKeyButtonPressed(KeyCode _key, Char _char)
390 {
391 if (mClientText == nullptr || getClientWidget() == nullptr)
392 {
393 Base::onKeyButtonPressed(_key, _char);
394 return;
395 }
396
397 // in static mode nothing is available
398 if (mModeStatic)
399 {
400 Base::onKeyButtonPressed(_key, _char);
401 return;
402 }
403
404 InputManager& input = InputManager::getInstance();
405
406 mClientText->setVisibleCursor(true);
407 mCursorTimer = 0.0f;
408
409 auto editCmd = ToEditCommand(_key, input);
410
411 if (editCmd == EditCommand::LoseFocus)
412 {
413 InputManager::getInstance().setKeyFocusWidget(nullptr);
414 }
415 else if (editCmd == EditCommand::ErasePrevious)
416 {
417 // if needed, delete selected text
418 if (!mModeReadOnly)
419 {
420 // reset repeat
421 commandResetRedo();
422
423 if (!deleteTextSelect(true))
424 {
425 // jump back one and delete
426 if (mCursorPosition != 0)
427 {
428 mCursorPosition--;
429 eraseText(mCursorPosition, 1, true);
430 }
431 }
432 // fire change event
433 eventEditTextChange(this);
434 }
435 }
436 else if (editCmd == EditCommand::EraseNext)
437 {
438 if (!mModeReadOnly)
439 {
440 // reset repeat
441 commandResetRedo();
442
443 // if needed, delete selected text
444 if (!deleteTextSelect(true))
445 {
446 if (mCursorPosition != mTextLength)
447 {
448 eraseText(mCursorPosition, 1, true);
449 }
450 }
451 // fire change event
452 eventEditTextChange(this);
453 }
454 }
455 else if (editCmd == EditCommand::Paste)
456 {
457 // reset repeat
458 commandResetRedo();
459
460 commandPaste();
461 }
462 else if (editCmd == EditCommand::Copy)
463 {
464 commandCopy();
465 }
466 else if (editCmd == EditCommand::Cut)
467 {
468 commandResetRedo();
469
470 commandCut();
471 }
472 else if (editCmd == EditCommand::Undo)
473 {
474 commandUndo();
475 }
476 else if (editCmd == EditCommand::Redo)
477 {
478 commandRedo();
479 }
480 else if (editCmd == EditCommand::SelectAll)
481 {
482 setTextSelection(0, mTextLength);
483 }
484 else if (editCmd == EditCommand::NewLine)
485 {
486 // only work in edit mode
487 if (!mModeReadOnly)
488 {
489 if (mModeMultiline)
490 {
491 // reset repeat
492 commandResetRedo();
493
494 // attempt to merge two commands
495 size_t size = mVectorUndoChangeInfo.size();
496 // actual operations
497 deleteTextSelect(true);
498 insertText(TextIterator::getTextNewLine(), mCursorPosition, true);
499 // check merge possibility
500 if ((size + 2) == mVectorUndoChangeInfo.size())
501 commandMerge();
502 // fire change event
503 eventEditTextChange(this);
504 }
505 // for single line and multi+ctrl send event
506 else
507 {
508 eventEditSelectAccept(this);
509 }
510 }
511 }
512 else if (editCmd == EditCommand::AcceptEntry)
513 {
514 eventEditSelectAccept(this);
515 }
516 else if (editCmd == EditCommand::MoveRight || editCmd == EditCommand::MoveRightWord)
517 {
518 if ((mCursorPosition) < mTextLength)
519 {
520 if (editCmd == EditCommand::MoveRightWord)
521 {
522 if (mModePassword)
523 {
524 mCursorPosition = mTextLength;
525 }
526 else
527 {
528 const UString text = getOnlyText();
529 while (mCursorPosition < mTextLength && isWhitespace(text[mCursorPosition]))
530 {
531 mCursorPosition++;
532 }
533 while (mCursorPosition < mTextLength && !isWhitespace(text[mCursorPosition]))
534 {
535 mCursorPosition++;
536 }
537 }
538 }
539 else
540 {
541 mCursorPosition++;
542 }
543 mClientText->setCursorPosition(mCursorPosition);
544 updateSelectText();
545 }
546 // reset selection
547 else if (isTextSelection() && !input.isShiftPressed())
548 {
549 resetSelect();
550 }
551 }
552 else if (editCmd == EditCommand::MoveLeft || editCmd == EditCommand::MoveLeftWord)
553 {
554 if (mCursorPosition != 0)
555 {
556 if (editCmd == EditCommand::MoveLeftWord)
557 {
558 if (mModePassword)
559 {
560 mCursorPosition = 0;
561 }
562 else
563 {
564 const UString text = getOnlyText();
565 while (mCursorPosition > 0 && isWhitespace(text[mCursorPosition - 1]))
566 {
567 mCursorPosition--;
568 }
569 while (mCursorPosition > 0 && !isWhitespace(text[mCursorPosition - 1]))
570 {
571 mCursorPosition--;
572 }
573 }
574 }
575 else
576 {
577 mCursorPosition--;
578 }
579 mClientText->setCursorPosition(mCursorPosition);
580 updateSelectText();
581 }
582 // reset selection
583 else if (isTextSelection() && !input.isShiftPressed())
584 {
585 resetSelect();
586 }
587 }
588 else if (editCmd == EditCommand::MoveUp)
589 {
590 moveCursorVertical(-mClientText->getFontHeight(), 0);
591 }
592 else if (editCmd == EditCommand::MoveDown)
593 {
594 moveCursorVertical(mClientText->getFontHeight(), mTextLength);
595 }
596 else if (editCmd == EditCommand::MoveLineBeginning)
597 {
598 IntPoint point = mClientText->getCursorPoint(mCursorPosition);
600 size_t old = mCursorPosition;
601 mCursorPosition = mClientText->getCursorPosition(point);
602 if (old != mCursorPosition)
603 {
604 mClientText->setCursorPosition(mCursorPosition);
605 updateSelectText();
606 }
607 else if (isTextSelection() && !input.isShiftPressed())
608 {
609 resetSelect();
610 }
611 }
612 else if (editCmd == EditCommand::MoveTextBeginning)
613 {
614 if (0 != mCursorPosition)
615 {
616 mCursorPosition = 0;
617 mClientText->setCursorPosition(mCursorPosition);
618 updateSelectText();
619 }
620 else if (isTextSelection() && !input.isShiftPressed())
621 {
622 resetSelect();
623 }
624 }
625 else if (editCmd == EditCommand::MoveLineEnd)
626 {
627 IntPoint point = mClientText->getCursorPoint(mCursorPosition);
629 size_t old = mCursorPosition;
630 mCursorPosition = mClientText->getCursorPosition(point);
631 if (old != mCursorPosition)
632 {
633 mClientText->setCursorPosition(mCursorPosition);
634 updateSelectText();
635 }
636 else if (isTextSelection() && !input.isShiftPressed())
637 {
638 resetSelect();
639 }
640 }
641 else if (editCmd == EditCommand::MoveTextEnd)
642 {
643 if (mTextLength != mCursorPosition)
644 {
645 mCursorPosition = mTextLength;
646 mClientText->setCursorPosition(mCursorPosition);
647 updateSelectText();
648 }
649 else if (isTextSelection() && !input.isShiftPressed())
650 {
651 resetSelect();
652 }
653 }
654 else if (editCmd == EditCommand::MovePageUp)
655 {
656 int step = (getClientWidget()->getHeight() > mClientText->getFontHeight())
657 ? getClientWidget()->getHeight()
658 : mClientText->getFontHeight();
659 moveCursorVertical(-step, 0);
660 }
661 else if (editCmd == EditCommand::MovePageDown)
662 {
663 int step = (getClientWidget()->getHeight() > mClientText->getFontHeight())
664 ? getClientWidget()->getHeight()
665 : mClientText->getFontHeight();
666 moveCursorVertical(step, mTextLength);
667 }
668 else if ((_key == KeyCode::LeftShift) || (_key == KeyCode::RightShift))
669 {
670 // for correct selection
671 if (mStartSelect == ITEM_NONE)
672 {
673 mStartSelect = mEndSelect = mCursorPosition;
674 }
675 }
676 else
677 {
678 // if control is not pressed, process as text
679 if (!mModeReadOnly && _char != 0)
680 {
681 // reset repeat
682 commandResetRedo();
683
684 // tab only if needed
685 if (_char != '\t' || mTabPrinting)
686 {
687 // attempt to merge two commands
688 size_t size = mVectorUndoChangeInfo.size();
689 // actual operations
690 deleteTextSelect(true);
691 insertText(TextIterator::getTextCharInfo(_char), mCursorPosition, true);
692 // check merge possibility
693 if ((size + 2) == mVectorUndoChangeInfo.size())
694 commandMerge();
695 // fire change event
696 eventEditTextChange(this);
697 }
698 }
699 }
700
701 Base::onKeyButtonPressed(_key, _char);
702 }
703
704 void EditBox::frameEntered(float _frame)
705 {
706 if (mClientText == nullptr)
707 return;
708
709 // in static mode nothing is available
710 if (mModeStatic)
711 return;
712
713 if (mCursorActive)
714 {
715 mCursorTimer += _frame;
716
717 if (mCursorTimer > EDIT_CURSOR_TIMER)
718 {
719 mClientText->setVisibleCursor(!mClientText->isVisibleCursor());
720 while (mCursorTimer > EDIT_CURSOR_TIMER)
721 mCursorTimer -= EDIT_CURSOR_TIMER;
722 }
723 }
724
725 // move cursor by mouse position
726 if (mMouseLeftPressed)
727 {
728 mActionMouseTimer += _frame;
729
730 if (mActionMouseTimer > EDIT_ACTION_MOUSE_TIMER)
731 {
732 IntPoint mouse = InputManager::getInstance().getMousePositionForWidget(getClientWidget());
733 const IntRect& view = getClientWidget()->getAbsoluteRect();
734 mouse.left -= view.left;
735 mouse.top -= view.top;
736 IntPoint point;
737
738 bool action = false;
739
740 // up one line
741 if ((mouse.top < 0) && (mouse.top > -EDIT_ACTION_MOUSE_ZONE))
742 {
743 if ((mouse.left > 0) && (mouse.left <= getClientWidget()->getWidth()))
744 {
745 point = mClientText->getCursorPoint(mCursorPosition);
746 point.top -= mClientText->getFontHeight();
747 action = true;
748 }
749 }
750 // down one line
751 else if (
752 (mouse.top > getClientWidget()->getHeight()) &&
753 (mouse.top < (getClientWidget()->getHeight() + EDIT_ACTION_MOUSE_ZONE)))
754 {
755 if ((mouse.left > 0) && (mouse.left <= getClientWidget()->getWidth()))
756 {
757 point = mClientText->getCursorPoint(mCursorPosition);
758 point.top += mClientText->getFontHeight();
759 action = true;
760 }
761 }
762
763 // left by a small distance
764 if ((mouse.left < 0) && (mouse.left > -EDIT_ACTION_MOUSE_ZONE))
765 {
766 point = mClientText->getCursorPoint(mCursorPosition);
767 point.left -= (int)EDIT_OFFSET_HORZ_CURSOR;
768 action = true;
769 }
770 // right by a small distance
771 else if (
772 (mouse.left > getClientWidget()->getWidth()) &&
773 (mouse.left < (getClientWidget()->getWidth() + EDIT_ACTION_MOUSE_ZONE)))
774 {
775 point = mClientText->getCursorPoint(mCursorPosition);
776 point.left += (int)EDIT_OFFSET_HORZ_CURSOR;
777 action = true;
778 }
779
780 if (action)
781 {
782 size_t old = mCursorPosition;
783 mCursorPosition = mClientText->getCursorPosition(point);
784
785 if (old != mCursorPosition)
786 {
787 mClientText->setCursorPosition(mCursorPosition);
788
789 if (mStartSelect == ITEM_NONE)
790 mStartSelect = old;
791
792 mEndSelect = (size_t)mCursorPosition;
793 if (mStartSelect > mEndSelect)
794 mClientText->setTextSelection(mEndSelect, mStartSelect);
795 else
796 mClientText->setTextSelection(mStartSelect, mEndSelect);
797
798 // try to show cursor
799 updateViewWithCursor();
800 }
801 }
802 // if not in zone, reset
803 else
804 {
805 mActionMouseTimer = 0;
806 }
807
808 while (mActionMouseTimer > EDIT_ACTION_MOUSE_TIMER)
809 mActionMouseTimer -= EDIT_ACTION_MOUSE_TIMER;
810 }
811
812 } // if (mMouseLeftPressed)
813 }
814
815 void EditBox::setTextCursor(size_t _index)
816 {
817 resetSelect();
818
819 // new position
820 if (_index > mTextLength)
821 _index = mTextLength;
822
823 if (mCursorPosition == _index)
824 return;
825
826 mCursorPosition = _index;
827
828 if (mClientText != nullptr)
829 mClientText->setCursorPosition(mCursorPosition);
830
831 updateSelectText();
832 }
833
834 void EditBox::setTextSelection(size_t _start, size_t _end)
835 {
836 if (_start > mTextLength)
837 _start = mTextLength;
838 if (_end > mTextLength)
839 _end = mTextLength;
840
841 mStartSelect = _start;
842 mEndSelect = _end;
843
844 if (mClientText != nullptr)
845 {
846 if (mStartSelect > mEndSelect)
847 mClientText->setTextSelection(mEndSelect, mStartSelect);
848 else
849 mClientText->setTextSelection(mStartSelect, mEndSelect);
850 }
851
852 if (mCursorPosition == mEndSelect)
853 return;
854 // cursor at end of selection
855 mCursorPosition = mEndSelect;
856
857 // update by position
858 if (mClientText != nullptr)
859 mClientText->setCursorPosition(mCursorPosition);
860 }
861
862 bool EditBox::deleteTextSelect(bool _history)
863 {
864 if (!isTextSelection())
865 return false;
866
867 // start and end of selection
868 size_t start = getTextSelectionStart();
869 size_t end = getTextSelectionEnd();
870
871 eraseText(start, end - start, _history);
872
873 return true;
874 }
875
876 void EditBox::resetSelect()
877 {
878 if (mStartSelect != ITEM_NONE)
879 {
880 mStartSelect = ITEM_NONE;
881 if (mClientText != nullptr)
882 mClientText->setTextSelection(0, 0);
883 }
884 }
885
886 void EditBox::commandPosition(size_t _undo, size_t _redo, size_t _length, VectorChangeInfo* _info)
887 {
888 if (_info != nullptr)
889 _info->push_back(TextCommandInfo(_undo, _redo, _length));
890 }
891
892 void EditBox::commandMerge()
893 {
894 if (mVectorUndoChangeInfo.size() < 2)
895 return; // just in case
896 // save the last undo set
897 VectorChangeInfo info = mVectorUndoChangeInfo.back();
898 mVectorUndoChangeInfo.pop_back();
899
900 // merge sequences
901 for (const auto& iter : info)
902 {
903 mVectorUndoChangeInfo.back().push_back(iter);
904 }
905 }
906
907 bool EditBox::commandUndo()
908 {
909 if (mVectorUndoChangeInfo.empty())
910 return false;
911
912 resetSelect();
913
914 // save last undo info
915 VectorChangeInfo info = mVectorUndoChangeInfo.back();
916 // move undo info to redo
917 mVectorUndoChangeInfo.pop_back();
918 mVectorRedoChangeInfo.push_back(info);
919
920 UString text = getRealString();
921
922 // apply undo
923 for (VectorChangeInfo::const_reverse_iterator iter = info.rbegin(); iter != info.rend(); ++iter)
924 {
925 const auto& change = *iter;
926 switch (change.type)
927 {
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;
933 break;
934 }
935 }
936
937 setRealString(text);
938
939 // restore cursor position
940 if (mClientText != nullptr)
941 mClientText->setCursorPosition(mCursorPosition);
942 updateSelectText();
943
944 eventEditTextChange(this);
945
946 return true;
947 }
948
949 bool EditBox::commandRedo()
950 {
951 if (mVectorRedoChangeInfo.empty())
952 return false;
953
954 // reset selection
955 resetSelect();
956
957 // save last undo info
958 VectorChangeInfo info = mVectorRedoChangeInfo.back();
959 // move redo info to undo
960 mVectorRedoChangeInfo.pop_back();
961 mVectorUndoChangeInfo.push_back(info);
962
963 UString text = getRealString();
964
965 // apply redo
966 for (const auto& change : info)
967 {
968 switch (change.type)
969 {
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;
975 break;
976 }
977 }
978
979 setRealString(text);
980
981 // restore cursor position
982 if (mClientText != nullptr)
983 mClientText->setCursorPosition(mCursorPosition);
984 updateSelectText();
985
986 eventEditTextChange(this);
987
988 return true;
989 }
990
991 void EditBox::saveInHistory(VectorChangeInfo* _info)
992 {
993 if (_info == nullptr)
994 {
995 commandResetHistory();
996 return;
997 }
998 // if no change information
999 if (_info->empty())
1000 return;
1001 if ((_info->size() == 1) && (_info->back().type == TextCommandInfo::COMMAND_POSITION))
1002 return;
1003
1004 mVectorUndoChangeInfo.push_back(*_info);
1005 // check for maximum size
1006 if (mVectorUndoChangeInfo.size() > EDIT_MAX_UNDO)
1007 mVectorUndoChangeInfo.pop_front();
1008 }
1009
1010 void EditBox::moveCursorVertical(int _delta, size_t _clampTarget)
1011 {
1012 IntPoint point = mClientText->getCursorPoint(mCursorPosition);
1013 point.top += _delta;
1014 size_t old = mCursorPosition;
1015 mCursorPosition = mClientText->getCursorPosition(point);
1016 if (old == mCursorPosition)
1017 {
1018 if (mCursorPosition != _clampTarget)
1019 {
1020 mCursorPosition = _clampTarget;
1021 mClientText->setCursorPosition(mCursorPosition);
1022 updateSelectText();
1023 }
1024 else if (isTextSelection() && !InputManager::getInstance().isShiftPressed())
1025 {
1026 resetSelect();
1027 }
1028 }
1029 else
1030 {
1031 mClientText->setCursorPosition(mCursorPosition);
1032 updateSelectText();
1033 }
1034 }
1035
1036 // returns text
1037 UString EditBox::getTextInterval(size_t _start, size_t _count) const
1038 {
1039 if (_start > mTextLength)
1040 _start = mTextLength;
1041 size_t end = _start + _count;
1042
1043 TextIterator iterator(getRealString());
1044
1045 UString colour;
1046 if (mClientText != nullptr)
1047 colour = TextIterator::convertTagColour(mClientText->getTextColour());
1048
1049 bool need_colour = true;
1050
1051 while (iterator.moveNext())
1052 {
1053 size_t pos = iterator.getPosition();
1054
1055 if (pos < _start)
1056 {
1057 iterator.getTagColour(colour);
1058 continue;
1059 }
1060
1061 if (pos == _start)
1062 {
1063 need_colour = !iterator.getTagColour(colour);
1064 iterator.saveStartPoint();
1065 }
1066
1067 else if (pos == end)
1068 break;
1069 }
1070
1071 if (need_colour)
1072 return colour + iterator.getFromStart();
1073 return iterator.getFromStart();
1074 }
1075
1076 void EditBox::_setTextColour(size_t _start, size_t _count, const Colour& _colour, bool _history)
1077 {
1078 if (_count == 0 || _start >= mTextLength)
1079 return;
1080 _count = std::min(_count, mTextLength - _start);
1081
1082 VectorChangeInfo* history = nullptr;
1083 if (_history)
1084 history = new VectorChangeInfo();
1085
1086 size_t end = _start + _count;
1087
1088 TextIterator iterator(getRealString(), history);
1089
1090 UString colour;
1091 if (mClientText != nullptr)
1092 colour = TextIterator::convertTagColour(mClientText->getTextColour());
1093
1094 while (iterator.moveNext())
1095 {
1096 size_t pos = iterator.getPosition();
1097
1098 iterator.getTagColour(colour);
1099
1100 if (pos < _start)
1101 continue;
1102
1103 if (pos == _start)
1104 iterator.setTagColour(_colour);
1105
1106 else if (pos < end)
1107 iterator.clearTagColour();
1108
1109 else if (pos == end)
1110 {
1111 iterator.setTagColour(colour);
1112 break;
1113 }
1114 }
1115
1116 commandPosition(_start, _start + _count, mTextLength, history);
1117
1118 saveInHistory(history);
1119 delete history;
1120
1121 setRealString(iterator.getText());
1122 }
1123
1124 void EditBox::setTextSelectColour(const Colour& _colour, bool _history)
1125 {
1126 if (!isTextSelection())
1127 return;
1128 // start and end of selection
1129 size_t start = getTextSelectionStart();
1130 size_t end = getTextSelectionEnd();
1131 _setTextColour(start, end - start, _colour, _history);
1132 }
1133
1134 UString EditBox::getTextSelection() const
1135 {
1136 if (!isTextSelection())
1137 return {};
1138 size_t start = getTextSelectionStart();
1139 size_t end = getTextSelectionEnd();
1140 return getTextInterval(start, end - start);
1141 }
1142
1143 void EditBox::setEditPassword(bool _password)
1144 {
1145 if (mModePassword == _password)
1146 return;
1147 if (_password && mModeMultiline)
1148 {
1149 MYGUI_LOG(Warning, "EditBox password mode is single-line; disabling multiline mode");
1150 setEditMultiLine(false);
1151 }
1152 mModePassword = _password;
1153
1154 if (mModePassword)
1155 {
1156 if (mClientText != nullptr)
1157 {
1158 mPasswordText = mClientText->getCaption();
1159 mClientText->setCaption(UString(mTextLength, mCharPassword));
1160 }
1161 }
1162 else
1163 {
1164 if (mClientText != nullptr)
1165 {
1166 mClientText->setCaption(mPasswordText);
1167 mPasswordText.clear();
1168 }
1169 }
1170 updateView();
1171 commandResetHistory();
1172 }
1173
1174 void EditBox::setText(const UString& _caption, bool _history)
1175 {
1176 resetSelect();
1177
1178 VectorChangeInfo* history = nullptr;
1179 if (_history)
1180 history = new VectorChangeInfo();
1181
1182 TextIterator iterator(getRealString(), history);
1183
1184 iterator.setText(_caption, mModeMultiline || mModeWordWrap);
1185
1186 if (mOverflowToTheLeft)
1187 {
1188 iterator.cutMaxLengthFromBeginning(mMaxTextLength);
1189 }
1190 else
1191 {
1192 iterator.cutMaxLength(mMaxTextLength);
1193 }
1194
1195 size_t old = mTextLength;
1196 mCursorPosition = mTextLength = iterator.getSize();
1197
1198 commandPosition(0, mTextLength, old, history);
1199
1200 saveInHistory(history);
1201 delete history;
1202
1203 setRealString(iterator.getText());
1204
1205 if (mClientText != nullptr)
1206 mClientText->setCursorPosition(mCursorPosition);
1207 updateSelectText();
1208 }
1209
1210 void EditBox::insertText(const UString& _text, size_t _start, bool _history)
1211 {
1212 resetSelect();
1213
1214 // if string is empty or max size reached
1215 if (_text.empty())
1216 return;
1217
1218 if ((!mOverflowToTheLeft) && (mTextLength == mMaxTextLength))
1219 return;
1220
1221 VectorChangeInfo* history = nullptr;
1222 if (_history)
1223 history = new VectorChangeInfo();
1224
1225 TextIterator iterator(getRealString(), history);
1226
1227 UString colour;
1228 if (mClientText != nullptr)
1229 colour = TextIterator::convertTagColour(mClientText->getTextColour());
1230 // whether text tag is needed
1231 // TODO rework through TextIterator to decouple tag concept from edit
1232 bool need_colour = ((_text.size() > 6) && (_text[0] == U'#') && (_text[1] != U'#'));
1233
1234 while (iterator.moveNext())
1235 {
1236 size_t pos = iterator.getPosition();
1237
1238 if (need_colour)
1239 iterator.getTagColour(colour);
1240
1241 if (pos == _start)
1242 break;
1243 }
1244
1245 if (need_colour)
1246 iterator.setTagColour(colour);
1247
1248 iterator.insertText(_text, mModeMultiline || mModeWordWrap);
1249
1250 if (mOverflowToTheLeft)
1251 {
1252 iterator.cutMaxLengthFromBeginning(mMaxTextLength);
1253 }
1254 else
1255 {
1256 iterator.cutMaxLength(mMaxTextLength);
1257 }
1258
1259 size_t old = mTextLength;
1260 mTextLength = iterator.getSize();
1261 mCursorPosition += mTextLength - old;
1262
1263 commandPosition(_start, _start + mTextLength - old, old, history);
1264
1265 saveInHistory(history);
1266 delete history;
1267
1268 setRealString(iterator.getText());
1269
1270 // update by position
1271 if (mClientText != nullptr)
1272 mClientText->setCursorPosition(mCursorPosition);
1273 updateSelectText();
1274 }
1275
1276 void EditBox::eraseText(size_t _start, size_t _count, bool _history)
1277 {
1278 if (_count == 0)
1279 return;
1280
1281 resetSelect();
1282
1283 VectorChangeInfo* history = nullptr;
1284 if (_history)
1285 history = new VectorChangeInfo();
1286
1287 TextIterator iterator(getRealString(), history);
1288
1289 UString colour;
1290 size_t end = _start + _count;
1291 bool need_colour = false;
1292
1293 while (iterator.moveNext())
1294 {
1295 size_t pos = iterator.getPosition();
1296
1297 if (pos < _start)
1298 {
1299 iterator.getTagColour(colour);
1300 continue;
1301 }
1302
1303 if (pos == _start)
1304 {
1305 if (!colour.empty())
1306 {
1307 need_colour = true;
1308 colour.clear();
1309 }
1310 iterator.getTagColour(colour);
1311 iterator.saveStartPoint();
1312 }
1313
1314 else if (pos < end)
1315 {
1316 iterator.getTagColour(colour);
1317 }
1318
1319 else if (pos == end)
1320 {
1321 if (!colour.empty())
1322 need_colour = true;
1323 if (iterator.getTagColour(colour))
1324 need_colour = false;
1325
1326 break;
1327 }
1328 }
1329
1330 iterator.eraseFromStart();
1331 if (need_colour)
1332 iterator.setTagColour(colour);
1333
1334 commandPosition(_start + _count, _start, mTextLength, history);
1335
1336 mCursorPosition = _start;
1337 mTextLength -= _count;
1338
1339 saveInHistory(history);
1340 delete history;
1341
1342 setRealString(iterator.getText());
1343
1344 // update by position
1345 if (mClientText != nullptr)
1346 mClientText->setCursorPosition(mCursorPosition);
1347 updateSelectText();
1348 }
1349
1350 void EditBox::commandCut()
1351 {
1352 commandCopy();
1353 if (isTextSelection() && !mModePassword && !mModeReadOnly)
1354 {
1355 deleteTextSelect(true);
1356 eventEditTextChange(this);
1357 }
1358 }
1359
1360 void EditBox::commandCopy() const
1361 {
1362 if (isTextSelection() && (!mModePassword))
1363 ClipboardManager::getInstance().setClipboardData(EDIT_CLIPBOARD_TYPE_TEXT, getTextSelection());
1364 else
1365 ClipboardManager::getInstance().clearClipboardData(EDIT_CLIPBOARD_TYPE_TEXT);
1366 }
1367
1368 void EditBox::commandPaste()
1369 {
1370 std::string clipboard = ClipboardManager::getInstance().getClipboardData(EDIT_CLIPBOARD_TYPE_TEXT);
1371 if ((!mModeReadOnly) && (!clipboard.empty()))
1372 {
1373 // attempt to merge two commands
1374 size_t size = mVectorUndoChangeInfo.size();
1375 deleteTextSelect(true);
1376 insertText(clipboard, mCursorPosition, true);
1377 // check merge possibility
1378 if ((size + 2) == mVectorUndoChangeInfo.size())
1379 commandMerge();
1380 eventEditTextChange(this);
1381 }
1382 }
1383
1384 const UString& EditBox::getRealString() const
1385 {
1386 if (mModePassword)
1387 return mPasswordText;
1388 if (mClientText == nullptr)
1389 return mPasswordText;
1390
1391 return mClientText->getCaption();
1392 }
1393
1394 void EditBox::setRealString(const UString& _caption)
1395 {
1396 if (mModePassword)
1397 {
1398 mPasswordText = _caption;
1399 if (mClientText != nullptr)
1400 mClientText->setCaption(UString(mTextLength, mCharPassword));
1401 }
1402 else
1403 {
1404 if (mClientText != nullptr)
1405 mClientText->setCaption(_caption);
1406 }
1407 }
1408
1409 void EditBox::setPasswordChar(Char _char)
1410 {
1411 mCharPassword = _char;
1412 if (mModePassword)
1413 {
1414 if (mClientText != nullptr)
1415 mClientText->setCaption(UString(mTextLength, mCharPassword));
1416 }
1417 }
1418
1419 void EditBox::updateEditState()
1420 {
1421 if (!getInheritedEnabled())
1422 {
1423 _setWidgetState("disabled");
1424 }
1425 else if (mIsPressed)
1426 {
1427 if (mIsFocus)
1428 _setWidgetState("pushed");
1429 else
1430 _setWidgetState("normal_checked");
1431 }
1432 else if (mIsFocus)
1433 {
1434 _setWidgetState("highlighted");
1435 }
1436 else
1437 {
1438 _setWidgetState("normal");
1439 }
1440 }
1441
1442 void EditBox::setPosition(const IntPoint& _point)
1443 {
1444 Base::setPosition(_point);
1445 }
1446
1447 void EditBox::eraseView()
1448 {
1449 // Reflow wrapped text after changes to the available width or font.
1450 if (mModeWordWrap)
1451 {
1452 if (mClientText != nullptr)
1453 mClientText->_invalidateTextLayout();
1454 }
1455
1456 updateView();
1457 }
1458
1459 void EditBox::setSize(const IntSize& _size)
1460 {
1461 Base::setSize(_size);
1462
1463 eraseView();
1464 }
1465
1466 void EditBox::setCoord(const IntCoord& _coord)
1467 {
1468 Base::setCoord(_coord);
1469
1470 eraseView();
1471 }
1472
1473 void EditBox::setCaption(const UString& _value)
1474 {
1475 setText(_value, false);
1476 }
1477
1478 const UString& EditBox::getCaption() const
1479 {
1480 return getRealString();
1481 }
1482
1483 void EditBox::updateSelectText()
1484 {
1485 if (!mModeStatic)
1486 {
1487 InputManager& input = InputManager::getInstance();
1488 if ((input.isShiftPressed()) && (mStartSelect != ITEM_NONE))
1489 {
1490 // change selection
1491 mEndSelect = (size_t)mCursorPosition;
1492 if (mClientText != nullptr)
1493 {
1494 if (mStartSelect > mEndSelect)
1495 mClientText->setTextSelection(mEndSelect, mStartSelect);
1496 else
1497 mClientText->setTextSelection(mStartSelect, mEndSelect);
1498 }
1499 }
1500 else if (mStartSelect != ITEM_NONE)
1501 {
1502 // reset shift
1503 mStartSelect = ITEM_NONE;
1504 if (mClientText != nullptr)
1505 mClientText->setTextSelection(0, 0);
1506 }
1507 }
1508
1509 // try to show cursor
1510 updateViewWithCursor();
1511 }
1512
1513 void EditBox::setTextAlign(Align _value)
1514 {
1515 Base::setTextAlign(_value);
1516
1517 if (mClientText != nullptr)
1518 mClientText->setTextAlign(_value);
1519
1520 // since we manage offsets ourselves
1521 updateView();
1522 }
1523
1524 void EditBox::setTextColour(const Colour& _value)
1525 {
1526 Base::setTextColour(_value);
1527
1528 if (mClientText != nullptr)
1529 mClientText->setTextColour(_value);
1530 }
1531
1532 IntCoord EditBox::getTextRegion() const
1533 {
1534 if (mClientText != nullptr)
1535 return mClientText->getCoord();
1536 return Base::getTextRegion();
1537 }
1538
1539 IntSize EditBox::getTextSize() const
1540 {
1541 if (mClientText != nullptr)
1542 return mClientText->getTextSize();
1543 return Base::getTextSize();
1544 }
1545
1546 void EditBox::notifyScrollChangePosition(ScrollBar* _sender, size_t _position)
1547 {
1548 if (mClientText == nullptr)
1549 return;
1550
1551 if (_sender == mVScroll)
1552 {
1553 IntPoint point = mClientText->getViewOffset();
1554 point.top = _position;
1555 mClientText->setViewOffset(point);
1556 }
1557 else if (_sender == mHScroll)
1558 {
1559 IntPoint point = mClientText->getViewOffset();
1560 point.left = _position;
1561 mClientText->setViewOffset(point);
1562 }
1563 }
1564
1565 void EditBox::notifyMouseWheel(Widget* _sender, int _rel)
1566 {
1567 if (mClientText == nullptr)
1568 return;
1569
1570 if (mVRange != 0)
1571 {
1572 IntPoint point = mClientText->getViewOffset();
1573 int offset = point.top;
1574 if (_rel < 0)
1575 offset += EDIT_MOUSE_WHEEL;
1576 else
1577 offset -= EDIT_MOUSE_WHEEL;
1578
1579 offset = std::clamp(offset, 0, int(mVRange));
1580
1581 if (offset != point.top)
1582 {
1583 point.top = offset;
1584 if (mVScroll != nullptr)
1585 mVScroll->setScrollPosition(offset);
1586 mClientText->setViewOffset(point);
1587 }
1588 }
1589 else if (mHRange != 0)
1590 {
1591 IntPoint point = mClientText->getViewOffset();
1592 int offset = point.left;
1593 if (_rel < 0)
1594 offset += EDIT_MOUSE_WHEEL;
1595 else
1596 offset -= EDIT_MOUSE_WHEEL;
1597
1598 offset = std::clamp(offset, 0, int(mHRange));
1599
1600 if (offset != point.left)
1601 {
1602 point.left = offset;
1603 if (mHScroll != nullptr)
1604 mHScroll->setScrollPosition(offset);
1605 mClientText->setViewOffset(point);
1606 }
1607 }
1608 }
1609
1610 void EditBox::setEditWordWrap(bool _value)
1611 {
1612 mModeWordWrap = _value;
1613 if (mClientText != nullptr)
1614 mClientText->setWordWrap(mModeWordWrap);
1615
1616 eraseView();
1617 }
1618
1619 void EditBox::setFontName(std::string_view _value)
1620 {
1621 Base::setFontName(_value);
1622
1623 if (mClientText != nullptr)
1624 mClientText->setFontName(_value);
1625
1626 eraseView();
1627 }
1628
1629 void EditBox::setFontHeight(int _value)
1630 {
1631 Base::setFontHeight(_value);
1632
1633 if (mClientText != nullptr)
1634 mClientText->setFontHeight(_value);
1635
1636 eraseView();
1637 }
1638
1639 int EditBox::getFontHeight() const
1640 {
1641 return (nullptr == mClientText) ? 0 : mClientText->getFontHeight();
1642 }
1643
1644 void EditBox::updateView()
1645 {
1646 updateScrollSize();
1647 updateScrollPosition();
1648 }
1649
1650 void EditBox::updateViewWithCursor()
1651 {
1652 updateScrollSize();
1653 updateCursorPosition();
1654 updateScrollPosition();
1655 }
1656
1657 void EditBox::updateCursorPosition()
1658 {
1659 if (mClientText == nullptr || getClientWidget() == nullptr)
1660 return;
1661
1662 // text context size
1663 IntSize textSize = mClientText->getTextSize();
1664
1665 IntPoint point = mClientText->getViewOffset();
1666 IntPoint offset = point;
1667
1668 IntRect cursor = mClientText->getCursorRect(mCursorPosition);
1669 cursor.right++;
1670
1671 const IntRect& view = getClientWidget()->getAbsoluteRect();
1672
1673 // check and show cursor
1674 if (!view.inside(cursor))
1675 {
1676 // horizontal offset
1677 if (textSize.width > view.width())
1678 {
1679 if (cursor.left < view.left)
1680 {
1681 offset.left = point.left - (view.left - cursor.left);
1682 // add offset only if cursor won't jump over
1683 if ((float(view.width()) - EDIT_OFFSET_HORZ_CURSOR) > EDIT_OFFSET_HORZ_CURSOR)
1684 offset.left -= int(EDIT_OFFSET_HORZ_CURSOR);
1685 }
1686 else if (cursor.right > view.right)
1687 {
1688 offset.left = point.left + (cursor.right - view.right);
1689 // add offset only if cursor won't jump over
1690 if ((float(view.width()) - EDIT_OFFSET_HORZ_CURSOR) > EDIT_OFFSET_HORZ_CURSOR)
1691 offset.left += int(EDIT_OFFSET_HORZ_CURSOR);
1692 }
1693 }
1694
1695 // vertical offset
1696 if (textSize.height > view.height())
1697 {
1698 int delta = 0;
1699 if (cursor.height() > view.height())
1700 {
1701 // if text is bigger than edit height then place it in center
1702 delta = ((cursor.bottom - view.bottom) - (view.top - cursor.top)) / 2;
1703 }
1704 else if (cursor.top < view.top)
1705 {
1706 delta = -(view.top - cursor.top);
1707 }
1708 else if (cursor.bottom > view.bottom)
1709 {
1710 delta = (cursor.bottom - view.bottom);
1711 }
1712 offset.top = point.top + delta;
1713 }
1714 }
1715
1716 if (offset != point)
1717 {
1718 mClientText->setViewOffset(offset);
1719 if (mVScroll != nullptr)
1720 mVScroll->setScrollPosition(offset.top);
1721 if (mHScroll != nullptr)
1722 mHScroll->setScrollPosition(offset.left);
1723 }
1724 }
1725
1726 void EditBox::setContentPosition(const IntPoint& _point)
1727 {
1728 if (mClientText != nullptr)
1729 mClientText->setViewOffset(_point);
1730 }
1731
1732 IntSize EditBox::getViewSize() const
1733 {
1734 if (mClientText != nullptr)
1735 return mClientText->getSize();
1736 return ScrollViewBase::getViewSize();
1737 }
1738
1739 IntSize EditBox::getContentSize() const
1740 {
1741 if (mClientText != nullptr)
1742 return mClientText->getTextSize();
1743 return ScrollViewBase::getContentSize();
1744 }
1745
1746 size_t EditBox::getVScrollPage() const
1747 {
1748 if (mClientText != nullptr)
1749 return (size_t)mClientText->getFontHeight();
1750 return ScrollViewBase::getVScrollPage();
1751 }
1752
1753 size_t EditBox::getHScrollPage() const
1754 {
1755 if (mClientText != nullptr)
1756 return (size_t)mClientText->getFontHeight();
1757 return ScrollViewBase::getHScrollPage();
1758 }
1759
1760 IntPoint EditBox::getContentPosition() const
1761 {
1762 if (mClientText != nullptr)
1763 return mClientText->getViewOffset();
1764 return ScrollViewBase::getContentPosition();
1765 }
1766
1767 Align EditBox::getContentAlign() const
1768 {
1769 if (mClientText != nullptr)
1770 return mClientText->getTextAlign();
1771 return ScrollViewBase::getContentAlign();
1772 }
1773
1774 void EditBox::setTextIntervalColour(size_t _start, size_t _count, const Colour& _colour)
1775 {
1776 _setTextColour(_start, _count, _colour, false);
1777 }
1778
1779 size_t EditBox::getTextSelectionStart() const
1780 {
1781 return (mStartSelect == ITEM_NONE) ? ITEM_NONE : (mStartSelect > mEndSelect ? mEndSelect : mStartSelect);
1782 }
1783
1784 size_t EditBox::getTextSelectionEnd() const
1785 {
1786 return (mStartSelect == ITEM_NONE) ? ITEM_NONE : (mStartSelect > mEndSelect ? mStartSelect : mEndSelect);
1787 }
1788
1789 bool EditBox::isTextSelection() const
1790 {
1791 return (mStartSelect != ITEM_NONE) && (mStartSelect != mEndSelect);
1792 }
1793
1794 void EditBox::deleteTextSelection()
1795 {
1796 deleteTextSelect(false);
1797 }
1798
1799 void EditBox::setTextSelectionColour(const Colour& _colour)
1800 {
1801 setTextSelectColour(_colour, false);
1802 }
1803
1804 size_t EditBox::getTextSelectionLength() const
1805 {
1806 if (!isTextSelection())
1807 return 0;
1808 return getTextSelectionEnd() - getTextSelectionStart();
1809 }
1810
1811 void EditBox::setOnlyText(const UString& _text)
1812 {
1813 setText(TextIterator::toTagsString(_text), false);
1814 }
1815
1816 UString EditBox::getOnlyText() const
1817 {
1818 return TextIterator::getOnlyText(getRealString());
1819 }
1820
1821 void EditBox::insertText(const UString& _text, size_t _index)
1822 {
1823 insertText(_text, _index, false);
1824 }
1825
1826 void EditBox::addText(const UString& _text)
1827 {
1828 insertText(_text, ITEM_NONE, false);
1829 }
1830
1831 void EditBox::eraseText(size_t _start, size_t _count)
1832 {
1833 eraseText(_start, _count, false);
1834 }
1835
1836 void EditBox::setEditReadOnly(bool _value)
1837 {
1838 mModeReadOnly = _value;
1839 commandResetHistory();
1840 }
1841
1842 void EditBox::setEditMultiLine(bool _value)
1843 {
1844 if (_value && mModePassword)
1845 MYGUI_LOG(Warning, "EditBox multiline mode is ignored while password mode is enabled");
1846 mModeMultiline = _value && !mModePassword;
1847 // just in case, to remove line breaks
1848 if (!mModeMultiline)
1849 {
1850 setText(getRealString(), false);
1851 }
1852 else
1853 {
1854 updateView();
1855 }
1856 commandResetHistory();
1857 }
1858
1859 void EditBox::setEditStatic(bool _value)
1860 {
1861 mModeStatic = _value;
1862 resetSelect();
1863
1864 if (getClientWidget() != nullptr)
1865 {
1866 if (mModeStatic)
1867 getClientWidget()->setPointer(std::string_view{});
1868 else
1869 getClientWidget()->setPointer(mOriginalPointer);
1870 }
1871 }
1872
1873 void EditBox::setPasswordChar(const UString& _value)
1874 {
1875 if (!_value.empty())
1876 setPasswordChar(_value[0]);
1877 }
1878
1879 void EditBox::setVisibleVScroll(bool _value)
1880 {
1881 mVisibleVScroll = _value;
1882 updateView();
1883 }
1884
1885 void EditBox::setVisibleHScroll(bool _value)
1886 {
1887 mVisibleHScroll = _value;
1888 updateView();
1889 }
1890
1891 size_t EditBox::getVScrollRange() const
1892 {
1893 return mVRange + 1;
1894 }
1895
1896 size_t EditBox::getVScrollPosition() const
1897 {
1898 return mClientText == nullptr ? 0 : mClientText->getViewOffset().top;
1899 }
1900
1901 void EditBox::setVScrollPosition(size_t _index)
1902 {
1903 if (mClientText == nullptr)
1904 return;
1905
1906 if (_index > mVRange)
1907 _index = mVRange;
1908
1909 IntPoint point = mClientText->getViewOffset();
1910 point.top = _index;
1911
1912 mClientText->setViewOffset(point);
1913 if (mVScroll != nullptr)
1914 mVScroll->setScrollPosition(point.top);
1915 }
1916
1917 size_t EditBox::getHScrollRange() const
1918 {
1919 return mHRange + 1;
1920 }
1921
1922 size_t EditBox::getHScrollPosition() const
1923 {
1924 return mClientText == nullptr ? 0 : mClientText->getViewOffset().left;
1925 }
1926
1927 void EditBox::setHScrollPosition(size_t _index)
1928 {
1929 if (mClientText == nullptr)
1930 return;
1931
1932 if (_index > mHRange)
1933 _index = mHRange;
1934
1935 IntPoint point = mClientText->getViewOffset();
1936 point.left = _index;
1937
1938 mClientText->setViewOffset(point);
1939 if (mHScroll != nullptr)
1940 mHScroll->setScrollPosition(point.left);
1941 }
1942
1943 bool EditBox::getInvertSelected() const
1944 {
1945 return mClientText == nullptr ? false : mClientText->getInvertSelected();
1946 }
1947
1948 void EditBox::setInvertSelected(bool _value)
1949 {
1950 if (mClientText != nullptr)
1951 mClientText->setInvertSelected(_value);
1952 }
1953
1954 void EditBox::setPropertyOverride(std::string_view _key, std::string_view _value)
1955 {
1957 if (_key == "CursorPosition")
1958 setTextCursor(utility::parseValue<size_t>(_value));
1959
1961 else if (_key == "TextSelect")
1962 setTextSelection(
1963 utility::parseValue<types::TSize<size_t>>(_value).width,
1964 utility::parseValue<types::TSize<size_t>>(_value).height);
1965
1967 else if (_key == "ReadOnly")
1968 setEditReadOnly(utility::parseValue<bool>(_value));
1969
1971 else if (_key == "Password")
1972 setEditPassword(utility::parseValue<bool>(_value));
1973
1975 else if (_key == "MultiLine")
1976 setEditMultiLine(utility::parseValue<bool>(_value));
1977
1979 else if (_key == "PasswordChar")
1980 setPasswordChar(UString(_value));
1981
1983 else if (_key == "MaxTextLength")
1984 setMaxTextLength(utility::parseValue<size_t>(_value));
1985
1987 else if (_key == "OverflowToTheLeft")
1988 setOverflowToTheLeft(utility::parseValue<bool>(_value));
1989
1991 else if (_key == "Static")
1992 setEditStatic(utility::parseValue<bool>(_value));
1993
1995 else if (_key == "VisibleVScroll")
1996 setVisibleVScroll(utility::parseValue<bool>(_value));
1997
1999 else if (_key == "VisibleHScroll")
2000 setVisibleHScroll(utility::parseValue<bool>(_value));
2001
2003 else if (_key == "WordWrap")
2004 setEditWordWrap(utility::parseValue<bool>(_value));
2005
2007 else if (_key == "TabPrinting")
2008 setTabPrinting(utility::parseValue<bool>(_value));
2009
2011 else if (_key == "InvertSelected")
2012 setInvertSelected(utility::parseValue<bool>(_value));
2013
2014 else
2015 {
2016 Base::setPropertyOverride(_key, _value);
2017 return;
2018 }
2019
2020 eventChangeProperty(this, _key, _value);
2021 }
2022
2023 size_t EditBox::getTextCursor() const
2024 {
2025 return mCursorPosition;
2026 }
2027
2028 size_t EditBox::getTextLength() const
2029 {
2030 return mTextLength;
2031 }
2032
2033 void EditBox::setOverflowToTheLeft(bool _value)
2034 {
2035 mOverflowToTheLeft = _value;
2036 }
2037
2038 bool EditBox::getOverflowToTheLeft() const
2039 {
2040 return mOverflowToTheLeft;
2041 }
2042
2043 void EditBox::setMaxTextLength(size_t _value)
2044 {
2045 mMaxTextLength = _value;
2046 }
2047
2048 size_t EditBox::getMaxTextLength() const
2049 {
2050 return mMaxTextLength;
2051 }
2052
2053 bool EditBox::getEditReadOnly() const
2054 {
2055 return mModeReadOnly;
2056 }
2057
2058 bool EditBox::getEditPassword() const
2059 {
2060 return mModePassword;
2061 }
2062
2063 bool EditBox::getEditMultiLine() const
2064 {
2065 return mModeMultiline;
2066 }
2067
2068 bool EditBox::getEditStatic() const
2069 {
2070 return mModeStatic;
2071 }
2072
2073 Char EditBox::getPasswordChar() const
2074 {
2075 return mCharPassword;
2076 }
2077
2078 bool EditBox::getEditWordWrap() const
2079 {
2080 return mModeWordWrap;
2081 }
2082
2083 void EditBox::setTabPrinting(bool _value)
2084 {
2085 mTabPrinting = _value;
2086 }
2087
2088 bool EditBox::getTabPrinting() const
2089 {
2090 return mTabPrinting;
2091 }
2092
2093 bool EditBox::isVisibleVScroll() const
2094 {
2095 return mVisibleVScroll;
2096 }
2097
2098 bool EditBox::isVisibleHScroll() const
2099 {
2100 return mVisibleHScroll;
2101 }
2102
2103 void EditBox::commandResetRedo()
2104 {
2105 mVectorRedoChangeInfo.clear();
2106 }
2107
2108 void EditBox::commandResetHistory()
2109 {
2110 mVectorRedoChangeInfo.clear();
2111 mVectorUndoChangeInfo.clear();
2112 }
2113
2114 void EditBox::setTextShadowColour(const Colour& _value)
2115 {
2116 Base::setTextShadowColour(_value);
2117
2118 if (mClientText != nullptr)
2119 mClientText->setShadowColour(_value);
2120 }
2121
2122 void EditBox::setTextShadow(bool _value)
2123 {
2124 Base::setTextShadow(_value);
2125
2126 if (mClientText != nullptr)
2127 mClientText->setShadow(_value);
2128 }
2129
2130} // namespace MyGUI
#define MYGUI_LOG(level, text)
const int EDIT_MOUSE_WHEEL
types::TRect< int > IntRect
Definition MyGUI_Types.h:32
const int EDIT_CURSOR_MIN_POSITION
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
std::vector< TextCommandInfo > VectorChangeInfo
const size_t EDIT_MAX_UNDO
const int EDIT_CURSOR_MAX_POSITION
unsigned int Char
Definition MyGUI_Types.h:49
constexpr size_t ITEM_NONE
const float EDIT_CURSOR_TIMER
static EditCommand ToEditCommand(KeyCode key, const InputManager &input)
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
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