Code

decrease header deps
[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"
19 #include "knotholder.h"
21 // needed for on-canvas editting:
22 #include "desktop.h"
24 namespace Inkscape {
26 namespace LivePathEffect {
28 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
29                         const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
30                         Effect* effect, const gchar *htip, Geom::Point default_value)
31     : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
32 {
33     knot_shape = SP_KNOT_SHAPE_DIAMOND;
34     knot_mode  = SP_KNOT_MODE_XOR;
35     knot_color = 0xffffff00;
36     handle_tip = g_strdup(htip);
37 }
39 PointParam::~PointParam()
40 {
41     if (handle_tip)
42         g_free(handle_tip);
43 }
45 void
46 PointParam::param_set_default()
47 {
48     param_setValue(defvalue);
49 }
51 bool
52 PointParam::param_readSVGValue(const gchar * strvalue)
53 {
54     gchar ** strarray = g_strsplit(strvalue, ",", 2);
55     double newx, newy;
56     unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
57     success += sp_svg_number_read_d(strarray[1], &newy);
58     g_strfreev (strarray);
59     if (success == 2) {
60         param_setValue( Geom::Point(newx, newy) );
61         return true;
62     }
63     return false;
64 }
66 gchar *
67 PointParam::param_getSVGValue() const
68 {
69     Inkscape::SVGOStringStream os;
70     os << *dynamic_cast<Geom::Point const *>( this );
71     gchar * str = g_strdup(os.str().c_str());
72     return str;
73 }
75 Gtk::Widget *
76 PointParam::param_newWidget(Gtk::Tooltips * /*tooltips*/)
77 {
78     Inkscape::UI::Widget::RegisteredTransformedPoint * pointwdg = Gtk::manage(
79         new Inkscape::UI::Widget::RegisteredTransformedPoint( param_label,
80                                                               param_tooltip,
81                                                               param_key,
82                                                               *param_wr,
83                                                               param_effect->getRepr(),
84                                                               param_effect->getSPDoc() ) );
85     // TODO: fix to get correct desktop (don't use SP_ACTIVE_DESKTOP)
86     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
87     Geom::Matrix transf = desktop->doc2dt();
88     pointwdg->setTransform(transf);
89     pointwdg->setValue( *this );
90     pointwdg->clearProgrammatically();
91     pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
93     Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
94     static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
95     static_cast<Gtk::HBox*>(hbox)->show_all_children();
97     return dynamic_cast<Gtk::Widget *> (hbox);
98 }
100 void
101 PointParam::param_setValue(Geom::Point newpoint)
103     *dynamic_cast<Geom::Point *>( this ) = newpoint;
106 void
107 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
109     Inkscape::SVGOStringStream os;
110     os << newpoint;
111     gchar * str = g_strdup(os.str().c_str());
112     param_write_to_repr(str);
113     g_free(str);
116 void
117 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
119     param_set_and_write_new_value( (*this) * postmul );
123 void
124 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
126     knot_shape = shape;
127     knot_mode  = mode;
128     knot_color = color;
131 class PointParamKnotHolderEntity : public LPEKnotHolderEntity {
132 public:
133     PointParamKnotHolderEntity(PointParam *p) { this->pparam = p; }
134     virtual ~PointParamKnotHolderEntity() {}
136     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
137     virtual Geom::Point knot_get();
138     virtual void knot_click(guint state);
140 private:
141     PointParam *pparam;
142 };
144 void
145 PointParamKnotHolderEntity::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
147     Geom::Point const s = snap_knot_position(p);
148     pparam->param_setValue(s);
149     sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
152 Geom::Point
153 PointParamKnotHolderEntity::knot_get()
155     return *pparam;
158 void
159 PointParamKnotHolderEntity::knot_click(guint /*state*/)
161     g_print ("This is the handle associated to parameter '%s'\n", pparam->param_key.c_str());
164 void
165 PointParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
167     PointParamKnotHolderEntity *e = new PointParamKnotHolderEntity(this);
168     // TODO: can we ditch handleTip() etc. because we have access to handle_tip etc. itself???
169     e->create(desktop, item, knotholder, handleTip(), knot_shape, knot_mode, knot_color);
170     knotholder->add(e);
174 } /* namespace LivePathEffect */
176 } /* namespace Inkscape */
178 /*
179   Local Variables:
180   mode:c++
181   c-file-style:"stroustrup"
182   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
183   indent-tabs-mode:nil
184   fill-column:99
185   End:
186 */
187 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :