Code

Connector tool: make connectors avoid the convex hull of shapes.
[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 "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 void
95 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
96 {
97         KnotHolder *knot_holder = this;
99     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
100         KnotHolderEntity *e = *i;
101         if (e->knot == knot) {
102             // no need to test whether knot_click exists since it's virtual now
103             e->knot_click(state);
104             break;
105         }
106     }
108     if (SP_IS_SHAPE(item)) {
109         sp_shape_set_shape(SP_SHAPE(item));
110     }
112     knot_holder->update_knots();
114     unsigned int object_verb = SP_VERB_NONE;
116     if (SP_IS_RECT(item))
117         object_verb = SP_VERB_CONTEXT_RECT;
118     else if (SP_IS_BOX3D(item))
119         object_verb = SP_VERB_CONTEXT_3DBOX;
120     else if (SP_IS_GENERICELLIPSE(item))
121         object_verb = SP_VERB_CONTEXT_ARC;
122     else if (SP_IS_STAR(item))
123         object_verb = SP_VERB_CONTEXT_STAR;
124     else if (SP_IS_SPIRAL(item))
125         object_verb = SP_VERB_CONTEXT_SPIRAL;
126     else if (SP_IS_OFFSET(item)) {
127         if (SP_OFFSET(item)->sourceHref)
128             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
129         else
130             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
131     }
133     // for drag, this is done by ungrabbed_handler, but for click we must do it here
134     sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb,
135                      _("Change handle"));
138 void
139 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
141     if (this->dragging == false) {
142         this->dragging = true;
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;
169         if (this->released) {
170         this->released(this->item);
171     } else {
172         SPObject *object = (SPObject *) this->item;
174         // Caution: this call involves a screen update, which may process events, and as a
175         // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
176         // fields of this knotholder (such as this->item), but only values we have saved beforehand
177         // (such as object).
178         object->updateRepr();
180         /* do cleanup tasks (e.g., for LPE items write the parameter values
181          * that were changed by dragging the handle to SVG)
182          */
183         if (SP_IS_LPE_ITEM(object)) {
184             // This writes all parameters to SVG. Is this sufficiently efficient or should we only
185             // write the ones that were changed?
187             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
188             if (lpe) {
189                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
190                 SP_OBJECT(lpeobj)->updateRepr();
191             }
192         }
194         unsigned int object_verb = SP_VERB_NONE;
196         if (SP_IS_RECT(object))
197             object_verb = SP_VERB_CONTEXT_RECT;
198         else if (SP_IS_BOX3D(object))
199             object_verb = SP_VERB_CONTEXT_3DBOX;
200         else if (SP_IS_GENERICELLIPSE(object))
201             object_verb = SP_VERB_CONTEXT_ARC;
202         else if (SP_IS_STAR(object))
203             object_verb = SP_VERB_CONTEXT_STAR;
204         else if (SP_IS_SPIRAL(object))
205             object_verb = SP_VERB_CONTEXT_SPIRAL;
206         else if (SP_IS_OFFSET(object)) {
207             if (SP_OFFSET(object)->sourceHref)
208                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
209             else
210                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
211         }
213         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
214                          _("Move handle"));
215     }
218 void
219 KnotHolder::add(KnotHolderEntity *e)
221     entity.push_back(e);
224 void
225 KnotHolder::add_pattern_knotholder()
227     if ((SP_OBJECT(item)->style->fill.isPaintserver())
228         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
229     {
230         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
231         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
232         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
233         entity_xy->create(desktop, item, this,
234                           // TRANSLATORS: This refers to the pattern that's inside the object
235                           _("<b>Move</b> the pattern fill inside the object"),
236                           SP_KNOT_SHAPE_CROSS);
237         entity_scale->create(desktop, item, this,
238                              _("<b>Scale</b> the pattern fill; uniformly if with <b>Ctrl</b>"),
239                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
240         entity_angle->create(desktop, item, this,
241                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
242                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
243         entity.push_back(entity_xy);
244         entity.push_back(entity_angle);
245         entity.push_back(entity_scale);
246     }
249 /*
250   Local Variables:
251   mode:c++
252   c-file-style:"stroustrup"
253   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254   indent-tabs-mode:nil
255   fill-column:99
256   End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :