Code

Pot and Dutch translation 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 "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 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_finish(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);
56 static void sp_spiral_cancel(SPSpiralContext *sc);
58 static SPEventContextClass *parent_class;
60 GtkType
61 sp_spiral_context_get_type()
62 {
63     static GType type = 0;
64     if (!type) {
65         GTypeInfo info = {
66             sizeof(SPSpiralContextClass),
67             NULL, NULL,
68             (GClassInitFunc) sp_spiral_context_class_init,
69             NULL, NULL,
70             sizeof(SPSpiralContext),
71             4,
72             (GInstanceInitFunc) sp_spiral_context_init,
73             NULL,    /* value_table */
74         };
75         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
76     }
77     return type;
78 }
80 static void
81 sp_spiral_context_class_init(SPSpiralContextClass *klass)
82 {
83     GObjectClass *object_class = (GObjectClass *) klass;
84     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
86     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
88     object_class->dispose = sp_spiral_context_dispose;
90     event_context_class->setup = sp_spiral_context_setup;
91     event_context_class->finish = sp_spiral_context_finish;
92     event_context_class->set = sp_spiral_context_set;
93     event_context_class->root_handler = sp_spiral_context_root_handler;
94 }
96 static void
97 sp_spiral_context_init(SPSpiralContext *spiral_context)
98 {
99     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
101     event_context->cursor_shape = cursor_spiral_xpm;
102     event_context->hot_x = 4;
103     event_context->hot_y = 4;
104     event_context->xp = 0;
105     event_context->yp = 0;
106     event_context->tolerance = 0;
107     event_context->within_tolerance = false;
108     event_context->item_to_select = 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 sp_spiral_context_finish(SPEventContext *ec)
121     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
122         SPDesktop *desktop = ec->desktop;
124         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
125         sp_spiral_finish(sc);
126     sc->sel_changed_connection.disconnect();
128     if (((SPEventContextClass *) parent_class)->finish) {
129                 ((SPEventContextClass *) parent_class)->finish(ec);
130         }
133 static void
134 sp_spiral_context_dispose(GObject *object)
136     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(object);
137     SPEventContext *ec = SP_EVENT_CONTEXT(object);
139     ec->enableGrDrag(false);
141     sc->sel_changed_connection.disconnect();
142     sc->sel_changed_connection.~connection();
144     delete ec->shape_editor;
145     ec->shape_editor = NULL;
147     /* fixme: This is necessary because we do not grab */
148     if (sc->item) sp_spiral_finish(sc);
150     if (sc->_message_context) {
151         delete sc->_message_context;
152     }
154     G_OBJECT_CLASS(parent_class)->dispose(object);
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     ec->shape_editor->unset_item(SH_KNOTHOLDER);
168     SPItem *item = selection->singleItem();
169     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
172 static void
173 sp_spiral_context_setup(SPEventContext *ec)
175     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
177     if (((SPEventContextClass *) parent_class)->setup)
178         ((SPEventContextClass *) parent_class)->setup(ec);
180     sp_event_context_read(ec, "expansion");
181     sp_event_context_read(ec, "revolution");
182     sp_event_context_read(ec, "t0");
184     ec->shape_editor = new ShapeEditor(ec->desktop);
186     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
187     if (item) {
188         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
189     }
191     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
192     sc->sel_changed_connection.disconnect();
193     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
195     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
196     if (prefs->getBool("/tools/shapes/selcue")) {
197         ec->enableSelectionCue();
198     }
199     if (prefs->getBool("/tools/shapes/gradientdrag")) {
200         ec->enableGrDrag();
201     }
203     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
206 static void
207 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
209     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
210     Glib::ustring name = val->getEntryName();
212     if (name == "expansion") {
213         sc->exp = CLAMP(val->getDouble(), 0.0, 1000.0);
214     } else if (name == "revolution") {
215         sc->revo = CLAMP(val->getDouble(3.0), 0.05, 40.0);
216     } else if (name == "t0") {
217         sc->t0 = CLAMP(val->getDouble(), 0.0, 0.999);
218     }
221 static gint
222 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
224     static gboolean dragging;
226     SPDesktop *desktop = event_context->desktop;
227     Inkscape::Selection *selection = sp_desktop_selection (desktop);
228     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
230     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
231     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
233     gint ret = FALSE;
235     switch (event->type) {
236         case GDK_BUTTON_PRESS:
237             if (event->button.button == 1 && !event_context->space_panning) {
239                 dragging = TRUE;
240                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
242                 SnapManager &m = desktop->namedview->snap_manager;
243                 m.setup(desktop);
244                 m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
245                 m.unSetup();
246                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
247                                     ( GDK_KEY_PRESS_MASK |
248                                       GDK_BUTTON_RELEASE_MASK |
249                                       GDK_POINTER_MOTION_MASK |
250                                       GDK_POINTER_MOTION_HINT_MASK |
251                                       GDK_BUTTON_PRESS_MASK    ),
252                                     NULL, event->button.time);
253                 ret = TRUE;
254             }
255             break;
256         case GDK_MOTION_NOTIFY:
257             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
259                 if ( event_context->within_tolerance
260                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
261                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
262                     break; // do not drag if we're within tolerance from origin
263                 }
264                 // Once the user has moved farther than tolerance from the original location
265                 // (indicating they intend to draw, not click), then always process the
266                 // motion notify coordinates as given (no snapping back to origin)
267                 event_context->within_tolerance = false;
269                 Geom::Point const motion_w(event->motion.x, event->motion.y);
270                 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
272                 SnapManager &m = desktop->namedview->snap_manager;
273                 m.setup(desktop, true, sc->item);
274                 m.freeSnapReturnByRef(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
275                 m.unSetup();
276                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
278                 gobble_motion_events(GDK_BUTTON1_MASK);
280                 ret = TRUE;
281             } else if (!sp_event_context_knot_mouseover(sc)) {
282                 SnapManager &m = desktop->namedview->snap_manager;
283                 m.setup(desktop);
284                 Geom::Point const motion_w(event->motion.x, event->motion.y);
285                 Geom::Point motion_dt(desktop->w2d(motion_w));
286                 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
287                 m.unSetup();
288             }
289             break;
290         case GDK_BUTTON_RELEASE:
291             event_context->xp = event_context->yp = 0;
292             if (event->button.button == 1 && !event_context->space_panning) {
293                 dragging = FALSE;
294                 sp_event_context_discard_delayed_snap_event(event_context);
295                 if (!event_context->within_tolerance) {
296                     // we've been dragging, finish the spiral
297                     sp_spiral_finish(sc);
298                 } else if (event_context->item_to_select) {
299                     // no dragging, select clicked item if any
300                     if (event->button.state & GDK_SHIFT_MASK) {
301                         selection->toggle(event_context->item_to_select);
302                     } else {
303                         selection->set(event_context->item_to_select);
304                     }
305                 } else {
306                     // click in an empty space
307                     selection->clear();
308                 }
310                 event_context->item_to_select = NULL;
311                 ret = TRUE;
312                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
313             }
314             break;
315         case GDK_KEY_PRESS:
316             switch (get_group0_keyval(&event->key)) {
317                 case GDK_Alt_R:
318                 case GDK_Control_L:
319                 case GDK_Control_R:
320                 case GDK_Shift_L:
321                 case GDK_Shift_R:
322                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
323                 case GDK_Meta_R:
324                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
325                                                _("<b>Ctrl</b>: snap angle"),
326                                                NULL,
327                                                _("<b>Alt</b>: lock spiral radius"));
328                     break;
329                 case GDK_Up:
330                 case GDK_Down:
331                 case GDK_KP_Up:
332                 case GDK_KP_Down:
333                     // prevent the zoom field from activation
334                     if (!MOD__CTRL_ONLY)
335                         ret = TRUE;
336                     break;
337                 case GDK_x:
338                 case GDK_X:
339                     if (MOD__ALT_ONLY) {
340                         desktop->setToolboxFocusTo ("altx-spiral");
341                         ret = TRUE;
342                     }
343                     break;
344                 case GDK_Escape:
345                         if (dragging) {
346                                 dragging = false;
347                                 sp_event_context_discard_delayed_snap_event(event_context);
348                                 // if drawing, cancel, otherwise pass it up for deselecting
349                                 sp_spiral_cancel(sc);
350                                 ret = TRUE;
351                         }
352                         break;
354                 case GDK_space:
355                     if (dragging) {
356                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
357                                               event->button.time);
358                         dragging = false;
359                         sp_event_context_discard_delayed_snap_event(event_context);
360                         if (!event_context->within_tolerance) {
361                             // we've been dragging, finish the spiral
362                             sp_spiral_finish(sc);
363                         }
364                         // do not return true, so that space would work switching to selector
365                     }
366                     break;
368                 default:
369                     break;
370             }
371             break;
372         case GDK_KEY_RELEASE:
373             switch (get_group0_keyval(&event->key)) {
374                 case GDK_Alt_L:
375                 case GDK_Alt_R:
376                 case GDK_Control_L:
377                 case GDK_Control_R:
378                 case GDK_Shift_L:
379                 case GDK_Shift_R:
380                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
381                 case GDK_Meta_R:
382                     event_context->defaultMessageContext()->clear();
383                     break;
384                 default:
385                     break;
386             }
387             break;
388         default:
389             break;
390     }
392     if (!ret) {
393         if (((SPEventContextClass *) parent_class)->root_handler)
394             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
395     }
397     return ret;
400 static void
401 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
403     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
405     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
406     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
408     if (!sc->item) {
410         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
411             return;
412         }
414         /* Create object */
415         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
416         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
417         repr->setAttribute("sodipodi:type", "spiral");
419         /* Set style */
420         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
422         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
423         Inkscape::GC::release(repr);
424         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
425         sc->item->updateRepr();
427         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
428     }
430     SnapManager &m = desktop->namedview->snap_manager;
431     m.setup(desktop, true, sc->item);
432     Geom::Point pt2g = to_2geom(p);
433     m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
434     m.unSetup();
435     Geom::Point const p0 = desktop->dt2doc(sc->center);
436     Geom::Point const p1 = desktop->dt2doc(pt2g);
438     SPSpiral *spiral = SP_SPIRAL(sc->item);
440     Geom::Point const delta = p1 - p0;
441     gdouble const rad = Geom::L2(delta);
443     gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
445     if (state & GDK_CONTROL_MASK) {
446         arg = sp_round(arg, M_PI/snaps);
447     }
449     /* Fixme: these parameters should be got from dialog box */
450     sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
451                            /*expansion*/ sc->exp,
452                            /*revolution*/ sc->revo,
453                            rad, arg,
454                            /*t0*/ sc->t0);
456     /* status text */
457     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
458     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
459                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
460                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
461     g_string_free(rads, FALSE);
464 static void
465 sp_spiral_finish(SPSpiralContext *sc)
467     sc->_message_context->clear();
469     if (sc->item != NULL) {
470         SPSpiral *spiral = SP_SPIRAL(sc->item);
471         if (spiral->rad == 0) {
472                 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
473                 return;
474         }
476         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
478         sp_shape_set_shape(SP_SHAPE(spiral));
479         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
481         sp_canvas_end_forced_full_redraws(desktop->canvas);
483         sp_desktop_selection(desktop)->set(sc->item);
484         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
485                          _("Create spiral"));
487         sc->item = NULL;
488     }
491 static void sp_spiral_cancel(SPSpiralContext *sc)
493         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
495         sp_desktop_selection(desktop)->clear();
496         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
498     if (sc->item != NULL) {
499         SP_OBJECT(sc->item)->deleteObject();
500         sc->item = NULL;
501     }
503     sc->within_tolerance = false;
504     sc->xp = 0;
505     sc->yp = 0;
506     sc->item_to_select = NULL;
508     sp_canvas_end_forced_full_redraws(desktop->canvas);
510     sp_document_cancel(sp_desktop_document(desktop));
513 /*
514   Local Variables:
515   mode:c++
516   c-file-style:"stroustrup"
517   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
518   indent-tabs-mode:nil
519   fill-column:99
520   End:
521 */
522 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :