/gui/widgets/popup.cpp
C++ | 548 lines | 376 code | 106 blank | 66 comment | 102 complexity | 37b046ef32f2d055b3b6932b458de7d5 MD5 | raw file
- /* 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.
- *
- * 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 2
- * of the License, or (at your option) any later version.
- *
- * 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
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
- #include "common/system.h"
- #include "gui/gui-manager.h"
- #include "gui/widgets/popup.h"
- #include "gui/ThemeEval.h"
- namespace GUI {
- //
- // PopUpDialog
- //
- PopUpDialog::PopUpDialog(Widget *boss, const Common::String &name, int clickX, int clickY):
- Dialog(name),
- _boss(boss),
- // Remember original mouse position
- _clickX(clickX),
- _clickY(clickY),
- _selection(-1),
- _initialSelection(-1),
- _openTime(0),
- _twoColumns(false),
- _entriesPerColumn(1),
- _leftPadding(0),
- _rightPadding(0),
- _lineHeight(kLineHeight),
- _lastRead(-1) {
- _backgroundType = ThemeEngine::kDialogBackgroundNone;
- _w = _boss->getWidth();
- }
- void PopUpDialog::open() {
- // Time the popup was opened
- _openTime = g_system->getMillis();
- _initialSelection = _selection;
- // Calculate real popup dimensions
- _h = _entries.size() * _lineHeight + 2;
- _entriesPerColumn = 1;
- // Perform clipping / switch to scrolling mode if we don't fit on the screen
- // FIXME - OSystem should send out notification messages when the screen
- // resolution changes... we could generalize CommandReceiver and CommandSender.
- const int screenH = g_system->getOverlayHeight();
- // HACK: For now, we do not do scrolling. Instead, we draw the dialog
- // in two columns if it's too tall.
- if (_h >= screenH) {
- const int screenW = g_system->getOverlayWidth();
- _twoColumns = true;
- _entriesPerColumn = _entries.size() / 2;
- if (_entries.size() & 1)
- _entriesPerColumn++;
- _h = _entriesPerColumn * _lineHeight + 2;
- _w = 0;
- for (uint i = 0; i < _entries.size(); i++) {
- int width = g_gui.getStringWidth(_entries[i]);
- if (width > _w)
- _w = width;
- }
- _w = 2 * _w + 10;
- if (!(_w & 1))
- _w++;
- if (_selection >= _entriesPerColumn) {
- _x -= _w / 2;
- _y = _boss->getAbsY() - (_selection - _entriesPerColumn) * _lineHeight;
- }
- if (_w >= screenW)
- _w = screenW - 1;
- if (_x < 0)
- _x = 0;
- if (_x + _w >= screenW)
- _x = screenW - 1 - _w;
- } else
- _twoColumns = false;
- if (_h >= screenH)
- _h = screenH - 1;
- if (_y < 0)
- _y = 0;
- else if (_y + _h >= screenH)
- _y = screenH - 1 - _h;
- // TODO - implement scrolling if we had to move the menu, or if there are too many entries
- _lastRead = -1;
- Dialog::open();
- }
- void PopUpDialog::reflowLayout() {
- }
- void PopUpDialog::drawDialog(DrawLayer layerToDraw) {
- Dialog::drawDialog(layerToDraw);
- if (g_gui.useRTL()) {
- _x = g_system->getOverlayWidth() - _x - _w + g_gui.getOverlayOffset();
- }
- // Draw the menu border
- g_gui.theme()->drawWidgetBackground(Common::Rect(_x, _y, _x + _w, _y + _h), ThemeEngine::kWidgetBackgroundPlain);
- /*if (_twoColumns)
- g_gui.vLine(_x + _w / 2, _y, _y + _h - 2, g_gui._color);*/
- // Draw the entries
- int count = _entries.size();
- for (int i = 0; i < count; i++) {
- drawMenuEntry(i, i == _selection);
- }
- // The last entry may be empty. Fill it with black.
- /*if (_twoColumns && (count & 1)) {
- g_gui.fillRect(_x + 1 + _w / 2, _y + 1 + kLineHeight * (_entriesPerColumn - 1), _w / 2 - 1, kLineHeight, g_gui._bgcolor);
- }*/
- }
- void PopUpDialog::handleMouseUp(int x, int y, int button, int clickCount) {
- int absX = x + getAbsX();
- int absY = y + getAbsY();
- // Mouse was released. If it wasn't moved much since the original mouse down,
- // let the popup stay open. If it did move, assume the user made his selection.
- int dist = (_clickX - absX) * (_clickX - absX) + (_clickY - absY) * (_clickY - absY);
- if (dist > 3 * 3 || g_system->getMillis() - _openTime > 300) {
- int item = findItem(x, y);
- setResult(item);
- close();
- }
- _clickX = -1;
- _clickY = -1;
- _openTime = (uint32)-1;
- }
- void PopUpDialog::handleMouseWheel(int x, int y, int direction) {
- if (direction < 0)
- moveUp();
- else if (direction > 0)
- moveDown();
- }
- void PopUpDialog::handleMouseMoved(int x, int y, int button) {
- // Compute over which item the mouse is...
- int item = findItem(x, y);
- if (item >= 0 && _entries[item].size() == 0)
- item = -1;
- if (item == -1 && !isMouseDown()) {
- setSelection(_initialSelection);
- return;
- }
- // ...and update the selection accordingly
- setSelection(item);
- if (_lastRead != item && _entries.size() > 0 && item != -1) {
- read(_entries[item]);
- _lastRead = item;
- }
- }
- void PopUpDialog::handleMouseLeft(int button) {
- _lastRead = -1;
- }
- void PopUpDialog::read(const Common::U32String &str) {
- if (ConfMan.hasKey("tts_enabled", "scummvm") &&
- ConfMan.getBool("tts_enabled", "scummvm")) {
- Common::TextToSpeechManager *ttsMan = g_system->getTextToSpeechManager();
- if (ttsMan != nullptr)
- ttsMan->say(str);
- }
- }
- void PopUpDialog::handleKeyDown(Common::KeyState state) {
- if (state.keycode == Common::KEYCODE_ESCAPE) {
- // Don't change the previous selection
- setResult(-1);
- close();
- return;
- }
- if (isMouseDown())
- return;
- switch (state.keycode) {
- case Common::KEYCODE_RETURN:
- case Common::KEYCODE_KP_ENTER:
- setResult(_selection);
- close();
- break;
- // Keypad & special keys
- // - if num lock is set, we ignore the keypress
- // - if num lock is not set, we fall down to the special key case
- case Common::KEYCODE_KP1:
- if (state.flags & Common::KBD_NUM)
- break;
- // fall through
- case Common::KEYCODE_END:
- setSelection(_entries.size()-1);
- break;
- case Common::KEYCODE_KP2:
- if (state.flags & Common::KBD_NUM)
- break;
- // fall through
- case Common::KEYCODE_DOWN:
- moveDown();
-