Code

now that selection description includes style (filtered, clipped), we need to update...
[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 "preferences.h"
43 #include "context-fns.h"
44 #include "shape-editor.h"
46 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
47 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
48 static void sp_spiral_context_dispose(GObject *object);
49 static void sp_spiral_context_setup(SPEventContext *ec);
50 static void sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
52 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
54 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
55 static void sp_spiral_finish(SPSpiralContext *sc);
57 static SPEventContextClass *parent_class;
59 GtkType
60 sp_spiral_context_get_type()
61 {
62     static GType type = 0;
63     if (!type) {
64         GTypeInfo info = {
65             sizeof(SPSpiralContextClass),
66             NULL, NULL,
67             (GClassInitFunc) sp_spiral_context_class_init,
68             NULL, NULL,
69             sizeof(SPSpiralContext),
70             4,
71             (GInstanceInitFunc) sp_spiral_context_init,
72             NULL,    /* value_table */
73         };
74         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
75     }
76     return type;
77 }
79 static void
80 sp_spiral_context_class_init(SPSpiralContextClass *klass)
81 {
82     GObjectClass *object_class = (GObjectClass *) klass;
83     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
85     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
87     object_class->dispose = sp_spiral_context_dispose;
89     event_context_class->setup = sp_spiral_context_setup;
90     event_context_class->set = sp_spiral_context_set;
91     event_context_class->root_handler = sp_spiral_context_root_handler;
92 }
94 static void
95 sp_spiral_context_init(SPSpiralContext *spiral_context)
96 {
97     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
99     event_context->cursor_shape = cursor_spiral_xpm;
100     event_context->hot_x = 4;
101     event_context->hot_y = 4;
102     event_context->xp = 0;
103     event_context->yp = 0;
104     event_context->tolerance = 0;
105     event_context->within_tolerance = false;
106     event_context->item_to_select = 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     delete ec->shape_editor;
129     ec->shape_editor = NULL;
131     /* fixme: This is necessary because we do not grab */
132     if (sc->item) sp_spiral_finish(sc);
134     if (sc->_message_context) {
135         delete sc->_message_context;
136     }
138     G_OBJECT_CLASS(parent_class)->dispose(object);
141 /**
142 \brief  Callback that processes the "changed" signal on the selection;
143 destroys old and creates new knotholder
144 */
145 void
146 sp_spiral_context_selection_changed(Inkscape::Selection *selection, gpointer data)
148     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
149     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
151     ec->shape_editor->unset_item(SH_KNOTHOLDER);
152     SPItem *item = selection->singleItem();
153     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
156 static void
157 sp_spiral_context_setup(SPEventContext *ec)
159     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
161     if (((SPEventContextClass *) parent_class)->setup)
162         ((SPEventContextClass *) parent_class)->setup(ec);
164     sp_event_context_read(ec, "expansion");
165     sp_event_context_read(ec, "revolution");
166     sp_event_context_read(ec, "t0");
168     ec->shape_editor = new ShapeEditor(ec->desktop);
170     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
171     if (item) {
172         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
173     }
175     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
176     sc->sel_changed_connection.disconnect();
177     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
179     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
180     if (prefs->getBool("/tools/shapes/selcue")) {
181         ec->enableSelectionCue();
182     }
183     if (prefs->getBool("/tools/shapes/gradientdrag")) {
184         ec->enableGrDrag();
185     }
187     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
190 static void
191 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
193     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
194     Glib::ustring name = val->getEntryName();
196     if (name == "expansion") {
197         sc->exp = CLAMP(val->getDouble(), 0.0, 1000.0);
198     } else if (name == "revolution") {
199         sc->revo = CLAMP(val->getDouble(3.0), 0.05, 40.0);
200     } else if (name == "t0") {
201         sc->t0 = CLAMP(val->getDouble(), 0.0, 0.999);
202     }
205 static gint
206 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
208     static gboolean dragging;
210     SPDesktop *desktop = event_context->desktop;
211     Inkscape::Selection *selection = sp_desktop_selection (desktop);
212     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
214     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
215     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
217     gint ret = FALSE;
219     switch (event->type) {
220         case GDK_BUTTON_PRESS:
221             if (event->button.button == 1 && !event_context->space_panning) {
223                 dragging = TRUE;
224                 sp_canvas_set_snap_delay_active(desktop->canvas, true);
225                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
227                 SnapManager &m = desktop->namedview->snap_manager;
228                 m.setup(desktop);
229                 Geom::Point pt2g = to_2geom(sc->center);
230                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
231                 sc->center = from_2geom(pt2g);
233                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
234                                     ( GDK_KEY_PRESS_MASK |
235                                       GDK_BUTTON_RELEASE_MASK |
236                                       GDK_POINTER_MOTION_MASK |
237                                       GDK_POINTER_MOTION_HINT_MASK |
238                                       GDK_BUTTON_PRESS_MASK    ),
239                                     NULL, event->button.time);
240                 ret = TRUE;
241             }
242             break;
243         case GDK_MOTION_NOTIFY:
244             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
246                 if ( event_context->within_tolerance
247                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
248                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
249                     break; // do not drag if we're within tolerance from origin
250                 }
251                 // Once the user has moved farther than tolerance from the original location
252                 // (indicating they intend to draw, not click), then always process the
253                 // motion notify coordinates as given (no snapping back to origin)
254                 event_context->within_tolerance = false;
256                 Geom::Point const motion_w(event->motion.x, event->motion.y);
257                 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
259                 SnapManager &m = desktop->namedview->snap_manager;
260                 m.setup(desktop, true, sc->item);
261                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt);
262                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
264                 gobble_motion_events(GDK_BUTTON1_MASK);
266                 ret = TRUE;
267             }
268             break;
269         case GDK_BUTTON_RELEASE:
270             event_context->xp = event_context->yp = 0;
271             if (event->button.button == 1 && !event_context->space_panning) {
272                 dragging = FALSE;
273                 sp_canvas_set_snap_delay_active(desktop->canvas, false);
274                 if (!event_context->within_tolerance) {
275                     // we've been dragging, finish the spiral
276                     sp_spiral_finish(sc);
277                 } else if (event_context->item_to_select) {
278                     // no dragging, select clicked item if any
279                     if (event->button.state & GDK_SHIFT_MASK) {
280                         selection->toggle(event_context->item_to_select);
281                     } else {
282                         selection->set(event_context->item_to_select);
283                     }
284                 } else {
285                     // click in an empty space
286                     selection->clear();
287                 }
289                 event_context->item_to_select = NULL;
290                 ret = TRUE;
291                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
292             }
293             break;
294         case GDK_KEY_PRESS:
295             switch (get_group0_keyval(&event->key)) {
296                 case GDK_Alt_R:
297                 case GDK_Control_L:
298                 case GDK_Control_R:
299                 case GDK_Shift_L:
300                 case GDK_Shift_R:
301                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
302                 case GDK_Meta_R:
303                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
304                                                _("<b>Ctrl</b>: snap angle"),
305                                                NULL,
306                                                _("<b>Alt</b>: lock spiral radius"));
307                     break;
308                 case GDK_Up:
309                 case GDK_Down:
310                 case GDK_KP_Up:
311                 case GDK_KP_Down:
312                     // prevent the zoom field from activation
313                     if (!MOD__CTRL_ONLY)
314                         ret = TRUE;
315                     break;
316                 case GDK_x:
317                 case GDK_X:
318                     if (MOD__ALT_ONLY) {
319                         desktop->setToolboxFocusTo ("altx-spiral");
320                         ret = TRUE;
321                     }
322                     break;
323                 case GDK_Escape:
324                     sp_desktop_selection(desktop)->clear();
325                     //TODO: make dragging escapable by Esc
326                     break;
328                 case GDK_space:
329                     if (dragging) {
330                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
331                                               event->button.time);
332                         dragging = false;
333                         sp_canvas_set_snap_delay_active(desktop->canvas, false);
334                         if (!event_context->within_tolerance) {
335                             // we've been dragging, finish the rect
336                             sp_spiral_finish(sc);
337                         }
338                         // do not return true, so that space would work switching to selector
339                     }
340                     break;
342                 default:
343                     break;
344             }
345             break;
346         case GDK_KEY_RELEASE:
347             switch (get_group0_keyval(&event->key)) {
348                 case GDK_Alt_L:
349                 case GDK_Alt_R:
350                 case GDK_Control_L:
351                 case GDK_Control_R:
352                 case GDK_Shift_L:
353                 case GDK_Shift_R:
354                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
355                 case GDK_Meta_R:
356                     event_context->defaultMessageContext()->clear();
357                     break;
358                 default:
359                     break;
360             }
361             break;
362         default:
363             break;
364     }
366     if (!ret) {
367         if (((SPEventContextClass *) parent_class)->root_handler)
368             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
369     }
371     return ret;
374 static void
375 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
377     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
379     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
380     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
382     if (!sc->item) {
384         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
385             return;
386         }
388         /* Create object */
389         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
390         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
391         repr->setAttribute("sodipodi:type", "spiral");
393         /* Set style */
394         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
396         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
397         Inkscape::GC::release(repr);
398         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
399         sc->item->updateRepr();
401         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
402     }
404     SnapManager &m = desktop->namedview->snap_manager;
405     m.setup(desktop, true, sc->item);
406     Geom::Point pt2g = to_2geom(p);
407     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
409     Geom::Point const p0 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, sc->center));
410     Geom::Point const p1 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, from_2geom(pt2g)));
412     SPSpiral *spiral = SP_SPIRAL(sc->item);
414     Geom::Point const delta = p1 - p0;
415     gdouble const rad = Geom::L2(delta);
417     gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
419     if (state & GDK_CONTROL_MASK) {
420         arg = sp_round(arg, M_PI/snaps);
421     }
423     /* Fixme: these parameters should be got from dialog box */
424     sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
425                            /*expansion*/ sc->exp,
426                            /*revolution*/ sc->revo,
427                            rad, arg,
428                            /*t0*/ sc->t0);
430     /* status text */
431     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
432     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
433                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
434                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
435     g_string_free(rads, FALSE);
438 static void
439 sp_spiral_finish(SPSpiralContext *sc)
441     sc->_message_context->clear();
443     if (sc->item != NULL) {
444         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
445         SPSpiral  *spiral = SP_SPIRAL(sc->item);
447         sp_shape_set_shape(SP_SHAPE(spiral));
448         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
450         sp_canvas_end_forced_full_redraws(desktop->canvas);
452         sp_desktop_selection(desktop)->set(sc->item);
453         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
454                          _("Create spiral"));
456         sc->item = NULL;
457     }
461 /*
462   Local Variables:
463   mode:c++
464   c-file-style:"stroustrup"
465   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
466   indent-tabs-mode:nil
467   fill-column:99
468   End:
469 */
470 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :