Code

show waiting cursor while ExecutionEnv is working
[inkscape.git] / src / flood-context.cpp
1 #define __SP_FLOOD_CONTEXT_C__
3 /** @file
4  * @brief Bucket fill drawing context, works by bitmap filling an area on a rendered version
5  * of the current display and then tracing the result using potrace.
6  */
7 /* Author:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *   John Bintz <jcoswell@coswellproductions.org>
11  *
12  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
13  * Copyright (C) 2000-2005 authors
14  * Copyright (C) 2000-2001 Ximian, Inc.
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include <gdk/gdkkeysyms.h>
24 #include <queue>
25 #include <deque>
27 #include "macros.h"
28 #include "display/sp-canvas.h"
29 #include "document.h"
30 #include "sp-namedview.h"
31 #include "sp-object.h"
32 #include "sp-rect.h"
33 #include "selection.h"
34 #include "desktop-handles.h"
35 #include "desktop.h"
36 #include "desktop-style.h"
37 #include "message-stack.h"
38 #include "message-context.h"
39 #include "pixmaps/cursor-paintbucket.xpm"
40 #include "flood-context.h"
41 #include "sp-metrics.h"
42 #include <glibmm/i18n.h>
43 #include "object-edit.h"
44 #include "xml/repr.h"
45 #include "xml/node-event-vector.h"
46 #include "preferences.h"
47 #include "context-fns.h"
48 #include "rubberband.h"
49 #include "shape-editor.h"
51 #include "display/nr-arena-item.h"
52 #include "display/nr-arena.h"
53 #include "display/nr-arena-image.h"
54 #include "display/canvas-arena.h"
55 #include "libnr/nr-pixops.h"
56 #include "libnr/nr-matrix-translate-ops.h"
57 #include "libnr/nr-scale-ops.h"
58 #include "libnr/nr-scale-translate-ops.h"
59 #include "libnr/nr-translate-matrix-ops.h"
60 #include "libnr/nr-translate-scale-ops.h"
61 #include "libnr/nr-matrix-ops.h"
62 #include <2geom/pathvector.h>
63 #include "sp-item.h"
64 #include "sp-root.h"
65 #include "sp-defs.h"
66 #include "sp-path.h"
67 #include "splivarot.h"
68 #include "livarot/Path.h"
69 #include "livarot/Shape.h"
70 #include "svg/svg.h"
71 #include "color.h"
73 #include "trace/trace.h"
74 #include "trace/imagemap.h"
75 #include "trace/potrace/inkscape-potrace.h"
77 static void sp_flood_context_class_init(SPFloodContextClass *klass);
78 static void sp_flood_context_init(SPFloodContext *flood_context);
79 static void sp_flood_context_dispose(GObject *object);
81 static void sp_flood_context_setup(SPEventContext *ec);
83 static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event);
84 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
86 static void sp_flood_finish(SPFloodContext *rc);
88 static SPEventContextClass *parent_class;
91 GtkType sp_flood_context_get_type()
92 {
93     static GType type = 0;
94     if (!type) {
95         GTypeInfo info = {
96             sizeof(SPFloodContextClass),
97             NULL, NULL,
98             (GClassInitFunc) sp_flood_context_class_init,
99             NULL, NULL,
100             sizeof(SPFloodContext),
101             4,
102             (GInstanceInitFunc) sp_flood_context_init,
103             NULL,    /* value_table */
104         };
105         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPFloodContext", &info, (GTypeFlags) 0);
106     }
107     return type;
110 static void sp_flood_context_class_init(SPFloodContextClass *klass)
112     GObjectClass *object_class = (GObjectClass *) klass;
113     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
115     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
117     object_class->dispose = sp_flood_context_dispose;
119     event_context_class->setup = sp_flood_context_setup;
120     event_context_class->root_handler  = sp_flood_context_root_handler;
121     event_context_class->item_handler  = sp_flood_context_item_handler;
124 static void sp_flood_context_init(SPFloodContext *flood_context)
126     SPEventContext *event_context = SP_EVENT_CONTEXT(flood_context);
128     event_context->cursor_shape = cursor_paintbucket_xpm;
129     event_context->hot_x = 11;
130     event_context->hot_y = 30;
131     event_context->xp = 0;
132     event_context->yp = 0;
133     event_context->tolerance = 4;
134     event_context->within_tolerance = false;
135     event_context->item_to_select = NULL;
137     flood_context->item = NULL;
139     new (&flood_context->sel_changed_connection) sigc::connection();
142 static void sp_flood_context_dispose(GObject *object)
144     SPFloodContext *rc = SP_FLOOD_CONTEXT(object);
145     SPEventContext *ec = SP_EVENT_CONTEXT(object);
147     rc->sel_changed_connection.disconnect();
148     rc->sel_changed_connection.~connection();
150     delete ec->shape_editor;
151     ec->shape_editor = NULL;
153     /* fixme: This is necessary because we do not grab */
154     if (rc->item) {
155         sp_flood_finish(rc);
156     }
158     if (rc->_message_context) {
159         delete rc->_message_context;
160     }
162     G_OBJECT_CLASS(parent_class)->dispose(object);
165 /**
166 \brief  Callback that processes the "changed" signal on the selection;
167 destroys old and creates new knotholder
168 */
169 void sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer data)
171     SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
172     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
174     ec->shape_editor->unset_item(SH_KNOTHOLDER);
175     SPItem *item = selection->singleItem(); 
176     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
179 static void sp_flood_context_setup(SPEventContext *ec)
181     SPFloodContext *rc = SP_FLOOD_CONTEXT(ec);
183     if (((SPEventContextClass *) parent_class)->setup) {
184         ((SPEventContextClass *) parent_class)->setup(ec);
185     }
187     ec->shape_editor = new ShapeEditor(ec->desktop);
189     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
190     if (item) {
191         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
192     }
194     rc->sel_changed_connection.disconnect();
195     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
196         sigc::bind(sigc::ptr_fun(&sp_flood_context_selection_changed), (gpointer)rc)
197     );
199     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
201     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
202     if (prefs->getBool("/tools/paintbucket/selcue")) {
203         rc->enableSelectionCue();
204     }
207 /**
208  * \brief Merge a pixel with the background color.
209  * \param orig The pixel to merge with the background.
210  * \param bg The background color.
211  * \param base The pixel to merge the original and background into.
212  */
213 inline static void
214 merge_pixel_with_background (unsigned char *orig, unsigned char *bg,
215            unsigned char *base)
217     int precalc_bg_alpha = (255 * (255 - bg[3])) / 255;
218     
219     for (int i = 0; i < 3; i++) {
220         base[i] = precalc_bg_alpha + (bg[i] * bg[3]) / 255;
221         base[i] = (base[i] * (255 - orig[3])) / 255 + (orig[i] * orig[3]) / 255;
222     }
225 /**
226  * \brief Get the pointer to a pixel in a pixel buffer.
227  * \param px The pixel buffer.
228  * \param x The X coordinate.
229  * \param y The Y coordinate.
230  * \param width The width of the pixel buffer.
231  */
232 inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
233     return px + (x + y * width) * 4;
236 inline unsigned char * get_trace_pixel(guchar *trace_px, int x, int y, int width) {
237     return trace_px + (x + y * width);
240 /**
241  * \brief Generate the list of trace channel selection entries.
242  */
243 GList * flood_channels_dropdown_items_list() {
244     GList *glist = NULL;
246     glist = g_list_append (glist, _("Visible Colors"));
247     glist = g_list_append (glist, _("Red"));
248     glist = g_list_append (glist, _("Green"));
249     glist = g_list_append (glist, _("Blue"));
250     glist = g_list_append (glist, _("Hue"));
251     glist = g_list_append (glist, _("Saturation"));
252     glist = g_list_append (glist, _("Lightness"));
253     glist = g_list_append (glist, _("Alpha"));
255     return glist;
258 /**
259  * \brief Generate the list of autogap selection entries.
260  */
261 GList * flood_autogap_dropdown_items_list() {
262     GList *glist = NULL;
264     glist = g_list_append (glist, _("None"));
265     glist = g_list_append (glist, _("Small"));
266     glist = g_list_append (glist, _("Medium"));
267     glist = g_list_append (glist, _("Large"));
269     return glist;
272 /**
273  * \brief Compare a pixel in a pixel buffer with another pixel to determine if a point should be included in the fill operation.
274  * \param check The pixel in the pixel buffer to check.
275  * \param orig The original selected pixel to use as the fill target color.
276  * \param merged_orig_pixel The original pixel merged with the background.
277  * \param dtc The desktop background color.
278  * \param threshold The fill threshold.
279  * \param method The fill method to use as defined in PaintBucketChannels.
280  */
281 static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *merged_orig_pixel, unsigned char *dtc, int threshold, PaintBucketChannels method) {
282     int diff = 0;
283     float hsl_check[3], hsl_orig[3];
284     
285     if ((method == FLOOD_CHANNELS_H) ||
286         (method == FLOOD_CHANNELS_S) ||
287         (method == FLOOD_CHANNELS_L)) {
288         sp_color_rgb_to_hsl_floatv(hsl_check, check[0] / 255.0, check[1] / 255.0, check[2] / 255.0);
289         sp_color_rgb_to_hsl_floatv(hsl_orig, orig[0] / 255.0, orig[1] / 255.0, orig[2] / 255.0);
290     }
291     
292     switch (method) {
293         case FLOOD_CHANNELS_ALPHA:
294             return ((int)abs(check[3] - orig[3]) <= threshold);
295         case FLOOD_CHANNELS_R:
296             return ((int)abs(check[0] - orig[0]) <= threshold);
297         case FLOOD_CHANNELS_G:
298             return ((int)abs(check[1] - orig[1]) <= threshold);
299         case FLOOD_CHANNELS_B:
300             return ((int)abs(check[2] - orig[2]) <= threshold);
301         case FLOOD_CHANNELS_RGB:
302             unsigned char merged_check[3];
303             
304             merge_pixel_with_background(check, dtc, merged_check);
305             
306             for (int i = 0; i < 3; i++) {
307               diff += (int)abs(merged_check[i] - merged_orig_pixel[i]);
308             }
309             return ((diff / 3) <= ((threshold * 3) / 4));
310         
311         case FLOOD_CHANNELS_H:
312             return ((int)(fabs(hsl_check[0] - hsl_orig[0]) * 100.0) <= threshold);
313         case FLOOD_CHANNELS_S:
314             return ((int)(fabs(hsl_check[1] - hsl_orig[1]) * 100.0) <= threshold);
315         case FLOOD_CHANNELS_L:
316             return ((int)(fabs(hsl_check[2] - hsl_orig[2]) * 100.0) <= threshold);
317     }
318     
319     return false;
322 enum {
323   PIXEL_CHECKED = 1,
324   PIXEL_QUEUED  = 2,
325   PIXEL_PAINTABLE = 4,
326   PIXEL_NOT_PAINTABLE = 8,
327   PIXEL_COLORED = 16
328 };
330 static inline bool is_pixel_checked(unsigned char *t) { return (*t & PIXEL_CHECKED) == PIXEL_CHECKED; }
331 static inline bool is_pixel_queued(unsigned char *t) { return (*t & PIXEL_QUEUED) == PIXEL_QUEUED; }
332 static inline bool is_pixel_paintability_checked(unsigned char *t) {
333   return !((*t & PIXEL_PAINTABLE) == 0) && ((*t & PIXEL_NOT_PAINTABLE) == 0);
335 static inline bool is_pixel_paintable(unsigned char *t) { return (*t & PIXEL_PAINTABLE) == PIXEL_PAINTABLE; }
336 static inline bool is_pixel_colored(unsigned char *t) { return (*t & PIXEL_COLORED) == PIXEL_COLORED; }
338 static inline void mark_pixel_checked(unsigned char *t) { *t |= PIXEL_CHECKED; }
339 static inline void mark_pixel_unchecked(unsigned char *t) { *t ^= PIXEL_CHECKED; }
340 static inline void mark_pixel_queued(unsigned char *t) { *t |= PIXEL_QUEUED; }
341 static inline void mark_pixel_paintable(unsigned char *t) { *t |= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }
342 static inline void mark_pixel_not_paintable(unsigned char *t) { *t |= PIXEL_NOT_PAINTABLE; *t ^= PIXEL_PAINTABLE; }
343 static inline void mark_pixel_colored(unsigned char *t) { *t |= PIXEL_COLORED; }
345 static inline void clear_pixel_paintability(unsigned char *t) { *t ^= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }
347 struct bitmap_coords_info {
348     bool is_left;
349     unsigned int x;
350     unsigned int y;
351     int y_limit;
352     unsigned int width;
353     unsigned int height;
354     unsigned int threshold;
355     unsigned int radius;
356     PaintBucketChannels method;
357     unsigned char *dtc;
358     unsigned char *merged_orig_pixel;
359     Geom::Rect bbox;
360     Geom::Rect screen;
361     unsigned int max_queue_size;
362     unsigned int current_step;
363 };
365 /**
366  * \brief Check if a pixel can be included in the fill.
367  * \param px The rendered pixel buffer to check.
368  * \param trace_t The pixel in the trace pixel buffer to check or mark.
369  * \param x The X coordinate.
370  * \param y The y coordinate.
371  * \param orig_color The original selected pixel to use as the fill target color.
372  * \param bci The bitmap_coords_info structure.
373  */
374 inline static bool check_if_pixel_is_paintable(guchar *px, unsigned char *trace_t, int x, int y, unsigned char *orig_color, bitmap_coords_info bci) {
375     if (is_pixel_paintability_checked(trace_t)) {
376         return is_pixel_paintable(trace_t);
377     } else {
378         unsigned char *t = get_pixel(px, x, y, bci.width);
379         if (compare_pixels(t, orig_color, bci.merged_orig_pixel, bci.dtc, bci.threshold, bci.method)) {
380             mark_pixel_paintable(trace_t);
381             return true;
382         } else {
383             mark_pixel_not_paintable(trace_t);
384             return false;
385         }
386     }
389 /**
390  * \brief Perform the bitmap-to-vector tracing and place the traced path onto the document.
391  * \param px The trace pixel buffer to trace to SVG.
392  * \param desktop The desktop on which to place the final SVG path.
393  * \param transform The transform to apply to the final SVG path.
394  * \param union_with_selection If true, merge the final SVG path with the current selection.
395  */
396 static void do_trace(bitmap_coords_info bci, guchar *trace_px, SPDesktop *desktop, Geom::Matrix transform, unsigned int min_x, unsigned int max_x, unsigned int min_y, unsigned int max_y, bool union_with_selection) {
397     SPDocument *document = sp_desktop_document(desktop);
399     unsigned char *trace_t;
401     GrayMap *gray_map = GrayMapCreate((max_x - min_x + 1), (max_y - min_y + 1));
402     unsigned int gray_map_y = 0;
403     for (unsigned int y = min_y; y <= max_y; y++) {
404         unsigned long *gray_map_t = gray_map->rows[gray_map_y];
406         trace_t = get_trace_pixel(trace_px, min_x, y, bci.width);
407         for (unsigned int x = min_x; x <= max_x; x++) {
408             *gray_map_t = is_pixel_colored(trace_t) ? GRAYMAP_BLACK : GRAYMAP_WHITE;
409             gray_map_t++;
410             trace_t++;
411         }
412         gray_map_y++;
413     }
415     Inkscape::Trace::Potrace::PotraceTracingEngine pte;
416     pte.keepGoing = 1;
417     std::vector<Inkscape::Trace::TracingEngineResult> results = pte.traceGrayMap(gray_map);
418     gray_map->destroy(gray_map);
420     Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
421     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
423     long totalNodeCount = 0L;
425     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
426     double offset = prefs->getDouble("/tools/paintbucket/offset", 0.0);
428     for (unsigned int i=0 ; i<results.size() ; i++) {
429         Inkscape::Trace::TracingEngineResult result = results[i];
430         totalNodeCount += result.getNodeCount();
432         Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
433         /* Set style */
434         sp_desktop_apply_style_tool (desktop, pathRepr, "/tools/paintbucket", false);
436         Geom::PathVector pathv = sp_svg_read_pathv(result.getPathData().c_str());
437         Path *path = new Path;
438         path->LoadPathVector(pathv);
440         if (offset != 0) {
441         
442             Shape *path_shape = new Shape();
443         
444             path->ConvertWithBackData(0.03);
445             path->Fill(path_shape, 0);
446             delete path;
447         
448             Shape *expanded_path_shape = new Shape();
449         
450             expanded_path_shape->ConvertToShape(path_shape, fill_nonZero);
451             path_shape->MakeOffset(expanded_path_shape, offset * desktop->current_zoom(), join_round, 4);
452             expanded_path_shape->ConvertToShape(path_shape, fill_positive);
454             Path *expanded_path = new Path();
455         
456             expanded_path->Reset();
457             expanded_path_shape->ConvertToForme(expanded_path);
458             expanded_path->ConvertEvenLines(1.0);
459             expanded_path->Simplify(1.0);
460         
461             delete path_shape;
462             delete expanded_path_shape;
463         
464             gchar *str = expanded_path->svg_dump_path();
465             if (str && *str) {
466                 pathRepr->setAttribute("d", str);
467                 g_free(str);
468             } else {
469                 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Too much inset</b>, the result is empty."));
470                 Inkscape::GC::release(pathRepr);
471                 g_free(str);
472                 return;
473             }
475             delete expanded_path;
477         } else {
478             gchar *str = path->svg_dump_path();
479             delete path;
480             pathRepr->setAttribute("d", str);
481             g_free(str);
482         }
484         layer_repr->addChild(pathRepr, NULL);
486         SPObject *reprobj = document->getObjectByRepr(pathRepr);
487         if (reprobj) {
488             sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
489             
490             // premultiply the item transform by the accumulated parent transform in the paste layer
491             Geom::Matrix local (sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer())));
492             if (!local.isIdentity()) {
493                 gchar const *t_str = pathRepr->attribute("transform");
494                 Geom::Matrix item_t (Geom::identity());
495                 if (t_str)
496                     sp_svg_transform_read(t_str, &item_t);
497                 item_t *= local.inverse();
498                 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
499                 gchar *affinestr=sp_svg_transform_write(item_t);
500                 pathRepr->setAttribute("transform", affinestr);
501                 g_free(affinestr);
502             }
504             Inkscape::Selection *selection = sp_desktop_selection(desktop);
506             pathRepr->setPosition(-1);
508             if (union_with_selection) {
509                 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)));
510                 selection->add(reprobj);
511                 sp_selected_path_union_skip_undo(desktop);
512             } else {
513                 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)));
514                 selection->set(reprobj);
515             }
517         }
519         Inkscape::GC::release(pathRepr);
521     }
524 /**
525  * \brief The possible return states of perform_bitmap_scanline_check()
526  */
527 enum ScanlineCheckResult {
528     SCANLINE_CHECK_OK,
529     SCANLINE_CHECK_ABORTED,
530     SCANLINE_CHECK_BOUNDARY
531 };
533 /**
534  * \brief Determine if the provided coordinates are within the pixel buffer limits.
535  * \param x The X coordinate.
536  * \param y The Y coordinate.
537  * \param bci The bitmap_coords_info structure.
538  */
539 inline static bool coords_in_range(unsigned int x, unsigned int y, bitmap_coords_info bci) {
540     return (x < bci.width) &&
541            (y < bci.height);
544 #define PAINT_DIRECTION_LEFT 1
545 #define PAINT_DIRECTION_RIGHT 2
546 #define PAINT_DIRECTION_UP 4
547 #define PAINT_DIRECTION_DOWN 8
548 #define PAINT_DIRECTION_ALL 15
550 /**
551  * \brief Paint a pixel or a square (if autogap is enabled) on the trace pixel buffer
552  * \param px The rendered pixel buffer to check.
553  * \param trace_px The trace pixel buffer.
554  * \param orig_color The original selected pixel to use as the fill target color.
555  * \param bci The bitmap_coords_info structure.
556  * \param original_point_trace_t The original pixel in the trace pixel buffer to check.
557  */
558 inline static unsigned int paint_pixel(guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci, unsigned char *original_point_trace_t) {
559     if (bci.radius == 0) {
560         mark_pixel_colored(original_point_trace_t); 
561         return PAINT_DIRECTION_ALL;
562     } else {
563         unsigned char *trace_t;
564   
565         bool can_paint_up = true;
566         bool can_paint_down = true;
567         bool can_paint_left = true;
568         bool can_paint_right = true;
569       
570         for (unsigned int ty = bci.y - bci.radius; ty <= bci.y + bci.radius; ty++) {
571             for (unsigned int tx = bci.x - bci.radius; tx <= bci.x + bci.radius; tx++) {
572                 if (coords_in_range(tx, ty, bci)) {
573                     trace_t = get_trace_pixel(trace_px, tx, ty, bci.width);
574                     if (!is_pixel_colored(trace_t)) {
575                         if (check_if_pixel_is_paintable(px, trace_t, tx, ty, orig_color, bci)) {
576                             mark_pixel_colored(trace_t); 
577                         } else {
578                             if (tx < bci.x) { can_paint_left = false; }
579                             if (tx > bci.x) { can_paint_right = false; }
580                             if (ty < bci.y) { can_paint_up = false; }
581                             if (ty > bci.y) { can_paint_down = false; }
582                         }
583                     }
584                 }
585             }
586         }
587     
588         unsigned int paint_directions = 0;
589         if (can_paint_left) { paint_directions += PAINT_DIRECTION_LEFT; }
590         if (can_paint_right) { paint_directions += PAINT_DIRECTION_RIGHT; }
591         if (can_paint_up) { paint_directions += PAINT_DIRECTION_UP; }
592         if (can_paint_down) { paint_directions += PAINT_DIRECTION_DOWN; }
593         
594         return paint_directions;
595     }
598 /**
599  * \brief Push a point to be checked onto the bottom of the rendered pixel buffer check queue.
600  * \param fill_queue The fill queue to add the point to.
601  * \param max_queue_size The maximum size of the fill queue.
602  * \param trace_t The trace pixel buffer pixel.
603  * \param x The X coordinate.
604  * \param y The Y coordinate.
605  */
606 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) {
607     if (!is_pixel_queued(trace_t)) {
608         if ((fill_queue->size() < max_queue_size)) {
609             fill_queue->push_back(Geom::Point(x, y));
610             mark_pixel_queued(trace_t);
611         }
612     }
615 /**
616  * \brief Shift a point to be checked onto the top of the rendered pixel buffer check queue.
617  * \param fill_queue The fill queue to add the point to.
618  * \param max_queue_size The maximum size of the fill queue.
619  * \param trace_t The trace pixel buffer pixel.
620  * \param x The X coordinate.
621  * \param y The Y coordinate.
622  */
623 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) {
624     if (!is_pixel_queued(trace_t)) {
625         if ((fill_queue->size() < max_queue_size)) {
626             fill_queue->push_front(Geom::Point(x, y));
627             mark_pixel_queued(trace_t);
628         }
629     }
632 /**
633  * \brief Scan a row in the rendered pixel buffer and add points to the fill queue as necessary.
634  * \param fill_queue The fill queue to add the point to.
635  * \param px The rendered pixel buffer.
636  * \param trace_px The trace pixel buffer.
637  * \param orig_color The original selected pixel to use as the fill target color.
638  * \param bci The bitmap_coords_info structure.
639  */
640 static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<Geom::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci, unsigned int *min_x, unsigned int *max_x) {
641     bool aborted = false;
642     bool reached_screen_boundary = false;
643     bool ok;
645     bool keep_tracing;
646     bool initial_paint = true;
648     unsigned char *current_trace_t = get_trace_pixel(trace_px, bci.x, bci.y, bci.width);
649     unsigned int paint_directions;
651     bool currently_painting_top = false;
652     bool currently_painting_bottom = false;
654     unsigned int top_ty = bci.y - 1;
655     unsigned int bottom_ty = bci.y + 1;
657     bool can_paint_top = (top_ty > 0);
658     bool can_paint_bottom = (bottom_ty < bci.height);
660     Geom::Point t = fill_queue->front();
662     do {
663         ok = false;
664         if (bci.is_left) {
665             keep_tracing = (bci.x != 0);
666         } else {
667             keep_tracing = (bci.x < bci.width);
668         }
670         *min_x = MIN(*min_x, bci.x);
671         *max_x = MAX(*max_x, bci.x);
673         if (keep_tracing) {
674             if (check_if_pixel_is_paintable(px, current_trace_t, bci.x, bci.y, orig_color, bci)) {
675                 paint_directions = paint_pixel(px, trace_px, orig_color, bci, current_trace_t);
676                 if (bci.radius == 0) {
677                     mark_pixel_checked(current_trace_t);
678                     if ((t[Geom::X] == bci.x) && (t[Geom::Y] == bci.y)) {
679                         fill_queue->pop_front(); t = fill_queue->front();
680                     }
681                 }
683                 if (can_paint_top) {
684                     if (paint_directions & PAINT_DIRECTION_UP) { 
685                         unsigned char *trace_t = current_trace_t - bci.width;
686                         if (!is_pixel_queued(trace_t)) {
687                             bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, top_ty, orig_color, bci);
689                             if (initial_paint) { currently_painting_top = !ok_to_paint; }
691                             if (ok_to_paint && (!currently_painting_top)) {
692                                 currently_painting_top = true;
693                                 push_point_onto_queue(fill_queue, bci.max_queue_size, trace_t, bci.x, top_ty);
694                             }
695                             if ((!ok_to_paint) && currently_painting_top) {
696                                 currently_painting_top = false;
697                             }
698                         }
699                     }
700                 }
702                 if (can_paint_bottom) {
703                     if (paint_directions & PAINT_DIRECTION_DOWN) { 
704                         unsigned char *trace_t = current_trace_t + bci.width;
705                         if (!is_pixel_queued(trace_t)) {
706                             bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, bottom_ty, orig_color, bci);
708                             if (initial_paint) { currently_painting_bottom = !ok_to_paint; }
710                             if (ok_to_paint && (!currently_painting_bottom)) {
711                                 currently_painting_bottom = true;
712                                 push_point_onto_queue(fill_queue, bci.max_queue_size, trace_t, bci.x, bottom_ty);
713                             }
714                             if ((!ok_to_paint) && currently_painting_bottom) {
715                                 currently_painting_bottom = false;
716                             }
717                         }
718                     }
719                 }
721                 if (bci.is_left) {
722                     if (paint_directions & PAINT_DIRECTION_LEFT) {
723                         bci.x--; current_trace_t--;
724                         ok = true;
725                     }
726                 } else {
727                     if (paint_directions & PAINT_DIRECTION_RIGHT) {
728                         bci.x++; current_trace_t++;
729                         ok = true;
730                     }
731                 }
733                 initial_paint = false;
734             }
735         } else {
736             if (bci.bbox.min()[Geom::X] > bci.screen.min()[Geom::X]) {
737                 aborted = true; break;
738             } else {
739                 reached_screen_boundary = true;
740             }
741         }
742     } while (ok);
744     if (aborted) { return SCANLINE_CHECK_ABORTED; }
745     if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
746     return SCANLINE_CHECK_OK;
749 /**
750  * \brief Sort the rendered pixel buffer check queue vertically.
751  */
752 static bool sort_fill_queue_vertical(Geom::Point a, Geom::Point b) {
753     return a[Geom::Y] > b[Geom::Y];
756 /**
757  * \brief Sort the rendered pixel buffer check queue horizontally.
758  */
759 static bool sort_fill_queue_horizontal(Geom::Point a, Geom::Point b) {
760     return a[Geom::X] > b[Geom::X];
763 /**
764  * \brief Perform a flood fill operation.
765  * \param event_context The event context for this tool.
766  * \param event The details of this event.
767  * \param union_with_selection If true, union the new fill with the current selection.
768  * \param is_point_fill If false, use the Rubberband "touch selection" to get the initial points for the fill.
769  * \param is_touch_fill If true, use only the initial contact point in the Rubberband "touch selection" as the fill target color.
770  */
771 static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event, bool union_with_selection, bool is_point_fill, bool is_touch_fill) {
772     SPDesktop *desktop = event_context->desktop;
773     SPDocument *document = sp_desktop_document(desktop);
775     /* Create new arena */
776     NRArena *arena = NRArena::create();
777     unsigned dkey = sp_item_display_key_new(1);
779     sp_document_ensure_up_to_date (document);
780     
781     SPItem *document_root = SP_ITEM(SP_DOCUMENT_ROOT(document));
782     Geom::OptRect bbox = document_root->getBounds(Geom::identity());
784     if (!bbox) {
785         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
786         return;
787     }
788     
789     double zoom_scale = desktop->current_zoom();
790     
791     // Render 160% of the physical display to the render pixel buffer, so that available
792     // fill areas off the screen can be included in the fill.
793     double padding = 1.6;
795     Geom::Rect screen = desktop->get_display_area();
797     unsigned int width = (int)ceil(screen.width() * zoom_scale * padding);
798     unsigned int height = (int)ceil(screen.height() * zoom_scale * padding);
800     Geom::Point origin(screen.min()[Geom::X],
801                        sp_document_height(document) - screen.height() - screen.min()[Geom::Y]);
802                     
803     origin[Geom::X] = origin[Geom::X] + (screen.width() * ((1 - padding) / 2));
804     origin[Geom::Y] = origin[Geom::Y] + (screen.height() * ((1 - padding) / 2));
805     
806     Geom::Scale scale(zoom_scale, zoom_scale);
807     Geom::Matrix affine = scale * Geom::Translate(-origin * scale);
808     
809     /* Create ArenaItems and set transform */
810     NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
811     nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
813     NRGC gc(NULL);
814     gc.transform.setIdentity();
815     
816     NRRectL final_bbox;
817     final_bbox.x0 = 0;
818     final_bbox.y0 = 0; //row;
819     final_bbox.x1 = width;
820     final_bbox.y1 = height; //row + num_rows;
821     
822     nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
824     guchar *px = g_new(guchar, 4 * width * height);
825     
826     NRPixBlock B;
827     nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
828                               final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
829                               px, 4 * width, FALSE, FALSE );
830     
831     SPNamedView *nv = sp_desktop_namedview(desktop);
832     unsigned long bgcolor = nv->pagecolor;
833     
834     unsigned char dtc[4];
835     dtc[0] = NR_RGBA32_R(bgcolor);
836     dtc[1] = NR_RGBA32_G(bgcolor);
837     dtc[2] = NR_RGBA32_B(bgcolor);
838     dtc[3] = NR_RGBA32_A(bgcolor);
839     
840     for (unsigned int fy = 0; fy < height; fy++) {
841         guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs;
842         for (unsigned int fx = 0; fx < width; fx++) {
843             for (int i = 0; i < 4; i++) { 
844                 *p++ = dtc[i];
845             }
846         }
847     }
849     nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
850     nr_pixblock_release(&B);
851     
852     // Hide items
853     sp_item_invoke_hide(SP_ITEM(sp_document_root(document)), dkey);
854     
855     nr_object_unref((NRObject *) arena);
856     
857     guchar *trace_px = g_new(guchar, width * height);
858     memset(trace_px, 0x00, width * height);
859     
860     std::deque<Geom::Point> fill_queue;
861     std::queue<Geom::Point> color_queue;
862     
863     std::vector<Geom::Point> fill_points;
864     
865     bool aborted = false;
866     int y_limit = height - 1;
868     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
869     PaintBucketChannels method = (PaintBucketChannels) prefs->getInt("/tools/paintbucket/channels", 0);
870     int threshold = prefs->getIntLimited("/tools/paintbucket/threshold", 1, 0, 100);
872     switch(method) {
873         case FLOOD_CHANNELS_ALPHA:
874         case FLOOD_CHANNELS_RGB:
875         case FLOOD_CHANNELS_R:
876         case FLOOD_CHANNELS_G:
877         case FLOOD_CHANNELS_B:
878             threshold = (255 * threshold) / 100;
879             break;
880         case FLOOD_CHANNELS_H:
881         case FLOOD_CHANNELS_S:
882         case FLOOD_CHANNELS_L:
883             break;
884     }
886     bitmap_coords_info bci;
887     
888     bci.y_limit = y_limit;
889     bci.width = width;
890     bci.height = height;
891     bci.threshold = threshold;
892     bci.method = method;
893     bci.bbox = *bbox;
894     bci.screen = screen;
895     bci.dtc = dtc;
896     bci.radius = prefs->getIntLimited("/tools/paintbucket/autogap", 0, 0, 3);
897     bci.max_queue_size = (width * height) / 4;
898     bci.current_step = 0;
900     if (is_point_fill) {
901         fill_points.push_back(Geom::Point(event->button.x, event->button.y));
902     } else {
903         Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
904         fill_points = r->getPoints();
905     }
907     for (unsigned int i = 0; i < fill_points.size(); i++) {
908         Geom::Point pw = Geom::Point(fill_points[i][Geom::X] / zoom_scale, sp_document_height(document) + (fill_points[i][Geom::Y] / zoom_scale)) * affine;
910         pw[Geom::X] = (int)MIN(width - 1, MAX(0, pw[Geom::X]));
911         pw[Geom::Y] = (int)MIN(height - 1, MAX(0, pw[Geom::Y]));
913         if (is_touch_fill) {
914             if (i == 0) {
915                 color_queue.push(pw);
916             } else {
917                 unsigned char *trace_t = get_trace_pixel(trace_px, (int)pw[Geom::X], (int)pw[Geom::Y], width);
918                 push_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, (int)pw[Geom::X], (int)pw[Geom::Y]);
919             }
920         } else {
921             color_queue.push(pw);
922         }
923     }
925     bool reached_screen_boundary = false;
927     bool first_run = true;
929     unsigned long sort_size_threshold = 5;
931     unsigned int min_y = height;
932     unsigned int max_y = 0;
933     unsigned int min_x = width;
934     unsigned int max_x = 0;
936     while (!color_queue.empty() && !aborted) {
937         Geom::Point color_point = color_queue.front();
938         color_queue.pop();
940         int cx = (int)color_point[Geom::X];
941         int cy = (int)color_point[Geom::Y];
943         unsigned char *orig_px = get_pixel(px, cx, cy, width);
944         unsigned char orig_color[4];
945         for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; }
947         unsigned char merged_orig[3];
949         merge_pixel_with_background(orig_color, dtc, merged_orig);
951         bci.merged_orig_pixel = merged_orig;
953         unsigned char *trace_t = get_trace_pixel(trace_px, cx, cy, width);
954         if (!is_pixel_checked(trace_t) && !is_pixel_colored(trace_t)) {
955             if (check_if_pixel_is_paintable(px, trace_px, cx, cy, orig_color, bci)) {
956                 shift_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, cx, cy);
958                 if (!first_run) {
959                     for (unsigned int y = 0; y < height; y++) {
960                         trace_t = get_trace_pixel(trace_px, 0, y, width);
961                         for (unsigned int x = 0; x < width; x++) {
962                             clear_pixel_paintability(trace_t);
963                             trace_t++;
964                         }
965                     }
966                 }
967                 first_run = false;
968             }
969         }
971         unsigned long old_fill_queue_size = fill_queue.size();
973         while (!fill_queue.empty() && !aborted) {
974             Geom::Point cp = fill_queue.front();
976             if (bci.radius == 0) {
977                 unsigned long new_fill_queue_size = fill_queue.size();
979                 /*
980                  * To reduce the number of points in the fill queue, periodically
981                  * resort all of the points in the queue so that scanline checks
982                  * can complete more quickly.  A point cannot be checked twice
983                  * in a normal scanline checks, so forcing scanline checks to start
984                  * from one corner of the rendered area as often as possible
985                  * will reduce the number of points that need to be checked and queued.
986                  */
987                 if (new_fill_queue_size > sort_size_threshold) {
988                     if (new_fill_queue_size > old_fill_queue_size) {
989                         std::sort(fill_queue.begin(), fill_queue.end(), sort_fill_queue_vertical);
991                         std::deque<Geom::Point>::iterator start_sort = fill_queue.begin();
992                         std::deque<Geom::Point>::iterator end_sort = fill_queue.begin();
993                         unsigned int sort_y = (unsigned int)cp[Geom::Y];
994                         unsigned int current_y = sort_y;
995                         
996                         for (std::deque<Geom::Point>::iterator i = fill_queue.begin(); i != fill_queue.end(); i++) {
997                             Geom::Point current = *i;
998                             current_y = (unsigned int)current[Geom::Y];
999                             if (current_y != sort_y) {
1000                                 if (start_sort != end_sort) {
1001                                     std::sort(start_sort, end_sort, sort_fill_queue_horizontal);
1002                                 }
1003                                 sort_y = current_y;
1004                                 start_sort = i;
1005                             }
1006                             end_sort = i;
1007                         }
1008                         if (start_sort != end_sort) {
1009                             std::sort(start_sort, end_sort, sort_fill_queue_horizontal);
1010                         }
1011                         
1012                         cp = fill_queue.front();
1013                     }
1014                 }
1016                 old_fill_queue_size = new_fill_queue_size;
1017             }
1019             fill_queue.pop_front();
1021             int x = (int)cp[Geom::X];
1022             int y = (int)cp[Geom::Y];
1024             min_y = MIN((unsigned int)y, min_y);
1025             max_y = MAX((unsigned int)y, max_y);
1027             unsigned char *trace_t = get_trace_pixel(trace_px, x, y, width);
1028             if (!is_pixel_checked(trace_t)) {
1029                 mark_pixel_checked(trace_t);
1031                 if (y == 0) {
1032                     if (bbox->min()[Geom::Y] > screen.min()[Geom::Y]) {
1033                         aborted = true; break;
1034                     } else {
1035                         reached_screen_boundary = true;
1036                     }
1037                 }
1039                 if (y == y_limit) {
1040                     if (bbox->max()[Geom::Y] < screen.max()[Geom::Y]) {
1041                         aborted = true; break;
1042                     } else {
1043                         reached_screen_boundary = true;
1044                     }
1045                 }
1047                 bci.is_left = true;
1048                 bci.x = x;
1049                 bci.y = y;
1051                 ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci, &min_x, &max_x);
1053                 switch (result) {
1054                     case SCANLINE_CHECK_ABORTED:
1055                         aborted = true;
1056                         break;
1057                     case SCANLINE_CHECK_BOUNDARY:
1058                         reached_screen_boundary = true;
1059                         break;
1060                     default:
1061                         break;
1062                 }
1064                 if (bci.x < width) {
1065                     trace_t++;
1066                     if (!is_pixel_checked(trace_t) && !is_pixel_queued(trace_t)) {
1067                         mark_pixel_checked(trace_t);
1068                         bci.is_left = false;
1069                         bci.x = x + 1;
1071                         result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci, &min_x, &max_x);
1073                         switch (result) {
1074                             case SCANLINE_CHECK_ABORTED:
1075                                 aborted = true;
1076                                 break;
1077                             case SCANLINE_CHECK_BOUNDARY:
1078                                 reached_screen_boundary = true;
1079                                 break;
1080                             default:
1081                                 break;
1082                         }
1083                     }
1084                 }
1085             }
1087             bci.current_step++;
1089             if (bci.current_step > bci.max_queue_size) {
1090                 aborted = true;
1091             }
1092         }
1093     }
1094     
1095     g_free(px);
1096     
1097     if (aborted) {
1098         g_free(trace_px);
1099         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
1100         return;
1101     }
1102     
1103     if (reached_screen_boundary) {
1104         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.")); 
1105     }
1107     unsigned int trace_padding = bci.radius + 1;
1108     if (min_y > trace_padding) { min_y -= trace_padding; }
1109     if (max_y < (y_limit - trace_padding)) { max_y += trace_padding; }
1110     if (min_x > trace_padding) { min_x -= trace_padding; }
1111     if (max_x < (width - 1 - trace_padding)) { max_x += trace_padding; }
1113     Geom::Point min_start = Geom::Point(min_x, min_y);
1114     
1115     affine = scale * Geom::Translate(-origin * scale - min_start);
1116     Geom::Matrix inverted_affine = Geom::Matrix(affine).inverse();
1117     
1118     do_trace(bci, trace_px, desktop, inverted_affine, min_x, max_x, min_y, max_y, union_with_selection);
1120     g_free(trace_px);
1121     
1122     sp_document_done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
1125 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
1127     gint ret = FALSE;
1129     SPDesktop *desktop = event_context->desktop;
1131     switch (event->type) {
1132     case GDK_BUTTON_PRESS:
1133         if ((event->button.state & GDK_CONTROL_MASK) && event->button.button == 1 && !event_context->space_panning) {
1134             Geom::Point const button_w(event->button.x,
1135                                        event->button.y);
1136             
1137             SPItem *item = sp_event_context_find_item (desktop, button_w, TRUE, TRUE);
1138             
1139             Inkscape::XML::Node *pathRepr = SP_OBJECT_REPR(item);
1140             /* Set style */
1141             sp_desktop_apply_style_tool (desktop, pathRepr, "/tools/paintbucket", false);
1142             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object"));
1143             ret = TRUE;
1144         }
1145         break;
1146     default:
1147         break;
1148     }
1150     if (((SPEventContextClass *) parent_class)->item_handler) {
1151         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
1152     }
1154     return ret;
1157 static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
1159     static bool dragging;
1160     
1161     gint ret = FALSE;
1162     SPDesktop *desktop = event_context->desktop;
1164     switch (event->type) {
1165     case GDK_BUTTON_PRESS:
1166         if (event->button.button == 1 && !event_context->space_panning) {
1167             if (!(event->button.state & GDK_CONTROL_MASK)) {
1168                 Geom::Point const button_w(event->button.x,
1169                                            event->button.y);
1170     
1171                 if (Inkscape::have_viable_layer(desktop, event_context->defaultMessageContext())) {
1172                     // save drag origin
1173                     event_context->xp = (gint) button_w[Geom::X];
1174                     event_context->yp = (gint) button_w[Geom::Y];
1175                     event_context->within_tolerance = true;
1176                       
1177                     dragging = true;
1178                     
1179                     Geom::Point const p(desktop->w2d(button_w));
1180                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
1181                     Inkscape::Rubberband::get(desktop)->start(desktop, p);
1182                 }
1183             }
1184         }
1185     case GDK_MOTION_NOTIFY:
1186         if ( dragging
1187              && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning)
1188         {
1189             if ( event_context->within_tolerance
1190                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
1191                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
1192                 break; // do not drag if we're within tolerance from origin
1193             }
1194             
1195             event_context->within_tolerance = false;
1196             
1197             Geom::Point const motion_pt(event->motion.x, event->motion.y);
1198             Geom::Point const p(desktop->w2d(motion_pt));
1199             if (Inkscape::Rubberband::get(desktop)->is_started()) {
1200                 Inkscape::Rubberband::get(desktop)->move(p);
1201                 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> areas to add to fill, hold <b>Alt</b> for touch fill"));
1202                 gobble_motion_events(GDK_BUTTON1_MASK);
1203             }
1204         }
1205         break;
1207     case GDK_BUTTON_RELEASE:
1208         if (event->button.button == 1 && !event_context->space_panning) {
1209             Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
1210             if (r->is_started()) {
1211                 // set "busy" cursor
1212                 desktop->setWaitingCursor();
1214                 if (SP_IS_EVENT_CONTEXT(event_context)) { 
1215                     // Since setWaitingCursor runs main loop iterations, we may have already left this tool!
1216                     // So check if the tool is valid before doing anything
1217                     dragging = false;
1219                     bool is_point_fill = event_context->within_tolerance;
1220                     bool is_touch_fill = event->button.state & GDK_MOD1_MASK;
1221                     
1222                     sp_flood_do_flood_fill(event_context, event, event->button.state & GDK_SHIFT_MASK, is_point_fill, is_touch_fill);
1223                     
1224                     desktop->clearWaitingCursor();
1225                     // restore cursor when done; note that it may already be different if e.g. user 
1226                     // switched to another tool during interruptible tracing or drawing, in which case do nothing
1228                     ret = TRUE;
1229                 }
1231                 r->stop();
1233                 if (SP_IS_EVENT_CONTEXT(event_context)) {
1234                     event_context->defaultMessageContext()->clear();
1235                 }
1236             }
1237         }
1238         break;
1239     case GDK_KEY_PRESS:
1240         switch (get_group0_keyval (&event->key)) {
1241         case GDK_Up:
1242         case GDK_Down:
1243         case GDK_KP_Up:
1244         case GDK_KP_Down:
1245             // prevent the zoom field from activation
1246             if (!MOD__CTRL_ONLY)
1247                 ret = TRUE;
1248             break;
1249         default:
1250             break;
1251         }
1252         break;
1253     default:
1254         break;
1255     }
1257     if (!ret) {
1258         if (((SPEventContextClass *) parent_class)->root_handler) {
1259             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1260         }
1261     }
1263     return ret;
1267 static void sp_flood_finish(SPFloodContext *rc)
1269     rc->_message_context->clear();
1271     if ( rc->item != NULL ) {
1272         SPDesktop * desktop;
1274         desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
1276         SP_OBJECT(rc->item)->updateRepr();
1278         sp_canvas_end_forced_full_redraws(desktop->canvas);
1280         sp_desktop_selection(desktop)->set(rc->item);
1281         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
1282                         _("Fill bounded area"));
1284         rc->item = NULL;
1285     }
1288 void flood_channels_set_channels( gint channels )
1290     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1291     prefs->setInt("/tools/paintbucket/channels", channels);
1294 /*
1295   Local Variables:
1296   mode:c++
1297   c-file-style:"stroustrup"
1298   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1299   indent-tabs-mode:nil
1300   fill-column:99
1301   End:
1302 */
1303 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :