[gtk+] scale, spinbutton: Avoid -0.0



commit f23e99b063857a4a3f9e2fc81ff62021820edb77
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 7 23:04:40 2016 -0400

    scale, spinbutton: Avoid -0.0
    
    This is a very longstanding bug; time to finally put it to rest.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=118959

 gtk/gtkscale.c      |   18 +++++++++++++++++-
 gtk/gtkspinbutton.c |   15 ++++++++++++++-
 2 files changed, 31 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkscale.c b/gtk/gtkscale.c
index 43161a3..2a40227 100644
--- a/gtk/gtkscale.c
+++ b/gtk/gtkscale.c
@@ -1933,6 +1933,19 @@ gtk_scale_real_get_layout_offsets (GtkScale *scale,
   *y = value_alloc.y;
 }
 
+static gchar *
+weed_out_neg_zero (gchar *str, gint digits)
+{
+  if (str[0] == '-')
+    {
+      gchar neg_zero[8];
+      snprintf (neg_zero, 8, "%0.*f", digits, -0.0);
+      if (strcmp (neg_zero, str) == 0)
+        memmove (str, str + 1, strlen (str) - 1);
+    }
+  return str;
+}
+
 /*
  * Emits #GtkScale::format-value signal to format the value,
  * if no user signal handlers, falls back to a default format.
@@ -1950,7 +1963,10 @@ gtk_scale_format_value (GtkScale *scale,
   if (fmt)
     return fmt;
   else
-    return g_strdup_printf ("%0.*f", scale->priv->digits, value);
+    {
+      fmt = g_strdup_printf ("%0.*f", scale->priv->digits, value);
+      return weed_out_neg_zero (fmt, scale->priv->digits);
+    }
 }
 
 static void
diff --git a/gtk/gtkspinbutton.c b/gtk/gtkspinbutton.c
index fbe9c17..c159f26 100644
--- a/gtk/gtkspinbutton.c
+++ b/gtk/gtkspinbutton.c
@@ -1116,13 +1116,26 @@ measure_string_width (PangoLayout *layout,
 }
 
 static gchar *
+weed_out_neg_zero (gchar *str, gint digits)
+{
+  if (str[0] == '-')
+    {
+      gchar neg_zero[8];
+      snprintf (neg_zero, 8, "%0.*f", digits, -0.0);
+      if (strcmp (neg_zero, str) == 0)
+        memmove (str, str + 1, strlen (str) - 1);
+    }
+  return str;
+}
+
+static gchar *
 gtk_spin_button_format_for_value (GtkSpinButton *spin_button,
                                   gdouble        value)
 {
   GtkSpinButtonPrivate *priv = spin_button->priv;
   gchar *buf = g_strdup_printf ("%0.*f", priv->digits, value);
 
-  return buf;
+  return weed_out_neg_zero (buf, priv->digits);
 }
 
 gint


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]