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 "live_effects/effect.h"
34 #include "desktop.h"
35 #include "display/sp-canvas.h"
37 #include "xml/repr.h" // for debugging only
39 #include <glibmm/i18n.h>
41 class SPDesktop;
43 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
44 {
45 Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
47 if (!desktop || !item || !SP_IS_ITEM(item)) {
48 g_print ("Error! Throw an exception, please!\n");
49 }
51 this->desktop = desktop;
52 this->item = item;
53 g_object_ref(G_OBJECT(item)); // TODO: is this still needed after C++-ification?
55 this->released = relhandler;
57 this->repr = repr;
58 this->local_change = FALSE;
60 this->dragging = false;
61 }
63 KnotHolder::~KnotHolder() {
64 g_object_unref(G_OBJECT(item));
65 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
66 KnotHolderEntity* e = (*i);
67 if (e->isDeletable()) {
68 delete (*i);
69 } else {
70 // we must not delete the entity (since it's attached to an LPE parameter),
71 // but the handle should be destroyed
72 g_object_unref(e->knot);
73 }
74 (*i) = NULL;
75 }
76 entity.clear(); // is this necessary?
77 }
79 /**
80 * \param p In desktop coordinates.
81 */
83 void
84 KnotHolder::update_knots()
85 {
86 Geom::Matrix const i2d(sp_item_i2d_affine(item));
88 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
89 KnotHolderEntity *e = *i;
90 e->update_knot();
91 }
92 }
94 /**
95 * \brief Returns true if at least one of the KnotHolderEntities has the mouse hovering above it
96 */
97 bool KnotHolder::knot_mouseover()
98 {
99 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
100 SPKnot *knot = (*i)->knot;
101 if (knot && (knot->flags & SP_KNOT_MOUSEOVER)) {
102 return true;
103 }
104 }
106 return false;
107 }
109 void
110 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
111 {
112 KnotHolder *knot_holder = this;
114 for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
115 KnotHolderEntity *e = *i;
116 if (e->knot == knot) {
117 // no need to test whether knot_click exists since it's virtual now
118 e->knot_click(state);
119 break;
120 }
121 }
123 if (SP_IS_SHAPE(item)) {
124 sp_shape_set_shape(SP_SHAPE(item));
125 }
127 knot_holder->update_knots();
129 unsigned int object_verb = SP_VERB_NONE;
131 if (SP_IS_RECT(item))
132 object_verb = SP_VERB_CONTEXT_RECT;
133 else if (SP_IS_BOX3D(item))
134 object_verb = SP_VERB_CONTEXT_3DBOX;
135 else if (SP_IS_GENERICELLIPSE(item))
136 object_verb = SP_VERB_CONTEXT_ARC;
137 else if (SP_IS_STAR(item))
138 object_verb = SP_VERB_CONTEXT_STAR;
139 else if (SP_IS_SPIRAL(item))
140 object_verb = SP_VERB_CONTEXT_SPIRAL;
141 else if (SP_IS_OFFSET(item)) {
142 if (SP_OFFSET(item)->sourceHref)
143 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
144 else
145 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
146 }
148 // for drag, this is done by ungrabbed_handler, but for click we must do it here
149 sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb,
150 _("Change handle"));
151 }
153 void
154 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
155 {
156 if (this->dragging == false) {
157 this->dragging = true;
158 }
160 // this was a local change and the knotholder does not need to be recreated:
161 this->local_change = TRUE;
163 for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
164 KnotHolderEntity *e = *i;
165 if (e->knot == knot) {
166 Geom::Point const q = p * sp_item_i2d_affine(item).inverse();
167 e->knot_set(q, e->knot->drag_origin * sp_item_i2d_affine(item).inverse(), state);
168 break;
169 }
170 }
172 if (SP_IS_SHAPE (item)) {
173 sp_shape_set_shape(SP_SHAPE (item));
174 }
176 this->update_knots();
177 }
179 void
180 KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
181 {
182 this->dragging = false;
184 if (this->released) {
185 this->released(this->item);
186 } else {
187 SPObject *object = (SPObject *) this->item;
189 // Caution: this call involves a screen update, which may process events, and as a
190 // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
191 // fields of this knotholder (such as this->item), but only values we have saved beforehand
192 // (such as object).
193 object->updateRepr();
195 /* do cleanup tasks (e.g., for LPE items write the parameter values
196 * that were changed by dragging the handle to SVG)
197 */
198 if (SP_IS_LPE_ITEM(object)) {
199 // This writes all parameters to SVG. Is this sufficiently efficient or should we only
200 // write the ones that were changed?
202 Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
203 if (lpe) {
204 LivePathEffectObject *lpeobj = lpe->getLPEObj();
205 SP_OBJECT(lpeobj)->updateRepr();
206 }
207 }
209 unsigned int object_verb = SP_VERB_NONE;
211 if (SP_IS_RECT(object))
212 object_verb = SP_VERB_CONTEXT_RECT;
213 else if (SP_IS_BOX3D(object))
214 object_verb = SP_VERB_CONTEXT_3DBOX;
215 else if (SP_IS_GENERICELLIPSE(object))
216 object_verb = SP_VERB_CONTEXT_ARC;
217 else if (SP_IS_STAR(object))
218 object_verb = SP_VERB_CONTEXT_STAR;
219 else if (SP_IS_SPIRAL(object))
220 object_verb = SP_VERB_CONTEXT_SPIRAL;
221 else if (SP_IS_OFFSET(object)) {
222 if (SP_OFFSET(object)->sourceHref)
223 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
224 else
225 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
226 }
228 sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
229 _("Move handle"));
230 }
231 }
233 void
234 KnotHolder::add(KnotHolderEntity *e)
235 {
236 entity.push_back(e);
237 }
239 void
240 KnotHolder::add_pattern_knotholder()
241 {
242 if ((SP_OBJECT(item)->style->fill.isPaintserver())
243 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
244 {
245 PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
246 PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
247 PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
248 entity_xy->create(desktop, item, this,
249 // TRANSLATORS: This refers to the pattern that's inside the object
250 _("<b>Move</b> the pattern fill inside the object"),
251 SP_KNOT_SHAPE_CROSS);
252 entity_scale->create(desktop, item, this,
253 _("<b>Scale</b> the pattern fill; uniformly if with <b>Ctrl</b>"),
254 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
255 entity_angle->create(desktop, item, this,
256 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
257 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
258 entity.push_back(entity_xy);
259 entity.push_back(entity_angle);
260 entity.push_back(entity_scale);
261 }
262 }
264 /*
265 Local Variables:
266 mode:c++
267 c-file-style:"stroustrup"
268 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
269 indent-tabs-mode:nil
270 fill-column:99
271 End:
272 */
273 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :