1 /* ResidualVM - A 3D game interpreter
2 *
3 * ResidualVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "common/system.h"
24
25 #include "gui/gui-manager.h"
26 #include "gui/ThemeEval.h"
27 #include "gui/widget.h"
28 #include "gui/widgets/edittext.h"
29
30 #include "engines/grim/inputdialog.h"
31
32 namespace Grim {
33
InputDialog(const Common::String & message,const Common::String & string,bool hasTextField)34 InputDialog::InputDialog(const Common::String &message, const Common::String &string, bool hasTextField) :
35 GUI::Dialog(30, 20, 260, 124), _hasTextField(hasTextField),
36 m_text(nullptr) {
37
38 const int screenW = g_system->getOverlayWidth();
39 const int screenH = g_system->getOverlayHeight();
40
41 int buttonWidth = g_gui.xmlEval()->getVar("Globals.Button.Width", 0);
42 int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
43
44 // First, determine the size the dialog needs. For this we have to break
45 // down the string into lines, and taking the maximum of their widths.
46 // Using this, and accounting for the space the button(s) need, we can set
47 // the real size of the dialog
48 Common::Array<Common::String> lines;
49 int lineCount;
50 int maxlineWidth = g_gui.getFont().wordWrapText(message, screenW - 2 * 20, lines);
51
52 // Calculate the desired dialog size (maxing out at 300*180 for now)
53 _w = MAX(maxlineWidth, (2 * buttonWidth) + 10) + 20;
54
55 lineCount = lines.size();
56
57 _h = 30 + kLineHeight;
58 _h += buttonHeight + 10;
59
60 // Limit the number of lines so that the dialog still fits on the screen.
61 if (lineCount > (screenH - 20 - _h) / kLineHeight) {
62 lineCount = (screenH - 20 - _h) / kLineHeight;
63 }
64 _h += lineCount * kLineHeight;
65
66 // Center the dialog
67 _x = (screenW - _w) / 2;
68 _y = (screenH - _h) / 2;
69
70 // Each line is represented by one static text item.
71 int height = 10;
72 for (int i = 0; i < lineCount; i++) {
73 new GUI::StaticTextWidget(this, 10, height, maxlineWidth, kLineHeight,
74 lines[i], Graphics::kTextAlignCenter);
75 height += kLineHeight;
76 }
77 height += 10;
78 if (_hasTextField) {
79 m_text = new GUI::EditTextWidget(this, 10, height, _w - 20, kLineHeight, string, "input");
80 height += kLineHeight + 10;
81 }
82
83 new GUI::ButtonWidget(this, 10, height, buttonWidth, buttonHeight, "Ok", nullptr, GUI::kOKCmd, Common::ASCII_RETURN); // Confirm dialog
84 new GUI::ButtonWidget(this, _w - buttonWidth - 10, height, buttonWidth, buttonHeight, "Cancel", nullptr, GUI::kCloseCmd, Common::ASCII_ESCAPE); // Cancel dialog
85 }
86
getString() const87 const Common::String &InputDialog::getString() const {
88 if (_hasTextField) {
89 return m_text->getEditString();
90 } else {
91 // TextBox-less dialogs shouldn't need any getString, but
92 // this is needed for safety and to avoid warnings.
93 return _name;
94 }
95
96 }
97
handleKeyDown(Common::KeyState state)98 void InputDialog::handleKeyDown(Common::KeyState state) {
99 if (state.keycode == Common::KEYCODE_RETURN) {
100 setResult(1);
101 close();
102 } else if (state.keycode == Common::KEYCODE_ESCAPE) {
103 setResult(0);
104 close();
105 } else {
106 GUI::Dialog::handleKeyDown(state);
107 }
108 }
109
handleCommand(GUI::CommandSender * sender,uint32 cmd,uint32 data)110 void InputDialog::handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data) {
111 if (cmd == GUI::kOKCmd) {
112 setResult(1);
113 close();
114 } else if (cmd == GUI::kCloseCmd) {
115 setResult(0);
116 close();
117 } else {
118 GUI::Dialog::handleCommand(sender, cmd, data);
119 }
120 }
121
122 }
123