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 = 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_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
123 {
124 param_set_and_write_new_value( (*this) * postmul );
125 }
128 void
129 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
130 {
131 knot_shape = shape;
132 knot_mode = mode;
133 knot_color = color;
134 }
136 class PointParamKnotHolderEntity : public LPEKnotHolderEntity {
137 public:
138 PointParamKnotHolderEntity(PointParam *p) { this->pparam = p; }
139 virtual ~PointParamKnotHolderEntity() {}
141 virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
142 virtual Geom::Point knot_get();
143 virtual void knot_click(guint state);
145 private:
146 PointParam *pparam;
147 };
149 void
150 PointParamKnotHolderEntity::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
151 {
152 Geom::Point const s = snap_knot_position(p);
153 pparam->param_setValue(s);
154 sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
155 }
157 Geom::Point
158 PointParamKnotHolderEntity::knot_get()
159 {
160 return *pparam;
161 }
163 void
164 PointParamKnotHolderEntity::knot_click(guint /*state*/)
165 {
166 g_print ("This is the handle associated to parameter '%s'\n", pparam->param_key.c_str());
167 }
169 void
170 PointParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
171 {
172 PointParamKnotHolderEntity *e = new PointParamKnotHolderEntity(this);
173 // TODO: can we ditch handleTip() etc. because we have access to handle_tip etc. itself???
174 e->create(desktop, item, knotholder, handleTip(), knot_shape, knot_mode, knot_color);
175 knotholder->add(e);
177 }
179 } /* namespace LivePathEffect */
181 } /* namespace Inkscape */
183 /*
184 Local Variables:
185 mode:c++
186 c-file-style:"stroustrup"
187 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
188 indent-tabs-mode:nil
189 fill-column:99
190 End:
191 */
192 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :