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();
117 }
119 static void sp_spiral_context_finish(SPEventContext *ec)
120 {
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 }
131 }
133 static void
134 sp_spiral_context_dispose(GObject *object)
135 {
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);
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)
163 {
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);
170 }
172 static void
173 sp_spiral_context_setup(SPEventContext *ec)
174 {
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());
204 }
206 static void
207 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
208 {
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 }
219 }
221 static gint
222 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
223 {
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);
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 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
277 gobble_motion_events(GDK_BUTTON1_MASK);
279 ret = TRUE;
280 } else if (sp_event_context_knot_mouseover(sc)) {
281 SnapManager &m = desktop->namedview->snap_manager;
282 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 }
288 break;
289 case GDK_BUTTON_RELEASE:
290 event_context->xp = event_context->yp = 0;
291 if (event->button.button == 1 && !event_context->space_panning) {
292 dragging = FALSE;
293 sp_event_context_discard_delayed_snap_event(event_context);
294 if (!event_context->within_tolerance) {
295 // we've been dragging, finish the spiral
296 sp_spiral_finish(sc);
297 } else if (event_context->item_to_select) {
298 // no dragging, select clicked item if any
299 if (event->button.state & GDK_SHIFT_MASK) {
300 selection->toggle(event_context->item_to_select);
301 } else {
302 selection->set(event_context->item_to_select);
303 }
304 } else {
305 // click in an empty space
306 selection->clear();
307 }
309 event_context->item_to_select = NULL;
310 ret = TRUE;
311 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
312 }
313 break;
314 case GDK_KEY_PRESS:
315 switch (get_group0_keyval(&event->key)) {
316 case GDK_Alt_R:
317 case GDK_Control_L:
318 case GDK_Control_R:
319 case GDK_Shift_L:
320 case GDK_Shift_R:
321 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
322 case GDK_Meta_R:
323 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
324 _("<b>Ctrl</b>: snap angle"),
325 NULL,
326 _("<b>Alt</b>: lock spiral radius"));
327 break;
328 case GDK_Up:
329 case GDK_Down:
330 case GDK_KP_Up:
331 case GDK_KP_Down:
332 // prevent the zoom field from activation
333 if (!MOD__CTRL_ONLY)
334 ret = TRUE;
335 break;
336 case GDK_x:
337 case GDK_X:
338 if (MOD__ALT_ONLY) {
339 desktop->setToolboxFocusTo ("altx-spiral");
340 ret = TRUE;
341 }
342 break;
343 case GDK_Escape:
344 if (dragging) {
345 dragging = false;
346 sp_event_context_discard_delayed_snap_event(event_context);
347 // if drawing, cancel, otherwise pass it up for deselecting
348 sp_spiral_cancel(sc);
349 ret = TRUE;
350 }
351 break;
353 case GDK_space:
354 if (dragging) {
355 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
356 event->button.time);
357 dragging = false;
358 sp_event_context_discard_delayed_snap_event(event_context);
359 if (!event_context->within_tolerance) {
360 // we've been dragging, finish the spiral
361 sp_spiral_finish(sc);
362 }
363 // do not return true, so that space would work switching to selector
364 }
365 break;
367 default:
368 break;
369 }
370 break;
371 case GDK_KEY_RELEASE:
372 switch (get_group0_keyval(&event->key)) {
373 case GDK_Alt_L:
374 case GDK_Alt_R:
375 case GDK_Control_L:
376 case GDK_Control_R:
377 case GDK_Shift_L:
378 case GDK_Shift_R:
379 case GDK_Meta_L: // Meta is when you press Shift+Alt
380 case GDK_Meta_R:
381 event_context->defaultMessageContext()->clear();
382 break;
383 default:
384 break;
385 }
386 break;
387 default:
388 break;
389 }
391 if (!ret) {
392 if (((SPEventContextClass *) parent_class)->root_handler)
393 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
394 }
396 return ret;
397 }
399 static void
400 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
401 {
402 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
404 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
405 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
407 if (!sc->item) {
409 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
410 return;
411 }
413 /* Create object */
414 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
415 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
416 repr->setAttribute("sodipodi:type", "spiral");
418 /* Set style */
419 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
421 sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
422 Inkscape::GC::release(repr);
423 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
424 sc->item->updateRepr();
426 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
427 }
429 SnapManager &m = desktop->namedview->snap_manager;
430 m.setup(desktop, true, sc->item);
431 Geom::Point pt2g = to_2geom(p);
432 m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
434 Geom::Point const p0 = desktop->dt2doc(sc->center);
435 Geom::Point const p1 = desktop->dt2doc(pt2g);
437 SPSpiral *spiral = SP_SPIRAL(sc->item);
439 Geom::Point const delta = p1 - p0;
440 gdouble const rad = Geom::L2(delta);
442 gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
444 if (state & GDK_CONTROL_MASK) {
445 arg = sp_round(arg, M_PI/snaps);
446 }
448 /* Fixme: these parameters should be got from dialog box */
449 sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
450 /*expansion*/ sc->exp,
451 /*revolution*/ sc->revo,
452 rad, arg,
453 /*t0*/ sc->t0);
455 /* status text */
456 GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
457 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
458 _("<b>Spiral</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle"),
459 rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
460 g_string_free(rads, FALSE);
461 }
463 static void
464 sp_spiral_finish(SPSpiralContext *sc)
465 {
466 sc->_message_context->clear();
468 if (sc->item != NULL) {
469 SPSpiral *spiral = SP_SPIRAL(sc->item);
470 if (spiral->rad == 0) {
471 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
472 return;
473 }
475 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
477 sp_shape_set_shape(SP_SHAPE(spiral));
478 SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
480 sp_canvas_end_forced_full_redraws(desktop->canvas);
482 sp_desktop_selection(desktop)->set(sc->item);
483 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
484 _("Create spiral"));
486 sc->item = NULL;
487 }
488 }
490 static void sp_spiral_cancel(SPSpiralContext *sc)
491 {
492 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
494 sp_desktop_selection(desktop)->clear();
495 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
497 if (sc->item != NULL) {
498 SP_OBJECT(sc->item)->deleteObject();
499 sc->item = NULL;
500 }
502 sc->within_tolerance = false;
503 sc->xp = 0;
504 sc->yp = 0;
505 sc->item_to_select = NULL;
507 sp_canvas_end_forced_full_redraws(desktop->canvas);
509 sp_document_cancel(sp_desktop_document(desktop));
510 }
512 /*
513 Local Variables:
514 mode:c++
515 c-file-style:"stroustrup"
516 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
517 indent-tabs-mode:nil
518 fill-column:99
519 End:
520 */
521 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :