Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-guide.cpp
index 9af5f9f021fd4a294d77b505daa970082080bde3..f92d791162e5267677eb10804185d5c4280771a0 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_GUIDE_C__
-
 /*
  * Inkscape guideline implementation
  *
@@ -7,6 +5,8 @@
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
  *   Johan Engelen
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2000-2002 authors
  * Copyright (C) 2004 Monash University
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+
+#include <algorithm>
+#include <cstring>
+#include <string>
+#include "desktop-handles.h"
 #include "display/guideline.h"
 #include "svg/svg.h"
 #include "svg/stringstream.h"
@@ -36,6 +41,7 @@
 #include <2geom/angle.h>
 #include "document.h"
 
+using Inkscape::DocumentUndo;
 using std::vector;
 
 enum {
@@ -68,7 +74,7 @@ GType sp_guide_get_type(void)
             sizeof(SPGuide),
             16,
             (GInstanceInitFunc) sp_guide_init,
-            NULL,      /* value_table */
+            NULL,       /* value_table */
         };
         guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
     }
@@ -109,7 +115,7 @@ static void sp_guide_class_init(SPGuideClass *gc)
 
 static void sp_guide_init(SPGuide *guide)
 {
-    guide->normal_to_line = component_vectors[NR::Y];
+    guide->normal_to_line = component_vectors[Geom::Y];
     guide->point_on_line = Geom::Point(0.,0.);
     guide->color = 0x0000ff7f;
     guide->hicolor = 0xff00007f;
@@ -153,8 +159,8 @@ static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML
         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
     }
 
-    sp_object_read_attr(object, "orientation");
-    sp_object_read_attr(object, "position");
+    object->readAttr( "orientation" );
+    object->readAttr( "position" );
 }
 
 static void sp_guide_release(SPObject *object)
@@ -162,7 +168,7 @@ static void sp_guide_release(SPObject *object)
     SPGuide *guide = (SPGuide *) object;
 
     while (guide->views) {
-        gtk_object_destroy(GTK_OBJECT(guide->views->data));
+        sp_guideline_delete(SP_GUIDELINE(guide->views->data));
         guide->views = g_slist_remove(guide->views, guide->views->data);
     }
 
@@ -180,28 +186,28 @@ static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
         {
             if (value && !strcmp(value, "horizontal")) {
                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
-                guide->normal_to_line = component_vectors[NR::Y];
+                guide->normal_to_line = component_vectors[Geom::Y];
             } else if (value && !strcmp(value, "vertical")) {
-                guide->normal_to_line = component_vectors[NR::X];
+                guide->normal_to_line = component_vectors[Geom::X];
             } else if (value) {
                 gchar ** strarray = g_strsplit(value, ",", 2);
                 double newx, newy;
                 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
                 success += sp_svg_number_read_d(strarray[1], &newy);
                 g_strfreev (strarray);
-                if (success == 2) {
+                if (success == 2 && (fabs(newx) > 1e-6 || fabs(newy) > 1e-6)) {
                     Geom::Point direction(newx, newy);
                     direction.normalize();
                     guide->normal_to_line = direction;
                 } else {
                     // default to vertical line for bad arguments
-                    guide->normal_to_line = component_vectors[NR::X];
+                    guide->normal_to_line = component_vectors[Geom::X];
                 }
             } else {
                 // default to vertical line for bad arguments
-                guide->normal_to_line = component_vectors[NR::X];
+                guide->normal_to_line = component_vectors[Geom::X];
             }
-            sp_guide_set_normal(*guide, guide->normal_to_line.to_2geom(), false);
+            sp_guide_set_normal(*guide, guide->normal_to_line, false);
         }
         break;
     case SP_ATTR_POSITION:
@@ -236,10 +242,10 @@ static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
     }
 }
 
-SPGuide *
-sp_guide_create(SPDocument *doc, Geom::Point const &pt1, Geom::Point const &pt2) {
-    SPDesktop *desktop = inkscape_active_desktop();
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+SPGuide *SPGuide::createSPGuide(SPDesktop *desktop, Geom::Point const &pt1, Geom::Point const &pt2)
+{
+    SPDocument *doc = sp_desktop_document(desktop);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
 
@@ -248,7 +254,7 @@ sp_guide_create(SPDocument *doc, Geom::Point const &pt1, Geom::Point const &pt2)
     sp_repr_set_point(repr, "position", pt1);
     sp_repr_set_point(repr, "orientation", n);
 
-    SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
+    desktop->namedview->appendChild(repr);
     Inkscape::GC::release(repr);
 
     SPGuide *guide= SP_GUIDE(doc->getObjectByRepr(repr));
@@ -256,33 +262,51 @@ sp_guide_create(SPDocument *doc, Geom::Point const &pt1, Geom::Point const &pt2)
 }
 
 void
-sp_guide_pt_pairs_to_guides(SPDocument *doc, std::list<std::pair<Geom::Point, Geom::Point> > &pts) {
+sp_guide_pt_pairs_to_guides(SPDesktop *dt, std::list<std::pair<Geom::Point, Geom::Point> > &pts) {
     for (std::list<std::pair<Geom::Point, Geom::Point> >::iterator i = pts.begin(); i != pts.end(); ++i) {
-        sp_guide_create(doc, (*i).first, (*i).second);
+        SPGuide::createSPGuide(dt, (*i).first, (*i).second);
     }
 }
 
-void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
+void
+sp_guide_create_guides_around_page(SPDesktop *dt) {
+    SPDocument *doc=sp_desktop_document(dt);
+    std::list<std::pair<Geom::Point, Geom::Point> > pts;
+
+    Geom::Point A(0, 0);
+    Geom::Point C(doc->getWidth(), doc->getHeight());
+    Geom::Point B(C[Geom::X], 0);
+    Geom::Point D(0, C[Geom::Y]);
+
+    pts.push_back(std::make_pair<Geom::Point, Geom::Point>(A, B));
+    pts.push_back(std::make_pair<Geom::Point, Geom::Point>(B, C));
+    pts.push_back(std::make_pair<Geom::Point, Geom::Point>(C, D));
+    pts.push_back(std::make_pair<Geom::Point, Geom::Point>(D, A));
+
+    sp_guide_pt_pairs_to_guides(dt, pts);
+
+    DocumentUndo::done(doc, SP_VERB_NONE, _("Guides Around Page"));
+}
+
+void SPGuide::showSPGuide(SPCanvasGroup *group, GCallback handler)
 {
-    SPCanvasItem *item = sp_guideline_new(group, guide->point_on_line, guide->normal_to_line.to_2geom());
-    sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
+    SPCanvasItem *item = sp_guideline_new(group, point_on_line, normal_to_line);
+    sp_guideline_set_color(SP_GUIDELINE(item), color);
 
-    g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
+    g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), this);
 
-    guide->views = g_slist_prepend(guide->views, item);
+    views = g_slist_prepend(views, item);
 }
 
-void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
+void SPGuide::hideSPGuide(SPCanvas *canvas)
 {
-    g_assert(guide != NULL);
-    g_assert(SP_IS_GUIDE(guide));
     g_assert(canvas != NULL);
     g_assert(SP_IS_CANVAS(canvas));
 
-    for (GSList *l = guide->views; l != NULL; l = l->next) {
+    for (GSList *l = views; l != NULL; l = l->next) {
         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
-            gtk_object_destroy(GTK_OBJECT(l->data));
-            guide->views = g_slist_remove(guide->views, l->data);
+            sp_guideline_delete(SP_GUIDELINE(l->data));
+            views = g_slist_remove(views, l->data);
             return;
         }
     }
@@ -290,14 +314,12 @@ void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
     g_assert_not_reached();
 }
 
-void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
+void SPGuide::sensitize(SPCanvas *canvas, gboolean sensitive)
 {
-    g_assert(guide != NULL);
-    g_assert(SP_IS_GUIDE(guide));
     g_assert(canvas != NULL);
     g_assert(SP_IS_CANVAS(canvas));
 
-    for (GSList *l = guide->views; l != NULL; l = l->next) {
+    for (GSList *l = views; l != NULL; l = l->next) {
         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
             return;
@@ -307,14 +329,14 @@ void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
     g_assert_not_reached();
 }
 
-Geom::Point sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
+Geom::Point SPGuide::getPositionFrom(Geom::Point const &pt) const
 {
-    return -(pt.to_2geom() - guide->point_on_line);
+    return -(pt - point_on_line);
 }
 
-double sp_guide_distance_from_pt(SPGuide const *guide, Geom::Point const &pt)
+double SPGuide::getDistanceFrom(Geom::Point const &pt) const
 {
-    return dot(pt - guide->point_on_line, guide->normal_to_line);
+    return Geom::dot(pt - point_on_line, normal_to_line);
 }
 
 /**
@@ -333,7 +355,8 @@ void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool
     /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
     if (commit) {
-        sp_repr_set_point(SP_OBJECT(&guide)->repr, "position", point_on_line);
+        //XML Tree being used here directly while it shouldn't be.
+        sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "position", point_on_line);
     }
 
 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
@@ -363,7 +386,8 @@ void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line,
     /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
     if (commit) {
-        sp_repr_set_point(SP_OBJECT(&guide)->repr, "orientation", normal_to_line);
+        //XML Tree being used directly while it shouldn't be
+        sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "orientation", normal_to_line);
     }
 
 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
@@ -379,30 +403,45 @@ void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line,
 
 /**
  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
+ * If verbose is false, only positioning information is included (useful for dialogs).
  *
  * The caller is responsible for freeing the string.
  */
-char *sp_guide_description(SPGuide const *guide)
+char *sp_guide_description(SPGuide const *guide, const bool verbose)
 {
-    using NR::X;
-    using NR::Y;
+    using Geom::X;
+    using Geom::Y;
             
-    GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
-    GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
-
-    if ( guide->normal_to_line == component_vectors[X] ) {
-        return g_strdup_printf(_("vertical, at %s"), position_string_x->str);
-    } else if ( guide->normal_to_line == component_vectors[Y] ) {
-        return g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
+    GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X],
+                                                        SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
+    GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y],
+                                                        SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
+
+    gchar *shortcuts = g_strdup_printf("; %s", _("<b>Shift+drag</b> to rotate, <b>Ctrl+drag</b> to move origin, <b>Del</b> to delete"));
+    gchar *descr;
+
+    if ( are_near(guide->normal_to_line, component_vectors[X]) ||
+         are_near(guide->normal_to_line, -component_vectors[X]) ) {
+        descr = g_strdup_printf(_("vertical, at %s"), position_string_x->str);
+    } else if ( are_near(guide->normal_to_line, component_vectors[Y]) ||
+                are_near(guide->normal_to_line, -component_vectors[Y]) ) {
+        descr = g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
     } else {
         double const radians = guide->angle();
         double const degrees = Geom::rad_to_deg(radians);
         int const degrees_int = (int) round(degrees);
-        return g_strdup_printf(_("at %d degrees, through (%s,%s)"), degrees_int, position_string_x->str, position_string_y->str);
+        descr = g_strdup_printf(_("at %d degrees, through (%s,%s)"), 
+                                degrees_int, position_string_x->str, position_string_y->str);
     }
 
     g_string_free(position_string_x, TRUE);
     g_string_free(position_string_y, TRUE);
+
+    if (verbose) {
+        descr = g_strconcat(descr, shortcuts, NULL);
+    }
+    g_free(shortcuts);
+    return descr;
 }
 
 void sp_guide_remove(SPGuide *guide)
@@ -418,7 +457,8 @@ void sp_guide_remove(SPGuide *guide)
     }
     guide->attached_items.clear();
 
-    sp_repr_unparent(SP_OBJECT(guide)->repr);
+    //XML Tree being used directly while it shouldn't be.
+    sp_repr_unparent(SP_OBJECT(guide)->getRepr());
 }
 
 /*