Code

fix include paths
[inkscape.git] / src / extension / internal / grid.cpp
index b9d626f783b379779603b46883f7bea189ee7191..57b1240352baa18bb209c34ef8d4e01651baf41e 100644 (file)
@@ -1,13 +1,11 @@
 /**
     \file grid.cpp
+
     A plug-in to add a grid creation effect into Inkscape.
 */
 /*
- * Authors:
- *   Ted Gould <ted@gould.cx>
- *
- * Copyright (C) 2004-2005 Authors
+ * Copyright (C) 2004-2005  Ted Gould <ted@gould.cx>
+ * Copyright (C) 2007  MenTaLguY <mental@rydia.net>
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
@@ -22,6 +20,8 @@
 #include "sp-object.h"
 #include "util/glib-list-iterators.h"
 
+#include "svg/path-string.h"
+
 #include "extension/effect.h"
 #include "extension/system.h"
 
@@ -38,19 +38,43 @@ namespace Internal {
     \return Whether the load was sucessful
 */
 bool
-Grid::load (Inkscape::Extension::Extension *module)
+Grid::load (Inkscape::Extension::Extension */*module*/)
 {
     // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
     return TRUE;
 }
 
+namespace {
+
+Glib::ustring build_lines(int axis, NR::Rect bounding_area,
+                          float offset, float spacing)
+{
+    NR::Point point_offset(0.0, 0.0);
+    point_offset[axis] = offset;
+
+    SVG::PathString path_data;
+    for (NR::Point start_point = bounding_area.min();
+            start_point[axis] + offset <= (bounding_area.max())[axis];
+            start_point[axis] += spacing) {
+        NR::Point end_point = start_point;
+        end_point[1-axis] = (bounding_area.max())[1-axis];
+
+        path_data.moveTo(start_point + point_offset)
+                 .lineTo(end_point + point_offset);
+    }
+
+    return path_data;
+}
+
+}
+
 /**
     \brief  This actually draws the grid.
     \param  module   The effect that was called (unused)
     \param  document What should be edited.
 */
 void
-Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document)
+Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
 {
     Inkscape::Selection * selection     = ((SPDesktop *)document)->selection;
 
@@ -62,7 +86,10 @@ Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *doc
                                  NR::Point(sp_document_width(doc),
                                            sp_document_height(doc)));
     } else {
-        bounding_area = selection->bounds();
+        NR::Maybe<NR::Rect> bounds = selection->bounds();
+        if (bounds) {
+            bounding_area = *bounds;
+        }
 
         gdouble doc_height  =  sp_document_height(document->doc());
         NR::Rect temprec = NR::Rect(NR::Point(bounding_area.min()[NR::X], doc_height - bounding_area.min()[NR::Y]),
@@ -71,67 +98,21 @@ Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *doc
         bounding_area = temprec;
     }
 
-
-    float xspacing = module->get_param_float("xspacing");
-    float yspacing = module->get_param_float("yspacing");
+    float spacings[2] = { module->get_param_float("xspacing"),
+                          module->get_param_float("yspacing") };
     float line_width = module->get_param_float("lineWidth");
-    float xoffset = module->get_param_float("xoffset");
-    float yoffset = module->get_param_float("yoffset");
-
-    // std::cout << "Spacing: " << spacing;
-    // std::cout << " Line Width: " << line_width;
-    // std::cout << " Offset: " << offset << std::endl;
+    float offsets[2] = { module->get_param_float("xoffset"),
+                         module->get_param_float("yoffset") };
 
-    Glib::ustring path_data;
-
-    for (NR::Point start_point = bounding_area.min();
-            start_point[NR::X] + xoffset <= (bounding_area.max())[NR::X];
-            start_point[NR::X] += xspacing) {
-        NR::Point end_point = start_point;
-        end_point[NR::Y] = (bounding_area.max())[NR::Y];
-        gchar floatstring[64];
-
-        path_data += "M ";
-        sprintf(floatstring, "%f", start_point[NR::X] + xoffset);
-        path_data += floatstring;
-        path_data += " ";
-        sprintf(floatstring, "%f", start_point[NR::Y]);
-        path_data += floatstring;
-        path_data += " L ";
-        sprintf(floatstring, "%f", end_point[NR::X] + xoffset);
-        path_data += floatstring;
-        path_data += " ";
-        sprintf(floatstring, "%f", end_point[NR::Y]);
-        path_data += floatstring;
-        path_data += " ";
+    Glib::ustring path_data("");
+    for ( int axis = 0 ; axis < 2 ; ++axis ) {
+        path_data += build_lines(axis, bounding_area,
+                                 offsets[axis], spacings[axis]);
     }
 
-    for (NR::Point start_point = bounding_area.min();
-            start_point[NR::Y] + yoffset <= (bounding_area.max())[NR::Y];
-            start_point[NR::Y] += yspacing) {
-        NR::Point end_point = start_point;
-        end_point[NR::X] = (bounding_area.max())[NR::X];
-        gchar floatstring[64];
-
-        path_data += "M ";
-        sprintf(floatstring, "%f", start_point[NR::X]);
-        path_data += floatstring;
-        path_data += " ";
-        sprintf(floatstring, "%f", start_point[NR::Y] + yoffset);
-        path_data += floatstring;
-        path_data += " L ";
-        sprintf(floatstring, "%f", end_point[NR::X]);
-        path_data += floatstring;
-        path_data += " ";
-        sprintf(floatstring, "%f", end_point[NR::Y] + yoffset);
-        path_data += floatstring;
-        path_data += " ";
-    }
-
-    // std::cout << "Path Data: " << path_data << std::endl;
-
-    Inkscape::XML::Node * current_layer = ((SPDesktop *)document)->currentLayer()->repr;
-    Inkscape::XML::Node * path = sp_repr_new("svg:path");
+    Inkscape::XML::Document * xml_doc = sp_document_repr_doc(document->doc());
+    Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->repr;
+    Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
 
     path->setAttribute("d", path_data.c_str());
 
@@ -143,9 +124,6 @@ Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *doc
     style += "pt";
     path->setAttribute("style", style.c_str());
 
-    // Glib::ustring transform("scale(1.25 1.25)");
-    // path->setAttribute("transform", transform.c_str());
-
     current_layer->appendChild(path);
 
     return;
@@ -162,7 +140,7 @@ public:
                 describing the parameter. */
     PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
             Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
-        this->set_value(_ext->get_param_float(_pref)); 
+        this->set_value(_ext->get_param_float(_pref));
         this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
         return;
     };
@@ -191,17 +169,17 @@ PrefAdjustment::val_changed (void)
     Uses AutoGUI for creating the GUI.
 */
 Gtk::Widget *
-Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view)
+Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
 {
     SPDocument * current_document = view->doc();
 
     using Inkscape::Util::GSListConstIterator;
     GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
     Inkscape::XML::Node * first_select = NULL;
-    if (selected != NULL) 
+    if (selected != NULL)
         first_select = SP_OBJECT_REPR(*selected);
 
-    return module->autogui(current_document, first_select);
+    return module->autogui(current_document, first_select, changeSignal);
 }
 
 #include "clear-n_.h"
@@ -214,10 +192,10 @@ Grid::init (void)
             "<name>" N_("Grid") "</name>\n"
             "<id>org.inkscape.effect.grid</id>\n"
             "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
-            "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\">10.0</param>\n"
-            "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\">10.0</param>\n"
-            "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\">5.0</param>\n"
-            "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\">5.0</param>\n"
+            "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
+            "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
+            "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
+            "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
             "<effect>\n"
                 "<object-type>all</object-type>\n"
                 "<effects-menu>\n"