Better speech bubble positioning.
[scummvm-innocent.git] / gui / message.cpp
blob7dc850482b7a9450c60bd8531ab67d3579c2ffa4
1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM 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.
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.
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.
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.
21 * $URL$
22 * $Id$
25 #include "common/events.h"
26 #include "common/str.h"
27 #include "common/system.h"
28 #include "gui/message.h"
29 #include "gui/GuiManager.h"
30 #include "gui/ThemeEval.h"
31 #include "gui/widget.h"
33 namespace GUI {
35 enum {
36 kOkCmd = 'OK ',
37 kCancelCmd = 'CNCL'
41 // TODO: The default button should be visibly distinct from the alternate button
43 MessageDialog::MessageDialog(const Common::String &message, const char *defaultButton, const char *altButton)
44 : Dialog(30, 20, 260, 124) {
46 const int screenW = g_system->getOverlayWidth();
47 const int screenH = g_system->getOverlayHeight();
49 int buttonWidth = g_gui.xmlEval()->getVar("Globals.Button.Width", 0);
50 int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
52 // First, determine the size the dialog needs. For this we have to break
53 // down the string into lines, and taking the maximum of their widths.
54 // Using this, and accounting for the space the button(s) need, we can set
55 // the real size of the dialog
56 Common::StringList lines;
57 int lineCount, okButtonPos, cancelButtonPos;
58 int maxlineWidth = g_gui.getFont().wordWrapText(message, screenW - 2 * 20, lines);
60 // Calculate the desired dialog size (maxing out at 300*180 for now)
61 _w = maxlineWidth + 20;
62 lineCount = lines.size();
64 _h = 16;
65 if (defaultButton || altButton)
66 _h += buttonHeight + 8;
68 // Limit the number of lines so that the dialog still fits on the screen.
69 if (lineCount > (screenH - 20 - _h) / kLineHeight) {
70 lineCount = (screenH - 20 - _h) / kLineHeight;
72 _h += lineCount * kLineHeight;
74 // Center the dialog
75 _x = (screenW - _w) / 2;
76 _y = (screenH - _h) / 2;
78 // Each line is represented by one static text item.
79 for (int i = 0; i < lineCount; i++) {
80 new StaticTextWidget(this, 10, 10 + i * kLineHeight, maxlineWidth, kLineHeight,
81 lines[i], Graphics::kTextAlignCenter);
84 if (defaultButton && altButton) {
85 okButtonPos = (_w - (buttonWidth * 2)) / 2;
86 cancelButtonPos = ((_w - (buttonWidth * 2)) / 2) + buttonWidth + 10;
87 } else {
88 okButtonPos = cancelButtonPos = (_w - buttonWidth) / 2;
91 if (defaultButton)
92 new ButtonWidget(this, okButtonPos, _h - buttonHeight - 8, buttonWidth, buttonHeight, defaultButton, kOkCmd, Common::ASCII_RETURN); // Confirm dialog
94 if (altButton)
95 new ButtonWidget(this, cancelButtonPos, _h - buttonHeight - 8, buttonWidth, buttonHeight, altButton, kCancelCmd, Common::ASCII_ESCAPE); // Cancel dialog
98 void MessageDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
99 // FIXME: It's a really bad thing that we use two arbitrary constants
100 if (cmd == kOkCmd) {
101 setResult(kMessageOK);
102 close();
103 } else if (cmd == kCancelCmd) {
104 setResult(kMessageCancel);
105 close();
106 } else {
107 Dialog::handleCommand(sender, cmd, data);
111 TimedMessageDialog::TimedMessageDialog(const Common::String &message, uint32 duration)
112 : MessageDialog(message, 0, 0) {
113 _timer = getMillis() + duration;
116 void TimedMessageDialog::handleTickle() {
117 MessageDialog::handleTickle();
118 if (getMillis() > _timer)
119 close();
122 } // End of namespace GUI