gimphistogrambox.c 14.6 KB
Newer Older
Michael Natterer's avatar
Michael Natterer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"
20

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

Michael Natterer's avatar
Michael Natterer committed
23 24 25 26 27 28 29 30
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "base/gimphistogram.h"

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

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


39
/*  #define DEBUG_VIEW  */
40

41
#define GRADIENT_HEIGHT  12
42 43 44
#define CONTROL_HEIGHT    8

#define HISTOGRAM_EVENT_MASK  (GDK_BUTTON_PRESS_MASK   | \
45 46
                               GDK_BUTTON_RELEASE_MASK | \
                               GDK_BUTTON_MOTION_MASK)
47 48


Michael Natterer's avatar
Michael Natterer committed
49 50
/*  local function prototypes  */

51
static void   gimp_histogram_box_init            (GimpHistogramBox  *box);
52 53 54 55 56 57 58 59 60

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);
61 62 63 64 65 66
static void   gimp_histogram_box_channel_notify  (GimpHistogramView *view,
                                                  GParamSpec        *pspec,
                                                  GimpColorBar      *bar);
static void   gimp_histogram_box_border_notify   (GimpHistogramView *view,
                                                  GParamSpec        *pspec,
                                                  GimpColorBar      *bar);
67
static gboolean gimp_histogram_slider_area_event (GtkWidget         *widget,
68 69
                                                  GdkEvent          *event,
                                                  GimpHistogramBox  *box);
70
static gboolean gimp_histogram_slider_area_expose (GtkWidget        *widget,
71 72
                                                  GdkEvent          *event,
                                                  GimpHistogramBox  *box);
73
static void gimp_histogram_draw_slider           (GtkWidget *widget,
74 75 76
                                                  GdkGC     *border_gc,
                                                  GdkGC     *fill_gc,
                                                  gint       xpos);
Michael Natterer's avatar
Michael Natterer committed
77 78 79 80 81 82 83 84 85 86 87

GType
gimp_histogram_box_get_type (void)
{
  static GType box_type = 0;

  if (! box_type)
    {
      static const GTypeInfo box_info =
      {
        sizeof (GimpHistogramBoxClass),
88 89 90
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) NULL,
91
        NULL,           /* class_finalize */
92 93 94 95
        NULL,           /* class_data     */
        sizeof (GimpHistogramBox),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_histogram_box_init,
Michael Natterer's avatar
Michael Natterer committed
96 97 98
      };

      box_type = g_type_register_static (GTK_TYPE_VBOX,
99
                                         "GimpHistogramBox",
Michael Natterer's avatar
Michael Natterer committed
100 101 102 103 104 105 106 107 108 109
                                         &box_info, 0);
    }

  return box_type;
}

static void
gimp_histogram_box_init (GimpHistogramBox *box)
{
  GtkWidget *hbox;
110
  GtkWidget *vbox;
Michael Natterer's avatar
Michael Natterer committed
111 112 113
  GtkWidget *spinbutton;
  GtkObject *adjustment;
  GtkWidget *frame;
114
  GtkWidget *view;
115
  GtkWidget *bar;
116
  GtkWidget *slider_area;
Michael Natterer's avatar
Michael Natterer committed
117

118
  gtk_box_set_spacing (GTK_BOX (box), 2);
Michael Natterer's avatar
Michael Natterer committed
119 120 121 122

  /*  The histogram  */
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
123
  gtk_box_pack_start (GTK_BOX (box), frame, TRUE, TRUE, 0);
Michael Natterer's avatar
Michael Natterer committed
124 125
  gtk_widget_show (frame);

126
  view = gimp_histogram_view_new (TRUE);
127 128
  gtk_container_add (GTK_CONTAINER (frame), view);
  gtk_widget_show (view);
Michael Natterer's avatar
Michael Natterer committed
129

130
  g_signal_connect (view, "range_changed",
Michael Natterer's avatar
Michael Natterer committed
131 132
                    G_CALLBACK (gimp_histogram_box_histogram_range),
                    box);
133

134
  box->view = GIMP_HISTOGRAM_VIEW (view);
135 136 137 138 139 140 141

  /*  The gradient below the histogram */
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  box->slider_area = slider_area = gtk_event_box_new ();
  gtk_widget_set_size_request (slider_area, -1,
                               GRADIENT_HEIGHT + CONTROL_HEIGHT);
  gtk_widget_add_events (slider_area, HISTOGRAM_EVENT_MASK);
  gtk_container_add (GTK_CONTAINER (frame), slider_area);
  gtk_widget_show (slider_area);

  g_signal_connect (slider_area, "event",
                    G_CALLBACK (gimp_histogram_slider_area_event),
                    box);
  g_signal_connect_after (slider_area, "expose_event",
                          G_CALLBACK (gimp_histogram_slider_area_expose),
                          box);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (slider_area), vbox);
  gtk_widget_show (vbox);

160
  bar = g_object_new (GIMP_TYPE_COLOR_BAR,
161 162 163
                      "histogram-channel", box->view->channel,
                      "xpad",              box->view->border_width,
                      "ypad",              box->view->border_width,
164
                      NULL);
165

166 167 168
  gtk_widget_set_size_request (bar,
                               -1,
                               GRADIENT_HEIGHT + 2 * box->view->border_width);
169
  gtk_box_pack_start (GTK_BOX (vbox), bar, FALSE, FALSE, 0);
170
  gtk_widget_show (bar);
171

172 173 174 175 176 177
  g_signal_connect (view, "notify::histogram-channel",
                    G_CALLBACK (gimp_histogram_box_channel_notify),
                    bar);
  g_signal_connect (view, "notify::border-width",
                    G_CALLBACK (gimp_histogram_box_border_notify),
                    bar);
178 179

  /*  The range selection */
180
  hbox = gtk_hbox_new (FALSE, 6);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  gtk_box_pack_start (GTK_BOX (box), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  /*  low spinbutton  */
  spinbutton = gimp_spin_button_new (&adjustment,
                                     0.0, 0.0, 255.0, 1.0, 16.0, 0.0,
                                     1.0, 0);
  box->low_adj = GTK_ADJUSTMENT (adjustment);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  g_signal_connect (adjustment, "value_changed",
                    G_CALLBACK (gimp_histogram_box_low_adj_update),
                    box);

  /*  high spinbutton  */
  spinbutton = gimp_spin_button_new (&adjustment,
                                     255.0, 0.0, 255.0, 1.0, 16.0, 0.0,
                                     1.0, 0);
  box->high_adj = GTK_ADJUSTMENT (adjustment);
201
  gtk_box_pack_end (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
202 203 204 205 206
  gtk_widget_show (spinbutton);

  g_signal_connect (adjustment, "value_changed",
                    G_CALLBACK (gimp_histogram_box_high_adj_update),
                    box);
207 208 209 210 211 212 213 214 215 216 217 218

#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
219 220 221 222 223 224
}

static void
gimp_histogram_box_low_adj_update (GtkAdjustment    *adjustment,
                                   GimpHistogramBox *box)
{
225
  if ((gdouble) box->view->start == adjustment->value)
226
    return;
Michael Natterer's avatar
Michael Natterer committed
227

228 229
  box->high_adj->lower = adjustment->value;
  gtk_adjustment_changed (box->high_adj);
230

231 232
  gimp_histogram_view_set_range (box->view,
                                 adjustment->value, box->view->end);
233
  gtk_widget_queue_draw (box->slider_area);
Michael Natterer's avatar
Michael Natterer committed
234 235 236 237 238 239
}

static void
gimp_histogram_box_high_adj_update (GtkAdjustment    *adjustment,
                                    GimpHistogramBox *box)
{
240
  if ((gdouble) box->view->end == adjustment->value)
241
    return;
Michael Natterer's avatar
Michael Natterer committed
242

243 244
  box->low_adj->upper = adjustment->value;
  gtk_adjustment_changed (box->low_adj);
245

246 247
  gimp_histogram_view_set_range (box->view,
                                 box->view->start, adjustment->value);
248
  gtk_widget_queue_draw (box->slider_area);
Michael Natterer's avatar
Michael Natterer committed
249 250 251
}

static void
252
gimp_histogram_box_histogram_range (GimpHistogramView *view,
Michael Natterer's avatar
Michael Natterer committed
253 254 255 256 257 258 259 260 261 262 263
                                    gint               start,
                                    gint               end,
                                    GimpHistogramBox  *box)
{
  box->high_adj->lower = start;
  box->low_adj->upper  = end;
  gtk_adjustment_changed (box->high_adj);
  gtk_adjustment_changed (box->low_adj);

  gtk_adjustment_set_value (box->low_adj,  start);
  gtk_adjustment_set_value (box->high_adj, end);
264
  gtk_widget_queue_draw (box->slider_area);
Michael Natterer's avatar
Michael Natterer committed
265 266
}

267
static void
268 269 270
gimp_histogram_box_channel_notify (GimpHistogramView *view,
                                   GParamSpec        *pspec,
                                   GimpColorBar      *bar)
271
{
272
  gimp_color_bar_set_channel (bar, view->channel);
273 274
}

275 276 277 278
static void
gimp_histogram_box_border_notify (GimpHistogramView *view,
                                  GParamSpec        *pspec,
                                  GimpColorBar      *bar)
279
{
280 281 282 283
  g_object_set (bar,
                "xpad", view->border_width,
                "ypad", view->border_width,
                NULL);
284

285 286
  gtk_widget_set_size_request (GTK_WIDGET (bar),
                               -1, GRADIENT_HEIGHT + 2 * view->border_width);
287 288
}

Michael Natterer's avatar
Michael Natterer committed
289
GtkWidget *
290
gimp_histogram_box_new (void)
Michael Natterer's avatar
Michael Natterer committed
291
{
292
  return g_object_new (GIMP_TYPE_HISTOGRAM_BOX, NULL);
Michael Natterer's avatar
Michael Natterer committed
293
}
294 295 296 297 298 299 300

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

301 302
  if (box->view)
    gimp_histogram_view_set_channel (box->view, channel);
303
}
304

305
static gboolean
306
gimp_histogram_slider_area_event (GtkWidget         *widget,
307 308
                                  GdkEvent          *event,
                                  GimpHistogramBox  *box)
309 310 311 312 313 314 315 316 317 318 319 320 321 322
{
  GdkEventButton *bevent;
  GdkEventMotion *mevent;
  gint            x, distance;
  gint            i;
  gboolean        update = FALSE;

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
      bevent = (GdkEventButton *) event;

      distance = G_MAXINT;
      for (i = 0; i < 2; i++)
323 324 325 326 327
        if (fabs (bevent->x - box->slider_pos[i]) < distance)
          {
            box->active_slider = i;
            distance = fabs (bevent->x - box->slider_pos[i]);
          }
328 329 330 331 332 333 334

      x = bevent->x;
      update = TRUE;
      break;

    case GDK_BUTTON_RELEASE:
      switch (box->active_slider)
335 336 337 338 339 340 341 342
        {
        case 3:  /*  low output  */
          gtk_adjustment_set_value (box->low_adj, box->low_slider_val);
          break;
        case 4:  /*  high output  */
          gtk_adjustment_set_value (box->high_adj, box->high_slider_val);
          break;
        }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

      break;

    case GDK_MOTION_NOTIFY:
      mevent = (GdkEventMotion *) event;
      gdk_window_get_pointer (widget->window, &x, NULL, NULL);
      update = TRUE;
      break;

    default:
      break;
    }

  if (update)
    {
      gint  width;
      gint  border;

      g_object_get (box->view, "border-width", &border, NULL);

      width = widget->allocation.width - 2 * border;

      if (width < 1)
        return FALSE;

      switch (box->active_slider)
369 370 371
        {
        case 0:  /*  low output  */
          box->low_adj->value =
372 373
            ((gdouble) (x - border) / (gdouble) width) * 255.0;

374
          box->low_adj->value =
375 376
            CLAMP (box->low_adj->value, 0, 255);

377 378
          gimp_histogram_box_low_adj_update (box->low_adj, box);
          break;
379

380 381
        case 1:  /*  high output  */
          box->high_adj->value =
382 383
            ((gdouble) (x - border) / (gdouble) width) * 255.0;

384
          box->high_adj->value =
385 386
            CLAMP (box->high_adj->value, 0, 255);

387 388 389
          gimp_histogram_box_high_adj_update (box->high_adj, box);
          break;
        }
390 391 392 393 394 395

    }

  return FALSE;
}

396
static gboolean
397
gimp_histogram_slider_area_expose (GtkWidget        *widget,
398 399
                                   GdkEvent          *event,
                                   GimpHistogramBox  *box)
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
{
  gint     width  = widget->allocation.width;
  gint     border;

  g_object_get (box->view, "border-width", &border, NULL);

  width -= 2 * border;

  box->slider_pos[0] = ROUND ((gdouble) width *
                               box->high_adj->lower /
                               256.0) + border;

  box->slider_pos[1] = ROUND ((gdouble) width *
                               box->low_adj->upper /
                               256.0) + border;

  gimp_histogram_draw_slider (widget,
417 418 419
                              widget->style->black_gc,
                              widget->style->black_gc,
                              box->slider_pos[0]);
420
  gimp_histogram_draw_slider (widget,
421 422 423
                              widget->style->black_gc,
                              widget->style->white_gc,
                              box->slider_pos[1]);
424 425 426 427 428 429

  return FALSE;
}

static void
gimp_histogram_draw_slider (GtkWidget *widget,
430 431 432
                    GdkGC     *border_gc,
                    GdkGC     *fill_gc,
                    gint       xpos)
433 434 435 436 437 438 439 440 441 442 443
{
  gint y;

  for (y = 0; y < CONTROL_HEIGHT; y++)
    gdk_draw_line (widget->window, fill_gc,
                   xpos - y / 2, GRADIENT_HEIGHT + y,
                   xpos + y / 2, GRADIENT_HEIGHT + y);

  gdk_draw_line (widget->window, border_gc,
                 xpos,
                 GRADIENT_HEIGHT,
444
                 xpos - (CONTROL_HEIGHT - 1) / 2,
445 446 447 448 449
                 GRADIENT_HEIGHT + CONTROL_HEIGHT - 1);

  gdk_draw_line (widget->window, border_gc,
                 xpos,
                 GRADIENT_HEIGHT,
450
                 xpos + (CONTROL_HEIGHT - 1) / 2,
451 452 453 454 455
                 GRADIENT_HEIGHT + CONTROL_HEIGHT - 1);

  gdk_draw_line (widget->window, border_gc,
                 xpos - (CONTROL_HEIGHT - 1) / 2,
                 GRADIENT_HEIGHT + CONTROL_HEIGHT - 1,
456
                 xpos + (CONTROL_HEIGHT - 1) / 2,
457 458 459
                 GRADIENT_HEIGHT + CONTROL_HEIGHT - 1);
}