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, const gchar *htip, Geom::Point default_value)
36 : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
37 {
38 knot_shape = SP_KNOT_SHAPE_DIAMOND;
39 knot_mode = SP_KNOT_MODE_XOR;
40 knot_color = 0xffffff00;
41 handle_tip = g_strdup(htip);
42 }
44 PointParam::~PointParam()
45 {
46 if (handle_tip)
47 g_free(handle_tip);
48 }
50 void
51 PointParam::param_set_default()
52 {
53 param_setValue(defvalue);
54 }
56 bool
57 PointParam::param_readSVGValue(const gchar * strvalue)
58 {
59 gchar ** strarray = g_strsplit(strvalue, ",", 2);
60 double newx, newy;
61 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
62 success += sp_svg_number_read_d(strarray[1], &newy);
63 g_strfreev (strarray);
64 if (success == 2) {
65 param_setValue( Geom::Point(newx, newy) );
66 return true;
67 }
68 return false;
69 }
71 gchar *
72 PointParam::param_getSVGValue() const
73 {
74 Inkscape::SVGOStringStream os;
75 os << *dynamic_cast<Geom::Point const *>( this );
76 gchar * str = g_strdup(os.str().c_str());
77 return str;
78 }
80 Gtk::Widget *
81 PointParam::param_newWidget(Gtk::Tooltips * /*tooltips*/)
82 {
83 Inkscape::UI::Widget::RegisteredTransformedPoint * pointwdg = Gtk::manage(
84 new Inkscape::UI::Widget::RegisteredTransformedPoint( param_label,
85 param_tooltip,
86 param_key,
87 *param_wr,
88 param_effect->getRepr(),
89 param_effect->getSPDoc() ) );
90 // TODO: fix to get correct desktop (don't use SP_ACTIVE_DESKTOP)
91 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
92 Geom::Matrix transf = to_2geom(desktop->doc2dt());
93 pointwdg->setTransform(transf);
94 pointwdg->setValue( *this );
95 pointwdg->clearProgrammatically();
96 pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
98 Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
99 static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
100 static_cast<Gtk::HBox*>(hbox)->show_all_children();
102 return dynamic_cast<Gtk::Widget *> (hbox);
103 }
105 void
106 PointParam::param_setValue(Geom::Point newpoint)
107 {
108 *dynamic_cast<Geom::Point *>( this ) = newpoint;
109 }
111 void
112 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
113 {
114 Inkscape::SVGOStringStream os;
115 os << newpoint;
116 gchar * str = g_strdup(os.str().c_str());
117 param_write_to_repr(str);
118 g_free(str);
119 }
121 void
122 PointParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
123 {
124 // If not already in nodecontext, goto it!
125 if (!tools_isactive(dt, TOOLS_NODES)) {
126 tools_switch_current(TOOLS_NODES);
127 }
129 PointParamKnotHolder * kh = new PointParamKnotHolder(dt, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str(), item);
130 if (kh) {
131 kh->add_knot(* dynamic_cast<Geom::Point *>( this ), NULL, knot_shape, knot_mode, knot_color, param_getTooltip()->c_str() );
133 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
134 shape_editor->set_knotholder(kh);
135 }
136 }
140 void
141 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
142 {
143 param_set_and_write_new_value( (*this) * postmul );
144 }
147 void
148 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
149 {
150 knot_shape = shape;
151 knot_mode = mode;
152 knot_color = color;
153 }
155 class PointParamKnotHolderEntity : public KnotHolderEntity {
156 public:
157 PointParamKnotHolderEntity(PointParam *p) { this->pparam = p; }
158 virtual ~PointParamKnotHolderEntity() {}
160 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
161 virtual NR::Point knot_get();
162 virtual void knot_click(guint state);
164 private:
165 PointParam *pparam;
166 };
168 void
169 PointParamKnotHolderEntity::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
170 {
171 pparam->param_setValue(p.to_2geom());
172 sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
173 }
175 NR::Point
176 PointParamKnotHolderEntity::knot_get()
177 {
178 return *pparam;
179 }
181 void
182 PointParamKnotHolderEntity::knot_click(guint /*state*/)
183 {
184 g_print ("This is the handle associated to parameter '%s'\n", pparam->param_key.c_str());
185 }
187 void
188 PointParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
189 {
190 PointParamKnotHolderEntity *e = new PointParamKnotHolderEntity(this);
191 // TODO: can we ditch handleTip() etc. because we have access to handle_tip etc. itself???
192 e->create(desktop, item, knotholder, handleTip(), knotShape(), knotMode(), knotColor());
193 knotholder->add(e);
195 }
197 } /* namespace LivePathEffect */
199 } /* namespace Inkscape */
201 /*
202 Local Variables:
203 mode:c++
204 c-file-style:"stroustrup"
205 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
206 indent-tabs-mode:nil
207 fill-column:99
208 End:
209 */
210 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :