gtkcolorplane.c 12.6 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 "gtkaccessible.h"
23 24
#include "gtkadjustment.h"
#include "gtkcolorutils.h"
25
#include "gtkpressandholdprivate.h"
26
#include "gtkintl.h"
27 28 29

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

34 35
  cairo_surface_t *surface;
  gboolean in_drag;
36 37

  GtkPressAndHold *press_and_hold;
38 39 40 41
};

G_DEFINE_TYPE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA)

Matthias Clasen's avatar
Matthias Clasen committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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);
}

60
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
61 62
plane_draw (GtkWidget *widget,
            cairo_t   *cr)
63 64 65 66 67 68 69 70
{
  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
71
  sv_to_xy (plane, &x, &y);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  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
102
create_surface (GtkColorPlane *plane)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
  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
135
  data = g_malloc (height * stride);
136

Matthias Clasen's avatar
Matthias Clasen committed
137
  h = gtk_adjustment_get_value (plane->priv->h_adj);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  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);
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
168 169
plane_configure (GtkWidget         *widget,
                 GdkEventConfigure *event)
170
{
Matthias Clasen's avatar
Matthias Clasen committed
171
  create_surface (GTK_COLOR_PLANE (widget));
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  return TRUE;
}

static void
set_cross_grab (GtkWidget *widget,
                GdkDevice *device,
                guint32    time)
{
  GdkCursor *cursor;

  cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (widget)),
                                       GDK_CROSSHAIR);
  gdk_device_grab (device,
                   gtk_widget_get_window (widget),
                   GDK_OWNERSHIP_NONE,
                   FALSE,
                   GDK_POINTER_MOTION_MASK
                    | GDK_POINTER_MOTION_HINT_MASK
                    | GDK_BUTTON_RELEASE_MASK,
                   cursor,
                   time);
  g_object_unref (cursor);
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
197 198
plane_grab_broken (GtkWidget          *widget,
                   GdkEventGrabBroken *event)
199
{
Matthias Clasen's avatar
Matthias Clasen committed
200 201 202
  GTK_COLOR_PLANE (widget)->priv->in_drag = FALSE;
  return TRUE;
}
203

Matthias Clasen's avatar
Matthias Clasen committed
204 205 206 207 208 209
static void
h_changed (GtkColorPlane *plane)
{
  create_surface (plane);
  gtk_widget_queue_draw (GTK_WIDGET (plane));
}
210

Matthias Clasen's avatar
Matthias Clasen committed
211 212 213 214
static void
sv_changed (GtkColorPlane *plane)
{
  gtk_widget_queue_draw (GTK_WIDGET (plane));
215 216 217
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
218 219 220
update_color (GtkColorPlane *plane,
              gint           x,
              gint           y)
221 222
{
  GtkWidget *widget = GTK_WIDGET (plane);
Matthias Clasen's avatar
Matthias Clasen committed
223
  gdouble s, v;
224

Matthias Clasen's avatar
Matthias Clasen committed
225 226 227 228
  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
229

230 231 232 233
  gtk_widget_queue_draw (widget);
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
234 235
plane_button_press (GtkWidget      *widget,
                    GdkEventButton *event)
236 237 238
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);

Matthias Clasen's avatar
Matthias Clasen committed
239 240 241 242 243 244 245 246 247
  if (event->button == GDK_BUTTON_SECONDARY)
    {
      gboolean handled;

      g_signal_emit_by_name (widget, "popup-menu", &handled);

      return TRUE;
    }

248 249 250 251 252
  if (plane->priv->in_drag || event->button != GDK_BUTTON_PRIMARY)
    return FALSE;

  plane->priv->in_drag = TRUE;
  set_cross_grab (widget, gdk_event_get_device ((GdkEvent*)event), event->time);
Matthias Clasen's avatar
Matthias Clasen committed
253
  update_color (plane, event->x, event->y);
254 255 256 257 258 259
  gtk_widget_grab_focus (widget);

  return TRUE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
260 261
plane_button_release (GtkWidget      *widget,
                      GdkEventButton *event)
262 263 264 265 266 267 268 269
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);

  if (!plane->priv->in_drag || event->button != GDK_BUTTON_PRIMARY)
    return FALSE;

  plane->priv->in_drag = FALSE;

Matthias Clasen's avatar
Matthias Clasen committed
270
  update_color (plane, event->x, event->y);
271 272 273 274 275 276
  gdk_device_ungrab (gdk_event_get_device ((GdkEvent *) event), event->time);

  return TRUE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
277 278
plane_motion_notify (GtkWidget      *widget,
                     GdkEventMotion *event)
279 280 281 282 283 284 285
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);

  if (!plane->priv->in_drag)
    return FALSE;

  gdk_event_request_motions (event);
Matthias Clasen's avatar
Matthias Clasen committed
286
  update_color (plane, event->x, event->y);
287 288 289 290

  return TRUE;
}

291 292 293 294 295 296 297 298 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 324 325 326 327
static void
hold_action (GtkPressAndHold *pah,
             gint             x,
             gint             y,
             GtkColorPlane   *plane)
{
  gboolean handled;

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

static gboolean
plane_touch (GtkWidget     *widget,
             GdkEventTouch *event)
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);

  if (!plane->priv->press_and_hold)
    {
      gint drag_threshold;

      g_object_get (gtk_widget_get_settings (widget),
                    "gtk-dnd-drag-threshold", &drag_threshold,
                    NULL);

      plane->priv->press_and_hold = gtk_press_and_hold_new ();

      g_object_set (plane->priv->press_and_hold,
                    "drag-threshold", drag_threshold,
                    "hold-time", 1000,
                    NULL);

      g_signal_connect (plane->priv->press_and_hold, "hold",
                        G_CALLBACK (hold_action), plane);
    }

  gtk_press_and_hold_process_event (plane->priv->press_and_hold, (GdkEvent *)event);
328
  update_color (plane, event->x, event->y);
329 330 331 332

  return TRUE;
}

333 334 335 336 337
static void
sv_move (GtkColorPlane *plane,
         gdouble        ds,
         gdouble        dv)
{
Matthias Clasen's avatar
Matthias Clasen committed
338 339 340 341 342 343
  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)
344
    {
Matthias Clasen's avatar
Matthias Clasen committed
345 346
      if (s < 1)
        s = 1;
347 348 349
      else
        goto error;
    }
Matthias Clasen's avatar
Matthias Clasen committed
350
  else if (s + ds < 0)
351
    {
Matthias Clasen's avatar
Matthias Clasen committed
352 353
      if (s > 0)
        s = 0;
354 355 356 357 358
      else
        goto error;
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
359
      s += ds;
360 361
    }

Matthias Clasen's avatar
Matthias Clasen committed
362
  if (v + dv > 1)
363
    {
Matthias Clasen's avatar
Matthias Clasen committed
364 365
      if (v < 1)
        v = 1;
366 367 368
      else
        goto error;
    }
Matthias Clasen's avatar
Matthias Clasen committed
369
  else if (v + dv < 0)
370
    {
Matthias Clasen's avatar
Matthias Clasen committed
371 372
      if (v > 0)
        v = 0;
373 374 375 376 377
      else
        goto error;
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
378
      v += dv;
379 380
    }

Matthias Clasen's avatar
Matthias Clasen committed
381 382
  gtk_adjustment_set_value (plane->priv->s_adj, s);
  gtk_adjustment_set_value (plane->priv->v_adj, v);
383 384 385 386 387 388 389
  return;

error:
  gtk_widget_error_bell (GTK_WIDGET (plane));
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
390 391
plane_key_press (GtkWidget   *widget,
                 GdkEventKey *event)
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
{
  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
414
    return GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->key_press_event (widget, event);
415 416 417 418 419 420 421

  return TRUE;
}

static void
gtk_color_plane_init (GtkColorPlane *plane)
{
422 423
  AtkObject *atk_obj;

424 425 426
  plane->priv = G_TYPE_INSTANCE_GET_PRIVATE (plane,
                                             GTK_TYPE_COLOR_PLANE,
                                             GtkColorPlanePrivate);
Matthias Clasen's avatar
Matthias Clasen committed
427

428 429
  gtk_widget_set_can_focus (GTK_WIDGET (plane), TRUE);
  gtk_widget_set_events (GTK_WIDGET (plane), GDK_KEY_PRESS_MASK
430
                                             | GDK_TOUCH_MASK
431 432 433
                                             | GDK_BUTTON_PRESS_MASK
                                             | GDK_BUTTON_RELEASE_MASK
                                             | GDK_POINTER_MOTION_MASK);
434 435 436 437 438 439 440

  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);
    }
441 442 443
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
444
plane_finalize (GObject *object)
445 446 447
{
  GtkColorPlane *plane = GTK_COLOR_PLANE (object);

Matthias Clasen's avatar
Matthias Clasen committed
448 449 450
  if (plane->priv->surface)
    cairo_surface_destroy (plane->priv->surface);

Matthias Clasen's avatar
Matthias Clasen committed
451 452 453
  g_clear_object (&plane->priv->h_adj);
  g_clear_object (&plane->priv->s_adj);
  g_clear_object (&plane->priv->v_adj);
454

455 456
  g_clear_object (&plane->priv->press_and_hold);

457 458 459 460 461 462 463 464 465
  G_OBJECT_CLASS (gtk_color_plane_parent_class)->finalize (object);
}

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
466
  object_class->finalize = plane_finalize;
467

Matthias Clasen's avatar
Matthias Clasen committed
468 469 470 471 472 473 474
  widget_class->draw = plane_draw;
  widget_class->configure_event = plane_configure;
  widget_class->button_press_event = plane_button_press;
  widget_class->button_release_event = plane_button_release;
  widget_class->motion_notify_event = plane_motion_notify;
  widget_class->grab_broken_event = plane_grab_broken;
  widget_class->key_press_event = plane_key_press;
475
  widget_class->touch_event = plane_touch;
476 477 478 479

  g_type_class_add_private (class, sizeof (GtkColorPlanePrivate));
}

Matthias Clasen's avatar
Matthias Clasen committed
480 481 482 483
GtkWidget *
gtk_color_plane_new (GtkAdjustment *h_adj,
                     GtkAdjustment *s_adj,
                     GtkAdjustment *v_adj)
484
{
Matthias Clasen's avatar
Matthias Clasen committed
485
  GtkColorPlane *plane;
486

Matthias Clasen's avatar
Matthias Clasen committed
487
  plane = (GtkColorPlane *) g_object_new (GTK_TYPE_COLOR_PLANE, NULL);
488

Matthias Clasen's avatar
Matthias Clasen committed
489 490 491
  plane->priv->h_adj = g_object_ref_sink (h_adj);
  plane->priv->s_adj = g_object_ref_sink (s_adj);
  plane->priv->v_adj = g_object_ref_sink (v_adj);
Matthias Clasen's avatar
Matthias Clasen committed
492 493 494
  g_signal_connect_swapped (h_adj, "value-changed", G_CALLBACK (h_changed), plane);
  g_signal_connect_swapped (s_adj, "value-changed", G_CALLBACK (sv_changed), plane);
  g_signal_connect_swapped (v_adj, "value-changed", G_CALLBACK (sv_changed), plane);
495

Matthias Clasen's avatar
Matthias Clasen committed
496
  return (GtkWidget *)plane;
497
}