Code

0a3cb295769138d99112eba3a52f891570c20c60
[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         delete *i;
62     }
63     entity.clear(); // this shouldn't be necessary, though
64 }
66 /**
67  * \param p In desktop coordinates.
68  */
70 void
71 KnotHolder::update_knots()
72 {
73     NR::Matrix const i2d(sp_item_i2d_affine(item));
75     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
76         KnotHolderEntity *e = *i;
77         e->update_knot();
78     }
79 }
81 void
82 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
83 {
84     KnotHolder *knot_holder = this;
86     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
87         KnotHolderEntity *e = *i;
88         if (e->knot == knot) {
89             // no need to test whether knot_click exists since it's virtual now
90             e->knot_click(state);
91             break;
92         }
93     }
95     if (SP_IS_SHAPE(item)) {
96         sp_shape_set_shape(SP_SHAPE(item));
97     }
99     knot_holder->update_knots();
101     unsigned int object_verb = SP_VERB_NONE;
103     if (SP_IS_RECT(item))
104         object_verb = SP_VERB_CONTEXT_RECT;
105     else if (SP_IS_BOX3D(item))
106         object_verb = SP_VERB_CONTEXT_3DBOX;
107     else if (SP_IS_GENERICELLIPSE(item))
108         object_verb = SP_VERB_CONTEXT_ARC;
109     else if (SP_IS_STAR(item))
110         object_verb = SP_VERB_CONTEXT_STAR;
111     else if (SP_IS_SPIRAL(item))
112         object_verb = SP_VERB_CONTEXT_SPIRAL;
113     else if (SP_IS_OFFSET(item)) {
114         if (SP_OFFSET(item)->sourceHref)
115             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
116         else
117             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
118     }
120     // for drag, this is done by ungrabbed_handler, but for click we must do it here
121     sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb, 
122                      _("Change handle"));
125 void
126 KnotHolder::knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state)
128     // this was a local change and the knotholder does not need to be recreated:
129     this->local_change = TRUE;
131     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
132         KnotHolderEntity *e = *i;
133         if (e->knot == knot) {
134             NR::Point const q = *p / sp_item_i2d_affine(item);
135             e->knot_set(q, e->knot->drag_origin / sp_item_i2d_affine(item), state);
136             break;
137         }
138     }
140     if (SP_IS_SHAPE (item)) {
141         sp_shape_set_shape(SP_SHAPE (item));
142     }
144     this->update_knots();
147 void
148 KnotHolder::knot_ungrabbed_handler()
150     if (this->released) {
151         this->released(this->item);
152     } else {
153         SPObject *object = (SPObject *) this->item;
154         object->updateRepr(object->repr, SP_OBJECT_WRITE_EXT);
156         unsigned int object_verb = SP_VERB_NONE;
158         if (SP_IS_RECT(object))
159             object_verb = SP_VERB_CONTEXT_RECT;
160         else if (SP_IS_BOX3D(object))
161             object_verb = SP_VERB_CONTEXT_3DBOX;
162         else if (SP_IS_GENERICELLIPSE(object))
163             object_verb = SP_VERB_CONTEXT_ARC;
164         else if (SP_IS_STAR(object))
165             object_verb = SP_VERB_CONTEXT_STAR;
166         else if (SP_IS_SPIRAL(object))
167             object_verb = SP_VERB_CONTEXT_SPIRAL;
168         else if (SP_IS_OFFSET(object)) {
169             if (SP_OFFSET(object)->sourceHref)
170                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
171             else
172                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
173         }
174         
175         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
176                          _("Move handle"));
177     }
180 void
181 KnotHolder::add_pattern_knotholder()
183     if ((SP_OBJECT(item)->style->fill.isPaintserver())
184         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
185     {
186         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
187         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
188         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
189         entity_xy->create(desktop, item, this,
190                           // TRANSLATORS: This refers to the pattern that's inside the object
191                           _("<b>Move</b> the pattern fill inside the object"),
192                           SP_KNOT_SHAPE_CROSS);
193         entity_angle->create(desktop, item, this,
194                              _("<b>Scale</b> the pattern fill uniformly"),
195                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
196         entity_scale->create(desktop, item, this,
197                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
198                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
199         entity.push_back(entity_xy);
200         entity.push_back(entity_angle);
201         entity.push_back(entity_scale);
202     }
205 /*
206   Local Variables:
207   mode:c++
208   c-file-style:"stroustrup"
209   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
210   indent-tabs-mode:nil
211   fill-column:99
212   End:
213 */
214 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :