gimphistogrambox.c 9.87 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Michael Natterer's avatar
Michael Natterer committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Michael Natterer's avatar
Michael Natterer committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Michael Natterer's avatar
Michael Natterer committed
7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Michael Natterer's avatar
Michael Natterer committed
16 17 18
 */

#include "config.h"
19

Hans Breuer's avatar
Hans Breuer committed
20 21
#include <string.h>

22
#include <gegl.h>
Michael Natterer's avatar
Michael Natterer committed
23 24
#include <gtk/gtk.h>

25
#include "libgimpmath/gimpmath.h"
Michael Natterer's avatar
Michael Natterer committed
26 27 28 29
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

30
#include "core/gimphistogram.h"
Michael Natterer's avatar
Michael Natterer committed
31

32
#include "gimpcolorbar.h"
33
#include "gimphandlebar.h"
Michael Natterer's avatar
Michael Natterer committed
34 35 36
#include "gimphistogrambox.h"
#include "gimphistogramview.h"

37
#include "gimp-intl.h"
Michael Natterer's avatar
Michael Natterer committed
38 39


40
/*  #define DEBUG_VIEW  */
41

42
#define GRADIENT_HEIGHT  12
43
#define CONTROL_HEIGHT   10
44 45


Michael Natterer's avatar
Michael Natterer committed
46 47
/*  local function prototypes  */

48 49 50 51 52 53 54 55
static void   gimp_histogram_box_low_adj_update  (GtkAdjustment     *adj,
                                                  GimpHistogramBox  *box);
static void   gimp_histogram_box_high_adj_update (GtkAdjustment     *adj,
                                                  GimpHistogramBox  *box);
static void   gimp_histogram_box_histogram_range (GimpHistogramView *view,
                                                  gint               start,
                                                  gint               end,
                                                  GimpHistogramBox  *box);
56 57
static void   gimp_histogram_box_channel_notify  (GimpHistogramView *view,
                                                  GParamSpec        *pspec,
58
                                                  GimpHistogramBox  *box);
59 60
static void   gimp_histogram_box_border_notify   (GimpHistogramView *view,
                                                  GParamSpec        *pspec,
61
                                                  GimpHistogramBox  *box);
62

63

64
G_DEFINE_TYPE (GimpHistogramBox, gimp_histogram_box, GTK_TYPE_BOX)
65 66 67 68


static void
gimp_histogram_box_class_init (GimpHistogramBoxClass *klass)
Michael Natterer's avatar
Michael Natterer committed
69 70 71 72 73 74 75
{
}

static void
gimp_histogram_box_init (GimpHistogramBox *box)
{
  GtkWidget *hbox;
76
  GtkWidget *vbox;
77
  GtkWidget *vbox2;
Michael Natterer's avatar
Michael Natterer committed
78
  GtkWidget *frame;
79
  GtkWidget *view;
80
  GtkWidget *bar;
Michael Natterer's avatar
Michael Natterer committed
81

82 83
  box->n_bins = 256;

84 85 86
  gtk_orientable_set_orientation (GTK_ORIENTABLE (box),
                                  GTK_ORIENTATION_VERTICAL);

87
  gtk_box_set_spacing (GTK_BOX (box), 2);
Michael Natterer's avatar
Michael Natterer committed
88 89 90

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
91
  gtk_box_pack_start (GTK_BOX (box), frame, TRUE, TRUE, 0);
Michael Natterer's avatar
Michael Natterer committed
92 93
  gtk_widget_show (frame);

94
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
95 96 97 98
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  /*  The histogram  */
99
  view = gimp_histogram_view_new (TRUE);
100
  gtk_box_pack_start (GTK_BOX (vbox), view, TRUE, TRUE, 0);
101
  gtk_widget_show (view);
Michael Natterer's avatar
Michael Natterer committed
102

103
  g_signal_connect (view, "range-changed",
Michael Natterer's avatar
Michael Natterer committed
104 105
                    G_CALLBACK (gimp_histogram_box_histogram_range),
                    box);
106

107
  box->view = GIMP_HISTOGRAM_VIEW (view);
108 109

  /*  The gradient below the histogram */
110
  vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
111 112 113 114 115 116 117 118 119 120
  gtk_container_set_border_width (GTK_CONTAINER (vbox2),
                                  GIMP_HISTOGRAM_VIEW (view)->border_width);
  gtk_box_pack_start (GTK_BOX (vbox), vbox2, FALSE, FALSE, 0);
  gtk_widget_show (vbox2);

  box->color_bar = bar = g_object_new (GIMP_TYPE_COLOR_BAR,
                                       "histogram-channel", box->view->channel,
                                       NULL);
  gtk_widget_set_size_request (bar, -1, GRADIENT_HEIGHT);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, FALSE, FALSE, 0);
121
  gtk_widget_show (bar);
122

123 124
  g_signal_connect (view, "notify::histogram-channel",
                    G_CALLBACK (gimp_histogram_box_channel_notify),
125
                    box);
126 127
  g_signal_connect (view, "notify::border-width",
                    G_CALLBACK (gimp_histogram_box_border_notify),
128 129 130 131 132 133 134
                    box);

  box->slider_bar = bar = g_object_new (GIMP_TYPE_HANDLE_BAR, NULL);
  gtk_widget_set_size_request (bar, -1, CONTROL_HEIGHT);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, FALSE, FALSE, 0);
  gtk_widget_show (bar);

135 136
  gimp_handle_bar_connect_events (GIMP_HANDLE_BAR (box->slider_bar),
                                  box->color_bar);
137 138

  /*  The range selection */
139
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
140 141 142 143
  gtk_box_pack_start (GTK_BOX (box), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  /*  low spinbutton  */
144
  box->low_adj = gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 16.0, 0.0);
145 146
  box->low_spinbutton = gtk_spin_button_new (box->low_adj, 1.0, 0);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (box->low_spinbutton), TRUE);
147 148
  gtk_box_pack_start (GTK_BOX (hbox), box->low_spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (box->low_spinbutton);
149

150
  g_signal_connect (box->low_adj, "value-changed",
151 152 153
                    G_CALLBACK (gimp_histogram_box_low_adj_update),
                    box);

154
  gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (bar), 0, box->low_adj);
155

156
  /*  high spinbutton  */
157
  box->high_adj = gtk_adjustment_new (255.0, 0.0, 255.0, 1.0, 16.0, 0.0);
158 159
  box->high_spinbutton = gtk_spin_button_new (box->high_adj, 1.0, 0);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (box->high_spinbutton), TRUE);
160 161
  gtk_box_pack_end (GTK_BOX (hbox), box->high_spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (box->high_spinbutton);
162

163
  g_signal_connect (box->high_adj, "value-changed",
164 165
                    G_CALLBACK (gimp_histogram_box_high_adj_update),
                    box);
166

167
  gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (bar), 2, box->high_adj);
168

169 170 171 172 173 174 175 176 177 178 179
#ifdef DEBUG_VIEW
  spinbutton = gimp_prop_spin_button_new (G_OBJECT (box->view), "border-width",
                                          1, 5, 0);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  spinbutton = gimp_prop_spin_button_new (G_OBJECT (box->view), "subdivisions",
                                          1, 5, 0);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);
#endif
Michael Natterer's avatar
Michael Natterer committed
180 181 182 183 184 185
}

static void
gimp_histogram_box_low_adj_update (GtkAdjustment    *adjustment,
                                   GimpHistogramBox *box)
{
186
  gdouble value = gtk_adjustment_get_value (adjustment);
Michael Natterer's avatar
Michael Natterer committed
187

188
  gtk_adjustment_set_lower (box->high_adj, value);
189

190 191 192 193 194 195 196
  if (box->n_bins != 256)
    value *= box->n_bins - 1;

  value = ROUND (value);

  if (box->view->start != value)
    gimp_histogram_view_set_range (box->view, value, box->view->end);
Michael Natterer's avatar
Michael Natterer committed
197 198 199 200 201 202
}

static void
gimp_histogram_box_high_adj_update (GtkAdjustment    *adjustment,
                                    GimpHistogramBox *box)
{
203
  gdouble value = gtk_adjustment_get_value (adjustment);
Michael Natterer's avatar
Michael Natterer committed
204

205
  gtk_adjustment_set_upper (box->low_adj, value);
206

207 208 209 210 211 212 213
  if (box->n_bins != 256)
    value *= box->n_bins - 1;

  value = ROUND (value);

  if (box->view->end != value)
    gimp_histogram_view_set_range (box->view, box->view->start, value);
Michael Natterer's avatar
Michael Natterer committed
214 215 216
}

static void
217
gimp_histogram_box_histogram_range (GimpHistogramView *view,
Michael Natterer's avatar
Michael Natterer committed
218 219 220 221
                                    gint               start,
                                    gint               end,
                                    GimpHistogramBox  *box)
{
222 223 224 225 226
  gdouble s = start;
  gdouble e = end;

  if (box->n_bins != view->n_bins)
    {
227 228 229 230 231
      gdouble upper;
      gdouble page_increment;
      gdouble step_increment;
      guint   digits;

232 233 234 235
      box->n_bins = view->n_bins;

      if (box->n_bins == 256)
        {
236 237 238 239
          digits         = 0;
          upper          = 255.0;
          step_increment = 1.0;
          page_increment = 16.0;
240 241 242
        }
      else
        {
243 244 245 246
          digits         = 3;
          upper          = 1.0;
          step_increment = 0.01;
          page_increment = 0.1;
247
        }
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

      g_object_set (G_OBJECT (box->high_adj),
                    "upper", upper,
                    "step-increment", step_increment,
                    "page-increment", page_increment,
                    NULL);

      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (box->high_spinbutton), digits);

      g_object_set (G_OBJECT (box->low_adj),
                    "step-increment", step_increment,
                    "page-increment", page_increment,
                    NULL);

      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (box->low_spinbutton), digits);
263 264 265 266 267 268 269
    }

  if (box->n_bins != 256)
    {
      s /= box->n_bins - 1;
      e /= box->n_bins - 1;
    }
270

271 272
  gtk_adjustment_set_lower (box->high_adj, s);
  gtk_adjustment_set_upper (box->low_adj,  e);
Michael Natterer's avatar
Michael Natterer committed
273

274 275
  gtk_adjustment_set_value (box->low_adj,  s);
  gtk_adjustment_set_value (box->high_adj, e);
Michael Natterer's avatar
Michael Natterer committed
276 277
}

278
static void
279 280
gimp_histogram_box_channel_notify (GimpHistogramView *view,
                                   GParamSpec        *pspec,
281
                                   GimpHistogramBox  *box)
282
{
283
  gimp_color_bar_set_channel (GIMP_COLOR_BAR (box->color_bar), view->channel);
284 285
}

286 287 288
static void
gimp_histogram_box_border_notify (GimpHistogramView *view,
                                  GParamSpec        *pspec,
289
                                  GimpHistogramBox  *box)
290
{
291 292 293
  GtkWidget *vbox = gtk_widget_get_parent (box->color_bar);

  gtk_container_set_border_width (GTK_CONTAINER (vbox), view->border_width);
294 295
}

296 297 298

/*  public functions  */

Michael Natterer's avatar
Michael Natterer committed
299
GtkWidget *
300
gimp_histogram_box_new (void)
Michael Natterer's avatar
Michael Natterer committed
301
{
302
  return g_object_new (GIMP_TYPE_HISTOGRAM_BOX, NULL);
Michael Natterer's avatar
Michael Natterer committed
303
}
304 305 306 307 308 309 310

void
gimp_histogram_box_set_channel (GimpHistogramBox     *box,
                                GimpHistogramChannel  channel)
{
  g_return_if_fail (GIMP_IS_HISTOGRAM_BOX (box));

311 312
  if (box->view)
    gimp_histogram_view_set_channel (box->view, channel);
313
}