1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 *
4 * gimpcolorselectorpalette.c
5 * Copyright (C) 2006 Michael Natterer <mitch@gimp.org>
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 "libgimpcolor/gimpcolor.h"
27 #include "libgimpwidgets/gimpwidgets.h"
28
29 #include "widgets-types.h"
30
31 #include "core/gimpcontext.h"
32 #include "core/gimppalette.h"
33
34 #include "gimpcolorselectorpalette.h"
35 #include "gimppaletteview.h"
36 #include "gimpviewrendererpalette.h"
37
38 #include "gimp-intl.h"
39
40
41 static void gimp_color_selector_palette_set_color (GimpColorSelector *selector,
42 const GimpRGB *rgb,
43 const GimpHSV *hsv);
44 static void gimp_color_selector_palette_set_config (GimpColorSelector *selector,
45 GimpColorConfig *config);
46
47
G_DEFINE_TYPE(GimpColorSelectorPalette,gimp_color_selector_palette,GIMP_TYPE_COLOR_SELECTOR)48 G_DEFINE_TYPE (GimpColorSelectorPalette, gimp_color_selector_palette,
49 GIMP_TYPE_COLOR_SELECTOR)
50
51 #define parent_class gimp_color_selector_palette_parent_class
52
53
54 static void
55 gimp_color_selector_palette_class_init (GimpColorSelectorPaletteClass *klass)
56 {
57 GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
58
59 selector_class->name = _("Palette");
60 selector_class->help_id = "gimp-colorselector-palette";
61 selector_class->icon_name = GIMP_ICON_PALETTE;
62 selector_class->set_color = gimp_color_selector_palette_set_color;
63 selector_class->set_config = gimp_color_selector_palette_set_config;
64 }
65
66 static void
gimp_color_selector_palette_init(GimpColorSelectorPalette * select)67 gimp_color_selector_palette_init (GimpColorSelectorPalette *select)
68 {
69 }
70
71 static void
gimp_color_selector_palette_set_color(GimpColorSelector * selector,const GimpRGB * rgb,const GimpHSV * hsv)72 gimp_color_selector_palette_set_color (GimpColorSelector *selector,
73 const GimpRGB *rgb,
74 const GimpHSV *hsv)
75 {
76 GimpColorSelectorPalette *select = GIMP_COLOR_SELECTOR_PALETTE (selector);
77
78 if (select->context)
79 {
80 GimpPalette *palette = gimp_context_get_palette (select->context);
81
82 if (palette && gimp_palette_get_n_colors (palette) > 0)
83 {
84 GimpPaletteEntry *entry;
85
86 entry = gimp_palette_find_entry (palette, rgb,
87 GIMP_PALETTE_VIEW (select->view)->selected);
88
89 if (entry)
90 gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (select->view),
91 entry);
92 }
93 }
94 }
95
96 static void
gimp_color_selector_palette_palette_changed(GimpContext * context,GimpPalette * palette,GimpColorSelectorPalette * select)97 gimp_color_selector_palette_palette_changed (GimpContext *context,
98 GimpPalette *palette,
99 GimpColorSelectorPalette *select)
100 {
101 gimp_view_set_viewable (GIMP_VIEW (select->view), GIMP_VIEWABLE (palette));
102 }
103
104 static void
gimp_color_selector_palette_entry_clicked(GimpPaletteView * view,GimpPaletteEntry * entry,GdkModifierType state,GimpColorSelector * selector)105 gimp_color_selector_palette_entry_clicked (GimpPaletteView *view,
106 GimpPaletteEntry *entry,
107 GdkModifierType state,
108 GimpColorSelector *selector)
109 {
110 selector->rgb = entry->color;
111 gimp_rgb_to_hsv (&selector->rgb, &selector->hsv);
112
113 gimp_color_selector_color_changed (selector);
114 }
115
116 static void
gimp_color_selector_palette_set_config(GimpColorSelector * selector,GimpColorConfig * config)117 gimp_color_selector_palette_set_config (GimpColorSelector *selector,
118 GimpColorConfig *config)
119 {
120 GimpColorSelectorPalette *select = GIMP_COLOR_SELECTOR_PALETTE (selector);
121
122 if (select->context)
123 {
124 g_signal_handlers_disconnect_by_func (select->context,
125 gimp_color_selector_palette_palette_changed,
126 select);
127 gimp_view_renderer_set_context (GIMP_VIEW (select->view)->renderer,
128 NULL);
129
130 g_clear_object (&select->context);
131 }
132
133 if (config)
134 select->context = g_object_get_data (G_OBJECT (config), "gimp-context");
135
136 if (select->context)
137 {
138 g_object_ref (select->context);
139
140 if (! select->view)
141 {
142 GtkWidget *frame;
143
144 frame = gtk_frame_new (NULL);
145 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
146 gtk_box_pack_start (GTK_BOX (select), frame, TRUE, TRUE, 0);
147 gtk_widget_show (frame);
148
149 select->view = gimp_view_new_full_by_types (select->context,
150 GIMP_TYPE_PALETTE_VIEW,
151 GIMP_TYPE_PALETTE,
152 100, 100, 0,
153 FALSE, TRUE, FALSE);
154 gimp_view_set_expand (GIMP_VIEW (select->view), TRUE);
155 gimp_view_renderer_palette_set_cell_size
156 (GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (select->view)->renderer),
157 -1);
158 gimp_view_renderer_palette_set_draw_grid
159 (GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (select->view)->renderer),
160 TRUE);
161 gtk_container_add (GTK_CONTAINER (frame), select->view);
162 gtk_widget_show (select->view);
163
164 g_signal_connect (select->view, "entry-clicked",
165 G_CALLBACK (gimp_color_selector_palette_entry_clicked),
166 select);
167 }
168 else
169 {
170 gimp_view_renderer_set_context (GIMP_VIEW (select->view)->renderer,
171 select->context);
172 }
173
174 g_signal_connect_object (select->context, "palette-changed",
175 G_CALLBACK (gimp_color_selector_palette_palette_changed),
176 select, 0);
177
178 gimp_color_selector_palette_palette_changed (select->context,
179 gimp_context_get_palette (select->context),
180 select);
181 }
182 }
183