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(int axis, Geom::Rect bounding_area,
50 float offset, float spacing)
51 {
52 Geom::Point point_offset(0.0, 0.0);
53 point_offset[axis] = offset;
55 SVG::PathString path_data;
56 for (Geom::Point start_point = bounding_area.min();
57 start_point[axis] + offset <= (bounding_area.max())[axis];
58 start_point[axis] += spacing) {
59 Geom::Point end_point = start_point;
60 end_point[1-axis] = (bounding_area.max())[1-axis];
62 path_data.moveTo(start_point + point_offset)
63 .lineTo(end_point + point_offset);
64 }
66 return path_data;
67 }
69 }
71 /**
72 \brief This actually draws the grid.
73 \param module The effect that was called (unused)
74 \param document What should be edited.
75 */
76 void
77 Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
78 {
79 Inkscape::Selection * selection = ((SPDesktop *)document)->selection;
81 Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
82 if (selection->isEmpty()) {
83 /* get page size */
84 SPDocument * doc = document->doc();
85 bounding_area = Geom::Rect( Geom::Point(0,0),
86 Geom::Point(sp_document_width(doc), sp_document_height(doc)) );
87 } else {
88 Geom::OptRect bounds = selection->bounds();
89 if (bounds) {
90 bounding_area = *bounds;
91 }
93 gdouble doc_height = sp_document_height(document->doc());
94 Geom::Rect temprec = Geom::Rect(Geom::Point(bounding_area.min()[Geom::X], doc_height - bounding_area.min()[Geom::Y]),
95 Geom::Point(bounding_area.max()[Geom::X], doc_height - bounding_area.max()[Geom::Y]));
97 bounding_area = temprec;
98 }
100 float spacings[2] = { module->get_param_float("xspacing"),
101 module->get_param_float("yspacing") };
102 float line_width = module->get_param_float("lineWidth");
103 float offsets[2] = { module->get_param_float("xoffset"),
104 module->get_param_float("yoffset") };
106 Glib::ustring path_data("");
107 for ( int axis = 0 ; axis < 2 ; ++axis ) {
108 path_data += build_lines(axis, bounding_area,
109 offsets[axis], spacings[axis]);
110 }
112 Inkscape::XML::Document * xml_doc = sp_document_repr_doc(document->doc());
113 Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->repr;
114 Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
116 path->setAttribute("d", path_data.c_str());
118 Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
119 style += ";stroke-width:";
120 gchar floatstring[64];
121 sprintf(floatstring, "%f", line_width);
122 style += floatstring;
123 style += "pt";
124 path->setAttribute("style", style.c_str());
126 current_layer->appendChild(path);
127 Inkscape::GC::release(path);
129 return;
130 }
132 /** \brief A class to make an adjustment that uses Extension params */
133 class PrefAdjustment : public Gtk::Adjustment {
134 /** Extension that this relates to */
135 Inkscape::Extension::Extension * _ext;
136 /** The string which represents the parameter */
137 char * _pref;
138 public:
139 /** \brief Make the adjustment using an extension and the string
140 describing the parameter. */
141 PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
142 Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
143 this->set_value(_ext->get_param_float(_pref));
144 this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
145 return;
146 };
148 void val_changed (void);
149 }; /* class PrefAdjustment */
151 /** \brief A function to respond to the value_changed signal from the
152 adjustment.
154 This function just grabs the value from the adjustment and writes
155 it to the parameter. Very simple, but yet beautiful.
156 */
157 void
158 PrefAdjustment::val_changed (void)
159 {
160 // std::cout << "Value Changed to: " << this->get_value() << std::endl;
161 _ext->set_param_float(_pref, this->get_value());
162 return;
163 }
165 /** \brief A function to get the prefences for the grid
166 \param moudule Module which holds the params
167 \param view Unused today - may get style information in the future.
169 Uses AutoGUI for creating the GUI.
170 */
171 Gtk::Widget *
172 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
173 {
174 SPDocument * current_document = view->doc();
176 using Inkscape::Util::GSListConstIterator;
177 GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
178 Inkscape::XML::Node * first_select = NULL;
179 if (selected != NULL)
180 first_select = SP_OBJECT_REPR(*selected);
182 return module->autogui(current_document, first_select, changeSignal);
183 }
185 #include "clear-n_.h"
187 void
188 Grid::init (void)
189 {
190 Inkscape::Extension::build_from_mem(
191 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
192 "<name>" N_("Grid") "</name>\n"
193 "<id>org.inkscape.effect.grid</id>\n"
194 "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
195 "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
196 "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
197 "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
198 "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
199 "<effect>\n"
200 "<object-type>all</object-type>\n"
201 "<effects-menu>\n"
202 "<submenu name=\"" N_("Render") "\" />\n"
203 "</effects-menu>\n"
204 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
205 "</effect>\n"
206 "</inkscape-extension>\n", new Grid());
207 return;
208 }
210 }; /* namespace Internal */
211 }; /* namespace Extension */
212 }; /* namespace Inkscape */
214 /*
215 Local Variables:
216 mode:c++
217 c-file-style:"stroustrup"
218 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
219 indent-tabs-mode:nil
220 fill-column:99
221 End:
222 */
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :