comparison 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
comparison
equal deleted inserted replaced
22698:80c87908ff1d 27826:5f9ca519ea9f
3465 #endif 3465 #endif
3466 return FALSE; 3466 return FALSE;
3467 #endif 3467 #endif
3468 } 3468 }
3469 3469
3470 gchar *
3471 pidgin_gtk_ellipsis_text(GtkWidget *widget, const char *text, gint min_width, gchar *ellipsis)
3472 {
3473 PangoLayout *layout;
3474 gint width, height;
3475 gint ewidth;
3476 glong len0, len1, len2;
3477 gchar *buf, *buf_tmp;
3478 gboolean with_ellipsis = FALSE;
3479 const gchar default_ellipsis[] = "...";
3480
3481 if(!ellipsis)
3482 ellipsis = default_ellipsis;
3483
3484 /* allocate buf */
3485 buf = g_malloc0(strlen(text) * 2);
3486
3487 /* create layout */
3488 layout = gtk_widget_create_pango_layout(widget, ellipsis);
3489 pango_layout_get_pixel_size(layout, &width, &height);
3490 ewidth = width; /* length of ellipsis text. */
3491
3492 len0 = 0;
3493 len1 = g_utf8_strlen(text, -1);
3494 len2 = len1;
3495
3496 while (1) {
3497
3498 if (len2 == len0)
3499 break;
3500
3501 g_utf8_strncpy(buf, text, len2);
3502 pango_layout_set_text(layout, buf, -1);
3503 pango_layout_get_pixel_size(layout, &width, &height);
3504
3505 if(!with_ellipsis && width <= min_width)
3506 break;
3507 else
3508 with_ellipsis = TRUE;
3509
3510 if (width + ewidth > min_width)
3511 len1 = len2;
3512 else
3513 len0 = len2;
3514
3515 len2 = (len0 + len1) / 2;
3516 }
3517
3518 g_object_unref(layout);
3519
3520 if (with_ellipsis) {
3521 buf_tmp = buf;
3522 buf = g_strdup_printf("%s%s", buf_tmp, ellipsis);
3523 g_free(buf_tmp);
3524 }
3525
3526 return buf;
3527 }