1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpbrushselect.c
5 * Copyright (C) 2004 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 "libgimpbase/gimpbase.h"
27 #include "libgimpwidgets/gimpwidgets.h"
28
29 #include "widgets-types.h"
30
31 #include "gegl/gimp-babl-compat.h"
32
33 #include "core/gimp.h"
34 #include "core/gimpcontext.h"
35 #include "core/gimpbrush.h"
36 #include "core/gimpparamspecs.h"
37 #include "core/gimptempbuf.h"
38
39 #include "pdb/gimppdb.h"
40
41 #include "gimpbrushfactoryview.h"
42 #include "gimpbrushselect.h"
43 #include "gimpcontainerbox.h"
44 #include "gimplayermodebox.h"
45 #include "gimpspinscale.h"
46
47 #include "gimp-intl.h"
48
49
50 enum
51 {
52 PROP_0,
53 PROP_OPACITY,
54 PROP_PAINT_MODE,
55 PROP_SPACING
56 };
57
58
59 static void gimp_brush_select_constructed (GObject *object);
60 static void gimp_brush_select_set_property (GObject *object,
61 guint property_id,
62 const GValue *value,
63 GParamSpec *pspec);
64
65 static GimpValueArray * gimp_brush_select_run_callback (GimpPdbDialog *dialog,
66 GimpObject *object,
67 gboolean closing,
68 GError **error);
69
70 static void gimp_brush_select_opacity_changed (GimpContext *context,
71 gdouble opacity,
72 GimpBrushSelect *select);
73 static void gimp_brush_select_mode_changed (GimpContext *context,
74 GimpLayerMode paint_mode,
75 GimpBrushSelect *select);
76
77 static void gimp_brush_select_opacity_update (GtkAdjustment *adj,
78 GimpBrushSelect *select);
79 static void gimp_brush_select_spacing_update (GtkAdjustment *adj,
80 GimpBrushSelect *select);
81
82
G_DEFINE_TYPE(GimpBrushSelect,gimp_brush_select,GIMP_TYPE_PDB_DIALOG)83 G_DEFINE_TYPE (GimpBrushSelect, gimp_brush_select, GIMP_TYPE_PDB_DIALOG)
84
85 #define parent_class gimp_brush_select_parent_class
86
87
88 static void
89 gimp_brush_select_class_init (GimpBrushSelectClass *klass)
90 {
91 GObjectClass *object_class = G_OBJECT_CLASS (klass);
92 GimpPdbDialogClass *pdb_class = GIMP_PDB_DIALOG_CLASS (klass);
93
94 object_class->constructed = gimp_brush_select_constructed;
95 object_class->set_property = gimp_brush_select_set_property;
96
97 pdb_class->run_callback = gimp_brush_select_run_callback;
98
99 g_object_class_install_property (object_class, PROP_OPACITY,
100 g_param_spec_double ("opacity", NULL, NULL,
101 GIMP_OPACITY_TRANSPARENT,
102 GIMP_OPACITY_OPAQUE,
103 GIMP_OPACITY_OPAQUE,
104 GIMP_PARAM_WRITABLE |
105 G_PARAM_CONSTRUCT));
106
107 g_object_class_install_property (object_class, PROP_PAINT_MODE,
108 g_param_spec_enum ("paint-mode", NULL, NULL,
109 GIMP_TYPE_LAYER_MODE,
110 GIMP_LAYER_MODE_NORMAL,
111 GIMP_PARAM_WRITABLE |
112 G_PARAM_CONSTRUCT));
113
114 g_object_class_install_property (object_class, PROP_SPACING,
115 g_param_spec_int ("spacing", NULL, NULL,
116 -G_MAXINT, 1000, -1,
117 GIMP_PARAM_WRITABLE |
118 G_PARAM_CONSTRUCT));
119 }
120
121 static void
gimp_brush_select_init(GimpBrushSelect * select)122 gimp_brush_select_init (GimpBrushSelect *select)
123 {
124 }
125
126 static void
gimp_brush_select_constructed(GObject * object)127 gimp_brush_select_constructed (GObject *object)
128 {
129 GimpPdbDialog *dialog = GIMP_PDB_DIALOG (object);
130 GimpBrushSelect *select = GIMP_BRUSH_SELECT (object);
131 GtkWidget *content_area;
132 GtkWidget *vbox;
133 GtkWidget *scale;
134 GtkWidget *hbox;
135 GtkWidget *label;
136 GtkAdjustment *spacing_adj;
137
138 G_OBJECT_CLASS (parent_class)->constructed (object);
139
140 gimp_context_set_opacity (dialog->context, select->initial_opacity);
141 gimp_context_set_paint_mode (dialog->context, select->initial_mode);
142
143 g_signal_connect (dialog->context, "opacity-changed",
144 G_CALLBACK (gimp_brush_select_opacity_changed),
145 dialog);
146 g_signal_connect (dialog->context, "paint-mode-changed",
147 G_CALLBACK (gimp_brush_select_mode_changed),
148 dialog);
149
150 dialog->view =
151 gimp_brush_factory_view_new (GIMP_VIEW_TYPE_GRID,
152 dialog->context->gimp->brush_factory,
153 dialog->context,
154 FALSE,
155 GIMP_VIEW_SIZE_MEDIUM, 1,
156 dialog->menu_factory);
157
158 gimp_container_box_set_size_request (GIMP_CONTAINER_BOX (GIMP_CONTAINER_EDITOR (dialog->view)->view),
159 5 * (GIMP_VIEW_SIZE_MEDIUM + 2),
160 5 * (GIMP_VIEW_SIZE_MEDIUM + 2));
161
162 gtk_container_set_border_width (GTK_CONTAINER (dialog->view), 12);
163
164 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
165 gtk_box_pack_start (GTK_BOX (content_area), dialog->view, TRUE, TRUE, 0);
166 gtk_widget_show (dialog->view);
167
168 vbox = GTK_WIDGET (GIMP_CONTAINER_EDITOR (dialog->view)->view);
169
170 /* Create the opacity scale widget */
171 select->opacity_data =
172 GTK_ADJUSTMENT (gtk_adjustment_new (gimp_context_get_opacity (dialog->context) * 100.0,
173 0.0, 100.0,
174 1.0, 10.0, 0.0));
175
176 scale = gimp_spin_scale_new (select->opacity_data,
177 _("Opacity"), 1);
178 gtk_box_pack_end (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
179 gtk_widget_show (scale);
180
181 g_signal_connect (select->opacity_data, "value-changed",
182 G_CALLBACK (gimp_brush_select_opacity_update),
183 select);
184
185 /* Create the paint mode option menu */
186 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
187 gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
188 gtk_widget_show (hbox);
189
190 label = gtk_label_new (_("Mode:"));
191 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
192 gtk_widget_show (label);
193
194 select->layer_mode_box = gimp_layer_mode_box_new (GIMP_LAYER_MODE_CONTEXT_PAINT);
195 gtk_box_pack_start (GTK_BOX (hbox), select->layer_mode_box, TRUE, TRUE, 0);
196 gtk_widget_show (select->layer_mode_box);
197
198 g_object_bind_property (G_OBJECT (dialog->context), "paint-mode",
199 G_OBJECT (select->layer_mode_box), "layer-mode",
200 G_BINDING_BIDIRECTIONAL |
201 G_BINDING_SYNC_CREATE);
202
203 spacing_adj = GIMP_BRUSH_FACTORY_VIEW (dialog->view)->spacing_adjustment;
204
205 /* Use passed spacing instead of brushes default */
206 if (select->spacing >= 0)
207 gtk_adjustment_set_value (spacing_adj, select->spacing);
208
209 g_signal_connect (spacing_adj, "value-changed",
210 G_CALLBACK (gimp_brush_select_spacing_update),
211 select);
212 }
213
214 static void
gimp_brush_select_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)215 gimp_brush_select_set_property (GObject *object,
216 guint property_id,
217 const GValue *value,
218 GParamSpec *pspec)
219 {
220 GimpPdbDialog *dialog = GIMP_PDB_DIALOG (object);
221 GimpBrushSelect *select = GIMP_BRUSH_SELECT (object);
222
223 switch (property_id)
224 {
225 case PROP_OPACITY:
226 if (dialog->view)
227 gimp_context_set_opacity (dialog->context, g_value_get_double (value));
228 else
229 select->initial_opacity = g_value_get_double (value);
230 break;
231 case PROP_PAINT_MODE:
232 if (dialog->view)
233 gimp_context_set_paint_mode (dialog->context, g_value_get_enum (value));
234 else
235 select->initial_mode = g_value_get_enum (value);
236 break;
237 case PROP_SPACING:
238 if (dialog->view)
239 {
240 if (g_value_get_int (value) >= 0)
241 gtk_adjustment_set_value (GIMP_BRUSH_FACTORY_VIEW (dialog->view)->spacing_adjustment,
242 g_value_get_int (value));
243 }
244 else
245 {
246 select->spacing = g_value_get_int (value);
247 }
248 break;
249 default:
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
251 break;
252 }
253 }
254
255 static GimpValueArray *
gimp_brush_select_run_callback(GimpPdbDialog * dialog,GimpObject * object,gboolean closing,GError ** error)256 gimp_brush_select_run_callback (GimpPdbDialog *dialog,
257 GimpObject *object,
258 gboolean closing,
259 GError **error)
260 {
261 GimpBrush *brush = GIMP_BRUSH (object);
262 GimpTempBuf *mask = gimp_brush_get_mask (brush);
263 const Babl *format;
264 gpointer data;
265 GimpArray *array;
266 GimpValueArray *return_vals;
267
268 format = gimp_babl_compat_u8_mask_format (gimp_temp_buf_get_format (mask));
269 data = gimp_temp_buf_lock (mask, format, GEGL_ACCESS_READ);
270
271 array = gimp_array_new (data,
272 gimp_temp_buf_get_width (mask) *
273 gimp_temp_buf_get_height (mask) *
274 babl_format_get_bytes_per_pixel (format),
275 TRUE);
276
277 return_vals =
278 gimp_pdb_execute_procedure_by_name (dialog->pdb,
279 dialog->caller_context,
280 NULL, error,
281 dialog->callback_name,
282 G_TYPE_STRING, gimp_object_get_name (object),
283 G_TYPE_DOUBLE, gimp_context_get_opacity (dialog->context) * 100.0,
284 GIMP_TYPE_INT32, GIMP_BRUSH_SELECT (dialog)->spacing,
285 GIMP_TYPE_INT32, gimp_context_get_paint_mode (dialog->context),
286 GIMP_TYPE_INT32, gimp_brush_get_width (brush),
287 GIMP_TYPE_INT32, gimp_brush_get_height (brush),
288 GIMP_TYPE_INT32, array->length,
289 GIMP_TYPE_INT8_ARRAY, array,
290 GIMP_TYPE_INT32, closing,
291 G_TYPE_NONE);
292
293 gimp_array_free (array);
294
295 gimp_temp_buf_unlock (mask, data);
296
297 return return_vals;
298 }
299
300 static void
gimp_brush_select_opacity_changed(GimpContext * context,gdouble opacity,GimpBrushSelect * select)301 gimp_brush_select_opacity_changed (GimpContext *context,
302 gdouble opacity,
303 GimpBrushSelect *select)
304 {
305 g_signal_handlers_block_by_func (select->opacity_data,
306 gimp_brush_select_opacity_update,
307 select);
308
309 gtk_adjustment_set_value (select->opacity_data, opacity * 100.0);
310
311 g_signal_handlers_unblock_by_func (select->opacity_data,
312 gimp_brush_select_opacity_update,
313 select);
314
315 gimp_pdb_dialog_run_callback (GIMP_PDB_DIALOG (select), FALSE);
316 }
317
318 static void
gimp_brush_select_mode_changed(GimpContext * context,GimpLayerMode paint_mode,GimpBrushSelect * select)319 gimp_brush_select_mode_changed (GimpContext *context,
320 GimpLayerMode paint_mode,
321 GimpBrushSelect *select)
322 {
323 gimp_pdb_dialog_run_callback (GIMP_PDB_DIALOG (select), FALSE);
324 }
325
326 static void
gimp_brush_select_opacity_update(GtkAdjustment * adjustment,GimpBrushSelect * select)327 gimp_brush_select_opacity_update (GtkAdjustment *adjustment,
328 GimpBrushSelect *select)
329 {
330 gimp_context_set_opacity (GIMP_PDB_DIALOG (select)->context,
331 gtk_adjustment_get_value (adjustment) / 100.0);
332 }
333
334 static void
gimp_brush_select_spacing_update(GtkAdjustment * adjustment,GimpBrushSelect * select)335 gimp_brush_select_spacing_update (GtkAdjustment *adjustment,
336 GimpBrushSelect *select)
337 {
338 gdouble value = gtk_adjustment_get_value (adjustment);
339
340 if (select->spacing != value)
341 {
342 select->spacing = value;
343
344 gimp_pdb_dialog_run_callback (GIMP_PDB_DIALOG (select), FALSE);
345 }
346 }
347