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_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
51 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
53 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
54 static void sp_spiral_finish(SPSpiralContext *sc);
55 static void sp_spiral_cancel(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();
115 }
117 static void
118 sp_spiral_context_dispose(GObject *object)
119 {
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);
139 }
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)
147 {
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);
154 }
156 static void
157 sp_spiral_context_setup(SPEventContext *ec)
158 {
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());
188 }
190 static void
191 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
192 {
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 }
203 }
205 static gint
206 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
207 {
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_event_context_snap_window_open(event_context);
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, Inkscape::SNAPSOURCE_HANDLE);
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, Inkscape::SNAPSOURCE_HANDLE);
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_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
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 if (dragging) {
325 dragging = false;
326 sp_event_context_snap_window_closed(event_context);
327 // if drawing, cancel, otherwise pass it up for deselecting
328 sp_spiral_cancel(sc);
329 ret = TRUE;
330 }
331 break;
333 case GDK_space:
334 if (dragging) {
335 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
336 event->button.time);
337 dragging = false;
338 sp_event_context_snap_window_closed(event_context);
339 if (!event_context->within_tolerance) {
340 // we've been dragging, finish the spiral
341 sp_spiral_finish(sc);
342 }
343 // do not return true, so that space would work switching to selector
344 }
345 break;
347 default:
348 break;
349 }
350 break;
351 case GDK_KEY_RELEASE:
352 switch (get_group0_keyval(&event->key)) {
353 case GDK_Alt_L:
354 case GDK_Alt_R:
355 case GDK_Control_L:
356 case GDK_Control_R:
357 case GDK_Shift_L:
358 case GDK_Shift_R:
359 case GDK_Meta_L: // Meta is when you press Shift+Alt
360 case GDK_Meta_R:
361 event_context->defaultMessageContext()->clear();
362 break;
363 default:
364 break;
365 }
366 break;
367 default:
368 break;
369 }
371 if (!ret) {
372 if (((SPEventContextClass *) parent_class)->root_handler)
373 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
374 }
376 return ret;
377 }
379 static void
380 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
381 {
382 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
384 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
385 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
387 if (!sc->item) {
389 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
390 return;
391 }
393 /* Create object */
394 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
395 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
396 repr->setAttribute("sodipodi:type", "spiral");
398 /* Set style */
399 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
401 sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
402 Inkscape::GC::release(repr);
403 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
404 sc->item->updateRepr();
406 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
407 }
409 SnapManager &m = desktop->namedview->snap_manager;
410 m.setup(desktop, true, sc->item);
411 Geom::Point pt2g = to_2geom(p);
412 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
414 Geom::Point const p0 = desktop->dt2doc(sc->center);
415 Geom::Point const p1 = desktop->dt2doc(pt2g);
417 SPSpiral *spiral = SP_SPIRAL(sc->item);
419 Geom::Point const delta = p1 - p0;
420 gdouble const rad = Geom::L2(delta);
422 gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
424 if (state & GDK_CONTROL_MASK) {
425 arg = sp_round(arg, M_PI/snaps);
426 }
428 /* Fixme: these parameters should be got from dialog box */
429 sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
430 /*expansion*/ sc->exp,
431 /*revolution*/ sc->revo,
432 rad, arg,
433 /*t0*/ sc->t0);
435 /* status text */
436 GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
437 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
438 _("<b>Spiral</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle"),
439 rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
440 g_string_free(rads, FALSE);
441 }
443 static void
444 sp_spiral_finish(SPSpiralContext *sc)
445 {
446 sc->_message_context->clear();
448 if (sc->item != NULL) {
449 SPSpiral *spiral = SP_SPIRAL(sc->item);
450 if (spiral->rad == 0) {
451 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
452 return;
453 }
455 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
457 sp_shape_set_shape(SP_SHAPE(spiral));
458 SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
460 sp_canvas_end_forced_full_redraws(desktop->canvas);
462 sp_desktop_selection(desktop)->set(sc->item);
463 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
464 _("Create spiral"));
466 sc->item = NULL;
467 }
468 }
470 static void sp_spiral_cancel(SPSpiralContext *sc)
471 {
472 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
474 sp_desktop_selection(desktop)->clear();
475 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
477 if (sc->item != NULL) {
478 SP_OBJECT(sc->item)->deleteObject();
479 sc->item = NULL;
480 }
482 sc->within_tolerance = false;
483 sc->xp = 0;
484 sc->yp = 0;
485 sc->item_to_select = NULL;
487 sp_canvas_end_forced_full_redraws(desktop->canvas);
489 sp_document_cancel(sp_desktop_document(desktop));
490 }
492 /*
493 Local Variables:
494 mode:c++
495 c-file-style:"stroustrup"
496 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
497 indent-tabs-mode:nil
498 fill-column:99
499 End:
500 */
501 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :