Code

work a bit on snapindicator: added switch statement for different types
[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"
34 #include "xml/repr.h" // for debugging only
36 #include <glibmm/i18n.h>
38 class SPDesktop;
40 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
41 {
42     Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
44     if (!desktop || !item || !SP_IS_ITEM(item)) {
45         g_print ("Error! Throw an exception, please!\n");
46     }
48     this->desktop = desktop;
49     this->item = item;
50     g_object_ref(G_OBJECT(item)); // TODO: is this still needed after C++-ification?
52     this->released = relhandler;
54     this->repr = repr;
55     this->local_change = FALSE;
56 }
58 KnotHolder::~KnotHolder() {
59     g_object_unref(G_OBJECT(item));
60     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
61         KnotHolderEntity* e = (*i);
62         if (e->isDeletable()) {
63             delete (*i);
64         } else {
65             // we must not delete the entity (since it's attached to an LPE parameter),
66             // but the handle should be destroyed
67             g_object_unref(e->knot);
68         }
69         (*i) = NULL;
70     }
71     entity.clear(); // is this necessary?
72 }
74 /**
75  * \param p In desktop coordinates.
76  */
78 void
79 KnotHolder::update_knots()
80 {
81     Geom::Matrix const i2d(sp_item_i2d_affine(item));
83     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
84         KnotHolderEntity *e = *i;
85         e->update_knot();
86     }
87 }
89 void
90 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
91 {
92     KnotHolder *knot_holder = this;
94     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
95         KnotHolderEntity *e = *i;
96         if (e->knot == knot) {
97             // no need to test whether knot_click exists since it's virtual now
98             e->knot_click(state);
99             break;
100         }
101     }
103     if (SP_IS_SHAPE(item)) {
104         sp_shape_set_shape(SP_SHAPE(item));
105     }
107     knot_holder->update_knots();
109     unsigned int object_verb = SP_VERB_NONE;
111     if (SP_IS_RECT(item))
112         object_verb = SP_VERB_CONTEXT_RECT;
113     else if (SP_IS_BOX3D(item))
114         object_verb = SP_VERB_CONTEXT_3DBOX;
115     else if (SP_IS_GENERICELLIPSE(item))
116         object_verb = SP_VERB_CONTEXT_ARC;
117     else if (SP_IS_STAR(item))
118         object_verb = SP_VERB_CONTEXT_STAR;
119     else if (SP_IS_SPIRAL(item))
120         object_verb = SP_VERB_CONTEXT_SPIRAL;
121     else if (SP_IS_OFFSET(item)) {
122         if (SP_OFFSET(item)->sourceHref)
123             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
124         else
125             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
126     }
128     // for drag, this is done by ungrabbed_handler, but for click we must do it here
129     sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb,
130                      _("Change handle"));
133 void
134 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
136     // this was a local change and the knotholder does not need to be recreated:
137     this->local_change = TRUE;
139     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
140         KnotHolderEntity *e = *i;
141         if (e->knot == knot) {
142             Geom::Point const q = p * sp_item_i2d_affine(item).inverse();
143             e->knot_set(q, e->knot->drag_origin * sp_item_i2d_affine(item).inverse(), state);
144             break;
145         }
146     }
148     if (SP_IS_SHAPE (item)) {
149         sp_shape_set_shape(SP_SHAPE (item));
150     }
152     this->update_knots();
155 void
156 KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
158     if (this->released) {
159         this->released(this->item);
160     } else {
161         SPObject *object = (SPObject *) this->item;
163         // Caution: this call involves a screen update, which may process events, and as a
164         // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
165         // fields of this knotholder (such as this->item), but only values we have saved beforehand
166         // (such as object).
167         object->updateRepr();
169         /* do cleanup tasks (e.g., for LPE items write the parameter values
170          * that were changed by dragging the handle to SVG)
171          */
172         if (SP_IS_LPE_ITEM(object)) {
173             // This writes all parameters to SVG. Is this sufficiently efficient or should we only
174             // write the ones that were changed?
176             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
177             if (lpe) {
178                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
179                 SP_OBJECT(lpeobj)->updateRepr();
180             }
181         }
183         unsigned int object_verb = SP_VERB_NONE;
185         if (SP_IS_RECT(object))
186             object_verb = SP_VERB_CONTEXT_RECT;
187         else if (SP_IS_BOX3D(object))
188             object_verb = SP_VERB_CONTEXT_3DBOX;
189         else if (SP_IS_GENERICELLIPSE(object))
190             object_verb = SP_VERB_CONTEXT_ARC;
191         else if (SP_IS_STAR(object))
192             object_verb = SP_VERB_CONTEXT_STAR;
193         else if (SP_IS_SPIRAL(object))
194             object_verb = SP_VERB_CONTEXT_SPIRAL;
195         else if (SP_IS_OFFSET(object)) {
196             if (SP_OFFSET(object)->sourceHref)
197                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
198             else
199                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
200         }
202         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
203                          _("Move handle"));
204     }
207 void
208 KnotHolder::add(KnotHolderEntity *e)
210     entity.push_back(e);
213 void
214 KnotHolder::add_pattern_knotholder()
216     if ((SP_OBJECT(item)->style->fill.isPaintserver())
217         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
218     {
219         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
220         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
221         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
222         entity_xy->create(desktop, item, this,
223                           // TRANSLATORS: This refers to the pattern that's inside the object
224                           _("<b>Move</b> the pattern fill inside the object"),
225                           SP_KNOT_SHAPE_CROSS);
226         entity_scale->create(desktop, item, this,
227                              _("<b>Scale</b> the pattern fill uniformly"),
228                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
229         entity_angle->create(desktop, item, this,
230                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
231                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
232         entity.push_back(entity_xy);
233         entity.push_back(entity_angle);
234         entity.push_back(entity_scale);
235     }
238 /*
239   Local Variables:
240   mode:c++
241   c-file-style:"stroustrup"
242   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
243   indent-tabs-mode:nil
244   fill-column:99
245   End:
246 */
247 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :