gtkcolorswatch.c 29.4 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
#include "gtkcolorswatchprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
21

22
#include "gtkcolorchooserprivate.h"
23 24
#include "gtkdnd.h"
#include "gtkicontheme.h"
25 26 27 28
#include "gtkmain.h"
#include "gtkmenu.h"
#include "gtkmenuitem.h"
#include "gtkmenushell.h"
29 30
#include "gtkprivate.h"
#include "gtkintl.h"
31
#include "gtkrenderprivate.h"
32
#include "gtkcssnodeprivate.h"
33
#include "gtkcsscustomgadgetprivate.h"
34
#include "gtkwidgetprivate.h"
35
#include "gtkstylecontextprivate.h"
36
#include "a11y/gtkcolorswatchaccessibleprivate.h"
37 38


39 40
/*
 * GtkColorSwatch has two CSS nodes, the main one named colorswatch
Matthias Clasen's avatar
Matthias Clasen committed
41
 * and a subnode named overlay. The main node gets the .light or .dark
42 43
 * style classes added depending on the brightness of the color that
 * the swatch is showing.
44 45 46
 *
 * The color swatch has the .activatable style class by default. It can
 * be removed for non-activatable swatches.
47 48
 */

49 50 51 52 53 54
struct _GtkColorSwatchPrivate
{
  GdkRGBA color;
  gdouble radius[4];
  gchar *icon;
  guint    has_color        : 1;
Matthias Clasen's avatar
Matthias Clasen committed
55
  guint    use_alpha        : 1;
56
  guint    selectable       : 1;
57
  guint    has_menu         : 1;
58 59

  GdkWindow *event_window;
60

61 62
  GtkGesture *long_press_gesture;
  GtkGesture *multipress_gesture;
63 64
  GtkCssGadget *gadget;
  GtkCssGadget *overlay_gadget;
65 66

  GtkWidget *popover;
67 68 69 70 71
};

enum
{
  PROP_ZERO,
72
  PROP_RGBA,
73 74
  PROP_SELECTABLE,
  PROP_HAS_MENU
75 76 77 78 79 80 81 82 83 84 85
};

enum
{
  ACTIVATE,
  CUSTOMIZE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

86

87
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorSwatch, gtk_color_swatch, GTK_TYPE_WIDGET)
88

89 90 91
static gboolean
swatch_draw (GtkWidget *widget,
             cairo_t   *cr)
92
{
93
  gtk_css_gadget_draw (GTK_COLOR_SWATCH (widget)->priv->gadget, cr);
94

95
  return FALSE;
96 97 98
}

#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
99
#define PIXBUF_SIZE 16
100 101

static gboolean
102 103 104 105 106 107 108
gtk_color_swatch_render (GtkCssGadget *gadget,
                         cairo_t      *cr,
                         int           x,
                         int           y,
                         int           width,
                         int           height,
                         gpointer      data)
109
{
110 111
  GtkWidget *widget;
  GtkColorSwatch *swatch;
112 113
  GtkStyleContext *context;

114 115
  widget = gtk_css_gadget_get_owner (gadget);
  swatch = GTK_COLOR_SWATCH (widget);
116
  context = gtk_widget_get_style_context (widget);
Matthias Clasen's avatar
Matthias Clasen committed
117

118 119
  if (swatch->priv->has_color)
    {
120 121 122
      cairo_pattern_t *pattern;
      cairo_matrix_t matrix;

123
      gtk_render_content_path (context, cr, x, y, width, height);
124

Matthias Clasen's avatar
Matthias Clasen committed
125
      if (swatch->priv->use_alpha)
Matthias Clasen's avatar
Matthias Clasen committed
126
        {
127 128 129 130
          cairo_save (cr);

          cairo_clip_preserve (cr);

Matthias Clasen's avatar
Matthias Clasen committed
131 132
          cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
          cairo_fill_preserve (cr);
133

134
          pattern = _gtk_color_chooser_get_checkered_pattern ();
Matthias Clasen's avatar
Matthias Clasen committed
135 136
          cairo_matrix_init_scale (&matrix, 0.125, 0.125);
          cairo_pattern_set_matrix (pattern, &matrix);
137 138

          cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
Matthias Clasen's avatar
Matthias Clasen committed
139 140
          cairo_mask (cr, pattern);
          cairo_pattern_destroy (pattern);
141

142 143
          cairo_restore (cr);

144
          gdk_cairo_set_source_rgba (cr, &swatch->priv->color);
Matthias Clasen's avatar
Matthias Clasen committed
145 146 147
        }
      else
        {
148 149 150 151
          cairo_set_source_rgb (cr,
                                swatch->priv->color.red,
                                swatch->priv->color.green,
                                swatch->priv->color.blue);
Matthias Clasen's avatar
Matthias Clasen committed
152
        }
153

154
      cairo_fill (cr);
155 156
    }

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  gtk_render_frame (context, cr, x, y, width, height);

  gtk_css_gadget_draw (swatch->priv->overlay_gadget, cr);

  return gtk_widget_has_visible_focus (widget);
}

static gboolean
gtk_color_swatch_render_overlay (GtkCssGadget *gadget,
                                 cairo_t      *cr,
                                 int           x,
                                 int           y,
                                 int           width,
                                 int           height,
                                 gpointer      data)
{
  GtkWidget *widget;
  GtkColorSwatch *swatch;
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkIconTheme *theme;
  GtkIconInfo *icon_info = NULL;
  gint scale;

  widget = gtk_css_gadget_get_owner (gadget);
  swatch = GTK_COLOR_SWATCH (widget);

  theme = gtk_icon_theme_get_default ();
  context = gtk_widget_get_style_context (widget);
  state = gtk_style_context_get_state (context);
187

188
  scale = gtk_widget_get_scale_factor (widget);
189 190
  if (swatch->priv->icon)
    {
191 192 193 194
      icon_info = gtk_icon_theme_lookup_icon_for_scale (theme, swatch->priv->icon, PIXBUF_SIZE,
                                                        scale,
                                                        GTK_ICON_LOOKUP_GENERIC_FALLBACK
                                                        | GTK_ICON_LOOKUP_USE_BUILTIN);
195
    }
196
  else if ((state & GTK_STATE_FLAG_SELECTED) != 0)
197
    {
198 199
      GIcon *gicon;

200 201 202
      gicon = g_themed_icon_new ("object-select-symbolic");
      /* fallback for themes that don't have object-select-symbolic */
      g_themed_icon_append_name (G_THEMED_ICON (gicon), "gtk-apply");
203

204 205
      icon_info = gtk_icon_theme_lookup_by_gicon_for_scale (theme, gicon, PIXBUF_SIZE,
                                                            scale,
206
                                                            GTK_ICON_LOOKUP_USE_BUILTIN);
207 208
      g_object_unref (gicon);
    }
209

210
  /* now draw the overlay image */
211 212 213 214
  if (icon_info != NULL)
    {
      GdkPixbuf *pixbuf;

215
      pixbuf = gtk_icon_info_load_symbolic_for_context (icon_info, context, NULL, NULL);
216 217
      if (pixbuf != NULL)
        {
218 219 220
          cairo_surface_t *surface;

          surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, gtk_widget_get_window (widget));
221
          gtk_render_icon_surface (context, cr, surface, x, y);
222
          cairo_surface_destroy (surface);
223 224 225
          g_object_unref (pixbuf);
        }

226
      g_object_unref (icon_info);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    }

  return FALSE;
}

static void
drag_set_color_icon (GdkDragContext *context,
                     const GdkRGBA  *color)
{
  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 48, 32);
  cr = cairo_create (surface);
  gdk_cairo_set_source_rgba (cr, color);
  cairo_paint (cr);

Matthias Clasen's avatar
Matthias Clasen committed
244
  cairo_surface_set_device_offset (surface, -4, -4);
245 246 247 248 249 250 251 252 253 254 255 256 257
  gtk_drag_set_icon_surface (context, surface);

  cairo_destroy (cr);
  cairo_surface_destroy (surface);
}

static void
swatch_drag_begin (GtkWidget      *widget,
                   GdkDragContext *context)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
  GdkRGBA color;

Matthias Clasen's avatar
Matthias Clasen committed
258
  gtk_color_swatch_get_rgba (swatch, &color);
259 260 261 262 263 264 265 266 267 268 269 270 271 272
  drag_set_color_icon (context, &color);
}

static void
swatch_drag_data_get (GtkWidget        *widget,
                      GdkDragContext   *context,
                      GtkSelectionData *selection_data,
                      guint             info,
                      guint             time)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
  guint16 vals[4];
  GdkRGBA color;

Matthias Clasen's avatar
Matthias Clasen committed
273
  gtk_color_swatch_get_rgba (swatch, &color);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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

  vals[0] = color.red * 0xffff;
  vals[1] = color.green * 0xffff;
  vals[2] = color.blue * 0xffff;
  vals[3] = color.alpha * 0xffff;

  gtk_selection_data_set (selection_data,
                          gdk_atom_intern_static_string ("application/x-color"),
                          16, (guchar *)vals, 8);
}

static void
swatch_drag_data_received (GtkWidget        *widget,
                           GdkDragContext   *context,
                           gint              x,
                           gint              y,
                           GtkSelectionData *selection_data,
                           guint             info,
                           guint             time)
{
  gint length;
  guint16 *vals;
  GdkRGBA color;

  length = gtk_selection_data_get_length (selection_data);

  if (length < 0)
    return;

  /* We accept drops with the wrong format, since the KDE color
   * chooser incorrectly drops application/x-color with format 8.
   */
  if (length != 8)
    {
      g_warning ("Received invalid color data\n");
      return;
    }

  vals = (guint16 *) gtk_selection_data_get_data (selection_data);

  color.red   = (gdouble)vals[0] / 0xffff;
  color.green = (gdouble)vals[1] / 0xffff;
  color.blue  = (gdouble)vals[2] / 0xffff;
  color.alpha = (gdouble)vals[3] / 0xffff;

Matthias Clasen's avatar
Matthias Clasen committed
319
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (widget), &color);
320 321 322
}

static void
323 324 325 326 327 328 329 330
gtk_color_swatch_measure (GtkCssGadget   *gadget,
                          GtkOrientation  orientation,
                          int             for_size,
                          int            *minimum,
                          int            *natural,
                          int            *minimum_baseline,
                          int            *natural_baseline,
                          gpointer        unused)
331
{
332 333 334
  GtkWidget *widget;
  GtkColorSwatch *swatch;
  gint w, h, min;
335

336 337 338 339 340 341 342 343
  widget = gtk_css_gadget_get_owner (gadget);
  swatch = GTK_COLOR_SWATCH (widget);

  gtk_css_gadget_get_preferred_size (swatch->priv->overlay_gadget,
                                     orientation,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
344

345 346 347 348 349
  gtk_widget_get_size_request (widget, &w, &h);
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    min = w < 0 ? 48 : w;
  else
    min = h < 0 ? 32 : h;
350

351 352
  *minimum = MAX (*minimum, min);
  *natural = MAX (*natural, min);
353 354 355
}

static void
356 357 358 359 360 361 362 363
gtk_color_swatch_measure_overlay (GtkCssGadget   *gadget,
                                  GtkOrientation  orientation,
                                  int             for_size,
                                  int            *minimum,
                                  int            *natural,
                                  int            *minimum_baseline,
                                  int            *natural_baseline,
                                  gpointer        unused)
364
{
365
  *minimum = *natural = 16;
366 367 368 369 370 371 372 373 374 375 376 377 378 379
}

static gboolean
swatch_key_press (GtkWidget   *widget,
                  GdkEventKey *event)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

  if (event->keyval == GDK_KEY_space ||
      event->keyval == GDK_KEY_Return ||
      event->keyval == GDK_KEY_ISO_Enter||
      event->keyval == GDK_KEY_KP_Enter ||
      event->keyval == GDK_KEY_KP_Space)
    {
380
      if (swatch->priv->has_color &&
381 382
          swatch->priv->selectable &&
          (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_SELECTED) == 0)
383
        gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
384 385 386 387 388
      else
        g_signal_emit (swatch, signals[ACTIVATE], 0);
      return TRUE;
    }

389 390 391
  if (GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->key_press_event (widget, event))
    return TRUE;

392 393 394 395
  return FALSE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
396 397
swatch_enter_notify (GtkWidget        *widget,
                     GdkEventCrossing *event)
398
{
399 400
  gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_PRELIGHT, FALSE);

401 402 403 404
  return FALSE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
405 406
swatch_leave_notify (GtkWidget        *widget,
                     GdkEventCrossing *event)
407
{
408 409
  gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_PRELIGHT);

410 411 412
  return FALSE;
}

413 414 415 416 417 418 419
static void
emit_customize (GtkColorSwatch *swatch)
{
  g_signal_emit (swatch, signals[CUSTOMIZE], 0);
}

static void
420
do_popup (GtkColorSwatch *swatch)
421
{
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  if (swatch->priv->popover == NULL)
    {
      GtkWidget *box;
      GtkWidget *item;

      swatch->priv->popover = gtk_popover_new (GTK_WIDGET (swatch));
      box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
      gtk_container_add (GTK_CONTAINER (swatch->priv->popover), box);
      g_object_set (box, "margin", 10, NULL);
      item = g_object_new (GTK_TYPE_MODEL_BUTTON,
                           "text", _("C_ustomize"),
                           NULL);
      g_signal_connect_swapped (item, "clicked",
                                G_CALLBACK (emit_customize), swatch);
      gtk_container_add (GTK_CONTAINER (box), item);
      gtk_widget_show_all (box);
    }
439

440
  gtk_widget_show (swatch->priv->popover);
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static gboolean
swatch_primary_action (GtkColorSwatch *swatch)
{
  GtkWidget *widget = (GtkWidget *)swatch;
  GtkStateFlags flags;

  flags = gtk_widget_get_state_flags (widget);
  if (!swatch->priv->has_color)
    {
      g_signal_emit (swatch, signals[ACTIVATE], 0);
      return TRUE;
    }
  else if (swatch->priv->selectable &&
           (flags & GTK_STATE_FLAG_SELECTED) == 0)
    {
      gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
      return TRUE;
    }

  return FALSE;
}

static void
466 467 468 469
hold_action (GtkGestureLongPress *gesture,
             gdouble              x,
             gdouble              y,
             GtkColorSwatch      *swatch)
470
{
471
  do_popup (swatch);
472
  gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
473 474 475
}

static void
476 477 478 479 480 481
tap_action (GtkGestureMultiPress *gesture,
            gint                  n_press,
            gdouble               x,
            gdouble               y,
            GtkColorSwatch       *swatch)
{
482 483 484 485 486 487 488 489 490 491 492 493 494
  guint button;

  button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));

  if (button == GDK_BUTTON_PRIMARY)
    {
      if (n_press == 1)
        swatch_primary_action (swatch);
      else if (n_press > 1)
        g_signal_emit (swatch, signals[ACTIVATE], 0);
    }
  else if (button == GDK_BUTTON_SECONDARY)
    {
495
      if (swatch->priv->has_color && swatch->priv->has_menu)
496
        do_popup (swatch);
497
    }
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static void
swatch_map (GtkWidget *widget)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

  GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->map (widget);

  if (swatch->priv->event_window)
    gdk_window_show (swatch->priv->event_window);
}

static void
swatch_unmap (GtkWidget *widget)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

  if (swatch->priv->event_window)
    gdk_window_hide (swatch->priv->event_window);

  GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->unmap (widget);
}

522 523 524
static void
swatch_realize (GtkWidget *widget)
{
525
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
526 527 528 529 530 531
  GtkAllocation allocation;
  GdkWindow *window;
  GdkWindowAttr attributes;
  gint attributes_mask;

  gtk_widget_get_allocation (widget, &allocation);
532
  gtk_widget_set_realized (widget, TRUE);
533 534 535 536 537 538

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
539 540
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.event_mask = gtk_widget_get_events (widget);
541 542 543 544 545
  attributes.event_mask |= GDK_BUTTON_PRESS_MASK
                           | GDK_BUTTON_RELEASE_MASK
                           | GDK_ENTER_NOTIFY_MASK
                           | GDK_LEAVE_NOTIFY_MASK
                           | GDK_TOUCH_MASK;
546

547
  attributes_mask = GDK_WA_X | GDK_WA_Y;
548

549
  window = gtk_widget_get_parent_window (widget);
550
  gtk_widget_set_window (widget, window);
551 552
  g_object_ref (window);

553
  swatch->priv->event_window = gdk_window_new (window, &attributes, attributes_mask);
554
  gtk_widget_register_window (widget, swatch->priv->event_window);
555 556 557 558 559 560 561 562 563
}

static void
swatch_unrealize (GtkWidget *widget)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

  if (swatch->priv->event_window)
    {
564
      gtk_widget_unregister_window (widget, swatch->priv->event_window);
565 566 567 568 569 570 571 572
      gdk_window_destroy (swatch->priv->event_window);
      swatch->priv->event_window = NULL;
    }

  GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->unrealize (widget);
}

static void
573
swatch_size_allocate (GtkWidget     *widget,
574 575 576
                      GtkAllocation *allocation)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
577
  GtkAllocation clip;
578 579 580 581 582 583 584 585 586

  gtk_widget_set_allocation (widget, allocation);

  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (swatch->priv->event_window,
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);
587

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  gtk_css_gadget_allocate (swatch->priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_color_swatch_allocate (GtkCssGadget        *gadget,
                           const GtkAllocation *allocation,
                           int                  baseline,
                           GtkAllocation       *out_clip,
                           gpointer             unused)
{
  GtkColorSwatch *swatch;
  GtkAllocation overlay_alloc;
  gint overlay_width, overlay_height;

  swatch = GTK_COLOR_SWATCH (gtk_css_gadget_get_owner (gadget));

  gtk_css_gadget_get_preferred_size (swatch->priv->overlay_gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     &overlay_width, NULL,
                                     NULL, NULL);
  gtk_css_gadget_get_preferred_size (swatch->priv->overlay_gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     &overlay_height, NULL,
                                     NULL, NULL);

  overlay_alloc.x = allocation->x + (allocation->width - overlay_width) / 2;
  overlay_alloc.y = allocation->y + (allocation->height - overlay_height) / 2;
  overlay_alloc.width = overlay_width;
  overlay_alloc.height = overlay_height;

  gtk_css_gadget_allocate (swatch->priv->overlay_gadget, &overlay_alloc, baseline, out_clip);
}

static void
swatch_get_preferred_width (GtkWidget *widget,
                            gint      *minimum,
                            gint      *natural)
{
  gtk_css_gadget_get_preferred_size (GTK_COLOR_SWATCH (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
}

static void
swatch_get_preferred_height (GtkWidget *widget,
                             gint      *minimum,
                             gint      *natural)
{
  gtk_css_gadget_get_preferred_size (GTK_COLOR_SWATCH (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
650 651
}

652
static gboolean
653
swatch_popup_menu (GtkWidget *widget)
654
{
655
  do_popup (GTK_COLOR_SWATCH (widget));
656 657 658
  return TRUE;
}

659 660 661 662 663 664
static void
swatch_state_flags_changed (GtkWidget     *widget,
                            GtkStateFlags  previous_state)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

665 666
  gtk_css_node_set_state (gtk_css_gadget_get_node (swatch->priv->gadget), gtk_widget_get_state_flags (widget));
  gtk_css_node_set_state (gtk_css_gadget_get_node (swatch->priv->overlay_gadget), gtk_widget_get_state_flags (widget));
667 668

  GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->state_flags_changed (widget, previous_state);
669 670
}

Matthias Clasen's avatar
Matthias Clasen committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
/* GObject implementation {{{1 */

static void
swatch_get_property (GObject    *object,
                     guint       prop_id,
                     GValue     *value,
                     GParamSpec *pspec)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
  GdkRGBA color;

  switch (prop_id)
    {
    case PROP_RGBA:
      gtk_color_swatch_get_rgba (swatch, &color);
      g_value_set_boxed (value, &color);
      break;
688 689 690
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_color_swatch_get_selectable (swatch));
      break;
691 692 693
    case PROP_HAS_MENU:
      g_value_set_boolean (value, swatch->priv->has_menu);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
swatch_set_property (GObject      *object,
                     guint         prop_id,
                     const GValue *value,
                     GParamSpec   *pspec)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);

  switch (prop_id)
    {
    case PROP_RGBA:
      gtk_color_swatch_set_rgba (swatch, g_value_get_boxed (value));
      break;
713 714 715
    case PROP_SELECTABLE:
      gtk_color_swatch_set_selectable (swatch, g_value_get_boolean (value));
      break;
716 717 718
    case PROP_HAS_MENU:
      swatch->priv->has_menu = g_value_get_boolean (value);
      break;
Matthias Clasen's avatar
Matthias Clasen committed
719 720 721 722 723 724 725 726 727 728 729 730
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
swatch_finalize (GObject *object)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);

  g_free (swatch->priv->icon);
731 732
  g_clear_object (&swatch->priv->gadget);
  g_clear_object (&swatch->priv->overlay_gadget);
733

Matthias Clasen's avatar
Matthias Clasen committed
734 735 736
  G_OBJECT_CLASS (gtk_color_swatch_parent_class)->finalize (object);
}

737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
static void
swatch_dispose (GObject *object)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);

  if (swatch->priv->popover)
    {
      gtk_widget_destroy (swatch->priv->popover);
      swatch->priv->popover = NULL;
    }

  g_clear_object (&swatch->priv->long_press_gesture);
  g_clear_object (&swatch->priv->multipress_gesture);

  G_OBJECT_CLASS (gtk_color_swatch_parent_class)->dispose (object);
}

754 755 756 757 758 759 760 761 762
static void
gtk_color_swatch_class_init (GtkColorSwatchClass *class)
{
  GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
  GObjectClass *object_class = (GObjectClass *)class;

  object_class->get_property = swatch_get_property;
  object_class->set_property = swatch_set_property;
  object_class->finalize = swatch_finalize;
763
  object_class->dispose = swatch_dispose;
764 765 766 767 768 769 770 771

  widget_class->get_preferred_width = swatch_get_preferred_width;
  widget_class->get_preferred_height = swatch_get_preferred_height;
  widget_class->draw = swatch_draw;
  widget_class->drag_begin = swatch_drag_begin;
  widget_class->drag_data_get = swatch_drag_data_get;
  widget_class->drag_data_received = swatch_drag_data_received;
  widget_class->key_press_event = swatch_key_press;
Matthias Clasen's avatar
Matthias Clasen committed
772 773 774
  widget_class->popup_menu = swatch_popup_menu;
  widget_class->enter_notify_event = swatch_enter_notify;
  widget_class->leave_notify_event = swatch_leave_notify;
775
  widget_class->realize = swatch_realize;
776 777 778 779
  widget_class->unrealize = swatch_unrealize;
  widget_class->map = swatch_map;
  widget_class->unmap = swatch_unmap;
  widget_class->size_allocate = swatch_size_allocate;
780
  widget_class->state_flags_changed = swatch_state_flags_changed;
781 782

  signals[ACTIVATE] =
783
    g_signal_new (I_("activate"),
784 785 786 787 788 789
                  GTK_TYPE_COLOR_SWATCH,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkColorSwatchClass, activate),
                  NULL, NULL, NULL, G_TYPE_NONE, 0);

  signals[CUSTOMIZE] =
790
    g_signal_new (I_("customize"),
791 792 793 794 795
                  GTK_TYPE_COLOR_SWATCH,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkColorSwatchClass, customize),
                  NULL, NULL, NULL, G_TYPE_NONE, 0);

Matthias Clasen's avatar
Matthias Clasen committed
796 797
  g_object_class_install_property (object_class, PROP_RGBA,
      g_param_spec_boxed ("rgba", P_("RGBA Color"), P_("Color as RGBA"),
798
                          GDK_TYPE_RGBA, GTK_PARAM_READWRITE));
799 800 801
  g_object_class_install_property (object_class, PROP_SELECTABLE,
      g_param_spec_boolean ("selectable", P_("Selectable"), P_("Whether the swatch is selectable"),
                            TRUE, GTK_PARAM_READWRITE));
802 803 804
  g_object_class_install_property (object_class, PROP_HAS_MENU,
      g_param_spec_boolean ("has-menu", P_("Has Menu"), P_("Whether the swatch should offer customization"),
                            TRUE, GTK_PARAM_READWRITE));
805

806
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COLOR_SWATCH_ACCESSIBLE);
807
  gtk_widget_class_set_css_name (widget_class, "colorswatch");
808 809
}

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
static void
gtk_color_swatch_init (GtkColorSwatch *swatch)
{
  GtkCssNode *widget_node;

  swatch->priv = gtk_color_swatch_get_instance_private (swatch);
  swatch->priv->use_alpha = TRUE;
  swatch->priv->selectable = TRUE;
  swatch->priv->has_menu = TRUE;

  gtk_widget_set_can_focus (GTK_WIDGET (swatch), TRUE);
  gtk_widget_set_has_window (GTK_WIDGET (swatch), FALSE);

  swatch->priv->long_press_gesture = gtk_gesture_long_press_new (GTK_WIDGET (swatch));
  gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (swatch->priv->long_press_gesture),
                                     TRUE);
  g_signal_connect (swatch->priv->long_press_gesture, "pressed",
                    G_CALLBACK (hold_action), swatch);

  swatch->priv->multipress_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (swatch));
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (swatch->priv->multipress_gesture), 0);
  g_signal_connect (swatch->priv->multipress_gesture, "pressed",
                    G_CALLBACK (tap_action), swatch);

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (swatch));
  swatch->priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                             GTK_WIDGET (swatch),
                                                             gtk_color_swatch_measure,
                                                             gtk_color_swatch_allocate,
                                                             gtk_color_swatch_render,
                                                             NULL,
                                                             NULL);

  swatch->priv->overlay_gadget = gtk_css_custom_gadget_new ("overlay",
                                                            GTK_WIDGET (swatch),
                                                            swatch->priv->gadget,
                                                            NULL,
                                                            gtk_color_swatch_measure_overlay,
                                                            NULL,
                                                            gtk_color_swatch_render_overlay,
                                                            NULL,
                                                            NULL);
  gtk_css_gadget_add_class (swatch->priv->gadget, "activatable");
}
854

855 856 857 858 859 860 861 862
/* Public API {{{1 */

GtkWidget *
gtk_color_swatch_new (void)
{
  return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SWATCH, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
863 864 865 866
static const GtkTargetEntry dnd_targets[] = {
  { "application/x-color", 0 }
};

867
void
Matthias Clasen's avatar
Matthias Clasen committed
868 869
gtk_color_swatch_set_rgba (GtkColorSwatch *swatch,
                           const GdkRGBA  *color)
870
{
871 872 873 874
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (GTK_WIDGET (swatch));

875
  if (!swatch->priv->has_color)
876 877 878 879 880 881
    {
      gtk_drag_source_set (GTK_WIDGET (swatch),
                           GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
                           dnd_targets, G_N_ELEMENTS (dnd_targets),
                           GDK_ACTION_COPY | GDK_ACTION_MOVE);
    }
882 883

  swatch->priv->has_color = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
884
  swatch->priv->color = *color;
885

886
  if (INTENSITY (swatch->priv->color.red, swatch->priv->color.green, swatch->priv->color.blue) > 0.5)
887 888 889 890
    {
      gtk_style_context_add_class (context, "light");
      gtk_style_context_remove_class (context, "dark");
    }
891
  else
892 893 894 895
    {
      gtk_style_context_add_class (context, "dark");
      gtk_style_context_remove_class (context, "light");
    }
896

897
  gtk_widget_queue_draw (GTK_WIDGET (swatch));
Matthias Clasen's avatar
Matthias Clasen committed
898
  g_object_notify (G_OBJECT (swatch), "rgba");
899 900 901
}

gboolean
Matthias Clasen's avatar
Matthias Clasen committed
902 903
gtk_color_swatch_get_rgba (GtkColorSwatch *swatch,
                           GdkRGBA        *color)
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
{
  if (swatch->priv->has_color)
    {
      color->red = swatch->priv->color.red;
      color->green = swatch->priv->color.green;
      color->blue = swatch->priv->color.blue;
      color->alpha = swatch->priv->color.alpha;
      return TRUE;
    }
  else
    {
      color->red = 1.0;
      color->green = 1.0;
      color->blue = 1.0;
      color->alpha = 1.0;
      return FALSE;
    }
}

void
gtk_color_swatch_set_icon (GtkColorSwatch *swatch,
                           const gchar    *icon)
{
  swatch->priv->icon = g_strdup (icon);
  gtk_widget_queue_draw (GTK_WIDGET (swatch));
}

void
gtk_color_swatch_set_can_drop (GtkColorSwatch *swatch,
                               gboolean        can_drop)
{
935 936 937 938 939 940 941 942 943 944 945 946 947
  if (can_drop)
    {
      gtk_drag_dest_set (GTK_WIDGET (swatch),
                         GTK_DEST_DEFAULT_HIGHLIGHT |
                         GTK_DEST_DEFAULT_MOTION |
                         GTK_DEST_DEFAULT_DROP,
                         dnd_targets, G_N_ELEMENTS (dnd_targets),
                         GDK_ACTION_COPY);
    }
  else
    {
      gtk_drag_dest_unset (GTK_WIDGET (swatch));
    }
948 949
}

Matthias Clasen's avatar
Matthias Clasen committed
950
void
Matthias Clasen's avatar
Matthias Clasen committed
951
gtk_color_swatch_set_use_alpha (GtkColorSwatch *swatch,
Matthias Clasen's avatar
Matthias Clasen committed
952
                                gboolean        use_alpha)
Matthias Clasen's avatar
Matthias Clasen committed
953
{
Matthias Clasen's avatar
Matthias Clasen committed
954
  swatch->priv->use_alpha = use_alpha;
Matthias Clasen's avatar
Matthias Clasen committed
955 956 957
  gtk_widget_queue_draw (GTK_WIDGET (swatch));
}

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
void
gtk_color_swatch_set_selectable (GtkColorSwatch *swatch,
                                 gboolean selectable)
{
  if (selectable == swatch->priv->selectable)
    return;

  swatch->priv->selectable = selectable;
  g_object_notify (G_OBJECT (swatch), "selectable");
}

gboolean
gtk_color_swatch_get_selectable (GtkColorSwatch *swatch)
{
  return swatch->priv->selectable;
}

975
/* vim:set foldmethod=marker: */