Code

- Move snap delay mechanism to the event context (used to be in SPCanvas)
[inkscape.git] / src / knotholder.cpp
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"));
137 void
138 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
140     if (this->dragging == false) {
141         this->dragging = true;
142         //sp_event_context_snap_window_open(desktop->canvas);
143     }
145         // this was a local change and the knotholder does not need to be recreated:
146     this->local_change = TRUE;
148     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
149         KnotHolderEntity *e = *i;
150         if (e->knot == knot) {
151             Geom::Point const q = p * sp_item_i2d_affine(item).inverse();
152             e->knot_set(q, e->knot->drag_origin * sp_item_i2d_affine(item).inverse(), state);
153             break;
154         }
155     }
157     if (SP_IS_SHAPE (item)) {
158         sp_shape_set_shape(SP_SHAPE (item));
159     }
161     this->update_knots();
164 void
165 KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
167         this->dragging = false;
168         //sp_event_context_snap_window_closed(desktop->canvas);
170         if (this->released) {
171         this->released(this->item);
172     } else {
173         SPObject *object = (SPObject *) this->item;
175         // Caution: this call involves a screen update, which may process events, and as a
176         // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
177         // fields of this knotholder (such as this->item), but only values we have saved beforehand
178         // (such as object).
179         object->updateRepr();
181         /* do cleanup tasks (e.g., for LPE items write the parameter values
182          * that were changed by dragging the handle to SVG)
183          */
184         if (SP_IS_LPE_ITEM(object)) {
185             // This writes all parameters to SVG. Is this sufficiently efficient or should we only
186             // write the ones that were changed?
188             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
189             if (lpe) {
190                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
191                 SP_OBJECT(lpeobj)->updateRepr();
192             }
193         }
195         unsigned int object_verb = SP_VERB_NONE;
197         if (SP_IS_RECT(object))
198             object_verb = SP_VERB_CONTEXT_RECT;
199         else if (SP_IS_BOX3D(object))
200             object_verb = SP_VERB_CONTEXT_3DBOX;
201         else if (SP_IS_GENERICELLIPSE(object))
202             object_verb = SP_VERB_CONTEXT_ARC;
203         else if (SP_IS_STAR(object))
204             object_verb = SP_VERB_CONTEXT_STAR;
205         else if (SP_IS_SPIRAL(object))
206             object_verb = SP_VERB_CONTEXT_SPIRAL;
207         else if (SP_IS_OFFSET(object)) {
208             if (SP_OFFSET(object)->sourceHref)
209                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
210             else
211                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
212         }
214         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
215                          _("Move handle"));
216     }
219 void
220 KnotHolder::add(KnotHolderEntity *e)
222     entity.push_back(e);
225 void
226 KnotHolder::add_pattern_knotholder()
228     if ((SP_OBJECT(item)->style->fill.isPaintserver())
229         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
230     {
231         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
232         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
233         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
234         entity_xy->create(desktop, item, this,
235                           // TRANSLATORS: This refers to the pattern that's inside the object
236                           _("<b>Move</b> the pattern fill inside the object"),
237                           SP_KNOT_SHAPE_CROSS);
238         entity_scale->create(desktop, item, this,
239                              _("<b>Scale</b> the pattern fill uniformly"),
240                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
241         entity_angle->create(desktop, item, this,
242                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
243                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
244         entity.push_back(entity_xy);
245         entity.push_back(entity_angle);
246         entity.push_back(entity_scale);
247     }
250 /*
251   Local Variables:
252   mode:c++
253   c-file-style:"stroustrup"
254   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
255   indent-tabs-mode:nil
256   fill-column:99
257   End:
258 */
259 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :