[gimp] app: start using the new histogram property notifications in the widgets



commit d26cd268baeba647e9b3c5f9fd6f4adc5f244756
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jun 11 22:36:33 2013 +0200

    app: start using the new histogram property notifications in the widgets
    
    This is only WIP, but it at least updates the selected range correctly
    when the image precision changes.

 app/widgets/gimphistogrambox.c  |    2 +
 app/widgets/gimphistogramview.c |  125 +++++++++++++++++++++++++++------------
 app/widgets/gimphistogramview.h |    1 +
 3 files changed, 89 insertions(+), 39 deletions(-)
---
diff --git a/app/widgets/gimphistogrambox.c b/app/widgets/gimphistogrambox.c
index 8273f4e..5519560 100644
--- a/app/widgets/gimphistogrambox.c
+++ b/app/widgets/gimphistogrambox.c
@@ -227,6 +227,8 @@ gimp_histogram_box_histogram_range (GimpHistogramView *view,
                                     GimpHistogramBox  *box)
 {
   gtk_adjustment_set_lower (box->high_adj, start);
+  gtk_adjustment_set_upper (box->high_adj, view->n_bins - 1);
+
   gtk_adjustment_set_upper (box->low_adj,  end);
 
   gtk_adjustment_set_value (box->low_adj,  start);
diff --git a/app/widgets/gimphistogramview.c b/app/widgets/gimphistogramview.c
index 4554441..39b5ba7 100644
--- a/app/widgets/gimphistogramview.c
+++ b/app/widgets/gimphistogramview.c
@@ -17,6 +17,8 @@
 
 #include "config.h"
 
+#include <string.h>
+
 #include <gegl.h>
 #include <gtk/gtk.h>
 
@@ -49,17 +51,17 @@ enum
 };
 
 
-static void  gimp_histogram_view_finalize          (GObject        *object);
-static void  gimp_histogram_view_set_property      (GObject        *object,
+static void     gimp_histogram_view_finalize       (GObject        *object);
+static void     gimp_histogram_view_set_property   (GObject        *object,
                                                     guint           property_id,
                                                     const GValue   *value,
                                                     GParamSpec     *pspec);
-static void  gimp_histogram_view_get_property      (GObject        *object,
+static void     gimp_histogram_view_get_property   (GObject        *object,
                                                     guint           property_id,
                                                     GValue         *value,
                                                     GParamSpec     *pspec);
 
-static void  gimp_histogram_view_size_request      (GtkWidget      *widget,
+static void     gimp_histogram_view_size_request   (GtkWidget      *widget,
                                                     GtkRequisition *requisition);
 static gboolean gimp_histogram_view_expose         (GtkWidget      *widget,
                                                     GdkEventExpose *event);
@@ -70,19 +72,24 @@ static gboolean gimp_histogram_view_button_release (GtkWidget      *widget,
 static gboolean gimp_histogram_view_motion_notify  (GtkWidget      *widget,
                                                     GdkEventMotion *bevent);
 
-static void     gimp_histogram_view_draw_spike (GimpHistogramView    *view,
-                                                GimpHistogramChannel  channel,
-                                                cairo_t              *cr,
-                                                const GdkColor       *fg_color,
-                                                cairo_operator_t      fg_operator,
-                                                const GdkColor       *bg_color,
-                                                gint                  x,
-                                                gint                  i,
-                                                gint                  j,
-                                                gdouble               max,
-                                                gdouble               bg_max,
-                                                gint                  height,
-                                                gint                  border);
+static void     gimp_histogram_view_notify      (GimpHistogram        *histogram,
+                                                 const GParamSpec     *pspec,
+                                                 GimpHistogramView    *view);
+static void     gimp_histogram_view_update_bins (GimpHistogramView    *view);
+
+static void     gimp_histogram_view_draw_spike  (GimpHistogramView    *view,
+                                                 GimpHistogramChannel  channel,
+                                                 cairo_t              *cr,
+                                                 const GdkColor       *fg_color,
+                                                 cairo_operator_t      fg_operator,
+                                                 const GdkColor       *bg_color,
+                                                 gint                  x,
+                                                 gint                  i,
+                                                 gint                  j,
+                                                 gdouble               max,
+                                                 gdouble               bg_max,
+                                                 gint                  height,
+                                                 gint                  border);
 
 
 G_DEFINE_TYPE (GimpHistogramView, gimp_histogram_view,
@@ -157,6 +164,7 @@ gimp_histogram_view_init (GimpHistogramView *view)
 {
   view->histogram    = NULL;
   view->bg_histogram = NULL;
+  view->n_bins       = 256;
   view->start        = 0;
   view->end          = 255;
 }
@@ -284,7 +292,6 @@ gimp_histogram_view_expose (GtkWidget      *widget,
   GtkStyle          *style = gtk_widget_get_style (widget);
   GtkAllocation      allocation;
   cairo_t           *cr;
-  gint               n_bins = 256;
   gint               x;
   gint               x1, x2;
   gint               border;
@@ -329,11 +336,8 @@ gimp_histogram_view_expose (GtkWidget      *widget,
       return FALSE;
     }
 
-  if (view->histogram)
-    n_bins = gimp_histogram_n_bins (view->histogram);
-
-  x1 = CLAMP (MIN (view->start, view->end), 0, n_bins - 1);
-  x2 = CLAMP (MAX (view->start, view->end), 0, n_bins - 1);
+  x1 = CLAMP (MIN (view->start, view->end), 0, view->n_bins - 1);
+  x2 = CLAMP (MAX (view->start, view->end), 0, view->n_bins - 1);
 
   if (view->histogram)
     max = gimp_histogram_view_get_maximum (view, view->histogram,
@@ -364,10 +368,10 @@ gimp_histogram_view_expose (GtkWidget      *widget,
     {
       gboolean  in_selection = FALSE;
 
-      gint  i = (x * n_bins) / width;
-      gint  j = ((x + 1) * n_bins) / width;
+      gint  i = (x * view->n_bins) / width;
+      gint  j = ((x + 1) * view->n_bins) / width;
 
-      if (! (x1 == 0 && x2 == (n_bins - 1)))
+      if (! (x1 == 0 && x2 == (view->n_bins - 1)))
         {
           gint k = i;
 
@@ -531,7 +535,6 @@ gimp_histogram_view_button_press (GtkWidget      *widget,
     {
       GtkAllocation allocation;
       gint          width;
-      gint          n_bins = 256;
 
       gtk_grab_add (widget);
 
@@ -539,11 +542,8 @@ gimp_histogram_view_button_press (GtkWidget      *widget,
 
       width = allocation.width - 2 * view->border_width;
 
-      if (view->histogram)
-        n_bins = gimp_histogram_n_bins (view->histogram);
-
-      view->start = CLAMP ((((bevent->x - view->border_width) * n_bins) / width),
-                           0, n_bins - 1);
+      view->start = CLAMP (((bevent->x - view->border_width) * view->n_bins) / width,
+                           0, view->n_bins - 1);
       view->end   = view->start;
 
       gtk_widget_queue_draw (widget);
@@ -584,23 +584,22 @@ gimp_histogram_view_motion_notify (GtkWidget      *widget,
   GimpHistogramView *view = GIMP_HISTOGRAM_VIEW (widget);
   GtkAllocation      allocation;
   gint               width;
-  gint               n_bins = 256;
 
   gtk_widget_get_allocation (widget, &allocation);
 
   width = allocation.width - 2 * view->border_width;
 
-  if (view->histogram)
-    n_bins = gimp_histogram_n_bins (view->histogram);
-
-  view->start = CLAMP ((((mevent->x - view->border_width) * n_bins) / width),
-                       0, n_bins - 1);
+  view->start = CLAMP (((mevent->x - view->border_width) * view->n_bins) / width,
+                       0, view->n_bins - 1);
 
   gtk_widget_queue_draw (widget);
 
   return TRUE;
 }
 
+
+/*  public funcions  */
+
 GtkWidget *
 gimp_histogram_view_new (gboolean range)
 {
@@ -630,7 +629,12 @@ gimp_histogram_view_set_histogram (GimpHistogramView *view,
   if (view->histogram != histogram)
     {
       if (view->histogram)
-        g_object_unref (view->histogram);
+        {
+          g_signal_handlers_disconnect_by_func (view->histogram,
+                                                gimp_histogram_view_notify,
+                                                view);
+          g_object_unref (view->histogram);
+        }
 
       view->histogram = histogram;
 
@@ -638,6 +642,13 @@ gimp_histogram_view_set_histogram (GimpHistogramView *view,
         {
           g_object_ref (histogram);
 
+          g_signal_connect (histogram, "notify",
+                            G_CALLBACK (gimp_histogram_view_notify),
+                            view);
+
+          if (view->n_bins != gimp_histogram_n_bins (histogram))
+            gimp_histogram_view_update_bins (view);
+
           if (view->channel >= gimp_histogram_n_channels (histogram))
             gimp_histogram_view_set_channel (view, GIMP_HISTOGRAM_VALUE);
         }
@@ -759,3 +770,39 @@ gimp_histogram_view_get_range (GimpHistogramView *view,
   if (start) *start = view->start;
   if (end)   *end   = view->end;
 }
+
+
+/*  private functions  */
+
+static void
+gimp_histogram_view_notify (GimpHistogram     *histogram,
+                            const GParamSpec  *pspec,
+                            GimpHistogramView *view)
+{
+  if (! strcmp (pspec->name, "n-bins"))
+    {
+      gimp_histogram_view_update_bins (view);
+    }
+  else
+    {
+      gtk_widget_queue_draw (GTK_WIDGET (view));
+    }
+}
+
+static void
+gimp_histogram_view_update_bins (GimpHistogramView *view)
+{
+  gint new_bins = gimp_histogram_n_bins (view->histogram);
+
+  view->start = ROUND (((gdouble) view->start *
+                        (new_bins - 1) /
+                        (view->n_bins - 1)));
+  view->end   = ROUND (((gdouble) view->end   *
+                        (new_bins - 1) /
+                        (view->n_bins - 1)));
+
+  view->n_bins = new_bins;
+
+  g_signal_emit (view, histogram_view_signals[RANGE_CHANGED], 0,
+                 view->start, view->end);
+}
diff --git a/app/widgets/gimphistogramview.h b/app/widgets/gimphistogramview.h
index 3dd810f..cbbfdb1 100644
--- a/app/widgets/gimphistogramview.h
+++ b/app/widgets/gimphistogramview.h
@@ -37,6 +37,7 @@ struct _GimpHistogramView
   GimpHistogram         *bg_histogram;
   GimpHistogramChannel   channel;
   GimpHistogramScale     scale;
+  gint                   n_bins;
   gint                   start;
   gint                   end;
 


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