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 sp_repr_synthesize_events(shape_repr, &ec_shape_repr_events, ec);
187 }
188 }
189 }
191 static void
192 sp_spiral_context_setup(SPEventContext *ec)
193 {
194 SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
196 if (((SPEventContextClass *) parent_class)->setup)
197 ((SPEventContextClass *) parent_class)->setup(ec);
199 sp_event_context_read(ec, "expansion");
200 sp_event_context_read(ec, "revolution");
201 sp_event_context_read(ec, "t0");
203 Inkscape::Selection *selection = SP_DT_SELECTION(ec->desktop);
205 SPItem *item = selection->singleItem();
206 if (item) {
207 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
208 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
209 if (shape_repr) {
210 ec->shape_repr = shape_repr;
211 Inkscape::GC::anchor(shape_repr);
212 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
213 sp_repr_synthesize_events(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_DT_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) {
265 dragging = TRUE;
266 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
268 SnapManager const m(desktop->namedview);
269 sc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
271 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
272 ( GDK_KEY_PRESS_MASK |
273 GDK_BUTTON_RELEASE_MASK |
274 GDK_POINTER_MOTION_MASK |
275 GDK_BUTTON_PRESS_MASK ),
276 NULL, event->button.time);
277 ret = TRUE;
278 }
279 break;
280 case GDK_MOTION_NOTIFY:
281 if (dragging && event->motion.state && GDK_BUTTON1_MASK) {
283 if ( event_context->within_tolerance
284 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
285 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
286 break; // do not drag if we're within tolerance from origin
287 }
288 // Once the user has moved farther than tolerance from the original location
289 // (indicating they intend to draw, not click), then always process the
290 // motion notify coordinates as given (no snapping back to origin)
291 event_context->within_tolerance = false;
293 NR::Point const motion_w(event->motion.x, event->motion.y);
294 NR::Point const motion_dt(event_context->desktop->w2d(motion_w));
295 sp_spiral_drag(sc, motion_dt, event->motion.state);
296 ret = TRUE;
297 }
298 break;
299 case GDK_BUTTON_RELEASE:
300 event_context->xp = event_context->yp = 0;
301 if (event->button.button == 1) {
302 dragging = FALSE;
303 if (!event_context->within_tolerance) {
304 // we've been dragging, finish the spiral
305 sp_spiral_finish(sc);
306 } else if (event_context->item_to_select) {
307 // no dragging, select clicked item if any
308 if (event->button.state & GDK_SHIFT_MASK) {
309 selection->toggle(event_context->item_to_select);
310 } else {
311 selection->set(event_context->item_to_select);
312 }
313 } else {
314 // click in an empty space
315 selection->clear();
316 }
318 event_context->item_to_select = NULL;
319 ret = TRUE;
320 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
321 }
322 break;
323 case GDK_KEY_PRESS:
324 switch (get_group0_keyval(&event->key)) {
325 case GDK_Alt_R:
326 case GDK_Control_L:
327 case GDK_Control_R:
328 case GDK_Shift_L:
329 case GDK_Shift_R:
330 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
331 case GDK_Meta_R:
332 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
333 _("<b>Ctrl</b>: snap angle"),
334 NULL,
335 _("<b>Alt</b>: lock spiral radius"));
336 break;
337 case GDK_Up:
338 case GDK_Down:
339 case GDK_KP_Up:
340 case GDK_KP_Down:
341 // prevent the zoom field from activation
342 if (!MOD__CTRL_ONLY)
343 ret = TRUE;
344 break;
345 case GDK_x:
346 case GDK_X:
347 if (MOD__ALT_ONLY) {
348 desktop->setToolboxFocusTo ("altx-spiral");
349 ret = TRUE;
350 }
351 break;
352 case GDK_Escape:
353 SP_DT_SELECTION(desktop)->clear();
354 //TODO: make dragging escapable by Esc
355 default:
356 break;
357 }
358 break;
359 case GDK_KEY_RELEASE:
360 switch (get_group0_keyval(&event->key)) {
361 case GDK_Alt_L:
362 case GDK_Alt_R:
363 case GDK_Control_L:
364 case GDK_Control_R:
365 case GDK_Shift_L:
366 case GDK_Shift_R:
367 case GDK_Meta_L: // Meta is when you press Shift+Alt
368 case GDK_Meta_R:
369 event_context->defaultMessageContext()->clear();
370 break;
371 default:
372 break;
373 }
374 break;
375 default:
376 break;
377 }
379 if (!ret) {
380 if (((SPEventContextClass *) parent_class)->root_handler)
381 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
382 }
384 return ret;
385 }
387 static void
388 sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
389 {
390 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
392 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
394 if (!sc->item) {
396 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
397 return;
398 }
400 /* Create object */
401 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
402 repr->setAttribute("sodipodi:type", "spiral");
404 /* Set style */
405 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
407 sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
408 Inkscape::GC::release(repr);
409 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
410 sc->item->updateRepr();
411 }
413 NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
414 NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
415 SnapManager const m(desktop->namedview);
416 p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
418 SPSpiral *spiral = SP_SPIRAL(sc->item);
420 NR::Point const delta = p1 - p0;
421 gdouble const rad = NR::L2(delta);
423 gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
425 if (state & GDK_CONTROL_MASK) {
426 arg = sp_round(arg, M_PI/snaps);
427 }
429 /* Fixme: these parameters should be got from dialog box */
430 sp_spiral_position_set(spiral, p0[NR::X], p0[NR::Y],
431 /*expansion*/ sc->exp,
432 /*revolution*/ sc->revo,
433 rad, arg,
434 /*t0*/ sc->t0);
436 /* status text */
437 GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
438 sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
439 _("<b>Spiral</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle"),
440 rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
441 g_string_free(rads, FALSE);
442 }
444 static void
445 sp_spiral_finish(SPSpiralContext *sc)
446 {
447 sc->_message_context->clear();
449 if (sc->item != NULL) {
450 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
451 SPSpiral *spiral = SP_SPIRAL(sc->item);
453 sp_shape_set_shape(SP_SHAPE(spiral));
454 SP_OBJECT(spiral)->updateRepr(NULL, SP_OBJECT_WRITE_EXT);
456 SP_DT_SELECTION(desktop)->set(sc->item);
457 sp_document_done(SP_DT_DOCUMENT(desktop));
459 sc->item = NULL;
460 }
461 }
464 /*
465 Local Variables:
466 mode:c++
467 c-file-style:"stroustrup"
468 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
469 indent-tabs-mode:nil
470 fill-column:99
471 End:
472 */
473 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :