Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / knotholder.cpp
1 /*
2  * Container for SPKnot visual handles
3  *
4  * Authors:
5  *   Mitsuru Oka <oka326@parkcity.ne.jp>
6  *   bulia byak <buliabyak@users.sf.net>
7  *   Maximilian Albert <maximilian.albert@gmail.com>
8  *   Abhishek Sharma
9  *
10  * Copyright (C) 2001-2008 authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include <glibmm/i18n.h>
17 #include "document.h"
18 #include "sp-shape.h"
19 #include "knot.h"
20 #include "knotholder.h"
21 #include "rect-context.h"
22 #include "sp-rect.h"
23 #include "arc-context.h"
24 #include "sp-ellipse.h"
25 #include "star-context.h"
26 #include "sp-star.h"
27 #include "spiral-context.h"
28 #include "sp-spiral.h"
29 #include "sp-offset.h"
30 #include "box3d.h"
31 #include "sp-pattern.h"
32 #include "style.h"
33 #include "live_effects/lpeobject.h"
34 #include "live_effects/effect.h"
35 #include "desktop.h"
36 #include "display/sp-canvas.h"
38 #include "xml/repr.h" // for debugging only
40 using Inkscape::DocumentUndo;
42 class SPDesktop;
44 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
45 {
46     //XML Tree being used directly here while it shouldn't be...
47     Inkscape::XML::Node *repr = SP_OBJECT(item)->getRepr();
49     if (!desktop || !item || !SP_IS_ITEM(item)) {
50         g_print ("Error! Throw an exception, please!\n");
51     }
53     this->desktop = desktop;
54     this->item = item;
55     g_object_ref(G_OBJECT(item)); // TODO: is this still needed after C++-ification?
57     this->released = relhandler;
59     this->repr = repr;
60     this->local_change = FALSE;
62     this->dragging = false;
63 }
65 KnotHolder::~KnotHolder() {
66     g_object_unref(G_OBJECT(item));
67     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
68         KnotHolderEntity* e = (*i);
69         if (e->isDeletable()) {
70             delete (*i);
71         } else {
72             // we must not delete the entity (since it's attached to an LPE parameter),
73             // but the handle should be destroyed
74             g_object_unref(e->knot);
75         }
76         (*i) = NULL;
77     }
78     entity.clear(); // is this necessary?
79 }
81 /**
82  * \param p In desktop coordinates.
83  */
85 void
86 KnotHolder::update_knots()
87 {
88     Geom::Matrix const i2d(item->i2d_affine());
90     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
91         KnotHolderEntity *e = *i;
92         e->update_knot();
93     }
94 }
96 /**
97  * \brief Returns true if at least one of the KnotHolderEntities has the mouse hovering above it
98  */
99 bool KnotHolder::knot_mouseover()
101     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
102         SPKnot *knot = (*i)->knot;
103         if (knot && (knot->flags & SP_KNOT_MOUSEOVER)) {
104             return true;
105         }
106     }
108     return false;
111 void
112 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
114         KnotHolder *knot_holder = this;
116     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
117         KnotHolderEntity *e = *i;
118         if (e->knot == knot) {
119             // no need to test whether knot_click exists since it's virtual now
120             e->knot_click(state);
121             break;
122         }
123     }
125     if (SP_IS_SHAPE(item)) {
126         SP_SHAPE(item)->setShape();
127     }
129     knot_holder->update_knots();
131     unsigned int object_verb = SP_VERB_NONE;
133     if (SP_IS_RECT(item))
134         object_verb = SP_VERB_CONTEXT_RECT;
135     else if (SP_IS_BOX3D(item))
136         object_verb = SP_VERB_CONTEXT_3DBOX;
137     else if (SP_IS_GENERICELLIPSE(item))
138         object_verb = SP_VERB_CONTEXT_ARC;
139     else if (SP_IS_STAR(item))
140         object_verb = SP_VERB_CONTEXT_STAR;
141     else if (SP_IS_SPIRAL(item))
142         object_verb = SP_VERB_CONTEXT_SPIRAL;
143     else if (SP_IS_OFFSET(item)) {
144         if (SP_OFFSET(item)->sourceHref)
145             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
146         else
147             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
148     }
150     // for drag, this is done by ungrabbed_handler, but for click we must do it here
151     DocumentUndo::done(SP_OBJECT_DOCUMENT(item), object_verb,
152                        _("Change handle"));
155 void
156 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
158     if (this->dragging == false) {
159         this->dragging = true;
160     }
162         // this was a local change and the knotholder does not need to be recreated:
163     this->local_change = TRUE;
165     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
166         KnotHolderEntity *e = *i;
167         if (e->knot == knot) {
168             Geom::Point const q = p * item->i2d_affine().inverse();
169             e->knot_set(q, e->knot->drag_origin * item->i2d_affine().inverse(), state);
170             break;
171         }
172     }
174     if (SP_IS_SHAPE (item)) {
175         SP_SHAPE (item)->setShape();
176     }
178     this->update_knots();
181 void
182 KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
184         this->dragging = false;
186         if (this->released) {
187         this->released(this->item);
188     } else {
189         SPObject *object = (SPObject *) this->item;
191         // Caution: this call involves a screen update, which may process events, and as a
192         // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
193         // fields of this knotholder (such as this->item), but only values we have saved beforehand
194         // (such as object).
195         object->updateRepr();
197         /* do cleanup tasks (e.g., for LPE items write the parameter values
198          * that were changed by dragging the handle to SVG)
199          */
200         if (SP_IS_LPE_ITEM(object)) {
201             // This writes all parameters to SVG. Is this sufficiently efficient or should we only
202             // write the ones that were changed?
204             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
205             if (lpe) {
206                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
207                 SP_OBJECT(lpeobj)->updateRepr();
208             }
209         }
211         unsigned int object_verb = SP_VERB_NONE;
213         if (SP_IS_RECT(object))
214             object_verb = SP_VERB_CONTEXT_RECT;
215         else if (SP_IS_BOX3D(object))
216             object_verb = SP_VERB_CONTEXT_3DBOX;
217         else if (SP_IS_GENERICELLIPSE(object))
218             object_verb = SP_VERB_CONTEXT_ARC;
219         else if (SP_IS_STAR(object))
220             object_verb = SP_VERB_CONTEXT_STAR;
221         else if (SP_IS_SPIRAL(object))
222             object_verb = SP_VERB_CONTEXT_SPIRAL;
223         else if (SP_IS_OFFSET(object)) {
224             if (SP_OFFSET(object)->sourceHref)
225                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
226             else
227                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
228         }
230         DocumentUndo::done(SP_OBJECT_DOCUMENT (object), object_verb,
231                            _("Move handle"));
232     }
235 void
236 KnotHolder::add(KnotHolderEntity *e)
238     entity.push_back(e);
241 void
242 KnotHolder::add_pattern_knotholder()
244     if ((SP_OBJECT(item)->style->fill.isPaintserver())
245         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
246     {
247         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
248         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
249         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
250         entity_xy->create(desktop, item, this,
251                           // TRANSLATORS: This refers to the pattern that's inside the object
252                           _("<b>Move</b> the pattern fill inside the object"),
253                           SP_KNOT_SHAPE_CROSS);
254         entity_scale->create(desktop, item, this,
255                              _("<b>Scale</b> the pattern fill; uniformly if with <b>Ctrl</b>"),
256                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
257         entity_angle->create(desktop, item, this,
258                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
259                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
260         entity.push_back(entity_xy);
261         entity.push_back(entity_angle);
262         entity.push_back(entity_scale);
263     }
266 /*
267   Local Variables:
268   mode:c++
269   c-file-style:"stroustrup"
270   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
271   indent-tabs-mode:nil
272   fill-column:99
273   End:
274 */
275 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :