Code

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