1 #define __KNOT_HOLDER_C__
3 /*
4 * Container for SPKnot visual handles
5 *
6 * Authors:
7 * Mitsuru Oka <oka326@parkcity.ne.jp>
8 * bulia byak <buliabyak@users.sf.net>
9 * Maximilian Albert <maximilian.albert@gmail.com>
10 *
11 * Copyright (C) 2001-2008 authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "document.h"
17 #include "sp-shape.h"
18 #include "knot.h"
19 #include "knotholder.h"
20 #include "rect-context.h"
21 #include "sp-rect.h"
22 #include "arc-context.h"
23 #include "sp-ellipse.h"
24 #include "star-context.h"
25 #include "sp-star.h"
26 #include "spiral-context.h"
27 #include "sp-spiral.h"
28 #include "sp-offset.h"
29 #include "box3d.h"
30 #include "sp-pattern.h"
31 #include "style.h"
32 #include "live_effects/lpeobject.h"
33 #include "desktop.h"
34 #include "display/sp-canvas.h"
36 #include "xml/repr.h" // for debugging only
38 #include <glibmm/i18n.h>
40 class SPDesktop;
42 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
43 {
44 Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
46 if (!desktop || !item || !SP_IS_ITEM(item)) {
47 g_print ("Error! Throw an exception, please!\n");
48 }
50 this->desktop = desktop;
51 this->item = item;
52 g_object_ref(G_OBJECT(item)); // TODO: is this still needed after C++-ification?
54 this->released = relhandler;
56 this->repr = repr;
57 this->local_change = FALSE;
59 this->dragging = false;
60 }
62 KnotHolder::~KnotHolder() {
63 g_object_unref(G_OBJECT(item));
64 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
65 KnotHolderEntity* e = (*i);
66 if (e->isDeletable()) {
67 delete (*i);
68 } else {
69 // we must not delete the entity (since it's attached to an LPE parameter),
70 // but the handle should be destroyed
71 g_object_unref(e->knot);
72 }
73 (*i) = NULL;
74 }
75 entity.clear(); // is this necessary?
76 }
78 /**
79 * \param p In desktop coordinates.
80 */
82 void
83 KnotHolder::update_knots()
84 {
85 Geom::Matrix const i2d(sp_item_i2d_affine(item));
87 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
88 KnotHolderEntity *e = *i;
89 e->update_knot();
90 }
91 }
93 void
94 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
95 {
96 KnotHolder *knot_holder = this;
98 for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
99 KnotHolderEntity *e = *i;
100 if (e->knot == knot) {
101 // no need to test whether knot_click exists since it's virtual now
102 e->knot_click(state);
103 break;
104 }
105 }
107 if (SP_IS_SHAPE(item)) {
108 sp_shape_set_shape(SP_SHAPE(item));
109 }
111 knot_holder->update_knots();
113 unsigned int object_verb = SP_VERB_NONE;
115 if (SP_IS_RECT(item))
116 object_verb = SP_VERB_CONTEXT_RECT;
117 else if (SP_IS_BOX3D(item))
118 object_verb = SP_VERB_CONTEXT_3DBOX;
119 else if (SP_IS_GENERICELLIPSE(item))
120 object_verb = SP_VERB_CONTEXT_ARC;
121 else if (SP_IS_STAR(item))
122 object_verb = SP_VERB_CONTEXT_STAR;
123 else if (SP_IS_SPIRAL(item))
124 object_verb = SP_VERB_CONTEXT_SPIRAL;
125 else if (SP_IS_OFFSET(item)) {
126 if (SP_OFFSET(item)->sourceHref)
127 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
128 else
129 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
130 }
132 // for drag, this is done by ungrabbed_handler, but for click we must do it here
133 sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb,
134 _("Change handle"));
135 }
137 void
138 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
139 {
140 if (this->dragging == false) {
141 this->dragging = true;
142 }
144 // this was a local change and the knotholder does not need to be recreated:
145 this->local_change = TRUE;
147 for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
148 KnotHolderEntity *e = *i;
149 if (e->knot == knot) {
150 Geom::Point const q = p * sp_item_i2d_affine(item).inverse();
151 e->knot_set(q, e->knot->drag_origin * sp_item_i2d_affine(item).inverse(), state);
152 break;
153 }
154 }
156 if (SP_IS_SHAPE (item)) {
157 sp_shape_set_shape(SP_SHAPE (item));
158 }
160 this->update_knots();
161 }
163 void
164 KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
165 {
166 this->dragging = false;
168 if (this->released) {
169 this->released(this->item);
170 } else {
171 SPObject *object = (SPObject *) this->item;
173 // Caution: this call involves a screen update, which may process events, and as a
174 // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
175 // fields of this knotholder (such as this->item), but only values we have saved beforehand
176 // (such as object).
177 object->updateRepr();
179 /* do cleanup tasks (e.g., for LPE items write the parameter values
180 * that were changed by dragging the handle to SVG)
181 */
182 if (SP_IS_LPE_ITEM(object)) {
183 // This writes all parameters to SVG. Is this sufficiently efficient or should we only
184 // write the ones that were changed?
186 Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
187 if (lpe) {
188 LivePathEffectObject *lpeobj = lpe->getLPEObj();
189 SP_OBJECT(lpeobj)->updateRepr();
190 }
191 }
193 unsigned int object_verb = SP_VERB_NONE;
195 if (SP_IS_RECT(object))
196 object_verb = SP_VERB_CONTEXT_RECT;
197 else if (SP_IS_BOX3D(object))
198 object_verb = SP_VERB_CONTEXT_3DBOX;
199 else if (SP_IS_GENERICELLIPSE(object))
200 object_verb = SP_VERB_CONTEXT_ARC;
201 else if (SP_IS_STAR(object))
202 object_verb = SP_VERB_CONTEXT_STAR;
203 else if (SP_IS_SPIRAL(object))
204 object_verb = SP_VERB_CONTEXT_SPIRAL;
205 else if (SP_IS_OFFSET(object)) {
206 if (SP_OFFSET(object)->sourceHref)
207 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
208 else
209 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
210 }
212 sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
213 _("Move handle"));
214 }
215 }
217 void
218 KnotHolder::add(KnotHolderEntity *e)
219 {
220 entity.push_back(e);
221 }
223 void
224 KnotHolder::add_pattern_knotholder()
225 {
226 if ((SP_OBJECT(item)->style->fill.isPaintserver())
227 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
228 {
229 PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
230 PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
231 PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
232 entity_xy->create(desktop, item, this,
233 // TRANSLATORS: This refers to the pattern that's inside the object
234 _("<b>Move</b> the pattern fill inside the object"),
235 SP_KNOT_SHAPE_CROSS);
236 entity_scale->create(desktop, item, this,
237 _("<b>Scale</b> the pattern fill uniformly"),
238 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
239 entity_angle->create(desktop, item, this,
240 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
241 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
242 entity.push_back(entity_xy);
243 entity.push_back(entity_angle);
244 entity.push_back(entity_scale);
245 }
246 }
248 /*
249 Local Variables:
250 mode:c++
251 c-file-style:"stroustrup"
252 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
253 indent-tabs-mode:nil
254 fill-column:99
255 End:
256 */
257 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :