Code

A simple layout document as to what, why and how is cppification.
[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         //XML Tree being used directly here while it shouldn't be...
46     Inkscape::XML::Node *repr = SP_OBJECT(item)->getRepr();
48     if (!desktop || !item || !SP_IS_ITEM(item)) {
49         g_print ("Error! Throw an exception, please!\n");
50     }
52     this->desktop = desktop;
53     this->item = item;
54     g_object_ref(G_OBJECT(item)); // TODO: is this still needed after C++-ification?
56     this->released = relhandler;
58     this->repr = repr;
59     this->local_change = FALSE;
61     this->dragging = false;
62 }
64 KnotHolder::~KnotHolder() {
65     g_object_unref(G_OBJECT(item));
66     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
67         KnotHolderEntity* e = (*i);
68         if (e->isDeletable()) {
69             delete (*i);
70         } else {
71             // we must not delete the entity (since it's attached to an LPE parameter),
72             // but the handle should be destroyed
73             g_object_unref(e->knot);
74         }
75         (*i) = NULL;
76     }
77     entity.clear(); // is this necessary?
78 }
80 /**
81  * \param p In desktop coordinates.
82  */
84 void
85 KnotHolder::update_knots()
86 {
87     Geom::Matrix const i2d(item->i2d_affine());
89     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
90         KnotHolderEntity *e = *i;
91         e->update_knot();
92     }
93 }
95 /**
96  * \brief Returns true if at least one of the KnotHolderEntities has the mouse hovering above it
97  */
98 bool KnotHolder::knot_mouseover()
99 {
100     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
101         SPKnot *knot = (*i)->knot;
102         if (knot && (knot->flags & SP_KNOT_MOUSEOVER)) {
103             return true;
104         }
105     }
107     return false;
110 void
111 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
113         KnotHolder *knot_holder = this;
115     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
116         KnotHolderEntity *e = *i;
117         if (e->knot == knot) {
118             // no need to test whether knot_click exists since it's virtual now
119             e->knot_click(state);
120             break;
121         }
122     }
124     if (SP_IS_SHAPE(item)) {
125         SP_SHAPE(item)->setShape();
126     }
128     knot_holder->update_knots();
130     unsigned int object_verb = SP_VERB_NONE;
132     if (SP_IS_RECT(item))
133         object_verb = SP_VERB_CONTEXT_RECT;
134     else if (SP_IS_BOX3D(item))
135         object_verb = SP_VERB_CONTEXT_3DBOX;
136     else if (SP_IS_GENERICELLIPSE(item))
137         object_verb = SP_VERB_CONTEXT_ARC;
138     else if (SP_IS_STAR(item))
139         object_verb = SP_VERB_CONTEXT_STAR;
140     else if (SP_IS_SPIRAL(item))
141         object_verb = SP_VERB_CONTEXT_SPIRAL;
142     else if (SP_IS_OFFSET(item)) {
143         if (SP_OFFSET(item)->sourceHref)
144             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
145         else
146             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
147     }
149     // for drag, this is done by ungrabbed_handler, but for click we must do it here
150     SPDocumentUndo::done(SP_OBJECT_DOCUMENT(item), object_verb,
151                      _("Change handle"));
154 void
155 KnotHolder::knot_moved_handler(SPKnot *knot, Geom::Point const &p, guint state)
157     if (this->dragging == false) {
158         this->dragging = true;
159     }
161         // this was a local change and the knotholder does not need to be recreated:
162     this->local_change = TRUE;
164     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
165         KnotHolderEntity *e = *i;
166         if (e->knot == knot) {
167             Geom::Point const q = p * item->i2d_affine().inverse();
168             e->knot_set(q, e->knot->drag_origin * item->i2d_affine().inverse(), state);
169             break;
170         }
171     }
173     if (SP_IS_SHAPE (item)) {
174         SP_SHAPE (item)->setShape();
175     }
177     this->update_knots();
180 void
181 KnotHolder::knot_ungrabbed_handler(SPKnot */*knot*/)
183         this->dragging = false;
185         if (this->released) {
186         this->released(this->item);
187     } else {
188         SPObject *object = (SPObject *) this->item;
190         // Caution: this call involves a screen update, which may process events, and as a
191         // result the knotholder may be destructed. So, after the updateRepr, we cannot use any
192         // fields of this knotholder (such as this->item), but only values we have saved beforehand
193         // (such as object).
194         object->updateRepr();
196         /* do cleanup tasks (e.g., for LPE items write the parameter values
197          * that were changed by dragging the handle to SVG)
198          */
199         if (SP_IS_LPE_ITEM(object)) {
200             // This writes all parameters to SVG. Is this sufficiently efficient or should we only
201             // write the ones that were changed?
203             Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(object));
204             if (lpe) {
205                 LivePathEffectObject *lpeobj = lpe->getLPEObj();
206                 SP_OBJECT(lpeobj)->updateRepr();
207             }
208         }
210         unsigned int object_verb = SP_VERB_NONE;
212         if (SP_IS_RECT(object))
213             object_verb = SP_VERB_CONTEXT_RECT;
214         else if (SP_IS_BOX3D(object))
215             object_verb = SP_VERB_CONTEXT_3DBOX;
216         else if (SP_IS_GENERICELLIPSE(object))
217             object_verb = SP_VERB_CONTEXT_ARC;
218         else if (SP_IS_STAR(object))
219             object_verb = SP_VERB_CONTEXT_STAR;
220         else if (SP_IS_SPIRAL(object))
221             object_verb = SP_VERB_CONTEXT_SPIRAL;
222         else if (SP_IS_OFFSET(object)) {
223             if (SP_OFFSET(object)->sourceHref)
224                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
225             else
226                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
227         }
229         SPDocumentUndo::done(SP_OBJECT_DOCUMENT (object), object_verb,
230                          _("Move handle"));
231     }
234 void
235 KnotHolder::add(KnotHolderEntity *e)
237     entity.push_back(e);
240 void
241 KnotHolder::add_pattern_knotholder()
243     if ((SP_OBJECT(item)->style->fill.isPaintserver())
244         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
245     {
246         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
247         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
248         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
249         entity_xy->create(desktop, item, this,
250                           // TRANSLATORS: This refers to the pattern that's inside the object
251                           _("<b>Move</b> the pattern fill inside the object"),
252                           SP_KNOT_SHAPE_CROSS);
253         entity_scale->create(desktop, item, this,
254                              _("<b>Scale</b> the pattern fill; uniformly if with <b>Ctrl</b>"),
255                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
256         entity_angle->create(desktop, item, this,
257                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
258                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
259         entity.push_back(entity_xy);
260         entity.push_back(entity_angle);
261         entity.push_back(entity_scale);
262     }
265 /*
266   Local Variables:
267   mode:c++
268   c-file-style:"stroustrup"
269   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
270   indent-tabs-mode:nil
271   fill-column:99
272   End:
273 */
274 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :