1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpfgbgview.c
5  * Copyright (C) 2005  Sven Neumann <sven@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 #include "core/gimpmarshal.h"
38 
39 #include "gimpdnd.h"
40 #include "gimpfgbgview.h"
41 
42 
43 enum
44 {
45   PROP_0,
46   PROP_CONTEXT
47 };
48 
49 
50 static void     gimp_fg_bg_view_dispose           (GObject        *object);
51 static void     gimp_fg_bg_view_set_property      (GObject        *object,
52                                                    guint           property_id,
53                                                    const GValue   *value,
54                                                    GParamSpec     *pspec);
55 static void     gimp_fg_bg_view_get_property      (GObject        *object,
56                                                    guint           property_id,
57                                                    GValue         *value,
58                                                    GParamSpec     *pspec);
59 
60 static gboolean gimp_fg_bg_view_expose            (GtkWidget      *widget,
61                                                    GdkEventExpose *eevent);
62 
63 static void     gimp_fg_bg_view_create_transform  (GimpFgBgView   *view);
64 static void     gimp_fg_bg_view_destroy_transform (GimpFgBgView   *view);
65 
66 
G_DEFINE_TYPE(GimpFgBgView,gimp_fg_bg_view,GTK_TYPE_WIDGET)67 G_DEFINE_TYPE (GimpFgBgView, gimp_fg_bg_view, GTK_TYPE_WIDGET)
68 
69 #define parent_class gimp_fg_bg_view_parent_class
70 
71 
72 static void
73 gimp_fg_bg_view_class_init (GimpFgBgViewClass *klass)
74 {
75   GObjectClass   *object_class = G_OBJECT_CLASS (klass);
76   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
77 
78   object_class->dispose      = gimp_fg_bg_view_dispose;
79   object_class->set_property = gimp_fg_bg_view_set_property;
80   object_class->get_property = gimp_fg_bg_view_get_property;
81 
82   widget_class->expose_event = gimp_fg_bg_view_expose;
83 
84   g_object_class_install_property (object_class, PROP_CONTEXT,
85                                    g_param_spec_object ("context",
86                                                         NULL, NULL,
87                                                         GIMP_TYPE_CONTEXT,
88                                                         GIMP_PARAM_READWRITE));
89 }
90 
91 static void
gimp_fg_bg_view_init(GimpFgBgView * view)92 gimp_fg_bg_view_init (GimpFgBgView *view)
93 {
94   gtk_widget_set_has_window (GTK_WIDGET (view), FALSE);
95 
96   gimp_widget_track_monitor (GTK_WIDGET (view),
97                              G_CALLBACK (gimp_fg_bg_view_destroy_transform),
98                              NULL);
99 }
100 
101 static void
gimp_fg_bg_view_dispose(GObject * object)102 gimp_fg_bg_view_dispose (GObject *object)
103 {
104   GimpFgBgView *view = GIMP_FG_BG_VIEW (object);
105 
106   if (view->context)
107     gimp_fg_bg_view_set_context (view, NULL);
108 
109   G_OBJECT_CLASS (parent_class)->dispose (object);
110 }
111 
112 static void
gimp_fg_bg_view_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)113 gimp_fg_bg_view_set_property (GObject      *object,
114                               guint         property_id,
115                               const GValue *value,
116                               GParamSpec   *pspec)
117 {
118   GimpFgBgView *view = GIMP_FG_BG_VIEW (object);
119 
120   switch (property_id)
121     {
122     case PROP_CONTEXT:
123       gimp_fg_bg_view_set_context (view, g_value_get_object (value));
124       break;
125 
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
128       break;
129     }
130 }
131 
132 static void
gimp_fg_bg_view_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)133 gimp_fg_bg_view_get_property (GObject    *object,
134                               guint       property_id,
135                               GValue     *value,
136                               GParamSpec *pspec)
137 {
138   GimpFgBgView *view = GIMP_FG_BG_VIEW (object);
139 
140   switch (property_id)
141     {
142     case PROP_CONTEXT:
143       g_value_set_object (value, view->context);
144       break;
145 
146     default:
147       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
148       break;
149     }
150 }
151 
152 static gboolean
gimp_fg_bg_view_expose(GtkWidget * widget,GdkEventExpose * eevent)153 gimp_fg_bg_view_expose (GtkWidget      *widget,
154                         GdkEventExpose *eevent)
155 {
156   GimpFgBgView *view   = GIMP_FG_BG_VIEW (widget);
157   GtkStyle     *style  = gtk_widget_get_style (widget);
158   GdkWindow    *window = gtk_widget_get_window (widget);
159   cairo_t      *cr;
160   GtkAllocation allocation;
161   gint          rect_w, rect_h;
162   GimpRGB       color;
163 
164   if (! gtk_widget_is_drawable (widget))
165     return FALSE;
166 
167   cr = gdk_cairo_create (eevent->window);
168 
169   gdk_cairo_region (cr, eevent->region);
170   cairo_clip (cr);
171 
172   gtk_widget_get_allocation (widget, &allocation);
173 
174   cairo_translate (cr, allocation.x, allocation.y);
175 
176   rect_w = allocation.width  * 3 / 4;
177   rect_h = allocation.height * 3 / 4;
178 
179   if (! view->transform)
180     gimp_fg_bg_view_create_transform (view);
181 
182   /*  draw the background area  */
183 
184   if (view->context)
185     {
186       gimp_context_get_background (view->context, &color);
187 
188       if (view->transform)
189         gimp_color_transform_process_pixels (view->transform,
190                                              babl_format ("R'G'B'A double"),
191                                              &color,
192                                              babl_format ("R'G'B'A double"),
193                                              &color,
194                                              1);
195 
196       gimp_cairo_set_source_rgb (cr, &color);
197 
198       cairo_rectangle (cr,
199                        allocation.width  - rect_w + 1,
200                        allocation.height - rect_h + 1,
201                        rect_w - 2,
202                        rect_h - 2);
203       cairo_fill (cr);
204     }
205 
206   gtk_paint_shadow (style, window, GTK_STATE_NORMAL,
207                     GTK_SHADOW_IN,
208                     NULL, widget, NULL,
209                     allocation.x + allocation.width  - rect_w,
210                     allocation.y + allocation.height - rect_h,
211                     rect_w, rect_h);
212 
213   /*  draw the foreground area  */
214 
215   if (view->context)
216     {
217       gimp_context_get_foreground (view->context, &color);
218 
219       if (view->transform)
220         gimp_color_transform_process_pixels (view->transform,
221                                              babl_format ("R'G'B'A double"),
222                                              &color,
223                                              babl_format ("R'G'B'A double"),
224                                              &color,
225                                              1);
226 
227       gimp_cairo_set_source_rgb (cr, &color);
228 
229       cairo_rectangle (cr, 1, 1, rect_w - 2, rect_h - 2);
230       cairo_fill (cr);
231     }
232 
233   gtk_paint_shadow (style, window, GTK_STATE_NORMAL,
234                     GTK_SHADOW_OUT,
235                     NULL, widget, NULL,
236                     allocation.x, allocation.y, rect_w, rect_h);
237 
238   cairo_destroy (cr);
239 
240   return TRUE;
241 }
242 
243 static void
gimp_fg_bg_view_create_transform(GimpFgBgView * view)244 gimp_fg_bg_view_create_transform (GimpFgBgView *view)
245 {
246   if (view->color_config)
247     {
248       static GimpColorProfile *profile = NULL;
249 
250       if (G_UNLIKELY (! profile))
251         profile = gimp_color_profile_new_rgb_srgb ();
252 
253       view->transform =
254         gimp_widget_get_color_transform (GTK_WIDGET (view),
255                                          view->color_config,
256                                          profile,
257                                          babl_format ("R'G'B'A double"),
258                                          babl_format ("R'G'B'A double"));
259     }
260 }
261 
262 static void
gimp_fg_bg_view_destroy_transform(GimpFgBgView * view)263 gimp_fg_bg_view_destroy_transform (GimpFgBgView *view)
264 {
265   g_clear_object (&view->transform);
266 
267   gtk_widget_queue_draw (GTK_WIDGET (view));
268 }
269 
270 
271 /*  public functions  */
272 
273 GtkWidget *
gimp_fg_bg_view_new(GimpContext * context)274 gimp_fg_bg_view_new (GimpContext *context)
275 {
276   g_return_val_if_fail (context == NULL || GIMP_IS_CONTEXT (context), NULL);
277 
278   return g_object_new (GIMP_TYPE_FG_BG_VIEW,
279                        "context", context,
280                        NULL);
281 }
282 
283 void
gimp_fg_bg_view_set_context(GimpFgBgView * view,GimpContext * context)284 gimp_fg_bg_view_set_context (GimpFgBgView *view,
285                              GimpContext  *context)
286 {
287   g_return_if_fail (GIMP_IS_FG_BG_VIEW (view));
288   g_return_if_fail (context == NULL || GIMP_IS_CONTEXT (context));
289 
290   if (context != view->context)
291     {
292       if (view->context)
293         {
294           g_signal_handlers_disconnect_by_func (view->context,
295                                                 gtk_widget_queue_draw,
296                                                 view);
297           g_clear_object (&view->context);
298 
299           g_signal_handlers_disconnect_by_func (view->color_config,
300                                                 gimp_fg_bg_view_destroy_transform,
301                                                 view);
302           g_clear_object (&view->color_config);
303         }
304 
305       view->context = context;
306 
307       if (context)
308         {
309           g_object_ref (context);
310 
311           g_signal_connect_swapped (context, "foreground-changed",
312                                     G_CALLBACK (gtk_widget_queue_draw),
313                                     view);
314           g_signal_connect_swapped (context, "background-changed",
315                                     G_CALLBACK (gtk_widget_queue_draw),
316                                     view);
317 
318           view->color_config = g_object_ref (context->gimp->config->color_management);
319 
320           g_signal_connect_swapped (view->color_config, "notify",
321                                     G_CALLBACK (gimp_fg_bg_view_destroy_transform),
322                                     view);
323         }
324 
325       gimp_fg_bg_view_destroy_transform (view);
326 
327       g_object_notify (G_OBJECT (view), "context");
328     }
329 }
330