1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpcolorhistory.c
5 * Copyright (C) 2015 Jehan <jehan@girinstud.io>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <gegl.h>
24 #include <gtk/gtk.h>
25
26 #include "libgimpwidgets/gimpwidgets.h"
27
28 #include "widgets-types.h"
29
30 #include "config/gimpcoreconfig.h"
31
32 #include "core/gimp.h"
33 #include "core/gimp-palettes.h"
34 #include "core/gimpcontext.h"
35 #include "core/gimpmarshal.h"
36 #include "core/gimppalettemru.h"
37
38 #include "gimpcolorhistory.h"
39
40 #include "gimp-intl.h"
41
42 enum
43 {
44 COLOR_SELECTED,
45 LAST_SIGNAL
46 };
47
48 enum
49 {
50 PROP_0,
51 PROP_CONTEXT,
52 PROP_HISTORY_SIZE,
53 };
54
55
56 #define DEFAULT_HISTORY_SIZE 12
57 #define COLOR_AREA_SIZE 20
58
59 static void gimp_color_history_constructed (GObject *object);
60 static void gimp_color_history_finalize (GObject *object);
61 static void gimp_color_history_set_property (GObject *object,
62 guint property_id,
63 const GValue *value,
64 GParamSpec *pspec);
65 static void gimp_color_history_get_property (GObject *object,
66 guint property_id,
67 GValue *value,
68 GParamSpec *pspec);
69
70 static void gimp_color_history_color_clicked (GtkWidget *widget,
71 GimpColorHistory *history);
72
73 static void gimp_color_history_palette_dirty (GimpPalette *palette,
74 GimpColorHistory *history);
75
76 static void gimp_color_history_color_changed (GtkWidget *widget,
77 gpointer data);
78
79
80 G_DEFINE_TYPE (GimpColorHistory, gimp_color_history, GTK_TYPE_TABLE)
81
82 #define parent_class gimp_color_history_parent_class
83
84 static guint history_signals[LAST_SIGNAL] = { 0 };
85
86 static void
gimp_color_history_class_init(GimpColorHistoryClass * klass)87 gimp_color_history_class_init (GimpColorHistoryClass *klass)
88 {
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
90
91 object_class->constructed = gimp_color_history_constructed;
92 object_class->set_property = gimp_color_history_set_property;
93 object_class->get_property = gimp_color_history_get_property;
94 object_class->finalize = gimp_color_history_finalize;
95
96 history_signals[COLOR_SELECTED] =
97 g_signal_new ("color-selected",
98 G_TYPE_FROM_CLASS (klass),
99 G_SIGNAL_RUN_FIRST,
100 G_STRUCT_OFFSET (GimpColorHistoryClass, color_selected),
101 NULL, NULL,
102 gimp_marshal_VOID__POINTER,
103 G_TYPE_NONE, 1,
104 G_TYPE_POINTER);
105
106 g_object_class_install_property (object_class, PROP_CONTEXT,
107 g_param_spec_object ("context", NULL, NULL,
108 GIMP_TYPE_CONTEXT,
109 GIMP_PARAM_READWRITE |
110 G_PARAM_CONSTRUCT));
111 g_object_class_install_property (object_class, PROP_HISTORY_SIZE,
112 g_param_spec_int ("history-size",
113 NULL, NULL,
114 2, G_MAXINT,
115 DEFAULT_HISTORY_SIZE,
116 GIMP_PARAM_READWRITE |
117 G_PARAM_CONSTRUCT));
118
119 klass->color_selected = NULL;
120 }
121
122 static void
gimp_color_history_init(GimpColorHistory * history)123 gimp_color_history_init (GimpColorHistory *history)
124 {
125 history->color_areas = NULL;
126 }
127
128 static void
gimp_color_history_constructed(GObject * object)129 gimp_color_history_constructed (GObject *object)
130 {
131 GimpColorHistory *history = GIMP_COLOR_HISTORY (object);
132 GimpPalette *palette;
133
134 G_OBJECT_CLASS (parent_class)->constructed (object);
135
136 palette = gimp_palettes_get_color_history (history->context->gimp);
137
138 g_signal_connect_object (palette, "dirty",
139 G_CALLBACK (gimp_color_history_palette_dirty),
140 G_OBJECT (history), 0);
141
142 gimp_color_history_palette_dirty (palette, history);
143 }
144
145 static void
gimp_color_history_finalize(GObject * object)146 gimp_color_history_finalize (GObject *object)
147 {
148 GimpColorHistory *history = GIMP_COLOR_HISTORY (object);
149
150 g_clear_pointer (&history->color_areas, g_free);
151
152 G_OBJECT_CLASS (parent_class)->finalize (object);
153 }
154
155 static void
gimp_color_history_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)156 gimp_color_history_set_property (GObject *object,
157 guint property_id,
158 const GValue *value,
159 GParamSpec *pspec)
160 {
161 GimpColorHistory *history = GIMP_COLOR_HISTORY (object);
162
163 switch (property_id)
164 {
165 case PROP_CONTEXT:
166 history->context = g_value_get_object (value);
167 break;
168 case PROP_HISTORY_SIZE:
169 {
170 GtkWidget *button;
171 gint i;
172
173 /* Destroy previous color buttons. */
174 gtk_container_foreach (GTK_CONTAINER (history),
175 (GtkCallback) gtk_widget_destroy, NULL);
176 history->history_size = g_value_get_int (value);
177 gtk_table_resize (GTK_TABLE (history),
178 2, (history->history_size + 1)/ 2);
179 gtk_table_set_row_spacings (GTK_TABLE (history), 2);
180 gtk_table_set_col_spacings (GTK_TABLE (history), 2);
181 history->color_areas = g_realloc_n (history->color_areas,
182 history->history_size,
183 sizeof (GtkWidget*));
184 for (i = 0; i < history->history_size; i++)
185 {
186 GimpRGB black = { 0.0, 0.0, 0.0, 1.0 };
187 gint row, column;
188
189 column = i % (history->history_size / 2);
190 row = i / (history->history_size / 2);
191
192 button = gtk_button_new ();
193 gtk_widget_set_size_request (button, COLOR_AREA_SIZE, COLOR_AREA_SIZE);
194 gtk_table_attach_defaults (GTK_TABLE (history), button,
195 column, column + 1, row, row + 1);
196 gtk_widget_show (button);
197
198 history->color_areas[i] = gimp_color_area_new (&black,
199 GIMP_COLOR_AREA_SMALL_CHECKS,
200 GDK_BUTTON2_MASK);
201 gimp_color_area_set_color_config (GIMP_COLOR_AREA (history->color_areas[i]),
202 history->context->gimp->config->color_management);
203 gtk_container_add (GTK_CONTAINER (button), history->color_areas[i]);
204 gtk_widget_show (history->color_areas[i]);
205
206 g_signal_connect (button, "clicked",
207 G_CALLBACK (gimp_color_history_color_clicked),
208 history);
209
210 g_signal_connect (history->color_areas[i], "color-changed",
211 G_CALLBACK (gimp_color_history_color_changed),
212 GINT_TO_POINTER (i));
213 }
214 }
215 break;
216
217 default:
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
219 break;
220 }
221 }
222
223 static void
gimp_color_history_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)224 gimp_color_history_get_property (GObject *object,
225 guint property_id,
226 GValue *value,
227 GParamSpec *pspec)
228 {
229 GimpColorHistory *history = GIMP_COLOR_HISTORY (object);
230
231 switch (property_id)
232 {
233 case PROP_CONTEXT:
234 g_value_set_object (value, history->context);
235 break;
236 case PROP_HISTORY_SIZE:
237 g_value_set_int (value, history->history_size);
238 break;
239
240 default:
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
242 break;
243 }
244 }
245
246 /* Public Functions */
247
248 GtkWidget *
gimp_color_history_new(GimpContext * context,gint history_size)249 gimp_color_history_new (GimpContext *context,
250 gint history_size)
251 {
252 GimpColorHistory *history;
253
254 g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
255
256 history = g_object_new (GIMP_TYPE_COLOR_HISTORY,
257 "context", context,
258 "history-size", history_size,
259 NULL);
260
261 return GTK_WIDGET (history);
262 }
263
264 /* Color history callback. */
265
266 static void
gimp_color_history_color_clicked(GtkWidget * widget,GimpColorHistory * history)267 gimp_color_history_color_clicked (GtkWidget *widget,
268 GimpColorHistory *history)
269 {
270 GimpColorArea *color_area;
271 GimpRGB color;
272
273 color_area = GIMP_COLOR_AREA (gtk_bin_get_child (GTK_BIN (widget)));
274
275 gimp_color_area_get_color (color_area, &color);
276
277 g_signal_emit (history, history_signals[COLOR_SELECTED], 0,
278 &color);
279 }
280
281 /* Color history palette callback. */
282
283 static void
gimp_color_history_palette_dirty(GimpPalette * palette,GimpColorHistory * history)284 gimp_color_history_palette_dirty (GimpPalette *palette,
285 GimpColorHistory *history)
286 {
287 gint i;
288
289 for (i = 0; i < history->history_size; i++)
290 {
291 GimpPaletteEntry *entry = gimp_palette_get_entry (palette, i);
292 GimpRGB black = { 0.0, 0.0, 0.0, 1.0 };
293
294 g_signal_handlers_block_by_func (history->color_areas[i],
295 gimp_color_history_color_changed,
296 GINT_TO_POINTER (i));
297
298 gimp_color_area_set_color (GIMP_COLOR_AREA (history->color_areas[i]),
299 entry ? &entry->color : &black);
300
301 g_signal_handlers_unblock_by_func (history->color_areas[i],
302 gimp_color_history_color_changed,
303 GINT_TO_POINTER (i));
304 }
305 }
306
307 /* Color area callbacks. */
308
309 static void
gimp_color_history_color_changed(GtkWidget * widget,gpointer data)310 gimp_color_history_color_changed (GtkWidget *widget,
311 gpointer data)
312 {
313 GimpColorHistory *history;
314 GimpPalette *palette;
315 GimpRGB color;
316
317 history = GIMP_COLOR_HISTORY (gtk_widget_get_ancestor (widget,
318 GIMP_TYPE_COLOR_HISTORY));
319
320 palette = gimp_palettes_get_color_history (history->context->gimp);
321
322 gimp_color_area_get_color (GIMP_COLOR_AREA (widget), &color);
323
324 gimp_palette_set_entry_color (palette, GPOINTER_TO_INT (data), &color);
325 }
326