Code

Rearrange to enable code that does not directly rely on lcms.
[inkscape.git] / src / spiral-context.cpp
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"
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);
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();
119 static void
120 sp_spiral_context_dispose(GObject *object)
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         delete 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);
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)
166     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
167     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
169     if (ec->shape_knot_holder) { // desktroy knotholder
170         delete 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     }
192 static void
193 sp_spiral_context_setup(SPEventContext *ec)
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     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
221     if (prefs->getBool("/tools/shapes/selcue")) {
222         ec->enableSelectionCue();
223     }
224     if (prefs->getBool("/tools/shapes/gradientdrag")) {
225         ec->enableGrDrag();
226     }
228     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
231 static void
232 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
234     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
235     Glib::ustring name = val->getEntryName();
237     if (name == "expansion") {
238         sc->exp = CLAMP(val->getDouble(), 0.0, 1000.0);
239     } else if (name == "revolution") {
240         sc->revo = CLAMP(val->getDouble(3.0), 0.05, 40.0);
241     } else if (name == "t0") {
242         sc->t0 = CLAMP(val->getDouble(), 0.0, 0.999);
243     }
246 static gint
247 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
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     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
256     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
258     gint ret = FALSE;
260     switch (event->type) {
261         case GDK_BUTTON_PRESS:
262             if (event->button.button == 1 && !event_context->space_panning) {
264                 dragging = TRUE;
265                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
267                 SnapManager &m = desktop->namedview->snap_manager;
268                 m.setup(desktop);
269                 Geom::Point pt2g = to_2geom(sc->center);
270                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
271                 sc->center = from_2geom(pt2g);
273                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
274                                     ( GDK_KEY_PRESS_MASK |
275                                       GDK_BUTTON_RELEASE_MASK |
276                                       GDK_POINTER_MOTION_MASK |
277                                       GDK_POINTER_MOTION_HINT_MASK |
278                                       GDK_BUTTON_PRESS_MASK    ),
279                                     NULL, event->button.time);
280                 ret = TRUE;
281             }
282             break;
283         case GDK_MOTION_NOTIFY:
284             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
286                 if ( event_context->within_tolerance
287                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
288                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
289                     break; // do not drag if we're within tolerance from origin
290                 }
291                 // Once the user has moved farther than tolerance from the original location
292                 // (indicating they intend to draw, not click), then always process the
293                 // motion notify coordinates as given (no snapping back to origin)
294                 event_context->within_tolerance = false;
296                 Geom::Point const motion_w(event->motion.x, event->motion.y);
297                 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
298                 
299                 SnapManager &m = desktop->namedview->snap_manager;
300                 m.setup(desktop, true, sc->item);
301                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt);
302                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
304                 gobble_motion_events(GDK_BUTTON1_MASK);
306                 ret = TRUE;
307             }
308             break;
309         case GDK_BUTTON_RELEASE:
310             event_context->xp = event_context->yp = 0;
311             if (event->button.button == 1 && !event_context->space_panning) {
312                 dragging = FALSE;
313                 if (!event_context->within_tolerance) {
314                     // we've been dragging, finish the spiral
315                     sp_spiral_finish(sc);
316                 } else if (event_context->item_to_select) {
317                     // no dragging, select clicked item if any
318                     if (event->button.state & GDK_SHIFT_MASK) {
319                         selection->toggle(event_context->item_to_select);
320                     } else {
321                         selection->set(event_context->item_to_select);
322                     }
323                 } else {
324                     // click in an empty space
325                     selection->clear();
326                 }
328                 event_context->item_to_select = NULL;
329                 ret = TRUE;
330                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
331             }
332             break;
333         case GDK_KEY_PRESS:
334             switch (get_group0_keyval(&event->key)) {
335                 case GDK_Alt_R:
336                 case GDK_Control_L:
337                 case GDK_Control_R:
338                 case GDK_Shift_L:
339                 case GDK_Shift_R:
340                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
341                 case GDK_Meta_R:
342                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
343                                                _("<b>Ctrl</b>: snap angle"),
344                                                NULL,
345                                                _("<b>Alt</b>: lock spiral radius"));
346                     break;
347                 case GDK_Up:
348                 case GDK_Down:
349                 case GDK_KP_Up:
350                 case GDK_KP_Down:
351                     // prevent the zoom field from activation
352                     if (!MOD__CTRL_ONLY)
353                         ret = TRUE;
354                     break;
355                 case GDK_x:
356                 case GDK_X:
357                     if (MOD__ALT_ONLY) {
358                         desktop->setToolboxFocusTo ("altx-spiral");
359                         ret = TRUE;
360                     }
361                     break;
362                 case GDK_Escape:
363                     sp_desktop_selection(desktop)->clear();
364                     //TODO: make dragging escapable by Esc
365                     break;
367                 case GDK_space:
368                     if (dragging) {
369                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
370                                               event->button.time);
371                         dragging = false;
372                         if (!event_context->within_tolerance) {
373                             // we've been dragging, finish the rect
374                             sp_spiral_finish(sc);
375                         }
376                         // do not return true, so that space would work switching to selector
377                     }
378                     break;
380                 default:
381                     break;
382             }
383             break;
384         case GDK_KEY_RELEASE:
385             switch (get_group0_keyval(&event->key)) {
386                 case GDK_Alt_L:
387                 case GDK_Alt_R:
388                 case GDK_Control_L:
389                 case GDK_Control_R:
390                 case GDK_Shift_L:
391                 case GDK_Shift_R:
392                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
393                 case GDK_Meta_R:
394                     event_context->defaultMessageContext()->clear();
395                     break;
396                 default:
397                     break;
398             }
399             break;
400         default:
401             break;
402     }
404     if (!ret) {
405         if (((SPEventContextClass *) parent_class)->root_handler)
406             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
407     }
409     return ret;
412 static void
413 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
415     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
417     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
418     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
420     if (!sc->item) {
422         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
423             return;
424         }
426         /* Create object */
427         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
428         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
429         repr->setAttribute("sodipodi:type", "spiral");
431         /* Set style */
432         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
434         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
435         Inkscape::GC::release(repr);
436         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
437         sc->item->updateRepr();
439         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
440     }
442     SnapManager &m = desktop->namedview->snap_manager;
443     m.setup(desktop, true, sc->item);
444     Geom::Point pt2g = to_2geom(p);
445     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
447     Geom::Point const p0 = to_2geom(sp_desktop_dt2root_xy_point(desktop, sc->center));
448     Geom::Point const p1 = to_2geom(sp_desktop_dt2root_xy_point(desktop, from_2geom(pt2g)));        
449     
450     SPSpiral *spiral = SP_SPIRAL(sc->item);
452     Geom::Point const delta = p1 - p0;
453     gdouble const rad = Geom::L2(delta);
455     gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
457     if (state & GDK_CONTROL_MASK) {
458         arg = sp_round(arg, M_PI/snaps);
459     }
461     /* Fixme: these parameters should be got from dialog box */
462     sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
463                            /*expansion*/ sc->exp,
464                            /*revolution*/ sc->revo,
465                            rad, arg,
466                            /*t0*/ sc->t0);
468     /* status text */
469     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
470     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
471                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
472                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
473     g_string_free(rads, FALSE);
476 static void
477 sp_spiral_finish(SPSpiralContext *sc)
479     sc->_message_context->clear();
481     if (sc->item != NULL) {
482         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
483         SPSpiral  *spiral = SP_SPIRAL(sc->item);
485         sp_shape_set_shape(SP_SHAPE(spiral));
486         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
488         sp_canvas_end_forced_full_redraws(desktop->canvas);
490         sp_desktop_selection(desktop)->set(sc->item);
491         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL, 
492                          _("Create spiral"));
494         sc->item = NULL;
495     }
499 /*
500   Local Variables:
501   mode:c++
502   c-file-style:"stroustrup"
503   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
504   indent-tabs-mode:nil
505   fill-column:99
506   End:
507 */
508 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :