diff pidgin/gtkutils.c @ 27826:5f9ca519ea9f

propagate from branch 'im.pidgin.pidgin' (head 90011f8844f95be26206d5698b603f1692f319a0) to branch 'im.pidgin.pidgin.yaz' (head e4862d866bc0b147e783d27c1faa51f1d3e0b58a)
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 24 Apr 2008 02:28:24 +0000
parents 030185a59a1a
children f9fd7ddf6996
line wrap: on
line diff
--- a/pidgin/gtkutils.c	Thu Apr 24 02:16:30 2008 +0000
+++ b/pidgin/gtkutils.c	Thu Apr 24 02:28:24 2008 +0000
@@ -3467,3 +3467,61 @@
 #endif
 }
 
+gchar *
+pidgin_gtk_ellipsis_text(GtkWidget *widget, const char *text, gint min_width, gchar *ellipsis)
+{
+	PangoLayout *layout;
+	gint width, height;
+	gint ewidth;
+	glong len0, len1, len2;
+	gchar *buf, *buf_tmp;
+	gboolean with_ellipsis = FALSE;
+	const gchar default_ellipsis[] = "...";
+
+	if(!ellipsis)
+		ellipsis = default_ellipsis;
+
+	/* allocate buf */
+	buf = g_malloc0(strlen(text) * 2);
+
+	/* create layout */
+	layout = gtk_widget_create_pango_layout(widget, ellipsis);
+	pango_layout_get_pixel_size(layout, &width, &height);
+	ewidth = width; /* length of ellipsis text. */
+
+	len0 = 0;
+	len1 = g_utf8_strlen(text, -1);
+	len2 = len1;
+
+	while (1) {
+
+		if (len2 == len0)
+			break;
+
+		g_utf8_strncpy(buf, text, len2);
+		pango_layout_set_text(layout, buf, -1);
+		pango_layout_get_pixel_size(layout, &width, &height);
+
+		if(!with_ellipsis && width <= min_width)
+			break;
+		else
+			with_ellipsis = TRUE;
+
+		if (width + ewidth > min_width)
+			len1 = len2;
+		else
+			len0 = len2;
+
+		len2 = (len0 + len1) / 2;
+	}
+
+	g_object_unref(layout);
+
+	if (with_ellipsis) {
+		buf_tmp = buf;
+		buf = g_strdup_printf("%s%s", buf_tmp, ellipsis);
+		g_free(buf_tmp);
+	}
+
+	return buf;
+}