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 knot = NULL;
36 }
38 PointParam::~PointParam()
39 {
40 if (knot)
41 g_object_unref (G_OBJECT (knot));
42 }
44 void
45 PointParam::param_set_default()
46 {
47 param_setValue(defvalue);
48 }
50 bool
51 PointParam::param_readSVGValue(const gchar * strvalue)
52 {
53 gchar ** strarray = g_strsplit(strvalue, ",", 2);
54 double newx, newy;
55 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
56 success += sp_svg_number_read_d(strarray[1], &newy);
57 g_strfreev (strarray);
58 if (success == 2) {
59 param_setValue( Geom::Point(newx, newy) );
60 return true;
61 }
62 return false;
63 }
65 gchar *
66 PointParam::param_writeSVGValue() const
67 {
68 Inkscape::SVGOStringStream os;
69 os << (*this)[0] << "," << (*this)[1];
70 gchar * str = g_strdup(os.str().c_str());
71 return str;
72 }
74 Gtk::Widget *
75 PointParam::param_newWidget(Gtk::Tooltips * tooltips)
76 {
77 // WIDGET TODO: This implementation is incorrect, it should create a *new* widget for the caller, not just return an already created widget
78 g_warning("PointParam::param_newWidget still needs recoding to work with multiple document views");
79 Inkscape::UI::Widget::RegisteredPoint * pointwdg = Gtk::manage(
80 new Inkscape::UI::Widget::RegisteredPoint( param_label,
81 param_tooltip,
82 param_key,
83 *param_wr,
84 param_effect->getRepr(),
85 param_effect->getSPDoc() ) );
86 pointwdg->setValue( (*this)[0], (*this)[1] );
87 pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
89 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
90 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
91 pButton->set_relief(Gtk::RELIEF_NONE);
92 pIcon->show();
93 pButton->add(*pIcon);
94 pButton->show();
95 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PointParam::on_button_click));
96 #ifndef LPEPOINTPARAM_DEBUG
97 pButton->set_sensitive(false);
98 #endif
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[0] << "," << newpoint[1];
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_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
128 {
129 param_set_and_write_new_value( (*this) * postmul );
130 }
133 // CALLBACKS:
135 void
136 PointParam::on_button_click()
137 {
138 g_message("add knot to canvas on correct location :S");
140 if (!knot) {
141 // create the knot
142 knot = sp_knot_new (SP_ACTIVE_DESKTOP, NULL);
143 knot->setMode(SP_KNOT_MODE_XOR);
144 knot->setFill(PRM_KNOT_COLOR_NORMAL, PRM_KNOT_COLOR_NORMAL, PRM_KNOT_COLOR_NORMAL);
145 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
146 sp_knot_update_ctrl(knot);
148 // move knot to the given point
149 sp_knot_set_position (knot, &NR::Point(*static_cast<Geom::Point*>(this)), SP_KNOT_STATE_NORMAL);
150 sp_knot_show (knot);
151 /*
152 // connect knot's signals
153 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
154 // luckily, midstops never snap to other nodes so are never unsnapped...
155 && ( (draggable->point_type == POINT_LG_MID)
156 || (draggable->point_type == POINT_RG_MID1)
157 || (draggable->point_type == POINT_RG_MID2) ) )
158 {
159 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
160 } else {
161 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
162 }
163 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
164 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
165 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
166 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
167 */
168 }
169 }
171 } /* namespace LivePathEffect */
173 } /* namespace Inkscape */
175 /*
176 Local Variables:
177 mode:c++
178 c-file-style:"stroustrup"
179 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
180 indent-tabs-mode:nil
181 fill-column:99
182 End:
183 */
184 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :