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"
34 #include "xml/repr.h" // for debugging only
36 #include <libnr/nr-matrix-div.h>
37 #include <glibmm/i18n.h>
39 class SPDesktop;
41 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
42 {
43 Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
45 if (!desktop || !item || !SP_IS_ITEM(item)) {
46 g_print ("Error! Throw an exception, please!\n");
47 }
49 this->desktop = desktop;
50 this->item = item;
51 g_object_ref(G_OBJECT(item)); // TODO: is this still needed after C++-ification?
53 this->released = relhandler;
55 this->repr = repr;
56 this->local_change = FALSE;
57 }
59 KnotHolder::~KnotHolder() {
60 g_object_unref(G_OBJECT(item));
61 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
62 KnotHolderEntity* e = (*i);
63 if (!e->isLPEParam()) {
64 // knotholder entity may be deleted
65 delete (*i);
66 } else {
67 // we must not delete the entity since it's an LPE parameter,
68 // but the handle should be destroyed
69 g_object_unref(e->knot);
70 }
71 (*i) = NULL;
72 }
73 entity.clear(); // this shouldn't be necessary, though
74 }
76 /**
77 * \param p In desktop coordinates.
78 */
80 void
81 KnotHolder::update_knots()
82 {
83 NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
85 for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
86 KnotHolderEntity *e = *i;
87 e->update_knot();
88 }
89 }
91 void
92 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
93 {
94 KnotHolder *knot_holder = this;
96 for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
97 KnotHolderEntity *e = *i;
98 if (e->knot == knot) {
99 // no need to test whether knot_click exists since it's virtual now
100 e->knot_click(state);
101 break;
102 }
103 }
105 if (SP_IS_SHAPE(item)) {
106 sp_shape_set_shape(SP_SHAPE(item));
107 }
109 knot_holder->update_knots();
111 unsigned int object_verb = SP_VERB_NONE;
113 if (SP_IS_RECT(item))
114 object_verb = SP_VERB_CONTEXT_RECT;
115 else if (SP_IS_BOX3D(item))
116 object_verb = SP_VERB_CONTEXT_3DBOX;
117 else if (SP_IS_GENERICELLIPSE(item))
118 object_verb = SP_VERB_CONTEXT_ARC;
119 else if (SP_IS_STAR(item))
120 object_verb = SP_VERB_CONTEXT_STAR;
121 else if (SP_IS_SPIRAL(item))
122 object_verb = SP_VERB_CONTEXT_SPIRAL;
123 else if (SP_IS_OFFSET(item)) {
124 if (SP_OFFSET(item)->sourceHref)
125 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
126 else
127 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
128 }
130 // for drag, this is done by ungrabbed_handler, but for click we must do it here
131 sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb,
132 _("Change handle"));
133 }
135 void
136 KnotHolder::knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state)
137 {
138 // this was a local change and the knotholder does not need to be recreated:
139 this->local_change = TRUE;
141 for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
142 KnotHolderEntity *e = *i;
143 if (e->knot == knot) {
144 NR::Point const q = *p / from_2geom(sp_item_i2d_affine(item));
145 e->knot_set(q, e->knot->drag_origin / from_2geom(sp_item_i2d_affine(item)), state);
146 break;
147 }
148 }
150 if (SP_IS_SHAPE (item)) {
151 sp_shape_set_shape(SP_SHAPE (item));
152 }
154 this->update_knots();
155 }
157 void
158 KnotHolder::knot_ungrabbed_handler(SPKnot *knot)
159 {
160 if (this->released) {
161 this->released(this->item);
162 } else {
163 SPObject *object = (SPObject *) this->item;
164 object->updateRepr();
166 /* do cleanup tasks (e.g., for LPE items write the parameter values
167 * that were changed by dragging the handle to SVG)
168 */
169 if (SP_IS_LPE_ITEM(item)) {
170 // This writes all parameters to SVG. Is this sufficiently efficient or should we only write
171 // the ones that were changed (e.g., via the individual handles' onKnotUngrabbed() method?
172 Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
173 if (lpe) {
174 LivePathEffectObject *lpeobj = lpe->getLPEObj();
175 SP_OBJECT(lpeobj)->updateRepr();
176 }
177 }
178 // this was once used to write individual parameter values to SVG but this is now done globally above;
179 // we leave the calls to onKnotUngrabbed, anyway, in case any other cleanup tasks need to be done
180 for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
181 KnotHolderEntity *e = *i;
182 if (e->knot == knot) {
183 e->onKnotUngrabbed(); // for most KnotHolderEntitys this does nothing
184 }
185 }
187 unsigned int object_verb = SP_VERB_NONE;
189 if (SP_IS_RECT(object))
190 object_verb = SP_VERB_CONTEXT_RECT;
191 else if (SP_IS_BOX3D(object))
192 object_verb = SP_VERB_CONTEXT_3DBOX;
193 else if (SP_IS_GENERICELLIPSE(object))
194 object_verb = SP_VERB_CONTEXT_ARC;
195 else if (SP_IS_STAR(object))
196 object_verb = SP_VERB_CONTEXT_STAR;
197 else if (SP_IS_SPIRAL(object))
198 object_verb = SP_VERB_CONTEXT_SPIRAL;
199 else if (SP_IS_OFFSET(object)) {
200 if (SP_OFFSET(object)->sourceHref)
201 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
202 else
203 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
204 }
206 sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
207 _("Move handle"));
208 }
209 }
211 void
212 KnotHolder::add(KnotHolderEntity *e)
213 {
214 entity.push_back(e);
215 }
217 void
218 KnotHolder::add_pattern_knotholder()
219 {
220 if ((SP_OBJECT(item)->style->fill.isPaintserver())
221 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
222 {
223 PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
224 PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
225 PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
226 entity_xy->create(desktop, item, this,
227 // TRANSLATORS: This refers to the pattern that's inside the object
228 _("<b>Move</b> the pattern fill inside the object"),
229 SP_KNOT_SHAPE_CROSS);
230 entity_angle->create(desktop, item, this,
231 _("<b>Scale</b> the pattern fill uniformly"),
232 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
233 entity_scale->create(desktop, item, this,
234 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
235 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
236 entity.push_back(entity_xy);
237 entity.push_back(entity_angle);
238 entity.push_back(entity_scale);
239 }
240 }
242 /*
243 Local Variables:
244 mode:c++
245 c-file-style:"stroustrup"
246 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
247 indent-tabs-mode:nil
248 fill-column:99
249 End:
250 */
251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :