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 "preferences.h"
43 #include "context-fns.h"
44 #include "shape-editor.h"
46 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
47 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
48 static void sp_spiral_context_dispose(GObject *object);
49 static void sp_spiral_context_setup(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);
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 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
226 SnapManager &m = desktop->namedview->snap_manager;
227 m.setup(desktop);
228 Geom::Point pt2g = to_2geom(sc->center);
229 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
230 sc->center = from_2geom(pt2g);
232 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
233 ( GDK_KEY_PRESS_MASK |
234 GDK_BUTTON_RELEASE_MASK |
235 GDK_POINTER_MOTION_MASK |
236 GDK_POINTER_MOTION_HINT_MASK |
237 GDK_BUTTON_PRESS_MASK ),
238 NULL, event->button.time);
239 ret = TRUE;
240 }
241 break;
242 case GDK_MOTION_NOTIFY:
243 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
245 if ( event_context->within_tolerance
246 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
247 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
248 break; // do not drag if we're within tolerance from origin
249 }
250 // Once the user has moved farther than tolerance from the original location
251 // (indicating they intend to draw, not click), then always process the
252 // motion notify coordinates as given (no snapping back to origin)
253 event_context->within_tolerance = false;
255 Geom::Point const motion_w(event->motion.x, event->motion.y);
256 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
258 SnapManager &m = desktop->namedview->snap_manager;
259 m.setup(desktop, true, sc->item);
260 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt);
261 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
263 gobble_motion_events(GDK_BUTTON1_MASK);
265 ret = TRUE;
266 }
267 break;
268 case GDK_BUTTON_RELEASE:
269 event_context->xp = event_context->yp = 0;
270 if (event->button.button == 1 && !event_context->space_panning) {
271 dragging = FALSE;
272 if (!event_context->within_tolerance) {
273 // we've been dragging, finish the spiral
274 sp_spiral_finish(sc);
275 } else if (event_context->item_to_select) {
276 // no dragging, select clicked item if any
277 if (event->button.state & GDK_SHIFT_MASK) {
278 selection->toggle(event_context->item_to_select);
279 } else {
280 selection->set(event_context->item_to_select);
281 }
282 } else {
283 // click in an empty space
284 selection->clear();
285 }
287 event_context->item_to_select = NULL;
288 ret = TRUE;
289 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
290 }
291 break;
292 case GDK_KEY_PRESS:
293 switch (get_group0_keyval(&event->key)) {
294 case GDK_Alt_R:
295 case GDK_Control_L:
296 case GDK_Control_R:
297 case GDK_Shift_L:
298 case GDK_Shift_R:
299 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
300 case GDK_Meta_R:
301 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
302 _("<b>Ctrl</b>: snap angle"),
303 NULL,
304 _("<b>Alt</b>: lock spiral radius"));
305 break;
306 case GDK_Up:
307 case GDK_Down:
308 case GDK_KP_Up:
309 case GDK_KP_Down:
310 // prevent the zoom field from activation
311 if (!MOD__CTRL_ONLY)
312 ret = TRUE;
313 break;
314 case GDK_x:
315 case GDK_X:
316 if (MOD__ALT_ONLY) {
317 desktop->setToolboxFocusTo ("altx-spiral");
318 ret = TRUE;
319 }
320 break;
321 case GDK_Escape:
322 sp_desktop_selection(desktop)->clear();
323 //TODO: make dragging escapable by Esc
324 break;
326 case GDK_space:
327 if (dragging) {
328 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
329 event->button.time);
330 dragging = false;
331 if (!event_context->within_tolerance) {
332 // we've been dragging, finish the rect
333 sp_spiral_finish(sc);
334 }
335 // do not return true, so that space would work switching to selector
336 }
337 break;
339 default:
340 break;
341 }
342 break;
343 case GDK_KEY_RELEASE:
344 switch (get_group0_keyval(&event->key)) {
345 case GDK_Alt_L:
346 case GDK_Alt_R:
347 case GDK_Control_L:
348 case GDK_Control_R:
349 case GDK_Shift_L:
350 case GDK_Shift_R:
351 case GDK_Meta_L: // Meta is when you press Shift+Alt
352 case GDK_Meta_R:
353 event_context->defaultMessageContext()->clear();
354 break;
355 default:
356 break;
357 }
358 break;
359 default:
360 break;
361 }
363 if (!ret) {
364 if (((SPEventContextClass *) parent_class)->root_handler)
365 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
366 }
368 return ret;
369 }
371 static void
372 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
373 {
374 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
376 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
377 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
379 if (!sc->item) {
381 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
382 return;
383 }
385 /* Create object */
386 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
387 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
388 repr->setAttribute("sodipodi:type", "spiral");
390 /* Set style */
391 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
393 sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
394 Inkscape::GC::release(repr);
395 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
396 sc->item->updateRepr();
398 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
399 }
401 SnapManager &m = desktop->namedview->snap_manager;
402 m.setup(desktop, true, sc->item);
403 Geom::Point pt2g = to_2geom(p);
404 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
406 Geom::Point const p0 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, sc->center));
407 Geom::Point const p1 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, from_2geom(pt2g)));
409 SPSpiral *spiral = SP_SPIRAL(sc->item);
411 Geom::Point const delta = p1 - p0;
412 gdouble const rad = Geom::L2(delta);
414 gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
416 if (state & GDK_CONTROL_MASK) {
417 arg = sp_round(arg, M_PI/snaps);
418 }
420 /* Fixme: these parameters should be got from dialog box */
421 sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
422 /*expansion*/ sc->exp,
423 /*revolution*/ sc->revo,
424 rad, arg,
425 /*t0*/ sc->t0);
427 /* status text */
428 GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
429 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
430 _("<b>Spiral</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle"),
431 rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
432 g_string_free(rads, FALSE);
433 }
435 static void
436 sp_spiral_finish(SPSpiralContext *sc)
437 {
438 sc->_message_context->clear();
440 if (sc->item != NULL) {
441 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
442 SPSpiral *spiral = SP_SPIRAL(sc->item);
444 sp_shape_set_shape(SP_SHAPE(spiral));
445 SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
447 sp_canvas_end_forced_full_redraws(desktop->canvas);
449 sp_desktop_selection(desktop)->set(sc->item);
450 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
451 _("Create spiral"));
453 sc->item = NULL;
454 }
455 }
458 /*
459 Local Variables:
460 mode:c++
461 c-file-style:"stroustrup"
462 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
463 indent-tabs-mode:nil
464 fill-column:99
465 End:
466 */
467 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :