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"
28 namespace Inkscape {
30 namespace LivePathEffect {
32 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
33 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
34 Effect* effect, Geom::Point default_value )
35 : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
36 {
37 oncanvas_editable = true;
39 knot_shape = SP_KNOT_SHAPE_SQUARE;
40 knot_mode = SP_KNOT_MODE_XOR;
41 knot_color = 0x00ff0000;
42 }
44 PointParam::~PointParam()
45 {
46 }
48 void
49 PointParam::param_set_default()
50 {
51 param_setValue(defvalue);
52 }
54 bool
55 PointParam::param_readSVGValue(const gchar * strvalue)
56 {
57 gchar ** strarray = g_strsplit(strvalue, ",", 2);
58 double newx, newy;
59 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
60 success += sp_svg_number_read_d(strarray[1], &newy);
61 g_strfreev (strarray);
62 if (success == 2) {
63 param_setValue( Geom::Point(newx, newy) );
64 return true;
65 }
66 return false;
67 }
69 gchar *
70 PointParam::param_writeSVGValue() const
71 {
72 Inkscape::SVGOStringStream os;
73 os << *dynamic_cast<Geom::Point const *>( this );
74 gchar * str = g_strdup(os.str().c_str());
75 return str;
76 }
78 Gtk::Widget *
79 PointParam::param_newWidget(Gtk::Tooltips * tooltips)
80 {
81 Inkscape::UI::Widget::RegisteredPoint * pointwdg = Gtk::manage(
82 new Inkscape::UI::Widget::RegisteredPoint( param_label,
83 param_tooltip,
84 param_key,
85 *param_wr,
86 param_effect->getRepr(),
87 param_effect->getSPDoc() ) );
88 pointwdg->setValue( (*this)[0], (*this)[1] );
89 pointwdg->clearProgrammatically();
90 pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
92 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
93 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
94 pButton->set_relief(Gtk::RELIEF_NONE);
95 pIcon->show();
96 pButton->add(*pIcon);
97 pButton->show();
98 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PointParam::on_button_click));
100 Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
101 static_cast<Gtk::HBox*>(hbox)->pack_start(*pButton, true, true);
102 static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
103 static_cast<Gtk::HBox*>(hbox)->show_all_children();
105 tooltips->set_tip(*pButton, _("Edit on-canvas"));
107 return dynamic_cast<Gtk::Widget *> (hbox);
108 }
110 void
111 PointParam::param_setValue(Geom::Point newpoint)
112 {
113 *dynamic_cast<Geom::Point *>( this ) = newpoint;
114 }
116 void
117 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
118 {
119 Inkscape::SVGOStringStream os;
120 os << newpoint;
121 gchar * str = g_strdup(os.str().c_str());
122 param_write_to_repr(str);
123 g_free(str);
124 }
126 void
127 PointParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
128 {
129 // If not already in nodecontext, goto it!
130 if (!tools_isactive(dt, TOOLS_NODES)) {
131 tools_switch_current(TOOLS_NODES);
132 }
134 PointParamKnotHolder * kh = pointparam_knot_holder_new( dt, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str(), item);
135 if (kh) {
136 kh->add_knot(* dynamic_cast<Geom::Point *>( this ), NULL, knot_shape, knot_mode, knot_color, param_getTooltip()->c_str() );
138 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
139 shape_editor->set_knotholder(kh);
140 }
141 }
145 void
146 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
147 {
148 param_set_and_write_new_value( (*this) * postmul );
149 }
152 void
153 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
154 {
155 knot_shape = shape;
156 knot_mode = mode;
157 knot_color = color;
158 }
161 // CALLBACKS:
163 void
164 PointParam::on_button_click()
165 {
166 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
167 SPItem * item = sp_desktop_selection(desktop)->singleItem();
168 if (item != NULL) {
169 param_editOncanvas(item, desktop);
170 }
171 }
173 } /* namespace LivePathEffect */
175 } /* namespace Inkscape */
177 /*
178 Local Variables:
179 mode:c++
180 c-file-style:"stroustrup"
181 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
182 indent-tabs-mode:nil
183 fill-column:99
184 End:
185 */
186 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :