Code

User message context in extensions
[inkscape.git] / src / spiral-context.cpp
1 /*
2  * Spiral drawing context
3  *
4  * Authors:
5  *   Mitsuru Oka
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Jon A. Cruz <jon@joncruz.org>
9  *   Abhishek Sharma
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 "snap.h"
31 #include "desktop.h"
32 #include "desktop-style.h"
33 #include "message-context.h"
34 #include "pixmaps/cursor-spiral.xpm"
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 "preferences.h"
42 #include "context-fns.h"
43 #include "shape-editor.h"
45 using Inkscape::DocumentUndo;
47 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
48 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
49 static void sp_spiral_context_dispose(GObject *object);
50 static void sp_spiral_context_setup(SPEventContext *ec);
51 static void sp_spiral_context_finish(SPEventContext *ec);
52 static void sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
54 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
56 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
57 static void sp_spiral_finish(SPSpiralContext *sc);
58 static void sp_spiral_cancel(SPSpiralContext *sc);
60 static SPEventContextClass *parent_class;
62 GtkType
63 sp_spiral_context_get_type()
64 {
65     static GType type = 0;
66     if (!type) {
67         GTypeInfo info = {
68             sizeof(SPSpiralContextClass),
69             NULL, NULL,
70             (GClassInitFunc) sp_spiral_context_class_init,
71             NULL, NULL,
72             sizeof(SPSpiralContext),
73             4,
74             (GInstanceInitFunc) sp_spiral_context_init,
75             NULL,    /* value_table */
76         };
77         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
78     }
79     return type;
80 }
82 static void
83 sp_spiral_context_class_init(SPSpiralContextClass *klass)
84 {
85     GObjectClass *object_class = (GObjectClass *) klass;
86     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
90     object_class->dispose = sp_spiral_context_dispose;
92     event_context_class->setup = sp_spiral_context_setup;
93     event_context_class->finish = sp_spiral_context_finish;
94     event_context_class->set = sp_spiral_context_set;
95     event_context_class->root_handler = sp_spiral_context_root_handler;
96 }
98 static void
99 sp_spiral_context_init(SPSpiralContext *spiral_context)
101     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
103     event_context->cursor_shape = cursor_spiral_xpm;
104     event_context->hot_x = 4;
105     event_context->hot_y = 4;
106     event_context->xp = 0;
107     event_context->yp = 0;
108     event_context->tolerance = 0;
109     event_context->within_tolerance = false;
110     event_context->item_to_select = NULL;
112     spiral_context->item = NULL;
114     spiral_context->revo = 3.0;
115     spiral_context->exp = 1.0;
116     spiral_context->t0 = 0.0;
118     new (&spiral_context->sel_changed_connection) sigc::connection();
121 static void sp_spiral_context_finish(SPEventContext *ec)
123     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
124         SPDesktop *desktop = ec->desktop;
126         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
127         sp_spiral_finish(sc);
128     sc->sel_changed_connection.disconnect();
130     if (((SPEventContextClass *) parent_class)->finish) {
131                 ((SPEventContextClass *) parent_class)->finish(ec);
132         }
135 static void
136 sp_spiral_context_dispose(GObject *object)
138     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(object);
139     SPEventContext *ec = SP_EVENT_CONTEXT(object);
141     ec->enableGrDrag(false);
143     sc->sel_changed_connection.disconnect();
144     sc->sel_changed_connection.~connection();
146     delete ec->shape_editor;
147     ec->shape_editor = NULL;
149     /* fixme: This is necessary because we do not grab */
150     if (sc->item) sp_spiral_finish(sc);
152     if (sc->_message_context) {
153         delete sc->_message_context;
154     }
156     G_OBJECT_CLASS(parent_class)->dispose(object);
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     ec->shape_editor->unset_item(SH_KNOTHOLDER);
170     SPItem *item = selection->singleItem();
171     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
174 static void
175 sp_spiral_context_setup(SPEventContext *ec)
177     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
179     if (((SPEventContextClass *) parent_class)->setup)
180         ((SPEventContextClass *) parent_class)->setup(ec);
182     sp_event_context_read(ec, "expansion");
183     sp_event_context_read(ec, "revolution");
184     sp_event_context_read(ec, "t0");
186     ec->shape_editor = new ShapeEditor(ec->desktop);
188     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
189     if (item) {
190         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
191     }
193     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
194     sc->sel_changed_connection.disconnect();
195     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
197     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
198     if (prefs->getBool("/tools/shapes/selcue")) {
199         ec->enableSelectionCue();
200     }
201     if (prefs->getBool("/tools/shapes/gradientdrag")) {
202         ec->enableGrDrag();
203     }
205     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
208 static void
209 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
211     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
212     Glib::ustring name = val->getEntryName();
214     if (name == "expansion") {
215         sc->exp = CLAMP(val->getDouble(), 0.0, 1000.0);
216     } else if (name == "revolution") {
217         sc->revo = CLAMP(val->getDouble(3.0), 0.05, 40.0);
218     } else if (name == "t0") {
219         sc->t0 = CLAMP(val->getDouble(), 0.0, 0.999);
220     }
223 static gint
224 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
226     static gboolean dragging;
228     SPDesktop *desktop = event_context->desktop;
229     Inkscape::Selection *selection = sp_desktop_selection (desktop);
230     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
232     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
233     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
235     gint ret = FALSE;
237     switch (event->type) {
238         case GDK_BUTTON_PRESS:
239             if (event->button.button == 1 && !event_context->space_panning) {
241                 dragging = TRUE;
242                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
244                 SnapManager &m = desktop->namedview->snap_manager;
245                 m.setup(desktop);
246                 m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
247                 m.unSetup();
248                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
249                                     ( GDK_KEY_PRESS_MASK |
250                                       GDK_BUTTON_RELEASE_MASK |
251                                       GDK_POINTER_MOTION_MASK |
252                                       GDK_POINTER_MOTION_HINT_MASK |
253                                       GDK_BUTTON_PRESS_MASK    ),
254                                     NULL, event->button.time);
255                 ret = TRUE;
256             }
257             break;
258         case GDK_MOTION_NOTIFY:
259             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
261                 if ( event_context->within_tolerance
262                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
263                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
264                     break; // do not drag if we're within tolerance from origin
265                 }
266                 // Once the user has moved farther than tolerance from the original location
267                 // (indicating they intend to draw, not click), then always process the
268                 // motion notify coordinates as given (no snapping back to origin)
269                 event_context->within_tolerance = false;
271                 Geom::Point const motion_w(event->motion.x, event->motion.y);
272                 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
274                 SnapManager &m = desktop->namedview->snap_manager;
275                 m.setup(desktop, true, sc->item);
276                 m.freeSnapReturnByRef(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
277                 m.unSetup();
278                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
280                 gobble_motion_events(GDK_BUTTON1_MASK);
282                 ret = TRUE;
283             } else if (!sp_event_context_knot_mouseover(sc)) {
284                 SnapManager &m = desktop->namedview->snap_manager;
285                 m.setup(desktop);
286                 Geom::Point const motion_w(event->motion.x, event->motion.y);
287                 Geom::Point motion_dt(desktop->w2d(motion_w));
288                 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
289                 m.unSetup();
290             }
291             break;
292         case GDK_BUTTON_RELEASE:
293             event_context->xp = event_context->yp = 0;
294             if (event->button.button == 1 && !event_context->space_panning) {
295                 dragging = FALSE;
296                 sp_event_context_discard_delayed_snap_event(event_context);
297                 if (!event_context->within_tolerance) {
298                     // we've been dragging, finish the spiral
299                     sp_spiral_finish(sc);
300                 } else if (event_context->item_to_select) {
301                     // no dragging, select clicked item if any
302                     if (event->button.state & GDK_SHIFT_MASK) {
303                         selection->toggle(event_context->item_to_select);
304                     } else {
305                         selection->set(event_context->item_to_select);
306                     }
307                 } else {
308                     // click in an empty space
309                     selection->clear();
310                 }
312                 event_context->item_to_select = NULL;
313                 ret = TRUE;
314                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
315             }
316             break;
317         case GDK_KEY_PRESS:
318             switch (get_group0_keyval(&event->key)) {
319                 case GDK_Alt_R:
320                 case GDK_Control_L:
321                 case GDK_Control_R:
322                 case GDK_Shift_L:
323                 case GDK_Shift_R:
324                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
325                 case GDK_Meta_R:
326                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
327                                                _("<b>Ctrl</b>: snap angle"),
328                                                NULL,
329                                                _("<b>Alt</b>: lock spiral radius"));
330                     break;
331                 case GDK_Up:
332                 case GDK_Down:
333                 case GDK_KP_Up:
334                 case GDK_KP_Down:
335                     // prevent the zoom field from activation
336                     if (!MOD__CTRL_ONLY)
337                         ret = TRUE;
338                     break;
339                 case GDK_x:
340                 case GDK_X:
341                     if (MOD__ALT_ONLY) {
342                         desktop->setToolboxFocusTo ("altx-spiral");
343                         ret = TRUE;
344                     }
345                     break;
346                 case GDK_Escape:
347                         if (dragging) {
348                                 dragging = false;
349                                 sp_event_context_discard_delayed_snap_event(event_context);
350                                 // if drawing, cancel, otherwise pass it up for deselecting
351                                 sp_spiral_cancel(sc);
352                                 ret = TRUE;
353                         }
354                         break;
356                 case GDK_space:
357                     if (dragging) {
358                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
359                                               event->button.time);
360                         dragging = false;
361                         sp_event_context_discard_delayed_snap_event(event_context);
362                         if (!event_context->within_tolerance) {
363                             // we've been dragging, finish the spiral
364                             sp_spiral_finish(sc);
365                         }
366                         // do not return true, so that space would work switching to selector
367                     }
368                     break;
370                 default:
371                     break;
372             }
373             break;
374         case GDK_KEY_RELEASE:
375             switch (get_group0_keyval(&event->key)) {
376                 case GDK_Alt_L:
377                 case GDK_Alt_R:
378                 case GDK_Control_L:
379                 case GDK_Control_R:
380                 case GDK_Shift_L:
381                 case GDK_Shift_R:
382                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
383                 case GDK_Meta_R:
384                     event_context->defaultMessageContext()->clear();
385                     break;
386                 default:
387                     break;
388             }
389             break;
390         default:
391             break;
392     }
394     if (!ret) {
395         if (((SPEventContextClass *) parent_class)->root_handler)
396             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
397     }
399     return ret;
402 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
404     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
406     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
407     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
409     if (!sc->item) {
411         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
412             return;
413         }
415         // Create object
416         Inkscape::XML::Document *xml_doc = SP_EVENT_CONTEXT_DOCUMENT(sc)->getReprDoc();
417         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
418         repr->setAttribute("sodipodi:type", "spiral");
420         // Set style
421         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
423         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
424         Inkscape::GC::release(repr);
425         sc->item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
426         sc->item->updateRepr();
428         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
429     }
431     SnapManager &m = desktop->namedview->snap_manager;
432     m.setup(desktop, true, sc->item);
433     Geom::Point pt2g = to_2geom(p);
434     m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
435     m.unSetup();
436     Geom::Point const p0 = desktop->dt2doc(sc->center);
437     Geom::Point const p1 = desktop->dt2doc(pt2g);
439     SPSpiral *spiral = SP_SPIRAL(sc->item);
441     Geom::Point const delta = p1 - p0;
442     gdouble const rad = Geom::L2(delta);
444     gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
446     if (state & GDK_CONTROL_MASK) {
447         arg = sp_round(arg, M_PI/snaps);
448     }
450     /* Fixme: these parameters should be got from dialog box */
451     sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
452                            /*expansion*/ sc->exp,
453                            /*revolution*/ sc->revo,
454                            rad, arg,
455                            /*t0*/ sc->t0);
457     /* status text */
458     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
459     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
460                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
461                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
462     g_string_free(rads, FALSE);
465 static void
466 sp_spiral_finish(SPSpiralContext *sc)
468     sc->_message_context->clear();
470     if (sc->item != NULL) {
471         SPSpiral *spiral = SP_SPIRAL(sc->item);
472         if (spiral->rad == 0) {
473                 sp_spiral_cancel(sc); // Don't allow the creating of zero sized spiral, for example when the start and and point snap to the snap grid point
474                 return;
475         }
477         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
479         SP_SHAPE(spiral)->setShape();
480         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
482         sp_canvas_end_forced_full_redraws(desktop->canvas);
484         sp_desktop_selection(desktop)->set(sc->item);
485         DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
486                            _("Create spiral"));
488         sc->item = NULL;
489     }
492 static void sp_spiral_cancel(SPSpiralContext *sc)
494         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
496         sp_desktop_selection(desktop)->clear();
497         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
499     if (sc->item != NULL) {
500         SP_OBJECT(sc->item)->deleteObject();
501         sc->item = NULL;
502     }
504     sc->within_tolerance = false;
505     sc->xp = 0;
506     sc->yp = 0;
507     sc->item_to_select = NULL;
509     sp_canvas_end_forced_full_redraws(desktop->canvas);
511     DocumentUndo::cancel(sp_desktop_document(desktop));
514 /*
515   Local Variables:
516   mode:c++
517   c-file-style:"stroustrup"
518   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
519   indent-tabs-mode:nil
520   fill-column:99
521   End:
522 */
523 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :