05c6bc4c956640cd8293e0a079fd3f6157862655
1 /**
2 \file grid.cpp
4 A plug-in to add a grid creation effect into Inkscape.
5 */
6 /*
7 * Authors:
8 * Ted Gould <ted@gould.cx>
9 *
10 * Copyright (C) 2004-2005 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <gtkmm/box.h>
16 #include <gtkmm/adjustment.h>
17 #include <gtkmm/spinbutton.h>
19 #include "desktop.h"
20 #include "desktop-handles.h"
21 #include "selection.h"
22 #include "sp-object.h"
23 #include "util/glib-list-iterators.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 /**
48 \brief This actually draws the grid.
49 \param module The effect that was called (unused)
50 \param document What should be edited.
51 */
52 void
53 Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document)
54 {
55 Inkscape::Selection * selection = ((SPDesktop *)document)->selection;
57 NR::Rect bounding_area = NR::Rect(NR::Point(0,0), NR::Point(100,100));
58 if (selection->isEmpty()) {
59 /* get page size */
60 SPDocument * doc = document->doc();
61 bounding_area = NR::Rect(NR::Point(0,0),
62 NR::Point(sp_document_width(doc),
63 sp_document_height(doc)));
64 } else {
65 bounding_area = selection->bounds();
67 gdouble doc_height = sp_document_height(document->doc());
68 NR::Rect temprec = NR::Rect(NR::Point(bounding_area.min()[NR::X], doc_height - bounding_area.min()[NR::Y]),
69 NR::Point(bounding_area.max()[NR::X], doc_height - bounding_area.max()[NR::Y]));
71 bounding_area = temprec;
72 }
75 float xspacing = module->get_param_float("xspacing");
76 float yspacing = module->get_param_float("yspacing");
77 float line_width = module->get_param_float("lineWidth");
78 float xoffset = module->get_param_float("xoffset");
79 float yoffset = module->get_param_float("yoffset");
81 // std::cout << "Spacing: " << spacing;
82 // std::cout << " Line Width: " << line_width;
83 // std::cout << " Offset: " << offset << std::endl;
85 Glib::ustring path_data;
87 for (NR::Point start_point = bounding_area.min();
88 start_point[NR::X] + xoffset <= (bounding_area.max())[NR::X];
89 start_point[NR::X] += xspacing) {
90 NR::Point end_point = start_point;
91 end_point[NR::Y] = (bounding_area.max())[NR::Y];
92 gchar floatstring[64];
94 path_data += "M ";
95 sprintf(floatstring, "%f", start_point[NR::X] + xoffset);
96 path_data += floatstring;
97 path_data += " ";
98 sprintf(floatstring, "%f", start_point[NR::Y]);
99 path_data += floatstring;
100 path_data += " L ";
101 sprintf(floatstring, "%f", end_point[NR::X] + xoffset);
102 path_data += floatstring;
103 path_data += " ";
104 sprintf(floatstring, "%f", end_point[NR::Y]);
105 path_data += floatstring;
106 path_data += " ";
107 }
109 for (NR::Point start_point = bounding_area.min();
110 start_point[NR::Y] + yoffset <= (bounding_area.max())[NR::Y];
111 start_point[NR::Y] += yspacing) {
112 NR::Point end_point = start_point;
113 end_point[NR::X] = (bounding_area.max())[NR::X];
114 gchar floatstring[64];
116 path_data += "M ";
117 sprintf(floatstring, "%f", start_point[NR::X]);
118 path_data += floatstring;
119 path_data += " ";
120 sprintf(floatstring, "%f", start_point[NR::Y] + yoffset);
121 path_data += floatstring;
122 path_data += " L ";
123 sprintf(floatstring, "%f", end_point[NR::X]);
124 path_data += floatstring;
125 path_data += " ";
126 sprintf(floatstring, "%f", end_point[NR::Y] + yoffset);
127 path_data += floatstring;
128 path_data += " ";
129 }
131 // std::cout << "Path Data: " << path_data << std::endl;
133 Inkscape::XML::Document * xml_doc = sp_document_repr_doc(document->doc());
134 Inkscape::XML::Node * current_layer = ((SPDesktop *)document)->currentLayer()->repr;
135 Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
137 path->setAttribute("d", path_data.c_str());
139 Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
140 style += ";stroke-width:";
141 gchar floatstring[64];
142 sprintf(floatstring, "%f", line_width);
143 style += floatstring;
144 style += "pt";
145 path->setAttribute("style", style.c_str());
147 // Glib::ustring transform("scale(1.25 1.25)");
148 // path->setAttribute("transform", transform.c_str());
150 current_layer->appendChild(path);
152 return;
153 }
155 /** \brief A class to make an adjustment that uses Extension params */
156 class PrefAdjustment : public Gtk::Adjustment {
157 /** Extension that this relates to */
158 Inkscape::Extension::Extension * _ext;
159 /** The string which represents the parameter */
160 char * _pref;
161 public:
162 /** \brief Make the adjustment using an extension and the string
163 describing the parameter. */
164 PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
165 Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
166 this->set_value(_ext->get_param_float(_pref));
167 this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
168 return;
169 };
171 void val_changed (void);
172 }; /* class PrefAdjustment */
174 /** \brief A function to respond to the value_changed signal from the
175 adjustment.
177 This function just grabs the value from the adjustment and writes
178 it to the parameter. Very simple, but yet beautiful.
179 */
180 void
181 PrefAdjustment::val_changed (void)
182 {
183 // std::cout << "Value Changed to: " << this->get_value() << std::endl;
184 _ext->set_param_float(_pref, this->get_value());
185 return;
186 }
188 /** \brief A function to get the prefences for the grid
189 \param moudule Module which holds the params
190 \param view Unused today - may get style information in the future.
192 Uses AutoGUI for creating the GUI.
193 */
194 Gtk::Widget *
195 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view)
196 {
197 SPDocument * current_document = view->doc();
199 using Inkscape::Util::GSListConstIterator;
200 GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
201 Inkscape::XML::Node * first_select = NULL;
202 if (selected != NULL)
203 first_select = SP_OBJECT_REPR(*selected);
205 return module->autogui(current_document, first_select);
206 }
208 #include "clear-n_.h"
210 void
211 Grid::init (void)
212 {
213 Inkscape::Extension::build_from_mem(
214 "<inkscape-extension>\n"
215 "<name>" N_("Grid") "</name>\n"
216 "<id>org.inkscape.effect.grid</id>\n"
217 "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
218 "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\">10.0</param>\n"
219 "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\">10.0</param>\n"
220 "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\">5.0</param>\n"
221 "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\">5.0</param>\n"
222 "<effect>\n"
223 "<object-type>all</object-type>\n"
224 "<effects-menu>\n"
225 "<submenu name=\"" N_("Render") "\" />\n"
226 "</effects-menu>\n"
227 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
228 "</effect>\n"
229 "</inkscape-extension>\n", new Grid());
230 return;
231 }
233 }; /* namespace Internal */
234 }; /* namespace Extension */
235 }; /* namespace Inkscape */
237 /*
238 Local Variables:
239 mode:c++
240 c-file-style:"stroustrup"
241 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
242 indent-tabs-mode:nil
243 fill-column:99
244 End:
245 */
246 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :