1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
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
344
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
|
/*
* GTK - The GIMP Toolkit
* Copyright (C) 2022 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkcolordialogbutton.h"
#include "gtkbinlayout.h"
#include "gtkbutton.h"
#include "gtkcolorswatchprivate.h"
#include "gtkdragsource.h"
#include "gtkdroptarget.h"
#include <glib/gi18n-lib.h>
#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtksnapshot.h"
#include "gtkwidgetprivate.h"
#include "gdk/gdkrgbaprivate.h"
static gboolean drop (GtkDropTarget *dest,
const GValue *value,
double x,
double y,
GtkColorDialogButton *self);
static GdkContentProvider *
drag_prepare (GtkDragSource *source,
double x,
double y,
GtkColorDialogButton *self);
static void button_clicked (GtkColorDialogButton *self);
static void update_button_sensitivity
(GtkColorDialogButton *self);
/**
* GtkColorDialogButton:
*
* The `GtkColorDialogButton` is a wrapped around a [class@Gtk.ColorDialog]
* and allows to open a color chooser dialog to change the color.
*
* ![An example GtkColorDialogButton](color-button.png)
*
* It is suitable widget for selecting a color in a preference dialog.
*
* # CSS nodes
*
* ```
* colorbutton
* ╰── button.color
* ╰── [content]
* ```
*
* `GtkColorDialogButton` has a single CSS node with name colorbutton which
* contains a button node. To differentiate it from a plain `GtkButton`,
* it gets the .color style class.
*
* Since: 4.10
*/
/* {{{ GObject implementation */
struct _GtkColorDialogButton
{
GtkWidget parent_instance;
GtkWidget *button;
GtkWidget *swatch;
GtkColorDialog *dialog;
GCancellable *cancellable;
GdkRGBA color;
};
/* Properties */
enum
{
PROP_DIALOG = 1,
PROP_RGBA,
NUM_PROPERTIES
};
static GParamSpec *properties[NUM_PROPERTIES];
G_DEFINE_TYPE (GtkColorDialogButton, gtk_color_dialog_button, GTK_TYPE_WIDGET)
static void
gtk_color_dialog_button_init (GtkColorDialogButton *self)
{
PangoLayout *layout;
PangoRectangle rect;
GtkDragSource *source;
GtkDropTarget *dest;
self->color = GDK_RGBA ("00000000");
self->button = gtk_button_new ();
g_signal_connect_swapped (self->button, "clicked", G_CALLBACK (button_clicked), self);
gtk_widget_set_parent (self->button, GTK_WIDGET (self));
self->swatch = g_object_new (GTK_TYPE_COLOR_SWATCH,
"accessible-role", GTK_ACCESSIBLE_ROLE_IMG,
"selectable", FALSE,
"has-menu", FALSE,
"can-drag", FALSE,
NULL);
gtk_widget_set_can_focus (self->swatch, FALSE);
gtk_widget_remove_css_class (self->swatch, "activatable");
layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), "Black");
pango_layout_get_pixel_extents (layout, NULL, &rect);
g_object_unref (layout);
gtk_widget_set_size_request (self->swatch, rect.width, rect.height);
gtk_button_set_child (GTK_BUTTON (self->button), self->swatch);
dest = gtk_drop_target_new (GDK_TYPE_RGBA, GDK_ACTION_COPY);
g_signal_connect (dest, "drop", G_CALLBACK (drop), self);
gtk_widget_add_controller (GTK_WIDGET (self->button), GTK_EVENT_CONTROLLER (dest));
source = gtk_drag_source_new ();
g_signal_connect (source, "prepare", G_CALLBACK (drag_prepare), self);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (source),
GTK_PHASE_CAPTURE);
gtk_widget_add_controller (self->button, GTK_EVENT_CONTROLLER (source));
gtk_widget_add_css_class (self->button, "color");
}
static void
gtk_color_dialog_button_unroot (GtkWidget *widget)
{
GtkColorDialogButton *self = GTK_COLOR_DIALOG_BUTTON (widget);
if (self->cancellable)
{
g_cancellable_cancel (self->cancellable);
g_clear_object (&self->cancellable);
update_button_sensitivity (self);
}
GTK_WIDGET_CLASS (gtk_color_dialog_button_parent_class)->unroot (widget);
}
static void
gtk_color_dialog_button_set_property (GObject *object,
unsigned int param_id,
const GValue *value,
GParamSpec *pspec)
{
GtkColorDialogButton *self = GTK_COLOR_DIALOG_BUTTON (object);
switch (param_id)
{
case PROP_DIALOG:
gtk_color_dialog_button_set_dialog (self, g_value_get_object (value));
break;
case PROP_RGBA:
gtk_color_dialog_button_set_rgba (self, g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gtk_color_dialog_button_get_property (GObject *object,
unsigned int param_id,
GValue *value,
GParamSpec *pspec)
{
GtkColorDialogButton *self = GTK_COLOR_DIALOG_BUTTON (object);
switch (param_id)
{
case PROP_DIALOG:
g_value_set_object (value, self->dialog);
break;
case PROP_RGBA:
g_value_set_boxed (value, &self->color);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gtk_color_dialog_button_dispose (GObject *object)
{
GtkColorDialogButton *self = GTK_COLOR_DIALOG_BUTTON (object);
g_clear_pointer (&self->button, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_color_dialog_button_parent_class)->dispose (object);
}
static void
gtk_color_dialog_button_finalize (GObject *object)
{
GtkColorDialogButton *self = GTK_COLOR_DIALOG_BUTTON (object);
g_assert (self->cancellable == NULL);
g_clear_object (&self->dialog);
G_OBJECT_CLASS (gtk_color_dialog_button_parent_class)->finalize (object);
}
static void
gtk_color_dialog_button_class_init (GtkColorDialogButtonClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
object_class->get_property = gtk_color_dialog_button_get_property;
object_class->set_property = gtk_color_dialog_button_set_property;
object_class->dispose = gtk_color_dialog_button_dispose;
object_class->finalize = gtk_color_dialog_button_finalize;
widget_class->grab_focus = gtk_widget_grab_focus_child;
widget_class->focus = gtk_widget_focus_child;
widget_class->unroot = gtk_color_dialog_button_unroot;
/**
* GtkColorDialogButton:dialog: (attributes org.gtk.Property.get=gtk_color_dialog_button_get_dialog org.gtk.Property.set=gtk_color_dialog_button_set_dialog)
*
* The `GtkColorDialog` that contains parameters for
* the color chooser dialog.
*
* Since: 4.10
*/
properties[PROP_DIALOG] =
g_param_spec_object ("dialog", NULL, NULL,
GTK_TYPE_COLOR_DIALOG,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkColorDialogButton:rgba: (attributes org.gtk.Property.get=gtk_color_dialog_button_get_rgba org.gtk.Property.set=gtk_color_dialog_button_set_rgba)
*
* The selected color.
*
* This property can be set to give the button its initial
* color, and it will be updated to reflect the users choice
* in the color chooser dialog.
*
* Listen to `notify::rgba` to get informed about changes
* to the buttons color.
*
* Since: 4.10
*/
properties[PROP_RGBA] =
g_param_spec_boxed ("rgba", NULL, NULL,
GDK_TYPE_RGBA,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
gtk_widget_class_set_css_name (widget_class, "colorbutton");
}
/* }}} */
/* {{{ Private API, callbacks */
static guint
scale_round (double value,
double scale)
{
value = floor (value * scale + 0.5);
value = CLAMP (value, 0, scale);
return (guint)value;
}
static char *
accessible_color_name (const GdkRGBA *color)
{
if (color->alpha < 1.0)
return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%, Alpha %d%%"),
scale_round (color->red, 100),
scale_round (color->green, 100),
scale_round (color->blue, 100),
scale_round (color->alpha, 100));
else
return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%"),
scale_round (color->red, 100),
scale_round (color->green, 100),
scale_round (color->blue, 100));
}
static gboolean
drop (GtkDropTarget *dest,
const GValue *value,
double x,
double y,
GtkColorDialogButton *self)
{
GdkRGBA *color = g_value_get_boxed (value);
gtk_color_dialog_button_set_rgba (self, color);
return TRUE;
}
static GdkContentProvider *
drag_prepare (GtkDragSource *source,
double x,
double y,
GtkColorDialogButton *self)
{
GdkRGBA color;
gtk_color_swatch_get_rgba (GTK_COLOR_SWATCH (self->swatch), &color);
return gdk_content_provider_new_typed (GDK_TYPE_RGBA, &color);
}
static void
update_button_sensitivity (GtkColorDialogButton *self)
{
if (self->button)
gtk_widget_set_sensitive (self->button,
self->dialog != NULL && self->cancellable == NULL);
}
static void
color_chosen (GObject *source,
GAsyncResult *result,
gpointer data)
{
GtkColorDialog *dialog = GTK_COLOR_DIALOG (source);
GtkColorDialogButton *self = data;
GdkRGBA *color;
color = gtk_color_dialog_choose_rgba_finish (dialog, result, NULL);
if (color)
{
gtk_color_dialog_button_set_rgba (self, color);
gdk_rgba_free (color);
}
g_clear_object (&self->cancellable);
update_button_sensitivity (self);
}
static void
button_clicked (GtkColorDialogButton *self)
{
GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (self));
GtkWindow *parent = NULL;
g_assert (self->cancellable == NULL);
self->cancellable = g_cancellable_new ();
update_button_sensitivity (self);
if (GTK_IS_WINDOW (root))
parent = GTK_WINDOW (root);
gtk_color_dialog_choose_rgba (self->dialog, parent, &self->color,
self->cancellable, color_chosen, self);
}
/* }}} */
/* {{{ Constructor */
/**
* gtk_color_dialog_button_new:
* @dialog: (nullable) (transfer full): the `GtkColorDialog` to use
*
* Creates a new `GtkColorDialogButton` with the
* given `GtkColorDialog`.
*
* You can pass `NULL` to this function and set a `GtkColorDialog`
* later. The button will be insensitive until that happens.
*
* Returns: the new `GtkColorDialogButton`
*
* Since: 4.10
*/
GtkWidget *
gtk_color_dialog_button_new (GtkColorDialog *dialog)
{
GtkWidget *self;
g_return_val_if_fail (dialog == NULL || GTK_IS_COLOR_DIALOG (dialog), NULL);
self = g_object_new (GTK_TYPE_COLOR_DIALOG_BUTTON,
"dialog", dialog,
NULL);
g_clear_object (&dialog);
return self;
}
/* }}} */
/* {{{ Getters and setters */
/**
* gtk_color_dialog_button_get_dialog:
* @self: a `GtkColorDialogButton`
*
* Returns the `GtkColorDialog` of @self.
*
* Returns: (transfer none) (nullable): the `GtkColorDialog`
*
* Since: 4.10
*/
GtkColorDialog *
gtk_color_dialog_button_get_dialog (GtkColorDialogButton *self)
{
g_return_val_if_fail (GTK_IS_COLOR_DIALOG_BUTTON (self), NULL);
return self->dialog;
}
/**
* gtk_color_dialog_button_set_dialog:
* @self: a `GtkColorDialogButton`
* @dialog: the new `GtkColorDialog`
*
* Sets a `GtkColorDialog` object to use for
* creating the color chooser dialog that is
* presented when the user clicks the button.
*
* Since: 4.10
*/
void
gtk_color_dialog_button_set_dialog (GtkColorDialogButton *self,
GtkColorDialog *dialog)
{
g_return_if_fail (GTK_IS_COLOR_DIALOG_BUTTON (self));
g_return_if_fail (dialog == NULL || GTK_IS_COLOR_DIALOG (dialog));
if (!g_set_object (&self->dialog, dialog))
return;
update_button_sensitivity (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DIALOG]);
}
/**
* gtk_color_dialog_button_get_rgba:
* @self: a `GtkColorDialogButton`
*
* Returns the color of the button.
*
* This function is what should be used to obtain
* the color that was chosen by the user. To get
* informed about changes, listen to "notify::color".
*
* Returns: the color
*
* Since: 4.10
*/
const GdkRGBA *
gtk_color_dialog_button_get_rgba (GtkColorDialogButton *self)
{
g_return_val_if_fail (GTK_IS_COLOR_DIALOG_BUTTON (self), NULL);
return &self->color;
}
/**
* gtk_color_dialog_button_set_rgba:
* @self: a `GtkColorDialogButton`
* @color: the new color
*
* Sets the color of the button.
*
* Since: 4.10
*/
void
gtk_color_dialog_button_set_rgba (GtkColorDialogButton *self,
const GdkRGBA *color)
{
char *text;
g_return_if_fail (GTK_IS_COLOR_DIALOG_BUTTON (self));
g_return_if_fail (color != NULL);
if (gdk_rgba_equal (&self->color, color))
return;
self->color = *color;
gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (self->swatch), color);
text = accessible_color_name (color);
gtk_accessible_update_property (GTK_ACCESSIBLE (self->swatch),
GTK_ACCESSIBLE_PROPERTY_LABEL, text,
-1);
g_free (text);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_RGBA]);
}
/* }}} */
/* vim:set foldmethod=marker expandtab: */
|