Code

Implement snapping in shape-tools during initial-drawing
[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 "spiral-context.h"
35 #include "sp-metrics.h"
36 #include <glibmm/i18n.h>
37 #include "object-edit.h"
38 #include "xml/repr.h"
39 #include "xml/node-event-vector.h"
40 #include "prefs-utils.h"
41 #include "context-fns.h"
43 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
44 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
45 static void sp_spiral_context_dispose(GObject *object);
46 static void sp_spiral_context_setup(SPEventContext *ec);
47 static void sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
49 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static void sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state);
52 static void sp_spiral_finish(SPSpiralContext *sc);
54 static SPEventContextClass *parent_class;
56 GtkType
57 sp_spiral_context_get_type()
58 {
59     static GType type = 0;
60     if (!type) {
61         GTypeInfo info = {
62             sizeof(SPSpiralContextClass),
63             NULL, NULL,
64             (GClassInitFunc) sp_spiral_context_class_init,
65             NULL, NULL,
66             sizeof(SPSpiralContext),
67             4,
68             (GInstanceInitFunc) sp_spiral_context_init,
69             NULL,    /* value_table */
70         };
71         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
72     }
73     return type;
74 }
76 static void
77 sp_spiral_context_class_init(SPSpiralContextClass *klass)
78 {
79     GObjectClass *object_class = (GObjectClass *) klass;
80     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
82     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
84     object_class->dispose = sp_spiral_context_dispose;
86     event_context_class->setup = sp_spiral_context_setup;
87     event_context_class->set = sp_spiral_context_set;
88     event_context_class->root_handler = sp_spiral_context_root_handler;
89 }
91 static void
92 sp_spiral_context_init(SPSpiralContext *spiral_context)
93 {
94     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
96     event_context->cursor_shape = cursor_spiral_xpm;
97     event_context->hot_x = 4;
98     event_context->hot_y = 4;
99     event_context->xp = 0;
100     event_context->yp = 0;
101     event_context->tolerance = 0;
102     event_context->within_tolerance = false;
103     event_context->item_to_select = NULL;
105     event_context->shape_repr = NULL;
106     event_context->shape_knot_holder = NULL;
108     spiral_context->item = NULL;
110     spiral_context->revo = 3.0;
111     spiral_context->exp = 1.0;
112     spiral_context->t0 = 0.0;
114     new (&spiral_context->sel_changed_connection) sigc::connection();
117 static void
118 sp_spiral_context_dispose(GObject *object)
120     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(object);
121     SPEventContext *ec = SP_EVENT_CONTEXT(object);
123     ec->enableGrDrag(false);
125     sc->sel_changed_connection.disconnect();
126     sc->sel_changed_connection.~connection();
128     /* fixme: This is necessary because we do not grab */
129     if (sc->item) sp_spiral_finish(sc);
131     if (ec->shape_knot_holder) {
132         sp_knot_holder_destroy(ec->shape_knot_holder);
133         ec->shape_knot_holder = NULL;
134     }
136     if (ec->shape_repr) { // remove old listener
137         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
138         Inkscape::GC::release(ec->shape_repr);
139         ec->shape_repr = 0;
140     }
142     if (sc->_message_context) {
143         delete sc->_message_context;
144     }
146     G_OBJECT_CLASS(parent_class)->dispose(object);
149 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
150     NULL, /* child_added */
151     NULL, /* child_removed */
152     ec_shape_event_attr_changed,
153     NULL, /* content_changed */
154     NULL  /* order_changed */
155 };
157 /**
158 \brief  Callback that processes the "changed" signal on the selection;
159 destroys old and creates new knotholder
160 */
161 void
162 sp_spiral_context_selection_changed(Inkscape::Selection *selection, gpointer data)
164     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
165     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
167     if (ec->shape_knot_holder) { // desktroy knotholder
168         sp_knot_holder_destroy(ec->shape_knot_holder);
169         ec->shape_knot_holder = NULL;
170     }
172     if (ec->shape_repr) { // remove old listener
173         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
174         Inkscape::GC::release(ec->shape_repr);
175         ec->shape_repr = 0;
176     }
178     SPItem *item = selection->singleItem();
179     if (item) {
180         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
181         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
182         if (shape_repr) {
183             ec->shape_repr = shape_repr;
184             Inkscape::GC::anchor(shape_repr);
185             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
186         }
187     }
190 static void
191 sp_spiral_context_setup(SPEventContext *ec)
193     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
195     if (((SPEventContextClass *) parent_class)->setup)
196         ((SPEventContextClass *) parent_class)->setup(ec);
198     sp_event_context_read(ec, "expansion");
199     sp_event_context_read(ec, "revolution");
200     sp_event_context_read(ec, "t0");
202     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
204     SPItem *item = selection->singleItem();
205     if (item) {
206         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
207         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
208         if (shape_repr) {
209             ec->shape_repr = shape_repr;
210             Inkscape::GC::anchor(shape_repr);
211             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
212         }
213     }
215     sc->sel_changed_connection.disconnect();
216     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
218     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
219         ec->enableSelectionCue();
220     }
222     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
223         ec->enableGrDrag();
224     }
226     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
229 static void
230 sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
232     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
234     if (!strcmp(key, "expansion")) {
235         sc->exp = (val) ? g_ascii_strtod(val, NULL) : 1.0;
236         sc->exp = CLAMP(sc->exp, 0.0, 1000.0);
237     } else if (!strcmp(key, "revolution")) {
238         sc->revo = (val) ? g_ascii_strtod(val, NULL) : 3.0;
239         sc->revo = CLAMP(sc->revo, 0.05, 40.0);
240     } else if (!strcmp(key, "t0")) {
241         sc->t0 = (val) ? g_ascii_strtod(val, NULL) : 0.0;
242         sc->t0 = CLAMP(sc->t0, 0.0, 0.999);
243     }
246 static gint
247 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
249     static gboolean dragging;
251     SPDesktop *desktop = event_context->desktop;
252     Inkscape::Selection *selection = sp_desktop_selection (desktop);
253     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
255     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
257     gint ret = FALSE;
259     switch (event->type) {
260         case GDK_BUTTON_PRESS:
261             if (event->button.button == 1) {
263                 dragging = TRUE;
264                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
266                 SnapManager const &m = desktop->namedview->snap_manager;
267                 sc->center = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
269                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
270                                     ( GDK_KEY_PRESS_MASK |
271                                       GDK_BUTTON_RELEASE_MASK |
272                                       GDK_POINTER_MOTION_MASK |
273                                       GDK_BUTTON_PRESS_MASK    ),
274                                     NULL, event->button.time);
275                 ret = TRUE;
276             }
277             break;
278         case GDK_MOTION_NOTIFY:
279             if (dragging && event->motion.state && GDK_BUTTON1_MASK) {
281                 if ( event_context->within_tolerance
282                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
283                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
284                     break; // do not drag if we're within tolerance from origin
285                 }
286                 // Once the user has moved farther than tolerance from the original location
287                 // (indicating they intend to draw, not click), then always process the
288                 // motion notify coordinates as given (no snapping back to origin)
289                 event_context->within_tolerance = false;
291                 NR::Point const motion_w(event->motion.x, event->motion.y);
292                 NR::Point motion_dt(event_context->desktop->w2d(motion_w));
293                 
294                 SnapManager const &m = desktop->namedview->snap_manager;
295                 motion_dt = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, motion_dt, sc->item).getPoint();
296             
297                 sp_spiral_drag(sc, motion_dt, event->motion.state);
298                 ret = TRUE;
299             }
300             break;
301         case GDK_BUTTON_RELEASE:
302             event_context->xp = event_context->yp = 0;
303             if (event->button.button == 1) {
304                 dragging = FALSE;
305                 if (!event_context->within_tolerance) {
306                     // we've been dragging, finish the spiral
307                     sp_spiral_finish(sc);
308                 } else if (event_context->item_to_select) {
309                     // no dragging, select clicked item if any
310                     if (event->button.state & GDK_SHIFT_MASK) {
311                         selection->toggle(event_context->item_to_select);
312                     } else {
313                         selection->set(event_context->item_to_select);
314                     }
315                 } else {
316                     // click in an empty space
317                     selection->clear();
318                 }
320                 event_context->item_to_select = NULL;
321                 ret = TRUE;
322                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
323             }
324             break;
325         case GDK_KEY_PRESS:
326             switch (get_group0_keyval(&event->key)) {
327                 case GDK_Alt_R:
328                 case GDK_Control_L:
329                 case GDK_Control_R:
330                 case GDK_Shift_L:
331                 case GDK_Shift_R:
332                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
333                 case GDK_Meta_R:
334                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
335                                                _("<b>Ctrl</b>: snap angle"),
336                                                NULL,
337                                                _("<b>Alt</b>: lock spiral radius"));
338                     break;
339                 case GDK_Up:
340                 case GDK_Down:
341                 case GDK_KP_Up:
342                 case GDK_KP_Down:
343                     // prevent the zoom field from activation
344                     if (!MOD__CTRL_ONLY)
345                         ret = TRUE;
346                     break;
347                 case GDK_x:
348                 case GDK_X:
349                     if (MOD__ALT_ONLY) {
350                         desktop->setToolboxFocusTo ("altx-spiral");
351                         ret = TRUE;
352                     }
353                     break;
354                 case GDK_Escape:
355                     sp_desktop_selection(desktop)->clear();
356                     //TODO: make dragging escapable by Esc
357                 default:
358                     break;
359             }
360             break;
361         case GDK_KEY_RELEASE:
362             switch (get_group0_keyval(&event->key)) {
363                 case GDK_Alt_L:
364                 case GDK_Alt_R:
365                 case GDK_Control_L:
366                 case GDK_Control_R:
367                 case GDK_Shift_L:
368                 case GDK_Shift_R:
369                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
370                 case GDK_Meta_R:
371                     event_context->defaultMessageContext()->clear();
372                     break;
373                 default:
374                     break;
375             }
376             break;
377         default:
378             break;
379     }
381     if (!ret) {
382         if (((SPEventContextClass *) parent_class)->root_handler)
383             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
384     }
386     return ret;
389 static void
390 sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
392     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
394     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
396     if (!sc->item) {
398         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
399             return;
400         }
402         /* Create object */
403         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
404         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
405         repr->setAttribute("sodipodi:type", "spiral");
407         /* Set style */
408         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
410         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
411         Inkscape::GC::release(repr);
412         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
413         sc->item->updateRepr();
415         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
416     }
418     NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
419     NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
420     SnapManager const &m = desktop->namedview->snap_manager;
421     p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
423     SPSpiral *spiral = SP_SPIRAL(sc->item);
425     NR::Point const delta = p1 - p0;
426     gdouble const rad = NR::L2(delta);
428     gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
430     if (state & GDK_CONTROL_MASK) {
431         arg = sp_round(arg, M_PI/snaps);
432     }
434     /* Fixme: these parameters should be got from dialog box */
435     sp_spiral_position_set(spiral, p0[NR::X], p0[NR::Y],
436                            /*expansion*/ sc->exp,
437                            /*revolution*/ sc->revo,
438                            rad, arg,
439                            /*t0*/ sc->t0);
441     /* status text */
442     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
443     sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
444                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
445                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
446     g_string_free(rads, FALSE);
449 static void
450 sp_spiral_finish(SPSpiralContext *sc)
452     sc->_message_context->clear();
454     if (sc->item != NULL) {
455         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
456         SPSpiral  *spiral = SP_SPIRAL(sc->item);
458         sp_shape_set_shape(SP_SHAPE(spiral));
459         SP_OBJECT(spiral)->updateRepr(NULL, SP_OBJECT_WRITE_EXT);
461         sp_canvas_end_forced_full_redraws(desktop->canvas);
463         sp_desktop_selection(desktop)->set(sc->item);
464         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL, 
465                          _("Create spiral"));
467         sc->item = NULL;
468     }
472 /*
473   Local Variables:
474   mode:c++
475   c-file-style:"stroustrup"
476   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
477   indent-tabs-mode:nil
478   fill-column:99
479   End:
480 */
481 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :