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>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-spiral.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "desktop-affine.h"
29 #include "snap.h"
30 #include "desktop.h"
31 #include "desktop-style.h"
32 #include "message-context.h"
33 #include "pixmaps/cursor-spiral.xpm"
34 #include "spiral-context.h"
35 #include "sp-metrics.h"
36 #include <glibmm/i18n.h>
37 #include "object-edit.h"
38 #include "xml/repr.h"
39 #include "xml/node-event-vector.h"
40 #include "prefs-utils.h"
41 #include "context-fns.h"
43 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
44 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
45 static void sp_spiral_context_dispose(GObject *object);
46 static void sp_spiral_context_setup(SPEventContext *ec);
47 static void sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
49 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static void sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state);
52 static void sp_spiral_finish(SPSpiralContext *sc);
54 static SPEventContextClass *parent_class;
56 GtkType
57 sp_spiral_context_get_type()
58 {
59 static GType type = 0;
60 if (!type) {
61 GTypeInfo info = {
62 sizeof(SPSpiralContextClass),
63 NULL, NULL,
64 (GClassInitFunc) sp_spiral_context_class_init,
65 NULL, NULL,
66 sizeof(SPSpiralContext),
67 4,
68 (GInstanceInitFunc) sp_spiral_context_init,
69 NULL, /* value_table */
70 };
71 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
72 }
73 return type;
74 }
76 static void
77 sp_spiral_context_class_init(SPSpiralContextClass *klass)
78 {
79 GObjectClass *object_class = (GObjectClass *) klass;
80 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
82 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
84 object_class->dispose = sp_spiral_context_dispose;
86 event_context_class->setup = sp_spiral_context_setup;
87 event_context_class->set = sp_spiral_context_set;
88 event_context_class->root_handler = sp_spiral_context_root_handler;
89 }
91 static void
92 sp_spiral_context_init(SPSpiralContext *spiral_context)
93 {
94 SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
96 event_context->cursor_shape = cursor_spiral_xpm;
97 event_context->hot_x = 4;
98 event_context->hot_y = 4;
99 event_context->xp = 0;
100 event_context->yp = 0;
101 event_context->tolerance = 0;
102 event_context->within_tolerance = false;
103 event_context->item_to_select = NULL;
105 event_context->shape_repr = NULL;
106 event_context->shape_knot_holder = 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 /* fixme: This is necessary because we do not grab */
129 if (sc->item) sp_spiral_finish(sc);
131 if (ec->shape_knot_holder) {
132 sp_knot_holder_destroy(ec->shape_knot_holder);
133 ec->shape_knot_holder = NULL;
134 }
136 if (ec->shape_repr) { // remove old listener
137 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
138 Inkscape::GC::release(ec->shape_repr);
139 ec->shape_repr = 0;
140 }
142 if (sc->_message_context) {
143 delete sc->_message_context;
144 }
146 G_OBJECT_CLASS(parent_class)->dispose(object);
147 }
149 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
150 NULL, /* child_added */
151 NULL, /* child_removed */
152 ec_shape_event_attr_changed,
153 NULL, /* content_changed */
154 NULL /* order_changed */
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 if (ec->shape_knot_holder) { // desktroy knotholder
168 sp_knot_holder_destroy(ec->shape_knot_holder);
169 ec->shape_knot_holder = NULL;
170 }
172 if (ec->shape_repr) { // remove old listener
173 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
174 Inkscape::GC::release(ec->shape_repr);
175 ec->shape_repr = 0;
176 }
178 SPItem *item = selection->singleItem();
179 if (item) {
180 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
181 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
182 if (shape_repr) {
183 ec->shape_repr = shape_repr;
184 Inkscape::GC::anchor(shape_repr);
185 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
186 }
187 }
188 }
190 static void
191 sp_spiral_context_setup(SPEventContext *ec)
192 {
193 SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
195 if (((SPEventContextClass *) parent_class)->setup)
196 ((SPEventContextClass *) parent_class)->setup(ec);
198 sp_event_context_read(ec, "expansion");
199 sp_event_context_read(ec, "revolution");
200 sp_event_context_read(ec, "t0");
202 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
204 SPItem *item = selection->singleItem();
205 if (item) {
206 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
207 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
208 if (shape_repr) {
209 ec->shape_repr = shape_repr;
210 Inkscape::GC::anchor(shape_repr);
211 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
212 }
213 }
215 sc->sel_changed_connection.disconnect();
216 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
218 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
219 ec->enableSelectionCue();
220 }
222 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
223 ec->enableGrDrag();
224 }
226 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
227 }
229 static void
230 sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
231 {
232 SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
234 if (!strcmp(key, "expansion")) {
235 sc->exp = (val) ? g_ascii_strtod(val, NULL) : 1.0;
236 sc->exp = CLAMP(sc->exp, 0.0, 1000.0);
237 } else if (!strcmp(key, "revolution")) {
238 sc->revo = (val) ? g_ascii_strtod(val, NULL) : 3.0;
239 sc->revo = CLAMP(sc->revo, 0.05, 40.0);
240 } else if (!strcmp(key, "t0")) {
241 sc->t0 = (val) ? g_ascii_strtod(val, NULL) : 0.0;
242 sc->t0 = CLAMP(sc->t0, 0.0, 0.999);
243 }
244 }
246 static gint
247 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
248 {
249 static gboolean dragging;
251 SPDesktop *desktop = event_context->desktop;
252 Inkscape::Selection *selection = sp_desktop_selection (desktop);
253 SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
255 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
257 gint ret = FALSE;
259 switch (event->type) {
260 case GDK_BUTTON_PRESS:
261 if (event->button.button == 1 && !event_context->space_panning) {
263 dragging = TRUE;
264 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
266 SnapManager const &m = desktop->namedview->snap_manager;
267 sc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, sc->center, sc->item).getPoint();
269 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
270 ( GDK_KEY_PRESS_MASK |
271 GDK_BUTTON_RELEASE_MASK |
272 GDK_POINTER_MOTION_MASK |
273 GDK_BUTTON_PRESS_MASK ),
274 NULL, event->button.time);
275 ret = TRUE;
276 }
277 break;
278 case GDK_MOTION_NOTIFY:
279 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
281 if ( event_context->within_tolerance
282 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
283 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
284 break; // do not drag if we're within tolerance from origin
285 }
286 // Once the user has moved farther than tolerance from the original location
287 // (indicating they intend to draw, not click), then always process the
288 // motion notify coordinates as given (no snapping back to origin)
289 event_context->within_tolerance = false;
291 NR::Point const motion_w(event->motion.x, event->motion.y);
292 NR::Point motion_dt(event_context->desktop->w2d(motion_w));
294 SnapManager const &m = desktop->namedview->snap_manager;
295 motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, sc->item).getPoint();
296 sp_spiral_drag(sc, motion_dt, event->motion.state);
298 gobble_motion_events(GDK_BUTTON1_MASK);
300 ret = TRUE;
301 }
302 break;
303 case GDK_BUTTON_RELEASE:
304 event_context->xp = event_context->yp = 0;
305 if (event->button.button == 1 && !event_context->space_panning) {
306 dragging = FALSE;
307 if (!event_context->within_tolerance) {
308 // we've been dragging, finish the spiral
309 sp_spiral_finish(sc);
310 } else if (event_context->item_to_select) {
311 // no dragging, select clicked item if any
312 if (event->button.state & GDK_SHIFT_MASK) {
313 selection->toggle(event_context->item_to_select);
314 } else {
315 selection->set(event_context->item_to_select);
316 }
317 } else {
318 // click in an empty space
319 selection->clear();
320 }
322 event_context->item_to_select = NULL;
323 ret = TRUE;
324 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
325 }
326 break;
327 case GDK_KEY_PRESS:
328 switch (get_group0_keyval(&event->key)) {
329 case GDK_Alt_R:
330 case GDK_Control_L:
331 case GDK_Control_R:
332 case GDK_Shift_L:
333 case GDK_Shift_R:
334 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
335 case GDK_Meta_R:
336 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
337 _("<b>Ctrl</b>: snap angle"),
338 NULL,
339 _("<b>Alt</b>: lock spiral radius"));
340 break;
341 case GDK_Up:
342 case GDK_Down:
343 case GDK_KP_Up:
344 case GDK_KP_Down:
345 // prevent the zoom field from activation
346 if (!MOD__CTRL_ONLY)
347 ret = TRUE;
348 break;
349 case GDK_x:
350 case GDK_X:
351 if (MOD__ALT_ONLY) {
352 desktop->setToolboxFocusTo ("altx-spiral");
353 ret = TRUE;
354 }
355 break;
356 case GDK_Escape:
357 sp_desktop_selection(desktop)->clear();
358 //TODO: make dragging escapable by Esc
359 break;
361 case GDK_space:
362 if (dragging) {
363 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
364 event->button.time);
365 dragging = false;
366 if (!event_context->within_tolerance) {
367 // we've been dragging, finish the rect
368 sp_spiral_finish(sc);
369 }
370 // do not return true, so that space would work switching to selector
371 }
372 break;
374 default:
375 break;
376 }
377 break;
378 case GDK_KEY_RELEASE:
379 switch (get_group0_keyval(&event->key)) {
380 case GDK_Alt_L:
381 case GDK_Alt_R:
382 case GDK_Control_L:
383 case GDK_Control_R:
384 case GDK_Shift_L:
385 case GDK_Shift_R:
386 case GDK_Meta_L: // Meta is when you press Shift+Alt
387 case GDK_Meta_R:
388 event_context->defaultMessageContext()->clear();
389 break;
390 default:
391 break;
392 }
393 break;
394 default:
395 break;
396 }
398 if (!ret) {
399 if (((SPEventContextClass *) parent_class)->root_handler)
400 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
401 }
403 return ret;
404 }
406 static void
407 sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
408 {
409 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
411 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
413 if (!sc->item) {
415 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
416 return;
417 }
419 /* Create object */
420 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
421 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
422 repr->setAttribute("sodipodi:type", "spiral");
424 /* Set style */
425 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
427 sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
428 Inkscape::GC::release(repr);
429 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
430 sc->item->updateRepr();
432 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
433 }
435 NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
436 NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
437 SnapManager const &m = desktop->namedview->snap_manager;
438 p1 = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p1, sc->item).getPoint();
440 SPSpiral *spiral = SP_SPIRAL(sc->item);
442 NR::Point const delta = p1 - p0;
443 gdouble const rad = NR::L2(delta);
445 gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
447 if (state & GDK_CONTROL_MASK) {
448 arg = sp_round(arg, M_PI/snaps);
449 }
451 /* Fixme: these parameters should be got from dialog box */
452 sp_spiral_position_set(spiral, p0[NR::X], p0[NR::Y],
453 /*expansion*/ sc->exp,
454 /*revolution*/ sc->revo,
455 rad, arg,
456 /*t0*/ sc->t0);
458 /* status text */
459 GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
460 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
461 _("<b>Spiral</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle"),
462 rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
463 g_string_free(rads, FALSE);
464 }
466 static void
467 sp_spiral_finish(SPSpiralContext *sc)
468 {
469 sc->_message_context->clear();
471 if (sc->item != NULL) {
472 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
473 SPSpiral *spiral = SP_SPIRAL(sc->item);
475 sp_shape_set_shape(SP_SHAPE(spiral));
476 SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
478 sp_canvas_end_forced_full_redraws(desktop->canvas);
480 sp_desktop_selection(desktop)->set(sc->item);
481 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
482 _("Create spiral"));
484 sc->item = NULL;
485 }
486 }
489 /*
490 Local Variables:
491 mode:c++
492 c-file-style:"stroustrup"
493 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
494 indent-tabs-mode:nil
495 fill-column:99
496 End:
497 */
498 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :