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

22
#include "gtkcolorchooserprivate.h"
23
#include "gtkroundedboxprivate.h"
24
#include "gtkthemingbackgroundprivate.h"
25 26
#include "gtkdnd.h"
#include "gtkicontheme.h"
27 28 29 30
#include "gtkmain.h"
#include "gtkmenu.h"
#include "gtkmenuitem.h"
#include "gtkmenushell.h"
31 32
#include "gtkprivate.h"
#include "gtkintl.h"
33
#include "a11y/gtkcolorswatchaccessibleprivate.h"
34 35 36 37 38 39 40 41 42


struct _GtkColorSwatchPrivate
{
  GdkRGBA color;
  gdouble radius[4];
  gchar *icon;
  guint    has_color        : 1;
  guint    contains_pointer : 1;
Matthias Clasen's avatar
Matthias Clasen committed
43
  guint    use_alpha        : 1;
44
  guint    selectable       : 1;
45 46

  GdkWindow *event_window;
47

48 49
  GtkGesture *long_press_gesture;
  GtkGesture *multipress_gesture;
50 51 52 53 54
};

enum
{
  PROP_ZERO,
55 56
  PROP_RGBA,
  PROP_SELECTABLE
57 58 59 60 61 62 63 64 65 66 67
};

enum
{
  ACTIVATE,
  CUSTOMIZE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

68 69 70 71 72 73 74 75 76 77
static void hold_action (GtkGestureLongPress  *gesture,
                         gdouble               x,
                         gdouble               y,
                         GtkColorSwatch       *swatch);
static void tap_action  (GtkGestureMultiPress *gesture,
                         gint                  n_press,
                         gdouble               x,
                         gdouble               y,
                         GtkColorSwatch       *swatch);

78
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorSwatch, gtk_color_swatch, GTK_TYPE_WIDGET)
79 80 81 82

static void
gtk_color_swatch_init (GtkColorSwatch *swatch)
{
83 84 85
  swatch->priv = gtk_color_swatch_get_instance_private (swatch);
  swatch->priv->use_alpha = TRUE;
  swatch->priv->selectable = TRUE;
86 87

  gtk_widget_set_can_focus (GTK_WIDGET (swatch), TRUE);
88
  gtk_widget_set_has_window (GTK_WIDGET (swatch), FALSE);
89 90 91 92

  swatch->priv->long_press_gesture = gtk_gesture_long_press_new (GTK_WIDGET (swatch));
  g_signal_connect (swatch->priv->long_press_gesture, "pressed",
                    G_CALLBACK (hold_action), swatch);
93
  gtk_gesture_attach (swatch->priv->long_press_gesture, GTK_PHASE_BUBBLE);
94 95 96 97

  swatch->priv->multipress_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (swatch));
  g_signal_connect (swatch->priv->multipress_gesture, "pressed",
                    G_CALLBACK (tap_action), swatch);
98
  gtk_gesture_attach (swatch->priv->multipress_gesture, GTK_PHASE_BUBBLE);
99 100 101
}

#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
102
#define ACTIVE_BADGE_RADIUS 10
103 104 105 106 107 108

static gboolean
swatch_draw (GtkWidget *widget,
             cairo_t   *cr)
{
  GtkColorSwatch *swatch = (GtkColorSwatch*)widget;
109
  GtkThemingBackground background;
110 111 112
  gdouble width, height;
  GtkStyleContext *context;
  GtkStateFlags state;
113 114
  GtkIconTheme *theme;
  GtkIconInfo *icon_info = NULL;
115

116
  theme = gtk_icon_theme_get_default ();
117 118 119 120 121 122 123
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);

  cairo_save (cr);

124
  gtk_style_context_save (context);
125 126
  gtk_style_context_set_state (context, state);

127 128 129
  _gtk_theming_background_init_from_context (&background, context,
                                             0, 0, width, height,
                                             GTK_JUNCTION_NONE);
Matthias Clasen's avatar
Matthias Clasen committed
130

131 132
  if (swatch->priv->has_color)
    {
133 134 135
      cairo_pattern_t *pattern;
      cairo_matrix_t matrix;

Matthias Clasen's avatar
Matthias Clasen committed
136
      if (swatch->priv->use_alpha)
Matthias Clasen's avatar
Matthias Clasen committed
137
        {
138 139
          cairo_save (cr);

140
          _gtk_rounded_box_path (&background.padding_box, cr);
141 142
          cairo_clip_preserve (cr);

Matthias Clasen's avatar
Matthias Clasen committed
143 144
          cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
          cairo_fill_preserve (cr);
145

146
          pattern = _gtk_color_chooser_get_checkered_pattern ();
Matthias Clasen's avatar
Matthias Clasen committed
147 148
          cairo_matrix_init_scale (&matrix, 0.125, 0.125);
          cairo_pattern_set_matrix (pattern, &matrix);
149 150

          cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
Matthias Clasen's avatar
Matthias Clasen committed
151 152
          cairo_mask (cr, pattern);
          cairo_pattern_destroy (pattern);
153

154 155 156
          cairo_restore (cr);

          background.bg_color = swatch->priv->color;
Matthias Clasen's avatar
Matthias Clasen committed
157 158 159
        }
      else
        {
160 161
          background.bg_color = swatch->priv->color;
          background.bg_color.alpha = 1.0;
Matthias Clasen's avatar
Matthias Clasen committed
162
        }
163 164

      _gtk_theming_background_render (&background, cr);
165
    }
166 167
  else
    _gtk_theming_background_render (&background, cr);
168

169 170
  gtk_render_frame (context, cr,
                    0, 0, width, height);
171 172 173 174 175 176 177 178

  if (gtk_widget_has_visible_focus (widget))
    {
      cairo_set_line_width (cr, 2);
      if (swatch->priv->has_color && INTENSITY (swatch->priv->color.red, swatch->priv->color.green, swatch->priv->color.blue) < 0.5)
        cairo_set_source_rgba (cr, 1., 1., 1., 0.4);
      else
        cairo_set_source_rgba (cr, 0., 0., 0., 0.4);
179 180 181
        _gtk_rounded_box_shrink (&background.padding_box, 3, 3, 3, 3);
        _gtk_rounded_box_path (&background.padding_box, cr);
        cairo_stroke (cr);
182 183 184 185
    }

  if (swatch->priv->icon)
    {
186
      icon_info = gtk_icon_theme_lookup_icon (theme, swatch->priv->icon, 16,
187 188
                                              GTK_ICON_LOOKUP_GENERIC_FALLBACK
                                              | GTK_ICON_LOOKUP_USE_BUILTIN);
189
    }
190
  else if ((state & GTK_STATE_FLAG_SELECTED) != 0)
191
    {
192 193 194 195 196
      GdkRGBA bg, border;
      GtkBorder border_width;
      GIcon *gicon;

      gtk_style_context_add_class (context, "color-active-badge");
197 198 199 200
      _gtk_theming_background_init_from_context (&background, context,
                                                 (width - 2 * ACTIVE_BADGE_RADIUS) / 2, (height - 2 * ACTIVE_BADGE_RADIUS) / 2,
                                                 2 * ACTIVE_BADGE_RADIUS, 2* ACTIVE_BADGE_RADIUS,
                                                 GTK_JUNCTION_NONE);
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      if (_gtk_theming_background_has_background_image (&background))
        {
          _gtk_theming_background_render (&background, cr);
        }
      else
        {
          gtk_style_context_get_background_color (context, state, &bg);
          gtk_style_context_get_border_color (context, state, &border);
          gtk_style_context_get_border (context, state, &border_width);

          cairo_new_sub_path (cr);
          cairo_arc (cr, width / 2, height / 2, ACTIVE_BADGE_RADIUS, 0, 2 * G_PI);
          cairo_close_path (cr);
          gdk_cairo_set_source_rgba (cr, &bg);
          cairo_fill_preserve (cr);

          gdk_cairo_set_source_rgba (cr, &border);
          cairo_set_line_width (cr, border_width.left);
          cairo_stroke (cr);
221

222 223 224 225 226 227 228 229 230
          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");

          icon_info = gtk_icon_theme_lookup_by_gicon (theme, gicon, 16,
                                                      GTK_ICON_LOOKUP_GENERIC_FALLBACK
                                                      | GTK_ICON_LOOKUP_USE_BUILTIN);
          g_object_unref (gicon);
        }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    }

  if (icon_info != NULL)
    {
      GdkPixbuf *pixbuf;

      pixbuf = gtk_icon_info_load_symbolic_for_context (icon_info, context,
                                                        NULL, NULL);

      if (pixbuf != NULL)
        {
          gtk_render_icon (context, cr, pixbuf,
                           (width - gdk_pixbuf_get_width (pixbuf)) / 2,
                           (height - gdk_pixbuf_get_height (pixbuf)) / 2);
          g_object_unref (pixbuf);
        }

248
      g_object_unref (icon_info);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    }

  cairo_restore (cr);
  gtk_style_context_restore (context);

  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
269
  cairo_surface_set_device_offset (surface, -4, -4);
270 271 272 273 274 275 276 277 278 279 280 281 282
  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
283
  gtk_color_swatch_get_rgba (swatch, &color);
284 285 286 287 288 289 290 291 292 293 294 295 296 297
  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
298
  gtk_color_swatch_get_rgba (swatch, &color);
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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

  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
344
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (widget), &color);
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
}

static void
swatch_get_preferred_width (GtkWidget *widget,
                            gint      *min,
                            gint      *nat)
{
  *min = *nat = 48;
}

static void
swatch_get_preferred_height (GtkWidget *widget,
                             gint      *min,
                             gint      *nat)
{
  *min = *nat = 32;
}

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)
    {
375 376 377
      if (swatch->priv->has_color && 
          swatch->priv->selectable &&
          (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_SELECTED) == 0)
378
        gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
379 380 381 382 383
      else
        g_signal_emit (swatch, signals[ACTIVATE], 0);
      return TRUE;
    }

384 385 386
  if (GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->key_press_event (widget, event))
    return TRUE;

387 388 389 390
  return FALSE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
391 392
swatch_enter_notify (GtkWidget        *widget,
                     GdkEventCrossing *event)
393 394 395
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
  swatch->priv->contains_pointer = TRUE;
396 397
  gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_PRELIGHT, FALSE);

398 399 400 401
  return FALSE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
402 403
swatch_leave_notify (GtkWidget        *widget,
                     GdkEventCrossing *event)
404 405 406
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
  swatch->priv->contains_pointer = FALSE;
407 408
  gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_PRELIGHT);

409 410 411
  return FALSE;
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
static void
emit_customize (GtkColorSwatch *swatch)
{
  g_signal_emit (swatch, signals[CUSTOMIZE], 0);
}

static void
popup_position_func (GtkMenu   *menu,
                     gint      *x,
                     gint      *y,
                     gboolean  *push_in,
                     gpointer   user_data)
{
  GtkWidget *widget;
  GtkRequisition req;
  gint root_x, root_y;
  GdkScreen *screen;
  GdkWindow *window;
  GdkRectangle monitor;
  gint monitor_num;

  widget = GTK_WIDGET (user_data);
  g_return_if_fail (gtk_widget_get_realized (widget));
  window = gtk_widget_get_window (widget);

  screen = gtk_widget_get_screen (widget);
  monitor_num = gdk_screen_get_monitor_at_window (screen, window);
  if (monitor_num < 0)
    monitor_num = 0;
  gtk_menu_set_monitor (menu, monitor_num);

  gdk_window_get_origin (window, &root_x, &root_y);
  gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);

  /* Put corner of menu centered on swatch */
  *x = root_x + gtk_widget_get_allocated_width (widget) / 2;
  *y = root_y + gtk_widget_get_allocated_height (widget) / 2;

  /* Ensure sanity */
  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
  *x = CLAMP (*x, monitor.x, MAX (monitor.x, monitor.width - req.width));
  *y = CLAMP (*y, monitor.y, MAX (monitor.y, monitor.height - req.height));
}

static void
do_popup (GtkWidget      *swatch,
          GdkEventButton *event)
{
  GtkWidget *menu;
  GtkWidget *item;

  menu = gtk_menu_new ();
464 465 466
  gtk_style_context_add_class (gtk_widget_get_style_context (menu),
                               GTK_STYLE_CLASS_CONTEXT_MENU);

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  item = gtk_menu_item_new_with_mnemonic (_("_Customize"));
  gtk_menu_attach_to_widget (GTK_MENU (menu), swatch, NULL);

  g_signal_connect_swapped (item, "activate",
                            G_CALLBACK (emit_customize), swatch);

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

  gtk_widget_show_all (item);

  if (event)
    gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
                    NULL, NULL, event->button, event->time);
  else
    gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
                    popup_position_func, swatch,
                    0, gtk_get_current_event_time ());
}

486 487 488 489 490 491
static gboolean
swatch_button_press (GtkWidget      *widget,
                     GdkEventButton *event)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

Matthias Clasen's avatar
Matthias Clasen committed
492
  gtk_widget_grab_focus (widget);
493

494 495
  if (gdk_event_triggers_context_menu ((GdkEvent *) event) &&
      swatch->priv->has_color)
496
    {
497
      do_popup (widget, event);
498 499
      return TRUE;
    }
500 501
  else if (event->type == GDK_2BUTTON_PRESS &&
           event->button == GDK_BUTTON_PRIMARY)
502 503 504 505
    {
      g_signal_emit (swatch, signals[ACTIVATE], 0);
      return TRUE;
    }
506 507 508 509
  else if (event->button == GDK_BUTTON_PRIMARY)
    {
      return TRUE;
    }
510 511 512 513

  return FALSE;
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
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;
}

536 537 538 539 540 541 542 543
static gboolean
swatch_button_release (GtkWidget      *widget,
                       GdkEventButton *event)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

  if (event->button == GDK_BUTTON_PRIMARY &&
      swatch->priv->contains_pointer)
544 545 546 547 548 549
    return swatch_primary_action (swatch);

  return FALSE;
}

static void
550 551 552 553
hold_action (GtkGestureLongPress *gesture,
             gdouble              x,
             gdouble              y,
             GtkColorSwatch      *swatch)
554
{
555
  emit_customize (swatch);
556
  gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
557 558 559
}

static void
560 561 562 563 564 565 566 567
tap_action (GtkGestureMultiPress *gesture,
            gint                  n_press,
            gdouble               x,
            gdouble               y,
            GtkColorSwatch       *swatch)
{
  if (n_press == 1)
    swatch_primary_action (swatch);
568 569
}

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
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);
}

592 593 594
static void
swatch_realize (GtkWidget *widget)
{
595
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
596 597 598 599 600 601
  GtkAllocation allocation;
  GdkWindow *window;
  GdkWindowAttr attributes;
  gint attributes_mask;

  gtk_widget_get_allocation (widget, &allocation);
602
  gtk_widget_set_realized (widget, TRUE);
603 604 605 606 607 608

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
609 610
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.event_mask = gtk_widget_get_events (widget);
611 612 613 614 615
  attributes.event_mask |= GDK_BUTTON_PRESS_MASK
                           | GDK_BUTTON_RELEASE_MASK
                           | GDK_ENTER_NOTIFY_MASK
                           | GDK_LEAVE_NOTIFY_MASK
                           | GDK_TOUCH_MASK;
616

617
  attributes_mask = GDK_WA_X | GDK_WA_Y;
618

619
  window = gtk_widget_get_parent_window (widget);
620
  gtk_widget_set_window (widget, window);
621 622 623 624 625
  g_object_ref (window);

  swatch->priv->event_window = 
    gdk_window_new (window,
                    &attributes, attributes_mask);
626
  gtk_widget_register_window (widget, swatch->priv->event_window);
627 628 629 630 631 632 633 634 635
}

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

  if (swatch->priv->event_window)
    {
636
      gtk_widget_unregister_window (widget, swatch->priv->event_window);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
      gdk_window_destroy (swatch->priv->event_window);
      swatch->priv->event_window = NULL;
    }

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

static void
swatch_size_allocate (GtkWidget *widget,
                      GtkAllocation *allocation)
{
  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);

  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);
658 659
}

660
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
661
swatch_popup_menu (GtkWidget *swatch)
662 663 664 665 666
{
  do_popup (swatch, NULL);
  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
/* 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;
684 685 686
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_color_swatch_get_selectable (swatch));
      break;
Matthias Clasen's avatar
Matthias Clasen committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    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;
706 707 708
    case PROP_SELECTABLE:
      gtk_color_swatch_set_selectable (swatch, g_value_get_boolean (value));
      break;
Matthias Clasen's avatar
Matthias Clasen committed
709 710 711 712 713 714 715 716 717 718 719 720
    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);
721

722
  gtk_gesture_detach (swatch->priv->long_press_gesture);
723 724
  g_object_unref (swatch->priv->long_press_gesture);

725
  gtk_gesture_detach (swatch->priv->multipress_gesture);
726
  g_object_unref (swatch->priv->multipress_gesture);
Matthias Clasen's avatar
Matthias Clasen committed
727 728 729 730

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

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
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;

  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
748
  widget_class->popup_menu = swatch_popup_menu;
749 750
  widget_class->button_press_event = swatch_button_press;
  widget_class->button_release_event = swatch_button_release;
Matthias Clasen's avatar
Matthias Clasen committed
751 752
  widget_class->enter_notify_event = swatch_enter_notify;
  widget_class->leave_notify_event = swatch_leave_notify;
753
  widget_class->realize = swatch_realize;
754 755 756 757
  widget_class->unrealize = swatch_unrealize;
  widget_class->map = swatch_map;
  widget_class->unmap = swatch_unmap;
  widget_class->size_allocate = swatch_size_allocate;
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

  signals[ACTIVATE] =
    g_signal_new ("activate",
                  GTK_TYPE_COLOR_SWATCH,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkColorSwatchClass, activate),
                  NULL, NULL, NULL, G_TYPE_NONE, 0);

  signals[CUSTOMIZE] =
    g_signal_new ("customize",
                  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
773 774
  g_object_class_install_property (object_class, PROP_RGBA,
      g_param_spec_boxed ("rgba", P_("RGBA Color"), P_("Color as RGBA"),
775
                          GDK_TYPE_RGBA, GTK_PARAM_READWRITE));
776 777 778
  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));
779

780
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COLOR_SWATCH_ACCESSIBLE);
781 782 783 784 785 786 787 788 789 790
}

/* 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
791 792 793 794
static const GtkTargetEntry dnd_targets[] = {
  { "application/x-color", 0 }
};

795
void
Matthias Clasen's avatar
Matthias Clasen committed
796 797
gtk_color_swatch_set_rgba (GtkColorSwatch *swatch,
                           const GdkRGBA  *color)
798
{
799 800 801 802
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (GTK_WIDGET (swatch));

803
  if (!swatch->priv->has_color)
804 805 806 807 808 809 810 811 812 813 814
    {
      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);
    }
  else
    {
      gtk_style_context_remove_class (context, "color-light");
      gtk_style_context_remove_class (context, "color-dark");
    }
815 816

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

819 820 821 822 823
  if (INTENSITY (swatch->priv->color.red, swatch->priv->color.green, swatch->priv->color.blue) > 0.5)
    gtk_style_context_add_class (context, "color-light");
  else
    gtk_style_context_add_class (context, "color-dark");

824
  gtk_widget_queue_draw (GTK_WIDGET (swatch));
Matthias Clasen's avatar
Matthias Clasen committed
825
  g_object_notify (G_OBJECT (swatch), "rgba");
826 827 828
}

gboolean
Matthias Clasen's avatar
Matthias Clasen committed
829 830
gtk_color_swatch_get_rgba (GtkColorSwatch *swatch,
                           GdkRGBA        *color)
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
{
  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)
{
862 863 864 865 866 867 868 869 870 871 872 873 874
  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));
    }
875 876
}

Matthias Clasen's avatar
Matthias Clasen committed
877
void
Matthias Clasen's avatar
Matthias Clasen committed
878
gtk_color_swatch_set_use_alpha (GtkColorSwatch *swatch,
Matthias Clasen's avatar
Matthias Clasen committed
879
                                gboolean        use_alpha)
Matthias Clasen's avatar
Matthias Clasen committed
880
{
Matthias Clasen's avatar
Matthias Clasen committed
881
  swatch->priv->use_alpha = use_alpha;
Matthias Clasen's avatar
Matthias Clasen committed
882 883 884
  gtk_widget_queue_draw (GTK_WIDGET (swatch));
}

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
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;
}

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