summaryrefslogtreecommitdiffstats
path: root/app/core/gimpfilloptions.c
blob: d10b6e457224b5824221da9d28a8a4efb7f9a823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
 *
 * gimpfilloptions.c
 * Copyright (C) 2003 Simon Budig
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"

#include "core-types.h"

#include "operations/layer-modes/gimp-layer-modes.h"

#include "gimp.h"
#include "gimp-palettes.h"
#include "gimpdrawable.h"
#include "gimpdrawable-fill.h"
#include "gimperror.h"
#include "gimpfilloptions.h"
#include "gimppattern.h"

#include "gimp-intl.h"


enum
{
  PROP_0,
  PROP_STYLE,
  PROP_ANTIALIAS,
  PROP_FEATHER,
  PROP_FEATHER_RADIUS,
  PROP_PATTERN_VIEW_TYPE,
  PROP_PATTERN_VIEW_SIZE
};


typedef struct _GimpFillOptionsPrivate GimpFillOptionsPrivate;

struct _GimpFillOptionsPrivate
{
  GimpFillStyle style;
  gboolean      antialias;
  gboolean      feather;
  gdouble       feather_radius;

  GimpViewType  pattern_view_type;
  GimpViewSize  pattern_view_size;

  const gchar  *undo_desc;
};

#define GET_PRIVATE(options) \
        ((GimpFillOptionsPrivate *) gimp_fill_options_get_instance_private ((GimpFillOptions *) (options)))


static void     gimp_fill_options_config_init  (GimpConfigInterface *iface);

static void     gimp_fill_options_set_property (GObject             *object,
                                                guint                property_id,
                                                const GValue        *value,
                                                GParamSpec          *pspec);
static void     gimp_fill_options_get_property (GObject             *object,
                                                guint                property_id,
                                                GValue              *value,
                                                GParamSpec          *pspec);

static gboolean gimp_fill_options_serialize    (GimpConfig          *config,
                                                GimpConfigWriter    *writer,
                                                gpointer             data);


G_DEFINE_TYPE_WITH_CODE (GimpFillOptions, gimp_fill_options, GIMP_TYPE_CONTEXT,
                         G_ADD_PRIVATE (GimpFillOptions)
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG,
                                                gimp_fill_options_config_init))


static void
gimp_fill_options_class_init (GimpFillOptionsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = gimp_fill_options_set_property;
  object_class->get_property = gimp_fill_options_get_property;

  GIMP_CONFIG_PROP_ENUM (object_class, PROP_STYLE,
                         "style",
                         _("Style"),
                         NULL,
                         GIMP_TYPE_FILL_STYLE,
                         GIMP_FILL_STYLE_SOLID,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_ANTIALIAS,
                            "antialias",
                            _("Antialiasing"),
                            NULL,
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_FEATHER,
                            "feather",
                            _("Feather edges"),
                            _("Enable feathering of fill edges"),
                            FALSE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_FEATHER_RADIUS,
                           "feather-radius",
                           _("Radius"),
                           _("Radius of feathering"),
                           0.0, 100.0, 10.0,
                           GIMP_PARAM_STATIC_STRINGS);

  g_object_class_install_property (object_class, PROP_PATTERN_VIEW_TYPE,
                                   g_param_spec_enum ("pattern-view-type",
                                                      NULL, NULL,
                                                      GIMP_TYPE_VIEW_TYPE,
                                                      GIMP_VIEW_TYPE_GRID,
                                                      G_PARAM_CONSTRUCT |
                                                      GIMP_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_PATTERN_VIEW_SIZE,
                                   g_param_spec_int ("pattern-view-size",
                                                     NULL, NULL,
                                                     GIMP_VIEW_SIZE_TINY,
                                                     GIMP_VIEWABLE_MAX_BUTTON_SIZE,
                                                     GIMP_VIEW_SIZE_SMALL,
                                                     G_PARAM_CONSTRUCT |
                                                     GIMP_PARAM_READWRITE));
}

static void
gimp_fill_options_config_init (GimpConfigInterface *iface)
{
  iface->serialize = gimp_fill_options_serialize;
}

static void
gimp_fill_options_init (GimpFillOptions *options)
{
}

static void
gimp_fill_options_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  GimpFillOptionsPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_STYLE:
      private->style = g_value_get_enum (value);
      private->undo_desc = NULL;
      break;
    case PROP_ANTIALIAS:
      private->antialias = g_value_get_boolean (value);
      break;
    case PROP_FEATHER:
      private->feather = g_value_get_boolean (value);
      break;
    case PROP_FEATHER_RADIUS:
      private->feather_radius = g_value_get_double (value);
      break;

    case PROP_PATTERN_VIEW_TYPE:
      private->pattern_view_type = g_value_get_enum (value);
      break;
    case PROP_PATTERN_VIEW_SIZE:
      private->pattern_view_size = g_value_get_int (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_fill_options_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  GimpFillOptionsPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_STYLE:
      g_value_set_enum (value, private->style);
      break;
    case PROP_ANTIALIAS:
      g_value_set_boolean (value, private->antialias);
      break;
    case PROP_FEATHER:
      g_value_set_boolean (value, private->feather);
      break;
    case PROP_FEATHER_RADIUS:
      g_value_set_double (value, private->feather_radius);
      break;

    case PROP_PATTERN_VIEW_TYPE:
      g_value_set_enum (value, private->pattern_view_type);
      break;
    case PROP_PATTERN_VIEW_SIZE:
      g_value_set_int (value, private->pattern_view_size);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static gboolean
gimp_fill_options_serialize (GimpConfig       *config,
                             GimpConfigWriter *writer,
                             gpointer          data)
{
  return gimp_config_serialize_properties (config, writer);
}


/*  public functions  */

GimpFillOptions *
gimp_fill_options_new (Gimp        *gimp,
                       GimpContext *context,
                       gboolean     use_context_color)
{
  GimpFillOptions *options;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (context == NULL || GIMP_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (use_context_color == FALSE || context != NULL, NULL);

  options = g_object_new (GIMP_TYPE_FILL_OPTIONS,
                          "gimp", gimp,
                          NULL);

  if (use_context_color)
    {
      gimp_context_define_properties (GIMP_CONTEXT (options),
                                      GIMP_CONTEXT_PROP_MASK_FOREGROUND |
                                      GIMP_CONTEXT_PROP_MASK_PATTERN,
                                      FALSE);

      gimp_context_set_parent (GIMP_CONTEXT (options), context);
    }

  return options;
}

GimpFillStyle
gimp_fill_options_get_style (GimpFillOptions *options)
{
  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), GIMP_FILL_STYLE_SOLID);

  return GET_PRIVATE (options)->style;
}

void
gimp_fill_options_set_style (GimpFillOptions *options,
                             GimpFillStyle    style)
{
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));

  g_object_set (options, "style", style, NULL);
}

gboolean
gimp_fill_options_get_antialias (GimpFillOptions *options)
{
  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);

  return GET_PRIVATE (options)->antialias;
}

void
gimp_fill_options_set_antialias (GimpFillOptions *options,
                                 gboolean         antialias)
{
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));

  g_object_set (options, "antialias", antialias, NULL);
}

gboolean
gimp_fill_options_get_feather (GimpFillOptions *options,
                               gdouble         *radius)
{
  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);

  if (radius)
    *radius = GET_PRIVATE (options)->feather_radius;

  return GET_PRIVATE (options)->feather;
}

void
gimp_fill_options_set_feather (GimpFillOptions *options,
                               gboolean         feather,
                               gdouble          radius)
{
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));

  g_object_set (options, "feather", feather, NULL);
  g_object_set (options, "feather-radius", radius, NULL);
}

gboolean
gimp_fill_options_set_by_fill_type (GimpFillOptions  *options,
                                    GimpContext      *context,
                                    GimpFillType      fill_type,
                                    GError          **error)
{
  GimpFillOptionsPrivate *private;
  GimpRGB                 color;
  const gchar            *undo_desc;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  private = GET_PRIVATE (options);

  private->undo_desc = NULL;

  switch (fill_type)
    {
    case GIMP_FILL_FOREGROUND:
      gimp_context_get_foreground (context, &color);
      undo_desc = C_("undo-type", "Fill with Foreground Color");
      break;

    case GIMP_FILL_BACKGROUND:
      gimp_context_get_background (context, &color);
      undo_desc = C_("undo-type", "Fill with Background Color");
      break;

    case GIMP_FILL_WHITE:
      gimp_rgba_set (&color, 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);
      undo_desc = C_("undo-type", "Fill with White");
      break;

    case GIMP_FILL_TRANSPARENT:
      gimp_context_get_background (context, &color);
      gimp_context_set_paint_mode (GIMP_CONTEXT (options),
                                   GIMP_LAYER_MODE_ERASE);
      undo_desc = C_("undo-type", "Fill with Transparency");
      break;

    case GIMP_FILL_PATTERN:
      {
        GimpPattern *pattern = gimp_context_get_pattern (context);

        if (! pattern)
          {
            g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                                 _("No patterns available for this operation."));
            return FALSE;
          }

        gimp_fill_options_set_style (options, GIMP_FILL_STYLE_PATTERN);
        gimp_context_set_pattern (GIMP_CONTEXT (options), pattern);
        private->undo_desc = C_("undo-type", "Fill with Pattern");

        return TRUE;
      }
      break;

    default:
      g_warning ("%s: invalid fill_type %d", G_STRFUNC, fill_type);
      return FALSE;
    }

  gimp_fill_options_set_style (options, GIMP_FILL_STYLE_SOLID);
  gimp_context_set_foreground (GIMP_CONTEXT (options), &color);
  private->undo_desc = undo_desc;

  return TRUE;
}

gboolean
gimp_fill_options_set_by_fill_mode (GimpFillOptions     *options,
                                    GimpContext         *context,
                                    GimpBucketFillMode   fill_mode,
                                    GError             **error)
{
  GimpFillType fill_type;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  switch (fill_mode)
    {
    default:
    case GIMP_BUCKET_FILL_FG:
      fill_type = GIMP_FILL_FOREGROUND;
      break;

    case GIMP_BUCKET_FILL_BG:
      fill_type = GIMP_FILL_BACKGROUND;
      break;

    case GIMP_BUCKET_FILL_PATTERN:
      fill_type = GIMP_FILL_PATTERN;
      break;
    }

  return gimp_fill_options_set_by_fill_type (options, context,
                                             fill_type, error);
}

const gchar *
gimp_fill_options_get_undo_desc (GimpFillOptions *options)
{
  GimpFillOptionsPrivate *private;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), NULL);

  private = GET_PRIVATE (options);

  if (private->undo_desc)
    return private->undo_desc;

  switch (private->style)
    {
    case GIMP_FILL_STYLE_SOLID:
      return C_("undo-type", "Fill with Solid Color");

    case GIMP_FILL_STYLE_PATTERN:
      return C_("undo-type", "Fill with Pattern");
    }

  g_return_val_if_reached (NULL);
}

const Babl *
gimp_fill_options_get_format (GimpFillOptions *options,
                              GimpDrawable    *drawable)
{
  GimpContext *context;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);

  context = GIMP_CONTEXT (options);

  return gimp_layer_mode_get_format (gimp_context_get_paint_mode (context),
                                     GIMP_LAYER_COLOR_SPACE_AUTO,
                                     GIMP_LAYER_COLOR_SPACE_AUTO,
                                     gimp_layer_mode_get_paint_composite_mode (
                                       gimp_context_get_paint_mode (context)),
                                     gimp_drawable_get_format (drawable));
}

GeglBuffer *
gimp_fill_options_create_buffer (GimpFillOptions     *options,
                                 GimpDrawable        *drawable,
                                 const GeglRectangle *rect,
                                 gint                 pattern_offset_x,
                                 gint                 pattern_offset_y)
{
  GeglBuffer *buffer;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), NULL);
  g_return_val_if_fail (gimp_fill_options_get_style (options) !=
                        GIMP_FILL_STYLE_PATTERN ||
                        gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL,
                        NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (rect != NULL, NULL);

  buffer = gegl_buffer_new (rect,
                            gimp_fill_options_get_format (options, drawable));

  gimp_fill_options_fill_buffer (options, drawable, buffer,
                                 pattern_offset_x, pattern_offset_y);

  return buffer;
}

void
gimp_fill_options_fill_buffer (GimpFillOptions *options,
                               GimpDrawable    *drawable,
                               GeglBuffer      *buffer,
                               gint             pattern_offset_x,
                               gint             pattern_offset_y)
{
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));
  g_return_if_fail (gimp_fill_options_get_style (options) !=
                    GIMP_FILL_STYLE_PATTERN ||
                    gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL);
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (GEGL_IS_BUFFER (buffer));

  switch (gimp_fill_options_get_style (options))
    {
    case GIMP_FILL_STYLE_SOLID:
      {
        GimpRGB color;

        gimp_context_get_foreground (GIMP_CONTEXT (options), &color);
        gimp_palettes_add_color_history (GIMP_CONTEXT (options)->gimp, &color);

        gimp_drawable_fill_buffer (drawable, buffer,
                                   &color, NULL, 0, 0);
      }
      break;

    case GIMP_FILL_STYLE_PATTERN:
      {
        GimpPattern *pattern;

        pattern = gimp_context_get_pattern (GIMP_CONTEXT (options));

        gimp_drawable_fill_buffer (drawable, buffer,
                                   NULL, pattern,
                                   pattern_offset_x,
                                   pattern_offset_y);
      }
      break;
    }
}