Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / flood-context.cpp
index 47e3d3d2beaad4a96846a87a55d41e4cf0c0024b..dd26782a37897239b14882be3fad5febee95c517 100644 (file)
@@ -1,21 +1,24 @@
-#define __SP_FLOOD_CONTEXT_C__
-
-/** \file
-* 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>
-*
-* 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
-*/
+/** @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 <gdk/gdkkeysyms.h>
 #include <queue>
 #include "object-edit.h"
 #include "xml/repr.h"
 #include "xml/node-event-vector.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "context-fns.h"
 #include "rubberband.h"
+#include "shape-editor.h"
 
 #include "display/nr-arena-item.h"
 #include "display/nr-arena.h"
@@ -70,6 +74,8 @@
 #include "trace/imagemap.h"
 #include "trace/potrace/inkscape-potrace.h"
 
+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);
@@ -130,9 +136,6 @@ static void sp_flood_context_init(SPFloodContext *flood_context)
     event_context->within_tolerance = false;
     event_context->item_to_select = NULL;
 
-    event_context->shape_repr = NULL;
-    event_context->shape_knot_holder = NULL;
-
     flood_context->item = NULL;
 
     new (&flood_context->sel_changed_connection) sigc::connection();
@@ -146,17 +149,14 @@ static void sp_flood_context_dispose(GObject *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 (ec->shape_repr) { // remove old listener
-        sp_repr_remove_listener_by_data(ec->shape_repr, ec);
-        Inkscape::GC::release(ec->shape_repr);
-        ec->shape_repr = 0;
-    }
-
     if (rc->_message_context) {
         delete rc->_message_context;
     }
@@ -164,14 +164,6 @@ static void sp_flood_context_dispose(GObject *object)
     G_OBJECT_CLASS(parent_class)->dispose(object);
 }
 
-static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
-    NULL, /* child_added */
-    NULL, /* child_removed */
-    ec_shape_event_attr_changed,
-    NULL, /* content_changed */
-    NULL  /* order_changed */
-};
-
 /**
 \brief  Callback that processes the "changed" signal on the selection;
 destroys old and creates new knotholder
@@ -181,21 +173,9 @@ void sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer
     SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
 
-    if (ec->shape_repr) { // remove old listener
-        sp_repr_remove_listener_by_data(ec->shape_repr, ec);
-        Inkscape::GC::release(ec->shape_repr);
-        ec->shape_repr = 0;
-    }
-
-    SPItem *item = selection->singleItem();
-    if (item) {
-        Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
-        if (shape_repr) {
-            ec->shape_repr = shape_repr;
-            Inkscape::GC::anchor(shape_repr);
-            sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
-        }
-    }
+    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)
@@ -206,14 +186,11 @@ static void sp_flood_context_setup(SPEventContext *ec)
         ((SPEventContextClass *) parent_class)->setup(ec);
     }
 
+    ec->shape_editor = new ShapeEditor(ec->desktop);
+
     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
     if (item) {
-        Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
-        if (shape_repr) {
-            ec->shape_repr = shape_repr;
-            Inkscape::GC::anchor(shape_repr);
-            sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
-        }
+        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
     }
 
     rc->sel_changed_connection.disconnect();
@@ -223,7 +200,8 @@ static void sp_flood_context_setup(SPEventContext *ec)
 
     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
 
-    if (prefs_get_int_attribute("tools.paintbucket", "selcue", 0) != 0) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (prefs->getBool("/tools/paintbucket/selcue")) {
         rc->enableSelectionCue();
     }
 }
@@ -285,10 +263,10 @@ GList * flood_channels_dropdown_items_list() {
 GList * flood_autogap_dropdown_items_list() {
     GList *glist = NULL;
 
-    glist = g_list_append (glist, _("None"));
-    glist = g_list_append (glist, _("Small"));
-    glist = g_list_append (glist, _("Medium"));
-    glist = g_list_append (glist, _("Large"));
+    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;
 }
@@ -437,15 +415,17 @@ static void do_trace(bitmap_coords_info bci, guchar *trace_px, SPDesktop *deskto
     }
 
     Inkscape::Trace::Potrace::PotraceTracingEngine pte;
+    pte.keepGoing = 1;
     std::vector<Inkscape::Trace::TracingEngineResult> results = pte.traceGrayMap(gray_map);
     gray_map->destroy(gray_map);
 
-    Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+    //XML Tree being used here directly while it shouldn't be...."
+    Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
 
     long totalNodeCount = 0L;
 
-    double offset = prefs_get_double_attribute("tools.paintbucket", "offset", 0.0);
+    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];
@@ -453,7 +433,7 @@ static void do_trace(bitmap_coords_info bci, guchar *trace_px, SPDesktop *deskto
 
         Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
         /* Set style */
-        sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
+        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;
@@ -503,14 +483,14 @@ static void do_trace(bitmap_coords_info bci, guchar *trace_px, SPDesktop *deskto
             g_free(str);
         }
 
-        layer_repr->addChild(pathRepr, NULL);
+        desktop->currentLayer()->addChild(pathRepr,NULL);
 
         SPObject *reprobj = document->getObjectByRepr(pathRepr);
         if (reprobj) {
-            sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
+            SP_ITEM(reprobj)->doWriteTransform(pathRepr, transform, NULL);
             
             // premultiply the item transform by the accumulated parent transform in the paste layer
-            Geom::Matrix local (sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer())));
+            Geom::Matrix local (SP_GROUP(desktop->currentLayer())->i2doc_affine());
             if (!local.isIdentity()) {
                 gchar const *t_str = pathRepr->attribute("transform");
                 Geom::Matrix item_t (Geom::identity());
@@ -796,12 +776,12 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
 
     /* Create new arena */
     NRArena *arena = NRArena::create();
-    unsigned dkey = sp_item_display_key_new(1);
+    unsigned dkey = SPItem::display_key_new(1);
 
-    sp_document_ensure_up_to_date (document);
+    document->ensureUpToDate();
     
-    SPItem *document_root = SP_ITEM(SP_DOCUMENT_ROOT(document));
-    boost::optional<Geom::Rect> bbox = document_root->getBounds(Geom::identity());
+    SPItem *document_root = SP_ITEM(document->getRoot());
+    Geom::OptRect bbox = document_root->getBounds(Geom::identity());
 
     if (!bbox) {
         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
@@ -820,7 +800,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     unsigned int height = (int)ceil(screen.height() * zoom_scale * padding);
 
     Geom::Point origin(screen.min()[Geom::X],
-                       sp_document_height(document) - screen.height() - screen.min()[Geom::Y]);
+                       document->getHeight() - screen.height() - screen.min()[Geom::Y]);
                     
     origin[Geom::X] = origin[Geom::X] + (screen.width() * ((1 - padding) / 2));
     origin[Geom::Y] = origin[Geom::Y] + (screen.height() * ((1 - padding) / 2));
@@ -829,7 +809,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     Geom::Matrix affine = scale * Geom::Translate(-origin * scale);
     
     /* Create ArenaItems and set transform */
-    NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
+    NRArenaItem *root = SP_ITEM(document->getRoot())->invoke_show( arena, dkey, SP_ITEM_SHOW_DISPLAY);
     nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
 
     NRGC gc(NULL);
@@ -872,7 +852,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     nr_pixblock_release(&B);
     
     // Hide items
-    sp_item_invoke_hide(SP_ITEM(sp_document_root(document)), dkey);
+    SP_ITEM(document->getRoot())->invoke_hide(dkey);
     
     nr_object_unref((NRObject *) arena);
     
@@ -887,8 +867,9 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     bool aborted = false;
     int y_limit = height - 1;
 
-    PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0);
-    int threshold = prefs_get_int_attribute_limited("tools.paintbucket", "threshold", 1, 0, 100);
+    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:
@@ -914,19 +895,19 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     bci.bbox = *bbox;
     bci.screen = screen;
     bci.dtc = dtc;
-    bci.radius = prefs_get_int_attribute_limited("tools.paintbucket", "autogap", 0, 0, 3);
+    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::Rubberband *r = Inkscape::Rubberband::get(desktop);
+        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, sp_document_height(document) + (fill_points[i][Geom::Y] / zoom_scale)) * affine;
+        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]));
@@ -1140,7 +1121,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
 
     g_free(trace_px);
     
-    sp_document_done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
+    DocumentUndo::done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
 }
 
 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
@@ -1157,10 +1138,9 @@ static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem
             
             SPItem *item = sp_event_context_find_item (desktop, button_w, TRUE, TRUE);
             
-            Inkscape::XML::Node *pathRepr = SP_OBJECT_REPR(item);
-            /* Set style */
-            sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
-            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object"));
+            // 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;
@@ -1227,7 +1207,7 @@ static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEven
 
     case GDK_BUTTON_RELEASE:
         if (event->button.button == 1 && !event_context->space_panning) {
-            Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
+            Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
             if (r->is_started()) {
                 // set "busy" cursor
                 desktop->setWaitingCursor();
@@ -1299,7 +1279,7 @@ static void sp_flood_finish(SPFloodContext *rc)
         sp_canvas_end_forced_full_redraws(desktop->canvas);
 
         sp_desktop_selection(desktop)->set(rc->item);
-        sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
+        DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
                         _("Fill bounded area"));
 
         rc->item = NULL;
@@ -1308,7 +1288,8 @@ static void sp_flood_finish(SPFloodContext *rc)
 
 void flood_channels_set_channels( gint channels )
 {
-    prefs_set_int_attribute("tools.paintbucket", "channels", channels);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setInt("/tools/paintbucket/channels", channels);
 }
 
 /*
@@ -1320,4 +1301,4 @@ void flood_channels_set_channels( gint channels )
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :