Code

1ca280d48c0b85db91f0cc52b1909264d3082dc3
[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"
33 #include "xml/repr.h" // for debugging only
35 #include <libnr/nr-matrix-div.h>
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->isLPEParam()) {
63             // knotholder entity may be deleted
64             delete (*i);
65         } else {
66             // we must not delete the entity since it's an LPE parameter,
67             // but the handle should be destroyed
68             g_object_unref(e->knot);
69         }
70         (*i) = NULL;
71     }
72     entity.clear(); // this shouldn't be necessary, though
73 }
75 /**
76  * \param p In desktop coordinates.
77  */
79 void
80 KnotHolder::update_knots()
81 {
82     NR::Matrix const i2d(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 / sp_item_i2d_affine(item);
144             e->knot_set(q, e->knot->drag_origin / 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;
163         object->updateRepr(object->repr, SP_OBJECT_WRITE_EXT);
165         /* do cleanup tasks (e.g., for LPE items write the parameter values
166          * that were changed by dragging the handle to SVG)
167          */
168         for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
169             KnotHolderEntity *e = *i;
170             if (e->knot == knot) {
171                 e->onKnotUngrabbed(); // for most KnotHolderEntitys this does nothing
172             }
173         }
175         unsigned int object_verb = SP_VERB_NONE;
177         if (SP_IS_RECT(object))
178             object_verb = SP_VERB_CONTEXT_RECT;
179         else if (SP_IS_BOX3D(object))
180             object_verb = SP_VERB_CONTEXT_3DBOX;
181         else if (SP_IS_GENERICELLIPSE(object))
182             object_verb = SP_VERB_CONTEXT_ARC;
183         else if (SP_IS_STAR(object))
184             object_verb = SP_VERB_CONTEXT_STAR;
185         else if (SP_IS_SPIRAL(object))
186             object_verb = SP_VERB_CONTEXT_SPIRAL;
187         else if (SP_IS_OFFSET(object)) {
188             if (SP_OFFSET(object)->sourceHref)
189                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
190             else
191                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
192         }
193         
194         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
195                          _("Move handle"));
196     }
199 void
200 KnotHolder::add(KnotHolderEntity *e)
202     entity.push_back(e);
205 void
206 KnotHolder::add_pattern_knotholder()
208     if ((SP_OBJECT(item)->style->fill.isPaintserver())
209         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
210     {
211         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
212         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
213         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
214         entity_xy->create(desktop, item, this,
215                           // TRANSLATORS: This refers to the pattern that's inside the object
216                           _("<b>Move</b> the pattern fill inside the object"),
217                           SP_KNOT_SHAPE_CROSS);
218         entity_angle->create(desktop, item, this,
219                              _("<b>Scale</b> the pattern fill uniformly"),
220                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
221         entity_scale->create(desktop, item, this,
222                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
223                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
224         entity.push_back(entity_xy);
225         entity.push_back(entity_angle);
226         entity.push_back(entity_scale);
227     }
230 /*
231   Local Variables:
232   mode:c++
233   c-file-style:"stroustrup"
234   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
235   indent-tabs-mode:nil
236   fill-column:99
237   End:
238 */
239 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :