Code

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