diff pidgin/gtkutils.c @ 27768:969a2aeae461

propagate from branch 'im.pidgin.pidgin' (head b7875f81b48bb1a776ac4b2473cc7376cb35b0f9) to branch 'im.pidgin.pidgin.yaz' (head e22890a44520ea1129da935f8ce82ea795081201)
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Wed, 27 Feb 2008 12:21:29 +0000
parents e9cf897bd873
children d44a96d49ee5
line wrap: on
line diff
--- a/pidgin/gtkutils.c	Wed Feb 27 09:18:06 2008 +0000
+++ b/pidgin/gtkutils.c	Wed Feb 27 12:21:29 2008 +0000
@@ -3459,3 +3459,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;
+}