changeset 14423:cfd5bcc06a7e

[gaim-migrate @ 17131] Try to make sure the strings are not too long to get out of the range of the screen. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sat, 02 Sep 2006 23:06:25 +0000
parents 84a480acb6ad
children c374f45f4c94
files console/libgnt/gntlabel.c console/libgnt/gntutils.c console/libgnt/gntutils.h
diffstat 3 files changed, 39 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/console/libgnt/gntlabel.c	Sat Sep 02 20:17:43 2006 +0000
+++ b/console/libgnt/gntlabel.c	Sat Sep 02 23:06:25 2006 +0000
@@ -101,7 +101,7 @@
 	GntWidget *widget = g_object_new(GNT_TYPE_LABEL, NULL);
 	GntLabel *label = GNT_LABEL(widget);
 
-	label->text = g_strdup(text);
+	label->text = gnt_util_onscreen_fit_string(text, -1);
 	label->flags = flags;
 	gnt_widget_set_take_focus(widget, FALSE);
 	GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
@@ -112,7 +112,7 @@
 void gnt_label_set_text(GntLabel *label, const char *text)
 {
 	g_free(label->text);
-	label->text = g_strdup(text);
+	label->text = gnt_util_onscreen_fit_string(text, -1);
 
 	if (GNT_WIDGET(label)->window)
 	{
--- a/console/libgnt/gntutils.c	Sat Sep 02 20:17:43 2006 +0000
+++ b/console/libgnt/gntutils.c	Sat Sep 02 23:06:25 2006 +0000
@@ -68,3 +68,32 @@
 	return str;
 }
 
+char *gnt_util_onscreen_fit_string(const char *string, int maxw)
+{
+	const char *start, *end;
+	GString *str;
+
+	if (maxw <= 0)
+		maxw = getmaxx(stdscr) - 4;
+
+	start = string;
+	str = g_string_new(NULL);
+
+	while (*start) {
+		if ((end = strchr(start, '\n')) != NULL ||
+			(end = strchr(start, '\r')) != NULL) {
+			if (gnt_util_onscreen_width(start, end) <= maxw) {
+				++end;
+			} else
+				end = NULL;
+		}
+		if (end == NULL)
+			end = gnt_util_onscreen_width_to_pointer(start, maxw, NULL);
+		str = g_string_append_len(str, start, end - start);
+		start = end;
+		if (*end && *end != '\n' && *end != '\r')
+			str = g_string_append_c(str, '\n');
+	}
+	return g_string_free(str, FALSE);
+}
+
--- a/console/libgnt/gntutils.h	Sat Sep 02 20:17:43 2006 +0000
+++ b/console/libgnt/gntutils.h	Sat Sep 02 23:06:25 2006 +0000
@@ -9,3 +9,11 @@
 int gnt_util_onscreen_width(const char *start, const char *end);
 
 const char *gnt_util_onscreen_width_to_pointer(const char *str, int len, int *w);
+
+/* Inserts newlines in 'string' where necessary so that its onscreen width is
+ * no more than 'maxw'.
+ * 'maxw' can be <= 0, in which case the maximum screen width is considered.
+ *
+ * Returns a newly allocated string.
+ */
+char *gnt_util_onscreen_fit_string(const char *string, int maxw);