Code

fix crash 1544495 when closing a document with mask/clippath: it makes no sense to...
[inkscape.git] / src / spiral-context.cpp
1 #define __SP_SPIRAL_CONTEXT_C__
3 /*
4  * Spiral drawing context
5  *
6  * Authors:
7  *   Mitsuru Oka
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 1999-2001 Lauris Kaplinski
12  * Copyright (C) 2001-2002 Mitsuru Oka
13  *
14  * Released under GNU GPL
15  */
17 #include "config.h"
19 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-spiral.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "desktop-affine.h"
29 #include "snap.h"
30 #include "desktop.h"
31 #include "desktop-style.h"
32 #include "message-context.h"
33 #include "pixmaps/cursor-spiral.xpm"
34 #include "pixmaps/cursor-spiral.pixbuf"
35 #include "spiral-context.h"
36 #include "sp-metrics.h"
37 #include <glibmm/i18n.h>
38 #include "object-edit.h"
39 #include "xml/repr.h"
40 #include "xml/node-event-vector.h"
41 #include "prefs-utils.h"
42 #include "context-fns.h"
44 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
45 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
46 static void sp_spiral_context_dispose(GObject *object);
47 static void sp_spiral_context_setup(SPEventContext *ec);
48 static void sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
52 static void sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state);
53 static void sp_spiral_finish(SPSpiralContext *sc);
55 static SPEventContextClass *parent_class;
57 GtkType
58 sp_spiral_context_get_type()
59 {
60     static GType type = 0;
61     if (!type) {
62         GTypeInfo info = {
63             sizeof(SPSpiralContextClass),
64             NULL, NULL,
65             (GClassInitFunc) sp_spiral_context_class_init,
66             NULL, NULL,
67             sizeof(SPSpiralContext),
68             4,
69             (GInstanceInitFunc) sp_spiral_context_init,
70             NULL,    /* value_table */
71         };
72         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
73     }
74     return type;
75 }
77 static void
78 sp_spiral_context_class_init(SPSpiralContextClass *klass)
79 {
80     GObjectClass *object_class = (GObjectClass *) klass;
81     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
85     object_class->dispose = sp_spiral_context_dispose;
87     event_context_class->setup = sp_spiral_context_setup;
88     event_context_class->set = sp_spiral_context_set;
89     event_context_class->root_handler = sp_spiral_context_root_handler;
90 }
92 static void
93 sp_spiral_context_init(SPSpiralContext *spiral_context)
94 {
95     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
97     event_context->cursor_shape = cursor_spiral_xpm;
98     event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
99             -1,
100             cursor_spiral_pixbuf,
101             FALSE,
102             NULL); 
103     event_context->hot_x = 4;
104     event_context->hot_y = 4;
105     event_context->xp = 0;
106     event_context->yp = 0;
107     event_context->tolerance = 0;
108     event_context->within_tolerance = false;
109     event_context->item_to_select = NULL;
111     event_context->shape_repr = NULL;
112     event_context->shape_knot_holder = NULL;
114     spiral_context->item = NULL;
116     spiral_context->revo = 3.0;
117     spiral_context->exp = 1.0;
118     spiral_context->t0 = 0.0;
120     new (&spiral_context->sel_changed_connection) sigc::connection();
123 static void
124 sp_spiral_context_dispose(GObject *object)
126     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(object);
127     SPEventContext *ec = SP_EVENT_CONTEXT(object);
129     ec->enableGrDrag(false);
131     sc->sel_changed_connection.disconnect();
132     sc->sel_changed_connection.~connection();
134     /* fixme: This is necessary because we do not grab */
135     if (sc->item) sp_spiral_finish(sc);
137     if (ec->shape_knot_holder) {
138         sp_knot_holder_destroy(ec->shape_knot_holder);
139         ec->shape_knot_holder = NULL;
140     }
142     if (ec->shape_repr) { // remove old listener
143         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
144         Inkscape::GC::release(ec->shape_repr);
145         ec->shape_repr = 0;
146     }
148     if (sc->_message_context) {
149         delete sc->_message_context;
150     }
152     G_OBJECT_CLASS(parent_class)->dispose(object);
155 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
156     NULL, /* child_added */
157     NULL, /* child_removed */
158     ec_shape_event_attr_changed,
159     NULL, /* content_changed */
160     NULL  /* order_changed */
161 };
163 /**
164 \brief  Callback that processes the "changed" signal on the selection;
165 destroys old and creates new knotholder
166 */
167 void
168 sp_spiral_context_selection_changed(Inkscape::Selection *selection, gpointer data)
170     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
171     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
173     if (ec->shape_knot_holder) { // desktroy knotholder
174         sp_knot_holder_destroy(ec->shape_knot_holder);
175         ec->shape_knot_holder = NULL;
176     }
178     if (ec->shape_repr) { // remove old listener
179         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
180         Inkscape::GC::release(ec->shape_repr);
181         ec->shape_repr = 0;
182     }
184     SPItem *item = selection->singleItem();
185     if (item) {
186         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
187         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
188         if (shape_repr) {
189             ec->shape_repr = shape_repr;
190             Inkscape::GC::anchor(shape_repr);
191             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
192         }
193     }
196 static void
197 sp_spiral_context_setup(SPEventContext *ec)
199     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
201     if (((SPEventContextClass *) parent_class)->setup)
202         ((SPEventContextClass *) parent_class)->setup(ec);
204     sp_event_context_read(ec, "expansion");
205     sp_event_context_read(ec, "revolution");
206     sp_event_context_read(ec, "t0");
208     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
210     SPItem *item = selection->singleItem();
211     if (item) {
212         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
213         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
214         if (shape_repr) {
215             ec->shape_repr = shape_repr;
216             Inkscape::GC::anchor(shape_repr);
217             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
218         }
219     }
221     sc->sel_changed_connection.disconnect();
222     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
224     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
225         ec->enableSelectionCue();
226     }
228     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
229         ec->enableGrDrag();
230     }
232     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
235 static void
236 sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
238     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
240     if (!strcmp(key, "expansion")) {
241         sc->exp = (val) ? g_ascii_strtod(val, NULL) : 1.0;
242         sc->exp = CLAMP(sc->exp, 0.0, 1000.0);
243     } else if (!strcmp(key, "revolution")) {
244         sc->revo = (val) ? g_ascii_strtod(val, NULL) : 3.0;
245         sc->revo = CLAMP(sc->revo, 0.05, 40.0);
246     } else if (!strcmp(key, "t0")) {
247         sc->t0 = (val) ? g_ascii_strtod(val, NULL) : 0.0;
248         sc->t0 = CLAMP(sc->t0, 0.0, 0.999);
249     }
252 static gint
253 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
255     static gboolean dragging;
257     SPDesktop *desktop = event_context->desktop;
258     Inkscape::Selection *selection = sp_desktop_selection (desktop);
259     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
261     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
263     gint ret = FALSE;
265     switch (event->type) {
266         case GDK_BUTTON_PRESS:
267             if (event->button.button == 1) {
269                 dragging = TRUE;
270                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
272                 SnapManager const &m = desktop->namedview->snap_manager;
273                 sc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
275                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
276                                     ( GDK_KEY_PRESS_MASK |
277                                       GDK_BUTTON_RELEASE_MASK |
278                                       GDK_POINTER_MOTION_MASK |
279                                       GDK_BUTTON_PRESS_MASK    ),
280                                     NULL, event->button.time);
281                 ret = TRUE;
282             }
283             break;
284         case GDK_MOTION_NOTIFY:
285             if (dragging && event->motion.state && GDK_BUTTON1_MASK) {
287                 if ( event_context->within_tolerance
288                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
289                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
290                     break; // do not drag if we're within tolerance from origin
291                 }
292                 // Once the user has moved farther than tolerance from the original location
293                 // (indicating they intend to draw, not click), then always process the
294                 // motion notify coordinates as given (no snapping back to origin)
295                 event_context->within_tolerance = false;
297                 NR::Point const motion_w(event->motion.x, event->motion.y);
298                 NR::Point const motion_dt(event_context->desktop->w2d(motion_w));
299                 sp_spiral_drag(sc, motion_dt, event->motion.state);
300                 ret = TRUE;
301             }
302             break;
303         case GDK_BUTTON_RELEASE:
304             event_context->xp = event_context->yp = 0;
305             if (event->button.button == 1) {
306                 dragging = FALSE;
307                 if (!event_context->within_tolerance) {
308                     // we've been dragging, finish the spiral
309                     sp_spiral_finish(sc);
310                 } else if (event_context->item_to_select) {
311                     // no dragging, select clicked item if any
312                     if (event->button.state & GDK_SHIFT_MASK) {
313                         selection->toggle(event_context->item_to_select);
314                     } else {
315                         selection->set(event_context->item_to_select);
316                     }
317                 } else {
318                     // click in an empty space
319                     selection->clear();
320                 }
322                 event_context->item_to_select = NULL;
323                 ret = TRUE;
324                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
325             }
326             break;
327         case GDK_KEY_PRESS:
328             switch (get_group0_keyval(&event->key)) {
329                 case GDK_Alt_R:
330                 case GDK_Control_L:
331                 case GDK_Control_R:
332                 case GDK_Shift_L:
333                 case GDK_Shift_R:
334                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
335                 case GDK_Meta_R:
336                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
337                                                _("<b>Ctrl</b>: snap angle"),
338                                                NULL,
339                                                _("<b>Alt</b>: lock spiral radius"));
340                     break;
341                 case GDK_Up:
342                 case GDK_Down:
343                 case GDK_KP_Up:
344                 case GDK_KP_Down:
345                     // prevent the zoom field from activation
346                     if (!MOD__CTRL_ONLY)
347                         ret = TRUE;
348                     break;
349                 case GDK_x:
350                 case GDK_X:
351                     if (MOD__ALT_ONLY) {
352                         desktop->setToolboxFocusTo ("altx-spiral");
353                         ret = TRUE;
354                     }
355                     break;
356                 case GDK_Escape:
357                     sp_desktop_selection(desktop)->clear();
358                     //TODO: make dragging escapable by Esc
359                 default:
360                     break;
361             }
362             break;
363         case GDK_KEY_RELEASE:
364             switch (get_group0_keyval(&event->key)) {
365                 case GDK_Alt_L:
366                 case GDK_Alt_R:
367                 case GDK_Control_L:
368                 case GDK_Control_R:
369                 case GDK_Shift_L:
370                 case GDK_Shift_R:
371                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
372                 case GDK_Meta_R:
373                     event_context->defaultMessageContext()->clear();
374                     break;
375                 default:
376                     break;
377             }
378             break;
379         default:
380             break;
381     }
383     if (!ret) {
384         if (((SPEventContextClass *) parent_class)->root_handler)
385             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
386     }
388     return ret;
391 static void
392 sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
394     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
396     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
398     if (!sc->item) {
400         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
401             return;
402         }
404         /* Create object */
405         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
406         repr->setAttribute("sodipodi:type", "spiral");
408         /* Set style */
409         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
411         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
412         Inkscape::GC::release(repr);
413         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
414         sc->item->updateRepr();
415     }
417     NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
418     NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
419     SnapManager const &m = desktop->namedview->snap_manager;
420     p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
422     SPSpiral *spiral = SP_SPIRAL(sc->item);
424     NR::Point const delta = p1 - p0;
425     gdouble const rad = NR::L2(delta);
427     gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
429     if (state & GDK_CONTROL_MASK) {
430         arg = sp_round(arg, M_PI/snaps);
431     }
433     /* Fixme: these parameters should be got from dialog box */
434     sp_spiral_position_set(spiral, p0[NR::X], p0[NR::Y],
435                            /*expansion*/ sc->exp,
436                            /*revolution*/ sc->revo,
437                            rad, arg,
438                            /*t0*/ sc->t0);
440     /* status text */
441     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
442     sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
443                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
444                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
445     g_string_free(rads, FALSE);
448 static void
449 sp_spiral_finish(SPSpiralContext *sc)
451     sc->_message_context->clear();
453     if (sc->item != NULL) {
454         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
455         SPSpiral  *spiral = SP_SPIRAL(sc->item);
457         sp_shape_set_shape(SP_SHAPE(spiral));
458         SP_OBJECT(spiral)->updateRepr(NULL, SP_OBJECT_WRITE_EXT);
460         sp_desktop_selection(desktop)->set(sc->item);
461         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL, 
462                          /* TODO: annotate */ "spiral-context.cpp:462");
464         sc->item = NULL;
465     }
469 /*
470   Local Variables:
471   mode:c++
472   c-file-style:"stroustrup"
473   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
474   indent-tabs-mode:nil
475   fill-column:99
476   End:
477 */
478 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :