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/effect.h"
11 #include "svg/svg.h"
12 #include "svg/stringstream.h"
13 #include <gtkmm.h>
14 #include "ui/widget/point.h"
15 #include "widgets/icon.h"
17 #include "knot.h"
18 #include "inkscape.h"
19 #include "verbs.h"
21 #define noLPEPOINTPARAM_DEBUG
23 #define PRM_KNOT_COLOR_NORMAL 0xffffff00
24 #define PRM_KNOT_COLOR_SELECTED 0x0000ff00
26 namespace Inkscape {
28 namespace LivePathEffect {
30 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
31 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
32 Effect* effect, Geom::Point default_value )
33 : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
34 {
35 _widget = NULL;
36 pointwdg = NULL;
37 knot = NULL;
38 _tooltips = NULL;
39 }
41 PointParam::~PointParam()
42 {
43 if (pointwdg)
44 delete pointwdg;
45 if (_tooltips)
46 delete _tooltips;
48 if (knot)
49 g_object_unref (G_OBJECT (knot));
50 }
52 void
53 PointParam::param_set_default()
54 {
55 param_setValue(defvalue);
56 }
58 bool
59 PointParam::param_readSVGValue(const gchar * strvalue)
60 {
61 gchar ** strarray = g_strsplit(strvalue, ",", 2);
62 double newx, newy;
63 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
64 success += sp_svg_number_read_d(strarray[1], &newy);
65 g_strfreev (strarray);
66 if (success == 2) {
67 param_setValue( Geom::Point(newx, newy) );
68 return true;
69 }
70 return false;
71 }
73 gchar *
74 PointParam::param_writeSVGValue() const
75 {
76 Inkscape::SVGOStringStream os;
77 os << (*this)[0] << "," << (*this)[1];
78 gchar * str = g_strdup(os.str().c_str());
79 return str;
80 }
82 Gtk::Widget *
83 PointParam::param_newWidget(Gtk::Tooltips * tooltips)
84 {
85 // WIDGET TODO: This implementation is incorrect, it should create a *new* widget for the caller, not just return an already created widget
86 g_warning("PointParam::param_newWidget still needs recoding to work with multiple document views");
87 if (!_widget) {
88 pointwdg = new Inkscape::UI::Widget::RegisteredPoint();
89 pointwdg->init(param_label, param_tooltip, param_key, *param_wr, param_effect->getRepr(), param_effect->getSPDoc());
90 pointwdg->setValue( (*this)[0], (*this)[1] );
91 pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
93 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
94 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
95 pButton->set_relief(Gtk::RELIEF_NONE);
96 pIcon->show();
97 pButton->add(*pIcon);
98 pButton->show();
99 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PointParam::on_button_click));
100 #ifndef LPEPOINTPARAM_DEBUG
101 pButton->set_sensitive(false);
102 #endif
104 _widget = Gtk::manage( new Gtk::HBox() );
105 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
106 static_cast<Gtk::HBox*>(_widget)->pack_start(*(pointwdg->getPoint()), true, true);
107 static_cast<Gtk::HBox*>(_widget)->show_all_children();
109 tooltips->set_tip(*pButton, _("Edit on-canvas"));
110 }
111 return dynamic_cast<Gtk::Widget *> (_widget);
112 }
114 void
115 PointParam::param_setValue(Geom::Point newpoint)
116 {
117 *dynamic_cast<Geom::Point *>( this ) = newpoint;
118 if (pointwdg)
119 pointwdg->setValue(newpoint[0], newpoint[1]);
120 }
122 void
123 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
124 {
125 Inkscape::SVGOStringStream os;
126 os << newpoint[0] << "," << newpoint[1];
127 gchar * str = g_strdup(os.str().c_str());
128 param_write_to_repr(str);
129 g_free(str);
130 }
132 void
133 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
134 {
135 param_set_and_write_new_value( (*this) * postmul );
136 }
139 // CALLBACKS:
141 void
142 PointParam::on_button_click()
143 {
144 g_message("add knot to canvas on correct location :S");
146 if (!knot) {
147 // create the knot
148 knot = sp_knot_new (SP_ACTIVE_DESKTOP, NULL);
149 knot->setMode(SP_KNOT_MODE_XOR);
150 knot->setFill(PRM_KNOT_COLOR_NORMAL, PRM_KNOT_COLOR_NORMAL, PRM_KNOT_COLOR_NORMAL);
151 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
152 sp_knot_update_ctrl(knot);
154 // move knot to the given point
155 sp_knot_set_position (knot, &NR::Point(*static_cast<Geom::Point*>(this)), SP_KNOT_STATE_NORMAL);
156 sp_knot_show (knot);
157 /*
158 // connect knot's signals
159 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
160 // luckily, midstops never snap to other nodes so are never unsnapped...
161 && ( (draggable->point_type == POINT_LG_MID)
162 || (draggable->point_type == POINT_RG_MID1)
163 || (draggable->point_type == POINT_RG_MID2) ) )
164 {
165 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
166 } else {
167 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
168 }
169 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
170 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
171 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
172 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
173 */
174 }
175 }
177 } /* namespace LivePathEffect */
179 } /* namespace Inkscape */
181 /*
182 Local Variables:
183 mode:c++
184 c-file-style:"stroustrup"
185 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
186 indent-tabs-mode:nil
187 fill-column:99
188 End:
189 */
190 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :