Code

Enable status bar tips for LPE handles that are automatically created from PointParams
[inkscape.git] / src / live_effects / parameter / point.cpp
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     oncanvas_editable = true;
40     knot_shape = SP_KNOT_SHAPE_SQUARE;
41     knot_mode  = SP_KNOT_MODE_XOR;
42     knot_color = 0x00ff0000;
43     handle_tip = g_strdup(htip);
44 }
46 PointParam::~PointParam()
47 {
48     if (handle_tip)
49         g_free(handle_tip);
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_getSVGValue() const
75 {
76     Inkscape::SVGOStringStream os;
77     os << *dynamic_cast<Geom::Point const *>( this );
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     Inkscape::UI::Widget::RegisteredTransformedPoint * pointwdg = Gtk::manage(
86         new Inkscape::UI::Widget::RegisteredTransformedPoint( param_label,
87                                                               param_tooltip,
88                                                               param_key,
89                                                               *param_wr,
90                                                               param_effect->getRepr(),
91                                                               param_effect->getSPDoc() ) );
92     // TODO: fix to get correct desktop (don't use SP_ACTIVE_DESKTOP)
93     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
94     Geom::Matrix transf = to_2geom(desktop->doc2dt());
95     pointwdg->setTransform(transf);
96     pointwdg->setValue( *this );
97     pointwdg->clearProgrammatically();
98     pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
100     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
101     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
102     pButton->set_relief(Gtk::RELIEF_NONE);
103     pIcon->show();
104     pButton->add(*pIcon);
105     pButton->show();
107     Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
108     static_cast<Gtk::HBox*>(hbox)->pack_start(*pButton, true, true);
109     static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
110     static_cast<Gtk::HBox*>(hbox)->show_all_children();
112     tooltips->set_tip(*pButton, _("Edit on-canvas"));
114     return dynamic_cast<Gtk::Widget *> (hbox);
117 void
118 PointParam::param_setValue(Geom::Point newpoint)
120     *dynamic_cast<Geom::Point *>( this ) = newpoint;
123 void
124 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
126     Inkscape::SVGOStringStream os;
127     os << newpoint;
128     gchar * str = g_strdup(os.str().c_str());
129     param_write_to_repr(str);
130     g_free(str);
133 void
134 PointParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
136     // If not already in nodecontext, goto it!
137     if (!tools_isactive(dt, TOOLS_NODES)) {
138         tools_switch_current(TOOLS_NODES);
139     }
141     PointParamKnotHolder * kh =  new PointParamKnotHolder(dt, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str(), item);
142     if (kh) {
143         kh->add_knot(* dynamic_cast<Geom::Point *>( this ), NULL, knot_shape, knot_mode, knot_color, param_getTooltip()->c_str() );
145         ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
146         shape_editor->set_knotholder(kh);
147     }
152 void
153 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
155     param_set_and_write_new_value( (*this) * postmul );
159 void
160 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
162     knot_shape = shape;
163     knot_mode  = mode;
164     knot_color = color;
167 void
168 PointParam::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
170     param_setValue(p.to_2geom());
171     sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
174 NR::Point
175 PointParam::knot_get()
177     return *this;
180 void
181 PointParam::knot_click(guint state)
183     g_print ("This is the handle associated to the parameter '%s'\n", param_key.c_str());
186 } /* namespace LivePathEffect */
188 } /* namespace Inkscape */
190 /*
191   Local Variables:
192   mode:c++
193   c-file-style:"stroustrup"
194   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
195   indent-tabs-mode:nil
196   fill-column:99
197   End:
198 */
199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :