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());
116 //XML Tree being used directly here while it shouldn't be.
117 Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->getRepr();
118 Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
120 path->setAttribute("d", path_data.c_str());
122 Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
123 style += ";stroke-width:";
124 gchar floatstring[64];
125 std::ostringstream stringstream;
126 stringstream << line_width;
127 sprintf(floatstring, "%s", stringstream.str().c_str());
128 style += floatstring;
129 style += "pt";
130 path->setAttribute("style", style.c_str());
132 current_layer->appendChild(path);
133 Inkscape::GC::release(path);
135 return;
136 }
138 /** \brief A class to make an adjustment that uses Extension params */
139 class PrefAdjustment : public Gtk::Adjustment {
140 /** Extension that this relates to */
141 Inkscape::Extension::Extension * _ext;
142 /** The string which represents the parameter */
143 char * _pref;
144 public:
145 /** \brief Make the adjustment using an extension and the string
146 describing the parameter. */
147 PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
148 Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
149 this->set_value(_ext->get_param_float(_pref));
150 this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
151 return;
152 };
154 void val_changed (void);
155 }; /* class PrefAdjustment */
157 /** \brief A function to respond to the value_changed signal from the
158 adjustment.
160 This function just grabs the value from the adjustment and writes
161 it to the parameter. Very simple, but yet beautiful.
162 */
163 void
164 PrefAdjustment::val_changed (void)
165 {
166 // std::cout << "Value Changed to: " << this->get_value() << std::endl;
167 _ext->set_param_float(_pref, this->get_value());
168 return;
169 }
171 /** \brief A function to get the prefences for the grid
172 \param moudule Module which holds the params
173 \param view Unused today - may get style information in the future.
175 Uses AutoGUI for creating the GUI.
176 */
177 Gtk::Widget *
178 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
179 {
180 SPDocument * current_document = view->doc();
182 using Inkscape::Util::GSListConstIterator;
183 GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
184 Inkscape::XML::Node * first_select = NULL;
185 if (selected != NULL)
186 first_select = SP_OBJECT_REPR(*selected);
188 return module->autogui(current_document, first_select, changeSignal);
189 }
191 #include "clear-n_.h"
193 void
194 Grid::init (void)
195 {
196 Inkscape::Extension::build_from_mem(
197 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
198 "<name>" N_("Grid") "</name>\n"
199 "<id>org.inkscape.effect.grid</id>\n"
200 "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
201 "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
202 "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
203 "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
204 "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
205 "<effect>\n"
206 "<object-type>all</object-type>\n"
207 "<effects-menu>\n"
208 "<submenu name=\"" N_("Render") "\" />\n"
209 "</effects-menu>\n"
210 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
211 "</effect>\n"
212 "</inkscape-extension>\n", new Grid());
213 return;
214 }
216 }; /* namespace Internal */
217 }; /* namespace Extension */
218 }; /* namespace Inkscape */
220 /*
221 Local Variables:
222 mode:c++
223 c-file-style:"stroustrup"
224 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
225 indent-tabs-mode:nil
226 fill-column:99
227 End:
228 */
229 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :