1 /*
2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
4 * All rights reserved.
5 *
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 /* Color picker button for GNOME
20 *
21 * Author: Federico Mena <federico@nuclecu.unam.mx>
22 *
23 * Modified by the GTK+ Team and others 2003. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 */
28
29 #include "config.h"
30
31 #include "gtkcolorbutton.h"
32
33 #include "gtkbutton.h"
34 #include "gtkmain.h"
35 #include "gtkcolorchooser.h"
36 #include "gtkcolorchooserprivate.h"
37 #include "gtkcolorchooserdialog.h"
38 #include "gtkcolorswatchprivate.h"
39 #include "gtkdnd.h"
40 #include "gtkdragdest.h"
41 #include "gtkdragsource.h"
42 #include "gtkmarshalers.h"
43 #include "gtkprivate.h"
44 #include "gtkintl.h"
45
46
47 /**
48 * SECTION:gtkcolorbutton
49 * @Short_description: A button to launch a color selection dialog
50 * @Title: GtkColorButton
51 * @See_also: #GtkColorSelectionDialog, #GtkFontButton
52 *
53 * The #GtkColorButton is a button which displays the currently selected
54 * color and allows to open a color selection dialog to change the color.
55 * It is suitable widget for selecting a color in a preference dialog.
56 *
57 * # CSS nodes
58 *
59 * GtkColorButton has a single CSS node with name button. To differentiate
60 * it from a plain #GtkButton, it gets the .color style class.
61 */
62
63
64 struct _GtkColorButtonPrivate
65 {
66 GtkWidget *swatch; /* Widget where we draw the color sample */
67 GtkWidget *cs_dialog; /* Color selection dialog */
68
69 gchar *title; /* Title for the color selection window */
70 GdkRGBA rgba;
71
72 guint use_alpha : 1; /* Use alpha or not */
73 guint show_editor : 1;
74 };
75
76 /* Properties */
77 enum
78 {
79 PROP_0,
80 PROP_USE_ALPHA,
81 PROP_TITLE,
82 PROP_COLOR,
83 PROP_ALPHA,
84 PROP_RGBA,
85 PROP_SHOW_EDITOR
86 };
87
88 /* Signals */
89 enum
90 {
91 COLOR_SET,
92 LAST_SIGNAL
93 };
94
95 /* gobject signals */
96 static void gtk_color_button_finalize (GObject *object);
97 static void gtk_color_button_set_property (GObject *object,
98 guint param_id,
99 const GValue *value,
100 GParamSpec *pspec);
101 static void gtk_color_button_get_property (GObject *object,
102 guint param_id,
103 GValue *value,
104 GParamSpec *pspec);
105
106 /* gtkbutton signals */
107 static void gtk_color_button_clicked (GtkButton *button);
108
109 /* source side drag signals */
110 static void gtk_color_button_drag_begin (GtkWidget *widget,
111 GdkDragContext *context,
112 gpointer data);
113 static void gtk_color_button_drag_data_get (GtkWidget *widget,
114 GdkDragContext *context,
115 GtkSelectionData *selection_data,
116 guint info,
117 guint time,
118 GtkColorButton *button);
119
120 /* target side drag signals */
121 static void gtk_color_button_drag_data_received (GtkWidget *widget,
122 GdkDragContext *context,
123 gint x,
124 gint y,
125 GtkSelectionData *selection_data,
126 guint info,
127 guint32 time,
128 GtkColorButton *button);
129
130
131 static guint color_button_signals[LAST_SIGNAL] = { 0 };
132
133 static const GtkTargetEntry drop_types[] = { { "application/x-color", 0, 0 } };
134
135 static void gtk_color_button_iface_init (GtkColorChooserInterface *iface);
136
G_DEFINE_TYPE_WITH_CODE(GtkColorButton,gtk_color_button,GTK_TYPE_BUTTON,G_ADD_PRIVATE (GtkColorButton)G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,gtk_color_button_iface_init))137 G_DEFINE_TYPE_WITH_CODE (GtkColorButton, gtk_color_button, GTK_TYPE_BUTTON,
138 G_ADD_PRIVATE (GtkColorButton)
139 G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,
140 gtk_color_button_iface_init))
141
142 static void
143 gtk_color_button_class_init (GtkColorButtonClass *klass)
144 {
145 GObjectClass *gobject_class;
146 GtkButtonClass *button_class;
147
148 gobject_class = G_OBJECT_CLASS (klass);
149 button_class = GTK_BUTTON_CLASS (klass);
150
151 gobject_class->get_property = gtk_color_button_get_property;
152 gobject_class->set_property = gtk_color_button_set_property;
153 gobject_class->finalize = gtk_color_button_finalize;
154 button_class->clicked = gtk_color_button_clicked;
155 klass->color_set = NULL;
156
157 /**
158 * GtkColorButton:use-alpha:
159 *
160 * If this property is set to %TRUE, the color swatch on the button is
161 * rendered against a checkerboard background to show its opacity and
162 * the opacity slider is displayed in the color selection dialog.
163 *
164 * Since: 2.4
165 */
166 g_object_class_install_property (gobject_class,
167 PROP_USE_ALPHA,
168 g_param_spec_boolean ("use-alpha", P_("Use alpha"),
169 P_("Whether to give the color an alpha value"),
170 FALSE,
171 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
172
173 /**
174 * GtkColorButton:title:
175 *
176 * The title of the color selection dialog
177 *
178 * Since: 2.4
179 */
180 g_object_class_install_property (gobject_class,
181 PROP_TITLE,
182 g_param_spec_string ("title",
183 P_("Title"),
184 P_("The title of the color selection dialog"),
185 _("Pick a Color"),
186 GTK_PARAM_READWRITE));
187
188 /**
189 * GtkColorButton:color:
190 *
191 * The selected color.
192 *
193 * Since: 2.4
194 *
195 * Deprecated: 3.4: Use #GtkColorButton:rgba instead.
196 */
197 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
198 g_object_class_install_property (gobject_class,
199 PROP_COLOR,
200 g_param_spec_boxed ("color",
201 P_("Current Color"),
202 P_("The selected color"),
203 GDK_TYPE_COLOR,
204 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
205 G_GNUC_END_IGNORE_DEPRECATIONS
206
207 /**
208 * GtkColorButton:alpha:
209 *
210 * The selected opacity value (0 fully transparent, 65535 fully opaque).
211 *
212 * Since: 2.4
213 */
214 g_object_class_install_property (gobject_class,
215 PROP_ALPHA,
216 g_param_spec_uint ("alpha",
217 P_("Current Alpha"),
218 P_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
219 0, 65535, 65535,
220 GTK_PARAM_READWRITE));
221
222 /**
223 * GtkColorButton:rgba:
224 *
225 * The RGBA color.
226 *
227 * Since: 3.0
228 */
229 g_object_class_install_property (gobject_class,
230 PROP_RGBA,
231 g_param_spec_boxed ("rgba",
232 P_("Current RGBA Color"),
233 P_("The selected RGBA color"),
234 GDK_TYPE_RGBA,
235 GTK_PARAM_READWRITE));
236
237
238 /**
239 * GtkColorButton::color-set:
240 * @widget: the object which received the signal.
241 *
242 * The ::color-set signal is emitted when the user selects a color.
243 * When handling this signal, use gtk_color_button_get_rgba() to
244 * find out which color was just selected.
245 *
246 * Note that this signal is only emitted when the user
247 * changes the color. If you need to react to programmatic color changes
248 * as well, use the notify::color signal.
249 *
250 * Since: 2.4
251 */
252 color_button_signals[COLOR_SET] = g_signal_new (I_("color-set"),
253 G_TYPE_FROM_CLASS (gobject_class),
254 G_SIGNAL_RUN_FIRST,
255 G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
256 NULL, NULL,
257 NULL,
258 G_TYPE_NONE, 0);
259
260 /**
261 * GtkColorButton:show-editor:
262 *
263 * Set this property to %TRUE to skip the palette
264 * in the dialog and go directly to the color editor.
265 *
266 * This property should be used in cases where the palette
267 * in the editor would be redundant, such as when the color
268 * button is already part of a palette.
269 *
270 * Since: 3.20
271 */
272 g_object_class_install_property (gobject_class,
273 PROP_SHOW_EDITOR,
274 g_param_spec_boolean ("show-editor", P_("Show Editor"),
275 P_("Whether to show the color editor right away"),
276 FALSE,
277 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
278 }
279
280 static void
gtk_color_button_drag_data_received(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * selection_data,guint info,guint32 time,GtkColorButton * button)281 gtk_color_button_drag_data_received (GtkWidget *widget,
282 GdkDragContext *context,
283 gint x,
284 gint y,
285 GtkSelectionData *selection_data,
286 guint info,
287 guint32 time,
288 GtkColorButton *button)
289 {
290 GtkColorButtonPrivate *priv = button->priv;
291 gint length;
292 guint16 *dropped;
293
294 length = gtk_selection_data_get_length (selection_data);
295
296 if (length < 0)
297 return;
298
299 /* We accept drops with the wrong format, since the KDE color
300 * chooser incorrectly drops application/x-color with format 8.
301 */
302 if (length != 8)
303 {
304 g_warning ("%s: Received invalid color data", G_STRFUNC);
305 return;
306 }
307
308
309 dropped = (guint16 *) gtk_selection_data_get_data (selection_data);
310
311 priv->rgba.red = dropped[0] / 65535.;
312 priv->rgba.green = dropped[1] / 65535.;
313 priv->rgba.blue = dropped[2] / 65535.;
314 priv->rgba.alpha = dropped[3] / 65535.;
315
316 gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
317
318 g_signal_emit (button, color_button_signals[COLOR_SET], 0);
319
320 g_object_freeze_notify (G_OBJECT (button));
321 g_object_notify (G_OBJECT (button), "color");
322 g_object_notify (G_OBJECT (button), "alpha");
323 g_object_notify (G_OBJECT (button), "rgba");
324 g_object_thaw_notify (G_OBJECT (button));
325 }
326
327 static void
set_color_icon(GdkDragContext * context,GdkRGBA * rgba)328 set_color_icon (GdkDragContext *context,
329 GdkRGBA *rgba)
330 {
331 cairo_surface_t *surface;
332 cairo_t *cr;
333
334 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 48, 32);
335 cr = cairo_create (surface);
336
337 gdk_cairo_set_source_rgba (cr, rgba);
338 cairo_paint (cr);
339
340 gtk_drag_set_icon_surface (context, surface);
341
342 cairo_destroy (cr);
343 cairo_surface_destroy (surface);
344 }
345
346 static void
gtk_color_button_drag_begin(GtkWidget * widget,GdkDragContext * context,gpointer data)347 gtk_color_button_drag_begin (GtkWidget *widget,
348 GdkDragContext *context,
349 gpointer data)
350 {
351 GtkColorButton *button = data;
352
353 set_color_icon (context, &button->priv->rgba);
354 }
355
356 static void
gtk_color_button_drag_data_get(GtkWidget * widget,GdkDragContext * context,GtkSelectionData * selection_data,guint info,guint time,GtkColorButton * button)357 gtk_color_button_drag_data_get (GtkWidget *widget,
358 GdkDragContext *context,
359 GtkSelectionData *selection_data,
360 guint info,
361 guint time,
362 GtkColorButton *button)
363 {
364 GtkColorButtonPrivate *priv = button->priv;
365 guint16 dropped[4];
366
367 dropped[0] = (guint16) (priv->rgba.red * 65535);
368 dropped[1] = (guint16) (priv->rgba.green * 65535);
369 dropped[2] = (guint16) (priv->rgba.blue * 65535);
370 dropped[3] = (guint16) (priv->rgba.alpha * 65535);
371
372 gtk_selection_data_set (selection_data,
373 gtk_selection_data_get_target (selection_data),
374 16, (guchar *)dropped, 8);
375 }
376
377 static void
gtk_color_button_init(GtkColorButton * button)378 gtk_color_button_init (GtkColorButton *button)
379 {
380 GtkColorButtonPrivate *priv;
381 PangoLayout *layout;
382 PangoRectangle rect;
383 GtkStyleContext *context;
384
385 /* Create the widgets */
386 priv = button->priv = gtk_color_button_get_instance_private (button);
387
388 priv->swatch = gtk_color_swatch_new ();
389 layout = gtk_widget_create_pango_layout (GTK_WIDGET (button), "Black");
390 pango_layout_get_pixel_extents (layout, NULL, &rect);
391 g_object_unref (layout);
392
393 gtk_widget_set_size_request (priv->swatch, rect.width, rect.height);
394
395 gtk_container_add (GTK_CONTAINER (button), priv->swatch);
396 gtk_widget_show (priv->swatch);
397
398 button->priv->title = g_strdup (_("Pick a Color")); /* default title */
399
400 /* Start with opaque black, alpha disabled */
401 priv->rgba.red = 0;
402 priv->rgba.green = 0;
403 priv->rgba.blue = 0;
404 priv->rgba.alpha = 1;
405 priv->use_alpha = FALSE;
406
407 gtk_drag_dest_set (GTK_WIDGET (button),
408 GTK_DEST_DEFAULT_MOTION |
409 GTK_DEST_DEFAULT_HIGHLIGHT |
410 GTK_DEST_DEFAULT_DROP,
411 drop_types, 1, GDK_ACTION_COPY);
412 gtk_drag_source_set (GTK_WIDGET (button),
413 GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
414 drop_types, 1,
415 GDK_ACTION_COPY);
416 g_signal_connect (button, "drag-begin",
417 G_CALLBACK (gtk_color_button_drag_begin), button);
418 g_signal_connect (button, "drag-data-received",
419 G_CALLBACK (gtk_color_button_drag_data_received), button);
420 g_signal_connect (button, "drag-data-get",
421 G_CALLBACK (gtk_color_button_drag_data_get), button);
422
423 context = gtk_widget_get_style_context (GTK_WIDGET (button));
424 gtk_style_context_add_class (context, "color");
425 }
426
427 static void
gtk_color_button_finalize(GObject * object)428 gtk_color_button_finalize (GObject *object)
429 {
430 GtkColorButton *button = GTK_COLOR_BUTTON (object);
431 GtkColorButtonPrivate *priv = button->priv;
432
433 if (priv->cs_dialog != NULL)
434 gtk_widget_destroy (priv->cs_dialog);
435
436 g_free (priv->title);
437
438 G_OBJECT_CLASS (gtk_color_button_parent_class)->finalize (object);
439 }
440
441
442 /**
443 * gtk_color_button_new:
444 *
445 * Creates a new color button.
446 *
447 * This returns a widget in the form of a small button containing
448 * a swatch representing the current selected color. When the button
449 * is clicked, a color-selection dialog will open, allowing the user
450 * to select a color. The swatch will be updated to reflect the new
451 * color when the user finishes.
452 *
453 * Returns: a new color button
454 *
455 * Since: 2.4
456 */
457 GtkWidget *
gtk_color_button_new(void)458 gtk_color_button_new (void)
459 {
460 return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
461 }
462
463 /**
464 * gtk_color_button_new_with_color:
465 * @color: A #GdkColor to set the current color with
466 *
467 * Creates a new color button.
468 *
469 * Returns: a new color button
470 *
471 * Since: 2.4
472 *
473 * Deprecated: 3.4: Use gtk_color_button_new_with_rgba() instead.
474 */
475 GtkWidget *
gtk_color_button_new_with_color(const GdkColor * color)476 gtk_color_button_new_with_color (const GdkColor *color)
477 {
478 return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
479 }
480
481 /**
482 * gtk_color_button_new_with_rgba:
483 * @rgba: A #GdkRGBA to set the current color with
484 *
485 * Creates a new color button.
486 *
487 * Returns: a new color button
488 *
489 * Since: 3.0
490 */
491 GtkWidget *
gtk_color_button_new_with_rgba(const GdkRGBA * rgba)492 gtk_color_button_new_with_rgba (const GdkRGBA *rgba)
493 {
494 return g_object_new (GTK_TYPE_COLOR_BUTTON, "rgba", rgba, NULL);
495 }
496
497 static gboolean
dialog_delete_event(GtkWidget * dialog,GdkEvent * event,gpointer user_data)498 dialog_delete_event (GtkWidget *dialog,
499 GdkEvent *event,
500 gpointer user_data)
501 {
502 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_CANCEL);
503
504 return TRUE;
505 }
506
507 static gboolean
dialog_destroy(GtkWidget * widget,gpointer data)508 dialog_destroy (GtkWidget *widget,
509 gpointer data)
510 {
511 GtkColorButton *button = GTK_COLOR_BUTTON (data);
512
513 button->priv->cs_dialog = NULL;
514
515 return FALSE;
516 }
517
518 static void
dialog_response(GtkDialog * dialog,gint response,gpointer data)519 dialog_response (GtkDialog *dialog,
520 gint response,
521 gpointer data)
522 {
523 if (response == GTK_RESPONSE_CANCEL)
524 gtk_widget_hide (GTK_WIDGET (dialog));
525 else if (response == GTK_RESPONSE_OK)
526 {
527 GtkColorButton *button = GTK_COLOR_BUTTON (data);
528 GtkColorButtonPrivate *priv = button->priv;
529
530 gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &priv->rgba);
531 gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
532
533 gtk_widget_hide (GTK_WIDGET (dialog));
534
535 g_object_ref (button);
536 g_signal_emit (button, color_button_signals[COLOR_SET], 0);
537
538 g_object_freeze_notify (G_OBJECT (button));
539 g_object_notify (G_OBJECT (button), "color");
540 g_object_notify (G_OBJECT (button), "alpha");
541 g_object_notify (G_OBJECT (button), "rgba");
542 g_object_thaw_notify (G_OBJECT (button));
543 g_object_unref (button);
544 }
545 }
546
547 /* Create the dialog and connects its buttons */
548 static void
ensure_dialog(GtkColorButton * button)549 ensure_dialog (GtkColorButton *button)
550 {
551 GtkColorButtonPrivate *priv = button->priv;
552 GtkWidget *parent, *dialog;
553
554 if (priv->cs_dialog != NULL)
555 return;
556
557 parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
558
559 priv->cs_dialog = dialog = gtk_color_chooser_dialog_new (priv->title, NULL);
560
561 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
562 {
563 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dialog)))
564 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
565
566 gtk_window_set_modal (GTK_WINDOW (dialog),
567 gtk_window_get_modal (GTK_WINDOW (parent)));
568 }
569
570 g_signal_connect (dialog, "response",
571 G_CALLBACK (dialog_response), button);
572 g_signal_connect (dialog, "destroy",
573 G_CALLBACK (dialog_destroy), button);
574 g_signal_connect (dialog, "delete-event",
575 G_CALLBACK (dialog_delete_event), button);
576 }
577
578
579 static void
gtk_color_button_clicked(GtkButton * b)580 gtk_color_button_clicked (GtkButton *b)
581 {
582 GtkColorButton *button = GTK_COLOR_BUTTON (b);
583 GtkColorButtonPrivate *priv = button->priv;
584
585 /* if dialog already exists, make sure it's shown and raised */
586 ensure_dialog (button);
587
588 g_object_set (priv->cs_dialog, "show-editor", priv->show_editor, NULL);
589
590 gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (priv->cs_dialog), priv->use_alpha);
591
592 gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (priv->cs_dialog), &priv->rgba);
593
594 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
595 gtk_window_present (GTK_WINDOW (priv->cs_dialog));
596 G_GNUC_END_IGNORE_DEPRECATIONS
597 }
598
599 /**
600 * gtk_color_button_set_color:
601 * @button: a #GtkColorButton
602 * @color: A #GdkColor to set the current color with
603 *
604 * Sets the current color to be @color.
605 *
606 * Since: 2.4
607 *
608 * Deprecated: Use gtk_color_chooser_set_rgba() instead.
609 */
610 void
gtk_color_button_set_color(GtkColorButton * button,const GdkColor * color)611 gtk_color_button_set_color (GtkColorButton *button,
612 const GdkColor *color)
613 {
614 GtkColorButtonPrivate *priv = button->priv;
615
616 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
617 g_return_if_fail (color != NULL);
618
619 priv->rgba.red = color->red / 65535.;
620 priv->rgba.green = color->green / 65535.;
621 priv->rgba.blue = color->blue / 65535.;
622
623 gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
624
625 g_object_notify (G_OBJECT (button), "color");
626 g_object_notify (G_OBJECT (button), "rgba");
627 }
628
629
630 /**
631 * gtk_color_button_set_alpha:
632 * @button: a #GtkColorButton
633 * @alpha: an integer between 0 and 65535
634 *
635 * Sets the current opacity to be @alpha.
636 *
637 * Since: 2.4
638 *
639 * Deprecated: 3.4: Use gtk_color_chooser_set_rgba() instead.
640 */
641 void
gtk_color_button_set_alpha(GtkColorButton * button,guint16 alpha)642 gtk_color_button_set_alpha (GtkColorButton *button,
643 guint16 alpha)
644 {
645 GtkColorButtonPrivate *priv = button->priv;
646
647 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
648
649 priv->rgba.alpha = alpha / 65535.;
650
651 gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
652
653 g_object_notify (G_OBJECT (button), "alpha");
654 g_object_notify (G_OBJECT (button), "rgba");
655 }
656
657 /**
658 * gtk_color_button_get_color:
659 * @button: a #GtkColorButton
660 * @color: (out): a #GdkColor to fill in with the current color
661 *
662 * Sets @color to be the current color in the #GtkColorButton widget.
663 *
664 * Since: 2.4
665 *
666 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
667 */
668 void
gtk_color_button_get_color(GtkColorButton * button,GdkColor * color)669 gtk_color_button_get_color (GtkColorButton *button,
670 GdkColor *color)
671 {
672 GtkColorButtonPrivate *priv = button->priv;
673
674 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
675
676 color->red = (guint16) (priv->rgba.red * 65535);
677 color->green = (guint16) (priv->rgba.green * 65535);
678 color->blue = (guint16) (priv->rgba.blue * 65535);
679 }
680
681 /**
682 * gtk_color_button_get_alpha:
683 * @button: a #GtkColorButton
684 *
685 * Returns the current alpha value.
686 *
687 * Returns: an integer between 0 and 65535
688 *
689 * Since: 2.4
690 *
691 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
692 */
693 guint16
gtk_color_button_get_alpha(GtkColorButton * button)694 gtk_color_button_get_alpha (GtkColorButton *button)
695 {
696 g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), 0);
697
698 return (guint16) (button->priv->rgba.alpha * 65535);
699 }
700
701 /**
702 * gtk_color_button_set_rgba: (skip)
703 * @button: a #GtkColorButton
704 * @rgba: a #GdkRGBA to set the current color with
705 *
706 * Sets the current color to be @rgba.
707 *
708 * Since: 3.0
709 *
710 * Deprecated: 3.4: Use gtk_color_chooser_set_rgba() instead.
711 */
712 void
gtk_color_button_set_rgba(GtkColorButton * button,const GdkRGBA * rgba)713 gtk_color_button_set_rgba (GtkColorButton *button,
714 const GdkRGBA *rgba)
715 {
716 GtkColorButtonPrivate *priv = button->priv;
717
718 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
719 g_return_if_fail (rgba != NULL);
720
721 priv->rgba = *rgba;
722 gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
723
724 g_object_notify (G_OBJECT (button), "color");
725 g_object_notify (G_OBJECT (button), "alpha");
726 g_object_notify (G_OBJECT (button), "rgba");
727 }
728
729 /**
730 * gtk_color_button_get_rgba: (skip)
731 * @button: a #GtkColorButton
732 * @rgba: (out): a #GdkRGBA to fill in with the current color
733 *
734 * Sets @rgba to be the current color in the #GtkColorButton widget.
735 *
736 * Since: 3.0
737 *
738 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
739 */
740 void
gtk_color_button_get_rgba(GtkColorButton * button,GdkRGBA * rgba)741 gtk_color_button_get_rgba (GtkColorButton *button,
742 GdkRGBA *rgba)
743 {
744 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
745 g_return_if_fail (rgba != NULL);
746
747 *rgba = button->priv->rgba;
748 }
749
750 static void
set_use_alpha(GtkColorButton * button,gboolean use_alpha)751 set_use_alpha (GtkColorButton *button,
752 gboolean use_alpha)
753 {
754 GtkColorButtonPrivate *priv = button->priv;
755
756 use_alpha = (use_alpha != FALSE);
757
758 if (priv->use_alpha != use_alpha)
759 {
760 priv->use_alpha = use_alpha;
761
762 gtk_color_swatch_set_use_alpha (GTK_COLOR_SWATCH (priv->swatch), use_alpha);
763
764 g_object_notify (G_OBJECT (button), "use-alpha");
765 }
766 }
767
768 /**
769 * gtk_color_button_set_use_alpha:
770 * @button: a #GtkColorButton
771 * @use_alpha: %TRUE if color button should use alpha channel, %FALSE if not
772 *
773 * Sets whether or not the color button should use the alpha channel.
774 *
775 * Since: 2.4
776 *
777 * Deprecated: 3.4: Use gtk_color_chooser_set_use_alpha() instead.
778 */
779 void
gtk_color_button_set_use_alpha(GtkColorButton * button,gboolean use_alpha)780 gtk_color_button_set_use_alpha (GtkColorButton *button,
781 gboolean use_alpha)
782 {
783 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
784 set_use_alpha (button, use_alpha);
785 }
786
787 /**
788 * gtk_color_button_get_use_alpha:
789 * @button: a #GtkColorButton
790 *
791 * Does the color selection dialog use the alpha channel ?
792 *
793 * Returns: %TRUE if the color sample uses alpha channel, %FALSE if not
794 *
795 * Since: 2.4
796 *
797 * Deprecated: 3.4: Use gtk_color_chooser_get_use_alpha() instead.
798 */
799 gboolean
gtk_color_button_get_use_alpha(GtkColorButton * button)800 gtk_color_button_get_use_alpha (GtkColorButton *button)
801 {
802 g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), FALSE);
803
804 return button->priv->use_alpha;
805 }
806
807
808 /**
809 * gtk_color_button_set_title:
810 * @button: a #GtkColorButton
811 * @title: String containing new window title
812 *
813 * Sets the title for the color selection dialog.
814 *
815 * Since: 2.4
816 */
817 void
gtk_color_button_set_title(GtkColorButton * button,const gchar * title)818 gtk_color_button_set_title (GtkColorButton *button,
819 const gchar *title)
820 {
821 GtkColorButtonPrivate *priv = button->priv;
822 gchar *old_title;
823
824 g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
825
826 old_title = priv->title;
827 priv->title = g_strdup (title);
828 g_free (old_title);
829
830 if (priv->cs_dialog)
831 gtk_window_set_title (GTK_WINDOW (priv->cs_dialog), priv->title);
832
833 g_object_notify (G_OBJECT (button), "title");
834 }
835
836 /**
837 * gtk_color_button_get_title:
838 * @button: a #GtkColorButton
839 *
840 * Gets the title of the color selection dialog.
841 *
842 * Returns: An internal string, do not free the return value
843 *
844 * Since: 2.4
845 */
846 const gchar *
gtk_color_button_get_title(GtkColorButton * button)847 gtk_color_button_get_title (GtkColorButton *button)
848 {
849 g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), NULL);
850
851 return button->priv->title;
852 }
853
854 static void
gtk_color_button_set_property(GObject * object,guint param_id,const GValue * value,GParamSpec * pspec)855 gtk_color_button_set_property (GObject *object,
856 guint param_id,
857 const GValue *value,
858 GParamSpec *pspec)
859 {
860 GtkColorButton *button = GTK_COLOR_BUTTON (object);
861
862 switch (param_id)
863 {
864 case PROP_USE_ALPHA:
865 set_use_alpha (button, g_value_get_boolean (value));
866 break;
867 case PROP_TITLE:
868 gtk_color_button_set_title (button, g_value_get_string (value));
869 break;
870 case PROP_COLOR:
871 {
872 GdkColor *color;
873 GdkRGBA rgba;
874
875 color = g_value_get_boxed (value);
876
877 rgba.red = color->red / 65535.0;
878 rgba.green = color->green / 65535.0;
879 rgba.blue = color->blue / 65535.0;
880 rgba.alpha = 1.0;
881
882 gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), &rgba);
883 }
884 break;
885 case PROP_ALPHA:
886 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
887 gtk_color_button_set_alpha (button, g_value_get_uint (value));
888 G_GNUC_END_IGNORE_DEPRECATIONS
889 break;
890 case PROP_RGBA:
891 gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), g_value_get_boxed (value));
892 break;
893 case PROP_SHOW_EDITOR:
894 {
895 gboolean show_editor = g_value_get_boolean (value);
896 if (button->priv->show_editor != show_editor)
897 {
898 button->priv->show_editor = show_editor;
899 g_object_notify (object, "show-editor");
900 }
901 }
902 break;
903 default:
904 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
905 break;
906 }
907 }
908
909 static void
gtk_color_button_get_property(GObject * object,guint param_id,GValue * value,GParamSpec * pspec)910 gtk_color_button_get_property (GObject *object,
911 guint param_id,
912 GValue *value,
913 GParamSpec *pspec)
914 {
915 GtkColorButton *button = GTK_COLOR_BUTTON (object);
916
917 switch (param_id)
918 {
919 case PROP_USE_ALPHA:
920 g_value_set_boolean (value, button->priv->use_alpha);
921 break;
922 case PROP_TITLE:
923 g_value_set_string (value, gtk_color_button_get_title (button));
924 break;
925 case PROP_COLOR:
926 {
927 GdkColor color;
928 GdkRGBA rgba;
929
930 gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &rgba);
931
932 color.red = (guint16) (rgba.red * 65535 + 0.5);
933 color.green = (guint16) (rgba.green * 65535 + 0.5);
934 color.blue = (guint16) (rgba.blue * 65535 + 0.5);
935
936 g_value_set_boxed (value, &color);
937 }
938 break;
939 case PROP_ALPHA:
940 {
941 guint16 alpha;
942
943 alpha = (guint16) (button->priv->rgba.alpha * 65535 + 0.5);
944
945 g_value_set_uint (value, alpha);
946 }
947 break;
948 case PROP_RGBA:
949 {
950 GdkRGBA rgba;
951
952 gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &rgba);
953 g_value_set_boxed (value, &rgba);
954 }
955 break;
956 case PROP_SHOW_EDITOR:
957 g_value_set_boolean (value, button->priv->show_editor);
958 break;
959 default:
960 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
961 break;
962 }
963 }
964
965 static void
gtk_color_button_add_palette(GtkColorChooser * chooser,GtkOrientation orientation,gint colors_per_line,gint n_colors,GdkRGBA * colors)966 gtk_color_button_add_palette (GtkColorChooser *chooser,
967 GtkOrientation orientation,
968 gint colors_per_line,
969 gint n_colors,
970 GdkRGBA *colors)
971 {
972 GtkColorButton *button = GTK_COLOR_BUTTON (chooser);
973
974 ensure_dialog (button);
975
976 gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (button->priv->cs_dialog),
977 orientation, colors_per_line, n_colors, colors);
978 }
979
980 typedef void (* get_rgba) (GtkColorChooser *, GdkRGBA *);
981 typedef void (* set_rgba) (GtkColorChooser *, const GdkRGBA *);
982
983 static void
gtk_color_button_iface_init(GtkColorChooserInterface * iface)984 gtk_color_button_iface_init (GtkColorChooserInterface *iface)
985 {
986 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
987 iface->get_rgba = (get_rgba)gtk_color_button_get_rgba;
988 iface->set_rgba = (set_rgba)gtk_color_button_set_rgba;
989 G_GNUC_END_IGNORE_DEPRECATIONS
990 iface->add_palette = gtk_color_button_add_palette;
991 }
992
993