1 /**
2 \file grid.cpp
4 A plug-in to add a grid creation effect into Inkscape.
5 */
6 /*
7 * Copyright (C) 2004-2005 Ted Gould <ted@gould.cx>
8 * Copyright (C) 2007 MenTaLguY <mental@rydia.net>
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include <gtkmm/box.h>
14 #include <gtkmm/adjustment.h>
15 #include <gtkmm/spinbutton.h>
17 #include "desktop.h"
18 #include "desktop-handles.h"
19 #include "selection.h"
20 #include "sp-object.h"
21 #include "util/glib-list-iterators.h"
23 #include "svg/path-string.h"
25 #include "extension/effect.h"
26 #include "extension/system.h"
29 #include "grid.h"
31 namespace Inkscape {
32 namespace Extension {
33 namespace Internal {
35 /**
36 \brief A function to allocated anything -- just an example here
37 \param module Unused
38 \return Whether the load was sucessful
39 */
40 bool
41 Grid::load (Inkscape::Extension::Extension */*module*/)
42 {
43 // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
44 return TRUE;
45 }
47 namespace {
49 Glib::ustring build_lines(Geom::Rect bounding_area,
50 float offset[], float spacing[])
51 {
52 Geom::Point point_offset(0.0, 0.0);
54 SVG::PathString path_data;
56 for ( int axis = 0 ; axis < 2 ; ++axis ) {
57 point_offset[axis] = offset[axis];
59 for (Geom::Point start_point = bounding_area.min();
60 start_point[axis] + offset[axis] <= (bounding_area.max())[axis];
61 start_point[axis] += spacing[axis]) {
62 Geom::Point end_point = start_point;
63 end_point[1-axis] = (bounding_area.max())[1-axis];
65 path_data.moveTo(start_point + point_offset)
66 .lineTo(end_point + point_offset);
67 }
68 }
69 // std::cout << "Path data:" << path_data.c_str() << std::endl;
70 return path_data;
71 }
73 }
75 /**
76 \brief This actually draws the grid.
77 \param module The effect that was called (unused)
78 \param document What should be edited.
79 */
80 void
81 Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
82 {
83 Inkscape::Selection * selection = ((SPDesktop *)document)->selection;
85 Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
86 if (selection->isEmpty()) {
87 /* get page size */
88 SPDocument * doc = document->doc();
89 bounding_area = Geom::Rect( Geom::Point(0,0),
90 Geom::Point(doc->getWidth(), doc->getHeight()) );
91 } else {
92 Geom::OptRect bounds = selection->bounds();
93 if (bounds) {
94 bounding_area = *bounds;
95 }
97 gdouble doc_height = (document->doc())->getHeight();
98 Geom::Rect temprec = Geom::Rect(Geom::Point(bounding_area.min()[Geom::X], doc_height - bounding_area.min()[Geom::Y]),
99 Geom::Point(bounding_area.max()[Geom::X], doc_height - bounding_area.max()[Geom::Y]));
101 bounding_area = temprec;
102 }
104 float spacings[2] = { module->get_param_float("xspacing"),
105 module->get_param_float("yspacing") };
106 float line_width = module->get_param_float("lineWidth");
107 float offsets[2] = { module->get_param_float("xoffset"),
108 module->get_param_float("yoffset") };
110 Glib::ustring path_data("");
112 path_data = build_lines(bounding_area,
113 offsets, spacings);
114 Inkscape::XML::Document * xml_doc = sp_document_repr_doc(document->doc());
115 Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->repr;
116 Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
118 path->setAttribute("d", path_data.c_str());
120 Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
121 style += ";stroke-width:";
122 gchar floatstring[64];
123 std::ostringstream stringstream;
124 stringstream << line_width;
125 sprintf(floatstring, "%s", stringstream.str().c_str());
126 style += floatstring;
127 style += "pt";
128 path->setAttribute("style", style.c_str());
130 current_layer->appendChild(path);
131 Inkscape::GC::release(path);
133 return;
134 }
136 /** \brief A class to make an adjustment that uses Extension params */
137 class PrefAdjustment : public Gtk::Adjustment {
138 /** Extension that this relates to */
139 Inkscape::Extension::Extension * _ext;
140 /** The string which represents the parameter */
141 char * _pref;
142 public:
143 /** \brief Make the adjustment using an extension and the string
144 describing the parameter. */
145 PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
146 Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
147 this->set_value(_ext->get_param_float(_pref));
148 this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
149 return;
150 };
152 void val_changed (void);
153 }; /* class PrefAdjustment */
155 /** \brief A function to respond to the value_changed signal from the
156 adjustment.
158 This function just grabs the value from the adjustment and writes
159 it to the parameter. Very simple, but yet beautiful.
160 */
161 void
162 PrefAdjustment::val_changed (void)
163 {
164 // std::cout << "Value Changed to: " << this->get_value() << std::endl;
165 _ext->set_param_float(_pref, this->get_value());
166 return;
167 }
169 /** \brief A function to get the prefences for the grid
170 \param moudule Module which holds the params
171 \param view Unused today - may get style information in the future.
173 Uses AutoGUI for creating the GUI.
174 */
175 Gtk::Widget *
176 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
177 {
178 SPDocument * current_document = view->doc();
180 using Inkscape::Util::GSListConstIterator;
181 GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
182 Inkscape::XML::Node * first_select = NULL;
183 if (selected != NULL)
184 first_select = SP_OBJECT_REPR(*selected);
186 return module->autogui(current_document, first_select, changeSignal);
187 }
189 #include "clear-n_.h"
191 void
192 Grid::init (void)
193 {
194 Inkscape::Extension::build_from_mem(
195 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
196 "<name>" N_("Grid") "</name>\n"
197 "<id>org.inkscape.effect.grid</id>\n"
198 "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
199 "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
200 "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
201 "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
202 "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
203 "<effect>\n"
204 "<object-type>all</object-type>\n"
205 "<effects-menu>\n"
206 "<submenu name=\"" N_("Render") "\" />\n"
207 "</effects-menu>\n"
208 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
209 "</effect>\n"
210 "</inkscape-extension>\n", new Grid());
211 return;
212 }
214 }; /* namespace Internal */
215 }; /* namespace Extension */
216 }; /* namespace Inkscape */
218 /*
219 Local Variables:
220 mode:c++
221 c-file-style:"stroustrup"
222 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
223 indent-tabs-mode:nil
224 fill-column:99
225 End:
226 */
227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :