From 42e6a81629fa0c3fc49a81a1a4f3102a1d5b56f9 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 10 Nov 2002 14:53:28 +0000 Subject: improved MessageDialog: long lines are split now (this needs improvement to favor splitting at spaces); maximum size is restricted to 300x180 svn-id: r5491 --- gui/message.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++-------------- gui/message.h | 5 +++++ 2 files changed, 57 insertions(+), 16 deletions(-) (limited to 'gui') diff --git a/gui/message.cpp b/gui/message.cpp index 91c37763f9..ff678e9dd5 100644 --- a/gui/message.cpp +++ b/gui/message.cpp @@ -22,8 +22,44 @@ #include "message.h" #include "newgui.h" -#include "common/list.h" +int MessageDialog::addLine(const char *line, int size) +{ + int width = 0, maxWidth = 0; + const char *start = line; + String tmp; + + for (int i = 0; i < size; ++i) { + int w = _gui->getCharWidth(*line); + + // Check if we exceed the maximum line width, if so, split the line + // TODO - we could make this more clever by trying to split at + // non-letters, e.g. at space/slash/dot + if (width + w > 280) { + if (maxWidth < width) + maxWidth = width; + + // Add the substring from intervall [start, i-1] + tmp = String(start, line - start); + _lines.push_back(tmp); + + start = line; + width = w; + } else + width += w; + + line++; + } + + if (maxWidth < width) + maxWidth = width; + + if (start < line) { + tmp = String(start, line - start); + _lines.push_back(tmp); + } + return maxWidth; +} MessageDialog::MessageDialog(NewGui *gui, const String &message) : Dialog(gui, 30, 20, 260, 124) @@ -32,42 +68,42 @@ MessageDialog::MessageDialog(NewGui *gui, const String &message) // down the string into lines, and taking the maximum of their widths. // Using this, and accounting for the space the button(s) need, we can set // the real size of the dialog - ScummVM::StringList lines; - String tmp; const char *str = message.c_str(); const char *start = str; int lineWidth, maxlineWidth = 0; + int lineCount; while (*str) { if (*str == '\n') { - tmp = String(start, str - start); - lines.push_back(tmp); - lineWidth = _gui->getStringWidth(tmp); + lineWidth = addLine(start, str - start); if (maxlineWidth < lineWidth) maxlineWidth = lineWidth; start = str + 1; } - ++str; } - // Add in the last line - tmp = String(start, str - start); - lines.push_back(tmp); - lineWidth = _gui->getStringWidth(tmp); + // Add the last line + lineWidth = addLine(start, str - start); if (maxlineWidth < lineWidth) maxlineWidth = lineWidth; - // TODO - we should probably check for over/underflows here + // Calculate the desired dialog size (maxing out at 300*180 for now) _w = maxlineWidth + 20; - _h = lines.size() * kLineHeight + 34; + lineCount = _lines.size(); + _h = lineCount * kLineHeight + 34; + if (_h > 180) { + lineCount = (180 - 34) / kLineHeight; + _h = lineCount * kLineHeight + 34; + } _x = (320 - _w) / 2; - for (int i = 0; i < lines.size(); i++) { + for (int i = 0; i < lineCount; i++) { new StaticTextWidget(this, 10, 10+i*kLineHeight, maxlineWidth, kLineHeight, - lines[i], kTextAlignCenter); + _lines[i], kTextAlignCenter); } - // FIXME - the vertical position has to be adjusted + // FIXME - allow for multiple buttons, and return in runModal() which one + // was selected. addButton((_w - kButtonWidth)/2, _h - 24, "OK", kCloseCmd, '\n'); // Confirm dialog } diff --git a/gui/message.h b/gui/message.h index 136d96be98..5b5c3ccea7 100644 --- a/gui/message.h +++ b/gui/message.h @@ -23,13 +23,18 @@ #include "dialog.h" #include "common/str.h" +#include "common/list.h" class MessageDialog : public Dialog { typedef ScummVM::String String; + typedef ScummVM::StringList StringList; public: MessageDialog(NewGui *gui, const String &message); protected: + StringList _lines; + + int addLine(const char *line, int size); }; #endif -- cgit v1.2.3