Code

switch to sigc++ signal for "release"
[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  *
10  * Copyright (C) 2001-2005 authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #define noKNOT_HOLDER_DEBUG
18 #include "document.h"
19 #include "sp-shape.h"
20 #include "knot.h"
21 #include "knotholder.h"
22 #include "knot-holder-entity.h"
23 #include "rect-context.h"
24 #include "sp-rect.h"
25 #include "arc-context.h"
26 #include "sp-ellipse.h"
27 #include "star-context.h"
28 #include "sp-star.h"
29 #include "spiral-context.h"
30 #include "sp-spiral.h"
32 #include <libnr/nr-matrix-div.h>
34 class SPDesktop;
36 static void knot_clicked_handler (SPKnot *knot, guint state, gpointer data);
37 static void knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state, gpointer data);
38 static void knot_ungrabbed_handler (SPKnot *knot, unsigned int state, SPKnotHolder *kh);
39 static void sp_knot_holder_class_init(SPKnotHolderClass *klass);
41 void sp_knot_holder_dispose(GObject *object);
43 #ifdef KNOT_HOLDER_DEBUG
45 static void sp_knot_holder_debug(GtkObject *object, gpointer data)
46 {
47     g_print("sp-knot-holder-debug: [type=%s] [data=%s]\n", gtk_type_name(GTK_OBJECT_TYPE(object)), (const gchar *) data);
48 }
49 #endif
51 static GObjectClass *parent_class;
53 /**
54  * Registers SPKnotHolder class and returns its type number.
55  */
56 GType sp_knot_holder_get_type()
57 {
58     static GType type = 0;
59     if (!type) {
60         GTypeInfo info = {
61             sizeof(SPKnotHolderClass),
62             NULL,       /* base_init */
63             NULL,       /* base_finalize */
64             (GClassInitFunc) sp_knot_holder_class_init,
65             NULL,       /* class_finalize */
66             NULL,       /* class_data */
67             sizeof (SPKnotHolder),
68             16, /* n_preallocs */
69             NULL,
70             NULL
71         };
72         type = g_type_register_static (G_TYPE_OBJECT, "SPKnotHolder", &info, (GTypeFlags) 0);
73     }
74     return type;
75 }
77 /**
78  * SPKnotHolder vtable initialization.
79  */
80 static void sp_knot_holder_class_init(SPKnotHolderClass *klass){
81     parent_class = (GObjectClass*) g_type_class_peek_parent(klass);
82     klass->dispose = sp_knot_holder_dispose;
83 }
85 SPKnotHolder *sp_knot_holder_new(SPDesktop *desktop, SPItem *item, SPKnotHolderReleasedFunc relhandler)
86 {
87     Inkscape::XML::Node *repr = SP_OBJECT(item)->repr;
89     g_return_val_if_fail(desktop != NULL, NULL);
90     g_return_val_if_fail(item != NULL, NULL);
91     g_return_val_if_fail(SP_IS_ITEM(item), NULL);
93     SPKnotHolder *knot_holder = (SPKnotHolder*)g_object_new (SP_TYPE_KNOT_HOLDER, 0);
94     knot_holder->desktop = desktop;
95     knot_holder->item = item;
96     g_object_ref(G_OBJECT(item));
97     knot_holder->entity = NULL;
99     knot_holder->released = relhandler;
101     knot_holder->repr = repr;
102     knot_holder->local_change = FALSE;
104 #ifdef KNOT_HOLDER_DEBUG
105     g_signal_connect(G_OBJECT(desktop), "destroy", sp_knot_holder_debug, (gpointer) "SPKnotHolder::item");
106 #endif
108     return knot_holder;
111 void sp_knot_holder_dispose(GObject *object) {
112     SPKnotHolder *kh = G_TYPE_CHECK_INSTANCE_CAST((object), SP_TYPE_KNOT_HOLDER, SPKnotHolder);
114     g_object_unref(G_OBJECT(kh->item));
115     while (kh->entity) {
116         SPKnotHolderEntity *e = (SPKnotHolderEntity *) kh->entity->data;
117         g_signal_handler_disconnect(e->knot, e->_click_handler_id);
118         g_signal_handler_disconnect(e->knot, e->_ungrab_handler_id);
119         /* unref should call destroy */
120         g_object_unref(e->knot);
121         g_free(e);
122         kh->entity = g_slist_remove(kh->entity, e);
123     }
126 void sp_knot_holder_destroy(SPKnotHolder *kh) {
127     g_object_unref(kh);
128     }
130 void sp_knot_holder_add(
131     SPKnotHolder *knot_holder,
132     SPKnotHolderSetFunc knot_set,
133     SPKnotHolderGetFunc knot_get,
134     void (* knot_click) (SPItem *item, guint state),
135     const gchar *tip
136     )
138     sp_knot_holder_add_full(knot_holder, knot_set, knot_get, knot_click, SP_KNOT_SHAPE_DIAMOND, SP_KNOT_MODE_XOR, tip);
141 void sp_knot_holder_add_full(
142     SPKnotHolder *knot_holder,
143     SPKnotHolderSetFunc knot_set,
144     SPKnotHolderGetFunc knot_get,
145     void (* knot_click) (SPItem *item, guint state),
146     SPKnotShapeType     shape,
147     SPKnotModeType      mode,
148     const gchar *tip
149     )
151     g_return_if_fail(knot_holder != NULL);
152     g_return_if_fail(knot_set != NULL);
153     g_return_if_fail(knot_get != NULL);
154         
155     SPItem *item = SP_ITEM(knot_holder->item);
157     /* create new SPKnotHolderEntry */
158     SPKnotHolderEntity *e = g_new(SPKnotHolderEntity, 1);
159     e->knot = sp_knot_new(knot_holder->desktop, tip);
160     e->knot_set = knot_set;
161     e->knot_get = knot_get;
162     if (knot_click) {
163         e->knot_click = knot_click;
164     } else {
165         e->knot_click = NULL;
166     }
168     g_object_set(G_OBJECT (e->knot->item), "shape", shape, NULL);
169     g_object_set(G_OBJECT (e->knot->item), "mode", mode, NULL);
171     // TODO: add a color argument
172     //e->knot->fill [SP_KNOT_STATE_NORMAL] = 0x00ff0000;
173     //g_object_set (G_OBJECT (e->knot->item), "fill_color", 0x00ff0000, NULL);
175     knot_holder->entity = g_slist_append(knot_holder->entity, e);
177     /* Move to current point. */
178     NR::Point dp = e->knot_get(item) * sp_item_i2d_affine(item);
179     sp_knot_set_position(e->knot, &dp, SP_KNOT_STATE_NORMAL);
181     e->handler_id = g_signal_connect(e->knot, "moved", G_CALLBACK(knot_moved_handler), knot_holder);
182     e->_click_handler_id = g_signal_connect(e->knot, "clicked", G_CALLBACK(knot_clicked_handler), knot_holder);
183     e->_ungrab_handler_id = g_signal_connect(e->knot, "ungrabbed", G_CALLBACK(knot_ungrabbed_handler), knot_holder);
185 #ifdef KNOT_HOLDER_DEBUG
186     g_signal_connect(ob, "destroy", sp_knot_holder_debug, "SPKnotHolder::knot");
187 #endif
188     sp_knot_show(e->knot);
191 /**
192  * \param p In desktop coordinates.
193  */
195 static void knotholder_update_knots(SPKnotHolder *knot_holder, SPItem *item)
197     NR::Matrix const i2d(sp_item_i2d_affine(item));
199     for (GSList *el = knot_holder->entity; el; el = el->next) {
200         SPKnotHolderEntity *e = (SPKnotHolderEntity *) el->data;
201         GObject *kob = e->knot;
203         NR::Point dp( e->knot_get(item) * i2d );
204         g_signal_handler_block(kob, e->handler_id);
205         sp_knot_set_position(e->knot, &dp, SP_KNOT_STATE_NORMAL);
206         g_signal_handler_unblock(kob, e->handler_id);
207     }
210 static void knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
212     SPKnotHolder *knot_holder = (SPKnotHolder *) data;
213     SPItem *item  = SP_ITEM (knot_holder->item);
215     g_object_ref(knot_holder);
216     for (GSList *el = knot_holder->entity; el; el = el->next) {
217         SPKnotHolderEntity *e = (SPKnotHolderEntity *) el->data;
218         if (e->knot == knot) {
219             if (e->knot_click) {
220                 e->knot_click(item, state);
221             }
222             break;
223         }
224     }
226     if (SP_IS_SHAPE(item)) {
227         sp_shape_set_shape(SP_SHAPE(item));
228     }
230     knotholder_update_knots(knot_holder, item);
231     g_object_unref(knot_holder);
233     unsigned int object_verb = SP_VERB_NONE;
235     if (SP_IS_RECT(item))
236         object_verb = SP_VERB_CONTEXT_RECT;
237     else if (SP_IS_GENERICELLIPSE(item))
238         object_verb = SP_VERB_CONTEXT_ARC;
239     else if (SP_IS_STAR(item))
240         object_verb = SP_VERB_CONTEXT_STAR;
241     else if (SP_IS_SPIRAL(item))
242         object_verb = SP_VERB_CONTEXT_SPIRAL;
244     // for drag, this is done by ungrabbed_handler, but for click we must do it here
245     sp_document_done(SP_OBJECT_DOCUMENT(knot_holder->item), object_verb, 
246                      /* TODO: annotate */ "knotholder.cpp:246");
249 static void knot_moved_handler(SPKnot *knot, NR::Point const *p, guint state, gpointer data)
251     SPKnotHolder *knot_holder = (SPKnotHolder *) data;
252     SPItem *item  = SP_ITEM (knot_holder->item);
253     // this was a local change and the knotholder does not need to be recreated:
254     knot_holder->local_change = TRUE;
256     for (GSList *el = knot_holder->entity; el; el = el->next) {
257         SPKnotHolderEntity *e = (SPKnotHolderEntity *) el->data;
258         if (e->knot == knot) {
259             NR::Point const q = *p / sp_item_i2d_affine(item);
260             e->knot_set(item, q, e->knot->drag_origin / sp_item_i2d_affine(item), state);
261             break;
262         }
263     }
265     if (SP_IS_SHAPE (item)) {
266         sp_shape_set_shape(SP_SHAPE (item));
267     }
269     knotholder_update_knots(knot_holder, item);
272 static void knot_ungrabbed_handler(SPKnot *knot, unsigned int state, SPKnotHolder *kh)
274     if (kh->released) {
275         kh->released(kh->item);
276     } else {
277         SPObject *object = (SPObject *) kh->item;
278         object->updateRepr(object->repr, SP_OBJECT_WRITE_EXT);
280         unsigned int object_verb = SP_VERB_NONE;
282         if (SP_IS_RECT(object))
283             object_verb = SP_VERB_CONTEXT_RECT;
284         else if (SP_IS_GENERICELLIPSE(object))
285             object_verb = SP_VERB_CONTEXT_ARC;
286         else if (SP_IS_STAR(object))
287             object_verb = SP_VERB_CONTEXT_STAR;
288         else if (SP_IS_SPIRAL(object))
289             object_verb = SP_VERB_CONTEXT_SPIRAL;
291         sp_document_done(SP_OBJECT_DOCUMENT (object), object_verb, 
292                          /* TODO: annotate */ "knotholder.cpp:292");
293     }
296 /*
297   Local Variables:
298   mode:c++
299   c-file-style:"stroustrup"
300   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
301   indent-tabs-mode:nil
302   fill-column:99
303   End:
304 */
305 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :