Code

Fix change in revision 9947 to be consistent with rest of the codebase.
[inkscape.git] / src / knotholder.cpp
index f0495a244d8c8012db34b275556623357770aaab..f5e28618eff8fb641fd5f2acfbc4a1cb85cfcde0 100644 (file)
@@ -1,5 +1,3 @@
-#define __KNOT_HOLDER_C__
-
 /*
  * Container for SPKnot visual handles
  *
@@ -7,12 +5,15 @@
  *   Mitsuru Oka <oka326@parkcity.ne.jp>
  *   bulia byak <buliabyak@users.sf.net>
  *   Maximilian Albert <maximilian.albert@gmail.com>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2001-2008 authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
+#include <glibmm/i18n.h>
+
 #include "document.h"
 #include "sp-shape.h"
 #include "knot.h"
 #include "sp-pattern.h"
 #include "style.h"
 #include "live_effects/lpeobject.h"
+#include "live_effects/effect.h"
+#include "desktop.h"
+#include "display/sp-canvas.h"
 
 #include "xml/repr.h" // for debugging only
 
-#include <libnr/nr-matrix-div.h>
-#include <glibmm/i18n.h>
+using Inkscape::DocumentUndo;
 
 class SPDesktop;
 
 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
 {
-    Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
+    //XML Tree being used directly here while it shouldn't be...
+    Inkscape::XML::Node *repr = SP_OBJECT(item)->getRepr();
 
     if (!desktop || !item || !SP_IS_ITEM(item)) {
         g_print ("Error! Throw an exception, please!\n");
@@ -54,23 +58,24 @@ KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFun
 
     this->repr = repr;
     this->local_change = FALSE;
+
+    this->dragging = false;
 }
 
 KnotHolder::~KnotHolder() {
     g_object_unref(G_OBJECT(item));
     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
         KnotHolderEntity* e = (*i);
-        if (!e->isLPEParam()) {
-            // knotholder entity may be deleted
+        if (e->isDeletable()) {
             delete (*i);
         } else {
-            // we must not delete the entity since it's an LPE parameter,
+            // we must not delete the entity (since it's attached to an LPE parameter),
             // but the handle should be destroyed
             g_object_unref(e->knot);
         }
         (*i) = NULL;
     }
-    entity.clear(); // this shouldn't be necessary, though
+    entity.clear(); // is this necessary?
 }
 
 /**
@@ -80,7 +85,7 @@ KnotHolder::~KnotHolder() {
 void
 KnotHolder::update_knots()
 {
-    NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
+    Geom::Matrix const i2d(item->i2d_affine());
 
     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
         KnotHolderEntity *e = *i;
@@ -88,10 +93,25 @@ KnotHolder::update_knots()
     }
 }
 
+/**
+ * \brief Returns true if at least one of the KnotHolderEntities has the mouse hovering above it
+ */
+bool KnotHolder::knot_mouseover()
+{
+    for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
+        SPKnot *knot = (*i)->knot;
+        if (knot && (knot->flags & SP_KNOT_MOUSEOVER)) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 void
 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
 {
-    KnotHolder *knot_holder = this;
+       KnotHolder *knot_holder = this;
 
     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
         KnotHolderEntity *e = *i;
@@ -103,7 +123,7 @@ KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
     }
 
     if (SP_IS_SHAPE(item)) {
-        sp_shape_set_shape(SP_SHAPE(item));
+        SP_SHAPE(item)->setShape();
     }
 
     knot_holder->update_knots();
@@ -128,61 +148,65 @@ KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
     }
 
     // for drag, this is done by ungrabbed_handler, but for click we must do it here
-    sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb, 
-                     _("Change handle"));
+    DocumentUndo::done(SP_OBJECT_DOCUMENT(item), object_verb,
+                       _("Change handle"));
 }
 
 void
-KnotHolder::knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state)
+KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
 {
-    // this was a local change and the knotholder does not need to be recreated:
+    if (this->dragging == false) {
+       this->dragging = true;
+    }
+
+       // this was a local change and the knotholder does not need to be recreated:
     this->local_change = TRUE;
 
     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
         KnotHolderEntity *e = *i;
         if (e->knot == knot) {
-            NR::Point const q = *p / from_2geom(sp_item_i2d_affine(item));
-            e->knot_set(q, e->knot->drag_origin / from_2geom(sp_item_i2d_affine(item)), state);
+            Geom::Point const q = p * item->i2d_affine().inverse();
+            e->knot_set(q, e->knot->drag_origin * item->i2d_affine().inverse(), state);
             break;
         }
     }
 
     if (SP_IS_SHAPE (item)) {
-        sp_shape_set_shape(SP_SHAPE (item));
+        SP_SHAPE (item)->setShape();
     }
 
     this->update_knots();
 }
 
 void
-KnotHolder::knot_ungrabbed_handler(SPKnot *knot)
+KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
 {
-    if (this->released) {
+       this->dragging = false;
+
+       if (this->released) {
         this->released(this->item);
     } else {
         SPObject *object = (SPObject *) this->item;
+
+        // Caution: this call involves a screen update, which may process events, and as a
+        // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
+        // fields of this knotholder (such as this->item), but only values we have saved beforehand
+        // (such as object).
         object->updateRepr();
 
         /* do cleanup tasks (e.g., for LPE items write the parameter values
          * that were changed by dragging the handle to SVG)
          */
-        if (SP_IS_LPE_ITEM(item)) {
-            // This writes all parameters to SVG. Is this sufficiently efficient or should we only write
-            // the ones that were changed (e.g., via the individual handles' onKnotUngrabbed() method?            
-            Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
+        if (SP_IS_LPE_ITEM(object)) {
+            // This writes all parameters to SVG. Is this sufficiently efficient or should we only
+            // write the ones that were changed?
+
+            Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
             if (lpe) {
                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
                 SP_OBJECT(lpeobj)->updateRepr();
             }
         }
-        // this was once used to write individual parameter values to SVG but this is now done globally above;
-        // we leave the calls to onKnotUngrabbed, anyway, in case any other cleanup tasks need to be done
-        for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
-            KnotHolderEntity *e = *i;
-            if (e->knot == knot) {
-                e->onKnotUngrabbed(); // for most KnotHolderEntitys this does nothing
-            }
-        }
 
         unsigned int object_verb = SP_VERB_NONE;
 
@@ -202,9 +226,9 @@ KnotHolder::knot_ungrabbed_handler(SPKnot *knot)
             else
                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
         }
-        
-        sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
-                         _("Move handle"));
+
+        DocumentUndo::done(SP_OBJECT_DOCUMENT (object), object_verb,
+                           _("Move handle"));
     }
 }
 
@@ -228,7 +252,7 @@ KnotHolder::add_pattern_knotholder()
                           _("<b>Move</b> the pattern fill inside the object"),
                           SP_KNOT_SHAPE_CROSS);
         entity_scale->create(desktop, item, this,
-                             _("<b>Scale</b> the pattern fill uniformly"),
+                             _("<b>Scale</b> the pattern fill; uniformly if with <b>Ctrl</b>"),
                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
         entity_angle->create(desktop, item, this,
                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
@@ -248,4 +272,4 @@ KnotHolder::add_pattern_knotholder()
   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 :