Code

Merge from fe-moved
[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/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"
16 #include "ui/widget/registered-widget.h"
17 #include "inkscape.h"
18 #include "verbs.h"
20 // needed for on-canvas editting:
21 #include "desktop.h"
23 namespace Inkscape {
25 namespace LivePathEffect {
27 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
28                         const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
29                         Effect* effect, const gchar *htip, Geom::Point default_value)
30     : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
31 {
32     knot_shape = SP_KNOT_SHAPE_DIAMOND;
33     knot_mode  = SP_KNOT_MODE_XOR;
34     knot_color = 0xffffff00;
35     handle_tip = g_strdup(htip);
36 }
38 PointParam::~PointParam()
39 {
40     if (handle_tip)
41         g_free(handle_tip);
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_getSVGValue() const
67 {
68     Inkscape::SVGOStringStream os;
69     os << *dynamic_cast<Geom::Point const *>( this );
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     Inkscape::UI::Widget::RegisteredTransformedPoint * pointwdg = Gtk::manage(
78         new Inkscape::UI::Widget::RegisteredTransformedPoint( param_label,
79                                                               param_tooltip,
80                                                               param_key,
81                                                               *param_wr,
82                                                               param_effect->getRepr(),
83                                                               param_effect->getSPDoc() ) );
84     // TODO: fix to get correct desktop (don't use SP_ACTIVE_DESKTOP)
85     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
86     Geom::Matrix transf = desktop->doc2dt();
87     pointwdg->setTransform(transf);
88     pointwdg->setValue( *this );
89     pointwdg->clearProgrammatically();
90     pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
92     Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
93     static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
94     static_cast<Gtk::HBox*>(hbox)->show_all_children();
96     return dynamic_cast<Gtk::Widget *> (hbox);
97 }
99 void
100 PointParam::param_setValue(Geom::Point newpoint)
102     *dynamic_cast<Geom::Point *>( this ) = newpoint;
105 void
106 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
108     Inkscape::SVGOStringStream os;
109     os << newpoint;
110     gchar * str = g_strdup(os.str().c_str());
111     param_write_to_repr(str);
112     g_free(str);
115 void
116 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
118     param_set_and_write_new_value( (*this) * postmul );
122 void
123 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
125     knot_shape = shape;
126     knot_mode  = mode;
127     knot_color = color;
130 class PointParamKnotHolderEntity : public LPEKnotHolderEntity {
131 public:
132     PointParamKnotHolderEntity(PointParam *p) { this->pparam = p; }
133     virtual ~PointParamKnotHolderEntity() {}
135     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
136     virtual Geom::Point knot_get();
137     virtual void knot_click(guint state);
139 private:
140     PointParam *pparam;
141 };
143 void
144 PointParamKnotHolderEntity::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
146     Geom::Point const s = snap_knot_position(p);
147     pparam->param_setValue(s);
148     sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
151 Geom::Point
152 PointParamKnotHolderEntity::knot_get()
154     return *pparam;
157 void
158 PointParamKnotHolderEntity::knot_click(guint /*state*/)
160     g_print ("This is the handle associated to parameter '%s'\n", pparam->param_key.c_str());
163 void
164 PointParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
166     PointParamKnotHolderEntity *e = new PointParamKnotHolderEntity(this);
167     // TODO: can we ditch handleTip() etc. because we have access to handle_tip etc. itself???
168     e->create(desktop, item, knotholder, handleTip(), knot_shape, knot_mode, knot_color);
169     knotholder->add(e);
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 :