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