From: <sa...@us...> - 2006-09-02 23:06:43
|
Revision: 17131 http://svn.sourceforge.net/gaim/?rev=17131&view=rev Author: sadrul Date: 2006-09-02 16:06:25 -0700 (Sat, 02 Sep 2006) Log Message: ----------- Try to make sure the strings are not too long to get out of the range of the screen. Modified Paths: -------------- trunk/console/libgnt/gntlabel.c trunk/console/libgnt/gntutils.c trunk/console/libgnt/gntutils.h Modified: trunk/console/libgnt/gntlabel.c =================================================================== --- trunk/console/libgnt/gntlabel.c 2006-09-02 20:17:43 UTC (rev 17130) +++ trunk/console/libgnt/gntlabel.c 2006-09-02 23:06:25 UTC (rev 17131) @@ -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) { Modified: trunk/console/libgnt/gntutils.c =================================================================== --- trunk/console/libgnt/gntutils.c 2006-09-02 20:17:43 UTC (rev 17130) +++ trunk/console/libgnt/gntutils.c 2006-09-02 23:06:25 UTC (rev 17131) @@ -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); +} + Modified: trunk/console/libgnt/gntutils.h =================================================================== --- trunk/console/libgnt/gntutils.h 2006-09-02 20:17:43 UTC (rev 17130) +++ trunk/console/libgnt/gntutils.h 2006-09-02 23:06:25 UTC (rev 17131) @@ -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); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |