gimpbucketfilltool.c 34.4 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2
3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Elliot Lee's avatar
Elliot Lee committed
7
8
9
10
11
12
13
14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17

18
#include "config.h"
Michael Natterer's avatar
Michael Natterer committed
19

20
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
21
#include <gtk/gtk.h>
Michael Natterer's avatar
Michael Natterer committed
22

23
24
#include "libgimpwidgets/gimpwidgets.h"

25
#include "tools-types.h"
Sven Neumann's avatar
Sven Neumann committed
26

Michael Natterer's avatar
Michael Natterer committed
27
#include "core/gimp.h"
28
29
#include "core/gimpasync.h"
#include "core/gimpcancelable.h"
30
#include "core/gimpdrawable-bucket-fill.h"
31
#include "core/gimpdrawable-edit.h"
32
#include "core/gimpdrawablefilter.h"
33
#include "core/gimperror.h"
34
#include "core/gimpfilloptions.h"
35
#include "core/gimpimage.h"
36
#include "core/gimpitem.h"
Jehan's avatar
Jehan committed
37
#include "core/gimplineart.h"
38
#include "core/gimp-parallel.h"
Jehan's avatar
Jehan committed
39
40
#include "core/gimppickable.h"
#include "core/gimppickable-contiguous-region.h"
41
42
#include "core/gimpprogress.h"
#include "core/gimpprojection.h"
43
#include "core/gimpwaitable.h"
44

45
46
47
48
#include "gegl/gimp-gegl-nodes.h"

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

49
#include "widgets/gimphelp-ids.h"
50
#include "widgets/gimpwidgets-utils.h"
51

52
53
#include "display/gimpdisplay.h"

54
#include "gimpbucketfilloptions.h"
55
#include "gimpbucketfilltool.h"
56
#include "gimptoolcontrol.h"
57

58
#include "gimp-intl.h"
59

Elliot Lee's avatar
Elliot Lee committed
60

Jehan's avatar
Jehan committed
61
62
struct _GimpBucketFillToolPrivate
{
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  GimpAsync          *async;
  GeglBuffer         *line_art;
  GWeakRef            cached_image;
  GWeakRef            cached_drawable;

  gboolean            fill_in_progress;
  gboolean            compute_line_art_after_fill;
  GeglBuffer         *fill_buffer;
  GeglBuffer         *fill_mask;

  /* For preview */
  GeglNode           *graph;
  GeglNode           *fill_node;
  GeglNode           *offset_node;

  GimpDrawableFilter *filter;
Jehan's avatar
Jehan committed
79
80
};

81
/*  local function prototypes  */
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
static void     gimp_bucket_fill_tool_constructed      (GObject               *object);
static void     gimp_bucket_fill_tool_finalize         (GObject               *object);

static gboolean gimp_bucket_fill_tool_initialize       (GimpTool              *tool,
                                                        GimpDisplay           *display,
                                                        GError               **error);

static void     gimp_bucket_fill_tool_start            (GimpBucketFillTool    *tool,
                                                        const GimpCoords      *coords,
                                                        GimpDisplay           *display);
static void     gimp_bucket_fill_tool_preview          (GimpBucketFillTool    *tool,
                                                        const GimpCoords      *coords,
                                                        GimpDisplay           *display,
                                                        GimpFillOptions       *fill_options);
static void     gimp_bucket_fill_tool_commit           (GimpBucketFillTool    *tool);
static void     gimp_bucket_fill_tool_halt             (GimpBucketFillTool    *tool);
static void     gimp_bucket_fill_tool_filter_flush     (GimpDrawableFilter    *filter,
                                                        GimpTool              *tool);
static void     gimp_bucket_fill_tool_create_graph     (GimpBucketFillTool    *tool);

static void     gimp_bucket_fill_tool_button_press     (GimpTool              *tool,
                                                        const GimpCoords      *coords,
                                                        guint32                time,
                                                        GdkModifierType        state,
                                                        GimpButtonPressType    press_type,
                                                        GimpDisplay           *display);
static void     gimp_bucket_fill_tool_motion           (GimpTool              *tool,
                                                        const GimpCoords      *coords,
                                                        guint32                time,
                                                        GdkModifierType        state,
                                                        GimpDisplay           *display);
static void     gimp_bucket_fill_tool_button_release   (GimpTool              *tool,
                                                        const GimpCoords      *coords,
                                                        guint32                time,
                                                        GdkModifierType        state,
                                                        GimpButtonReleaseType  release_type,
                                                        GimpDisplay           *display);
static void     gimp_bucket_fill_tool_modifier_key     (GimpTool              *tool,
                                                        GdkModifierType        key,
                                                        gboolean               press,
                                                        GdkModifierType        state,
                                                        GimpDisplay           *display);
static void     gimp_bucket_fill_tool_cursor_update    (GimpTool              *tool,
                                                        const GimpCoords      *coords,
                                                        GdkModifierType        state,
                                                        GimpDisplay           *display);
129

Jehan's avatar
Jehan committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
static void     gimp_bucket_fill_compute_line_art      (GimpBucketFillTool    *tool);
static gboolean gimp_bucket_fill_tool_connect_handlers (gpointer               data);
static void     gimp_bucket_fill_tool_options_notified (GimpBucketFillOptions *options,
                                                        GParamSpec            *pspec,
                                                        GimpBucketFillTool    *tool);
static void     gimp_bucket_fill_tool_image_changed    (GimpContext           *context,
                                                        GimpImage             *image,
                                                        GimpBucketFillTool    *tool);
static void     gimp_bucket_fill_tool_drawable_changed (GimpImage            *image,
                                                        GimpBucketFillTool   *tool);
static void     gimp_bucket_fill_tool_drawable_update  (GimpDrawable         *drawable,
                                                        gint                  x,
                                                        gint                  y,
                                                        gint                  width,
                                                        gint                  height,
                                                        GimpBucketFillTool   *tool);


G_DEFINE_TYPE_WITH_PRIVATE (GimpBucketFillTool, gimp_bucket_fill_tool, GIMP_TYPE_TOOL)
149
150
151

#define parent_class gimp_bucket_fill_tool_parent_class

Elliot Lee's avatar
Elliot Lee committed
152

153
void
Nate Summers's avatar
Nate Summers committed
154
gimp_bucket_fill_tool_register (GimpToolRegisterCallback  callback,
155
                                gpointer                  data)
156
{
Nate Summers's avatar
Nate Summers committed
157
  (* callback) (GIMP_TYPE_BUCKET_FILL_TOOL,
158
159
                GIMP_TYPE_BUCKET_FILL_OPTIONS,
                gimp_bucket_fill_options_gui,
160
161
162
163
164
                GIMP_CONTEXT_PROP_MASK_FOREGROUND |
                GIMP_CONTEXT_PROP_MASK_BACKGROUND |
                GIMP_CONTEXT_PROP_MASK_OPACITY    |
                GIMP_CONTEXT_PROP_MASK_PAINT_MODE |
                GIMP_CONTEXT_PROP_MASK_PATTERN,
165
                "gimp-bucket-fill-tool",
166
                _("Bucket Fill"),
167
                _("Bucket Fill Tool: Fill selected area with a color or pattern"),
168
                N_("_Bucket Fill"), "<shift>B",
169
                NULL, GIMP_HELP_TOOL_BUCKET_FILL,
170
                GIMP_ICON_TOOL_BUCKET_FILL,
171
                data);
172
173
174
175
176
}

static void
gimp_bucket_fill_tool_class_init (GimpBucketFillToolClass *klass)
{
Jehan's avatar
Jehan committed
177
178
179
180
181
  GObjectClass  *object_class = G_OBJECT_CLASS (klass);
  GimpToolClass *tool_class   = GIMP_TOOL_CLASS (klass);

  object_class->constructed  = gimp_bucket_fill_tool_constructed;
  object_class->finalize     = gimp_bucket_fill_tool_finalize;
182

183
  tool_class->initialize     = gimp_bucket_fill_tool_initialize;
184
185
  tool_class->button_press   = gimp_bucket_fill_tool_button_press;
  tool_class->motion         = gimp_bucket_fill_tool_motion;
186
187
188
189
190
191
192
193
  tool_class->button_release = gimp_bucket_fill_tool_button_release;
  tool_class->modifier_key   = gimp_bucket_fill_tool_modifier_key;
  tool_class->cursor_update  = gimp_bucket_fill_tool_cursor_update;
}

static void
gimp_bucket_fill_tool_init (GimpBucketFillTool *bucket_fill_tool)
{
Michael Natterer's avatar
Michael Natterer committed
194
  GimpTool *tool = GIMP_TOOL (bucket_fill_tool);
195

196
  gimp_tool_control_set_scroll_lock     (tool->control, TRUE);
197
  gimp_tool_control_set_wants_click     (tool->control, TRUE);
198
199
  gimp_tool_control_set_tool_cursor     (tool->control,
                                         GIMP_TOOL_CURSOR_BUCKET_FILL);
200
  gimp_tool_control_set_action_opacity  (tool->control,
201
202
203
                                         "context/context-opacity-set");
  gimp_tool_control_set_action_object_1 (tool->control,
                                         "context/context-pattern-select-set");
Jehan's avatar
Jehan committed
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

  bucket_fill_tool->priv = gimp_bucket_fill_tool_get_instance_private (bucket_fill_tool);
}

static void
gimp_bucket_fill_tool_constructed (GObject *object)
{
  GimpTool              *tool    = GIMP_TOOL (object);
  GimpBucketFillOptions *options = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (object);
  Gimp                  *gimp    = GIMP_CONTEXT (options)->gimp;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  /* Avoid computing initial line art several times (for every option
   * property as it gets deserialized) if tool is selected when starting
   * GIMP with an image.
   */
  if (gimp_is_restored (gimp))
    gimp_bucket_fill_tool_connect_handlers (tool);
  else
    g_idle_add (gimp_bucket_fill_tool_connect_handlers, tool);
}

static void
gimp_bucket_fill_tool_finalize (GObject *object)
{
  GimpBucketFillTool    *tool     = GIMP_BUCKET_FILL_TOOL (object);
  GimpBucketFillOptions *options  = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  Gimp                  *gimp     = GIMP_CONTEXT (options)->gimp;
  GimpContext           *context  = gimp_get_user_context (gimp);
  GimpImage             *image    = g_weak_ref_get (&tool->priv->cached_image);
  GimpDrawable          *drawable = g_weak_ref_get (&tool->priv->cached_drawable);

  g_clear_object (&tool->priv->line_art);

  if (image)
    {
      g_signal_handlers_disconnect_by_data (image, tool);
      g_object_unref (image);
    }
  if (drawable)
    {
      g_signal_handlers_disconnect_by_data (drawable, tool);
      g_object_unref (drawable);
    }

  g_signal_handlers_disconnect_by_func (options,
                                        G_CALLBACK (gimp_bucket_fill_tool_options_notified),
                                        tool);
  g_signal_handlers_disconnect_by_data (context, tool);

  G_OBJECT_CLASS (parent_class)->finalize (object);
256
257
}

258
259
260
261
262
static gboolean
gimp_bucket_fill_tool_initialize (GimpTool     *tool,
                                  GimpDisplay  *display,
                                  GError      **error)
{
263
264
  GimpImage    *image    = gimp_display_get_image (display);
  GimpDrawable *drawable = gimp_image_get_active_drawable (image);
265
266
267
268
269
270

  if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }

271
272
273
  if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
Jehan's avatar
Jehan committed
274
                           _("Cannot modify the pixels of layer groups."));
275
276
277
      return FALSE;
    }

Jehan's avatar
Jehan committed
278
  if (! gimp_item_is_visible (GIMP_ITEM (drawable)))
279
280
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
Jehan's avatar
Jehan committed
281
                           _("The active layer is not visible."));
282
283
284
      return FALSE;
    }

Jehan's avatar
Jehan committed
285
  if (gimp_item_is_content_locked (GIMP_ITEM (drawable)))
286
287
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
Jehan's avatar
Jehan committed
288
                           _("The active layer's pixels are locked."));
289
290
291
      return FALSE;
    }

292
293
294
  return TRUE;
}

295
static void
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
gimp_bucket_fill_tool_start (GimpBucketFillTool *tool,
                             const GimpCoords   *coords,
                             GimpDisplay        *display)
{
  GimpBucketFillOptions *options  = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  GimpContext           *context  = GIMP_CONTEXT (options);
  GimpImage             *image    = gimp_display_get_image (display);
  GimpDrawable          *drawable = gimp_image_get_active_drawable (image);

  g_return_if_fail (! tool->priv->filter);

  tool->priv->fill_in_progress = TRUE;

  GIMP_TOOL (tool)->display  = display;
  GIMP_TOOL (tool)->drawable = drawable;

  gimp_bucket_fill_tool_create_graph (tool);

  tool->priv->filter = gimp_drawable_filter_new (drawable, _("Bucket fill"),
                                                 tool->priv->graph,
                                                 GIMP_ICON_TOOL_BUCKET_FILL);

  gimp_drawable_filter_set_region (tool->priv->filter, GIMP_FILTER_REGION_DRAWABLE);

  /* We only set these here, and don't need to update it since we assume
   * the settings can't change while the fill started.
   */
  gimp_drawable_filter_set_mode (tool->priv->filter,
                                 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_filter_set_opacity (tool->priv->filter,
                                    gimp_context_get_opacity (context));

  g_signal_connect (tool->priv->filter, "flush",
                    G_CALLBACK (gimp_bucket_fill_tool_filter_flush),
                    tool);

  if (options->fill_criterion == GIMP_SELECT_CRITERION_LINE_ART &&
      tool->priv->async)
    {
      gimp_waitable_wait (GIMP_WAITABLE (tool->priv->async));
      g_object_unref (tool->priv->async);
      tool->priv->async = NULL;
    }
}

static void
gimp_bucket_fill_tool_preview (GimpBucketFillTool *tool,
                               const GimpCoords   *coords,
                               GimpDisplay        *display,
                               GimpFillOptions    *fill_options)
{
  GimpBucketFillOptions *options  = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  GimpImage             *image    = gimp_display_get_image (display);
  GimpDrawable          *drawable = gimp_image_get_active_drawable (image);

  if (tool->priv->filter)
    {
      GeglBuffer *fill     = NULL;
      GeglBuffer *line_art = NULL;
      gdouble     x        = coords->x;
      gdouble     y        = coords->y;

      if (! options->sample_merged)
        {
          gint off_x, off_y;

          gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);

          x -= (gdouble) off_x;
          y -= (gdouble) off_y;
        }

      if (options->fill_criterion == GIMP_SELECT_CRITERION_LINE_ART)
        line_art = g_object_ref (tool->priv->line_art);

      fill = gimp_drawable_get_bucket_fill_buffer (drawable,
                                                   line_art,
                                                   fill_options,
                                                   options->fill_transparent,
                                                   options->fill_criterion,
                                                   options->threshold / 255.0,
                                                   options->sample_merged,
                                                   options->diagonal_neighbors,
382
383
                                                   options->line_art_threshold,
                                                   options->line_art_erosion,
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
                                                   x, y, &tool->priv->fill_mask,
                                                   &x, &y, NULL, NULL);
      if (line_art)
        g_object_unref (line_art);

      if (fill)
        {
          gegl_node_set (tool->priv->fill_node,
                         "buffer", fill,
                         NULL);
          gegl_node_set (tool->priv->offset_node,
                         "x", x,
                         "y", y,
                         NULL);
          gimp_drawable_filter_apply (tool->priv->filter, NULL);
          g_object_unref (fill);
        }
    }
}

static void
gimp_bucket_fill_tool_commit (GimpBucketFillTool *tool)
{
407
408
  tool->priv->fill_in_progress = FALSE;

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
  if (tool->priv->filter)
    {
      gimp_drawable_filter_commit (tool->priv->filter,
                                   GIMP_PROGRESS (tool), FALSE);
      gimp_image_flush (gimp_display_get_image (GIMP_TOOL (tool)->display));
    }
}

static void
gimp_bucket_fill_tool_halt (GimpBucketFillTool *tool)
{
  if (tool->priv->graph)
    {
      g_clear_object (&tool->priv->graph);
      tool->priv->fill_node      = NULL;
      tool->priv->offset_node    = NULL;
    }
  if (tool->priv->filter)
    {
      gimp_drawable_filter_abort (tool->priv->filter);
      g_clear_object (&tool->priv->filter);
    }
  g_clear_object (&tool->priv->fill_mask);

  tool->priv->fill_in_progress            = FALSE;
  tool->priv->compute_line_art_after_fill = FALSE;

  GIMP_TOOL (tool)->display  = NULL;
  GIMP_TOOL (tool)->drawable = NULL;
}

static void
gimp_bucket_fill_tool_filter_flush (GimpDrawableFilter *filter,
                                    GimpTool           *tool)
{
  GimpImage *image = gimp_display_get_image (tool->display);

  gimp_projection_flush (gimp_image_get_projection (image));
}

static void
gimp_bucket_fill_tool_create_graph (GimpBucketFillTool *tool)
{
  GeglNode *graph;
  GeglNode *output;
  GeglNode *fill_node;
  GeglNode *offset_node;

  g_return_if_fail (! tool->priv->graph     &&
                    ! tool->priv->fill_node &&
                    ! tool->priv->offset_node);

  graph = gegl_node_new ();

  fill_node = gegl_node_new_child (graph,
                                   "operation", "gegl:buffer-source",
                                   NULL);
  offset_node = gegl_node_new_child (graph,
                                     "operation", "gegl:translate",
                                     NULL);
  output = gegl_node_get_output_proxy (graph, "output");
  gegl_node_link_many (fill_node, offset_node, output, NULL);

  tool->priv->graph       = graph;
  tool->priv->fill_node   = fill_node;
  tool->priv->offset_node = offset_node;
}

static void
gimp_bucket_fill_tool_button_press (GimpTool            *tool,
                                    const GimpCoords    *coords,
                                    guint32              time,
                                    GdkModifierType      state,
                                    GimpButtonPressType  press_type,
                                    GimpDisplay         *display)
Elliot Lee's avatar
Elliot Lee committed
484
{
485
486
487
  GimpBucketFillTool    *bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
  GimpBucketFillOptions *options     = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  GimpImage             *image       = gimp_display_get_image (display);
488

489
  if (press_type == GIMP_BUTTON_PRESS_NORMAL &&
490
      gimp_image_coords_in_active_pickable (image, coords,
491
                                            options->sample_merged, TRUE))
Elliot Lee's avatar
Elliot Lee committed
492
    {
493
494
495
496
      GimpDrawable    *drawable = gimp_image_get_active_drawable (image);
      GimpContext     *context  = GIMP_CONTEXT (options);
      GimpFillOptions *fill_options;
      GError          *error = NULL;
497

498
      fill_options = gimp_fill_options_new (image->gimp, NULL, FALSE);
499

500
501
502
      if (gimp_fill_options_set_by_fill_mode (fill_options, context,
                                              options->fill_mode,
                                              &error))
503
        {
504
505
          gimp_fill_options_set_antialias (fill_options, options->antialias);

506
507
508
509
          gimp_context_set_opacity (GIMP_CONTEXT (fill_options),
                                    gimp_context_get_opacity (context));
          gimp_context_set_paint_mode (GIMP_CONTEXT (fill_options),
                                       gimp_context_get_paint_mode (context));
510

511
          if (options->fill_selection)
512
            {
513
              gimp_drawable_edit_fill (drawable, fill_options, NULL);
514
              gimp_image_flush (image);
515
            }
516
          else
517
            {
518
519
              gimp_bucket_fill_tool_start (bucket_tool, coords, display);
              gimp_bucket_fill_tool_preview (bucket_tool, coords, display, fill_options);
520
            }
521
522
523
524
525
526
527
        }
      else
        {
          gimp_message_literal (display->gimp, G_OBJECT (display),
                                GIMP_MESSAGE_WARNING, error->message);
          g_clear_error (&error);
        }
528

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
      g_object_unref (fill_options);
    }

  GIMP_TOOL_CLASS (parent_class)->button_press (tool, coords, time, state,
                                                press_type, display);
}

static void
gimp_bucket_fill_tool_motion (GimpTool         *tool,
                              const GimpCoords *coords,
                              guint32           time,
                              GdkModifierType   state,
                              GimpDisplay      *display)
{
  GimpBucketFillTool    *bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
  GimpBucketFillOptions *options     = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  GimpImage             *image       = gimp_display_get_image (display);

  GIMP_TOOL_CLASS (parent_class)->motion (tool, coords, time, state, display);

  if (gimp_image_coords_in_active_pickable (image, coords,
                                            options->sample_merged, TRUE) &&
      /* Fill selection only needs to happen once. */
      ! options->fill_selection)
    {
      GimpContext     *context  = GIMP_CONTEXT (options);
      GimpFillOptions *fill_options;
      GError          *error = NULL;

      g_return_if_fail (bucket_tool->priv->fill_in_progress);

      fill_options = gimp_fill_options_new (image->gimp, NULL, FALSE);

      if (gimp_fill_options_set_by_fill_mode (fill_options, context,
                                              options->fill_mode,
                                              &error))
        {
          gimp_fill_options_set_antialias (fill_options, options->antialias);

          gimp_context_set_opacity (GIMP_CONTEXT (fill_options),
                                    gimp_context_get_opacity (context));
          gimp_context_set_paint_mode (GIMP_CONTEXT (fill_options),
                                       gimp_context_get_paint_mode (context));

          gimp_bucket_fill_tool_preview (bucket_tool, coords, display, fill_options);
Michael Natterer's avatar
Michael Natterer committed
574
575
576
        }
      else
        {
577
578
579
          gimp_message_literal (display->gimp, G_OBJECT (display),
                                GIMP_MESSAGE_WARNING, error->message);
          g_clear_error (&error);
Michael Natterer's avatar
Michael Natterer committed
580
        }
581
582

      g_object_unref (fill_options);
Elliot Lee's avatar
Elliot Lee committed
583
    }
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
}

static void
gimp_bucket_fill_tool_button_release (GimpTool              *tool,
                                      const GimpCoords      *coords,
                                      guint32                time,
                                      GdkModifierType        state,
                                      GimpButtonReleaseType  release_type,
                                      GimpDisplay           *display)
{
  GimpBucketFillTool *bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
  gboolean            commit;

  commit = (release_type != GIMP_BUTTON_RELEASE_CANCEL);

  if (commit)
    gimp_bucket_fill_tool_commit (bucket_tool);

  gimp_bucket_fill_tool_halt (bucket_tool);
Elliot Lee's avatar
Elliot Lee committed
603

604
605
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Elliot Lee's avatar
Elliot Lee committed
606
607
608
}

static void
609
610
611
612
gimp_bucket_fill_tool_modifier_key (GimpTool        *tool,
                                    GdkModifierType  key,
                                    gboolean         press,
                                    GdkModifierType  state,
613
                                    GimpDisplay     *display)
614
{
615
  GimpBucketFillOptions *options = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
616

617
  if (key == gimp_get_toggle_behavior_mask ())
618
    {
619
      switch (options->fill_mode)
620
        {
621
622
        case GIMP_BUCKET_FILL_FG:
          g_object_set (options, "fill-mode", GIMP_BUCKET_FILL_BG, NULL);
623
          break;
624

625
626
        case GIMP_BUCKET_FILL_BG:
          g_object_set (options, "fill-mode", GIMP_BUCKET_FILL_FG, NULL);
627
          break;
628

629
630
631
632
        default:
          break;
        }
    }
633
  else if (key == gimp_get_extend_selection_mask ())
634
635
636
    {
      g_object_set (options, "fill-selection", ! options->fill_selection, NULL);
    }
637
638
639
}

static void
640
641
642
643
gimp_bucket_fill_tool_cursor_update (GimpTool         *tool,
                                     const GimpCoords *coords,
                                     GdkModifierType   state,
                                     GimpDisplay      *display)
Elliot Lee's avatar
Elliot Lee committed
644
{
645
  GimpBucketFillOptions *options  = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
646
  GimpCursorModifier     modifier = GIMP_CURSOR_MODIFIER_BAD;
647
  GimpImage             *image    = gimp_display_get_image (display);
Elliot Lee's avatar
Elliot Lee committed
648

649
  if (gimp_image_coords_in_active_pickable (image, coords,
650
                                            options->sample_merged, TRUE))
651
    {
652
      GimpDrawable *drawable = gimp_image_get_active_drawable (image);
653

654
      if (! gimp_viewable_get_children (GIMP_VIEWABLE (drawable)) &&
655
656
          ! gimp_item_is_content_locked (GIMP_ITEM (drawable))    &&
          gimp_item_is_visible (GIMP_ITEM (drawable)))
657
658
659
        {
          switch (options->fill_mode)
            {
660
            case GIMP_BUCKET_FILL_FG:
661
662
663
              modifier = GIMP_CURSOR_MODIFIER_FOREGROUND;
              break;

664
            case GIMP_BUCKET_FILL_BG:
665
666
667
              modifier = GIMP_CURSOR_MODIFIER_BACKGROUND;
              break;

668
            case GIMP_BUCKET_FILL_PATTERN:
669
670
671
              modifier = GIMP_CURSOR_MODIFIER_PATTERN;
              break;
            }
672
        }
673
    }
674

675
  gimp_tool_control_set_cursor_modifier (tool->control, modifier);
676

677
  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
Elliot Lee's avatar
Elliot Lee committed
678
}
Jehan's avatar
Jehan committed
679

680
681
682
683
684
typedef struct
{
  GimpBucketFillTool *tool;
  GimpPickable       *pickable;
  gboolean            fill_transparent;
685
686
  gdouble             line_art_threshold;
  gint                line_art_erosion;
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
} PrecomputeData;

static void
precompute_data_free (PrecomputeData *data)
{
  g_object_unref (data->pickable);
  g_object_unref (data->tool);
  g_slice_free (PrecomputeData, data);
}

static void
gimp_bucket_fill_compute_line_art_async  (GimpAsync      *async,
                                          PrecomputeData *data)
{
  GeglBuffer *line_art;

  line_art = gimp_pickable_contiguous_region_prepare_line_art (data->pickable,
704
705
706
                                                               data->fill_transparent,
                                                               data->line_art_threshold,
                                                               data->line_art_erosion);
707
708
709
710
711
712
713
714
715
716
717
  precompute_data_free (data);
  if (gimp_async_is_canceled (async))
    {
      g_object_unref (line_art);
      gimp_async_abort (async);
      return;
    }
  gimp_async_finish (async, line_art);
}

static void
718
gimp_bucket_fill_compute_line_art_cb (GimpAsync          *async,
719
720
721
722
723
724
725
726
727
                                      GimpBucketFillTool *tool)
{
  if (gimp_async_is_canceled (async))
    return;

  if (gimp_async_is_finished (async))
    tool->priv->line_art = gimp_async_get_result (async);
}

Jehan's avatar
Jehan committed
728
729
730
731
732
static void
gimp_bucket_fill_compute_line_art (GimpBucketFillTool *tool)
{
  GimpBucketFillOptions *options = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);

733
734
735
736
737
738
  if (tool->priv->fill_in_progress)
    {
      tool->priv->compute_line_art_after_fill = TRUE;
      return;
    }

Jehan's avatar
Jehan committed
739
740
741
742
743
744
745
746
  g_clear_object (&tool->priv->line_art);
  if (options->fill_criterion == GIMP_SELECT_CRITERION_LINE_ART)
    {
      GimpPickable *pickable = NULL;
      GimpDrawable *image    = g_weak_ref_get (&tool->priv->cached_image);
      GimpDrawable *drawable = g_weak_ref_get (&tool->priv->cached_drawable);

      if (image && options->sample_merged)
747
748
749
750
        {
          pickable = GIMP_PICKABLE (image);
          g_object_unref (drawable);
        }
Jehan's avatar
Jehan committed
751
      else if (drawable && ! options->sample_merged)
752
753
754
755
756
757
758
759
760
        {
          pickable = GIMP_PICKABLE (drawable);
          g_object_unref (image);
        }
      else
        {
          g_object_unref (image);
          g_object_unref (drawable);
        }
Jehan's avatar
Jehan committed
761
762

      if (pickable)
763
764
765
        {
          PrecomputeData *data = g_slice_new (PrecomputeData);

766
767
768
769
770
          data->tool               = g_object_ref (tool);
          data->pickable           = pickable;
          data->fill_transparent   = options->fill_transparent;
          data->line_art_threshold = options->line_art_threshold;
          data->line_art_erosion   = options->line_art_erosion;
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785

          if (tool->priv->async)
            {
              gimp_cancelable_cancel (GIMP_CANCELABLE (tool->priv->async));
              g_object_unref (tool->priv->async);
            }
          tool->priv->async = gimp_parallel_run_async_full (1,
                                                            (GimpParallelRunAsyncFunc) gimp_bucket_fill_compute_line_art_async,
                                                            data, (GDestroyNotify) precompute_data_free);
          gimp_async_add_callback (tool->priv->async,
                                   (GimpAsyncCallback) gimp_bucket_fill_compute_line_art_cb,
                                   tool);
        }
      else
        g_object_unref (pickable);
Jehan's avatar
Jehan committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
    }
}

static gboolean
gimp_bucket_fill_tool_connect_handlers (gpointer data)
{
  GimpBucketFillTool    *tool    = GIMP_BUCKET_FILL_TOOL (data);
  GimpBucketFillOptions *options = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  Gimp                  *gimp    = GIMP_CONTEXT (options)->gimp;

  if (gimp_is_restored (gimp))
    {
      GimpContext *context = gimp_get_user_context (gimp);
      GimpImage   *image   = gimp_context_get_image (context);

      g_signal_connect (options, "notify::fill-criterion",
                        G_CALLBACK (gimp_bucket_fill_tool_options_notified),
                        tool);
      g_signal_connect (options, "notify::sample-merged",
                        G_CALLBACK (gimp_bucket_fill_tool_options_notified),
                        tool);
      g_signal_connect (options, "notify::fill-transparent",
                        G_CALLBACK (gimp_bucket_fill_tool_options_notified),
                        tool);
810
811
812
813
814
815
      g_signal_connect (options, "notify::line-art-erosion",
                        G_CALLBACK (gimp_bucket_fill_tool_options_notified),
                        tool);
      g_signal_connect (options, "notify::line-art-threshold",
                        G_CALLBACK (gimp_bucket_fill_tool_options_notified),
                        tool);
Jehan's avatar
Jehan committed
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

      g_signal_connect (context, "image-changed",
                        G_CALLBACK (gimp_bucket_fill_tool_image_changed),
                        tool);
      gimp_bucket_fill_tool_image_changed (context, image, GIMP_BUCKET_FILL_TOOL (tool));

      return G_SOURCE_REMOVE;
    }
  return G_SOURCE_CONTINUE;
}

static void
gimp_bucket_fill_tool_options_notified (GimpBucketFillOptions *options,
                                        GParamSpec            *pspec,
                                        GimpBucketFillTool    *tool)
{
832
833
834
835
836
  if ((! strcmp (pspec->name, "fill-criterion")     ||
       ! strcmp (pspec->name, "fill-transparent")   ||
       ! strcmp (pspec->name, "line-art-erosion")   ||
       ! strcmp (pspec->name, "line-art-erosion")   ||
       ! strcmp (pspec->name, "line-art-threshold") ||
Jehan's avatar
Jehan committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
       ! strcmp (pspec->name, "sample-merged")) &&
      options->fill_criterion == GIMP_SELECT_CRITERION_LINE_ART)
    {
      gimp_bucket_fill_compute_line_art (tool);
    }
}

static void
gimp_bucket_fill_tool_image_changed (GimpContext        *context,
                                     GimpImage          *image,
                                     GimpBucketFillTool *tool)
{
  GimpImage *prev_image = g_weak_ref_get (&tool->priv->cached_image);

  if (image != prev_image)
    {
      GimpImage *prev_drawable = g_weak_ref_get (&tool->priv->cached_drawable);

      g_clear_object (&tool->priv->line_art);

      if (prev_image)
        g_signal_handlers_disconnect_by_data (prev_image, tool);
      if (prev_drawable)
        {
          g_signal_handlers_disconnect_by_data (prev_drawable, tool);
          g_object_unref (prev_drawable);
        }

      g_weak_ref_set (&tool->priv->cached_image, image ? image : NULL);
      g_weak_ref_set (&tool->priv->cached_drawable, NULL);
      if (image)
        {
          g_signal_connect (image, "active-layer-changed",
                            G_CALLBACK (gimp_bucket_fill_tool_drawable_changed),
                            tool);
          g_signal_connect (image, "active-channel-changed",
                            G_CALLBACK (gimp_bucket_fill_tool_drawable_changed),
                            tool);
          gimp_bucket_fill_tool_drawable_changed (image, tool);
        }
    }
  if (prev_image)
    g_object_unref (prev_image);
}

static void
gimp_bucket_fill_tool_drawable_changed (GimpImage          *image,
                                        GimpBucketFillTool *tool)
{
  GimpDrawable *drawable      = gimp_image_get_active_drawable (image);
  GimpDrawable *prev_drawable = g_weak_ref_get (&tool->priv->cached_drawable);

  if (drawable != prev_drawable)
    {
      if (prev_drawable)
        g_signal_handlers_disconnect_by_data (prev_drawable, tool);

      g_weak_ref_set (&tool->priv->cached_drawable, drawable ? drawable : NULL);
      if (drawable)
        g_signal_connect (drawable, "update",
                          G_CALLBACK (gimp_bucket_fill_tool_drawable_update),
                          tool);

      gimp_bucket_fill_compute_line_art (tool);
    }
  if (prev_drawable)
    g_object_unref (prev_drawable);
}

static void
gimp_bucket_fill_tool_drawable_update (GimpDrawable       *drawable,
                                       gint                x,
                                       gint                y,
                                       gint                width,
                                       gint                height,
                                       GimpBucketFillTool *tool)
{
  gimp_bucket_fill_compute_line_art (tool);
}