2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2002-11-21 12:48:50 +00:00
|
|
|
*
|
2021-12-26 18:47:58 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2002-11-21 12:48:50 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-02-18 02:34:20 +01:00
|
|
|
*
|
2002-11-21 12:48:50 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-25 21:08:55 +10:00
|
|
|
#include "common/system.h"
|
2023-03-01 02:42:45 +05:30
|
|
|
#include "common/unicode-bidi.h"
|
2010-11-16 10:11:57 +00:00
|
|
|
#include "gui/widgets/edittext.h"
|
2010-11-16 10:24:16 +00:00
|
|
|
#include "gui/gui-manager.h"
|
2003-11-10 23:40:48 +00:00
|
|
|
|
2008-08-07 18:42:47 +00:00
|
|
|
#include "gui/ThemeEval.h"
|
|
|
|
|
2003-11-10 23:40:48 +00:00
|
|
|
namespace GUI {
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2023-03-23 21:04:50 +00:00
|
|
|
EditTextWidget::EditTextWidget(GuiObject *boss, int x, int y, int w, int h, bool scale, const Common::U32String &text, const Common::U32String &tooltip, uint32 cmd, uint32 finishCmd, ThemeEngine::FontStyle font)
|
|
|
|
: EditableWidget(boss, x, y - 1, w, h + 2, scale, tooltip, cmd) {
|
2007-11-04 03:38:30 +00:00
|
|
|
setFlags(WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE);
|
2002-11-21 15:20:52 +00:00
|
|
|
_type = kEditTextWidget;
|
2011-04-16 16:18:59 +03:00
|
|
|
_finishCmd = finishCmd;
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2022-08-25 17:56:06 +02:00
|
|
|
_leftPadding = _rightPadding = 0;
|
|
|
|
|
2006-05-19 22:41:51 +00:00
|
|
|
setEditString(text);
|
2019-07-18 23:10:49 +07:00
|
|
|
setFontStyle(font);
|
2005-01-29 18:04:34 +00:00
|
|
|
}
|
2003-01-10 21:33:42 +00:00
|
|
|
|
2023-03-23 21:04:50 +00:00
|
|
|
EditTextWidget::EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const Common::U32String &text, const Common::U32String &tooltip, uint32 cmd, uint32 finishCmd, ThemeEngine::FontStyle font)
|
|
|
|
: EditTextWidget(boss, x, y, w, h, false, text, tooltip, cmd, finishCmd, font) {
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:56:44 +01:00
|
|
|
EditTextWidget::EditTextWidget(GuiObject *boss, const Common::String &name, const Common::U32String &text, const Common::U32String &tooltip, uint32 cmd, uint32 finishCmd, ThemeEngine::FontStyle font)
|
2010-06-15 10:54:22 +00:00
|
|
|
: EditableWidget(boss, name, tooltip, cmd) {
|
2007-11-04 03:38:30 +00:00
|
|
|
setFlags(WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE);
|
2006-03-09 05:18:00 +00:00
|
|
|
_type = kEditTextWidget;
|
2011-04-16 16:18:59 +03:00
|
|
|
_finishCmd = finishCmd;
|
2006-03-09 05:18:00 +00:00
|
|
|
|
2022-08-25 17:56:06 +02:00
|
|
|
_leftPadding = _rightPadding = 0;
|
2023-03-01 02:42:45 +05:30
|
|
|
_shiftPressed = _isDragging = false;
|
2022-08-25 17:56:06 +02:00
|
|
|
|
2006-05-19 22:41:51 +00:00
|
|
|
setEditString(text);
|
2019-07-18 23:10:49 +07:00
|
|
|
setFontStyle(font);
|
2006-03-09 05:18:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:56:44 +01:00
|
|
|
void EditTextWidget::setEditString(const Common::U32String &str) {
|
2005-01-29 18:04:34 +00:00
|
|
|
EditableWidget::setEditString(str);
|
|
|
|
_backupString = str;
|
2002-11-21 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
2006-08-04 13:55:53 +00:00
|
|
|
void EditTextWidget::reflowLayout() {
|
2008-08-07 18:42:47 +00:00
|
|
|
_leftPadding = g_gui.xmlEval()->getVar("Globals.EditTextWidget.Padding.Left", 0);
|
|
|
|
_rightPadding = g_gui.xmlEval()->getVar("Globals.EditTextWidget.Padding.Right", 0);
|
2006-05-27 05:46:04 +00:00
|
|
|
|
2006-08-04 15:48:37 +00:00
|
|
|
EditableWidget::reflowLayout();
|
2006-05-18 20:53:28 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:38:30 +00:00
|
|
|
void EditTextWidget::drawWidget() {
|
2019-12-28 10:43:58 +01:00
|
|
|
g_gui.theme()->drawWidgetBackground(Common::Rect(_x, _y, _x + _w, _y + _h),
|
2018-01-27 08:59:53 +01:00
|
|
|
ThemeEngine::kWidgetBackgroundEditText);
|
2002-11-21 15:20:52 +00:00
|
|
|
|
|
|
|
// Draw the text
|
2003-05-05 16:10:19 +00:00
|
|
|
adjustOffset();
|
2021-04-22 01:31:11 +01:00
|
|
|
Common::Rect drawRect = getEditRect();
|
|
|
|
drawRect.translate(_x, _y);
|
|
|
|
setTextDrawableArea(drawRect);
|
2013-06-13 12:32:36 +00:00
|
|
|
|
2023-03-01 02:42:45 +05:30
|
|
|
int x = -_editScrollOffset;
|
|
|
|
int y = drawRect.top;
|
|
|
|
|
|
|
|
int selBegin = _selCaretPos;
|
|
|
|
int selEnd = _selOffset + _selCaretPos;
|
|
|
|
if (selBegin > selEnd)
|
|
|
|
SWAP(selBegin, selEnd);
|
|
|
|
selBegin = MAX(selBegin, 0);
|
|
|
|
selEnd = MAX(selEnd, 0);
|
|
|
|
|
2023-03-01 04:02:56 +05:30
|
|
|
if (!g_gui.useRTL()) {
|
|
|
|
Common::UnicodeBiDiText utxt(_editString);
|
|
|
|
Common::U32String left = Common::U32String(utxt.visual.c_str(), utxt.visual.c_str() + selBegin);
|
|
|
|
Common::U32String selected = Common::U32String(utxt.visual.c_str() + selBegin, selEnd - selBegin);
|
|
|
|
Common::U32String right = Common::U32String(utxt.visual.c_str() + selEnd);
|
|
|
|
Common::U32StringArray parts {left, selected, right};
|
|
|
|
int scrollOffset = _editScrollOffset;
|
|
|
|
for (uint i = 0; i < parts.size(); i++) {
|
|
|
|
if (!parts[i].size())
|
|
|
|
continue;
|
|
|
|
Common::U32String part = parts[i];
|
|
|
|
int partW = g_gui.getStringWidth(part, _font);
|
|
|
|
int clipL = drawRect.left + (scrollOffset < 0 ? -scrollOffset : 0);
|
|
|
|
int clipR = MIN(clipL + partW, (int)drawRect.right);
|
|
|
|
if (x + partW > 0 && x < _w && clipL < drawRect.right) {
|
|
|
|
int sO = scrollOffset < 0 ? 0 : -scrollOffset;
|
|
|
|
_inversion = i == 1 ? ThemeEngine::kTextInversionFocus : ThemeEngine::kTextInversionNone;
|
|
|
|
g_gui.theme()->drawText(Common::Rect(clipL, y, clipR, y + drawRect.height()), part, _state,
|
|
|
|
_drawAlign, _inversion, sO, false, _font, ThemeEngine::kFontColorNormal,
|
|
|
|
true, _textDrawableArea);
|
|
|
|
}
|
|
|
|
x += partW;
|
|
|
|
scrollOffset -= partW;
|
2023-03-01 02:42:45 +05:30
|
|
|
}
|
2023-03-01 04:02:56 +05:30
|
|
|
} else {
|
|
|
|
// The above method does not render RTL languages correctly, so fallback to default method
|
|
|
|
// There are only two possible cases, either the whole string has been selected
|
|
|
|
// or nothing has been selected.
|
|
|
|
_inversion = _selOffset ? ThemeEngine::kTextInversionFocus : ThemeEngine::kTextInversionNone;
|
|
|
|
g_gui.theme()->drawText(drawRect, _editString, _state, _drawAlign, _inversion,
|
|
|
|
-_editScrollOffset, false, _font, ThemeEngine::kFontColorNormal, true,
|
|
|
|
_textDrawableArea);
|
2023-03-01 02:42:45 +05:30
|
|
|
}
|
2005-01-29 16:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::Rect EditTextWidget::getEditRect() const {
|
2022-03-05 23:57:49 +02:00
|
|
|
// Calculate (right - left) difference for editRect's X-axis coordinates:
|
|
|
|
// (_w - 1 - _rightPadding) - (2 + _leftPadding)
|
|
|
|
int editWidth = _w - _rightPadding - _leftPadding - 3;
|
2022-08-25 17:56:06 +02:00
|
|
|
int editHeight = _h - 2;
|
2022-03-05 23:57:49 +02:00
|
|
|
// Ensure r will always be a valid rect
|
|
|
|
if (editWidth < 0) {
|
|
|
|
editWidth = 0;
|
|
|
|
}
|
2022-08-25 17:56:06 +02:00
|
|
|
if (editHeight < 0) {
|
|
|
|
editHeight = 0;
|
|
|
|
}
|
|
|
|
Common::Rect r(2 + _leftPadding, 1, 2 + _leftPadding + editWidth, 1 + editHeight);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
return r;
|
2003-05-05 16:10:19 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
void EditTextWidget::receivedFocusWidget() {
|
2019-02-12 23:42:50 +00:00
|
|
|
g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, true);
|
2005-01-29 16:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditTextWidget::lostFocusWidget() {
|
2023-03-01 02:42:45 +05:30
|
|
|
// If we lose focus, 'commit' the user changes and clear selection
|
2005-01-29 16:30:51 +00:00
|
|
|
_backupString = _editString;
|
|
|
|
drawCaret(true);
|
2023-03-01 02:42:45 +05:30
|
|
|
clearSelection();
|
2019-02-12 23:42:50 +00:00
|
|
|
|
|
|
|
g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, false);
|
2005-01-29 16:30:51 +00:00
|
|
|
}
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
void EditTextWidget::startEditMode() {
|
|
|
|
}
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
void EditTextWidget::endEditMode() {
|
|
|
|
releaseFocus();
|
2011-04-16 16:18:59 +03:00
|
|
|
|
|
|
|
sendCommand(_finishCmd, 0);
|
2005-01-29 16:30:51 +00:00
|
|
|
}
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
void EditTextWidget::abortEditMode() {
|
2005-01-29 18:04:34 +00:00
|
|
|
setEditString(_backupString);
|
2009-06-06 23:22:35 +00:00
|
|
|
sendCommand(_cmd, 0);
|
2018-08-18 09:37:59 +03:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
releaseFocus();
|
|
|
|
}
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-11-10 23:40:48 +00:00
|
|
|
} // End of namespace GUI
|