Code

Remove redundancy from snapping API (type of snapsource no longer has to be specified...
[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 "snap.h"
34 #include "desktop.h"
35 #include "desktop-style.h"
36 #include "message-context.h"
37 #include "libnr/nr-macros.h"
38 #include "pixmaps/cursor-star.xpm"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "preferences.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "object-edit.h"
45 #include "context-fns.h"
46 #include "shape-editor.h"
48 #include "star-context.h"
50 static void sp_star_context_class_init (SPStarContextClass * klass);
51 static void sp_star_context_init (SPStarContext * star_context);
52 static void sp_star_context_dispose (GObject *object);
54 static void sp_star_context_setup (SPEventContext *ec);
55 static void sp_star_context_finish(SPEventContext *ec);
56 static void sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val);
57 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
59 static void sp_star_drag (SPStarContext * sc, Geom::Point p, guint state);
60 static void sp_star_finish (SPStarContext * sc);
61 static void sp_star_cancel(SPStarContext * sc);
63 static SPEventContextClass * parent_class;
65 GtkType
66 sp_star_context_get_type (void)
67 {
68     static GType type = 0;
69     if (!type) {
70         GTypeInfo info = {
71             sizeof (SPStarContextClass),
72             NULL, NULL,
73             (GClassInitFunc) sp_star_context_class_init,
74             NULL, NULL,
75             sizeof (SPStarContext),
76             4,
77             (GInstanceInitFunc) sp_star_context_init,
78             NULL,    /* value_table */
79         };
80         type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
81     }
82     return type;
83 }
85 static void
86 sp_star_context_class_init (SPStarContextClass * klass)
87 {
88     GObjectClass *object_class = (GObjectClass *) klass;
89     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
91     parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
93     object_class->dispose = sp_star_context_dispose;
95     event_context_class->setup = sp_star_context_setup;
96     event_context_class->finish = sp_star_context_finish;
97     event_context_class->set = sp_star_context_set;
98     event_context_class->root_handler = sp_star_context_root_handler;
99 }
101 static void
102 sp_star_context_init (SPStarContext * star_context)
104     SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
106     event_context->cursor_shape = cursor_star_xpm;
107     event_context->hot_x = 4;
108     event_context->hot_y = 4;
109     event_context->xp = 0;
110     event_context->yp = 0;
111     event_context->tolerance = 0;
112     event_context->within_tolerance = false;
113     event_context->item_to_select = NULL;
115     star_context->item = NULL;
117     star_context->magnitude = 5;
118     star_context->proportion = 0.5;
119     star_context->isflatsided = false;
121     new (&star_context->sel_changed_connection) sigc::connection();
124 static void sp_star_context_finish(SPEventContext *ec)
126     SPStarContext *sc = SP_STAR_CONTEXT(ec);
127         SPDesktop *desktop = ec->desktop;
129         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
130         sp_star_finish(sc);
131     sc->sel_changed_connection.disconnect();
133     if (((SPEventContextClass *) parent_class)->finish) {
134                 ((SPEventContextClass *) parent_class)->finish(ec);
135         }
139 static void
140 sp_star_context_dispose (GObject *object)
142     SPEventContext *ec = SP_EVENT_CONTEXT (object);
143     SPStarContext *sc = SP_STAR_CONTEXT (object);
145     ec->enableGrDrag(false);
147     sc->sel_changed_connection.disconnect();
148     sc->sel_changed_connection.~connection();
150     delete ec->shape_editor;
151     ec->shape_editor = NULL;
153     /* fixme: This is necessary because we do not grab */
154     if (sc->item) sp_star_finish (sc);
156     if (sc->_message_context) {
157         delete sc->_message_context;
158     }
160     G_OBJECT_CLASS (parent_class)->dispose (object);
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     ec->shape_editor->unset_item(SH_KNOTHOLDER);
177     SPItem *item = selection->singleItem();
178     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
181 static void
182 sp_star_context_setup (SPEventContext *ec)
184    SPStarContext *sc = SP_STAR_CONTEXT (ec);
186     if (((SPEventContextClass *) parent_class)->setup)
187         ((SPEventContextClass *) parent_class)->setup (ec);
189     sp_event_context_read (ec, "magnitude");
190     sp_event_context_read (ec, "proportion");
191     sp_event_context_read (ec, "isflatsided");
192     sp_event_context_read (ec, "rounded");
193     sp_event_context_read (ec, "randomized");
195     ec->shape_editor = new ShapeEditor(ec->desktop);
197     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
198     if (item) {
199         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
200     }
202     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
203     sc->sel_changed_connection.disconnect();
204     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
206     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
207     if (prefs->getBool("/tools/shapes/selcue")) {
208         ec->enableSelectionCue();
209     }
211     if (prefs->getBool("/tools/shapes/gradientdrag")) {
212         ec->enableGrDrag();
213     }
215     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
218 static void
219 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
221     SPStarContext *sc = SP_STAR_CONTEXT (ec);
222     Glib::ustring path = val->getEntryName();
224     if (path == "magnitude") {
225         sc->magnitude = NR_CLAMP(val->getInt(5), 3, 1024);
226     } else if (path == "proportion") {
227         sc->proportion = NR_CLAMP(val->getDouble(0.5), 0.01, 2.0);
228     } else if (path == "isflatsided") {
229         sc->isflatsided = val->getBool();
230     } else if (path == "rounded") {
231         sc->rounded = val->getDouble();
232     } else if (path == "randomized") {
233         sc->randomized = val->getDouble();
234     }
237 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
239     static gboolean dragging;
241     SPDesktop *desktop = event_context->desktop;
242     Inkscape::Selection *selection = sp_desktop_selection (desktop);
243     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
245     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
247     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
249     gint ret = FALSE;
251     switch (event->type) {
252     case GDK_BUTTON_PRESS:
253         if (event->button.button == 1 && !event_context->space_panning) {
255             dragging = TRUE;
257             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
259             /* Snap center */
260             SnapManager &m = desktop->namedview->snap_manager;
261             m.setup(desktop, true);
262             m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
264             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
265                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
266                                 GDK_POINTER_MOTION_MASK |
267                                 GDK_POINTER_MOTION_HINT_MASK |
268                                 GDK_BUTTON_PRESS_MASK,
269                                 NULL, event->button.time);
270             ret = TRUE;
271         }
272         break;
273     case GDK_MOTION_NOTIFY:
274         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
276             if ( event_context->within_tolerance
277                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
278                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
279                 break; // do not drag if we're within tolerance from origin
280             }
281             // Once the user has moved farther than tolerance from the original location
282             // (indicating they intend to draw, not click), then always process the
283             // motion notify coordinates as given (no snapping back to origin)
284             event_context->within_tolerance = false;
286             Geom::Point const motion_w(event->motion.x, event->motion.y);
287             Geom::Point motion_dt(desktop->w2d(motion_w));
289             sp_star_drag (sc, motion_dt, event->motion.state);
291             gobble_motion_events(GDK_BUTTON1_MASK);
293             ret = TRUE;
294         }
295         break;
296     case GDK_BUTTON_RELEASE:
297         event_context->xp = event_context->yp = 0;
298         if (event->button.button == 1 && !event_context->space_panning) {
299             dragging = FALSE;
300             sp_event_context_discard_delayed_snap_event(event_context);
301             if (!event_context->within_tolerance) {
302                 // we've been dragging, finish the star
303                 sp_star_finish (sc);
304             } else if (event_context->item_to_select) {
305                 // no dragging, select clicked item if any
306                 if (event->button.state & GDK_SHIFT_MASK) {
307                     selection->toggle(event_context->item_to_select);
308                 } else {
309                     selection->set(event_context->item_to_select);
310                 }
311             } else {
312                 // click in an empty space
313                 selection->clear();
314             }
316             event_context->item_to_select = NULL;
317             ret = TRUE;
318             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
319         }
320         break;
321     case GDK_KEY_PRESS:
322         switch (get_group0_keyval(&event->key)) {
323         case GDK_Alt_R:
324         case GDK_Control_L:
325         case GDK_Control_R:
326         case GDK_Shift_L:
327         case GDK_Shift_R:
328         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
329         case GDK_Meta_R:
330             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
331                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
332                                        NULL,
333                                        NULL);
334             break;
335         case GDK_Up:
336         case GDK_Down:
337         case GDK_KP_Up:
338         case GDK_KP_Down:
339             // prevent the zoom field from activation
340             if (!MOD__CTRL_ONLY)
341                 ret = TRUE;
342             break;
343         case GDK_x:
344         case GDK_X:
345             if (MOD__ALT_ONLY) {
346                 desktop->setToolboxFocusTo ("altx-star");
347                 ret = TRUE;
348             }
349             break;
350         case GDK_Escape:
351                 if (dragging) {
352                         dragging = false;
353                         sp_event_context_discard_delayed_snap_event(event_context);
354                         // if drawing, cancel, otherwise pass it up for deselecting
355                         sp_star_cancel(sc);
356                         ret = TRUE;
357                 }
358                 break;
359         case GDK_space:
360             if (dragging) {
361                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
362                                       event->button.time);
363                 dragging = false;
364                 sp_event_context_discard_delayed_snap_event(event_context);
365                 if (!event_context->within_tolerance) {
366                     // we've been dragging, finish the star
367                     sp_star_finish(sc);
368                 }
369                 // do not return true, so that space would work switching to selector
370             }
371             break;
373         default:
374             break;
375         }
376         break;
377     case GDK_KEY_RELEASE:
378         switch (get_group0_keyval (&event->key)) {
379         case GDK_Alt_L:
380         case GDK_Alt_R:
381         case GDK_Control_L:
382         case GDK_Control_R:
383         case GDK_Shift_L:
384         case GDK_Shift_R:
385         case GDK_Meta_L:  // Meta is when you press Shift+Alt
386         case GDK_Meta_R:
387             event_context->defaultMessageContext()->clear();
388             break;
389         default:
390             break;
391         }
392         break;
393     default:
394         break;
395     }
397     if (!ret) {
398         if (((SPEventContextClass *) parent_class)->root_handler)
399             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
400     }
402     return ret;
405 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
407     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
409     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
410     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
412     if (!sc->item) {
414         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
415             return;
416         }
418         /* Create object */
419         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
420         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
421         repr->setAttribute("sodipodi:type", "star");
423         /* Set style */
424         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
426         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
427         Inkscape::GC::release(repr);
428         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
429         sc->item->updateRepr();
431         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
432     }
434     /* Snap corner point with no constraints */
435     SnapManager &m = desktop->namedview->snap_manager;
436     m.setup(desktop, true, sc->item);
437     Geom::Point pt2g = to_2geom(p);
438     m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
440     Geom::Point const p0 = desktop->dt2doc(sc->center);
441     Geom::Point const p1 = desktop->dt2doc(pt2g);
443     SPStar *star = SP_STAR(sc->item);
445     double const sides = (gdouble) sc->magnitude;
446     Geom::Point const d = p1 - p0;
447     Geom::Coord const r1 = Geom::L2(d);
448     double arg1 = atan2(from_2geom(d));
450     if (state & GDK_CONTROL_MASK) {
451         /* Snap angle */
452         arg1 = sp_round(arg1, M_PI / snaps);
453     }
455     sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
456                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
458     /* status text */
459     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
460     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
461                                ( sc->isflatsided?
462                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
463                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
464                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
466     g_string_free(rads, FALSE);
469 static void
470 sp_star_finish (SPStarContext * sc)
472     sc->_message_context->clear();
474     if (sc->item != NULL) {
475         SPStar *star = SP_STAR(sc->item);
476         if (star->r[1] == 0) {
477                 sp_star_cancel(sc); // Don't allow the creating of zero sized arc, for example when the start and and point snap to the snap grid point
478                 return;
479         }
481         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
482         SPObject *object = SP_OBJECT(sc->item);
484         sp_shape_set_shape(SP_SHAPE(sc->item));
486         object->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_STAR,
492                          _("Create star"));
494         sc->item = NULL;
495     }
498 static void sp_star_cancel(SPStarContext *sc)
500         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
502         sp_desktop_selection(desktop)->clear();
503         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
505     if (sc->item != NULL) {
506         SP_OBJECT(sc->item)->deleteObject();
507         sc->item = NULL;
508     }
510     sc->within_tolerance = false;
511     sc->xp = 0;
512     sc->yp = 0;
513     sc->item_to_select = NULL;
515     sp_canvas_end_forced_full_redraws(desktop->canvas);
517     sp_document_cancel(sp_desktop_document(desktop));
520 /*
521   Local Variables:
522   mode:c++
523   c-file-style:"stroustrup"
524   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
525   indent-tabs-mode:nil
526   fill-column:99
527   End:
528 */
529 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :