1 /* LIBGIMP - The GIMP Library
2  * Copyright (C) 1995-1999 Peter Mattis and Spencer Kimball
3  *
4  * gimplayermodebox.c
5  * Copyright (C) 2017  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 <gtk/gtk.h>
24 #include <gegl.h>
25 
26 #include "libgimpbase/gimpbase.h"
27 #include "libgimpwidgets/gimpwidgets.h"
28 
29 #include "widgets-types.h"
30 
31 #include "operations/layer-modes/gimp-layer-modes.h"
32 
33 #include "gimplayermodebox.h"
34 #include "gimplayermodecombobox.h"
35 
36 #include "gimp-intl.h"
37 
38 
39 /**
40  * SECTION: gimplayermodebox
41  * @title: GimpLayerModeBox
42  * @short_description: A #GtkBox subclass for selecting a layer mode.
43  *
44  * A #GtkBox subclass for selecting a layer mode
45  **/
46 
47 
48 enum
49 {
50   PROP_0,
51   PROP_CONTEXT,
52   PROP_LAYER_MODE
53 };
54 
55 
56 struct _GimpLayerModeBoxPrivate
57 {
58   GimpLayerModeContext  context;
59   GimpLayerMode         layer_mode;
60 
61   GtkWidget            *mode_combo;
62   GtkWidget            *group_combo;
63 };
64 
65 
66 static void   gimp_layer_mode_box_constructed  (GObject      *object);
67 static void   gimp_layer_mode_box_set_property (GObject      *object,
68                                                 guint         prop_id,
69                                                 const GValue *value,
70                                                 GParamSpec   *pspec);
71 static void   gimp_layer_mode_box_get_property (GObject      *object,
72                                                 guint         prop_id,
73                                                 GValue       *value,
74                                                 GParamSpec   *pspec);
75 
76 
G_DEFINE_TYPE_WITH_PRIVATE(GimpLayerModeBox,gimp_layer_mode_box,GTK_TYPE_BOX)77 G_DEFINE_TYPE_WITH_PRIVATE (GimpLayerModeBox, gimp_layer_mode_box, GTK_TYPE_BOX)
78 
79 #define parent_class gimp_layer_mode_box_parent_class
80 
81 
82 static void
83 gimp_layer_mode_box_class_init (GimpLayerModeBoxClass *klass)
84 {
85   GObjectClass *object_class = G_OBJECT_CLASS (klass);
86 
87   object_class->constructed  = gimp_layer_mode_box_constructed;
88   object_class->set_property = gimp_layer_mode_box_set_property;
89   object_class->get_property = gimp_layer_mode_box_get_property;
90 
91   g_object_class_install_property (object_class, PROP_CONTEXT,
92                                    g_param_spec_flags ("context",
93                                                        NULL, NULL,
94                                                        GIMP_TYPE_LAYER_MODE_CONTEXT,
95                                                        GIMP_LAYER_MODE_CONTEXT_ALL,
96                                                        GIMP_PARAM_READWRITE |
97                                                        G_PARAM_CONSTRUCT));
98 
99   g_object_class_install_property (object_class, PROP_LAYER_MODE,
100                                    g_param_spec_enum ("layer-mode",
101                                                       NULL, NULL,
102                                                       GIMP_TYPE_LAYER_MODE,
103                                                       GIMP_LAYER_MODE_NORMAL,
104                                                       GIMP_PARAM_READWRITE |
105                                                       G_PARAM_CONSTRUCT));
106 }
107 
108 static void
gimp_layer_mode_box_init(GimpLayerModeBox * box)109 gimp_layer_mode_box_init (GimpLayerModeBox *box)
110 {
111   box->priv = gimp_layer_mode_box_get_instance_private (box);
112 
113   gtk_orientable_set_orientation (GTK_ORIENTABLE (box),
114                                   GTK_ORIENTATION_HORIZONTAL);
115   gtk_box_set_spacing (GTK_BOX (box), 4);
116 }
117 
118 static void
gimp_layer_mode_box_constructed(GObject * object)119 gimp_layer_mode_box_constructed (GObject *object)
120 {
121   GimpLayerModeBox *box = GIMP_LAYER_MODE_BOX (object);
122   GtkWidget        *mode_combo;
123   GtkWidget        *group_combo;
124   GtkTreeModel     *group_model;
125   gint              i;
126 
127   G_OBJECT_CLASS (parent_class)->constructed (object);
128 
129   box->priv->mode_combo = mode_combo =
130     gimp_layer_mode_combo_box_new (box->priv->context);
131   gtk_box_pack_start (GTK_BOX (box), mode_combo, TRUE, TRUE, 0);
132   gtk_widget_show (mode_combo);
133 
134   g_object_bind_property (object,                "context",
135                           G_OBJECT (mode_combo), "context",
136                           G_BINDING_BIDIRECTIONAL |
137                           G_BINDING_SYNC_CREATE);
138 
139   g_object_bind_property (object,                "layer-mode",
140                           G_OBJECT (mode_combo), "layer-mode",
141                           G_BINDING_BIDIRECTIONAL |
142                           G_BINDING_SYNC_CREATE);
143 
144   box->priv->group_combo = group_combo =
145     gimp_prop_enum_combo_box_new (G_OBJECT (mode_combo),
146                                   "group", 0, 0);
147   gimp_int_combo_box_set_layout (GIMP_INT_COMBO_BOX (group_combo),
148                                  GIMP_INT_COMBO_BOX_LAYOUT_ICON_ONLY);
149   gtk_box_pack_start (GTK_BOX (box), group_combo, FALSE, FALSE, 0);
150   gtk_widget_show (group_combo);
151 
152   gimp_help_set_help_data (group_combo,
153                            _("Switch to another group of modes"),
154                            NULL);
155 
156   group_model = gtk_combo_box_get_model (GTK_COMBO_BOX (group_combo));
157 
158   for (i = 0; i < 2; i++)
159     {
160       static const gchar *icons[] =
161       {
162         "gimp-reset",
163         "gimp-wilber-eek"
164       };
165 
166       GtkTreeIter iter;
167 
168       if (gimp_int_store_lookup_by_value (group_model, i, &iter))
169         gtk_list_store_set (GTK_LIST_STORE (group_model), &iter,
170                             GIMP_INT_STORE_ICON_NAME, icons[i],
171                             -1);
172     }
173 }
174 
175 static void
gimp_layer_mode_box_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)176 gimp_layer_mode_box_set_property (GObject      *object,
177                                   guint         prop_id,
178                                   const GValue *value,
179                                   GParamSpec   *pspec)
180 {
181   GimpLayerModeBox *box = GIMP_LAYER_MODE_BOX (object);
182 
183   switch (prop_id)
184     {
185     case PROP_CONTEXT:
186       gimp_layer_mode_box_set_context (box, g_value_get_flags (value));
187       break;
188 
189     case PROP_LAYER_MODE:
190       gimp_layer_mode_box_set_mode (box, g_value_get_enum (value));
191       break;
192 
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195       break;
196     }
197 }
198 
199 static void
gimp_layer_mode_box_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)200 gimp_layer_mode_box_get_property (GObject    *object,
201                                   guint       prop_id,
202                                   GValue     *value,
203                                   GParamSpec *pspec)
204 {
205   GimpLayerModeBox *box = GIMP_LAYER_MODE_BOX (object);
206 
207   switch (prop_id)
208     {
209     case PROP_CONTEXT:
210       g_value_set_flags (value, box->priv->context);
211       break;
212 
213     case PROP_LAYER_MODE:
214       g_value_set_enum (value, box->priv->layer_mode);
215       break;
216 
217     default:
218       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219       break;
220     }
221 }
222 
223 
224 /**
225  * gimp_layer_mode_box_new:
226  * Foo.
227  *
228  * Return value: a new #GimpLayerModeBox.
229  **/
230 GtkWidget *
gimp_layer_mode_box_new(GimpLayerModeContext context)231 gimp_layer_mode_box_new (GimpLayerModeContext context)
232 {
233   return g_object_new (GIMP_TYPE_LAYER_MODE_BOX,
234                        "context", context,
235                        NULL);
236 }
237 
238 void
gimp_layer_mode_box_set_context(GimpLayerModeBox * box,GimpLayerModeContext context)239 gimp_layer_mode_box_set_context (GimpLayerModeBox     *box,
240                                  GimpLayerModeContext  context)
241 {
242   g_return_if_fail (GIMP_IS_LAYER_MODE_BOX (box));
243 
244   if (context != box->priv->context)
245     {
246       box->priv->context = context;
247 
248       g_object_notify (G_OBJECT (box), "context");
249     }
250 }
251 
252 GimpLayerModeContext
gimp_layer_mode_box_get_context(GimpLayerModeBox * box)253 gimp_layer_mode_box_get_context (GimpLayerModeBox *box)
254 {
255   g_return_val_if_fail (GIMP_IS_LAYER_MODE_BOX (box),
256                         GIMP_LAYER_MODE_CONTEXT_ALL);
257 
258   return box->priv->context;
259 }
260 
261 void
gimp_layer_mode_box_set_mode(GimpLayerModeBox * box,GimpLayerMode mode)262 gimp_layer_mode_box_set_mode (GimpLayerModeBox *box,
263                               GimpLayerMode     mode)
264 {
265   g_return_if_fail (GIMP_IS_LAYER_MODE_BOX (box));
266 
267   if (mode != box->priv->layer_mode)
268     {
269       box->priv->layer_mode = mode;
270 
271       g_object_notify (G_OBJECT (box), "layer-mode");
272     }
273 }
274 
275 GimpLayerMode
gimp_layer_mode_box_get_mode(GimpLayerModeBox * box)276 gimp_layer_mode_box_get_mode (GimpLayerModeBox *box)
277 {
278   g_return_val_if_fail (GIMP_IS_LAYER_MODE_BOX (box),
279                         GIMP_LAYER_MODE_NORMAL);
280 
281   return box->priv->layer_mode;
282 }
283 
284 void
gimp_layer_mode_box_set_label(GimpLayerModeBox * box,const gchar * label)285 gimp_layer_mode_box_set_label (GimpLayerModeBox *box,
286                                const gchar      *label)
287 {
288   g_return_if_fail (GIMP_IS_LAYER_MODE_BOX (box));
289 
290   gimp_int_combo_box_set_label (GIMP_INT_COMBO_BOX (box->priv->mode_combo),
291                                 label);
292 }
293 
294 void
gimp_layer_mode_box_set_ellipsize(GimpLayerModeBox * box,PangoEllipsizeMode mode)295 gimp_layer_mode_box_set_ellipsize (GimpLayerModeBox   *box,
296                                    PangoEllipsizeMode  mode)
297 {
298   g_return_if_fail (GIMP_IS_LAYER_MODE_BOX (box));
299 
300   g_object_set (box->priv->mode_combo,
301                 "ellipsize", mode,
302                 NULL);
303 }
304