GNOME Bugzilla – Bug 541399
Widget tooltips: treat "" same as NULL
Last modified: 2008-07-10 17:48:31 UTC
Patch attached: 2008-07-03 Murray Cumming <murrayc@murrayc.com> * gtk/gtkwidget.c (gtk_widget_set_property): tooltip-text and tooltip-markup properties: Interpret an empty string as a NULL string because an empty tooltip is silly. This will help language bindings that do not bother to have the two types of empty/null strings.
Created attachment 113926 [details] [review] gtk_widget_tooltip_empty_is_null.patch
Comment on attachment 113926 [details] [review] gtk_widget_tooltip_empty_is_null.patch Thanks, the patch looks good Murray, please apply after the following coding style issues have been fixed. >Index: gtk/gtkwidget.c >=================================================================== >--- gtk/gtkwidget.c (revision 20744) >+++ gtk/gtkwidget.c (working copy) >@@ -2478,6 +2478,13 @@ > tooltip_window = g_object_get_qdata (object, quark_tooltip_window); > tooltip_markup = g_value_dup_string (value); > >+ /* Treat an empty string as a NULL string, >+ because an empty string would be useless for a tooltip: */ All comment lines are prefixed with '*' in Gtk+ coding style. also '*/' should go on its own line. >+ if (tooltip_markup && (strlen (tooltip_markup) == 0)) { Braces also go on their own line. >+ g_free (tooltip_markup); >+ tooltip_markup = NULL; >+ } >+ > g_object_set_qdata_full (object, quark_tooltip_markup, > tooltip_markup, g_free); > >@@ -2486,7 +2493,14 @@ > break; > case PROP_TOOLTIP_TEXT: > tooltip_window = g_object_get_qdata (object, quark_tooltip_window); >+ > tooltip_text = g_value_get_string (value); >+ >+ /* Treat an empty string as a NULL string, >+ because an empty string would be useless for a tooltip: */ dito. >+ if (tooltip_text && (strlen (tooltip_text) == 0)) >+ tooltip_text = NULL; >+ > tooltip_markup = tooltip_text ? g_markup_escape_text (tooltip_text, -1) : NULL; > > g_object_set_qdata_full (object, quark_tooltip_markup,
Admittedly I have no clue about GTK's coding styles/rules, so sorry if I ask something stupid, but wouldn't it be faster to replace the "strlen"-calls in the patch with a check if the first char is a '\0', i.e. if (tooltip_markup && *tooltip_markup) /* or (*tooltip_markup != '\0') */ and if (tooltip_text && *tooltip_text)
(In reply to comment #3) > Admittedly I have no clue about GTK's coding styles/rules, so sorry if I ask > something stupid, but wouldn't it be faster to replace the "strlen"-calls in > the patch with a check if the first char is a '\0', i.e. > > if (tooltip_markup && *tooltip_markup) /* or (*tooltip_markup != '\0') */ > > and > > if (tooltip_text && *tooltip_text) Yes, it would be faster, but only a tiny bit and the code in question isn't performance critical in anyway. In general, readability should be the prime concern, personally I don't have a strong preference for one over the other here though.
Committed to trunk and and gtk-2-12 with Tim's code formatting changes.