Code

Fix a regression in the snapper, caused by me. Sorry!
[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         sp_knot_holder_destroy (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         sp_knot_holder_destroy (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);
283             SnapManager &m = desktop->namedview->snap_manager;
284             m.setup(desktop, sc->item);
285             m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, sc->center);
287             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
288                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
289                                 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
290                                 NULL, event->button.time);
291             ret = TRUE;
292         }
293         break;
294     case GDK_MOTION_NOTIFY:
295         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
297             if ( event_context->within_tolerance
298                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
299                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
300                 break; // do not drag if we're within tolerance from origin
301             }
302             // Once the user has moved farther than tolerance from the original location
303             // (indicating they intend to draw, not click), then always process the
304             // motion notify coordinates as given (no snapping back to origin)
305             event_context->within_tolerance = false;
307             NR::Point const motion_w(event->motion.x, event->motion.y);
308             NR::Point motion_dt(event_context->desktop->w2d(motion_w));
309             
310             SnapManager &m = desktop->namedview->snap_manager;
311             m.setup(desktop, sc->item);
312             m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt);
313             
314             sp_star_drag (sc, motion_dt, event->motion.state);
316             gobble_motion_events(GDK_BUTTON1_MASK);
318             ret = TRUE;
319         }
320         break;
321     case GDK_BUTTON_RELEASE:
322         event_context->xp = event_context->yp = 0;
323         if (event->button.button == 1 && !event_context->space_panning) {
324             dragging = FALSE;
325             if (!event_context->within_tolerance) {
326                 // we've been dragging, finish the star
327                 sp_star_finish (sc);
328             } else if (event_context->item_to_select) {
329                 // no dragging, select clicked item if any
330                 if (event->button.state & GDK_SHIFT_MASK) {
331                     selection->toggle(event_context->item_to_select);
332                 } else {
333                     selection->set(event_context->item_to_select);
334                 }
335             } else {
336                 // click in an empty space
337                 selection->clear();
338             }
340             event_context->item_to_select = NULL;
341             ret = TRUE;
342             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
343         }
344         break;
345     case GDK_KEY_PRESS:
346         switch (get_group0_keyval(&event->key)) {
347         case GDK_Alt_R:
348         case GDK_Control_L:
349         case GDK_Control_R:
350         case GDK_Shift_L:
351         case GDK_Shift_R:
352         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
353         case GDK_Meta_R:
354             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
355                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
356                                        NULL,
357                                        NULL);
358             break;
359         case GDK_Up:
360         case GDK_Down:
361         case GDK_KP_Up:
362         case GDK_KP_Down:
363             // prevent the zoom field from activation
364             if (!MOD__CTRL_ONLY)
365                 ret = TRUE;
366             break;
367         case GDK_x:
368         case GDK_X:
369             if (MOD__ALT_ONLY) {
370                 desktop->setToolboxFocusTo ("altx-star");
371                 ret = TRUE;
372             }
373             break;
374         case GDK_Escape:
375             sp_desktop_selection(desktop)->clear();
376             //TODO: make dragging escapable by Esc
377             break;
379         case GDK_space:
380             if (dragging) {
381                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
382                                       event->button.time);
383                 dragging = false;
384                 if (!event_context->within_tolerance) {
385                     // we've been dragging, finish the rect
386                     sp_star_finish(sc);
387                 }
388                 // do not return true, so that space would work switching to selector
389             }
390             break;
392         default:
393             break;
394         }
395         break;
396     case GDK_KEY_RELEASE:
397         switch (get_group0_keyval (&event->key)) {
398         case GDK_Alt_L:
399         case GDK_Alt_R:
400         case GDK_Control_L:
401         case GDK_Control_R:
402         case GDK_Shift_L:
403         case GDK_Shift_R:
404         case GDK_Meta_L:  // Meta is when you press Shift+Alt
405         case GDK_Meta_R:
406             event_context->defaultMessageContext()->clear();
407             break;
408         default:
409             break;
410         }
411         break;
412     default:
413         break;
414     }
416     if (!ret) {
417         if (((SPEventContextClass *) parent_class)->root_handler)
418             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
419     }
421     return ret;
424 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
426     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
428     int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
430     if (!sc->item) {
432         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
433             return;
434         }
436         /* Create object */
437         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
438         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
439         repr->setAttribute("sodipodi:type", "star");
441         /* Set style */
442         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
444         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
445         Inkscape::GC::release(repr);
446         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
447         sc->item->updateRepr();
449         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
450     }
452     NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
453     NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
455     /* Snap corner point with no constraints */
456     SnapManager &m = desktop->namedview->snap_manager;
457     m.setup(desktop, sc->item);
458     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p1);
460     SPStar *star = SP_STAR(sc->item);
462     double const sides = (gdouble) sc->magnitude;
463     NR::Point const d = p1 - p0;
464     NR::Coord const r1 = NR::L2(d);
465     double arg1 = atan2(d);
467     if (state & GDK_CONTROL_MASK) {
468         /* Snap angle */
469         arg1 = sp_round(arg1, M_PI / snaps);
470     }
472     sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
473                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
475     /* status text */
476     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
477     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
478                                ( sc->isflatsided?
479                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
480                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
481                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
483     g_string_free(rads, FALSE);
486 static void
487 sp_star_finish (SPStarContext * sc)
489     sc->_message_context->clear();
491     if (sc->item != NULL) {
492         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
493         SPObject *object = SP_OBJECT(sc->item);
495         sp_shape_set_shape(SP_SHAPE(sc->item));
497         object->updateRepr(SP_OBJECT_WRITE_EXT);
499         sp_canvas_end_forced_full_redraws(desktop->canvas);
501         sp_desktop_selection(desktop)->set(sc->item);
502         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR, 
503                          _("Create star"));
505         sc->item = NULL;
506     }
509 /*
510   Local Variables:
511   mode:c++
512   c-file-style:"stroustrup"
513   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
514   indent-tabs-mode:nil
515   fill-column:99
516   End:
517 */
518 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :