Code

1e341e3d1812b3ea6062b2df73c19a570277ae55
[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>
20 #include <cstring>
21 #include <string>
23 #include "macros.h"
24 #include "display/sp-canvas.h"
25 #include "sp-spiral.h"
26 #include "document.h"
27 #include "sp-namedview.h"
28 #include "selection.h"
29 #include "desktop-handles.h"
30 #include "desktop-affine.h"
31 #include "snap.h"
32 #include "desktop.h"
33 #include "desktop-style.h"
34 #include "message-context.h"
35 #include "pixmaps/cursor-spiral.xpm"
36 #include "spiral-context.h"
37 #include "sp-metrics.h"
38 #include <glibmm/i18n.h>
39 #include "object-edit.h"
40 #include "xml/repr.h"
41 #include "xml/node-event-vector.h"
42 #include "prefs-utils.h"
43 #include "context-fns.h"
45 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
46 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
47 static void sp_spiral_context_dispose(GObject *object);
48 static void sp_spiral_context_setup(SPEventContext *ec);
49 static void sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
51 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
53 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
54 static void sp_spiral_finish(SPSpiralContext *sc);
56 static SPEventContextClass *parent_class;
58 GtkType
59 sp_spiral_context_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SPSpiralContextClass),
65             NULL, NULL,
66             (GClassInitFunc) sp_spiral_context_class_init,
67             NULL, NULL,
68             sizeof(SPSpiralContext),
69             4,
70             (GInstanceInitFunc) sp_spiral_context_init,
71             NULL,    /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
74     }
75     return type;
76 }
78 static void
79 sp_spiral_context_class_init(SPSpiralContextClass *klass)
80 {
81     GObjectClass *object_class = (GObjectClass *) klass;
82     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
84     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
86     object_class->dispose = sp_spiral_context_dispose;
88     event_context_class->setup = sp_spiral_context_setup;
89     event_context_class->set = sp_spiral_context_set;
90     event_context_class->root_handler = sp_spiral_context_root_handler;
91 }
93 static void
94 sp_spiral_context_init(SPSpiralContext *spiral_context)
95 {
96     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
98     event_context->cursor_shape = cursor_spiral_xpm;
99     event_context->hot_x = 4;
100     event_context->hot_y = 4;
101     event_context->xp = 0;
102     event_context->yp = 0;
103     event_context->tolerance = 0;
104     event_context->within_tolerance = false;
105     event_context->item_to_select = NULL;
107     event_context->shape_repr = NULL;
108     event_context->shape_knot_holder = NULL;
110     spiral_context->item = NULL;
112     spiral_context->revo = 3.0;
113     spiral_context->exp = 1.0;
114     spiral_context->t0 = 0.0;
116     new (&spiral_context->sel_changed_connection) sigc::connection();
119 static void
120 sp_spiral_context_dispose(GObject *object)
122     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(object);
123     SPEventContext *ec = SP_EVENT_CONTEXT(object);
125     ec->enableGrDrag(false);
127     sc->sel_changed_connection.disconnect();
128     sc->sel_changed_connection.~connection();
130     /* fixme: This is necessary because we do not grab */
131     if (sc->item) sp_spiral_finish(sc);
133     if (ec->shape_knot_holder) {
134         delete ec->shape_knot_holder;
135         ec->shape_knot_holder = NULL;
136     }
138     if (ec->shape_repr) { // remove old listener
139         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
140         Inkscape::GC::release(ec->shape_repr);
141         ec->shape_repr = 0;
142     }
144     if (sc->_message_context) {
145         delete sc->_message_context;
146     }
148     G_OBJECT_CLASS(parent_class)->dispose(object);
151 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
152     NULL, /* child_added */
153     NULL, /* child_removed */
154     ec_shape_event_attr_changed,
155     NULL, /* content_changed */
156     NULL  /* order_changed */
157 };
159 /**
160 \brief  Callback that processes the "changed" signal on the selection;
161 destroys old and creates new knotholder
162 */
163 void
164 sp_spiral_context_selection_changed(Inkscape::Selection *selection, gpointer data)
166     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
167     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
169     if (ec->shape_knot_holder) { // desktroy knotholder
170         delete ec->shape_knot_holder;
171         ec->shape_knot_holder = NULL;
172     }
174     if (ec->shape_repr) { // remove old listener
175         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
176         Inkscape::GC::release(ec->shape_repr);
177         ec->shape_repr = 0;
178     }
180     SPItem *item = selection->singleItem();
181     if (item) {
182         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
183         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
184         if (shape_repr) {
185             ec->shape_repr = shape_repr;
186             Inkscape::GC::anchor(shape_repr);
187             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
188         }
189     }
192 static void
193 sp_spiral_context_setup(SPEventContext *ec)
195     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
197     if (((SPEventContextClass *) parent_class)->setup)
198         ((SPEventContextClass *) parent_class)->setup(ec);
200     sp_event_context_read(ec, "expansion");
201     sp_event_context_read(ec, "revolution");
202     sp_event_context_read(ec, "t0");
204     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
206     SPItem *item = selection->singleItem();
207     if (item) {
208         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
209         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
210         if (shape_repr) {
211             ec->shape_repr = shape_repr;
212             Inkscape::GC::anchor(shape_repr);
213             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
214         }
215     }
217     sc->sel_changed_connection.disconnect();
218     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
220     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
221         ec->enableSelectionCue();
222     }
224     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
225         ec->enableGrDrag();
226     }
228     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
231 static void
232 sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
234     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
236     if (!strcmp(key, "expansion")) {
237         sc->exp = (val) ? g_ascii_strtod(val, NULL) : 1.0;
238         sc->exp = CLAMP(sc->exp, 0.0, 1000.0);
239     } else if (!strcmp(key, "revolution")) {
240         sc->revo = (val) ? g_ascii_strtod(val, NULL) : 3.0;
241         sc->revo = CLAMP(sc->revo, 0.05, 40.0);
242     } else if (!strcmp(key, "t0")) {
243         sc->t0 = (val) ? g_ascii_strtod(val, NULL) : 0.0;
244         sc->t0 = CLAMP(sc->t0, 0.0, 0.999);
245     }
248 static gint
249 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
251     static gboolean dragging;
253     SPDesktop *desktop = event_context->desktop;
254     Inkscape::Selection *selection = sp_desktop_selection (desktop);
255     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
257     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
259     gint ret = FALSE;
261     switch (event->type) {
262         case GDK_BUTTON_PRESS:
263             if (event->button.button == 1 && !event_context->space_panning) {
265                 dragging = TRUE;
266                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
268                 SnapManager &m = desktop->namedview->snap_manager;
269                 m.setup(desktop);
270                 Geom::Point pt2g = to_2geom(sc->center);
271                 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
272                 sc->center = from_2geom(pt2g);
274                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
275                                     ( GDK_KEY_PRESS_MASK |
276                                       GDK_BUTTON_RELEASE_MASK |
277                                       GDK_POINTER_MOTION_MASK |
278                                       GDK_POINTER_MOTION_HINT_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) && !event_context->space_panning) {
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                 Geom::Point const motion_w(event->motion.x, event->motion.y);
298                 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
299                 
300                 SnapManager &m = desktop->namedview->snap_manager;
301                 m.setup(desktop, true, sc->item);
302                 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt);
303                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
305                 gobble_motion_events(GDK_BUTTON1_MASK);
307                 ret = TRUE;
308             }
309             break;
310         case GDK_BUTTON_RELEASE:
311             event_context->xp = event_context->yp = 0;
312             if (event->button.button == 1 && !event_context->space_panning) {
313                 dragging = FALSE;
314                 if (!event_context->within_tolerance) {
315                     // we've been dragging, finish the spiral
316                     sp_spiral_finish(sc);
317                 } else if (event_context->item_to_select) {
318                     // no dragging, select clicked item if any
319                     if (event->button.state & GDK_SHIFT_MASK) {
320                         selection->toggle(event_context->item_to_select);
321                     } else {
322                         selection->set(event_context->item_to_select);
323                     }
324                 } else {
325                     // click in an empty space
326                     selection->clear();
327                 }
329                 event_context->item_to_select = NULL;
330                 ret = TRUE;
331                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
332             }
333             break;
334         case GDK_KEY_PRESS:
335             switch (get_group0_keyval(&event->key)) {
336                 case GDK_Alt_R:
337                 case GDK_Control_L:
338                 case GDK_Control_R:
339                 case GDK_Shift_L:
340                 case GDK_Shift_R:
341                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
342                 case GDK_Meta_R:
343                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
344                                                _("<b>Ctrl</b>: snap angle"),
345                                                NULL,
346                                                _("<b>Alt</b>: lock spiral radius"));
347                     break;
348                 case GDK_Up:
349                 case GDK_Down:
350                 case GDK_KP_Up:
351                 case GDK_KP_Down:
352                     // prevent the zoom field from activation
353                     if (!MOD__CTRL_ONLY)
354                         ret = TRUE;
355                     break;
356                 case GDK_x:
357                 case GDK_X:
358                     if (MOD__ALT_ONLY) {
359                         desktop->setToolboxFocusTo ("altx-spiral");
360                         ret = TRUE;
361                     }
362                     break;
363                 case GDK_Escape:
364                     sp_desktop_selection(desktop)->clear();
365                     //TODO: make dragging escapable by Esc
366                     break;
368                 case GDK_space:
369                     if (dragging) {
370                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
371                                               event->button.time);
372                         dragging = false;
373                         if (!event_context->within_tolerance) {
374                             // we've been dragging, finish the rect
375                             sp_spiral_finish(sc);
376                         }
377                         // do not return true, so that space would work switching to selector
378                     }
379                     break;
381                 default:
382                     break;
383             }
384             break;
385         case GDK_KEY_RELEASE:
386             switch (get_group0_keyval(&event->key)) {
387                 case GDK_Alt_L:
388                 case GDK_Alt_R:
389                 case GDK_Control_L:
390                 case GDK_Control_R:
391                 case GDK_Shift_L:
392                 case GDK_Shift_R:
393                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
394                 case GDK_Meta_R:
395                     event_context->defaultMessageContext()->clear();
396                     break;
397                 default:
398                     break;
399             }
400             break;
401         default:
402             break;
403     }
405     if (!ret) {
406         if (((SPEventContextClass *) parent_class)->root_handler)
407             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
408     }
410     return ret;
413 static void
414 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
416     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
418     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
420     if (!sc->item) {
422         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
423             return;
424         }
426         /* Create object */
427         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
428         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
429         repr->setAttribute("sodipodi:type", "spiral");
431         /* Set style */
432         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
434         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
435         Inkscape::GC::release(repr);
436         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
437         sc->item->updateRepr();
439         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
440     }
442     SnapManager &m = desktop->namedview->snap_manager;
443     m.setup(desktop, true, sc->item);
444     Geom::Point pt2g = to_2geom(p);
445     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
447     Geom::Point const p0 = to_2geom(sp_desktop_dt2root_xy_point(desktop, sc->center));
448     Geom::Point const p1 = to_2geom(sp_desktop_dt2root_xy_point(desktop, from_2geom(pt2g)));        
449     
450     SPSpiral *spiral = SP_SPIRAL(sc->item);
452     Geom::Point const delta = p1 - p0;
453     gdouble const rad = Geom::L2(delta);
455     gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
457     if (state & GDK_CONTROL_MASK) {
458         arg = sp_round(arg, M_PI/snaps);
459     }
461     /* Fixme: these parameters should be got from dialog box */
462     sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
463                            /*expansion*/ sc->exp,
464                            /*revolution*/ sc->revo,
465                            rad, arg,
466                            /*t0*/ sc->t0);
468     /* status text */
469     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
470     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
471                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
472                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
473     g_string_free(rads, FALSE);
476 static void
477 sp_spiral_finish(SPSpiralContext *sc)
479     sc->_message_context->clear();
481     if (sc->item != NULL) {
482         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
483         SPSpiral  *spiral = SP_SPIRAL(sc->item);
485         sp_shape_set_shape(SP_SHAPE(spiral));
486         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
488         sp_canvas_end_forced_full_redraws(desktop->canvas);
490         sp_desktop_selection(desktop)->set(sc->item);
491         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL, 
492                          _("Create spiral"));
494         sc->item = NULL;
495     }
499 /*
500   Local Variables:
501   mode:c++
502   c-file-style:"stroustrup"
503   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
504   indent-tabs-mode:nil
505   fill-column:99
506   End:
507 */
508 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :