gtkhsv.c 38.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* HSV color selector for GTK+
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code)
 *          Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+)
 *          Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+)
 *
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11 12 13 14 15 16
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20 21 22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

25 26 27 28 29 30 31
/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

32
#include "config.h"
33

34
#include <math.h>
35
#include <string.h>
36 37

#include "gtkhsv.h"
38
#include "gtkbindings.h"
39
#include "gtkmarshalers.h"
40
#include "gtktypebuiltins.h"
41
#include "gtkintl.h"
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

/**
 * SECTION:gtkhsv
 * @Short_description: A 'color wheel' widget
 * @Title: GtkHSV
 * @See_also: #GtkColorSelection, #GtkColorSelectionDialog
 *
 * #GtkHSV is the 'color wheel' part of a complete color selector widget.
 * It allows to select a color by determining its HSV components in an
 * intuitive way. Moving the selection around the outer ring changes the hue,
 * and moving the selection point inside the inner triangle changes value and
 * saturation.
 */


58 59 60 61 62 63 64 65 66
/* Default width/height */
#define DEFAULT_SIZE 100

/* Default ring width */
#define DEFAULT_RING_WIDTH 10


/* Dragging modes */
typedef enum {
67 68 69
  DRAG_NONE,
  DRAG_H,
  DRAG_SV
70 71 72
} DragMode;

/* Private part of the GtkHSV structure */
73
struct _GtkHSVPrivate
74
{
75 76 77 78 79 80 81 82 83 84 85 86 87 88
  /* Color value */
  double h;
  double s;
  double v;
  
  /* Size and ring width */
  int size;
  int ring_width;
  
  /* Window for capturing events */
  GdkWindow *window;
  
  /* Dragging mode */
  DragMode mode;
89 90

  guint focus_on_ring : 1;
91
};
92 93 94 95 96


/* Signal IDs */

enum {
97
  CHANGED,
98
  MOVE,
99
  LAST_SIGNAL
100 101
};

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
static void     gtk_hsv_destroy              (GtkWidget          *widget);
static void     gtk_hsv_realize              (GtkWidget          *widget);
static void     gtk_hsv_unrealize            (GtkWidget          *widget);
static void     gtk_hsv_get_preferred_width  (GtkWidget          *widget,
                                              gint               *minimum,
                                              gint               *natural);
static void     gtk_hsv_get_preferred_height (GtkWidget          *widget,
                                              gint               *minimum,
                                              gint               *natural);
static void     gtk_hsv_size_allocate        (GtkWidget          *widget,
                                              GtkAllocation      *allocation);
static gboolean gtk_hsv_button_press         (GtkWidget          *widget,
                                              GdkEventButton     *event);
static gboolean gtk_hsv_button_release       (GtkWidget          *widget,
                                              GdkEventButton     *event);
static gboolean gtk_hsv_motion               (GtkWidget          *widget,
                                              GdkEventMotion     *event);
static gboolean gtk_hsv_draw                 (GtkWidget          *widget,
                                              cairo_t            *cr);
static gboolean gtk_hsv_grab_broken          (GtkWidget          *widget,
                                              GdkEventGrabBroken *event);
static gboolean gtk_hsv_focus                (GtkWidget          *widget,
                                              GtkDirectionType    direction);
static void     gtk_hsv_move                 (GtkHSV             *hsv,
                                              GtkDirectionType    dir);
127 128 129

static guint hsv_signals[LAST_SIGNAL];

Matthias Clasen's avatar
Matthias Clasen committed
130
G_DEFINE_TYPE (GtkHSV, gtk_hsv, GTK_TYPE_WIDGET)
131 132 133 134 135

/* Class initialization function for the HSV color selector */
static void
gtk_hsv_class_init (GtkHSVClass *class)
{
136
  GObjectClass   *gobject_class;
137
  GtkWidgetClass *widget_class;
138
  GtkHSVClass    *hsv_class;
Manish Singh's avatar
Manish Singh committed
139
  GtkBindingSet  *binding_set;
140

141
  gobject_class = (GObjectClass *) class;
142
  widget_class = (GtkWidgetClass *) class;
143
  hsv_class = GTK_HSV_CLASS (class);
144 145

  widget_class->destroy = gtk_hsv_destroy;
146 147
  widget_class->realize = gtk_hsv_realize;
  widget_class->unrealize = gtk_hsv_unrealize;
148 149
  widget_class->get_preferred_width = gtk_hsv_get_preferred_width;
  widget_class->get_preferred_height = gtk_hsv_get_preferred_height;
150 151 152 153
  widget_class->size_allocate = gtk_hsv_size_allocate;
  widget_class->button_press_event = gtk_hsv_button_press;
  widget_class->button_release_event = gtk_hsv_button_release;
  widget_class->motion_notify_event = gtk_hsv_motion;
154
  widget_class->draw = gtk_hsv_draw;
155
  widget_class->focus = gtk_hsv_focus;
156
  widget_class->grab_broken_event = gtk_hsv_grab_broken;
157 158

  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_COLOR_CHOOSER);
159

160
  hsv_class->move = gtk_hsv_move;
161

162
  hsv_signals[CHANGED] =
163
    g_signal_new (I_("changed"),
164 165 166 167 168 169
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkHSVClass, changed),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
170 171

  hsv_signals[MOVE] =
172
    g_signal_new (I_("move"),
173 174 175 176 177 178 179
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkHSVClass, move),
                  NULL, NULL,
                  _gtk_marshal_VOID__ENUM,
                  G_TYPE_NONE, 1,
                  GTK_TYPE_DIRECTION_TYPE);
180 181 182

  binding_set = gtk_binding_set_by_class (class);

183
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
184
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
185
                                G_TYPE_ENUM, GTK_DIR_UP);
186
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
187
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
188
                                G_TYPE_ENUM, GTK_DIR_UP);
189
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
190
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
191
                                G_TYPE_ENUM, GTK_DIR_DOWN);
192
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
193
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
194
                                G_TYPE_ENUM, GTK_DIR_DOWN);
195
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0,
196
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
197
                                G_TYPE_ENUM, GTK_DIR_RIGHT);
198
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0,
199
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
200
                                G_TYPE_ENUM, GTK_DIR_RIGHT);
201
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0,
202
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
203
                                G_TYPE_ENUM, GTK_DIR_LEFT);
204
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0,
205
                                "move", 1,
Manish Singh's avatar
Manish Singh committed
206
                                G_TYPE_ENUM, GTK_DIR_LEFT);
207

208
  g_type_class_add_private (gobject_class, sizeof (GtkHSVPrivate));
209 210 211 212 213
}

static void
gtk_hsv_init (GtkHSV *hsv)
{
214
  GtkHSVPrivate *priv;
215

216
  priv = G_TYPE_INSTANCE_GET_PRIVATE (hsv, GTK_TYPE_HSV, GtkHSVPrivate);
217

218
  hsv->priv = priv;
219

220
  gtk_widget_set_has_window (GTK_WIDGET (hsv), FALSE);
221
  gtk_widget_set_can_focus (GTK_WIDGET (hsv), TRUE);
222

223 224 225
  priv->h = 0.0;
  priv->s = 0.0;
  priv->v = 0.0;
226

227 228
  priv->size = DEFAULT_SIZE;
  priv->ring_width = DEFAULT_RING_WIDTH;
229 230 231
}

static void
232
gtk_hsv_destroy (GtkWidget *widget)
233
{
234
  GTK_WIDGET_CLASS (gtk_hsv_parent_class)->destroy (widget);
235 236 237 238 239
}

static void
gtk_hsv_realize (GtkWidget *widget)
{
240
  GtkHSV *hsv = GTK_HSV (widget);
241
  GtkHSVPrivate *priv = hsv->priv;
242
  GtkAllocation allocation;
243
  GdkWindow *parent_window;
244 245
  GdkWindowAttr attr;
  int attr_mask;
246

247
  gtk_widget_set_realized (widget, TRUE);
248 249 250

  gtk_widget_get_allocation (widget, &allocation);

251
  attr.window_type = GDK_WINDOW_CHILD;
252 253 254 255
  attr.x = allocation.x;
  attr.y = allocation.y;
  attr.width = allocation.width;
  attr.height = allocation.height;
256 257
  attr.wclass = GDK_INPUT_ONLY;
  attr.event_mask = gtk_widget_get_events (widget);
258 259
  attr.event_mask |= (GDK_KEY_PRESS_MASK
                      | GDK_BUTTON_PRESS_MASK
260 261
                      | GDK_BUTTON_RELEASE_MASK
                      | GDK_POINTER_MOTION_MASK
262 263
                      | GDK_ENTER_NOTIFY_MASK
                      | GDK_LEAVE_NOTIFY_MASK);
264
  attr_mask = GDK_WA_X | GDK_WA_Y;
265

266
  parent_window = gtk_widget_get_parent_window (widget);
267 268 269
  gtk_widget_set_window (widget, parent_window);
  g_object_ref (parent_window);

270 271
  priv->window = gdk_window_new (parent_window, &attr, attr_mask);
  gdk_window_set_user_data (priv->window, hsv);
272
  gdk_window_show (priv->window);
273 274 275 276 277
}

static void
gtk_hsv_unrealize (GtkWidget *widget)
{
278
  GtkHSV *hsv = GTK_HSV (widget);
279
  GtkHSVPrivate *priv = hsv->priv;
280

281 282 283
  gdk_window_set_user_data (priv->window, NULL);
  gdk_window_destroy (priv->window);
  priv->window = NULL;
284

285
  GTK_WIDGET_CLASS (gtk_hsv_parent_class)->unrealize (widget);
286 287 288
}

static void
289 290 291
gtk_hsv_get_preferred_width (GtkWidget *widget,
                             gint      *minimum,
                             gint      *natural)
292
{
293
  GtkHSV *hsv = GTK_HSV (widget);
294
  GtkHSVPrivate *priv = hsv->priv;
295 296 297 298
  gint focus_width;
  gint focus_pad;

  gtk_widget_style_get (widget,
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
                        "focus-line-width", &focus_width,
                        "focus-padding", &focus_pad,
                        NULL);

  *minimum = priv->size + 2 * (focus_width + focus_pad);
  *natural = priv->size + 2 * (focus_width + focus_pad);
}

static void
gtk_hsv_get_preferred_height (GtkWidget *widget,
                              gint      *minimum,
                              gint      *natural)
{
  GtkHSV *hsv = GTK_HSV (widget);
  GtkHSVPrivate *priv = hsv->priv;
  gint focus_width;
  gint focus_pad;

  gtk_widget_style_get (widget,
                        "focus-line-width", &focus_width,
                        "focus-padding", &focus_pad,
                        NULL);

  *minimum = priv->size + 2 * (focus_width + focus_pad);
  *natural = priv->size + 2 * (focus_width + focus_pad);
324 325 326
}

static void
327
gtk_hsv_size_allocate (GtkWidget     *widget,
328
                       GtkAllocation *allocation)
329
{
330
  GtkHSV *hsv = GTK_HSV (widget);
331
  GtkHSVPrivate *priv = hsv->priv;
332

333 334
  gtk_widget_set_allocation (widget, allocation);

335
  if (gtk_widget_get_realized (widget))
336
    gdk_window_move_resize (priv->window,
337 338 339 340
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);
341 342 343 344 345 346 347 348 349
}


/* Utility functions */

#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)

/* Converts from HSV to RGB */
static void
350
hsv_to_rgb (gdouble *h,
351 352
            gdouble *s,
            gdouble *v)
353
{
354 355
  gdouble hue, saturation, value;
  gdouble f, p, q, t;
356

357 358 359 360 361 362 363 364 365 366 367
  if (*s == 0.0)
    {
      *h = *v;
      *s = *v;
      *v = *v; /* heh */
    }
  else
    {
      hue = *h * 6.0;
      saturation = *s;
      value = *v;
368

369
      if (hue == 6.0)
370 371
        hue = 0.0;

372 373 374 375
      f = hue - (int) hue;
      p = value * (1.0 - saturation);
      q = value * (1.0 - saturation * f);
      t = value * (1.0 - saturation * (1.0 - f));
376

377
      switch ((int) hue)
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        {
        case 0:
          *h = value;
          *s = t;
          *v = p;
          break;

        case 1:
          *h = q;
          *s = value;
          *v = p;
          break;

        case 2:
          *h = p;
          *s = value;
          *v = t;
          break;

        case 3:
          *h = p;
          *s = q;
          *v = value;
          break;

        case 4:
          *h = t;
          *s = p;
          *v = value;
          break;

        case 5:
          *h = value;
          *s = p;
          *v = q;
          break;

        default:
          g_assert_not_reached ();
        }
418
    }
419 420 421 422
}

/* Converts from RGB to HSV */
static void
423
rgb_to_hsv (gdouble *r,
424 425
            gdouble *g,
            gdouble *b)
426
{
427 428 429 430
  gdouble red, green, blue;
  gdouble h, s, v;
  gdouble min, max;
  gdouble delta;
431

432 433 434
  red = *r;
  green = *g;
  blue = *b;
435

436
  h = 0.0;
437

438 439 440
  if (red > green)
    {
      if (red > blue)
441
        max = red;
442
      else
443 444
        max = blue;

445
      if (green < blue)
446
        min = green;
447
      else
448
        min = blue;
449 450 451 452
    }
  else
    {
      if (green > blue)
453
        max = green;
454
      else
455 456
        max = blue;

457
      if (red < blue)
458
        min = red;
459
      else
460
        min = blue;
461
    }
462

463
  v = max;
464

465 466 467 468
  if (max != 0.0)
    s = (max - min) / max;
  else
    s = 0.0;
469

470 471 472 473 474
  if (s == 0.0)
    h = 0.0;
  else
    {
      delta = max - min;
475

476
      if (red == max)
477
        h = (green - blue) / delta;
478
      else if (green == max)
479
        h = 2 + (blue - red) / delta;
480
      else if (blue == max)
481 482
        h = 4 + (red - green) / delta;

483
      h /= 6.0;
484

485
      if (h < 0.0)
486
        h += 1.0;
487
      else if (h > 1.0)
488
        h -= 1.0;
489
    }
490

491 492 493
  *r = h;
  *g = s;
  *b = v;
494 495 496 497
}

/* Computes the vertices of the saturation/value triangle */
static void
498
compute_triangle (GtkHSV *hsv,
499 500 501 502 503 504
                  gint   *hx,
                  gint   *hy,
                  gint   *sx,
                  gint   *sy,
                  gint   *vx,
                  gint   *vy)
505
{
506
  GtkHSVPrivate *priv = hsv->priv;
507
  GtkWidget *widget = GTK_WIDGET (hsv);
508 509
  gdouble center_x;
  gdouble center_y;
510 511
  gdouble inner, outer;
  gdouble angle;
512

513 514
  center_x = gtk_widget_get_allocated_width (widget) / 2.0;
  center_y = gtk_widget_get_allocated_height (widget) / 2.0;
515 516
  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
517
  angle = priv->h * 2.0 * G_PI;
518 519 520 521 522 523 524

  *hx = floor (center_x + cos (angle) * inner + 0.5);
  *hy = floor (center_y - sin (angle) * inner + 0.5);
  *sx = floor (center_x + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
  *sy = floor (center_y - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
  *vx = floor (center_x + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
  *vy = floor (center_y - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
525 526 527 528
}

/* Computes whether a point is inside the hue ring */
static gboolean
529
is_in_ring (GtkHSV *hsv,
530 531
            gdouble x,
            gdouble y)
532
{
533
  GtkHSVPrivate *priv = hsv->priv;
534
  GtkWidget *widget = GTK_WIDGET (hsv);
535
  gdouble dx, dy, dist;
536 537 538 539
  gdouble center_x;
  gdouble center_y;
  gdouble inner, outer;

540 541
  center_x = gtk_widget_get_allocated_width (widget) / 2.0;
  center_y = gtk_widget_get_allocated_height (widget) / 2.0;
542 543
  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
544 545 546

  dx = x - center_x;
  dy = center_y - y;
547
  dist = dx * dx + dy * dy;
548

549
  return (dist >= inner * inner && dist <= outer * outer);
550 551 552 553
}

/* Computes a saturation/value pair based on the mouse coordinates */
static void
554
compute_sv (GtkHSV  *hsv,
555 556 557 558
            gdouble  x,
            gdouble  y,
            gdouble *s,
            gdouble *v)
559
{
560
  GtkWidget *widget = GTK_WIDGET (hsv);
561 562
  int ihx, ihy, isx, isy, ivx, ivy;
  double hx, hy, sx, sy, vx, vy;
563 564 565
  double center_x;
  double center_y;

566
  compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
567 568
  center_x = gtk_widget_get_allocated_width (widget) / 2.0;
  center_y = gtk_widget_get_allocated_height (widget) / 2.0;
569 570 571 572 573 574 575 576
  hx = ihx - center_x;
  hy = center_y - ihy;
  sx = isx - center_x;
  sy = center_y - isy;
  vx = ivx - center_x;
  vy = center_y - ivy;
  x -= center_x;
  y = center_y - y;
577 578 579 580 581
  
  if (vx * (x - sx) + vy * (y - sy) < 0.0)
    {
      *s = 1.0;
      *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
582
            / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
583 584
      
      if (*v < 0.0)
585
        *v = 0.0;
586
      else if (*v > 1.0)
587
        *v = 1.0;
588 589 590 591 592
    }
  else if (hx * (x - sx) + hy * (y - sy) < 0.0)
    {
      *s = 0.0;
      *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
593
            / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
594 595
      
      if (*v < 0.0)
596
        *v = 0.0;
597
      else if (*v > 1.0)
598
        *v = 1.0;
599 600 601 602 603
    }
  else if (sx * (x - hx) + sy * (y - hy) < 0.0)
    {
      *v = 1.0;
      *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
604
            ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
605 606
      
      if (*s < 0.0)
607
        *s = 0.0;
608
      else if (*s > 1.0)
609
        *s = 1.0;
610 611 612 613
    }
  else
    {
      *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
614
            / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
615 616
      
      if (*v<= 0.0)
617 618 619 620
        {
          *v = 0.0;
          *s = 0.0;
        }
621
      else
622 623 624 625 626 627 628 629 630 631 632 633 634 635
        {
          if (*v > 1.0)
            *v = 1.0;

          if (fabs (hy - vy) < fabs (hx - vx))
            *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
          else
            *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));

          if (*s < 0.0)
            *s = 0.0;
          else if (*s > 1.0)
            *s = 1.0;
        }
636
    }
637 638 639 640
}

/* Computes whether a point is inside the saturation/value triangle */
static gboolean
641
is_in_triangle (GtkHSV *hsv,
642 643
                gdouble x,
                gdouble y)
644
{
645 646 647 648 649 650 651 652 653 654 655
  int hx, hy, sx, sy, vx, vy;
  double det, s, v;
  
  compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
  
  det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
  
  s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
  v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
  
  return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
656 657 658 659
}

/* Computes a value based on the mouse coordinates */
static double
660
compute_v (GtkHSV *hsv,
661 662
           gdouble x,
           gdouble y)
663
{
664
  GtkWidget *widget = GTK_WIDGET (hsv);
665 666
  double center_x;
  double center_y;
667 668
  double dx, dy;
  double angle;
669

670 671
  center_x = gtk_widget_get_allocated_width (widget) / 2.0;
  center_y = gtk_widget_get_allocated_height (widget) / 2.0;
672 673 674
  dx = x - center_x;
  dy = center_y - y;

675 676
  angle = atan2 (dy, dx);
  if (angle < 0.0)
677
    angle += 2.0 * G_PI;
678

679
  return angle / (2.0 * G_PI);
680 681 682 683 684
}

/* Event handlers */

static void
685 686 687
set_cross_grab (GtkHSV    *hsv,
                GdkDevice *device,
                guint32    time)
688
{
689
  GtkHSVPrivate *priv = hsv->priv;
690
  GdkCursor *cursor;
691

692
  cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
693
                                       GDK_CROSSHAIR);
694 695 696 697 698 699 700 701 702
  gdk_device_grab (device,
                   priv->window,
                   GDK_OWNERSHIP_NONE,
                   FALSE,
                   GDK_POINTER_MOTION_MASK
                    | GDK_POINTER_MOTION_HINT_MASK
                    | GDK_BUTTON_RELEASE_MASK,
                   cursor,
                   time);
703
  g_object_unref (cursor);
704 705
}

706
static gboolean
707
gtk_hsv_grab_broken (GtkWidget          *widget,
708
                     GdkEventGrabBroken *event)
709 710
{
  GtkHSV *hsv = GTK_HSV (widget);
711
  GtkHSVPrivate *priv = hsv->priv;
712

713
  priv->mode = DRAG_NONE;
714

715 716 717
  return TRUE;
}

718
static gint
719
gtk_hsv_button_press (GtkWidget      *widget,
720
                      GdkEventButton *event)
721
{
722
  GtkHSV *hsv = GTK_HSV (widget);
723
  GtkHSVPrivate *priv = hsv->priv;
724
  double x, y;
725

726 727
  if (priv->mode != DRAG_NONE || event->button != 1)
    return FALSE;
728

729 730
  x = event->x;
  y = event->y;
731

732 733 734
  if (is_in_ring (hsv, x, y))
    {
      priv->mode = DRAG_H;
Benjamin Otte's avatar
Benjamin Otte committed
735
      set_cross_grab (hsv, gdk_event_get_device ((GdkEvent *) event), event->time);
736

737
      gtk_hsv_set_color (hsv,
738 739 740
                         compute_v (hsv, x, y),
                         priv->s,
                         priv->v);
741 742 743

      gtk_widget_grab_focus (widget);
      priv->focus_on_ring = TRUE;
744

745 746
      return TRUE;
    }
747

748 749 750
  if (is_in_triangle (hsv, x, y))
    {
      gdouble s, v;
751

752
      priv->mode = DRAG_SV;
Benjamin Otte's avatar
Benjamin Otte committed
753
      set_cross_grab (hsv, gdk_event_get_device ((GdkEvent *) event), event->time);
754

755 756
      compute_sv (hsv, x, y, &s, &v);
      gtk_hsv_set_color (hsv, priv->h, s, v);
757 758 759

      gtk_widget_grab_focus (widget);
      priv->focus_on_ring = FALSE;
760

761 762
      return TRUE;
    }
763

764
  return FALSE;
765 766 767
}

static gint
768
gtk_hsv_button_release (GtkWidget      *widget,
769
                        GdkEventButton *event)
770
{
771
  GtkHSV *hsv = GTK_HSV (widget);
772
  GtkHSVPrivate *priv = hsv->priv;
773 774
  DragMode mode;
  gdouble x, y;
775

776 777
  if (priv->mode == DRAG_NONE || event->button != 1)
    return FALSE;
778

779 780 781 782 783
  /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
   * can see that this is the final color state.
   */
  mode = priv->mode;
  priv->mode = DRAG_NONE;
784

785 786
  x = event->x;
  y = event->y;
787

788
  if (mode == DRAG_H)
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    {
      gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
    }
  else if (mode == DRAG_SV)
    {
      gdouble s, v;

      compute_sv (hsv, x, y, &s, &v);
      gtk_hsv_set_color (hsv, priv->h, s, v);
    }
  else
    {
      g_assert_not_reached ();
    }

Benjamin Otte's avatar
Benjamin Otte committed
804
  gdk_device_ungrab (gdk_event_get_device ((GdkEvent *) event), event->time);
805

806
  return TRUE;
807 808 809
}

static gint
810
gtk_hsv_motion (GtkWidget      *widget,
811
                GdkEventMotion *event)
812
{
813
  GtkHSV *hsv = GTK_HSV (widget);
814
  GtkHSVPrivate *priv = hsv->priv;
815
  gdouble x, y;
816

817 818
  if (priv->mode == DRAG_NONE)
    return FALSE;
819

820 821 822 823
  gdk_event_request_motions (event);
  x = event->x;
  y = event->y;

824 825 826 827 828 829 830
  if (priv->mode == DRAG_H)
    {
      gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
      return TRUE;
    }
  else if (priv->mode == DRAG_SV)
    {
831 832
      gdouble s, v;

833 834 835 836
      compute_sv (hsv, x, y, &s, &v);
      gtk_hsv_set_color (hsv, priv->h, s, v);
      return TRUE;
    }
837

838
  g_assert_not_reached ();
839

840
  return FALSE;
841 842 843 844 845 846 847
}


/* Redrawing */

/* Paints the hue ring */
static void
848 849
paint_ring (GtkHSV  *hsv,
            cairo_t *cr)
850
{
851
  GtkHSVPrivate *priv = hsv->priv;
852
  GtkWidget *widget = GTK_WIDGET (hsv);
853
  int xx, yy, width, height;
854
  gdouble dx, dy, dist;
855 856
  gdouble center_x;
  gdouble center_y;
857
  gdouble inner, outer;
858
  guint32 *buf, *p;
859 860 861
  gdouble angle;
  gdouble hue;
  gdouble r, g, b;
862 863
  cairo_surface_t *source;
  cairo_t *source_cr;
864
  gint stride;
865

866 867
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
868

869 870
  center_x = width / 2.0;
  center_y = height / 2.0;
871

872 873
  outer = priv->size / 2.0;
  inner = outer - priv->ring_width;
874

875
  /* Create an image initialized with the ring colors */
876

877 878
  stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
  buf = g_new (guint32, height * stride / 4);
879

880 881
  for (yy = 0; yy < height; yy++)
    {
882
      p = buf + yy * width;
883

884
      dy = -(yy - center_y);
885

886
      for (xx = 0; xx < width; xx++)
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
        {
          dx = xx - center_x;

          dist = dx * dx + dy * dy;
          if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
            {
              *p++ = 0;
              continue;
            }

          angle = atan2 (dy, dx);
          if (angle < 0.0)
            angle += 2.0 * G_PI;

          hue = angle / (2.0 * G_PI);

          r = hue;
          g = 1.0;
          b = 1.0;
          hsv_to_rgb (&r, &g, &b);

          *p++ = (((int)floor (r * 255 + 0.5) << 16) |
                  ((int)floor (g * 255 + 0.5) << 8) |
                  (int)floor (b * 255 + 0.5));
        }
912
    }
913

914
  source = cairo_image_surface_create_for_data ((unsigned char *)buf,
915 916
                                                CAIRO_FORMAT_RGB24,
                                                width, height, stride);
917 918 919 920

  /* Now draw the value marker onto the source image, so that it
   * will get properly clipped at the edges of the ring
   */
921
  source_cr = cairo_create (source);
922 923 924 925 926 927 928
  
  r = priv->h;
  g = 1.0;
  b = 1.0;
  hsv_to_rgb (&r, &g, &b);
  
  if (INTENSITY (r, g, b) > 0.5)
929
    cairo_set_source_rgb (source_cr, 0., 0., 0.);
930
  else
931
    cairo_set_source_rgb (source_cr, 1., 1., 1.);
932

933
  cairo_move_to (source_cr, center_x, center_y);
934
  cairo_line_to (source_cr,
935 936
                 center_x + cos (priv->h * 2.0 * G_PI) * priv->size / 2,
                 center_y - sin (priv->h * 2.0 * G_PI) * priv->size / 2);
937 938 939 940 941 942 943
  cairo_stroke (source_cr);
  cairo_destroy (source_cr);

  /* Draw the ring using the source image */

  cairo_save (cr);
    
944
  cairo_set_source_surface (cr, source, 0, 0);
945 946 947 948 949
  cairo_surface_destroy (source);

  cairo_set_line_width (cr, priv->ring_width);
  cairo_new_path (cr);
  cairo_arc (cr,
950 951 952
             center_x, center_y,
             priv->size / 2. - priv->ring_width / 2.,
             0, 2 * G_PI);
953
  cairo_stroke (cr);
954
  
955
  cairo_restore (cr);
956 957
  
  g_free (buf);
958 959 960 961
}

/* Converts an HSV triplet to an integer RGB triplet */
static void
962
get_color (gdouble h,
963 964 965 966 967
           gdouble s,
           gdouble v,
           gint   *r,
           gint   *g,
           gint   *b)
968
{
969 970 971 972 973
  hsv_to_rgb (&h, &s, &v);
  
  *r = floor (h * 255 + 0.5);
  *g = floor (s * 255 + 0.5);
  *b = floor (v * 255 + 0.5);
974 975 976 977
}

#define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))

978 979 980
#define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0)                                       \
                               ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1)))     \
                               : (a))
981

982 983 984 985 986
/* Number of pixels we extend out from the edges when creating
 * color source to avoid artifacts
 */
#define PAD 3

987 988
/* Paints the HSV triangle */
static void
989 990 991
paint_triangle (GtkHSV   *hsv,
                cairo_t  *cr,
                gboolean  draw_focus)
992
{
993
  GtkHSVPrivate *priv = hsv->priv;
994
  GtkWidget *widget = GTK_WIDGET (hsv);
995 996 997 998 999
  gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
  gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
  gint x2, y2, r2, g2, b2; /* Second vertex */
  gint x3, y3, r3, g3, b3; /* Third vertex */
  gint t;
1000
  guint32 *buf, *p, c;
1001 1002
  gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
  gint xx, yy;
1003 1004 1005
  gint x_interp, y_interp;
  gint x_start, x_end;
  cairo_surface_t *source;
1006
  gdouble r, g, b;
1007
  gint stride;
1008
  int width, height;
1009 1010 1011
  GtkStyleContext *context;
  GtkStateFlags state;

1012
  priv = hsv->priv;
1013 1014
  width = gtk_widget_get_allocated_width (widget); 
  height = gtk_widget_get_allocated_height (widget); 
1015 1016 1017 1018 1019 1020 1021
  /* Compute triangle's vertices */
  
  compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
  
  x1 = hx;
  y1 = hy;
  get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1022

1023 1024 1025
  x2 = sx;
  y2 = sy;
  get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1026

1027 1028 1029
  x3 = vx;
  y3 = vy;
  get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1030

1031 1032 1033 1034 1035 1036 1037 1038
  if (y2 > y3)
    {
      SWAP (x2, x3, t);
      SWAP (y2, y3, t);
      SWAP (r2, r3, t);
      SWAP (g2, g3, t);
      SWAP (b2, b3, t);
    }
1039

1040 1041 1042 1043 1044 1045 1046 1047
  if (y1 > y3)
    {
      SWAP (x1, x3, t);
      SWAP (y1, y3, t);
      SWAP (r1, r3, t);
      SWAP (g1, g3, t);
      SWAP (b1, b3, t);
    }
1048

1049 1050 1051 1052 1053 1054 1055 1056
  if (y1 > y2)
    {
      SWAP (x1, x2, t);
      SWAP (y1, y2, t);
      SWAP (r1, r2, t);
      SWAP (g1, g2, t);
      SWAP (b1, b2, t);
    }
1057

1058
  /* Shade the triangle */
1059 1060 1061

  stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
  buf = g_new (guint32, height * stride / 4);
1062

1063 1064
  for (yy = 0; yy < height; yy++)
    {
1065
      p = buf + yy * width;
1066

1067
      if (yy >= y1 - PAD && yy < y3 + PAD) {
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
        y_interp = CLAMP (yy, y1, y3);

        if (y_interp < y2)
          {
            xl = LERP (x1, x2, y1, y2, y_interp);

            rl = LERP (r1, r2, y1, y2, y_interp);
            gl = LERP (g1, g2, y1, y2, y_interp);
            bl = LERP (b1, b2, y1, y2, y_interp);
          }
        else
          {
            xl = LERP (x2, x3, y2, y3, y_interp);

            rl = LERP (r2, r3, y2, y3, y_interp);
            gl = LERP (g2, g3, y2, y3, y_interp);
            bl = LERP (b2, b3, y2, y3, y_interp);
          }

        xr = LERP (x1, x3, y1, y3, y_interp);

        rr = LERP (r1, r3, y1, y3, y_interp);
        gr = LERP (g1, g3, y1, y3, y_interp);
        br = LERP (b1, b3, y1, y3, y_interp);

        if (xl > xr)
          {
            SWAP (xl, xr, t);
            SWAP (rl, rr, t);
            SWAP (gl, gr, t);
            SWAP (bl, br, t);
          }

        x_start = MAX (xl - PAD, 0);
        x_end = MIN (xr + PAD, width);
        x_start = MIN (x_start, x_end);

        c = (rl << 16) | (gl << 8) | bl;

        for (xx = 0; xx < x_start; xx++)
          *p++ = c;

        for (; xx < x_end; xx++)
          {
            x_interp = CLAMP (xx, xl, xr);

            *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
                    (LERP (gl, gr, xl, xr, x_interp) << 8) |
                    LERP (bl, br, xl, xr, x_interp));
          }

        c = (rr << 16) | (gr << 8) | br;

        for (; xx < width; xx++)
          *p++ = c;
1123 1124
      }
    }
1125

1126
  source = cairo_image_surface_create_for_data ((unsigned char *)buf,
1127 1128
                                                CAIRO_FORMAT_RGB24,
                                                width, height, stride);
1129
  
1130 1131
  /* Draw a triangle with the image as a source */

1132
  cairo_set_source_surface (cr, source, 0, 0);
1133
  cairo_surface_destroy (source);
1134
  
1135 1136 1137 1138 1139
  cairo_move_to (cr, x1, y1);
  cairo_line_to (cr, x2, y2);
  cairo_line_to (cr, x3, y3);
  cairo_close_path (cr);
  cairo_fill (cr);
1140 1141 1142 1143 1144
  
  g_free (buf);
  
  /* Draw value marker */
  
1145 1146
  xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
  yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1147 1148 1149 1150 1151
  
  r = priv->h;
  g = priv->s;
  b = priv->v;
  hsv_to_rgb (&r, &g, &b);
1152

1153 1154 1155 1156 1157 1158
  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);
  state = gtk_widget_get_state_flags (widget);
  gtk_style_context_set_state (context, state);

1159
  if (INTENSITY (r, g, b) > 0.5)
1160
    {
1161
      gtk_style_context_add_class (context, "light-area-focus");
1162
      cairo_set_source_rgb (cr, 0., 0., 0.);
1163
    }
1164
  else
1165
    {
1166
      gtk_style_context_add_class (context, "dark-area-focus");
1167
      cairo_set_source_rgb (cr, 1., 1., 1.);
1168 1169
    }

1170
#define RADIUS 4
1171 1172
#define FOCUS_RADIUS 6

1173
  cairo_new_path (cr);
1174
  cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1175 1176
  cairo_stroke (cr);
  
1177 1178
  /* Draw focus outline */

1179
  if (draw_focus && !priv->focus_on_ring)
1180 1181 1182 1183 1184
    {
      gint focus_width;
      gint focus_pad;

      gtk_widget_style_get (widget,
1185 1186 1187
                            "focus-line-width", &focus_width,
                            "focus-padding", &focus_pad,
                            NULL);
1188

1189 1190 1191 1192 1193
      gtk_render_focus (context, cr,
                        xx - FOCUS_RADIUS - focus_width - focus_pad,
                        yy - FOCUS_RADIUS - focus_width - focus_pad,
                        2 * (FOCUS_RADIUS + focus_width + focus_pad),
                        2 * (FOCUS_RADIUS + focus_width + focus_pad));
1194
    }
1195 1196

  gtk_style_context_restore (context);
1197 1198 1199
}

/* Paints the contents of the HSV color selector */
1200
static gboolean
1201 1202
gtk_hsv_draw (GtkWidget *widget,
              cairo_t   *cr)
1203
{
1204
  GtkHSV *hsv = GTK_HSV (widget);
1205
  GtkHSVPrivate *priv = hsv->priv;
1206 1207 1208
  gboolean draw_focus;

  draw_focus = gtk_widget_has_visible_focus (widget);
1209

1210
  paint_ring (hsv, cr);
1211 1212
  paint_triangle (hsv, cr, draw_focus);

1213

1214
  if (draw_focus && priv->focus_on_ring)
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    {
      GtkStyleContext *context;
      GtkStateFlags state;

      context = gtk_widget_get_style_context (widget);
      state = gtk_widget_get_state_flags (widget);

      gtk_style_context_save (context);
      gtk_style_context_set_state (context, state);

      gtk_render_focus (context, cr, 0, 0,
                        gtk_widget_get_allocated_width (widget),
                        gtk_widget_get_allocated_height (widget));

      gtk_style_context_restore (context);
    }
1231

1232
  return FALSE;
1233 1234
}

1235
static gboolean
1236
gtk_hsv_focus (GtkWidget       *widget,
1237 1238
               GtkDirectionType dir)
{
1239
  GtkHSV *hsv = GTK_HSV (widget);
1240
  GtkHSVPrivate *priv = hsv->priv;
1241

1242
  if (!gtk_widget_has_focus (widget))
1243
    {
1244 1245 1246 1247 1248
      if (dir == GTK_DIR_TAB_BACKWARD)
        priv->focus_on_ring = FALSE;
      else
        priv->focus_on_ring = TRUE;