1 #define INKSCAPE_LIVEPATHEFFECT_PARAMETER_POINT_CPP
3 /*
4 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5 *
6 * Released under GNU GPL, read the file 'COPYING' for more information
7 */
9 #include "live_effects/parameter/point.h"
10 #include "live_effects/parameter/pointparam-knotholder.h"
11 #include "live_effects/effect.h"
12 #include "svg/svg.h"
13 #include "svg/stringstream.h"
14 #include <gtkmm.h>
15 #include "ui/widget/point.h"
16 #include "widgets/icon.h"
17 #include "ui/widget/registered-widget.h"
18 #include "inkscape.h"
19 #include "verbs.h"
21 // needed for on-canvas editting:
22 #include "tools-switch.h"
23 #include "node-context.h"
24 #include "shape-editor.h"
25 #include "desktop.h"
26 #include "selection.h"
27 #include "libnr/nr-convert2geom.h"
29 namespace Inkscape {
31 namespace LivePathEffect {
33 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
34 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
35 Effect* effect, Geom::Point default_value )
36 : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
37 {
38 oncanvas_editable = true;
40 knot_shape = SP_KNOT_SHAPE_SQUARE;
41 knot_mode = SP_KNOT_MODE_XOR;
42 knot_color = 0x00ff0000;
43 }
45 PointParam::~PointParam()
46 {
47 }
49 void
50 PointParam::param_set_default()
51 {
52 param_setValue(defvalue);
53 }
55 bool
56 PointParam::param_readSVGValue(const gchar * strvalue)
57 {
58 gchar ** strarray = g_strsplit(strvalue, ",", 2);
59 double newx, newy;
60 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
61 success += sp_svg_number_read_d(strarray[1], &newy);
62 g_strfreev (strarray);
63 if (success == 2) {
64 param_setValue( Geom::Point(newx, newy) );
65 return true;
66 }
67 return false;
68 }
70 gchar *
71 PointParam::param_getSVGValue() const
72 {
73 Inkscape::SVGOStringStream os;
74 os << *dynamic_cast<Geom::Point const *>( this );
75 gchar * str = g_strdup(os.str().c_str());
76 return str;
77 }
79 Gtk::Widget *
80 PointParam::param_newWidget(Gtk::Tooltips * tooltips)
81 {
82 Inkscape::UI::Widget::RegisteredTransformedPoint * pointwdg = Gtk::manage(
83 new Inkscape::UI::Widget::RegisteredTransformedPoint( param_label,
84 param_tooltip,
85 param_key,
86 *param_wr,
87 param_effect->getRepr(),
88 param_effect->getSPDoc() ) );
89 // TODO: fix to get correct desktop (don't use SP_ACTIVE_DESKTOP)
90 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
91 Geom::Matrix transf = to_2geom(desktop->doc2dt());
92 pointwdg->setTransform(transf);
93 pointwdg->setValue( *this );
94 pointwdg->clearProgrammatically();
95 pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
97 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
98 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
99 pButton->set_relief(Gtk::RELIEF_NONE);
100 pIcon->show();
101 pButton->add(*pIcon);
102 pButton->show();
104 Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
105 static_cast<Gtk::HBox*>(hbox)->pack_start(*pButton, true, true);
106 static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
107 static_cast<Gtk::HBox*>(hbox)->show_all_children();
109 tooltips->set_tip(*pButton, _("Edit on-canvas"));
111 return dynamic_cast<Gtk::Widget *> (hbox);
112 }
114 void
115 PointParam::param_setValue(Geom::Point newpoint)
116 {
117 *dynamic_cast<Geom::Point *>( this ) = newpoint;
118 }
120 void
121 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
122 {
123 Inkscape::SVGOStringStream os;
124 os << newpoint;
125 gchar * str = g_strdup(os.str().c_str());
126 param_write_to_repr(str);
127 g_free(str);
128 }
130 void
131 PointParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
132 {
133 // If not already in nodecontext, goto it!
134 if (!tools_isactive(dt, TOOLS_NODES)) {
135 tools_switch_current(TOOLS_NODES);
136 }
138 PointParamKnotHolder * kh = new PointParamKnotHolder(dt, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str(), item);
139 if (kh) {
140 kh->add_knot(* dynamic_cast<Geom::Point *>( this ), NULL, knot_shape, knot_mode, knot_color, param_getTooltip()->c_str() );
142 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
143 shape_editor->set_knotholder(kh);
144 }
145 }
149 void
150 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
151 {
152 param_set_and_write_new_value( (*this) * postmul );
153 }
156 void
157 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
158 {
159 knot_shape = shape;
160 knot_mode = mode;
161 knot_color = color;
162 }
164 void
165 PointParam::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
166 {
167 param_setValue(p.to_2geom());
168 sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
169 }
171 NR::Point
172 PointParam::knot_get()
173 {
174 return *this;
175 }
177 void
178 PointParam::knot_click(guint state)
179 {
180 g_print ("This is the handle associated to the parameter '%s'\n", param_key.c_str());
181 }
183 } /* namespace LivePathEffect */
185 } /* namespace Inkscape */
187 /*
188 Local Variables:
189 mode:c++
190 c-file-style:"stroustrup"
191 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
192 indent-tabs-mode:nil
193 fill-column:99
194 End:
195 */
196 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :