Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / star-context.cpp
index d6e9f85837cdb08590db09e35d3491f7afdc59f5..0fb20a2ab0a044ac928e5acc24ce919d162c7e6b 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_STAR_CONTEXT_C__
-
 /*
  * Star drawing context
  *
@@ -7,6 +5,8 @@
  *   Mitsuru Oka <oka326@parkcity.ne.jp>
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2002 Lauris Kaplinski
  * Copyright (C) 2001-2002 Mitsuru Oka
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cstring>
+#include <string>
 
 #include <gdk/gdkkeysyms.h>
 
 #include "sp-namedview.h"
 #include "selection.h"
 #include "desktop-handles.h"
-#include "desktop-affine.h"
 #include "snap.h"
 #include "desktop.h"
 #include "desktop-style.h"
 #include "message-context.h"
+#include "libnr/nr-macros.h"
 #include "pixmaps/cursor-star.xpm"
 #include "sp-metrics.h"
 #include <glibmm/i18n.h>
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "xml/repr.h"
 #include "xml/node-event-vector.h"
 #include "object-edit.h"
 #include "context-fns.h"
+#include "shape-editor.h"
 
 #include "star-context.h"
 
+using Inkscape::DocumentUndo;
+
 static void sp_star_context_class_init (SPStarContextClass * klass);
 static void sp_star_context_init (SPStarContext * star_context);
 static void sp_star_context_dispose (GObject *object);
 
 static void sp_star_context_setup (SPEventContext *ec);
-static void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
+static void sp_star_context_finish(SPEventContext *ec);
+static void sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val);
 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
 
-static void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
+static void sp_star_drag (SPStarContext * sc, Geom::Point p, guint state);
 static void sp_star_finish (SPStarContext * sc);
+static void sp_star_cancel(SPStarContext * sc);
 
 static SPEventContextClass * parent_class;
 
@@ -85,6 +95,7 @@ sp_star_context_class_init (SPStarContextClass * klass)
     object_class->dispose = sp_star_context_dispose;
 
     event_context_class->setup = sp_star_context_setup;
+    event_context_class->finish = sp_star_context_finish;
     event_context_class->set = sp_star_context_set;
     event_context_class->root_handler = sp_star_context_root_handler;
 }
@@ -102,9 +113,7 @@ sp_star_context_init (SPStarContext * star_context)
     event_context->tolerance = 0;
     event_context->within_tolerance = false;
     event_context->item_to_select = NULL;
-
-    event_context->shape_repr = NULL;
-    event_context->shape_knot_holder = NULL;
+    event_context->tool_url = "/tools/shapes/star";
 
     star_context->item = NULL;
 
@@ -115,6 +124,21 @@ sp_star_context_init (SPStarContext * star_context)
     new (&star_context->sel_changed_connection) sigc::connection();
 }
 
+static void sp_star_context_finish(SPEventContext *ec)
+{
+    SPStarContext *sc = SP_STAR_CONTEXT(ec);
+       SPDesktop *desktop = ec->desktop;
+
+       sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
+       sp_star_finish(sc);
+    sc->sel_changed_connection.disconnect();
+
+    if (((SPEventContextClass *) parent_class)->finish) {
+               ((SPEventContextClass *) parent_class)->finish(ec);
+       }
+}
+
+
 static void
 sp_star_context_dispose (GObject *object)
 {
@@ -126,16 +150,8 @@ sp_star_context_dispose (GObject *object)
     sc->sel_changed_connection.disconnect();
     sc->sel_changed_connection.~connection();
 
-    if (ec->shape_knot_holder) {
-        sp_knot_holder_destroy (ec->shape_knot_holder);
-        ec->shape_knot_holder = NULL;
-    }
-
-    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;
-    }
+    delete ec->shape_editor;
+    ec->shape_editor = NULL;
 
     /* fixme: This is necessary because we do not grab */
     if (sc->item) sp_star_finish (sc);
@@ -147,14 +163,6 @@ sp_star_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
@@ -168,27 +176,9 @@ sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer dat
     SPStarContext *sc = SP_STAR_CONTEXT (data);
     SPEventContext *ec = SP_EVENT_CONTEXT (sc);
 
-    if (ec->shape_knot_holder) { // desktroy knotholder
-        sp_knot_holder_destroy (ec->shape_knot_holder);
-        ec->shape_knot_holder = NULL;
-    }
-
-    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;
-    }
-
+    ec->shape_editor->unset_item(SH_KNOTHOLDER);
     SPItem *item = selection->singleItem();
-    if (item) {
-        ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
-        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);
 }
 
 static void
@@ -205,27 +195,23 @@ sp_star_context_setup (SPEventContext *ec)
     sp_event_context_read (ec, "rounded");
     sp_event_context_read (ec, "randomized");
 
-    Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
+    ec->shape_editor = new ShapeEditor(ec->desktop);
 
-    SPItem *item = selection->singleItem();
-        if (item) {
-            ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
-            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);
-            }
-        }
+    SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
+    if (item) {
+        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
+    }
 
+    Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
     sc->sel_changed_connection.disconnect();
     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
 
-    if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (prefs->getBool("/tools/shapes/selcue")) {
         ec->enableSelectionCue();
     }
 
-    if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
+    if (prefs->getBool("/tools/shapes/gradientdrag")) {
         ec->enableGrDrag();
     }
 
@@ -233,24 +219,21 @@ sp_star_context_setup (SPEventContext *ec)
 }
 
 static void
-sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
+sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
 {
     SPStarContext *sc = SP_STAR_CONTEXT (ec);
-    if (!strcmp (key, "magnitude")) {
-        sc->magnitude = (val) ? atoi (val) : 5;
-        sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
-    } else if (!strcmp (key, "proportion")) {
-        sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
-        sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
-    } else if (!strcmp (key, "isflatsided")) {
-        if (val && !strcmp(val, "true"))
-            sc->isflatsided = true;
-        else
-            sc->isflatsided = false;
-    } else if (!strcmp (key, "rounded")) {
-        sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
-    } else if (!strcmp (key, "randomized")) {
-        sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
+    Glib::ustring path = val->getEntryName();
+
+    if (path == "magnitude") {
+        sc->magnitude = NR_CLAMP(val->getInt(5), 3, 1024);
+    } else if (path == "proportion") {
+        sc->proportion = NR_CLAMP(val->getDouble(0.5), 0.01, 2.0);
+    } else if (path == "isflatsided") {
+        sc->isflatsided = val->getBool();
+    } else if (path == "rounded") {
+        sc->rounded = val->getDouble();
+    } else if (path == "randomized") {
+        sc->randomized = val->getDouble();
     }
 }
 
@@ -260,33 +243,38 @@ static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent
 
     SPDesktop *desktop = event_context->desktop;
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
 
-    event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
+    event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
 
     gint ret = FALSE;
 
     switch (event->type) {
     case GDK_BUTTON_PRESS:
-        if (event->button.button == 1) {
+        if (event->button.button == 1 && !event_context->space_panning) {
 
             dragging = TRUE;
 
             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
 
-            SnapManager const &m = desktop->namedview->snap_manager;
-            sc->center = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
-
+            /* Snap center */
+            SnapManager &m = desktop->namedview->snap_manager;
+            m.setup(desktop, true);
+            m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
+            m.unSetup();
             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
-                                GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
+                                GDK_POINTER_MOTION_MASK |
+                                GDK_POINTER_MOTION_HINT_MASK |
+                                GDK_BUTTON_PRESS_MASK,
                                 NULL, event->button.time);
             ret = TRUE;
         }
         break;
     case GDK_MOTION_NOTIFY:
-        if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
+        if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
 
             if ( event_context->within_tolerance
                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
@@ -298,20 +286,30 @@ static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent
             // motion notify coordinates as given (no snapping back to origin)
             event_context->within_tolerance = false;
 
-            NR::Point const motion_w(event->motion.x, event->motion.y);
-            NR::Point motion_dt(event_context->desktop->w2d(motion_w));
-            
-            SnapManager const &m = desktop->namedview->snap_manager;
-            motion_dt = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, motion_dt, sc->item).getPoint();
-            
+            Geom::Point const motion_w(event->motion.x, event->motion.y);
+            Geom::Point motion_dt(desktop->w2d(motion_w));
+
             sp_star_drag (sc, motion_dt, event->motion.state);
+
+            gobble_motion_events(GDK_BUTTON1_MASK);
+
             ret = TRUE;
+        } else if (!sp_event_context_knot_mouseover(event_context)) {
+            SnapManager &m = desktop->namedview->snap_manager;
+            m.setup(desktop);
+
+            Geom::Point const motion_w(event->motion.x, event->motion.y);
+            Geom::Point motion_dt(desktop->w2d(motion_w));
+
+            m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
+            m.unSetup();
         }
         break;
     case GDK_BUTTON_RELEASE:
         event_context->xp = event_context->yp = 0;
-        if (event->button.button == 1) {
+        if (event->button.button == 1 && !event_context->space_panning) {
             dragging = FALSE;
+            sp_event_context_discard_delayed_snap_event(event_context);
             if (!event_context->within_tolerance) {
                 // we've been dragging, finish the star
                 sp_star_finish (sc);
@@ -362,8 +360,28 @@ static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent
             }
             break;
         case GDK_Escape:
-            sp_desktop_selection(desktop)->clear();
-            //TODO: make dragging escapable by Esc
+               if (dragging) {
+                       dragging = false;
+                       sp_event_context_discard_delayed_snap_event(event_context);
+                       // if drawing, cancel, otherwise pass it up for deselecting
+                       sp_star_cancel(sc);
+                       ret = TRUE;
+               }
+               break;
+        case GDK_space:
+            if (dragging) {
+                sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
+                                      event->button.time);
+                dragging = false;
+                sp_event_context_discard_delayed_snap_event(event_context);
+                if (!event_context->within_tolerance) {
+                    // we've been dragging, finish the star
+                    sp_star_finish(sc);
+                }
+                // do not return true, so that space would work switching to selector
+            }
+            break;
+
         default:
             break;
         }
@@ -396,11 +414,12 @@ static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent
     return ret;
 }
 
-static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
+static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
 {
     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
 
-    int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
     if (!sc->item) {
 
@@ -408,47 +427,49 @@ static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
             return;
         }
 
-        /* Create object */
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
+        // Create object
+        Inkscape::XML::Document *xml_doc = SP_EVENT_CONTEXT_DOCUMENT(sc)->getReprDoc();
         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
         repr->setAttribute("sodipodi:type", "star");
 
-        /* Set style */
-        sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
+        // Set style
+        sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
 
         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
         Inkscape::GC::release(repr);
-        sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
+        sc->item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
         sc->item->updateRepr();
 
         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
     }
 
-    NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
-    NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
-
     /* Snap corner point with no constraints */
-    SnapManager const &m = desktop->namedview->snap_manager;
-    p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
+    SnapManager &m = desktop->namedview->snap_manager;
+    m.setup(desktop, true, sc->item);
+    Geom::Point pt2g = to_2geom(p);
+    m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
+    m.unSetup();
+    Geom::Point const p0 = desktop->dt2doc(sc->center);
+    Geom::Point const p1 = desktop->dt2doc(pt2g);
 
     SPStar *star = SP_STAR(sc->item);
 
     double const sides = (gdouble) sc->magnitude;
-    NR::Point const d = p1 - p0;
-    NR::Coord const r1 = NR::L2(d);
-    double arg1 = atan2(d);
+    Geom::Point const d = p1 - p0;
+    Geom::Coord const r1 = Geom::L2(d);
+    double arg1 = atan2(from_2geom(d));
 
     if (state & GDK_CONTROL_MASK) {
         /* Snap angle */
         arg1 = sp_round(arg1, M_PI / snaps);
     }
 
-    sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
+    sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
 
     /* status text */
     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
-    sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
+    sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
                                ( sc->isflatsided?
                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
@@ -463,21 +484,53 @@ sp_star_finish (SPStarContext * sc)
     sc->_message_context->clear();
 
     if (sc->item != NULL) {
+        SPStar *star = SP_STAR(sc->item);
+        if (star->r[1] == 0) {
+            sp_star_cancel(sc); // Don't allow the creating of zero sized arc, for example when the start and and point snap to the snap grid point
+            return;
+        }
+
+        // Set transform center, so that odd stars rotate correctly
+        // LP #462157
+        sc->item->setCenter(sc->center);
+
         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
         SPObject *object = SP_OBJECT(sc->item);
 
-        sp_shape_set_shape(SP_SHAPE(sc->item));
+        (SP_SHAPE(sc->item))->setShape();
 
         object->updateRepr(SP_OBJECT_WRITE_EXT);
 
         sp_canvas_end_forced_full_redraws(desktop->canvas);
 
         sp_desktop_selection(desktop)->set(sc->item);
-        sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR, 
-                         _("Create star"));
+        DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
+                           _("Create star"));
+
+        sc->item = NULL;
+    }
+}
+
+static void sp_star_cancel(SPStarContext *sc)
+{
+    SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
 
+    sp_desktop_selection(desktop)->clear();
+    sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
+
+    if (sc->item != NULL) {
+        SP_OBJECT(sc->item)->deleteObject();
         sc->item = NULL;
     }
+
+    sc->within_tolerance = false;
+    sc->xp = 0;
+    sc->yp = 0;
+    sc->item_to_select = NULL;
+
+    sp_canvas_end_forced_full_redraws(desktop->canvas);
+
+    DocumentUndo::cancel(sp_desktop_document(desktop));
 }
 
 /*
@@ -489,4 +542,4 @@ sp_star_finish (SPStarContext * sc)
   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 :