1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2012 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcolorswatch.h"
24 #include "gtkroundedboxprivate.h"
26 #include "gtkicontheme.h"
29 #include "gtkmenuitem.h"
30 #include "gtkmenushell.h"
31 #include "gtkprivate.h"
35 struct _GtkColorSwatchPrivate
43 guint contains_pointer : 1;
61 static guint signals[LAST_SIGNAL];
63 G_DEFINE_TYPE (GtkColorSwatch, gtk_color_swatch, GTK_TYPE_DRAWING_AREA)
66 gtk_color_swatch_init (GtkColorSwatch *swatch)
68 swatch->priv = G_TYPE_INSTANCE_GET_PRIVATE (swatch,
69 GTK_TYPE_COLOR_SWATCH,
70 GtkColorSwatchPrivate);
72 gtk_widget_set_can_focus (GTK_WIDGET (swatch), TRUE);
73 gtk_widget_set_events (GTK_WIDGET (swatch), GDK_BUTTON_PRESS_MASK
74 | GDK_BUTTON_RELEASE_MASK
76 | GDK_ENTER_NOTIFY_MASK
77 | GDK_LEAVE_NOTIFY_MASK);
78 swatch->priv->use_alpha = TRUE;
81 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
83 static cairo_pattern_t *
84 get_checkered_pattern (void)
86 /* need to respect pixman's stride being a multiple of 4 */
87 static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
88 0x00, 0xFF, 0x00, 0x00 };
89 static cairo_surface_t *checkered = NULL;
90 cairo_pattern_t *pattern;
92 if (checkered == NULL)
93 checkered = cairo_image_surface_create_for_data (data,
97 pattern = cairo_pattern_create_for_surface (checkered);
98 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
99 cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
105 swatch_draw (GtkWidget *widget,
108 GtkColorSwatch *swatch = (GtkColorSwatch*)widget;
111 gdouble width, height;
112 GtkStyleContext *context;
116 context = gtk_widget_get_style_context (widget);
117 state = gtk_widget_get_state_flags (widget);
118 width = gtk_widget_get_allocated_width (widget);
119 height = gtk_widget_get_allocated_height (widget);
121 gtk_style_context_save (context);
124 gtk_style_context_set_state (context, state);
126 _gtk_rounded_box_init_rect (&box, 0, 0, width, height);
127 for (i = 0; i < 4; i++)
128 box.corner[i].horizontal = box.corner[i].vertical = swatch->priv->radius[i];
130 _gtk_rounded_box_path (&box, cr);
132 cairo_clip_preserve (cr);
134 if (swatch->priv->has_color)
136 cairo_pattern_t *pattern;
137 cairo_matrix_t matrix;
139 if (swatch->priv->use_alpha)
141 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
142 cairo_fill_preserve (cr);
143 cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
145 pattern = get_checkered_pattern ();
146 cairo_matrix_init_scale (&matrix, 0.125, 0.125);
147 cairo_pattern_set_matrix (pattern, &matrix);
148 cairo_mask (cr, pattern);
149 cairo_pattern_destroy (pattern);
151 gdk_cairo_set_source_rgba (cr, &swatch->priv->color);
155 cairo_set_source_rgb (cr,
156 swatch->priv->color.red,
157 swatch->priv->color.green,
158 swatch->priv->color.blue);
160 cairo_fill_preserve (cr);
163 cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
164 cairo_set_line_width (cr, 1);
167 if (gtk_widget_has_visible_focus (widget))
169 cairo_set_line_width (cr, 2);
170 if (swatch->priv->has_color && INTENSITY (swatch->priv->color.red, swatch->priv->color.green, swatch->priv->color.blue) < 0.5)
171 cairo_set_source_rgba (cr, 1., 1., 1., 0.4);
173 cairo_set_source_rgba (cr, 0., 0., 0., 0.4);
174 _gtk_rounded_box_shrink (&box, 3, 3, 3, 3);
175 _gtk_rounded_box_path (&box, cr);
179 if (swatch->priv->icon)
183 theme = gtk_icon_theme_get_default ();
184 pixbuf = gtk_icon_theme_load_icon (theme, "list-add-symbolic", 16,
185 GTK_ICON_LOOKUP_GENERIC_FALLBACK
186 | GTK_ICON_LOOKUP_USE_BUILTIN,
189 gtk_render_icon (context, cr, pixbuf,
190 (width - gdk_pixbuf_get_width (pixbuf)) / 2,
191 (height - gdk_pixbuf_get_height (pixbuf)) / 2);
192 g_object_unref (pixbuf);
194 else if (swatch->priv->selected)
196 gtk_style_context_get_background_color (context, state, &bg);
197 cairo_new_sub_path (cr);
198 cairo_arc (cr, width / 2, height / 2, 10, 0, 2 * G_PI);
199 cairo_close_path (cr);
200 gdk_cairo_set_source_rgba (cr, &bg);
201 cairo_fill_preserve (cr);
202 if (INTENSITY (swatch->priv->color.red, swatch->priv->color.green, swatch->priv->color.blue) > 0.5)
203 cairo_set_source_rgba (cr, 0., 0., 0., 0.4);
205 cairo_set_source_rgba (cr, 1., 1., 1., 0.4);
206 cairo_set_line_width (cr, 2);
208 gtk_style_context_set_state (context, state | GTK_STATE_FLAG_ACTIVE);
209 gtk_render_check (context, cr, width / 2 - 5, height / 2 - 5, 10, 10);
213 gtk_style_context_restore (context);
219 drag_set_color_icon (GdkDragContext *context,
220 const GdkRGBA *color)
222 cairo_surface_t *surface;
225 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 48, 32);
226 cr = cairo_create (surface);
227 gdk_cairo_set_source_rgba (cr, color);
230 cairo_surface_set_device_offset (surface, -4, -4);
231 gtk_drag_set_icon_surface (context, surface);
234 cairo_surface_destroy (surface);
238 swatch_drag_begin (GtkWidget *widget,
239 GdkDragContext *context)
241 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
244 gtk_color_swatch_get_rgba (swatch, &color);
245 drag_set_color_icon (context, &color);
249 swatch_drag_data_get (GtkWidget *widget,
250 GdkDragContext *context,
251 GtkSelectionData *selection_data,
255 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
259 gtk_color_swatch_get_rgba (swatch, &color);
261 vals[0] = color.red * 0xffff;
262 vals[1] = color.green * 0xffff;
263 vals[2] = color.blue * 0xffff;
264 vals[3] = color.alpha * 0xffff;
266 gtk_selection_data_set (selection_data,
267 gdk_atom_intern_static_string ("application/x-color"),
268 16, (guchar *)vals, 8);
272 swatch_drag_data_received (GtkWidget *widget,
273 GdkDragContext *context,
276 GtkSelectionData *selection_data,
284 length = gtk_selection_data_get_length (selection_data);
289 /* We accept drops with the wrong format, since the KDE color
290 * chooser incorrectly drops application/x-color with format 8.
294 g_warning ("Received invalid color data\n");
298 vals = (guint16 *) gtk_selection_data_get_data (selection_data);
300 color.red = (gdouble)vals[0] / 0xffff;
301 color.green = (gdouble)vals[1] / 0xffff;
302 color.blue = (gdouble)vals[2] / 0xffff;
303 color.alpha = (gdouble)vals[3] / 0xffff;
305 gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (widget), &color);
309 swatch_get_preferred_width (GtkWidget *widget,
317 swatch_get_preferred_height (GtkWidget *widget,
325 swatch_key_press (GtkWidget *widget,
328 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
330 if (event->keyval == GDK_KEY_space ||
331 event->keyval == GDK_KEY_Return ||
332 event->keyval == GDK_KEY_ISO_Enter||
333 event->keyval == GDK_KEY_KP_Enter ||
334 event->keyval == GDK_KEY_KP_Space)
336 if (swatch->priv->has_color && !swatch->priv->selected)
337 gtk_color_swatch_set_selected (swatch, TRUE);
339 g_signal_emit (swatch, signals[ACTIVATE], 0);
343 if (GTK_WIDGET_CLASS (gtk_color_swatch_parent_class)->key_press_event (widget, event))
350 swatch_enter_notify (GtkWidget *widget,
351 GdkEventCrossing *event)
353 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
354 swatch->priv->contains_pointer = TRUE;
359 swatch_leave_notify (GtkWidget *widget,
360 GdkEventCrossing *event)
362 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
363 swatch->priv->contains_pointer = FALSE;
368 emit_customize (GtkColorSwatch *swatch)
370 g_signal_emit (swatch, signals[CUSTOMIZE], 0);
374 popup_position_func (GtkMenu *menu,
385 GdkRectangle monitor;
388 widget = GTK_WIDGET (user_data);
389 g_return_if_fail (gtk_widget_get_realized (widget));
390 window = gtk_widget_get_window (widget);
392 screen = gtk_widget_get_screen (widget);
393 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
396 gtk_menu_set_monitor (menu, monitor_num);
398 gdk_window_get_origin (window, &root_x, &root_y);
399 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
401 /* Put corner of menu centered on swatch */
402 *x = root_x + gtk_widget_get_allocated_width (widget) / 2;
403 *y = root_y + gtk_widget_get_allocated_height (widget) / 2;
406 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
407 *x = CLAMP (*x, monitor.x, MAX (monitor.x, monitor.width - req.width));
408 *y = CLAMP (*y, monitor.y, MAX (monitor.y, monitor.height - req.height));
412 do_popup (GtkWidget *swatch,
413 GdkEventButton *event)
418 menu = gtk_menu_new ();
419 item = gtk_menu_item_new_with_mnemonic (_("_Customize"));
420 gtk_menu_attach_to_widget (GTK_MENU (menu), swatch, NULL);
422 g_signal_connect_swapped (item, "activate",
423 G_CALLBACK (emit_customize), swatch);
425 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
427 gtk_widget_show_all (item);
430 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
431 NULL, NULL, event->button, event->time);
433 gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
434 popup_position_func, swatch,
435 0, gtk_get_current_event_time ());
439 swatch_button_press (GtkWidget *widget,
440 GdkEventButton *event)
442 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
444 gtk_widget_grab_focus (widget);
446 if (gdk_event_triggers_context_menu ((GdkEvent *) event) &&
447 swatch->priv->has_color)
449 do_popup (widget, event);
452 else if (event->type == GDK_2BUTTON_PRESS &&
453 event->button == GDK_BUTTON_PRIMARY)
455 g_signal_emit (swatch, signals[ACTIVATE], 0);
463 swatch_button_release (GtkWidget *widget,
464 GdkEventButton *event)
466 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
468 if (event->button == GDK_BUTTON_PRIMARY &&
469 swatch->priv->contains_pointer)
471 if (!swatch->priv->has_color)
473 g_signal_emit (swatch, signals[ACTIVATE], 0);
476 else if (!swatch->priv->selected)
478 gtk_color_swatch_set_selected (swatch, TRUE);
487 swatch_popup_menu (GtkWidget *swatch)
489 do_popup (swatch, NULL);
493 /* GObject implementation {{{1 */
496 swatch_get_property (GObject *object,
501 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
507 gtk_color_swatch_get_rgba (swatch, &color);
508 g_value_set_boxed (value, &color);
511 g_value_set_boolean (value, swatch->priv->selected);
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
520 swatch_set_property (GObject *object,
525 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
530 gtk_color_swatch_set_rgba (swatch, g_value_get_boxed (value));
533 gtk_color_swatch_set_selected (swatch, g_value_get_boolean (value));
536 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
542 swatch_finalize (GObject *object)
544 GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
546 g_free (swatch->priv->icon);
548 G_OBJECT_CLASS (gtk_color_swatch_parent_class)->finalize (object);
552 gtk_color_swatch_class_init (GtkColorSwatchClass *class)
554 GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
555 GObjectClass *object_class = (GObjectClass *)class;
557 object_class->get_property = swatch_get_property;
558 object_class->set_property = swatch_set_property;
559 object_class->finalize = swatch_finalize;
561 widget_class->get_preferred_width = swatch_get_preferred_width;
562 widget_class->get_preferred_height = swatch_get_preferred_height;
563 widget_class->draw = swatch_draw;
564 widget_class->drag_begin = swatch_drag_begin;
565 widget_class->drag_data_get = swatch_drag_data_get;
566 widget_class->drag_data_received = swatch_drag_data_received;
567 widget_class->key_press_event = swatch_key_press;
568 widget_class->popup_menu = swatch_popup_menu;
569 widget_class->button_press_event = swatch_button_press;
570 widget_class->button_release_event = swatch_button_release;
571 widget_class->enter_notify_event = swatch_enter_notify;
572 widget_class->leave_notify_event = swatch_leave_notify;
575 g_signal_new ("activate",
576 GTK_TYPE_COLOR_SWATCH,
578 G_STRUCT_OFFSET (GtkColorSwatchClass, activate),
579 NULL, NULL, NULL, G_TYPE_NONE, 0);
582 g_signal_new ("customize",
583 GTK_TYPE_COLOR_SWATCH,
585 G_STRUCT_OFFSET (GtkColorSwatchClass, customize),
586 NULL, NULL, NULL, G_TYPE_NONE, 0);
588 g_object_class_install_property (object_class, PROP_RGBA,
589 g_param_spec_boxed ("rgba", P_("RGBA Color"), P_("Color as RGBA"),
590 GDK_TYPE_RGBA, GTK_PARAM_READWRITE));
592 g_object_class_install_property (object_class, PROP_SELECTED,
593 g_param_spec_boolean ("selected", P_("Selected"), P_("Selected"),
594 FALSE, GTK_PARAM_READWRITE));
596 g_type_class_add_private (object_class, sizeof (GtkColorSwatchPrivate));
599 /* Public API {{{1 */
602 gtk_color_swatch_new (void)
604 return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SWATCH, NULL);
607 static const GtkTargetEntry dnd_targets[] = {
608 { "application/x-color", 0 }
612 gtk_color_swatch_set_rgba (GtkColorSwatch *swatch,
613 const GdkRGBA *color)
615 GtkStyleContext *context;
617 context = gtk_widget_get_style_context (GTK_WIDGET (swatch));
619 if (!swatch->priv->has_color)
621 gtk_drag_source_set (GTK_WIDGET (swatch),
622 GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
623 dnd_targets, G_N_ELEMENTS (dnd_targets),
624 GDK_ACTION_COPY | GDK_ACTION_MOVE);
628 gtk_style_context_remove_class (context, "color-light");
629 gtk_style_context_remove_class (context, "color-dark");
632 swatch->priv->has_color = TRUE;
633 swatch->priv->color = *color;
635 if (INTENSITY (swatch->priv->color.red, swatch->priv->color.green, swatch->priv->color.blue) > 0.5)
636 gtk_style_context_add_class (context, "color-light");
638 gtk_style_context_add_class (context, "color-dark");
640 gtk_widget_queue_draw (GTK_WIDGET (swatch));
641 g_object_notify (G_OBJECT (swatch), "rgba");
645 gtk_color_swatch_get_rgba (GtkColorSwatch *swatch,
648 if (swatch->priv->has_color)
650 color->red = swatch->priv->color.red;
651 color->green = swatch->priv->color.green;
652 color->blue = swatch->priv->color.blue;
653 color->alpha = swatch->priv->color.alpha;
667 gtk_color_swatch_set_corner_radii (GtkColorSwatch *swatch,
670 gdouble bottom_right,
673 swatch->priv->radius[0] = top_left;
674 swatch->priv->radius[1] = top_right;
675 swatch->priv->radius[2] = bottom_right;
676 swatch->priv->radius[3] = bottom_left;
678 gtk_widget_queue_draw (GTK_WIDGET (swatch));
682 gtk_color_swatch_set_selected (GtkColorSwatch *swatch,
685 if (swatch->priv->selected != selected)
687 swatch->priv->selected = selected;
688 gtk_widget_queue_draw (GTK_WIDGET (swatch));
689 g_object_notify (G_OBJECT (swatch), "selected");
694 gtk_color_swatch_set_icon (GtkColorSwatch *swatch,
697 swatch->priv->icon = g_strdup (icon);
698 gtk_widget_queue_draw (GTK_WIDGET (swatch));
702 gtk_color_swatch_set_can_drop (GtkColorSwatch *swatch,
705 if (!swatch->priv->can_drop)
706 gtk_drag_dest_set (GTK_WIDGET (swatch),
707 GTK_DEST_DEFAULT_HIGHLIGHT |
708 GTK_DEST_DEFAULT_MOTION |
709 GTK_DEST_DEFAULT_DROP,
710 dnd_targets, G_N_ELEMENTS (dnd_targets),
713 swatch->priv->can_drop = can_drop;
717 gtk_color_swatch_set_use_alpha (GtkColorSwatch *swatch,
720 swatch->priv->use_alpha = use_alpha;
721 gtk_widget_queue_draw (GTK_WIDGET (swatch));
724 /* vim:set foldmethod=marker: */