Mercurial > pidgin.yaz
comparison pidgin/gtkutils.c @ 25508:e0add2aafeaf
- merge nosuke's tab width patch.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 11 Jun 2007 13:08:55 +0000 |
parents | 3aafa9d3119c |
children | ff69c2a9ccf2 |
comparison
equal
deleted
inserted
replaced
25507:abc46fd562bd | 25508:e0add2aafeaf |
---|---|
3076 va_end (args); | 3076 va_end (args); |
3077 | 3077 |
3078 return path; | 3078 return path; |
3079 } | 3079 } |
3080 #endif | 3080 #endif |
3081 | |
3082 gchar * | |
3083 pidgin_gtk_ellipsis_text(GtkWidget *widget, const char *text, gint min_width, gchar *ellipsis) | |
3084 { | |
3085 PangoLayout *layout; | |
3086 gint width, height; | |
3087 glong len0, len1, len2; | |
3088 static gchar buf[1024], buf_tmp[1024]; | |
3089 gint ewidth; | |
3090 gboolean with_ellipsis = FALSE; | |
3091 | |
3092 g_strlcpy(buf_tmp, text, sizeof(buf_tmp)); | |
3093 | |
3094 if (strlen(text) >= sizeof(buf)) { | |
3095 /* cutting off */ | |
3096 if (! g_utf8_validate(buf_tmp, -1, NULL)) { | |
3097 buf_tmp[sizeof(buf_tmp) - 1] = '\0'; | |
3098 if (! g_utf8_validate(buf_tmp, -1, NULL)) { | |
3099 buf_tmp[sizeof(buf_tmp) - 2] = '\0'; | |
3100 if (! g_utf8_validate(buf_tmp, -1, NULL)) { | |
3101 return NULL; /* failed */ | |
3102 } | |
3103 } | |
3104 } | |
3105 } | |
3106 | |
3107 buf[0] = '\0'; | |
3108 | |
3109 #define ELLIPSIS "..." | |
3110 | |
3111 layout = gtk_widget_create_pango_layout(widget, | |
3112 ellipsis ? ellipsis : ELLIPSIS); | |
3113 | |
3114 pango_layout_get_pixel_size(layout, &width, &height); | |
3115 ewidth = width; | |
3116 | |
3117 len0 = 0; | |
3118 len1 = g_utf8_strlen(buf_tmp, -1); | |
3119 len2 = len1; | |
3120 | |
3121 while (1) { | |
3122 | |
3123 if (len2 == len0) | |
3124 break; | |
3125 | |
3126 g_utf8_strncpy (buf, buf_tmp, len2); | |
3127 pango_layout_set_text(layout, buf, -1); | |
3128 pango_layout_get_pixel_size(layout, &width, &height); | |
3129 | |
3130 if (! with_ellipsis && width <= min_width) | |
3131 break; | |
3132 else | |
3133 with_ellipsis = TRUE; | |
3134 | |
3135 if (width + ewidth > min_width) | |
3136 len1 = len2; | |
3137 else | |
3138 len0 = len2; | |
3139 | |
3140 len2 = (len0 + len1) / 2; | |
3141 } | |
3142 | |
3143 g_utf8_strncpy (buf, buf_tmp, len2); | |
3144 g_object_unref(layout); | |
3145 | |
3146 | |
3147 if (with_ellipsis) | |
3148 g_strlcat (buf, ellipsis ? ellipsis : ELLIPSIS, sizeof(buf)); | |
3149 | |
3150 #undef ELLIPSIS | |
3151 | |
3152 return buf; | |
3153 } | |
3154 |