Code

Modify the parameters required for setting up the SnapManager
[inkscape.git] / src / star-context.cpp
1 #define __SP_STAR_CONTEXT_C__
3 /*
4  * Star drawing context
5  *
6  * Authors:
7  *   Mitsuru Oka <oka326@parkcity.ne.jp>
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 1999-2002 Lauris Kaplinski
12  * Copyright (C) 2001-2002 Mitsuru Oka
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <cstring>
22 #include <string>
24 #include <gdk/gdkkeysyms.h>
26 #include "macros.h"
27 #include "display/sp-canvas.h"
28 #include "sp-star.h"
29 #include "document.h"
30 #include "sp-namedview.h"
31 #include "selection.h"
32 #include "desktop-handles.h"
33 #include "desktop-affine.h"
34 #include "snap.h"
35 #include "desktop.h"
36 #include "desktop-style.h"
37 #include "message-context.h"
38 #include "pixmaps/cursor-star.xpm"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "prefs-utils.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "object-edit.h"
45 #include "context-fns.h"
47 #include "star-context.h"
49 static void sp_star_context_class_init (SPStarContextClass * klass);
50 static void sp_star_context_init (SPStarContext * star_context);
51 static void sp_star_context_dispose (GObject *object);
53 static void sp_star_context_setup (SPEventContext *ec);
54 static void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
55 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
57 static void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
58 static void sp_star_finish (SPStarContext * sc);
60 static SPEventContextClass * parent_class;
62 GtkType
63 sp_star_context_get_type (void)
64 {
65     static GType type = 0;
66     if (!type) {
67         GTypeInfo info = {
68             sizeof (SPStarContextClass),
69             NULL, NULL,
70             (GClassInitFunc) sp_star_context_class_init,
71             NULL, NULL,
72             sizeof (SPStarContext),
73             4,
74             (GInstanceInitFunc) sp_star_context_init,
75             NULL,    /* value_table */
76         };
77         type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
78     }
79     return type;
80 }
82 static void
83 sp_star_context_class_init (SPStarContextClass * klass)
84 {
85     GObjectClass *object_class = (GObjectClass *) klass;
86     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88     parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
90     object_class->dispose = sp_star_context_dispose;
92     event_context_class->setup = sp_star_context_setup;
93     event_context_class->set = sp_star_context_set;
94     event_context_class->root_handler = sp_star_context_root_handler;
95 }
97 static void
98 sp_star_context_init (SPStarContext * star_context)
99 {
100     SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
102     event_context->cursor_shape = cursor_star_xpm;
103     event_context->hot_x = 4;
104     event_context->hot_y = 4;
105     event_context->xp = 0;
106     event_context->yp = 0;
107     event_context->tolerance = 0;
108     event_context->within_tolerance = false;
109     event_context->item_to_select = NULL;
111     event_context->shape_repr = NULL;
112     event_context->shape_knot_holder = NULL;
114     star_context->item = NULL;
116     star_context->magnitude = 5;
117     star_context->proportion = 0.5;
118     star_context->isflatsided = false;
120     new (&star_context->sel_changed_connection) sigc::connection();
123 static void
124 sp_star_context_dispose (GObject *object)
126     SPEventContext *ec = SP_EVENT_CONTEXT (object);
127     SPStarContext *sc = SP_STAR_CONTEXT (object);
129     ec->enableGrDrag(false);
131     sc->sel_changed_connection.disconnect();
132     sc->sel_changed_connection.~connection();
134     if (ec->shape_knot_holder) {
135         delete ec->shape_knot_holder;
136         ec->shape_knot_holder = NULL;
137     }
139     if (ec->shape_repr) { // remove old listener
140         sp_repr_remove_listener_by_data (ec->shape_repr, ec);
141         Inkscape::GC::release(ec->shape_repr);
142         ec->shape_repr = 0;
143     }
145     /* fixme: This is necessary because we do not grab */
146     if (sc->item) sp_star_finish (sc);
148     if (sc->_message_context) {
149         delete sc->_message_context;
150     }
152     G_OBJECT_CLASS (parent_class)->dispose (object);
155 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
156     NULL, /* child_added */
157     NULL, /* child_removed */
158     ec_shape_event_attr_changed,
159     NULL, /* content_changed */
160     NULL  /* order_changed */
161 };
163 /**
164 \brief  Callback that processes the "changed" signal on the selection;
165 destroys old and creates new knotholder
166 \param  selection Should not be NULL.
167 */
168 void
169 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
171     g_assert (selection != NULL);
173     SPStarContext *sc = SP_STAR_CONTEXT (data);
174     SPEventContext *ec = SP_EVENT_CONTEXT (sc);
176     if (ec->shape_knot_holder) { // desktroy knotholder
177         delete ec->shape_knot_holder;
178         ec->shape_knot_holder = NULL;
179     }
181     if (ec->shape_repr) { // remove old listener
182         sp_repr_remove_listener_by_data (ec->shape_repr, ec);
183         Inkscape::GC::release(ec->shape_repr);
184         ec->shape_repr = 0;
185     }
187     SPItem *item = selection->singleItem();
188     if (item) {
189         ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
190         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
191         if (shape_repr) {
192             ec->shape_repr = shape_repr;
193             Inkscape::GC::anchor(shape_repr);
194             sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
195         }
196     }
199 static void
200 sp_star_context_setup (SPEventContext *ec)
202    SPStarContext *sc = SP_STAR_CONTEXT (ec);
204     if (((SPEventContextClass *) parent_class)->setup)
205         ((SPEventContextClass *) parent_class)->setup (ec);
207     sp_event_context_read (ec, "magnitude");
208     sp_event_context_read (ec, "proportion");
209     sp_event_context_read (ec, "isflatsided");
210     sp_event_context_read (ec, "rounded");
211     sp_event_context_read (ec, "randomized");
213     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
215     SPItem *item = selection->singleItem();
216         if (item) {
217             ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
218             Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
219             if (shape_repr) {
220                 ec->shape_repr = shape_repr;
221                 Inkscape::GC::anchor(shape_repr);
222                 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
223             }
224         }
226     sc->sel_changed_connection.disconnect();
227     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
229     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
230         ec->enableSelectionCue();
231     }
233     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
234         ec->enableGrDrag();
235     }
237     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
240 static void
241 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
243     SPStarContext *sc = SP_STAR_CONTEXT (ec);
244     if (!strcmp (key, "magnitude")) {
245         sc->magnitude = (val) ? atoi (val) : 5;
246         sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
247     } else if (!strcmp (key, "proportion")) {
248         sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
249         sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
250     } else if (!strcmp (key, "isflatsided")) {
251         if (val && !strcmp(val, "true"))
252             sc->isflatsided = true;
253         else
254             sc->isflatsided = false;
255     } else if (!strcmp (key, "rounded")) {
256         sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
257     } else if (!strcmp (key, "randomized")) {
258         sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
259     }
262 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
264     static gboolean dragging;
266     SPDesktop *desktop = event_context->desktop;
267     Inkscape::Selection *selection = sp_desktop_selection (desktop);
269     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
271     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
273     gint ret = FALSE;
275     switch (event->type) {
276     case GDK_BUTTON_PRESS:
277         if (event->button.button == 1 && !event_context->space_panning) {
279             dragging = TRUE;
281             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
282             
283             /* Snap center */
284             SnapManager &m = desktop->namedview->snap_manager;
285             m.setup(desktop, true);
286             Geom::Point pt2g = to_2geom(sc->center);
287             m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
288             sc->center = from_2geom(pt2g);
290             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
291                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
292                                 GDK_POINTER_MOTION_MASK |
293                                 GDK_POINTER_MOTION_HINT_MASK |
294                                 GDK_BUTTON_PRESS_MASK,
295                                 NULL, event->button.time);
296             ret = TRUE;
297         }
298         break;
299     case GDK_MOTION_NOTIFY:
300         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
302             if ( event_context->within_tolerance
303                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
304                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
305                 break; // do not drag if we're within tolerance from origin
306             }
307             // Once the user has moved farther than tolerance from the original location
308             // (indicating they intend to draw, not click), then always process the
309             // motion notify coordinates as given (no snapping back to origin)
310             event_context->within_tolerance = false;
312             NR::Point const motion_w(event->motion.x, event->motion.y);
313             NR::Point motion_dt(desktop->w2d(motion_w));
314             
315             sp_star_drag (sc, motion_dt, event->motion.state);
317             gobble_motion_events(GDK_BUTTON1_MASK);
319             ret = TRUE;
320         }
321         break;
322     case GDK_BUTTON_RELEASE:
323         event_context->xp = event_context->yp = 0;
324         if (event->button.button == 1 && !event_context->space_panning) {
325             dragging = FALSE;
326             if (!event_context->within_tolerance) {
327                 // we've been dragging, finish the star
328                 sp_star_finish (sc);
329             } else if (event_context->item_to_select) {
330                 // no dragging, select clicked item if any
331                 if (event->button.state & GDK_SHIFT_MASK) {
332                     selection->toggle(event_context->item_to_select);
333                 } else {
334                     selection->set(event_context->item_to_select);
335                 }
336             } else {
337                 // click in an empty space
338                 selection->clear();
339             }
341             event_context->item_to_select = NULL;
342             ret = TRUE;
343             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
344         }
345         break;
346     case GDK_KEY_PRESS:
347         switch (get_group0_keyval(&event->key)) {
348         case GDK_Alt_R:
349         case GDK_Control_L:
350         case GDK_Control_R:
351         case GDK_Shift_L:
352         case GDK_Shift_R:
353         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
354         case GDK_Meta_R:
355             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
356                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
357                                        NULL,
358                                        NULL);
359             break;
360         case GDK_Up:
361         case GDK_Down:
362         case GDK_KP_Up:
363         case GDK_KP_Down:
364             // prevent the zoom field from activation
365             if (!MOD__CTRL_ONLY)
366                 ret = TRUE;
367             break;
368         case GDK_x:
369         case GDK_X:
370             if (MOD__ALT_ONLY) {
371                 desktop->setToolboxFocusTo ("altx-star");
372                 ret = TRUE;
373             }
374             break;
375         case GDK_Escape:
376             sp_desktop_selection(desktop)->clear();
377             //TODO: make dragging escapable by Esc
378             break;
380         case GDK_space:
381             if (dragging) {
382                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
383                                       event->button.time);
384                 dragging = false;
385                 if (!event_context->within_tolerance) {
386                     // we've been dragging, finish the rect
387                     sp_star_finish(sc);
388                 }
389                 // do not return true, so that space would work switching to selector
390             }
391             break;
393         default:
394             break;
395         }
396         break;
397     case GDK_KEY_RELEASE:
398         switch (get_group0_keyval (&event->key)) {
399         case GDK_Alt_L:
400         case GDK_Alt_R:
401         case GDK_Control_L:
402         case GDK_Control_R:
403         case GDK_Shift_L:
404         case GDK_Shift_R:
405         case GDK_Meta_L:  // Meta is when you press Shift+Alt
406         case GDK_Meta_R:
407             event_context->defaultMessageContext()->clear();
408             break;
409         default:
410             break;
411         }
412         break;
413     default:
414         break;
415     }
417     if (!ret) {
418         if (((SPEventContextClass *) parent_class)->root_handler)
419             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
420     }
422     return ret;
425 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
427     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
429     int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
431     if (!sc->item) {
433         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
434             return;
435         }
437         /* Create object */
438         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
439         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
440         repr->setAttribute("sodipodi:type", "star");
442         /* Set style */
443         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
445         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
446         Inkscape::GC::release(repr);
447         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
448         sc->item->updateRepr();
450         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
451     }
453     /* Snap corner point with no constraints */
454     SnapManager &m = desktop->namedview->snap_manager;
455     m.setup(desktop, true, sc->item);
456     Geom::Point pt2g = to_2geom(p);
457     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);    
458     
459     Geom::Point const p0 = to_2geom(sp_desktop_dt2root_xy_point(desktop, sc->center));
460     Geom::Point const p1 = to_2geom(sp_desktop_dt2root_xy_point(desktop, from_2geom(pt2g)));
461     
462     SPStar *star = SP_STAR(sc->item);
464     double const sides = (gdouble) sc->magnitude;
465     Geom::Point const d = p1 - p0;
466     Geom::Coord const r1 = Geom::L2(d);
467     double arg1 = atan2(from_2geom(d));
469     if (state & GDK_CONTROL_MASK) {
470         /* Snap angle */
471         arg1 = sp_round(arg1, M_PI / snaps);
472     }
474     sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
475                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
477     /* status text */
478     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
479     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
480                                ( sc->isflatsided?
481                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
482                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
483                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
485     g_string_free(rads, FALSE);
488 static void
489 sp_star_finish (SPStarContext * sc)
491     sc->_message_context->clear();
493     if (sc->item != NULL) {
494         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
495         SPObject *object = SP_OBJECT(sc->item);
497         sp_shape_set_shape(SP_SHAPE(sc->item));
499         object->updateRepr(SP_OBJECT_WRITE_EXT);
501         sp_canvas_end_forced_full_redraws(desktop->canvas);
503         sp_desktop_selection(desktop)->set(sc->item);
504         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR, 
505                          _("Create star"));
507         sc->item = NULL;
508     }
511 /*
512   Local Variables:
513   mode:c++
514   c-file-style:"stroustrup"
515   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
516   indent-tabs-mode:nil
517   fill-column:99
518   End:
519 */
520 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :