[Scummvm-cvs-logs] CVS: scummvm/gui message.cpp,1.3,1.4 message.h,1.1,1.2

Max Horn fingolfin at users.sourceforge.net
Sun Nov 10 06:54:02 CET 2002


Update of /cvsroot/scummvm/scummvm/gui
In directory usw-pr-cvs1:/tmp/cvs-serv29425

Modified Files:
	message.cpp message.h 
Log Message:
improved MessageDialog: long lines are split now (this needs improvement to favor splitting at spaces); maximum size is restricted to 300x180

Index: message.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/message.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- message.cpp	12 Oct 2002 00:26:24 -0000	1.3
+++ message.cpp	10 Nov 2002 14:53:28 -0000	1.4
@@ -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 @@
 	// 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
 }

Index: message.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/message.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- message.h	26 Sep 2002 11:44:02 -0000	1.1
+++ message.h	10 Nov 2002 14:53:28 -0000	1.2
@@ -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





More information about the Scummvm-git-logs mailing list