| 1 | //============================================================================ |
| 2 | // |
| 3 | // SSSS tt lll lll |
| 4 | // SS SS tt ll ll |
| 5 | // SS tttttt eeee ll ll aaaa |
| 6 | // SSSS tt ee ee ll ll aa |
| 7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
| 8 | // SS SS tt ee ll ll aa aa |
| 9 | // SSSS ttt eeeee llll llll aaaaa |
| 10 | // |
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
| 12 | // and the Stella Team |
| 13 | // |
| 14 | // See the file "License.txt" for information on usage and redistribution of |
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 16 | //============================================================================ |
| 17 | |
| 18 | #include "OSystem.hxx" |
| 19 | #include "FBSurface.hxx" |
| 20 | #include "Dialog.hxx" |
| 21 | #include "Font.hxx" |
| 22 | #include "EditTextWidget.hxx" |
| 23 | |
| 24 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 25 | EditTextWidget::EditTextWidget(GuiObject* boss, const GUI::Font& font, |
| 26 | int x, int y, int w, int h, const string& text) |
| 27 | : EditableWidget(boss, font, x, y, w, h + 2, text), |
| 28 | _changed(false) |
| 29 | { |
| 30 | _flags = Widget::FLAG_ENABLED | Widget::FLAG_CLEARBG | Widget::FLAG_RETAIN_FOCUS; |
| 31 | |
| 32 | startEditMode(); // We're always in edit mode |
| 33 | } |
| 34 | |
| 35 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 36 | void EditTextWidget::setText(const string& str, bool changed) |
| 37 | { |
| 38 | EditableWidget::setText(str, changed); |
| 39 | _backupString = str; |
| 40 | _changed = changed; |
| 41 | } |
| 42 | |
| 43 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 44 | void EditTextWidget::handleMouseEntered() |
| 45 | { |
| 46 | setFlags(Widget::FLAG_HILITED); |
| 47 | setDirty(); |
| 48 | } |
| 49 | |
| 50 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 51 | void EditTextWidget::handleMouseLeft() |
| 52 | { |
| 53 | clearFlags(Widget::FLAG_HILITED); |
| 54 | setDirty(); |
| 55 | } |
| 56 | |
| 57 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 58 | void EditTextWidget::handleMouseDown(int x, int y, MouseButton b, int clickCount) |
| 59 | { |
| 60 | if(!isEditable()) |
| 61 | return; |
| 62 | |
| 63 | x += _editScrollOffset; |
| 64 | |
| 65 | int width = 0; |
| 66 | uInt32 i; |
| 67 | |
| 68 | for (i = 0; i < editString().size(); ++i) |
| 69 | { |
| 70 | width += _font.getCharWidth(editString()[i]); |
| 71 | if (width >= x) |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if (setCaretPos(i)) |
| 76 | setDirty(); |
| 77 | } |
| 78 | |
| 79 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 80 | void EditTextWidget::drawWidget(bool hilite) |
| 81 | { |
| 82 | FBSurface& s = _boss->dialog().surface(); |
| 83 | bool onTop = _boss->dialog().isOnTop(); |
| 84 | |
| 85 | // Highlight changes |
| 86 | if(_changed && onTop) |
| 87 | s.fillRect(_x, _y, _w, _h, kDbgChangedColor); |
| 88 | else if(!isEditable() || !isEnabled()) |
| 89 | s.fillRect(_x, _y, _w, _h, onTop ? kDlgColor : kBGColorLo); |
| 90 | |
| 91 | // Draw a thin frame around us. |
| 92 | s.frameRect(_x, _y, _w, _h, hilite && isEditable() && isEnabled() ? kWidColorHi : kColor); |
| 93 | |
| 94 | // Draw the text |
| 95 | adjustOffset(); |
| 96 | s.drawString(_font, editString(), _x + 2, _y + 2, getEditRect().w(), getEditRect().h(), |
| 97 | _changed && onTop && isEnabled() |
| 98 | ? kDbgChangedTextColor |
| 99 | : onTop && isEnabled() ? _textcolor : kColor, |
| 100 | TextAlign::Left, isEditable() ? -_editScrollOffset : 0, !isEditable()); |
| 101 | |
| 102 | // Draw the caret |
| 103 | drawCaret(); |
| 104 | } |
| 105 | |
| 106 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 107 | Common::Rect EditTextWidget::getEditRect() const |
| 108 | { |
| 109 | return Common::Rect(2, 1, _w - 2, _h); |
| 110 | } |
| 111 | |
| 112 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 113 | void EditTextWidget::lostFocusWidget() |
| 114 | { |
| 115 | // If we loose focus, 'commit' the user changes |
| 116 | _backupString = editString(); |
| 117 | } |
| 118 | |
| 119 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 120 | void EditTextWidget::startEditMode() |
| 121 | { |
| 122 | EditableWidget::startEditMode(); |
| 123 | } |
| 124 | |
| 125 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 126 | void EditTextWidget::endEditMode() |
| 127 | { |
| 128 | // Editing is always enabled |
| 129 | } |
| 130 | |
| 131 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 132 | void EditTextWidget::abortEditMode() |
| 133 | { |
| 134 | // Editing is always enabled |
| 135 | setText(_backupString); |
| 136 | } |
| 137 | |