Code

Get rid of the SP_DT_* macros which do nothing more than provide additional, confusin...
[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);
267                 sc->center = m.freeSnap(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 const motion_dt(event_context->desktop->w2d(motion_w));
293                 sp_spiral_drag(sc, motion_dt, event->motion.state);
294                 ret = TRUE;
295             }
296             break;
297         case GDK_BUTTON_RELEASE:
298             event_context->xp = event_context->yp = 0;
299             if (event->button.button == 1) {
300                 dragging = FALSE;
301                 if (!event_context->within_tolerance) {
302                     // we've been dragging, finish the spiral
303                     sp_spiral_finish(sc);
304                 } else if (event_context->item_to_select) {
305                     // no dragging, select clicked item if any
306                     if (event->button.state & GDK_SHIFT_MASK) {
307                         selection->toggle(event_context->item_to_select);
308                     } else {
309                         selection->set(event_context->item_to_select);
310                     }
311                 } else {
312                     // click in an empty space
313                     selection->clear();
314                 }
316                 event_context->item_to_select = NULL;
317                 ret = TRUE;
318                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
319             }
320             break;
321         case GDK_KEY_PRESS:
322             switch (get_group0_keyval(&event->key)) {
323                 case GDK_Alt_R:
324                 case GDK_Control_L:
325                 case GDK_Control_R:
326                 case GDK_Shift_L:
327                 case GDK_Shift_R:
328                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
329                 case GDK_Meta_R:
330                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
331                                                _("<b>Ctrl</b>: snap angle"),
332                                                NULL,
333                                                _("<b>Alt</b>: lock spiral radius"));
334                     break;
335                 case GDK_Up:
336                 case GDK_Down:
337                 case GDK_KP_Up:
338                 case GDK_KP_Down:
339                     // prevent the zoom field from activation
340                     if (!MOD__CTRL_ONLY)
341                         ret = TRUE;
342                     break;
343                 case GDK_x:
344                 case GDK_X:
345                     if (MOD__ALT_ONLY) {
346                         desktop->setToolboxFocusTo ("altx-spiral");
347                         ret = TRUE;
348                     }
349                     break;
350                 case GDK_Escape:
351                     sp_desktop_selection(desktop)->clear();
352                     //TODO: make dragging escapable by Esc
353                 default:
354                     break;
355             }
356             break;
357         case GDK_KEY_RELEASE:
358             switch (get_group0_keyval(&event->key)) {
359                 case GDK_Alt_L:
360                 case GDK_Alt_R:
361                 case GDK_Control_L:
362                 case GDK_Control_R:
363                 case GDK_Shift_L:
364                 case GDK_Shift_R:
365                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
366                 case GDK_Meta_R:
367                     event_context->defaultMessageContext()->clear();
368                     break;
369                 default:
370                     break;
371             }
372             break;
373         default:
374             break;
375     }
377     if (!ret) {
378         if (((SPEventContextClass *) parent_class)->root_handler)
379             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
380     }
382     return ret;
385 static void
386 sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
388     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
390     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
392     if (!sc->item) {
394         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
395             return;
396         }
398         /* Create object */
399         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
400         repr->setAttribute("sodipodi:type", "spiral");
402         /* Set style */
403         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
405         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
406         Inkscape::GC::release(repr);
407         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
408         sc->item->updateRepr();
409     }
411     NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
412     NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
413     SnapManager const m(desktop->namedview);
414     p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
416     SPSpiral *spiral = SP_SPIRAL(sc->item);
418     NR::Point const delta = p1 - p0;
419     gdouble const rad = NR::L2(delta);
421     gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
423     if (state & GDK_CONTROL_MASK) {
424         arg = sp_round(arg, M_PI/snaps);
425     }
427     /* Fixme: these parameters should be got from dialog box */
428     sp_spiral_position_set(spiral, p0[NR::X], p0[NR::Y],
429                            /*expansion*/ sc->exp,
430                            /*revolution*/ sc->revo,
431                            rad, arg,
432                            /*t0*/ sc->t0);
434     /* status text */
435     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
436     sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
437                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
438                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
439     g_string_free(rads, FALSE);
442 static void
443 sp_spiral_finish(SPSpiralContext *sc)
445     sc->_message_context->clear();
447     if (sc->item != NULL) {
448         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
449         SPSpiral  *spiral = SP_SPIRAL(sc->item);
451         sp_shape_set_shape(SP_SHAPE(spiral));
452         SP_OBJECT(spiral)->updateRepr(NULL, SP_OBJECT_WRITE_EXT);
454         sp_desktop_selection(desktop)->set(sc->item);
455         sp_document_done(sp_desktop_document(desktop));
457         sc->item = NULL;
458     }
462 /*
463   Local Variables:
464   mode:c++
465   c-file-style:"stroustrup"
466   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
467   indent-tabs-mode:nil
468   fill-column:99
469   End:
470 */
471 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :