Code

Fix image transform when there is no rotate or scale
[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  * 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::Node * current_layer = ((SPDesktop *)document)->currentLayer()->repr;
134     Inkscape::XML::Node * path = sp_repr_new("svg:path");
136     path->setAttribute("d", path_data.c_str());
138     Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
139     style += ";stroke-width:";
140     gchar floatstring[64];
141     sprintf(floatstring, "%f", line_width);
142     style += floatstring;
143     style += "pt";
144     path->setAttribute("style", style.c_str());
146     // Glib::ustring transform("scale(1.25 1.25)");
147     // path->setAttribute("transform", transform.c_str());
149     current_layer->appendChild(path);
151     return;
154 /** \brief  A class to make an adjustment that uses Extension params */
155 class PrefAdjustment : public Gtk::Adjustment {
156     /** Extension that this relates to */
157     Inkscape::Extension::Extension * _ext;
158     /** The string which represents the parameter */
159     char * _pref;
160 public:
161     /** \brief  Make the adjustment using an extension and the string
162                 describing the parameter. */
163     PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
164             Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
165         this->set_value(_ext->get_param_float(_pref)); 
166         this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
167         return;
168     };
170     void val_changed (void);
171 }; /* class PrefAdjustment */
173 /** \brief  A function to respond to the value_changed signal from the
174             adjustment.
176     This function just grabs the value from the adjustment and writes
177     it to the parameter.  Very simple, but yet beautiful.
178 */
179 void
180 PrefAdjustment::val_changed (void)
182     // std::cout << "Value Changed to: " << this->get_value() << std::endl;
183     _ext->set_param_float(_pref, this->get_value());
184     return;
187 /** \brief  A function to get the prefences for the grid
188     \param  moudule  Module which holds the params
189     \param  view     Unused today - may get style information in the future.
191     Uses AutoGUI for creating the GUI.
192 */
193 Gtk::Widget *
194 Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view)
196     SPDocument * current_document = view->doc();
198     using Inkscape::Util::GSListConstIterator;
199     GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
200     Inkscape::XML::Node * first_select = NULL;
201     if (selected != NULL) 
202         first_select = SP_OBJECT_REPR(*selected);
204     return module->autogui(current_document, first_select);
207 #include "clear-n_.h"
209 void
210 Grid::init (void)
212     Inkscape::Extension::build_from_mem(
213         "<inkscape-extension>\n"
214             "<name>" N_("Grid") "</name>\n"
215             "<id>org.inkscape.effect.grid</id>\n"
216             "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
217             "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\">10.0</param>\n"
218             "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\">10.0</param>\n"
219             "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\">5.0</param>\n"
220             "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\">5.0</param>\n"
221             "<effect>\n"
222                 "<object-type>all</object-type>\n"
223                 "<effects-menu>\n"
224                     "<submenu name=\"" N_("Render") "\" />\n"
225                 "</effects-menu>\n"
226                 "<menu-tip>" N_("Draw a path which is a grid") "</menu-tip>\n"
227             "</effect>\n"
228         "</inkscape-extension>\n", new Grid());
229     return;
232 }; /* namespace Internal */
233 }; /* namespace Extension */
234 }; /* namespace Inkscape */
236 /*
237   Local Variables:
238   mode:c++
239   c-file-style:"stroustrup"
240   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
241   indent-tabs-mode:nil
242   fill-column:99
243   End:
244 */
245 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :