ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

message.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/str.h"
00024 #include "common/system.h"
00025 #include "gui/message.h"
00026 #include "gui/gui-manager.h"
00027 #include "gui/ThemeEval.h"
00028 #include "gui/widget.h"
00029 
00030 namespace GUI {
00031 
00032 enum {
00033     kOkCmd = 'OK  ',
00034     kCancelCmd = 'CNCL'
00035 };
00036 
00037 
00038 // TODO: The default button should be visibly distinct from the alternate button
00039 
00040 MessageDialog::MessageDialog(const Common::String &message, const char *defaultButton, const char *altButton)
00041     : Dialog(30, 20, 260, 124) {
00042 
00043     const int screenW = g_system->getOverlayWidth();
00044     const int screenH = g_system->getOverlayHeight();
00045 
00046     int buttonWidth = g_gui.xmlEval()->getVar("Globals.Button.Width", 0);
00047     int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
00048 
00049     // First, determine the size the dialog needs. For this we have to break
00050     // down the string into lines, and taking the maximum of their widths.
00051     // Using this, and accounting for the space the button(s) need, we can set
00052     // the real size of the dialog
00053     Common::Array<Common::String> lines;
00054     int lineCount, okButtonPos, cancelButtonPos;
00055     int maxlineWidth = g_gui.getFont().wordWrapText(message, screenW - 2 * 20, lines);
00056 
00057     // Calculate the desired dialog size (maxing out at 300*180 for now)
00058     if (altButton)
00059         _w = MAX(maxlineWidth, (2 * buttonWidth) + 10) + 20;
00060     else
00061         _w = MAX(maxlineWidth, buttonWidth) + 20;
00062 
00063     lineCount = lines.size();
00064 
00065     _h = 16;
00066     if (defaultButton || altButton)
00067         _h += buttonHeight + 8;
00068 
00069     // Limit the number of lines so that the dialog still fits on the screen.
00070     if (lineCount > (screenH - 20 - _h) / kLineHeight) {
00071         lineCount = (screenH - 20 - _h) / kLineHeight;
00072     }
00073     _h += lineCount * kLineHeight;
00074 
00075     // Center the dialog
00076     _x = (screenW - _w) / 2;
00077     _y = (screenH - _h) / 2;
00078 
00079     // Each line is represented by one static text item.
00080     for (int i = 0; i < lineCount; i++) {
00081         new StaticTextWidget(this, 10, 10 + i * kLineHeight, maxlineWidth, kLineHeight,
00082                                 lines[i], Graphics::kTextAlignCenter);
00083     }
00084 
00085     if (defaultButton && altButton) {
00086         okButtonPos = (_w - (buttonWidth * 2)) / 2;
00087         cancelButtonPos = ((_w - (buttonWidth * 2)) / 2) + buttonWidth + 10;
00088     } else {
00089         okButtonPos = cancelButtonPos = (_w - buttonWidth) / 2;
00090     }
00091 
00092     if (defaultButton)
00093         new ButtonWidget(this, okButtonPos, _h - buttonHeight - 8, buttonWidth, buttonHeight, defaultButton, 0, kOkCmd, Common::ASCII_RETURN);  // Confirm dialog
00094 
00095     if (altButton)
00096         new ButtonWidget(this, cancelButtonPos, _h - buttonHeight - 8, buttonWidth, buttonHeight, altButton, 0, kCancelCmd, Common::ASCII_ESCAPE);  // Cancel dialog
00097 }
00098 
00099 void MessageDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
00100     // FIXME: It's a really bad thing that we use two arbitrary constants
00101     if (cmd == kOkCmd) {
00102         setResult(kMessageOK);
00103         close();
00104     } else if (cmd == kCancelCmd) {
00105         setResult(kMessageCancel);
00106         close();
00107     } else {
00108         Dialog::handleCommand(sender, cmd, data);
00109     }
00110 }
00111 
00112 TimedMessageDialog::TimedMessageDialog(const Common::String &message, uint32 duration)
00113     : MessageDialog(message, 0, 0) {
00114     _timer = g_system->getMillis() + duration;
00115 }
00116 
00117 void TimedMessageDialog::handleTickle() {
00118     MessageDialog::handleTickle();
00119     if (g_system->getMillis() > _timer)
00120         close();
00121 }
00122 
00123 } // End of namespace GUI


Generated on Sat Feb 16 2019 05:00:58 for ResidualVM by doxygen 1.7.1
curved edge   curved edge