Code

b589d18c5bf65baadda28edfe2c3f3ec1cbf718c
[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 //static void knot_clicked_handler (SPKnot *knot, guint state, gpointer data);
41 //static void knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state, gpointer data);
42 //static void knot_ungrabbed_handler (SPKnot *knot, unsigned int state, KnotHolder *kh);
44 KnotHolder::KnotHolder(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
45 {
46     Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
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;
60 }
62 KnotHolder::~KnotHolder() {
63     g_object_unref(G_OBJECT(item));
64     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
65         delete *i;
66     }
67     entity.clear(); // this shouldn't be necessary, though
68 }
70 /** TODO: is this still needed?
71 void sp_knot_holder_destroy(SPKnotHolder *kh) {
72     g_object_unref(kh);
73     }
74 **/
76 /**
77  * \param p In desktop coordinates.
78  */
80 void
81 KnotHolder::update_knots()
82 {
83     NR::Matrix const i2d(sp_item_i2d_affine(item));
85     for(std::list<KnotHolderEntity *>::iterator i = entity.begin(); i != entity.end(); ++i) {
86         KnotHolderEntity *e = *i;
87         e->update_knot();
88     }
89 }
91 void
92 KnotHolder::knot_clicked_handler(SPKnot *knot, guint state)
93 {
94     KnotHolder *knot_holder = this;
96     for(std::list<KnotHolderEntity *>::iterator i = knot_holder->entity.begin(); i != knot_holder->entity.end(); ++i) {
97         KnotHolderEntity *e = *i;
98         if (e->knot == knot) {
99             // no need to test whether knot_click exists since it's virtual now
100             e->knot_click_func(state);
101             break;
102         }
103     }
105     if (SP_IS_SHAPE(item)) {
106         sp_shape_set_shape(SP_SHAPE(item));
107     }
109     knot_holder->update_knots();
111     unsigned int object_verb = SP_VERB_NONE;
113     if (SP_IS_RECT(item))
114         object_verb = SP_VERB_CONTEXT_RECT;
115     else if (SP_IS_BOX3D(item))
116         object_verb = SP_VERB_CONTEXT_3DBOX;
117     else if (SP_IS_GENERICELLIPSE(item))
118         object_verb = SP_VERB_CONTEXT_ARC;
119     else if (SP_IS_STAR(item))
120         object_verb = SP_VERB_CONTEXT_STAR;
121     else if (SP_IS_SPIRAL(item))
122         object_verb = SP_VERB_CONTEXT_SPIRAL;
123     else if (SP_IS_OFFSET(item)) {
124         if (SP_OFFSET(item)->sourceHref)
125             object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
126         else
127             object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
128     }
130     // for drag, this is done by ungrabbed_handler, but for click we must do it here
131     sp_document_done(SP_OBJECT_DOCUMENT(item), object_verb, 
132                      _("Change handle"));
135 void
136 KnotHolder::knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state)
138     // this was a local change and the knotholder does not need to be recreated:
139     this->local_change = TRUE;
141     for(std::list<KnotHolderEntity *>::iterator i = this->entity.begin(); i != this->entity.end(); ++i) {
142         KnotHolderEntity *e = *i;
143         if (e->knot == knot) {
144             NR::Point const q = *p / sp_item_i2d_affine(item);
145             e->knot_set_func(q, e->knot->drag_origin / sp_item_i2d_affine(item), state);
146             break;
147         }
148     }
150     if (SP_IS_SHAPE (item)) {
151         sp_shape_set_shape(SP_SHAPE (item));
152     }
154     this->update_knots();
157 void
158 KnotHolder::knot_ungrabbed_handler()
160     if (this->released) {
161         this->released(this->item);
162     } else {
163         SPObject *object = (SPObject *) this->item;
164         object->updateRepr(object->repr, SP_OBJECT_WRITE_EXT);
166         unsigned int object_verb = SP_VERB_NONE;
168         if (SP_IS_RECT(object))
169             object_verb = SP_VERB_CONTEXT_RECT;
170         else if (SP_IS_BOX3D(object))
171             object_verb = SP_VERB_CONTEXT_3DBOX;
172         else if (SP_IS_GENERICELLIPSE(object))
173             object_verb = SP_VERB_CONTEXT_ARC;
174         else if (SP_IS_STAR(object))
175             object_verb = SP_VERB_CONTEXT_STAR;
176         else if (SP_IS_SPIRAL(object))
177             object_verb = SP_VERB_CONTEXT_SPIRAL;
178         else if (SP_IS_OFFSET(object)) {
179             if (SP_OFFSET(object)->sourceHref)
180                 object_verb = SP_VERB_SELECTION_LINKED_OFFSET;
181             else
182                 object_verb = SP_VERB_SELECTION_DYNAMIC_OFFSET;
183         }
184         
185         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb,
186                          _("Move handle"));
187     }
190 void
191 KnotHolder::add_pattern_knotholder()
193     if ((SP_OBJECT(item)->style->fill.isPaintserver())
194         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
195     {
196         PatternKnotHolderEntityXY *entity_xy = new PatternKnotHolderEntityXY();
197         PatternKnotHolderEntityAngle *entity_angle = new PatternKnotHolderEntityAngle();
198         PatternKnotHolderEntityScale *entity_scale = new PatternKnotHolderEntityScale();
199         entity_xy->create(desktop, item, this,
200                           // TRANSLATORS: This refers to the pattern that's inside the object
201                           _("<b>Move</b> the pattern fill inside the object"),
202                           SP_KNOT_SHAPE_CROSS);
203         entity_angle->create(desktop, item, this,
204                              _("<b>Scale</b> the pattern fill uniformly"),
205                              SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR);
206         entity_scale->create(desktop, item, this,
207                              _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"),
208                              SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR);
209         entity.push_back(entity_xy);
210         entity.push_back(entity_angle);
211         entity.push_back(entity_scale);
212     }
215 /*
216   Local Variables:
217   mode:c++
218   c-file-style:"stroustrup"
219   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
220   indent-tabs-mode:nil
221   fill-column:99
222   End:
223 */
224 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :