Code

Merging from trunk
[inkscape.git] / src / live_effects / parameter / pointparam-knotholder.cpp
1 #define INKSCAPE_LPE_POINTPARAM_KNOTHOLDER_C
3 /*
4  * Container for PointParamKnotHolder visual handles
5  *
6  * Authors:
7  *   Johan Engelen <goejendaagh@zonnet.nl>
8  *
9  * Copyright (C) 2008 authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "live_effects/parameter/pointparam-knotholder.h"
15 #include "live_effects/lpeobject.h"
16 #include "document.h"
17 #include "sp-shape.h"
18 #include "knot.h"
19 #include "knotholder.h"
21 #include <glibmm/i18n.h>
22 #include <2geom/point.h>
23 #include <2geom/matrix.h>
24 #include "svg/stringstream.h"
25 #include "xml/repr.h"
27 class SPDesktop;
29 namespace Inkscape {
31 static void pointparam_knot_clicked_handler (SPKnot *knot, guint state, PointParamKnotHolder *kh);
32 static void pointparam_knot_moved_handler(SPKnot *knot, Geom::Point const *p, guint state, PointParamKnotHolder *kh);
33 static void pointparam_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, PointParamKnotHolder *kh);
35 PointParamKnotHolder::PointParamKnotHolder(SPDesktop *desktop, SPObject *lpeobject, const gchar * key, SPItem *item)
36 {
37     if (!desktop || !item || !SP_IS_ITEM(item)) {
38         g_print ("Error! Throw an exception, please!\n");
39     }
41     this->desktop = desktop;
42     this->item = item;
43     this->lpeobject = LIVEPATHEFFECT(lpeobject);
44     g_object_ref(G_OBJECT(item));
45     g_object_ref(G_OBJECT(lpeobject));
47     this->released = NULL;
49     this->repr = lpeobject->repr;
50     this->repr_key = key;
52     this->local_change = FALSE;
53 }
55 PointParamKnotHolder::~PointParamKnotHolder()
56 {
57     g_object_unref(G_OBJECT(this->item));
58     g_object_unref(G_OBJECT(this->lpeobject));
59 }
61 class KnotHolderEntityPointParam : public LPEKnotHolderEntity {
62 public:
63     virtual Geom::Point knot_get();
64     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
65 };
67 Geom::Point
68 KnotHolderEntityPointParam::knot_get() {
69     return Geom::Point(0,0);
70 }
72 void
73 KnotHolderEntityPointParam::knot_set(Geom::Point const &/*p*/, Geom::Point const &/*origin*/, guint /*state*/) {
74 }
76 void
77 PointParamKnotHolder::add_knot (
78     Geom::Point         & p,
79 // TODO: check if knot_click being ignored is bad:
80     PointParamKnotHolderClickedFunc /*knot_click*/,
81     SPKnotShapeType     shape,
82     SPKnotModeType      mode,
83     guint32             color,
84     const gchar *tip )
85 {
86     /* create new SPKnotHolderEntry */
87     KnotHolderEntity *e = new KnotHolderEntityPointParam();
88     e->create(this->desktop, this->item, this, tip, shape, mode, color);
90     entity.push_back(e);
92     // Move to current point.
93     Geom::Point dp = p * sp_item_i2d_affine(item);
94     sp_knot_set_position(e->knot, dp, SP_KNOT_STATE_NORMAL);
96     e->handler_id = g_signal_connect(e->knot, "moved", G_CALLBACK(pointparam_knot_moved_handler), this);
97     e->_click_handler_id = g_signal_connect(e->knot, "clicked", G_CALLBACK(pointparam_knot_clicked_handler), this);
98     e->_ungrab_handler_id = g_signal_connect(e->knot, "ungrabbed", G_CALLBACK(pointparam_knot_ungrabbed_handler), this);
100     sp_knot_show(e->knot);
103 static void pointparam_knot_clicked_handler(SPKnot */*knot*/, guint /*state*/, PointParamKnotHolder */*kh*/)
108 /**
109  * \param p In desktop coordinates.
110  *  This function does not write to XML, but tries to write directly to the PointParam to quickly live update the effect
111  */
112 static void pointparam_knot_moved_handler(SPKnot */*knot*/, Geom::Point const *p, guint /*state*/, PointParamKnotHolder *kh)
114     Geom::Matrix const i2d(sp_item_i2d_affine(kh->getItem()));
115     Geom::Point pos = (*p) * i2d.inverse();
117     Inkscape::SVGOStringStream os;
118     os << pos;
120     // note: get_lpe() will always return a valid pointer?
121     kh->lpeobject->get_lpe()->setParameter(kh->repr_key, os.str().c_str());
124 static void pointparam_knot_ungrabbed_handler(SPKnot *knot, unsigned int /*state*/, PointParamKnotHolder *kh)
126     Geom::Matrix const i2d(sp_item_i2d_affine(kh->getItem()));
127     Geom::Point pos = sp_knot_position(knot) * i2d.inverse();
129     Inkscape::SVGOStringStream os;
130     os << pos;
132     kh->repr->setAttribute(kh->repr_key , os.str().c_str());
134     sp_document_done(SP_OBJECT_DOCUMENT (kh->lpeobject), SP_VERB_CONTEXT_LPE, _("Change LPE point parameter"));
137 } // namespace Inkscape
139 /*
140   Local Variables:
141   mode:c++
142   c-file-style:"stroustrup"
143   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
144   indent-tabs-mode:nil
145   fill-column:99
146   End:
147 */
148 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :