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