[gimp] app: add an optional "factor" to GimpSpinScale



commit 766c4a026c3e58ae187d310fa4e55f7a42296079
Author: Michael Natterer <mitch gimp org>
Date:   Tue May 28 23:45:45 2013 +0200

    app: add an optional "factor" to GimpSpinScale
    
    which allows to display a value different from the original
    adjustment's value.
    
    This is sortof a hack but insanely useful to map normalized config
    values to a nicer range (e.g. 0.0 -> 1.0 to 0 ->100).

 app/widgets/gimpspinscale.c |  126 ++++++++++++++++++++++++++++++++++++------
 app/widgets/gimpspinscale.h |    7 +++
 2 files changed, 115 insertions(+), 18 deletions(-)
---
diff --git a/app/widgets/gimpspinscale.c b/app/widgets/gimpspinscale.c
index a86a6d1..64ace82 100644
--- a/app/widgets/gimpspinscale.c
+++ b/app/widgets/gimpspinscale.c
@@ -52,24 +52,26 @@ typedef struct _GimpSpinScalePrivate GimpSpinScalePrivate;
 
 struct _GimpSpinScalePrivate
 {
-  gchar       *label;
-  gchar       *label_text;
-  gchar       *label_pattern;
-
-  GtkWindow   *mnemonic_window;
-  guint        mnemonic_keyval;
-  gboolean     mnemonics_visible;
-
-  gboolean     scale_limits_set;
-  gdouble      scale_lower;
-  gdouble      scale_upper;
-  gdouble      gamma;
-
-  PangoLayout *layout;
-  gboolean     changing_value;
-  gboolean     relative_change;
-  gdouble      start_x;
-  gdouble      start_value;
+  gchar         *label;
+  gchar         *label_text;
+  gchar         *label_pattern;
+
+  GtkWindow     *mnemonic_window;
+  guint          mnemonic_keyval;
+  gboolean       mnemonics_visible;
+
+  gboolean       scale_limits_set;
+  gdouble        scale_lower;
+  gdouble        scale_upper;
+  gdouble        gamma;
+  gdouble        factor;
+  GtkAdjustment *original_adjustment;
+
+  PangoLayout   *layout;
+  gboolean       changing_value;
+  gboolean       relative_change;
+  gdouble        start_x;
+  gdouble        start_value;
 };
 
 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
@@ -174,6 +176,7 @@ gimp_spin_scale_init (GimpSpinScale *scale)
 
   private->mnemonic_keyval = GDK_KEY_VoidSymbol;
   private->gamma           = 1.0;
+  private->factor          = 1.0;
 }
 
 static void
@@ -193,6 +196,12 @@ gimp_spin_scale_dispose (GObject *object)
       private->layout = NULL;
     }
 
+  if (private->original_adjustment)
+    {
+      g_object_unref (private->original_adjustment);
+      private->original_adjustment = NULL;
+    }
+
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
 
@@ -1120,3 +1129,84 @@ gimp_spin_scale_get_gamma (GimpSpinScale *scale)
 
   return GET_PRIVATE (scale)->gamma;
 }
+
+static gboolean
+gimp_spin_scale_apply_factor (GBinding     *binding,
+                              const GValue *source_value,
+                              GValue       *target_value,
+                              gpointer      user_data)
+{
+  GimpSpinScalePrivate *private = GET_PRIVATE (user_data);
+  gdouble               value;
+
+  value = g_value_get_double (source_value);
+  g_value_set_double (target_value, value * private->factor);
+
+  return TRUE;
+}
+
+static gboolean
+gimp_spin_scale_unapply_factor (GBinding     *binding,
+                                const GValue *source_value,
+                                GValue       *target_value,
+                                gpointer      user_data)
+{
+  GimpSpinScalePrivate *private = GET_PRIVATE (user_data);
+  gdouble               value;
+
+  value = g_value_get_double (source_value);
+  g_value_set_double (target_value, value / private->factor);
+
+  return TRUE;
+}
+
+void
+gimp_spin_scale_set_factor (GimpSpinScale *scale,
+                            gdouble        factor)
+{
+  GimpSpinScalePrivate *private;
+
+  g_return_if_fail (GIMP_IS_SPIN_SCALE (scale));
+  g_return_if_fail (factor != 0.0);
+
+  private = GET_PRIVATE (scale);
+
+  private->factor = factor;
+
+  if (! private->original_adjustment)
+    {
+      GtkAdjustment *original;
+      GtkAdjustment *adjustment;
+
+      original = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (scale));
+
+      private->original_adjustment = g_object_ref (original);
+
+      adjustment = (GtkAdjustment *)
+        gtk_adjustment_new (factor * gtk_adjustment_get_value (original),
+                            factor * gtk_adjustment_get_lower (original),
+                            factor * gtk_adjustment_get_upper (original),
+                            factor * gtk_adjustment_get_step_increment (original),
+                            factor * gtk_adjustment_get_page_increment (original),
+                            factor * gtk_adjustment_get_page_size (original));
+
+      gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (scale), adjustment);
+
+      g_object_bind_property_full (original,   "value",
+                                   adjustment, "value",
+                                   G_BINDING_BIDIRECTIONAL,
+                                   gimp_spin_scale_apply_factor,
+                                   gimp_spin_scale_unapply_factor,
+                                   scale, NULL);
+    }
+
+  gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale));
+}
+
+gdouble
+gimp_spin_scale_get_factor (GimpSpinScale *scale)
+{
+  g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), 1.0);
+
+  return GET_PRIVATE (scale)->factor;
+}
diff --git a/app/widgets/gimpspinscale.h b/app/widgets/gimpspinscale.h
index d7993a2..6b69b70 100644
--- a/app/widgets/gimpspinscale.h
+++ b/app/widgets/gimpspinscale.h
@@ -66,5 +66,12 @@ void          gimp_spin_scale_set_gamma          (GimpSpinScale *scale,
                                                   gdouble        gamma);
 gdouble       gimp_spin_scale_get_gamma          (GimpSpinScale *scale);
 
+/* note: after calling set_factor(), gtk_spin_button_get_adjustment()
+ * will return a different adjustment!
+ */
+void          gimp_spin_scale_set_factor         (GimpSpinScale *scale,
+                                                  gdouble        factor);
+gdouble       gimp_spin_scale_get_factor         (GimpSpinScale *scale);
+
 
 #endif  /*  __GIMP_SPIN_SCALE_H__  */


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