Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / gradient-drag.cpp
index e7536a86ae35684816d05e6f5d86e3d31b074c29..fe8297286c2489562352fa06cd128a7c384a910a 100644 (file)
@@ -5,6 +5,7 @@
  *   bulia byak <buliabyak@users.sf.net>
  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
  *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007 Johan Engelen
  * Copyright (C) 2005,2010 Authors
@@ -24,6 +25,7 @@
 #include "selection.h"
 #include "desktop.h"
 #include "desktop-style.h"
+#include "desktop-handles.h"
 #include "document.h"
 #include "display/sp-ctrlline.h"
 #include "display/sp-canvas-util.h"
@@ -44,6 +46,8 @@
 #include "sp-namedview.h"
 #include "selection-chemistry.h"
 
+using Inkscape::DocumentUndo;
+
 #define GR_KNOT_COLOR_NORMAL 0xffffff00
 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
@@ -185,7 +189,7 @@ Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull )
         Glib::ustring::size_type pos = colorStr.find("url(#");
         if ( pos != Glib::ustring::npos ) {
             Glib::ustring targetName = colorStr.substr(pos + 5, colorStr.length() - 6);
-            const GSList *gradients = sp_document_get_resource_list(desktop->doc(), "gradient");
+            const GSList *gradients = desktop->doc()->getResourceList("gradient");
             for (const GSList *item = gradients; item; item = item->next) {
                 SPGradient* grad = SP_GRADIENT(item->data);
                 if ( targetName == grad->getId() ) {
@@ -194,7 +198,7 @@ Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull )
                     if (firstStop) {
                         Glib::ustring stopColorStr;
                         if (firstStop->currentColor) {
-                            stopColorStr = sp_object_get_style_property(firstStop, "color", NULL);
+                            stopColorStr = firstStop->getStyleProperty("color", NULL);
                         } else {
                             stopColorStr = firstStop->specified_color.toString();
                         }
@@ -568,22 +572,6 @@ SPObject *GrDraggable::getServer()
     return server;
 }
 
-static
-boost::optional<Geom::Point>
-get_snap_vector (Geom::Point p, Geom::Point o, double snap, double initial)
-{
-    double r = L2 (p - o);
-    if (r < 1e-3) {
-        return boost::optional<Geom::Point>();
-    }
-
-    double angle = atan2 (p - o);
-    // snap angle to snaps increments, starting from initial:
-    double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
-    // calculate the new position and subtract p to get the vector:
-    return (o + r * Geom::Point(cos(a_snapped), sin(a_snapped)) - p);
-}
-
 static void
 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
 {
@@ -638,22 +626,24 @@ gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gp
                 d_new->updateKnotShape ();
                 d_new->updateTip ();
                 d_new->updateDependencies(true);
-                sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
-                                  _("Merge gradient handles"));
+                DocumentUndo::done(sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
+                                   _("Merge gradient handles"));
                 return;
             }
         }
     }
 
-    m.setup(desktop);
     if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
+        m.setup(desktop);
         Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
+        m.unSetup();
         if (s.getSnapped()) {
             p = s.getPoint();
             sp_knot_moveto (knot, p);
         }
     } else if (state & GDK_CONTROL_MASK) {
         SnappedConstraints sc;
+        Inkscape::SnapCandidatePoint scp = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
         unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
         /* 0 means no snapping. */
@@ -699,36 +689,38 @@ gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gp
                 dr_snap = dragger->point_original;
             }
 
-            boost::optional<Geom::Point> snap_vector;
+            // dr_snap contains the origin of the gradient, whereas p will be the new endpoint which we will try to snap now
+            Inkscape::SnappedPoint sp;
             if (dr_snap.isFinite()) {
+                m.setup(desktop);
                 if (state & GDK_MOD1_MASK) {
                     // with Alt, snap to the original angle and its perpendiculars
-                    snap_vector = get_snap_vector (p, dr_snap, M_PI/2, Geom::atan2 (dragger->point_original - dr_snap));
+                    sp = m.constrainedAngularSnap(scp, dragger->point_original, dr_snap, 2);
                 } else {
                     // with Ctrl, snap to M_PI/snaps
-                    snap_vector = get_snap_vector (p, dr_snap, M_PI/snaps, 0);
-                }
-                if (snap_vector) {
-                    Inkscape::Snapper::SnapConstraint cl(dr_snap, p + *snap_vector - dr_snap);
-                    Inkscape::SnappedPoint s = m.constrainedSnap(Inkscape::SnapCandidatePoint(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE), cl);
-                    if (s.getSnapped()) {
-                        s.setTransformation(s.getPoint() - p);
-                        sc.points.push_back(s);
-                    } else {
-                        Inkscape::SnappedPoint dummy(p + *snap_vector, Inkscape::SNAPSOURCE_OTHER_HANDLE, 0, Inkscape::SNAPTARGET_CONSTRAINED_ANGLE, Geom::L2(*snap_vector), 10000, true, true, false);
-                        dummy.setTransformation(*snap_vector);
-                        sc.points.push_back(dummy);
-                    }
+                    sp = m.constrainedAngularSnap(scp, boost::optional<Geom::Point>(), dr_snap, snaps);
                 }
+                m.unSetup();
+                sc.points.push_back(sp);
             }
         }
 
-        Inkscape::SnappedPoint bsp = m.findBestSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE), sc, true); // snap indicator will be displayed if needed
-
-        if (bsp.getSnapped()) {
-            p += bsp.getTransformation();
-            sp_knot_moveto (knot, p);
+        m.setup(desktop, false); // turn of the snap indicator temporarily
+        Inkscape::SnappedPoint bsp = m.findBestSnap(scp, sc, true);
+        m.unSetup();
+        if (!bsp.getSnapped()) {
+            // If we didn't truly snap to an object or to a grid, then we will still have to look for the
+            // closest projection onto one of the constraints. findBestSnap() will not do this for us
+            for (std::list<Inkscape::SnappedPoint>::const_iterator i = sc.points.begin(); i != sc.points.end(); i++) {
+                if (i == sc.points.begin() || (Geom::L2((*i).getPoint() - p) < Geom::L2(bsp.getPoint() - p))) {
+                    bsp.setPoint((*i).getPoint());
+                    bsp.setTarget(Inkscape::SNAPTARGET_CONSTRAINED_ANGLE);
+                }
+            }
         }
+        //p = sc.points.front().getPoint();
+        p = bsp.getPoint();
+        sp_knot_moveto (knot, p);
     }
 
     drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
@@ -864,6 +856,7 @@ gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, gu
             SnapManager &m = desktop->namedview->snap_manager;
             m.setup(desktop);
             m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
+            m.unSetup();
         }
     }
     Geom::Point displacement = p - dragger->point;
@@ -938,8 +931,8 @@ gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
     dragger->updateDependencies(true);
 
     // we did an undoable action
-    sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
-                      _("Move gradient handle"));
+    DocumentUndo::done(sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
+                       _("Move gradient handle"));
 }
 
 /**
@@ -991,8 +984,8 @@ gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
             }
 
             SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
-            sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
-                      _("Delete gradient stop"));
+            DocumentUndo::done(SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
+                               _("Delete gradient stop"));
         }
     } else {
     // select the dragger
@@ -1151,7 +1144,7 @@ GrDragger::updateTip ()
 
     if (g_slist_length (this->draggables) == 1) {
         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
-        char *item_desc = sp_item_description(draggable->item);
+        char *item_desc = draggable->item->description();
         switch (draggable->point_type) {
             case POINT_LG_MID:
             case POINT_RG_MID1:
@@ -1795,7 +1788,7 @@ GrDrag::updateLevels ()
 
     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
         SPItem *item = SP_ITEM(i->data);
-        Geom::OptRect rect = sp_item_bbox_desktop (item);
+        Geom::OptRect rect = item->getBboxDesktop ();
         if (rect) {
             // Remember the edges of the bbox and the center axis
             hor_levels.push_back(rect->min()[Geom::Y]);
@@ -1874,7 +1867,7 @@ GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
 
     if (write_repr && did) {
         // we did an undoable action
-        sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
+        DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
                                 _("Move gradient handle(s)"));
         return;
     }
@@ -1910,7 +1903,7 @@ GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
 
         if (write_repr && did) {
             // we did an undoable action
-            sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
+            DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
                                     _("Move gradient mid stop(s)"));
         }
     }
@@ -2058,11 +2051,11 @@ GrDrag::deleteSelected (bool just_one)
         // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
         // manually count the children, don't know if there already exists a function for this...
         int len = 0;
-        for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
-              child != NULL ;
-              child = SP_OBJECT_NEXT(child) )
+        for ( SPObject *child = (stopinfo->vector)->firstChild() ; child ; child = child->getNext() )
         {
-            if ( SP_IS_STOP(child) )  len ++;
+            if ( SP_IS_STOP(child) ) {
+                len ++;
+            }
         }
         if (len > 2)
         {
@@ -2199,7 +2192,7 @@ GrDrag::deleteSelected (bool just_one)
     }
 
     if (document) {
-        sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
+        DocumentUndo::done( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
     }
 }