gtkcolorplane.c 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20 21
#include "gtkcolorplaneprivate.h"

22
#include "gtkgesturedrag.h"
23
#include "gtkgesturelongpress.h"
24
#include "gtkaccessible.h"
25 26
#include "gtkadjustment.h"
#include "gtkcolorutils.h"
27
#include "gtkintl.h"
28 29 30

struct _GtkColorPlanePrivate
{
Matthias Clasen's avatar
Matthias Clasen committed
31 32 33 34
  GtkAdjustment *h_adj;
  GtkAdjustment *s_adj;
  GtkAdjustment *v_adj;

35
  cairo_surface_t *surface;
36

37
  GtkGesture *drag_gesture;
38
  GtkGesture *long_press_gesture;
39 40
};

41 42 43 44 45 46 47
enum {
  PROP_0,
  PROP_H_ADJUSTMENT,
  PROP_S_ADJUSTMENT,
  PROP_V_ADJUSTMENT
};

48
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA)
49

Matthias Clasen's avatar
Matthias Clasen committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void
sv_to_xy (GtkColorPlane *plane,
          gint          *x,
          gint          *y)
{
  gdouble s, v;
  gint width, height;

  width = gtk_widget_get_allocated_width (GTK_WIDGET (plane));
  height = gtk_widget_get_allocated_height (GTK_WIDGET (plane));

  s = gtk_adjustment_get_value (plane->priv->s_adj);
  v = gtk_adjustment_get_value (plane->priv->v_adj);

  *x = CLAMP (width * v, 0, width - 1);
  *y = CLAMP (height * (1 - s), 0, height - 1);
}

68
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
69 70
plane_draw (GtkWidget *widget,
            cairo_t   *cr)
71 72 73 74 75 76 77 78
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
  gint x, y;
  gint width, height;

  cairo_set_source_surface (cr, plane->priv->surface, 0, 0);
  cairo_paint (cr);

Matthias Clasen's avatar
Matthias Clasen committed
79
  sv_to_xy (plane, &x, &y);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);

  cairo_move_to (cr, 0,     y + 0.5);
  cairo_line_to (cr, width, y + 0.5);

  cairo_move_to (cr, x + 0.5, 0);
  cairo_line_to (cr, x + 0.5, height);

  if (gtk_widget_has_visible_focus (widget))
    {
      cairo_set_line_width (cr, 3.0);
      cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.6);
      cairo_stroke_preserve (cr);

      cairo_set_line_width (cr, 1.0);
      cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.8);
      cairo_stroke (cr);
    }
  else
    {
      cairo_set_line_width (cr, 1.0);
      cairo_set_source_rgba (cr, 0.8, 0.8, 0.8, 0.8);
      cairo_stroke (cr);
    }

  return FALSE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
110
create_surface (GtkColorPlane *plane)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
{
  GtkWidget *widget = GTK_WIDGET (plane);
  cairo_t *cr;
  cairo_surface_t *surface;
  gint width, height, stride;
  cairo_surface_t *tmp;
  guint red, green, blue;
  guint32 *data, *p;
  gdouble h, s, v;
  gdouble r, g, b;
  gdouble sf, vf;
  gint x, y;

  if (!gtk_widget_get_realized (widget))
    return;

  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);

  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
                                               CAIRO_CONTENT_COLOR,
                                               width, height);

  if (plane->priv->surface)
    cairo_surface_destroy (plane->priv->surface);
  plane->priv->surface = surface;

  if (width == 1 || height == 1)
    return;

  stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);

Matthias Clasen's avatar
Matthias Clasen committed
143
  data = g_malloc (height * stride);
144

Matthias Clasen's avatar
Matthias Clasen committed
145
  h = gtk_adjustment_get_value (plane->priv->h_adj);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  sf = 1.0 / (height - 1);
  vf = 1.0 / (width - 1);
  for (y = 0; y < height; y++)
    {
      s = CLAMP (1.0 - y * sf, 0.0, 1.0);
      p = data + y * (stride / 4);
      for (x = 0; x < width; x++)
        {
          v = x * vf;
          gtk_hsv_to_rgb (h, s, v, &r, &g, &b);
          red = CLAMP (r * 255, 0, 255);
          green = CLAMP (g * 255, 0, 255);
          blue = CLAMP (b * 255, 0, 255);
          p[x] = (red << 16) | (green << 8) | blue;
        }
    }

  tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
                                             width, height, stride);
  cr = cairo_create (surface);

  cairo_set_source_surface (cr, tmp, 0, 0);
  cairo_paint (cr);

  cairo_destroy (cr);
  cairo_surface_destroy (tmp);
  g_free (data);
}

175 176 177
static void
plane_size_allocate (GtkWidget     *widget,
                     GtkAllocation *allocation)
178
{
179 180
  GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->size_allocate (widget, allocation);

Matthias Clasen's avatar
Matthias Clasen committed
181
  create_surface (GTK_COLOR_PLANE (widget));
182 183
}

184 185 186 187 188 189 190 191
static void
plane_realize (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->realize (widget);

  create_surface (GTK_COLOR_PLANE (widget));
}

192
static void
193 194
set_cross_cursor (GtkWidget *widget,
                  gboolean   enabled)
195
{
196 197 198
  GdkCursor *cursor = NULL;
  GdkWindow *window;
  GdkDevice *device;
199

200 201 202 203 204 205 206
  window = gtk_widget_get_window (widget);
  device = gtk_gesture_get_device (GTK_COLOR_PLANE (widget)->priv->drag_gesture);

  if (!window || !device)
    return;

  if (enabled)
207
    cursor = gdk_cursor_new_from_name (gtk_widget_get_display (GTK_WIDGET (widget)), "crosshair");
208 209 210 211 212

  gdk_window_set_device_cursor (window, device, cursor);

  if (cursor)
    g_object_unref (cursor);
Matthias Clasen's avatar
Matthias Clasen committed
213
}
214

Matthias Clasen's avatar
Matthias Clasen committed
215 216 217 218 219 220
static void
h_changed (GtkColorPlane *plane)
{
  create_surface (plane);
  gtk_widget_queue_draw (GTK_WIDGET (plane));
}
221

Matthias Clasen's avatar
Matthias Clasen committed
222 223 224 225
static void
sv_changed (GtkColorPlane *plane)
{
  gtk_widget_queue_draw (GTK_WIDGET (plane));
226 227 228
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
229 230 231
update_color (GtkColorPlane *plane,
              gint           x,
              gint           y)
232 233
{
  GtkWidget *widget = GTK_WIDGET (plane);
Matthias Clasen's avatar
Matthias Clasen committed
234
  gdouble s, v;
235

Matthias Clasen's avatar
Matthias Clasen committed
236 237 238 239
  s = CLAMP (1 - y * (1.0 / gtk_widget_get_allocated_height (widget)), 0, 1);
  v = CLAMP (x * (1.0 / gtk_widget_get_allocated_width (widget)), 0, 1);
  gtk_adjustment_set_value (plane->priv->s_adj, s);
  gtk_adjustment_set_value (plane->priv->v_adj, v);
Matthias Clasen's avatar
Matthias Clasen committed
240

241 242 243
  gtk_widget_queue_draw (widget);
}

244
static void
245 246 247 248
hold_action (GtkGestureLongPress *gesture,
             gdouble              x,
             gdouble              y,
             GtkColorPlane       *plane)
249 250 251 252 253 254
{
  gboolean handled;

  g_signal_emit_by_name (plane, "popup-menu", &handled);
}

255 256 257 258 259
static void
sv_move (GtkColorPlane *plane,
         gdouble        ds,
         gdouble        dv)
{
Matthias Clasen's avatar
Matthias Clasen committed
260 261 262 263 264 265
  gdouble s, v;

  s = gtk_adjustment_get_value (plane->priv->s_adj);
  v = gtk_adjustment_get_value (plane->priv->v_adj);

  if (s + ds > 1)
266
    {
Matthias Clasen's avatar
Matthias Clasen committed
267 268
      if (s < 1)
        s = 1;
269 270 271
      else
        goto error;
    }
Matthias Clasen's avatar
Matthias Clasen committed
272
  else if (s + ds < 0)
273
    {
Matthias Clasen's avatar
Matthias Clasen committed
274 275
      if (s > 0)
        s = 0;
276 277 278 279 280
      else
        goto error;
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
281
      s += ds;
282 283
    }

Matthias Clasen's avatar
Matthias Clasen committed
284
  if (v + dv > 1)
285
    {
Matthias Clasen's avatar
Matthias Clasen committed
286 287
      if (v < 1)
        v = 1;
288 289 290
      else
        goto error;
    }
Matthias Clasen's avatar
Matthias Clasen committed
291
  else if (v + dv < 0)
292
    {
Matthias Clasen's avatar
Matthias Clasen committed
293 294
      if (v > 0)
        v = 0;
295 296 297 298 299
      else
        goto error;
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
300
      v += dv;
301 302
    }

Matthias Clasen's avatar
Matthias Clasen committed
303 304
  gtk_adjustment_set_value (plane->priv->s_adj, s);
  gtk_adjustment_set_value (plane->priv->v_adj, v);
305 306 307 308 309 310 311
  return;

error:
  gtk_widget_error_bell (GTK_WIDGET (plane));
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
312 313
plane_key_press (GtkWidget   *widget,
                 GdkEventKey *event)
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
  gdouble step;

  if ((event->state & GDK_MOD1_MASK) != 0)
    step = 0.1;
  else
    step = 0.01;

  if (event->keyval == GDK_KEY_Up ||
      event->keyval == GDK_KEY_KP_Up)
    sv_move (plane, step, 0);
  else if (event->keyval == GDK_KEY_Down ||
           event->keyval == GDK_KEY_KP_Down)
    sv_move (plane, -step, 0);
  else if (event->keyval == GDK_KEY_Left ||
           event->keyval == GDK_KEY_KP_Left)
    sv_move (plane, 0, -step);
  else if (event->keyval == GDK_KEY_Right ||
           event->keyval == GDK_KEY_KP_Right)
    sv_move (plane, 0, step);
  else
Matthias Clasen's avatar
Matthias Clasen committed
336
    return GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->key_press_event (widget, event);
337 338 339 340

  return TRUE;
}

341 342 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
static void
plane_drag_gesture_begin (GtkGestureDrag *gesture,
                          gdouble         start_x,
                          gdouble         start_y,
                          GtkColorPlane  *plane)
{
  guint button;

  button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));

  if (button == GDK_BUTTON_SECONDARY)
    {
      gboolean handled;

      g_signal_emit_by_name (plane, "popup-menu", &handled);
    }

  if (button != GDK_BUTTON_PRIMARY)
    {
      gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
      return;
    }

  set_cross_cursor (GTK_WIDGET (plane), TRUE);
  update_color (plane, start_x, start_y);
  gtk_widget_grab_focus (GTK_WIDGET (plane));
  gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
}

static void
plane_drag_gesture_update (GtkGestureDrag *gesture,
                           gdouble         offset_x,
                           gdouble         offset_y,
                           GtkColorPlane  *plane)
{
  gdouble start_x, start_y;

  gtk_gesture_drag_get_start_point (GTK_GESTURE_DRAG (gesture),
                                    &start_x, &start_y);
  update_color (plane, start_x + offset_x, start_y + offset_y);
}

static void
plane_drag_gesture_end (GtkGestureDrag *gesture,
                        gdouble         offset_x,
                        gdouble         offset_y,
                        GtkColorPlane  *plane)
{
  set_cross_cursor (GTK_WIDGET (plane), FALSE);
}

392 393 394
static void
gtk_color_plane_init (GtkColorPlane *plane)
{
395 396
  AtkObject *atk_obj;

397
  plane->priv = gtk_color_plane_get_instance_private (plane);
Matthias Clasen's avatar
Matthias Clasen committed
398

399 400
  gtk_widget_set_can_focus (GTK_WIDGET (plane), TRUE);
  gtk_widget_set_events (GTK_WIDGET (plane), GDK_KEY_PRESS_MASK
401
                                             | GDK_TOUCH_MASK
402 403 404
                                             | GDK_BUTTON_PRESS_MASK
                                             | GDK_BUTTON_RELEASE_MASK
                                             | GDK_POINTER_MOTION_MASK);
405 406 407 408 409 410 411

  atk_obj = gtk_widget_get_accessible (GTK_WIDGET (plane));
  if (GTK_IS_ACCESSIBLE (atk_obj))
    {
      atk_object_set_name (atk_obj, _("Color Plane"));
      atk_object_set_role (atk_obj, ATK_ROLE_COLOR_CHOOSER);
    }
412

413 414 415 416 417 418 419
  plane->priv->drag_gesture = gtk_gesture_drag_new (GTK_WIDGET (plane));
  g_signal_connect (plane->priv->drag_gesture, "drag-begin",
		    G_CALLBACK (plane_drag_gesture_begin), plane);
  g_signal_connect (plane->priv->drag_gesture, "drag-update",
		    G_CALLBACK (plane_drag_gesture_update), plane);
  g_signal_connect (plane->priv->drag_gesture, "drag-end",
		    G_CALLBACK (plane_drag_gesture_end), plane);
420
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (plane->priv->drag_gesture), 0);
421

422 423 424
  plane->priv->long_press_gesture = gtk_gesture_long_press_new (GTK_WIDGET (plane));
  g_signal_connect (plane->priv->long_press_gesture, "pressed",
                    G_CALLBACK (hold_action), plane);
425
  gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (plane->priv->long_press_gesture),
426
                                     TRUE);
427 428 429
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
430
plane_finalize (GObject *object)
431 432 433
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (object);

Matthias Clasen's avatar
Matthias Clasen committed
434 435 436
  if (plane->priv->surface)
    cairo_surface_destroy (plane->priv->surface);

Matthias Clasen's avatar
Matthias Clasen committed
437 438 439
  g_clear_object (&plane->priv->h_adj);
  g_clear_object (&plane->priv->s_adj);
  g_clear_object (&plane->priv->v_adj);
440

441
  g_clear_object (&plane->priv->drag_gesture);
442
  g_clear_object (&plane->priv->long_press_gesture);
443

444 445 446
  G_OBJECT_CLASS (gtk_color_plane_parent_class)->finalize (object);
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
static void
plane_set_property (GObject      *object,
		    guint         prop_id,
		    const GValue *value,
		    GParamSpec   *pspec)
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (object);
  GObject *adjustment;

  /* Construct only properties can only be set once, these are created
   * only in order to be properly buildable from gtkcoloreditor.ui
   */
  switch (prop_id)
    {
    case PROP_H_ADJUSTMENT:
      adjustment = g_value_get_object (value);
      if (adjustment)
	{
465
	  plane->priv->h_adj = GTK_ADJUSTMENT (g_object_ref_sink (adjustment));
466 467 468 469 470 471 472
	  g_signal_connect_swapped (adjustment, "value-changed", G_CALLBACK (h_changed), plane);
	}
      break;
    case PROP_S_ADJUSTMENT:
      adjustment = g_value_get_object (value);
      if (adjustment)
	{
473
	  plane->priv->s_adj = GTK_ADJUSTMENT (g_object_ref_sink (adjustment));
474 475 476 477 478 479 480
	  g_signal_connect_swapped (adjustment, "value-changed", G_CALLBACK (sv_changed), plane);
	}
      break;
    case PROP_V_ADJUSTMENT:
      adjustment = g_value_get_object (value);
      if (adjustment)
	{
481
	  plane->priv->v_adj = GTK_ADJUSTMENT (g_object_ref_sink (adjustment));
482 483 484 485 486 487 488 489 490
	  g_signal_connect_swapped (adjustment, "value-changed", G_CALLBACK (sv_changed), plane);
	}
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

491 492 493 494 495 496
static void
gtk_color_plane_class_init (GtkColorPlaneClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

Matthias Clasen's avatar
Matthias Clasen committed
497
  object_class->finalize = plane_finalize;
498
  object_class->set_property = plane_set_property;
499

Matthias Clasen's avatar
Matthias Clasen committed
500
  widget_class->draw = plane_draw;
501
  widget_class->size_allocate = plane_size_allocate;
502
  widget_class->realize = plane_realize;
Matthias Clasen's avatar
Matthias Clasen committed
503
  widget_class->key_press_event = plane_key_press;
504

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
  g_object_class_install_property (object_class,
                                   PROP_H_ADJUSTMENT,
                                   g_param_spec_object ("h-adjustment",
                                                        "Hue Adjustment",
                                                        "Hue Adjustment",
							GTK_TYPE_ADJUSTMENT,
							G_PARAM_WRITABLE |
							G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class,
                                   PROP_S_ADJUSTMENT,
                                   g_param_spec_object ("s-adjustment",
                                                        "Saturation Adjustment",
                                                        "Saturation Adjustment",
							GTK_TYPE_ADJUSTMENT,
							G_PARAM_WRITABLE |
							G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class,
                                   PROP_V_ADJUSTMENT,
                                   g_param_spec_object ("v-adjustment",
                                                        "Value Adjustment",
                                                        "Value Adjustment",
							GTK_TYPE_ADJUSTMENT,
							G_PARAM_WRITABLE |
							G_PARAM_CONSTRUCT_ONLY));
531 532
}

Matthias Clasen's avatar
Matthias Clasen committed
533 534 535 536
GtkWidget *
gtk_color_plane_new (GtkAdjustment *h_adj,
                     GtkAdjustment *s_adj,
                     GtkAdjustment *v_adj)
537
{
538 539 540 541 542
  return g_object_new (GTK_TYPE_COLOR_PLANE,
                       "h-adjustment", h_adj,
                       "s-adjustment", s_adj,
                       "v-adjustment", v_adj,
                       NULL);
543
}