Code

Rename isLPEParam() to isDeletable(), create new class LPEKnotHolderEntity and make...
[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 <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->isDeletable()) {
64             delete (*i);
65         } else {
66             // we must not delete the entity (since it's attached to an LPE parameter),
67             // but the handle should be destroyed
68             g_object_unref(e->knot);
69         }
70         (*i) = NULL;
71     }
72     entity.clear(); // is this necessary?
73 }
75 /**
76  * \param p In desktop coordinates.
77  */
79 void
80 KnotHolder::update_knots()
81 {
82     NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
84     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
85         KnotHolderEntity *e = *i;
86         e->update_knot();
87     }
88 }
90 void
91 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
92 {
93     KnotHolder *knot_holder = this;
95     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
96         KnotHolderEntity *e = *i;
97         if (e->knot == knot) {
98             // no need to test whether knot_click exists since it's virtual now
99             e->knot_click(state);
100             break;
101         }
102     }
104     if (SP_IS_SHAPE(item)) {
105         sp_shape_set_shape(SP_SHAPE(item));
106     }
108     knot_holder->update_knots();
110     unsigned int object_verb = SP_VERB_NONE;
112     if (SP_IS_RECT(item))
113         object_verb = SP_VERB_CONTEXT_RECT;
114     else if (SP_IS_BOX3D(item))
115         object_verb = SP_VERB_CONTEXT_3DBOX;
116     else if (SP_IS_GENERICELLIPSE(item))
117         object_verb = SP_VERB_CONTEXT_ARC;
118     else if (SP_IS_STAR(item))
119         object_verb = SP_VERB_CONTEXT_STAR;
120     else if (SP_IS_SPIRAL(item))
121         object_verb = SP_VERB_CONTEXT_SPIRAL;
122     else if (SP_IS_OFFSET(item)) {
123         if (SP_OFFSET(item)->sourceHref)
124             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
125         else
126             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
127     }
129     // for drag, this is done by ungrabbed_handler, but for click we must do it here
130     sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb, 
131                      _("Change handle"));
134 void
135 KnotHolder::knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state)
137     // this was a local change and the knotholder does not need to be recreated:
138     this->local_change = TRUE;
140     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
141         KnotHolderEntity *e = *i;
142         if (e->knot == knot) {
143             NR::Point const q = *p / from_2geom(sp_item_i2d_affine(item));
144             e->knot_set(q, e->knot->drag_origin / from_2geom(sp_item_i2d_affine(item)), state);
145             break;
146         }
147     }
149     if (SP_IS_SHAPE (item)) {
150         sp_shape_set_shape(SP_SHAPE (item));
151     }
153     this->update_knots();
156 void
157 KnotHolder::knot_ungrabbed_handler(SPKnot *knot)
159     if (this->released) {
160         this->released(this->item);
161     } else {
162         SPObject *object = (SPObject *) this->item;
164         // Caution: this call involves a screen update, which may process events, and as a
165         // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
166         // fields of this knotholder (such as this->item), but only values we have saved beforehand
167         // (such as object).
168         object->updateRepr();
170         /* do cleanup tasks (e.g., for LPE items write the parameter values
171          * that were changed by dragging the handle to SVG)
172          */
173         if (SP_IS_LPE_ITEM(object)) {
174             // This writes all parameters to SVG. Is this sufficiently efficient or should we only
175             // write the ones that were changed?
177             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
178             if (lpe) {
179                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
180                 SP_OBJECT(lpeobj)->updateRepr();
181             }
182         }
184         unsigned int object_verb = SP_VERB_NONE;
186         if (SP_IS_RECT(object))
187             object_verb = SP_VERB_CONTEXT_RECT;
188         else if (SP_IS_BOX3D(object))
189             object_verb = SP_VERB_CONTEXT_3DBOX;
190         else if (SP_IS_GENERICELLIPSE(object))
191             object_verb = SP_VERB_CONTEXT_ARC;
192         else if (SP_IS_STAR(object))
193             object_verb = SP_VERB_CONTEXT_STAR;
194         else if (SP_IS_SPIRAL(object))
195             object_verb = SP_VERB_CONTEXT_SPIRAL;
196         else if (SP_IS_OFFSET(object)) {
197             if (SP_OFFSET(object)->sourceHref)
198                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
199             else
200                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
201         }
202         
203         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
204                          _("Move handle"));
205     }
208 void
209 KnotHolder::add(KnotHolderEntity *e)
211     entity.push_back(e);
214 void
215 KnotHolder::add_pattern_knotholder()
217     if ((SP_OBJECT(item)->style->fill.isPaintserver())
218         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
219     {
220         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
221         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
222         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
223         entity_xy->create(desktop, item, this,
224                           // TRANSLATORS: This refers to the pattern that's inside the object
225                           _("<b>Move</b> the pattern fill inside the object"),
226                           SP_KNOT_SHAPE_CROSS);
227         entity_scale->create(desktop, item, this,
228                              _("<b>Scale</b> the pattern fill uniformly"),
229                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
230         entity_angle->create(desktop, item, this,
231                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
232                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
233         entity.push_back(entity_xy);
234         entity.push_back(entity_angle);
235         entity.push_back(entity_scale);
236     }
239 /*
240   Local Variables:
241   mode:c++
242   c-file-style:"stroustrup"
243   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
244   indent-tabs-mode:nil
245   fill-column:99
246   End:
247 */
248 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :