1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpcoloreditor.c
5  * Copyright (C) 2002 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 <string.h>
24 
25 #include <gegl.h>
26 #include <gtk/gtk.h>
27 
28 #include "libgimpcolor/gimpcolor.h"
29 #include "libgimpwidgets/gimpwidgets.h"
30 
31 #include "widgets-types.h"
32 
33 #include "config/gimpcoreconfig.h"
34 
35 #include "core/gimp.h"
36 #include "core/gimpcontext.h"
37 
38 #include "gimpcoloreditor.h"
39 #include "gimpcolorhistory.h"
40 #include "gimpdocked.h"
41 #include "gimpfgbgeditor.h"
42 #include "gimpfgbgview.h"
43 #include "gimpsessioninfo-aux.h"
44 
45 #include "gimp-intl.h"
46 
47 
48 enum
49 {
50   PROP_0,
51   PROP_CONTEXT
52 };
53 
54 
55 static void   gimp_color_editor_docked_iface_init (GimpDockedInterface  *iface);
56 
57 static void   gimp_color_editor_constructed     (GObject           *object);
58 static void   gimp_color_editor_dispose         (GObject           *object);
59 static void   gimp_color_editor_set_property    (GObject           *object,
60                                                  guint              property_id,
61                                                  const GValue      *value,
62                                                  GParamSpec        *pspec);
63 static void   gimp_color_editor_get_property    (GObject           *object,
64                                                  guint              property_id,
65                                                  GValue            *value,
66                                                  GParamSpec        *pspec);
67 
68 static void   gimp_color_editor_style_set       (GtkWidget         *widget,
69                                                  GtkStyle          *prev_style);
70 
71 static void   gimp_color_editor_set_aux_info    (GimpDocked        *docked,
72                                                  GList             *aux_info);
73 static GList *gimp_color_editor_get_aux_info     (GimpDocked       *docked);
74 static GtkWidget *gimp_color_editor_get_preview (GimpDocked        *docked,
75                                                  GimpContext       *context,
76                                                  GtkIconSize        size);
77 static void   gimp_color_editor_set_context     (GimpDocked        *docked,
78                                                  GimpContext       *context);
79 
80 static void   gimp_color_editor_fg_changed      (GimpContext       *context,
81                                                  const GimpRGB     *rgb,
82                                                  GimpColorEditor   *editor);
83 static void   gimp_color_editor_bg_changed      (GimpContext       *context,
84                                                  const GimpRGB     *rgb,
85                                                  GimpColorEditor   *editor);
86 static void   gimp_color_editor_color_changed   (GimpColorSelector *selector,
87                                                  const GimpRGB     *rgb,
88                                                  const GimpHSV     *hsv,
89                                                  GimpColorEditor   *editor);
90 static void   gimp_color_editor_tab_toggled     (GtkWidget         *widget,
91                                                  GimpColorEditor   *editor);
92 static void   gimp_color_editor_fg_bg_notify    (GtkWidget         *widget,
93                                                  GParamSpec        *pspec,
94                                                  GimpColorEditor   *editor);
95 static void   gimp_color_editor_color_picked    (GtkWidget         *widget,
96                                                  const GimpRGB     *rgb,
97                                                  GimpColorEditor   *editor);
98 static void   gimp_color_editor_entry_changed   (GimpColorHexEntry *entry,
99                                                  GimpColorEditor   *editor);
100 
101 static void  gimp_color_editor_history_selected (GimpColorHistory *history,
102                                                  const GimpRGB    *rgb,
103                                                  GimpColorEditor  *editor);
104 
105 G_DEFINE_TYPE_WITH_CODE (GimpColorEditor, gimp_color_editor, GIMP_TYPE_EDITOR,
106                          G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,
107                                                 gimp_color_editor_docked_iface_init))
108 
109 #define parent_class gimp_color_editor_parent_class
110 
111 static GimpDockedInterface *parent_docked_iface = NULL;
112 
113 
114 static void
gimp_color_editor_class_init(GimpColorEditorClass * klass)115 gimp_color_editor_class_init (GimpColorEditorClass* klass)
116 {
117   GObjectClass   *object_class = G_OBJECT_CLASS (klass);
118   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
119 
120   object_class->constructed  = gimp_color_editor_constructed;
121   object_class->dispose      = gimp_color_editor_dispose;
122   object_class->set_property = gimp_color_editor_set_property;
123   object_class->get_property = gimp_color_editor_get_property;
124 
125   widget_class->style_set    = gimp_color_editor_style_set;
126 
127   g_object_class_install_property (object_class, PROP_CONTEXT,
128                                    g_param_spec_object ("context",
129                                                         NULL, NULL,
130                                                         GIMP_TYPE_CONTEXT,
131                                                         G_PARAM_CONSTRUCT |
132                                                         GIMP_PARAM_READWRITE));
133 }
134 
135 static void
gimp_color_editor_docked_iface_init(GimpDockedInterface * iface)136 gimp_color_editor_docked_iface_init (GimpDockedInterface *iface)
137 {
138   parent_docked_iface = g_type_interface_peek_parent (iface);
139 
140   if (! parent_docked_iface)
141     parent_docked_iface = g_type_default_interface_peek (GIMP_TYPE_DOCKED);
142 
143   iface->get_preview  = gimp_color_editor_get_preview;
144   iface->set_aux_info = gimp_color_editor_set_aux_info;
145   iface->get_aux_info = gimp_color_editor_get_aux_info;
146   iface->set_context  = gimp_color_editor_set_context;
147 }
148 
149 static void
gimp_color_editor_init(GimpColorEditor * editor)150 gimp_color_editor_init (GimpColorEditor *editor)
151 {
152   GtkWidget   *notebook;
153   GtkWidget   *hbox;
154   GtkWidget   *button;
155   gint         content_spacing;
156   gint         button_spacing;
157   GtkIconSize  button_icon_size;
158   GimpRGB      rgb;
159   GimpHSV      hsv;
160   GList       *list;
161   GSList      *group;
162 
163   editor->context = NULL;
164   editor->edit_bg = FALSE;
165 
166   gimp_rgba_set (&rgb, 0.0, 0.0, 0.0, 1.0);
167   gimp_rgb_to_hsv (&rgb, &hsv);
168 
169   gtk_widget_style_get (GTK_WIDGET (editor),
170                         "content-spacing",  &content_spacing,
171                         "button-spacing",   &button_spacing,
172                         "button-icon-size", &button_icon_size,
173                         NULL);
174 
175   editor->hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, button_spacing);
176   gtk_box_set_homogeneous (GTK_BOX (editor->hbox), TRUE);
177   gtk_box_pack_start (GTK_BOX (editor), editor->hbox, FALSE, FALSE, 0);
178   gtk_widget_show (editor->hbox);
179 
180   editor->notebook = gimp_color_selector_new (GIMP_TYPE_COLOR_NOTEBOOK,
181                                               &rgb, &hsv,
182                                               GIMP_COLOR_SELECTOR_RED);
183   gimp_color_selector_set_show_alpha (GIMP_COLOR_SELECTOR (editor->notebook),
184                                       FALSE);
185   gtk_box_pack_start (GTK_BOX (editor), editor->notebook,
186                       TRUE, TRUE, content_spacing);
187   gtk_widget_show (editor->notebook);
188 
189   g_signal_connect (editor->notebook, "color-changed",
190                     G_CALLBACK (gimp_color_editor_color_changed),
191                     editor);
192 
193   notebook = GIMP_COLOR_NOTEBOOK (editor->notebook)->notebook;
194 
195   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
196   gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
197 
198   gimp_color_notebook_set_has_page (GIMP_COLOR_NOTEBOOK (editor->notebook),
199                                     GIMP_TYPE_COLOR_SCALES, TRUE);
200 
201   group = NULL;
202 
203   for (list = GIMP_COLOR_NOTEBOOK (editor->notebook)->selectors;
204        list;
205        list = g_list_next (list))
206     {
207       GimpColorSelector      *selector;
208       GimpColorSelectorClass *selector_class;
209       GtkWidget              *button;
210       GtkWidget              *image;
211 
212       selector       = GIMP_COLOR_SELECTOR (list->data);
213       selector_class = GIMP_COLOR_SELECTOR_GET_CLASS (selector);
214 
215       button = gtk_radio_button_new (group);
216       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
217       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
218       gtk_box_pack_start (GTK_BOX (editor->hbox), button, TRUE, TRUE, 0);
219       gtk_widget_show (button);
220 
221       image = gtk_image_new_from_icon_name (selector_class->icon_name,
222                                             GTK_ICON_SIZE_BUTTON);
223       gtk_container_add (GTK_CONTAINER (button), image);
224       gtk_widget_show (image);
225 
226       gimp_help_set_help_data (button,
227                                selector_class->name, selector_class->help_id);
228 
229       g_object_set_data (G_OBJECT (button),   "selector", selector);
230       g_object_set_data (G_OBJECT (selector), "button",   button);
231 
232       g_signal_connect (button, "toggled",
233                         G_CALLBACK (gimp_color_editor_tab_toggled),
234                         editor);
235     }
236 
237   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
238   gtk_box_set_homogeneous (GTK_BOX (hbox), FALSE);
239   gtk_box_pack_start (GTK_BOX (editor), hbox, FALSE, FALSE, 0);
240   gtk_widget_show (hbox);
241 
242   /*  FG/BG editor  */
243   editor->fg_bg = gimp_fg_bg_editor_new (NULL);
244   gtk_box_pack_start (GTK_BOX (hbox), editor->fg_bg, TRUE, TRUE, 0);
245   gtk_widget_show (editor->fg_bg);
246 
247   g_signal_connect (editor->fg_bg, "notify::active-color",
248                     G_CALLBACK (gimp_color_editor_fg_bg_notify),
249                     editor);
250 
251   /*  The color picker  */
252   button = gimp_pick_button_new ();
253   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
254   gtk_widget_show (button);
255 
256   g_signal_connect (button, "color-picked",
257                     G_CALLBACK (gimp_color_editor_color_picked),
258                     editor);
259 
260   /*  The hex triplet entry  */
261   editor->hex_entry = gimp_color_hex_entry_new ();
262   gtk_box_pack_end (GTK_BOX (hbox), editor->hex_entry, TRUE, TRUE, 0);
263   gtk_widget_show (editor->hex_entry);
264 
265   g_signal_connect (editor->hex_entry, "color-changed",
266                     G_CALLBACK (gimp_color_editor_entry_changed),
267                     editor);
268 }
269 
270 static void
gimp_color_editor_constructed(GObject * object)271 gimp_color_editor_constructed (GObject *object)
272 {
273   GimpColorEditor *editor = GIMP_COLOR_EDITOR (object);
274   GtkWidget       *history;
275 
276   G_OBJECT_CLASS (parent_class)->constructed (object);
277 
278   /* The color history */
279   history = gimp_color_history_new (editor->context, 12);
280   gtk_box_pack_end (GTK_BOX (editor), history, FALSE, FALSE, 0);
281   gtk_widget_show (history);
282 
283   g_signal_connect (history, "color-selected",
284                     G_CALLBACK (gimp_color_editor_history_selected),
285                     editor);
286 }
287 
288 static void
gimp_color_editor_dispose(GObject * object)289 gimp_color_editor_dispose (GObject *object)
290 {
291   GimpColorEditor *editor = GIMP_COLOR_EDITOR (object);
292 
293   if (editor->context)
294     gimp_docked_set_context (GIMP_DOCKED (editor), NULL);
295 
296   G_OBJECT_CLASS (parent_class)->dispose (object);
297 }
298 
299 static void
gimp_color_editor_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)300 gimp_color_editor_set_property (GObject      *object,
301                                 guint         property_id,
302                                 const GValue *value,
303                                 GParamSpec   *pspec)
304 {
305   switch (property_id)
306     {
307     case PROP_CONTEXT:
308       gimp_docked_set_context (GIMP_DOCKED (object),
309                                g_value_get_object (value));
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
313       break;
314     }
315 }
316 
317 static void
gimp_color_editor_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)318 gimp_color_editor_get_property (GObject    *object,
319                                 guint       property_id,
320                                 GValue     *value,
321                                 GParamSpec *pspec)
322 {
323   GimpColorEditor *editor = GIMP_COLOR_EDITOR (object);
324 
325   switch (property_id)
326     {
327     case PROP_CONTEXT:
328       g_value_set_object (value, editor->context);
329       break;
330     default:
331       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
332       break;
333     }
334 }
335 
336 static GtkWidget *
gimp_color_editor_get_preview(GimpDocked * docked,GimpContext * context,GtkIconSize size)337 gimp_color_editor_get_preview (GimpDocked  *docked,
338                                GimpContext *context,
339                                GtkIconSize  size)
340 {
341   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (docked));
342   GtkWidget   *preview;
343   gint         width;
344   gint         height;
345 
346   preview = gimp_fg_bg_view_new (context);
347 
348   if (gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
349     gtk_widget_set_size_request (preview, width, height);
350 
351   return preview;
352 }
353 
354 #define AUX_INFO_CURRENT_PAGE "current-page"
355 
356 static void
gimp_color_editor_set_aux_info(GimpDocked * docked,GList * aux_info)357 gimp_color_editor_set_aux_info (GimpDocked *docked,
358                                 GList      *aux_info)
359 {
360   GimpColorEditor *editor   = GIMP_COLOR_EDITOR (docked);
361   GtkWidget       *notebook = GIMP_COLOR_NOTEBOOK (editor->notebook)->notebook;
362   GList           *list;
363 
364   parent_docked_iface->set_aux_info (docked, aux_info);
365 
366   for (list = aux_info; list; list = g_list_next (list))
367     {
368       GimpSessionInfoAux *aux = list->data;
369 
370       if (! strcmp (aux->name, AUX_INFO_CURRENT_PAGE))
371         {
372           GList *children;
373           GList *child;
374 
375           children = gtk_container_get_children (GTK_CONTAINER (notebook));
376 
377           for (child = children; child; child = g_list_next (child))
378             {
379               if (! strcmp (G_OBJECT_TYPE_NAME (child->data), aux->value))
380                 {
381                   GtkWidget *button;
382 
383                   button = g_object_get_data (G_OBJECT (child->data), "button");
384 
385                   if (button)
386                     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
387                                                   TRUE);
388 
389                   break;
390                 }
391             }
392 
393           g_list_free (children);
394         }
395     }
396 }
397 
398 static GList *
gimp_color_editor_get_aux_info(GimpDocked * docked)399 gimp_color_editor_get_aux_info (GimpDocked *docked)
400 {
401   GimpColorEditor    *editor   = GIMP_COLOR_EDITOR (docked);
402   GimpColorNotebook  *notebook = GIMP_COLOR_NOTEBOOK (editor->notebook);
403   GList              *aux_info;
404 
405   aux_info = parent_docked_iface->get_aux_info (docked);
406 
407   if (notebook->cur_page)
408     {
409       GimpSessionInfoAux *aux;
410 
411       aux = gimp_session_info_aux_new (AUX_INFO_CURRENT_PAGE,
412                                        G_OBJECT_TYPE_NAME (notebook->cur_page));
413       aux_info = g_list_append (aux_info, aux);
414     }
415 
416   return aux_info;
417 }
418 
419 static void
gimp_color_editor_set_context(GimpDocked * docked,GimpContext * context)420 gimp_color_editor_set_context (GimpDocked  *docked,
421                                GimpContext *context)
422 {
423   GimpColorEditor *editor = GIMP_COLOR_EDITOR (docked);
424 
425   if (context == editor->context)
426     return;
427 
428   if (editor->context)
429     {
430       g_signal_handlers_disconnect_by_func (editor->context,
431                                             gimp_color_editor_fg_changed,
432                                             editor);
433       g_signal_handlers_disconnect_by_func (editor->context,
434                                             gimp_color_editor_bg_changed,
435                                             editor);
436 
437       g_object_unref (editor->context);
438     }
439 
440   editor->context = context;
441 
442   if (editor->context)
443     {
444       GimpRGB rgb;
445 
446       g_object_ref (editor->context);
447 
448       g_signal_connect (editor->context, "foreground-changed",
449                         G_CALLBACK (gimp_color_editor_fg_changed),
450                         editor);
451       g_signal_connect (editor->context, "background-changed",
452                         G_CALLBACK (gimp_color_editor_bg_changed),
453                         editor);
454 
455       if (editor->edit_bg)
456         {
457           gimp_context_get_background (editor->context, &rgb);
458           gimp_color_editor_bg_changed (editor->context, &rgb, editor);
459         }
460       else
461         {
462           gimp_context_get_foreground (editor->context, &rgb);
463           gimp_color_editor_fg_changed (editor->context, &rgb, editor);
464         }
465 
466       g_object_set_data (G_OBJECT (context->gimp->config->color_management),
467                          "gimp-context", editor->context);
468 
469       gimp_color_selector_set_config (GIMP_COLOR_SELECTOR (editor->notebook),
470                                       context->gimp->config->color_management);
471 
472       g_object_set_data (G_OBJECT (context->gimp->config->color_management),
473                          "gimp-context", NULL);
474     }
475 
476   gimp_fg_bg_editor_set_context (GIMP_FG_BG_EDITOR (editor->fg_bg), context);
477 }
478 
479 GtkWidget *
gimp_color_editor_new(GimpContext * context)480 gimp_color_editor_new (GimpContext *context)
481 {
482   return g_object_new (GIMP_TYPE_COLOR_EDITOR,
483                        "context", context,
484                        NULL);
485 }
486 
487 static void
gimp_color_editor_style_set(GtkWidget * widget,GtkStyle * prev_style)488 gimp_color_editor_style_set (GtkWidget *widget,
489                              GtkStyle  *prev_style)
490 {
491   GimpColorEditor *editor = GIMP_COLOR_EDITOR (widget);
492 
493   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
494 
495   if (editor->hbox)
496     gimp_editor_set_box_style (GIMP_EDITOR (editor), GTK_BOX (editor->hbox));
497 }
498 
499 
500 static void
gimp_color_editor_set_color(GimpColorEditor * editor,const GimpRGB * rgb)501 gimp_color_editor_set_color (GimpColorEditor *editor,
502                              const GimpRGB   *rgb)
503 {
504   GimpHSV hsv;
505 
506   gimp_rgb_to_hsv (rgb, &hsv);
507 
508   g_signal_handlers_block_by_func (editor->notebook,
509                                    gimp_color_editor_color_changed,
510                                    editor);
511 
512   gimp_color_selector_set_color (GIMP_COLOR_SELECTOR (editor->notebook),
513                                  rgb, &hsv);
514 
515   g_signal_handlers_unblock_by_func (editor->notebook,
516                                      gimp_color_editor_color_changed,
517                                      editor);
518 
519   g_signal_handlers_block_by_func (editor->hex_entry,
520                                    gimp_color_editor_entry_changed,
521                                    editor);
522 
523   gimp_color_hex_entry_set_color (GIMP_COLOR_HEX_ENTRY (editor->hex_entry),
524                                   rgb);
525 
526   g_signal_handlers_unblock_by_func (editor->hex_entry,
527                                      gimp_color_editor_entry_changed,
528                                      editor);
529 }
530 
531 static void
gimp_color_editor_fg_changed(GimpContext * context,const GimpRGB * rgb,GimpColorEditor * editor)532 gimp_color_editor_fg_changed (GimpContext     *context,
533                               const GimpRGB   *rgb,
534                               GimpColorEditor *editor)
535 {
536   if (! editor->edit_bg)
537     gimp_color_editor_set_color (editor, rgb);
538 }
539 
540 static void
gimp_color_editor_bg_changed(GimpContext * context,const GimpRGB * rgb,GimpColorEditor * editor)541 gimp_color_editor_bg_changed (GimpContext     *context,
542                               const GimpRGB   *rgb,
543                               GimpColorEditor *editor)
544 {
545   if (editor->edit_bg)
546     gimp_color_editor_set_color (editor, rgb);
547 }
548 
549 static void
gimp_color_editor_color_changed(GimpColorSelector * selector,const GimpRGB * rgb,const GimpHSV * hsv,GimpColorEditor * editor)550 gimp_color_editor_color_changed (GimpColorSelector *selector,
551                                  const GimpRGB     *rgb,
552                                  const GimpHSV     *hsv,
553                                  GimpColorEditor   *editor)
554 {
555   if (editor->context)
556     {
557       if (editor->edit_bg)
558         {
559           g_signal_handlers_block_by_func (editor->context,
560                                            gimp_color_editor_bg_changed,
561                                            editor);
562 
563           gimp_context_set_background (editor->context, rgb);
564 
565           g_signal_handlers_unblock_by_func (editor->context,
566                                              gimp_color_editor_bg_changed,
567                                              editor);
568         }
569       else
570         {
571           g_signal_handlers_block_by_func (editor->context,
572                                            gimp_color_editor_fg_changed,
573                                            editor);
574 
575           gimp_context_set_foreground (editor->context, rgb);
576 
577           g_signal_handlers_unblock_by_func (editor->context,
578                                              gimp_color_editor_fg_changed,
579                                              editor);
580         }
581     }
582 
583   g_signal_handlers_block_by_func (editor->hex_entry,
584                                    gimp_color_editor_entry_changed,
585                                    editor);
586 
587   gimp_color_hex_entry_set_color (GIMP_COLOR_HEX_ENTRY (editor->hex_entry),
588                                   rgb);
589 
590   g_signal_handlers_unblock_by_func (editor->hex_entry,
591                                      gimp_color_editor_entry_changed,
592                                      editor);
593 }
594 
595 static void
gimp_color_editor_tab_toggled(GtkWidget * widget,GimpColorEditor * editor)596 gimp_color_editor_tab_toggled (GtkWidget       *widget,
597                                GimpColorEditor *editor)
598 {
599   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
600     {
601       GtkWidget *selector;
602 
603       selector = g_object_get_data (G_OBJECT (widget), "selector");
604 
605       if (selector)
606         {
607           GtkWidget *notebook;
608           gint       page_num;
609 
610           notebook = GIMP_COLOR_NOTEBOOK (editor->notebook)->notebook;
611 
612           page_num = gtk_notebook_page_num (GTK_NOTEBOOK (notebook), selector);
613 
614           if (page_num >= 0)
615             gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page_num);
616         }
617     }
618 }
619 
620 static void
gimp_color_editor_fg_bg_notify(GtkWidget * widget,GParamSpec * pspec,GimpColorEditor * editor)621 gimp_color_editor_fg_bg_notify (GtkWidget       *widget,
622                                 GParamSpec      *pspec,
623                                 GimpColorEditor *editor)
624 {
625   gboolean edit_bg;
626 
627   edit_bg = (GIMP_FG_BG_EDITOR (widget)->active_color ==
628              GIMP_ACTIVE_COLOR_BACKGROUND);
629 
630   if (edit_bg != editor->edit_bg)
631     {
632       editor->edit_bg = edit_bg;
633 
634       if (editor->context)
635         {
636           GimpRGB rgb;
637 
638           if (edit_bg)
639             {
640               gimp_context_get_background (editor->context, &rgb);
641               gimp_color_editor_bg_changed (editor->context, &rgb, editor);
642             }
643           else
644             {
645               gimp_context_get_foreground (editor->context, &rgb);
646               gimp_color_editor_fg_changed (editor->context, &rgb, editor);
647             }
648         }
649     }
650 }
651 
652 static void
gimp_color_editor_color_picked(GtkWidget * widget,const GimpRGB * rgb,GimpColorEditor * editor)653 gimp_color_editor_color_picked (GtkWidget       *widget,
654                                 const GimpRGB   *rgb,
655                                 GimpColorEditor *editor)
656 {
657   if (editor->context)
658     {
659       if (editor->edit_bg)
660         gimp_context_set_background (editor->context, rgb);
661       else
662         gimp_context_set_foreground (editor->context, rgb);
663     }
664 }
665 
666 static void
gimp_color_editor_entry_changed(GimpColorHexEntry * entry,GimpColorEditor * editor)667 gimp_color_editor_entry_changed (GimpColorHexEntry *entry,
668                                  GimpColorEditor   *editor)
669 {
670   GimpRGB  rgb;
671 
672   gimp_color_hex_entry_get_color (entry, &rgb);
673 
674   if (editor->context)
675     {
676       if (editor->edit_bg)
677         gimp_context_set_background (editor->context, &rgb);
678       else
679         gimp_context_set_foreground (editor->context, &rgb);
680     }
681 }
682 
683 static void
gimp_color_editor_history_selected(GimpColorHistory * history,const GimpRGB * rgb,GimpColorEditor * editor)684 gimp_color_editor_history_selected (GimpColorHistory *history,
685                                     const GimpRGB    *rgb,
686                                     GimpColorEditor  *editor)
687 {
688   if (editor->context)
689     {
690       if (editor->edit_bg)
691         gimp_context_set_background (editor->context, rgb);
692       else
693         gimp_context_set_foreground (editor->context, rgb);
694     }
695 }
696