~centralelyon2010/inkscape/imagelinks2

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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
/** @file
 * @brief Bucket fill drawing context, works by bitmap filling an area on a rendered version
 * of the current display and then tracing the result using potrace.
 */
/* Author:
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   bulia byak <buliabyak@users.sf.net>
 *   John Bintz <jcoswell@coswellproductions.org>
 *   Jon A. Cruz <jon@joncruz.org>
 *   Abhishek Sharma
 *
 * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
 * Copyright (C) 2000-2005 authors
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <2geom/pathvector.h>
#include <gdk/gdkkeysyms.h>
#include <queue>
#include <deque>
#include <glibmm/i18n.h>

#include "color.h"
#include "context-fns.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-style.h"
#include "display/cairo-utils.h"
#include "display/drawing-context.h"
#include "display/drawing-image.h"
#include "display/drawing-item.h"
#include "display/drawing.h"
#include "display/sp-canvas.h"
#include "document.h"
#include "flood-context.h"
#include "livarot/Path.h"
#include "livarot/Shape.h"
#include "macros.h"
#include "message-context.h"
#include "message-stack.h"
#include "object-edit.h"
#include "preferences.h"
#include "rubberband.h"
#include "selection.h"
#include "shape-editor.h"
#include "sp-defs.h"
#include "sp-item.h"
#include "splivarot.h"
#include "sp-metrics.h"
#include "sp-namedview.h"
#include "sp-object.h"
#include "sp-path.h"
#include "sp-rect.h"
#include "sp-root.h"
#include "svg/svg.h"
#include "trace/imagemap.h"
#include "trace/potrace/inkscape-potrace.h"
#include "trace/trace.h"
#include "xml/node-event-vector.h"
#include "xml/repr.h"

#include "pixmaps/cursor-paintbucket.xpm"

using Inkscape::DocumentUndo;

static void sp_flood_context_class_init(SPFloodContextClass *klass);
static void sp_flood_context_init(SPFloodContext *flood_context);
static void sp_flood_context_dispose(GObject *object);

static void sp_flood_context_setup(SPEventContext *ec);

static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event);
static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);

static void sp_flood_finish(SPFloodContext *rc);

static SPEventContextClass *parent_class;


GType sp_flood_context_get_type()
{
    static GType type = 0;
    if (!type) {
        GTypeInfo info = {
            sizeof(SPFloodContextClass),
            NULL, NULL,
            (GClassInitFunc) sp_flood_context_class_init,
            NULL, NULL,
            sizeof(SPFloodContext),
            4,
            (GInstanceInitFunc) sp_flood_context_init,
            NULL,    /* value_table */
        };
        type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPFloodContext", &info, (GTypeFlags) 0);
    }
    return type;
}

static void sp_flood_context_class_init(SPFloodContextClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;
    SPEventContextClass *event_context_class = (SPEventContextClass *) klass;

    parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);

    object_class->dispose = sp_flood_context_dispose;

    event_context_class->setup = sp_flood_context_setup;
    event_context_class->root_handler  = sp_flood_context_root_handler;
    event_context_class->item_handler  = sp_flood_context_item_handler;
}

static void sp_flood_context_init(SPFloodContext *flood_context)
{
    SPEventContext *event_context = SP_EVENT_CONTEXT(flood_context);

    event_context->cursor_shape = cursor_paintbucket_xpm;
    event_context->hot_x = 11;
    event_context->hot_y = 30;
    event_context->xp = 0;
    event_context->yp = 0;
    event_context->tolerance = 4;
    event_context->within_tolerance = false;
    event_context->item_to_select = NULL;

    flood_context->item = NULL;

    new (&flood_context->sel_changed_connection) sigc::connection();
}

static void sp_flood_context_dispose(GObject *object)
{
    SPFloodContext *rc = SP_FLOOD_CONTEXT(object);
    SPEventContext *ec = SP_EVENT_CONTEXT(object);

    rc->sel_changed_connection.disconnect();
    rc->sel_changed_connection.~connection();

    delete ec->shape_editor;
    ec->shape_editor = NULL;

    /* fixme: This is necessary because we do not grab */
    if (rc->item) {
        sp_flood_finish(rc);
    }

    if (rc->_message_context) {
        delete rc->_message_context;
    }

    G_OBJECT_CLASS(parent_class)->dispose(object);
}

/**
\brief  Callback that processes the "changed" signal on the selection;
destroys old and creates new knotholder
*/
void sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer data)
{
    SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
    SPEventContext *ec = SP_EVENT_CONTEXT(rc);

    ec->shape_editor->unset_item(SH_KNOTHOLDER);
    SPItem *item = selection->singleItem(); 
    ec->shape_editor->set_item(item, SH_KNOTHOLDER);
}

static void sp_flood_context_setup(SPEventContext *ec)
{
    SPFloodContext *rc = SP_FLOOD_CONTEXT(ec);

    if (((SPEventContextClass *) parent_class)->setup) {
        ((SPEventContextClass *) parent_class)->setup(ec);
    }

    ec->shape_editor = new ShapeEditor(ec->desktop);

    SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
    if (item) {
        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
    }

    rc->sel_changed_connection.disconnect();
    rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
        sigc::bind(sigc::ptr_fun(&sp_flood_context_selection_changed), (gpointer)rc)
    );

    rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    if (prefs->getBool("/tools/paintbucket/selcue")) {
        rc->enableSelectionCue();
    }
}

inline static guint32
compose_onto (guint32 px, guint32 bg)
{
    EXTRACT_ARGB32(px, ap,rp,gp,bp)
    EXTRACT_ARGB32(bg, ab,rb,gb,bb)
    guint32 ao,ro,bo,go;

    ao = 255*255 - (255-ap)*(255-bp);  ao = (ao + 127) / 255;
    ro = (255-ap)*rb + rp;             ro = (ro + 127) / 255;
    go = (255-ap)*gb + gp;             go = (go + 127) / 255;
    bo = (255-ap)*bb + bp;             bo = (bo + 127) / 255;

    ASSEMBLE_ARGB32(pxout, ao,ro,go,bo)
    return pxout;
}

/**
 * \brief Get the pointer to a pixel in a pixel buffer.
 * \param px The pixel buffer.
 * \param x The X coordinate.
 * \param y The Y coordinate.
 * \param stride The rowstride of the pixel buffer.
 */
inline guint32 get_pixel(guchar *px, int x, int y, int stride) {
    return *reinterpret_cast<guint32*>(px + y * stride + x * 4);
}

inline unsigned char * get_trace_pixel(guchar *trace_px, int x, int y, int width) {
    return trace_px + (x + y * width);
}

/**
 * \brief Generate the list of trace channel selection entries.
 */
GList * flood_channels_dropdown_items_list() {
    GList *glist = NULL;

    glist = g_list_append (glist, _("Visible Colors"));
    glist = g_list_append (glist, _("Red"));
    glist = g_list_append (glist, _("Green"));
    glist = g_list_append (glist, _("Blue"));
    glist = g_list_append (glist, _("Hue"));
    glist = g_list_append (glist, _("Saturation"));
    glist = g_list_append (glist, _("Lightness"));
    glist = g_list_append (glist, _("Alpha"));

    return glist;
}

/**
 * \brief Generate the list of autogap selection entries.
 */
GList * flood_autogap_dropdown_items_list() {
    GList *glist = NULL;

    glist = g_list_append (glist, (void*) C_("Flood autogap", "None"));
    glist = g_list_append (glist, (void*) C_("Flood autogap", "Small"));
    glist = g_list_append (glist, (void*) C_("Flood autogap", "Medium"));
    glist = g_list_append (glist, (void*) C_("Flood autogap", "Large"));

    return glist;
}

/**
 * \brief Compare a pixel in a pixel buffer with another pixel to determine if a point should be included in the fill operation.
 * \param check The pixel in the pixel buffer to check.
 * \param orig The original selected pixel to use as the fill target color.
 * \param merged_orig_pixel The original pixel merged with the background.
 * \param dtc The desktop background color.
 * \param threshold The fill threshold.
 * \param method The fill method to use as defined in PaintBucketChannels.
 */
static bool compare_pixels(guint32 check, guint32 orig, guint32 merged_orig_pixel, guint32 dtc, int threshold, PaintBucketChannels method)
{
    int diff = 0;
    float hsl_check[3] = {0,0,0}, hsl_orig[3] = {0,0,0};

    EXTRACT_ARGB32(check, ac,rc,gc,bc)
    EXTRACT_ARGB32(orig, ao,ro,go,bo)
    EXTRACT_ARGB32(dtc, ad,rd,gd,bd)
    EXTRACT_ARGB32(merged_orig_pixel, amop,rmop,gmop,bmop)

    if ((method == FLOOD_CHANNELS_H) ||
        (method == FLOOD_CHANNELS_S) ||
        (method == FLOOD_CHANNELS_L)) {
        double dac = ac;
        double dao = ao;
        sp_color_rgb_to_hsl_floatv(hsl_check, rc / dac, gc / dac, bc / dac);
        sp_color_rgb_to_hsl_floatv(hsl_orig, ro / dao, go / dao, bo / dao);
    }
    
    switch (method) {
        case FLOOD_CHANNELS_ALPHA:
            return abs(static_cast<int>(ac) - ao) <= threshold;
        case FLOOD_CHANNELS_R:
            return abs(static_cast<int>(ac ? unpremul_alpha(rc, ac) : 0) - (ao ? unpremul_alpha(ro, ao) : 0)) <= threshold;
        case FLOOD_CHANNELS_G:
            return abs(static_cast<int>(ac ? unpremul_alpha(gc, ac) : 0) - (ao ? unpremul_alpha(go, ao) : 0)) <= threshold;
        case FLOOD_CHANNELS_B:
            return abs(static_cast<int>(ac ? unpremul_alpha(bc, ac) : 0) - (ao ? unpremul_alpha(bo, ao) : 0)) <= threshold;
        case FLOOD_CHANNELS_RGB:
            guint32 amc, rmc, bmc, gmc;
            amc = 255*255 - (255-ac)*(255-ad); amc = (amc + 127) / 255;
            rmc = (255-ac)*rd + rc; rmc = (rmc + 127) / 255;
            gmc = (255-ac)*gd + gc; gmc = (gmc + 127) / 255;
            bmc = (255-ac)*bd + bc; bmc = (bmc + 127) / 255;

            diff += abs(static_cast<int>(amc ? unpremul_alpha(rmc, amc) : 0) - (amop ? unpremul_alpha(rmop, amop) : 0));
            diff += abs(static_cast<int>(amc ? unpremul_alpha(gmc, amc) : 0) - (amop ? unpremul_alpha(gmop, amop) : 0));
            diff += abs(static_cast<int>(amc ? unpremul_alpha(bmc, amc) : 0) - (amop ? unpremul_alpha(bmop, amop) : 0));
            return ((diff / 3) <= ((threshold * 3) / 4));
        
        case FLOOD_CHANNELS_H:
            return ((int)(fabs(hsl_check[0] - hsl_orig[0]) * 100.0) <= threshold);
        case FLOOD_CHANNELS_S:
            return ((int)(fabs(hsl_check[1] - hsl_orig[1]) * 100.0) <= threshold);
        case FLOOD_CHANNELS_L:
            return ((int)(fabs(hsl_check[2] - hsl_orig[2]) * 100.0) <= threshold);
    }
    
    return false;
}

enum {
  PIXEL_CHECKED = 1,
  PIXEL_QUEUED  = 2,
  PIXEL_PAINTABLE = 4,
  PIXEL_NOT_PAINTABLE = 8,
  PIXEL_COLORED = 16
};

static inline bool is_pixel_checked(unsigned char *t) { return (*t & PIXEL_CHECKED) == PIXEL_CHECKED; }
static inline bool is_pixel_queued(unsigned char *t) { return (*t & PIXEL_QUEUED) == PIXEL_QUEUED; }
static inline bool is_pixel_paintability_checked(unsigned char *t) {
  return !((*t & PIXEL_PAINTABLE) == 0) && ((*t & PIXEL_NOT_PAINTABLE) == 0);
}
static inline bool is_pixel_paintable(unsigned char *t) { return (*t & PIXEL_PAINTABLE) == PIXEL_PAINTABLE; }
static inline bool is_pixel_colored(unsigned char *t) { return (*t & PIXEL_COLORED) == PIXEL_COLORED; }

static inline void mark_pixel_checked(unsigned char *t) { *t |= PIXEL_CHECKED; }
static inline void mark_pixel_unchecked(unsigned char *t) { *t ^= PIXEL_CHECKED; }
static inline void mark_pixel_queued(unsigned char *t) { *t |= PIXEL_QUEUED; }
static inline void mark_pixel_paintable(unsigned char *t) { *t |= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }
static inline void mark_pixel_not_paintable(unsigned char *t) { *t |= PIXEL_NOT_PAINTABLE; *t ^= PIXEL_PAINTABLE; }
static inline void mark_pixel_colored(unsigned char *t) { *t |= PIXEL_COLORED; }

static inline void clear_pixel_paintability(unsigned char *t) { *t ^= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }

struct bitmap_coords_info {
    bool is_left;
    unsigned int x;
    unsigned int y;
    int y_limit;
    unsigned int width;
    unsigned int height;
    unsigned int stride;
    unsigned int threshold;
    unsigned int radius;
    PaintBucketChannels method;
    guint32 dtc;
    guint32 merged_orig_pixel;
    Geom::Rect bbox;
    Geom::Rect screen;
    unsigned int max_queue_size;
    unsigned int current_step;
};

/**
 * \brief Check if a pixel can be included in the fill.
 * \param px The rendered pixel buffer to check.
 * \param trace_t The pixel in the trace pixel buffer to check or mark.
 * \param x The X coordinate.
 * \param y The y coordinate.
 * \param orig_color The original selected pixel to use as the fill target color.
 * \param bci The bitmap_coords_info structure.
 */
inline static bool check_if_pixel_is_paintable(guchar *px, unsigned char *trace_t, int x, int y, guint32 orig_color, bitmap_coords_info bci) {
    if (is_pixel_paintability_checked(trace_t)) {
        return is_pixel_paintable(trace_t);
    } else {
        guint32 pixel = get_pixel(px, x, y, bci.stride);
        if (compare_pixels(pixel, orig_color, bci.merged_orig_pixel, bci.dtc, bci.threshold, bci.method)) {
            mark_pixel_paintable(trace_t);
            return true;
        } else {
            mark_pixel_not_paintable(trace_t);
            return false;
        }
    }
}

/**
 * \brief Perform the bitmap-to-vector tracing and place the traced path onto the document.
 * \param px The trace pixel buffer to trace to SVG.
 * \param desktop The desktop on which to place the final SVG path.
 * \param transform The transform to apply to the final SVG path.
 * \param union_with_selection If true, merge the final SVG path with the current selection.
 */
static void do_trace(bitmap_coords_info bci, guchar *trace_px, SPDesktop *desktop, Geom::Affine transform, unsigned int min_x, unsigned int max_x, unsigned int min_y, unsigned int max_y, bool union_with_selection) {
    SPDocument *document = sp_desktop_document(desktop);

    unsigned char *trace_t;

    GrayMap *gray_map = GrayMapCreate((max_x - min_x + 1), (max_y - min_y + 1));
    unsigned int gray_map_y = 0;
    for (unsigned int y = min_y; y <= max_y; y++) {
        unsigned long *gray_map_t = gray_map->rows[gray_map_y];

        trace_t = get_trace_pixel(trace_px, min_x, y, bci.width);
        for (unsigned int x = min_x; x <= max_x; x++) {
            *gray_map_t = is_pixel_colored(trace_t) ? GRAYMAP_BLACK : GRAYMAP_WHITE;
            gray_map_t++;
            trace_t++;
        }
        gray_map_y++;
    }

    Inkscape::Trace::Potrace::PotraceTracingEngine pte;
    pte.keepGoing = 1;
    std::vector<Inkscape::Trace::TracingEngineResult> results = pte.traceGrayMap(gray_map);
    gray_map->destroy(gray_map);

    //XML Tree being used here directly while it shouldn't be...."
    Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();

    long totalNodeCount = 0L;

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    double offset = prefs->getDouble("/tools/paintbucket/offset", 0.0);

    for (unsigned int i=0 ; i<results.size() ; i++) {
        Inkscape::Trace::TracingEngineResult result = results[i];
        totalNodeCount += result.getNodeCount();

        Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
        /* Set style */
        sp_desktop_apply_style_tool (desktop, pathRepr, "/tools/paintbucket", false);

        Geom::PathVector pathv = sp_svg_read_pathv(result.getPathData().c_str());
        Path *path = new Path;
        path->LoadPathVector(pathv);

        if (offset != 0) {
        
            Shape *path_shape = new Shape();
        
            path->ConvertWithBackData(0.03);
            path->Fill(path_shape, 0);
            delete path;
        
            Shape *expanded_path_shape = new Shape();
        
            expanded_path_shape->ConvertToShape(path_shape, fill_nonZero);
            path_shape->MakeOffset(expanded_path_shape, offset * desktop->current_zoom(), join_round, 4);
            expanded_path_shape->ConvertToShape(path_shape, fill_positive);

            Path *expanded_path = new Path();
        
            expanded_path->Reset();
            expanded_path_shape->ConvertToForme(expanded_path);
            expanded_path->ConvertEvenLines(1.0);
            expanded_path->Simplify(1.0);
        
            delete path_shape;
            delete expanded_path_shape;
        
            gchar *str = expanded_path->svg_dump_path();
            if (str && *str) {
                pathRepr->setAttribute("d", str);
                g_free(str);
            } else {
                desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Too much inset</b>, the result is empty."));
                Inkscape::GC::release(pathRepr);
                g_free(str);
                return;
            }

            delete expanded_path;

        } else {
            gchar *str = path->svg_dump_path();
            delete path;
            pathRepr->setAttribute("d", str);
            g_free(str);
        }

        desktop->currentLayer()->addChild(pathRepr,NULL);

        SPObject *reprobj = document->getObjectByRepr(pathRepr);
        if (reprobj) {
            SP_ITEM(reprobj)->doWriteTransform(pathRepr, transform, NULL);
            
            // premultiply the item transform by the accumulated parent transform in the paste layer
            Geom::Affine local (SP_GROUP(desktop->currentLayer())->i2doc_affine());
            if (!local.isIdentity()) {
                gchar const *t_str = pathRepr->attribute("transform");
                Geom::Affine item_t (Geom::identity());
                if (t_str)
                    sp_svg_transform_read(t_str, &item_t);
                item_t *= local.inverse();
                // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
                gchar *affinestr=sp_svg_transform_write(item_t);
                pathRepr->setAttribute("transform", affinestr);
                g_free(affinestr);
            }

            Inkscape::Selection *selection = sp_desktop_selection(desktop);

            pathRepr->setPosition(-1);

            if (union_with_selection) {
                desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, ngettext("Area filled, path with <b>%d</b> node created and unioned with selection.","Area filled, path with <b>%d</b> nodes created and unioned with selection.",sp_nodes_in_path(SP_PATH(reprobj))), sp_nodes_in_path(SP_PATH(reprobj)));
                selection->add(reprobj);
                sp_selected_path_union_skip_undo(desktop);
            } else {
                desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, ngettext("Area filled, path with <b>%d</b> node created.","Area filled, path with <b>%d</b> nodes created.",sp_nodes_in_path(SP_PATH(reprobj))), sp_nodes_in_path(SP_PATH(reprobj)));
                selection->set(reprobj);
            }

        }

        Inkscape::GC::release(pathRepr);

    }
}

/**
 * \brief The possible return states of perform_bitmap_scanline_check()
 */
enum ScanlineCheckResult {
    SCANLINE_CHECK_OK,
    SCANLINE_CHECK_ABORTED,
    SCANLINE_CHECK_BOUNDARY
};

/**
 * \brief Determine if the provided coordinates are within the pixel buffer limits.
 * \param x The X coordinate.
 * \param y The Y coordinate.
 * \param bci The bitmap_coords_info structure.
 */
inline static bool coords_in_range(unsigned int x, unsigned int y, bitmap_coords_info bci) {
    return (x < bci.width) &&
           (y < bci.height);
}

#define PAINT_DIRECTION_LEFT 1
#define PAINT_DIRECTION_RIGHT 2
#define PAINT_DIRECTION_UP 4
#define PAINT_DIRECTION_DOWN 8
#define PAINT_DIRECTION_ALL 15

/**
 * \brief Paint a pixel or a square (if autogap is enabled) on the trace pixel buffer
 * \param px The rendered pixel buffer to check.
 * \param trace_px The trace pixel buffer.
 * \param orig_color The original selected pixel to use as the fill target color.
 * \param bci The bitmap_coords_info structure.
 * \param original_point_trace_t The original pixel in the trace pixel buffer to check.
 */
inline static unsigned int paint_pixel(guchar *px, guchar *trace_px, guint32 orig_color, bitmap_coords_info bci, unsigned char *original_point_trace_t) {
    if (bci.radius == 0) {
        mark_pixel_colored(original_point_trace_t); 
        return PAINT_DIRECTION_ALL;
    } else {
        unsigned char *trace_t;
  
        bool can_paint_up = true;
        bool can_paint_down = true;
        bool can_paint_left = true;
        bool can_paint_right = true;
      
        for (unsigned int ty = bci.y - bci.radius; ty <= bci.y + bci.radius; ty++) {
            for (unsigned int tx = bci.x - bci.radius; tx <= bci.x + bci.radius; tx++) {
                if (coords_in_range(tx, ty, bci)) {
                    trace_t = get_trace_pixel(trace_px, tx, ty, bci.width);
                    if (!is_pixel_colored(trace_t)) {
                        if (check_if_pixel_is_paintable(px, trace_t, tx, ty, orig_color, bci)) {
                            mark_pixel_colored(trace_t); 
                        } else {
                            if (tx < bci.x) { can_paint_left = false; }
                            if (tx > bci.x) { can_paint_right = false; }
                            if (ty < bci.y) { can_paint_up = false; }
                            if (ty > bci.y) { can_paint_down = false; }
                        }
                    }
                }
            }
        }
    
        unsigned int paint_directions = 0;
        if (can_paint_left) { paint_directions += PAINT_DIRECTION_LEFT; }
        if (can_paint_right) { paint_directions += PAINT_DIRECTION_RIGHT; }
        if (can_paint_up) { paint_directions += PAINT_DIRECTION_UP; }
        if (can_paint_down) { paint_directions += PAINT_DIRECTION_DOWN; }
        
        return paint_directions;
    }
}

/**
 * \brief Push a point to be checked onto the bottom of the rendered pixel buffer check queue.
 * \param fill_queue The fill queue to add the point to.
 * \param max_queue_size The maximum size of the fill queue.
 * \param trace_t The trace pixel buffer pixel.
 * \param x The X coordinate.
 * \param y The Y coordinate.
 */
static void push_point_onto_queue(std::deque<Geom::Point> *fill_queue, unsigned int max_queue_size, unsigned char *trace_t, unsigned int x, unsigned int y) {
    if (!is_pixel_queued(trace_t)) {
        if ((fill_queue->size() < max_queue_size)) {
            fill_queue->push_back(Geom::Point(x, y));
            mark_pixel_queued(trace_t);
        }
    }
}

/**
 * \brief Shift a point to be checked onto the top of the rendered pixel buffer check queue.
 * \param fill_queue The fill queue to add the point to.
 * \param max_queue_size The maximum size of the fill queue.
 * \param trace_t The trace pixel buffer pixel.
 * \param x The X coordinate.
 * \param y The Y coordinate.
 */
static void shift_point_onto_queue(std::deque<Geom::Point> *fill_queue, unsigned int max_queue_size, unsigned char *trace_t, unsigned int x, unsigned int y) {
    if (!is_pixel_queued(trace_t)) {
        if ((fill_queue->size() < max_queue_size)) {
            fill_queue->push_front(Geom::Point(x, y));
            mark_pixel_queued(trace_t);
        }
    }
}

/**
 * \brief Scan a row in the rendered pixel buffer and add points to the fill queue as necessary.
 * \param fill_queue The fill queue to add the point to.
 * \param px The rendered pixel buffer.
 * \param trace_px The trace pixel buffer.
 * \param orig_color The original selected pixel to use as the fill target color.
 * \param bci The bitmap_coords_info structure.
 */
static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<Geom::Point> *fill_queue, guchar *px, guchar *trace_px, guint32 orig_color, bitmap_coords_info bci, unsigned int *min_x, unsigned int *max_x) {
    bool aborted = false;
    bool reached_screen_boundary = false;
    bool ok;

    bool keep_tracing;
    bool initial_paint = true;

    unsigned char *current_trace_t = get_trace_pixel(trace_px, bci.x, bci.y, bci.width);
    unsigned int paint_directions;

    bool currently_painting_top = false;
    bool currently_painting_bottom = false;

    unsigned int top_ty = bci.y - 1;
    unsigned int bottom_ty = bci.y + 1;

    bool can_paint_top = (top_ty > 0);
    bool can_paint_bottom = (bottom_ty < bci.height);

    Geom::Point t = fill_queue->front();

    do {
        ok = false;
        if (bci.is_left) {
            keep_tracing = (bci.x != 0);
        } else {
            keep_tracing = (bci.x < bci.width);
        }

        *min_x = MIN(*min_x, bci.x);
        *max_x = MAX(*max_x, bci.x);

        if (keep_tracing) {
            if (check_if_pixel_is_paintable(px, current_trace_t, bci.x, bci.y, orig_color, bci)) {
                paint_directions = paint_pixel(px, trace_px, orig_color, bci, current_trace_t);
                if (bci.radius == 0) {
                    mark_pixel_checked(current_trace_t);
                    if ((t[Geom::X] == bci.x) && (t[Geom::Y] == bci.y)) {
                        fill_queue->pop_front(); t = fill_queue->front();
                    }
                }

                if (can_paint_top) {
                    if (paint_directions & PAINT_DIRECTION_UP) { 
                        unsigned char *trace_t = current_trace_t - bci.width;
                        if (!is_pixel_queued(trace_t)) {
                            bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, top_ty, orig_color, bci);

                            if (initial_paint) { currently_painting_top = !ok_to_paint; }

                            if (ok_to_paint && (!currently_painting_top)) {
                                currently_painting_top = true;
                                push_point_onto_queue(fill_queue, bci.max_queue_size, trace_t, bci.x, top_ty);
                            }
                            if ((!ok_to_paint) && currently_painting_top) {
                                currently_painting_top = false;
                            }
                        }
                    }
                }

                if (can_paint_bottom) {
                    if (paint_directions & PAINT_DIRECTION_DOWN) { 
                        unsigned char *trace_t = current_trace_t + bci.width;
                        if (!is_pixel_queued(trace_t)) {
                            bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, bottom_ty, orig_color, bci);

                            if (initial_paint) { currently_painting_bottom = !ok_to_paint; }

                            if (ok_to_paint && (!currently_painting_bottom)) {
                                currently_painting_bottom = true;
                                push_point_onto_queue(fill_queue, bci.max_queue_size, trace_t, bci.x, bottom_ty);
                            }
                            if ((!ok_to_paint) && currently_painting_bottom) {
                                currently_painting_bottom = false;
                            }
                        }
                    }
                }

                if (bci.is_left) {
                    if (paint_directions & PAINT_DIRECTION_LEFT) {
                        bci.x--; current_trace_t--;
                        ok = true;
                    }
                } else {
                    if (paint_directions & PAINT_DIRECTION_RIGHT) {
                        bci.x++; current_trace_t++;
                        ok = true;
                    }
                }

                initial_paint = false;
            }
        } else {
            if (bci.bbox.min()[Geom::X] > bci.screen.min()[Geom::X]) {
                aborted = true; break;
            } else {
                reached_screen_boundary = true;
            }
        }
    } while (ok);

    if (aborted) { return SCANLINE_CHECK_ABORTED; }
    if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
    return SCANLINE_CHECK_OK;
}

/**
 * \brief Sort the rendered pixel buffer check queue vertically.
 */
static bool sort_fill_queue_vertical(Geom::Point a, Geom::Point b) {
    return a[Geom::Y] > b[Geom::Y];
}

/**
 * \brief Sort the rendered pixel buffer check queue horizontally.
 */
static bool sort_fill_queue_horizontal(Geom::Point a, Geom::Point b) {
    return a[Geom::X] > b[Geom::X];
}

/**
 * \brief Perform a flood fill operation.
 * \param event_context The event context for this tool.
 * \param event The details of this event.
 * \param union_with_selection If true, union the new fill with the current selection.
 * \param is_point_fill If false, use the Rubberband "touch selection" to get the initial points for the fill.
 * \param is_touch_fill If true, use only the initial contact point in the Rubberband "touch selection" as the fill target color.
 */
static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event, bool union_with_selection, bool is_point_fill, bool is_touch_fill) {
    SPDesktop *desktop = event_context->desktop;
    SPDocument *document = sp_desktop_document(desktop);

    document->ensureUpToDate();
    
    Geom::OptRect bbox = document->getRoot()->visualBounds();

    if (!bbox) {
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
        return;
    }
    
    double zoom_scale = desktop->current_zoom();
    
    // Render 160% of the physical display to the render pixel buffer, so that available
    // fill areas off the screen can be included in the fill.
    double padding = 1.6;

    Geom::Rect screen = desktop->get_display_area();

    unsigned int width = (int)ceil(screen.width() * zoom_scale * padding);
    unsigned int height = (int)ceil(screen.height() * zoom_scale * padding);

    Geom::Point origin(screen.min()[Geom::X],
                       document->getHeight() - screen.height() - screen.min()[Geom::Y]);
                    
    origin[Geom::X] += (screen.width() * ((1 - padding) / 2));
    origin[Geom::Y] += (screen.height() * ((1 - padding) / 2));
    
    Geom::Scale scale(zoom_scale, zoom_scale);
    Geom::Affine affine = scale * Geom::Translate(-origin * scale);

    int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
    guchar *px = g_new(guchar, stride * height);
    guint32 bgcolor, dtc;

    { // this block limits the lifetime of Drawing and DrawingContext
        /* Create DrawingItems and set transform */
        unsigned dkey = SPItem::display_key_new(1);
        Inkscape::Drawing drawing;
        Inkscape::DrawingItem *root = document->getRoot()->invoke_show( drawing, dkey, SP_ITEM_SHOW_DISPLAY);
        root->setTransform(affine);
        drawing.setRoot(root);

        Geom::IntRect final_bbox = Geom::IntRect::from_xywh(0, 0, width, height);
        drawing.update(final_bbox);

        cairo_surface_t *s = cairo_image_surface_create_for_data(
            px, CAIRO_FORMAT_ARGB32, width, height, stride);
        Inkscape::DrawingContext ct(s, Geom::Point(0,0));
        // cairo_translate not necessary here - surface origin is at 0,0

        SPNamedView *nv = sp_desktop_namedview(desktop);
        bgcolor = nv->pagecolor;
        // bgcolor is 0xrrggbbaa, we need 0xaarrggbb
        dtc = (bgcolor >> 8) | (bgcolor << 24);

        ct.setSource(bgcolor);
        ct.setOperator(CAIRO_OPERATOR_SOURCE);
        ct.paint();
        ct.setOperator(CAIRO_OPERATOR_OVER);

        drawing.render(ct, final_bbox);

        cairo_surface_flush(s);
        cairo_surface_destroy(s);
        
        // Hide items
        document->getRoot()->invoke_hide(dkey);
    }

    guchar *trace_px = g_new(guchar, width * height);
    memset(trace_px, 0x00, width * height);
    
    std::deque<Geom::Point> fill_queue;
    std::queue<Geom::Point> color_queue;
    
    std::vector<Geom::Point> fill_points;
    
    bool aborted = false;
    int y_limit = height - 1;

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    PaintBucketChannels method = (PaintBucketChannels) prefs->getInt("/tools/paintbucket/channels", 0);
    int threshold = prefs->getIntLimited("/tools/paintbucket/threshold", 1, 0, 100);

    switch(method) {
        case FLOOD_CHANNELS_ALPHA:
        case FLOOD_CHANNELS_RGB:
        case FLOOD_CHANNELS_R:
        case FLOOD_CHANNELS_G:
        case FLOOD_CHANNELS_B:
            threshold = (255 * threshold) / 100;
            break;
        case FLOOD_CHANNELS_H:
        case FLOOD_CHANNELS_S:
        case FLOOD_CHANNELS_L:
            break;
    }

    bitmap_coords_info bci;
    
    bci.y_limit = y_limit;
    bci.width = width;
    bci.height = height;
    bci.stride = stride;
    bci.threshold = threshold;
    bci.method = method;
    bci.bbox = *bbox;
    bci.screen = screen;
    bci.dtc = dtc;
    bci.radius = prefs->getIntLimited("/tools/paintbucket/autogap", 0, 0, 3);
    bci.max_queue_size = (width * height) / 4;
    bci.current_step = 0;

    if (is_point_fill) {
        fill_points.push_back(Geom::Point(event->button.x, event->button.y));
    } else {
        Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
        fill_points = r->getPoints();
    }

    for (unsigned int i = 0; i < fill_points.size(); i++) {
        Geom::Point pw = Geom::Point(fill_points[i][Geom::X] / zoom_scale, document->getHeight() + (fill_points[i][Geom::Y] / zoom_scale)) * affine;

        pw[Geom::X] = (int)MIN(width - 1, MAX(0, pw[Geom::X]));
        pw[Geom::Y] = (int)MIN(height - 1, MAX(0, pw[Geom::Y]));

        if (is_touch_fill) {
            if (i == 0) {
                color_queue.push(pw);
            } else {
                unsigned char *trace_t = get_trace_pixel(trace_px, (int)pw[Geom::X], (int)pw[Geom::Y], width);
                push_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, (int)pw[Geom::X], (int)pw[Geom::Y]);
            }
        } else {
            color_queue.push(pw);
        }
    }

    bool reached_screen_boundary = false;

    bool first_run = true;

    unsigned long sort_size_threshold = 5;

    unsigned int min_y = height;
    unsigned int max_y = 0;
    unsigned int min_x = width;
    unsigned int max_x = 0;

    while (!color_queue.empty() && !aborted) {
        Geom::Point color_point = color_queue.front();
        color_queue.pop();

        int cx = (int)color_point[Geom::X];
        int cy = (int)color_point[Geom::Y];

        guint32 orig_color = get_pixel(px, cx, cy, stride);
        bci.merged_orig_pixel = compose_onto(orig_color, dtc);

        unsigned char *trace_t = get_trace_pixel(trace_px, cx, cy, width);
        if (!is_pixel_checked(trace_t) && !is_pixel_colored(trace_t)) {
            if (check_if_pixel_is_paintable(px, trace_px, cx, cy, orig_color, bci)) {
                shift_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, cx, cy);

                if (!first_run) {
                    for (unsigned int y = 0; y < height; y++) {
                        trace_t = get_trace_pixel(trace_px, 0, y, width);
                        for (unsigned int x = 0; x < width; x++) {
                            clear_pixel_paintability(trace_t);
                            trace_t++;
                        }
                    }
                }
                first_run = false;
            }
        }

        unsigned long old_fill_queue_size = fill_queue.size();

        while (!fill_queue.empty() && !aborted) {
            Geom::Point cp = fill_queue.front();

            if (bci.radius == 0) {
                unsigned long new_fill_queue_size = fill_queue.size();

                /*
                 * To reduce the number of points in the fill queue, periodically
                 * resort all of the points in the queue so that scanline checks
                 * can complete more quickly.  A point cannot be checked twice
                 * in a normal scanline checks, so forcing scanline checks to start
                 * from one corner of the rendered area as often as possible
                 * will reduce the number of points that need to be checked and queued.
                 */
                if (new_fill_queue_size > sort_size_threshold) {
                    if (new_fill_queue_size > old_fill_queue_size) {
                        std::sort(fill_queue.begin(), fill_queue.end(), sort_fill_queue_vertical);

                        std::deque<Geom::Point>::iterator start_sort = fill_queue.begin();
                        std::deque<Geom::Point>::iterator end_sort = fill_queue.begin();
                        unsigned int sort_y = (unsigned int)cp[Geom::Y];
                        unsigned int current_y = sort_y;
                        
                        for (std::deque<Geom::Point>::iterator i = fill_queue.begin(); i != fill_queue.end(); i++) {
                            Geom::Point current = *i;
                            current_y = (unsigned int)current[Geom::Y];
                            if (current_y != sort_y) {
                                if (start_sort != end_sort) {
                                    std::sort(start_sort, end_sort, sort_fill_queue_horizontal);
                                }
                                sort_y = current_y;
                                start_sort = i;
                            }
                            end_sort = i;
                        }
                        if (start_sort != end_sort) {
                            std::sort(start_sort, end_sort, sort_fill_queue_horizontal);
                        }
                        
                        cp = fill_queue.front();
                    }
                }

                old_fill_queue_size = new_fill_queue_size;
            }

            fill_queue.pop_front();

            int x = (int)cp[Geom::X];
            int y = (int)cp[Geom::Y];

            min_y = MIN((unsigned int)y, min_y);
            max_y = MAX((unsigned int)y, max_y);

            unsigned char *trace_t = get_trace_pixel(trace_px, x, y, width);
            if (!is_pixel_checked(trace_t)) {
                mark_pixel_checked(trace_t);

                if (y == 0) {
                    if (bbox->min()[Geom::Y] > screen.min()[Geom::Y]) {
                        aborted = true; break;
                    } else {
                        reached_screen_boundary = true;
                    }
                }

                if (y == y_limit) {
                    if (bbox->max()[Geom::Y] < screen.max()[Geom::Y]) {
                        aborted = true; break;
                    } else {
                        reached_screen_boundary = true;
                    }
                }

                bci.is_left = true;
                bci.x = x;
                bci.y = y;

                ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci, &min_x, &max_x);

                switch (result) {
                    case SCANLINE_CHECK_ABORTED:
                        aborted = true;
                        break;
                    case SCANLINE_CHECK_BOUNDARY:
                        reached_screen_boundary = true;
                        break;
                    default:
                        break;
                }

                if (bci.x < width) {
                    trace_t++;
                    if (!is_pixel_checked(trace_t) && !is_pixel_queued(trace_t)) {
                        mark_pixel_checked(trace_t);
                        bci.is_left = false;
                        bci.x = x + 1;

                        result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci, &min_x, &max_x);

                        switch (result) {
                            case SCANLINE_CHECK_ABORTED:
                                aborted = true;
                                break;
                            case SCANLINE_CHECK_BOUNDARY:
                                reached_screen_boundary = true;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }

            bci.current_step++;

            if (bci.current_step > bci.max_queue_size) {
                aborted = true;
            }
        }
    }
    
    g_free(px);
    
    if (aborted) {
        g_free(trace_px);
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
        return;
    }
    
    if (reached_screen_boundary) {
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Only the visible part of the bounded area was filled.</b> If you want to fill all of the area, undo, zoom out, and fill again.")); 
    }

    unsigned int trace_padding = bci.radius + 1;
    if (min_y > trace_padding) { min_y -= trace_padding; }
    if (max_y < (y_limit - trace_padding)) { max_y += trace_padding; }
    if (min_x > trace_padding) { min_x -= trace_padding; }
    if (max_x < (width - 1 - trace_padding)) { max_x += trace_padding; }

    Geom::Point min_start = Geom::Point(min_x, min_y);
    
    affine = scale * Geom::Translate(-origin * scale - min_start);
    Geom::Affine inverted_affine = Geom::Affine(affine).inverse();
    
    do_trace(bci, trace_px, desktop, inverted_affine, min_x, max_x, min_y, max_y, union_with_selection);

    g_free(trace_px);
    
    DocumentUndo::done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
}

static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
{
    gint ret = FALSE;

    SPDesktop *desktop = event_context->desktop;

    switch (event->type) {
    case GDK_BUTTON_PRESS:
        if ((event->button.state & GDK_CONTROL_MASK) && event->button.button == 1 && !event_context->space_panning) {
            Geom::Point const button_w(event->button.x,
                                       event->button.y);
            
            SPItem *item = sp_event_context_find_item (desktop, button_w, TRUE, TRUE);
            
            // Set style
            desktop->applyCurrentOrToolStyle(item, "/tools/paintbucket", false);
            DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object"));
            ret = TRUE;
        }
        break;
    default:
        break;
    }

    if (((SPEventContextClass *) parent_class)->item_handler) {
        ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
    }

    return ret;
}

static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
{
    static bool dragging;
    
    gint ret = FALSE;
    SPDesktop *desktop = event_context->desktop;

    switch (event->type) {
    case GDK_BUTTON_PRESS:
        if (event->button.button == 1 && !event_context->space_panning) {
            if (!(event->button.state & GDK_CONTROL_MASK)) {
                Geom::Point const button_w(event->button.x,
                                           event->button.y);
    
                if (Inkscape::have_viable_layer(desktop, event_context->defaultMessageContext())) {
                    // save drag origin
                    event_context->xp = (gint) button_w[Geom::X];
                    event_context->yp = (gint) button_w[Geom::Y];
                    event_context->within_tolerance = true;
                      
                    dragging = true;
                    
                    Geom::Point const p(desktop->w2d(button_w));
                    Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
                    Inkscape::Rubberband::get(desktop)->start(desktop, p);
                }
            }
        }
    case GDK_MOTION_NOTIFY:
        if ( dragging
             && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning)
        {
            if ( event_context->within_tolerance
                 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
                 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
                break; // do not drag if we're within tolerance from origin
            }
            
            event_context->within_tolerance = false;
            
            Geom::Point const motion_pt(event->motion.x, event->motion.y);
            Geom::Point const p(desktop->w2d(motion_pt));
            if (Inkscape::Rubberband::get(desktop)->is_started()) {
                Inkscape::Rubberband::get(desktop)->move(p);
                event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> areas to add to fill, hold <b>Alt</b> for touch fill"));
                gobble_motion_events(GDK_BUTTON1_MASK);
            }
        }
        break;

    case GDK_BUTTON_RELEASE:
        if (event->button.button == 1 && !event_context->space_panning) {
            Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
            if (r->is_started()) {
                // set "busy" cursor
                desktop->setWaitingCursor();

                if (SP_IS_EVENT_CONTEXT(event_context)) { 
                    // Since setWaitingCursor runs main loop iterations, we may have already left this tool!
                    // So check if the tool is valid before doing anything
                    dragging = false;

                    bool is_point_fill = event_context->within_tolerance;
                    bool is_touch_fill = event->button.state & GDK_MOD1_MASK;
                    
                    sp_flood_do_flood_fill(event_context, event, event->button.state & GDK_SHIFT_MASK, is_point_fill, is_touch_fill);
                    
                    desktop->clearWaitingCursor();
                    // restore cursor when done; note that it may already be different if e.g. user 
                    // switched to another tool during interruptible tracing or drawing, in which case do nothing

                    ret = TRUE;
                }

                r->stop();

                if (SP_IS_EVENT_CONTEXT(event_context)) {
                    event_context->defaultMessageContext()->clear();
                }
            }
        }
        break;
    case GDK_KEY_PRESS:
        switch (get_group0_keyval (&event->key)) {
        case GDK_Up:
        case GDK_Down:
        case GDK_KP_Up:
        case GDK_KP_Down:
            // prevent the zoom field from activation
            if (!MOD__CTRL_ONLY)
                ret = TRUE;
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }

    if (!ret) {
        if (((SPEventContextClass *) parent_class)->root_handler) {
            ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
        }
    }

    return ret;
}


static void sp_flood_finish(SPFloodContext *rc)
{
    rc->_message_context->clear();

    if ( rc->item != NULL ) {
        SPDesktop * desktop;

        desktop = SP_EVENT_CONTEXT_DESKTOP(rc);

        SP_OBJECT(rc->item)->updateRepr();

        sp_canvas_end_forced_full_redraws(desktop->canvas);

        sp_desktop_selection(desktop)->set(rc->item);
        DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
                        _("Fill bounded area"));

        rc->item = NULL;
    }
}

void flood_channels_set_channels( gint channels )
{
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    prefs->setInt("/tools/paintbucket/channels", channels);
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :