Code

Add and adjust fill channels
[inkscape.git] / src / flood-context.cpp
1 #define __SP_FLOOD_CONTEXT_C__
3 /*
4  * Flood fill drawing context
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *   John Bintz <jcoswell@coswellproductions.org>
10  *
11  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
12  * Copyright (C) 2000-2005 authors
13  * Copyright (C) 2000-2001 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include "config.h"
20 #include <gdk/gdkkeysyms.h>
21 #include <queue>
23 #include "macros.h"
24 #include "display/sp-canvas.h"
25 #include "document.h"
26 #include "sp-namedview.h"
27 #include "sp-object.h"
28 #include "sp-rect.h"
29 #include "selection.h"
30 #include "selection-chemistry.h"
31 #include "desktop-handles.h"
32 #include "snap.h"
33 #include "desktop.h"
34 #include "desktop-style.h"
35 #include "message-stack.h"
36 #include "message-context.h"
37 #include "pixmaps/cursor-paintbucket.xpm"
38 #include "flood-context.h"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "object-edit.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "prefs-utils.h"
45 #include "context-fns.h"
47 #include "display/nr-arena-item.h"
48 #include "display/nr-arena.h"
49 #include "display/nr-arena-image.h"
50 #include "display/canvas-arena.h"
51 #include "libnr/nr-pixops.h"
52 #include "libnr/nr-matrix-rotate-ops.h"
53 #include "libnr/nr-matrix-translate-ops.h"
54 #include "libnr/nr-rotate-fns.h"
55 #include "libnr/nr-scale-ops.h"
56 #include "libnr/nr-scale-translate-ops.h"
57 #include "libnr/nr-translate-matrix-ops.h"
58 #include "libnr/nr-translate-scale-ops.h"
59 #include "libnr/nr-matrix-ops.h"
60 #include "sp-item.h"
61 #include "sp-root.h"
62 #include "sp-defs.h"
63 #include "sp-path.h"
64 #include "splivarot.h"
65 #include "livarot/Path.h"
66 #include "livarot/Shape.h"
67 #include "libnr/n-art-bpath.h"
68 #include "svg/svg.h"
70 #include "trace/trace.h"
71 #include "trace/potrace/inkscape-potrace.h"
73 static void sp_flood_context_class_init(SPFloodContextClass *klass);
74 static void sp_flood_context_init(SPFloodContext *flood_context);
75 static void sp_flood_context_dispose(GObject *object);
77 static void sp_flood_context_setup(SPEventContext *ec);
79 static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event);
80 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
82 static void sp_flood_finish(SPFloodContext *rc);
84 static SPEventContextClass *parent_class;
87 GtkType sp_flood_context_get_type()
88 {
89     static GType type = 0;
90     if (!type) {
91         GTypeInfo info = {
92             sizeof(SPFloodContextClass),
93             NULL, NULL,
94             (GClassInitFunc) sp_flood_context_class_init,
95             NULL, NULL,
96             sizeof(SPFloodContext),
97             4,
98             (GInstanceInitFunc) sp_flood_context_init,
99             NULL,    /* value_table */
100         };
101         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPFloodContext", &info, (GTypeFlags) 0);
102     }
103     return type;
106 static void sp_flood_context_class_init(SPFloodContextClass *klass)
108     GObjectClass *object_class = (GObjectClass *) klass;
109     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
111     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
113     object_class->dispose = sp_flood_context_dispose;
115     event_context_class->setup = sp_flood_context_setup;
116     event_context_class->root_handler  = sp_flood_context_root_handler;
117     event_context_class->item_handler  = sp_flood_context_item_handler;
120 static void sp_flood_context_init(SPFloodContext *flood_context)
122     SPEventContext *event_context = SP_EVENT_CONTEXT(flood_context);
124     event_context->cursor_shape = cursor_paintbucket_xpm;
125     event_context->hot_x = 11;
126     event_context->hot_y = 30;
127     event_context->xp = 0;
128     event_context->yp = 0;
129     event_context->tolerance = 0;
130     event_context->within_tolerance = false;
131     event_context->item_to_select = NULL;
133     event_context->shape_repr = NULL;
134     event_context->shape_knot_holder = NULL;
136     flood_context->item = NULL;
138     new (&flood_context->sel_changed_connection) sigc::connection();
141 static void sp_flood_context_dispose(GObject *object)
143     SPFloodContext *rc = SP_FLOOD_CONTEXT(object);
144     SPEventContext *ec = SP_EVENT_CONTEXT(object);
146     rc->sel_changed_connection.disconnect();
147     rc->sel_changed_connection.~connection();
149     /* fixme: This is necessary because we do not grab */
150     if (rc->item) {
151         sp_flood_finish(rc);
152     }
154     if (ec->shape_repr) { // remove old listener
155         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
156         Inkscape::GC::release(ec->shape_repr);
157         ec->shape_repr = 0;
158     }
160     if (rc->_message_context) {
161         delete rc->_message_context;
162     }
164     G_OBJECT_CLASS(parent_class)->dispose(object);
167 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
168     NULL, /* child_added */
169     NULL, /* child_removed */
170     ec_shape_event_attr_changed,
171     NULL, /* content_changed */
172     NULL  /* order_changed */
173 };
175 /**
176 \brief  Callback that processes the "changed" signal on the selection;
177 destroys old and creates new knotholder
178 */
179 void sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer data)
181     SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
182     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
184     if (ec->shape_repr) { // remove old listener
185         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
186         Inkscape::GC::release(ec->shape_repr);
187         ec->shape_repr = 0;
188     }
190     SPItem *item = selection->singleItem();
191     if (item) {
192         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
193         if (shape_repr) {
194             ec->shape_repr = shape_repr;
195             Inkscape::GC::anchor(shape_repr);
196             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
197         }
198     }
201 static void sp_flood_context_setup(SPEventContext *ec)
203     SPFloodContext *rc = SP_FLOOD_CONTEXT(ec);
205     if (((SPEventContextClass *) parent_class)->setup) {
206         ((SPEventContextClass *) parent_class)->setup(ec);
207     }
209     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
210     if (item) {
211         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
212         if (shape_repr) {
213             ec->shape_repr = shape_repr;
214             Inkscape::GC::anchor(shape_repr);
215             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
216         }
217     }
219     rc->sel_changed_connection.disconnect();
220     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
221         sigc::bind(sigc::ptr_fun(&sp_flood_context_selection_changed), (gpointer)rc)
222     );
224     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
227 static void merge_pixel_with_background(unsigned char *orig, unsigned char *bg, unsigned char *base) {
228   for (int i = 0; i < 3; i++) { 
229     base[i] = (255 * (255 - bg[3])) / 255 + (bg[i] * bg[3]) / 255;
230     base[i] = (base[i] * (255 - orig[3])) / 255 + (orig[i] * orig[3]) / 255;
231   }
232   base[3] = 255;
235 inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
236   return px + (x + y * width) * 4;
239 enum PaintBucketChannels {
240     FLOOD_CHANNELS_RGB,
241     FLOOD_CHANNELS_R,
242     FLOOD_CHANNELS_G,
243     FLOOD_CHANNELS_B,
244     FLOOD_CHANNELS_ALPHA
245 };
247 GList * flood_channels_dropdown_items_list() {
248     GList *glist = NULL;
250     glist = g_list_append (glist, _("Visible Colors"));
251     glist = g_list_append (glist, _("Red"));
252     glist = g_list_append (glist, _("Green"));
253     glist = g_list_append (glist, _("Blue"));
254     glist = g_list_append (glist, _("Alpha"));
256     return glist;
259 static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int tolerance, PaintBucketChannels method) {
260   int diff = 0;
261   
262   switch (method) {
263     case FLOOD_CHANNELS_ALPHA:
264       return ((int)abs(check[3] - orig[3]) <= tolerance);
265     case FLOOD_CHANNELS_R:
266       return ((int)abs(check[0] - orig[0]) <= tolerance);
267     case FLOOD_CHANNELS_G:
268       return ((int)abs(check[1] - orig[1]) <= tolerance);
269     case FLOOD_CHANNELS_B:
270       return ((int)abs(check[2] - orig[2]) <= tolerance);
271     case FLOOD_CHANNELS_RGB:
272       unsigned char merged_orig[4];
273       unsigned char merged_check[4];
274       
275       merge_pixel_with_background(orig, dtc, merged_orig);
276       merge_pixel_with_background(check, dtc, merged_check);
277       
278       for (int i = 0; i < 3; i++) {
279         diff += (int)abs(merged_check[i] - merged_orig[i]);
280       }
281       return ((diff / 3) <= ((tolerance * 3) / 4));
282   }
283   
284   return false;
287 static bool try_add_to_queue(std::queue<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig, unsigned char *dtc, int x, int y, int width, int tolerance, PaintBucketChannels method, bool fill_switch) {
288   unsigned char *t = get_pixel(px, x, y, width);
289   if (compare_pixels(t, orig, dtc, tolerance, method)) {
290     unsigned char *trace_t = get_pixel(trace_px, x, y, width);
291     if (trace_t[3] != 255) {
292       if (fill_switch) {
293         fill_queue->push(NR::Point(x, y));
294       }
295     }
296     return false;
297   }
298   return true;
301 static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
302     SPDocument *document = sp_desktop_document(desktop);
303     
304     Inkscape::Trace::Potrace::PotraceTracingEngine pte;
305         
306     pte.setTraceType(Inkscape::Trace::Potrace::TRACE_BRIGHTNESS);
307     pte.setInvert(false);
309     Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(px, true);
310     
311     std::vector<Inkscape::Trace::TracingEngineResult> results = pte.trace(pixbuf);
312     
313     Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
314     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
316     long totalNodeCount = 0L;
318     double offset = prefs_get_double_attribute("tools.paintbucket", "offset", 0.0);
320     for (unsigned int i=0 ; i<results.size() ; i++) {
321         Inkscape::Trace::TracingEngineResult result = results[i];
322         totalNodeCount += result.getNodeCount();
324         Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
325         /* Set style */
326         sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
328         NArtBpath *bpath = sp_svg_read_path(result.getPathData().c_str());
329         Path *path = bpath_to_Path(bpath);
330         g_free(bpath);
332         if (offset != 0) {
333         
334             Shape *path_shape = new Shape();
335         
336             path->ConvertWithBackData(0.03);
337             path->Fill(path_shape, 0);
338             delete path;
339         
340             Shape *expanded_path_shape = new Shape();
341         
342             expanded_path_shape->ConvertToShape(path_shape, fill_nonZero);
343             path_shape->MakeOffset(expanded_path_shape, offset * desktop->current_zoom(), join_round, 4);
344             expanded_path_shape->ConvertToShape(path_shape, fill_positive);
346             Path *expanded_path = new Path();
347         
348             expanded_path->Reset();
349             expanded_path_shape->ConvertToForme(expanded_path);
350             expanded_path->ConvertEvenLines(1.0);
351             expanded_path->Simplify(1.0);
352         
353             delete path_shape;
354             delete expanded_path_shape;
355         
356             gchar *str = expanded_path->svg_dump_path();
357             if (str && *str) {
358                 pathRepr->setAttribute("d", str);
359                 g_free(str);
360             } else {
361                 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Too much inset</b>, the result is empty."));
362                 Inkscape::GC::release(pathRepr);
363                 g_free(str);
364                 return;
365             }
367             delete expanded_path;
369         } else {
370             gchar *str = path->svg_dump_path();
371             delete path;
372             pathRepr->setAttribute("d", str);
373             g_free(str);
374         }
376         layer_repr->addChild(pathRepr, NULL);
378         SPObject *reprobj = document->getObjectByRepr(pathRepr);
379         if (reprobj) {
380             sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
381             
382             // premultiply the item transform by the accumulated parent transform in the paste layer
383             NR::Matrix local = sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer()));
384             if (!local.test_identity()) {
385                 gchar const *t_str = pathRepr->attribute("transform");
386                 NR::Matrix item_t (NR::identity());
387                 if (t_str)
388                     sp_svg_transform_read(t_str, &item_t);
389                 item_t *= local.inverse();
390                 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
391                 gchar *affinestr=sp_svg_transform_write(item_t);
392                 pathRepr->setAttribute("transform", affinestr);
393                 g_free(affinestr);
394             }
396             Inkscape::Selection *selection = sp_desktop_selection(desktop);
397             selection->set(reprobj);
398             pathRepr->setPosition(-1);
400             desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with <b>%d</b> nodes created."), sp_nodes_in_path(SP_PATH(reprobj)));
401         }
402         
403         Inkscape::GC::release(pathRepr);
405     }
408 struct bitmap_coords_info {
409   bool is_left;
410   int x;
411   int y;
412   int y_limit;
413   int width;
414   int tolerance;
415   PaintBucketChannels method;
416   unsigned char *dtc;
417   bool top_fill;
418   bool bottom_fill;
419   NR::Rect bbox;
420   NR::Rect screen;
421 };
423 enum ScanlineCheckResult {
424   SCANLINE_CHECK_OK,
425   SCANLINE_CHECK_ABORTED,
426   SCANLINE_CHECK_BOUNDARY
427 };
429 static ScanlineCheckResult perform_bitmap_scanline_check(std::queue<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) {
430     bool aborted = false;
431     bool reached_screen_boundary = false;
432     bool ok;
433   
434     bool keep_tracing;
435     unsigned char *t, *trace_t;
436   
437     do {
438         ok = false;
439         if (bci.is_left) {
440             keep_tracing = (bci.x >= 0);
441         } else {
442             keep_tracing = (bci.x < bci.width);
443         }
444         
445         if (keep_tracing) {
446             t = get_pixel(px, bci.x, bci.y, bci.width);
447             if (compare_pixels(t, orig_color, bci.dtc, bci.tolerance, bci.method)) {
448                 for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
449                 trace_t = get_pixel(trace_px, bci.x, bci.y, bci.width);
450                 trace_t[3] = 255; 
451                 if (bci.y > 0) { 
452                     bci.top_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y - 1, bci.width, bci.tolerance, bci.method, bci.top_fill);
453                 }
454                 if (bci.y < bci.y_limit) { 
455                     bci.bottom_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y + 1, bci.width, bci.tolerance, bci.method, bci.bottom_fill);
456                 }
457                 if (bci.is_left) {
458                     bci.x--;
459                 } else {
460                     bci.x++;
461                 }
462                 ok = true;
463             }
464         } else {
465             if (bci.bbox.min()[NR::X] > bci.screen.min()[NR::X]) {
466                 aborted = true; break;
467             } else {
468                 reached_screen_boundary = true;
469             }
470         }
471     } while (ok);
472     
473     if (aborted) { return SCANLINE_CHECK_ABORTED; }
474     if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
475     return SCANLINE_CHECK_OK;
478 static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event) {
479     SPDesktop *desktop = event_context->desktop;
480     SPDocument *document = sp_desktop_document(desktop);
482     /* Create new arena */
483     NRArena *arena = NRArena::create();
484     unsigned dkey = sp_item_display_key_new(1);
486     sp_document_ensure_up_to_date (document);
487     
488     SPItem *document_root = SP_ITEM(SP_DOCUMENT_ROOT(document));
489     NR::Maybe<NR::Rect> bbox = document_root->getBounds(NR::identity());
491     if (!bbox) {
492       desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
493       return;
494     }
495     
496     double zoom_scale = desktop->current_zoom();
497     double padding = 1.6;
499     NR::Rect screen = desktop->get_display_area();
501     int width = (int)ceil(screen.extent(NR::X) * zoom_scale * padding);
502     int height = (int)ceil(screen.extent(NR::Y) * zoom_scale * padding);
504     NR::Point origin(screen.min()[NR::X],
505                      sp_document_height(document) - screen.extent(NR::Y) - screen.min()[NR::Y]);
506                      
507     origin[NR::X] = origin[NR::X] + (screen.extent(NR::X) * ((1 - padding) / 2));
508     origin[NR::Y] = origin[NR::Y] + (screen.extent(NR::Y) * ((1 - padding) / 2));
509     
510     NR::scale scale(zoom_scale, zoom_scale);
511     NR::Matrix affine = scale * NR::translate(-origin * scale);
512     
513     /* Create ArenaItems and set transform */
514     NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
515     nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
517     NRGC gc(NULL);
518     nr_matrix_set_identity(&gc.transform);
519     
520     NRRectL final_bbox;
521     final_bbox.x0 = 0;
522     final_bbox.y0 = 0;//row;
523     final_bbox.x1 = width;
524     final_bbox.y1 = height;//row + num_rows;
525     
526     nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
528     guchar *px = g_new(guchar, 4 * width * height);
529     //memset(px, 0x00, 4 * width * height);
530     
531     NRPixBlock B;
532     nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
533                               final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
534                               px, 4 * width, FALSE, FALSE );
535     
536     SPNamedView *nv = sp_desktop_namedview(desktop);
537     unsigned long bgcolor = nv->pagecolor;
538     
539     unsigned char dtc[4];
540     dtc[0] = NR_RGBA32_R(bgcolor);
541     dtc[1] = NR_RGBA32_G(bgcolor);
542     dtc[2] = NR_RGBA32_B(bgcolor);
543     dtc[3] = NR_RGBA32_A(bgcolor);
544     
545     for (int fy = 0; fy < height; fy++) {
546       guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs;
547       for (int fx = 0; fx < width; fx++) {
548         for (int i = 0; i < 4; i++) { 
549           *p++ = dtc[i];
550         }
551       }
552     }
554     nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
555     nr_pixblock_release(&B);
556     
557     // Hide items
558     sp_item_invoke_hide(SP_ITEM(sp_document_root(document)), dkey);
559     
560     nr_arena_item_unref(root);
561     nr_object_unref((NRObject *) arena);
562     
563     NR::Point pw = NR::Point(event->button.x / zoom_scale, sp_document_height(document) + (event->button.y / zoom_scale)) * affine;
564     
565     pw[NR::X] = (int)MIN(width - 1, MAX(0, pw[NR::X]));
566     pw[NR::Y] = (int)MIN(height - 1, MAX(0, pw[NR::Y]));
568     guchar *trace_px = g_new(guchar, 4 * width * height);
569     memset(trace_px, 0x00, 4 * width * height);
570     
571     std::queue<NR::Point> fill_queue;
572     fill_queue.push(pw);
573     
574     bool aborted = false;
575     int y_limit = height - 1;
576     
577     unsigned char orig_color[4];
578     unsigned char *orig_px = get_pixel(px, (int)pw[NR::X], (int)pw[NR::Y], width);
579     for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; }
581     unsigned char merged_orig[4];
583     merge_pixel_with_background(orig_color, dtc, merged_orig);
584     
585     int tolerance = (255 * prefs_get_int_attribute_limited("tools.paintbucket", "tolerance", 1, 0, 100)) / 100;
586     PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0);
588     bool reached_screen_boundary = false;
590     bitmap_coords_info bci;
591     
592     bci.y_limit = y_limit;
593     bci.width = width;
594     bci.tolerance = tolerance;
595     bci.method = method;
596     bci.bbox = *bbox;
597     bci.screen = screen;
598     bci.dtc = dtc;
600     while (!fill_queue.empty() && !aborted) {
601       NR::Point cp = fill_queue.front();
602       fill_queue.pop();
603       unsigned char *s = get_pixel(px, (int)cp[NR::X], (int)cp[NR::Y], width);
604       
605       // same color at this point
606       if (compare_pixels(s, orig_color, dtc, tolerance, method)) {
607         int x = (int)cp[NR::X];
608         int y = (int)cp[NR::Y];
609         
610         bool top_fill = true;
611         bool bottom_fill = true;
612         
613         if (y > 0) { 
614           top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y - 1, width, tolerance, method, top_fill);
615         } else {
616           if (bbox->min()[NR::Y] > screen.min()[NR::Y]) {
617             aborted = true; break;
618           } else {
619             reached_screen_boundary = true;
620           }
621         }
622         if (y < y_limit) { 
623           bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y + 1, width, tolerance, method, bottom_fill);
624         } else {
625           if (bbox->max()[NR::Y] < screen.max()[NR::Y]) {
626             aborted = true; break;
627           } else {
628             reached_screen_boundary = true;
629           }
630         }
631         
632         bci.is_left = true;
633         bci.x = x;
634         bci.y = y;
635         bci.top_fill = top_fill;
636         bci.bottom_fill = bottom_fill;
637         
638         ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
639         
640         switch (result) {
641             case SCANLINE_CHECK_ABORTED:
642                 aborted = true;
643                 break;
644             case SCANLINE_CHECK_BOUNDARY:
645                 reached_screen_boundary = true;
646                 break;
647             default:
648                 break;
649         }
650         
651         bci.is_left = false;
652         bci.x = x + 1;
653         bci.y = y;
654         bci.top_fill = top_fill;
655         bci.bottom_fill = bottom_fill;
656         
657         result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
658         
659         switch (result) {
660             case SCANLINE_CHECK_ABORTED:
661                 aborted = true;
662                 break;
663             case SCANLINE_CHECK_BOUNDARY:
664                 reached_screen_boundary = true;
665                 break;
666             default:
667                 break;
668         }
669       }
670     }
671     
672     g_free(px);
673     
674     if (aborted) {
675       g_free(trace_px);
676       desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
677       return;
678     }
679     
680     if (reached_screen_boundary) {
681       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.")); 
682     }
683     
684     GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(trace_px,
685                                       GDK_COLORSPACE_RGB,
686                                       TRUE,
687                                       8, width, height, width * 4,
688                                       (GdkPixbufDestroyNotify)g_free,
689                                       NULL);
691     NR::Matrix inverted_affine = NR::Matrix(affine).inverse();
692     
693     do_trace(pixbuf, desktop, inverted_affine);
695     g_free(trace_px);
696     
697     sp_document_done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
700 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
702     gint ret = FALSE;
704     switch (event->type) {
705     case GDK_BUTTON_PRESS:
706         break;
707         // motion and release are always on root (why?)
708     default:
709         break;
710     }
712     if (((SPEventContextClass *) parent_class)->item_handler) {
713         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
714     }
716     return ret;
719 static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
721     gint ret = FALSE;
722     switch (event->type) {
723     case GDK_BUTTON_PRESS:
724         if ( event->button.button == 1 ) {
725             sp_flood_do_flood_fill(event_context, event);
727             ret = TRUE;
728         }
729         break;
730     case GDK_KEY_PRESS:
731         switch (get_group0_keyval (&event->key)) {
732         case GDK_Up:
733         case GDK_Down:
734         case GDK_KP_Up:
735         case GDK_KP_Down:
736             // prevent the zoom field from activation
737             if (!MOD__CTRL_ONLY)
738                 ret = TRUE;
739             break;
740         default:
741             break;
742         }
743         break;
744     default:
745         break;
746     }
748     if (!ret) {
749         if (((SPEventContextClass *) parent_class)->root_handler) {
750             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
751         }
752     }
754     return ret;
758 static void sp_flood_finish(SPFloodContext *rc)
760     rc->_message_context->clear();
762     if ( rc->item != NULL ) {
763         SPDesktop * desktop;
765         desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
767         SP_OBJECT(rc->item)->updateRepr();
769         sp_canvas_end_forced_full_redraws(desktop->canvas);
771         sp_desktop_selection(desktop)->set(rc->item);
772         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
773                          _("Fill bounded area"));
775         rc->item = NULL;
776     }
779 void flood_channels_changed(GtkComboBox *cbox, GtkWidget *tbl)
781     prefs_set_int_attribute("tools.paintbucket", "channels", (gint)gtk_combo_box_get_active(cbox));
784 /*
785   Local Variables:
786   mode:c++
787   c-file-style:"stroustrup"
788   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
789   indent-tabs-mode:nil
790   fill-column:99
791   End:
792 */
793 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :