Code

Make stars and spirals escapable
[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 "pixmaps/cursor-star.xpm"
38 #include "sp-metrics.h"
39 #include <glibmm/i18n.h>
40 #include "preferences.h"
41 #include "xml/repr.h"
42 #include "xml/node-event-vector.h"
43 #include "object-edit.h"
44 #include "context-fns.h"
45 #include "shape-editor.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, Inkscape::Preferences::Entry *val);
55 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
57 static void sp_star_drag (SPStarContext * sc, Geom::Point p, guint state);
58 static void sp_star_finish (SPStarContext * sc);
59 static void sp_star_cancel(SPStarContext * sc);
61 static SPEventContextClass * parent_class;
63 GtkType
64 sp_star_context_get_type (void)
65 {
66     static GType type = 0;
67     if (!type) {
68         GTypeInfo info = {
69             sizeof (SPStarContextClass),
70             NULL, NULL,
71             (GClassInitFunc) sp_star_context_class_init,
72             NULL, NULL,
73             sizeof (SPStarContext),
74             4,
75             (GInstanceInitFunc) sp_star_context_init,
76             NULL,    /* value_table */
77         };
78         type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
79     }
80     return type;
81 }
83 static void
84 sp_star_context_class_init (SPStarContextClass * klass)
85 {
86     GObjectClass *object_class = (GObjectClass *) klass;
87     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89     parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
91     object_class->dispose = sp_star_context_dispose;
93     event_context_class->setup = sp_star_context_setup;
94     event_context_class->set = sp_star_context_set;
95     event_context_class->root_handler = sp_star_context_root_handler;
96 }
98 static void
99 sp_star_context_init (SPStarContext * star_context)
101     SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
103     event_context->cursor_shape = cursor_star_xpm;
104     event_context->hot_x = 4;
105     event_context->hot_y = 4;
106     event_context->xp = 0;
107     event_context->yp = 0;
108     event_context->tolerance = 0;
109     event_context->within_tolerance = false;
110     event_context->item_to_select = NULL;
112     star_context->item = NULL;
114     star_context->magnitude = 5;
115     star_context->proportion = 0.5;
116     star_context->isflatsided = false;
118     new (&star_context->sel_changed_connection) sigc::connection();
121 static void
122 sp_star_context_dispose (GObject *object)
124     SPEventContext *ec = SP_EVENT_CONTEXT (object);
125     SPStarContext *sc = SP_STAR_CONTEXT (object);
127     ec->enableGrDrag(false);
129     sc->sel_changed_connection.disconnect();
130     sc->sel_changed_connection.~connection();
132     delete ec->shape_editor;
133     ec->shape_editor = NULL;
135     /* fixme: This is necessary because we do not grab */
136     if (sc->item) sp_star_finish (sc);
138     if (sc->_message_context) {
139         delete sc->_message_context;
140     }
142     G_OBJECT_CLASS (parent_class)->dispose (object);
145 /**
146 \brief  Callback that processes the "changed" signal on the selection;
147 destroys old and creates new knotholder
148 \param  selection Should not be NULL.
149 */
150 void
151 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
153     g_assert (selection != NULL);
155     SPStarContext *sc = SP_STAR_CONTEXT (data);
156     SPEventContext *ec = SP_EVENT_CONTEXT (sc);
158     ec->shape_editor->unset_item(SH_KNOTHOLDER);
159     SPItem *item = selection->singleItem();
160     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
163 static void
164 sp_star_context_setup (SPEventContext *ec)
166    SPStarContext *sc = SP_STAR_CONTEXT (ec);
168     if (((SPEventContextClass *) parent_class)->setup)
169         ((SPEventContextClass *) parent_class)->setup (ec);
171     sp_event_context_read (ec, "magnitude");
172     sp_event_context_read (ec, "proportion");
173     sp_event_context_read (ec, "isflatsided");
174     sp_event_context_read (ec, "rounded");
175     sp_event_context_read (ec, "randomized");
177     ec->shape_editor = new ShapeEditor(ec->desktop);
179     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
180     if (item) {
181         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
182     }
184     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
185     sc->sel_changed_connection.disconnect();
186     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
188     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
189     if (prefs->getBool("/tools/shapes/selcue")) {
190         ec->enableSelectionCue();
191     }
193     if (prefs->getBool("/tools/shapes/gradientdrag")) {
194         ec->enableGrDrag();
195     }
197     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
200 static void
201 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
203     SPStarContext *sc = SP_STAR_CONTEXT (ec);
204     Glib::ustring path = val->getEntryName();
206     if (path == "magnitude") {
207         sc->magnitude = CLAMP (val->getInt(5), 3, 1024);
208     } else if (path == "proportion") {
209         sc->proportion = CLAMP (val->getDouble(0.5), 0.01, 2.0);
210     } else if (path == "isflatsided") {
211         sc->isflatsided = val->getBool();
212     } else if (path == "rounded") {
213         sc->rounded = val->getDouble();
214     } else if (path == "randomized") {
215         sc->randomized = val->getDouble();
216     }
219 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
221     static gboolean dragging;
223     SPDesktop *desktop = event_context->desktop;
224     Inkscape::Selection *selection = sp_desktop_selection (desktop);
225     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
227     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
229     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
231     gint ret = FALSE;
233     switch (event->type) {
234     case GDK_BUTTON_PRESS:
235         if (event->button.button == 1 && !event_context->space_panning) {
237             dragging = TRUE;
238             sp_event_context_snap_window_open(event_context);
240             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
242             /* Snap center */
243             SnapManager &m = desktop->namedview->snap_manager;
244             m.setup(desktop, true);
245             Geom::Point pt2g = to_2geom(sc->center);
246             m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
247             sc->center = from_2geom(pt2g);
249             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
250                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
251                                 GDK_POINTER_MOTION_MASK |
252                                 GDK_POINTER_MOTION_HINT_MASK |
253                                 GDK_BUTTON_PRESS_MASK,
254                                 NULL, event->button.time);
255             ret = TRUE;
256         }
257         break;
258     case GDK_MOTION_NOTIFY:
259         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
261             if ( event_context->within_tolerance
262                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
263                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
264                 break; // do not drag if we're within tolerance from origin
265             }
266             // Once the user has moved farther than tolerance from the original location
267             // (indicating they intend to draw, not click), then always process the
268             // motion notify coordinates as given (no snapping back to origin)
269             event_context->within_tolerance = false;
271             Geom::Point const motion_w(event->motion.x, event->motion.y);
272             Geom::Point motion_dt(desktop->w2d(motion_w));
274             sp_star_drag (sc, motion_dt, event->motion.state);
276             gobble_motion_events(GDK_BUTTON1_MASK);
278             ret = TRUE;
279         }
280         break;
281     case GDK_BUTTON_RELEASE:
282         event_context->xp = event_context->yp = 0;
283         if (event->button.button == 1 && !event_context->space_panning) {
284             dragging = FALSE;
285             sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
286             if (!event_context->within_tolerance) {
287                 // we've been dragging, finish the star
288                 sp_star_finish (sc);
289             } else if (event_context->item_to_select) {
290                 // no dragging, select clicked item if any
291                 if (event->button.state & GDK_SHIFT_MASK) {
292                     selection->toggle(event_context->item_to_select);
293                 } else {
294                     selection->set(event_context->item_to_select);
295                 }
296             } else {
297                 // click in an empty space
298                 selection->clear();
299             }
301             event_context->item_to_select = NULL;
302             ret = TRUE;
303             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
304         }
305         break;
306     case GDK_KEY_PRESS:
307         switch (get_group0_keyval(&event->key)) {
308         case GDK_Alt_R:
309         case GDK_Control_L:
310         case GDK_Control_R:
311         case GDK_Shift_L:
312         case GDK_Shift_R:
313         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
314         case GDK_Meta_R:
315             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
316                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
317                                        NULL,
318                                        NULL);
319             break;
320         case GDK_Up:
321         case GDK_Down:
322         case GDK_KP_Up:
323         case GDK_KP_Down:
324             // prevent the zoom field from activation
325             if (!MOD__CTRL_ONLY)
326                 ret = TRUE;
327             break;
328         case GDK_x:
329         case GDK_X:
330             if (MOD__ALT_ONLY) {
331                 desktop->setToolboxFocusTo ("altx-star");
332                 ret = TRUE;
333             }
334             break;
335         case GDK_Escape:
336                 if (dragging) {
337                         dragging = false;
338                         sp_event_context_snap_window_closed(event_context);
339                         // if drawing, cancel, otherwise pass it up for deselecting
340                         sp_star_cancel(sc);
341                         ret = TRUE;
342                 }
343                 break;
344         case GDK_space:
345             if (dragging) {
346                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
347                                       event->button.time);
348                 dragging = false;
349                 sp_event_context_snap_window_closed(event_context);
350                 if (!event_context->within_tolerance) {
351                     // we've been dragging, finish the star
352                     sp_star_finish(sc);
353                 }
354                 // do not return true, so that space would work switching to selector
355             }
356             break;
358         default:
359             break;
360         }
361         break;
362     case GDK_KEY_RELEASE:
363         switch (get_group0_keyval (&event->key)) {
364         case GDK_Alt_L:
365         case GDK_Alt_R:
366         case GDK_Control_L:
367         case GDK_Control_R:
368         case GDK_Shift_L:
369         case GDK_Shift_R:
370         case GDK_Meta_L:  // Meta is when you press Shift+Alt
371         case GDK_Meta_R:
372             event_context->defaultMessageContext()->clear();
373             break;
374         default:
375             break;
376         }
377         break;
378     default:
379         break;
380     }
382     if (!ret) {
383         if (((SPEventContextClass *) parent_class)->root_handler)
384             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
385     }
387     return ret;
390 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
392     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
394     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
395     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
397     if (!sc->item) {
399         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
400             return;
401         }
403         /* Create object */
404         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
405         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
406         repr->setAttribute("sodipodi:type", "star");
408         /* Set style */
409         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
411         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
412         Inkscape::GC::release(repr);
413         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
414         sc->item->updateRepr();
416         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
417     }
419     /* Snap corner point with no constraints */
420     SnapManager &m = desktop->namedview->snap_manager;
421     m.setup(desktop, true, sc->item);
422     Geom::Point pt2g = to_2geom(p);
423     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
425     Geom::Point const p0 = desktop->dt2doc(sc->center);
426     Geom::Point const p1 = desktop->dt2doc(pt2g);
428     SPStar *star = SP_STAR(sc->item);
430     double const sides = (gdouble) sc->magnitude;
431     Geom::Point const d = p1 - p0;
432     Geom::Coord const r1 = Geom::L2(d);
433     double arg1 = atan2(from_2geom(d));
435     if (state & GDK_CONTROL_MASK) {
436         /* Snap angle */
437         arg1 = sp_round(arg1, M_PI / snaps);
438     }
440     sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
441                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
443     /* status text */
444     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
445     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
446                                ( sc->isflatsided?
447                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
448                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
449                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
451     g_string_free(rads, FALSE);
454 static void
455 sp_star_finish (SPStarContext * sc)
457     sc->_message_context->clear();
459     if (sc->item != NULL) {
460         SPStar *star = SP_STAR(sc->item);
461         if (star->r[1] == 0) {
462                 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
463                 return;
464         }
466         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
467         SPObject *object = SP_OBJECT(sc->item);
469         sp_shape_set_shape(SP_SHAPE(sc->item));
471         object->updateRepr(SP_OBJECT_WRITE_EXT);
473         sp_canvas_end_forced_full_redraws(desktop->canvas);
475         sp_desktop_selection(desktop)->set(sc->item);
476         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
477                          _("Create star"));
479         sc->item = NULL;
480     }
483 static void sp_star_cancel(SPStarContext *sc)
485         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
487         sp_desktop_selection(desktop)->clear();
488         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
490     if (sc->item != NULL) {
491         SP_OBJECT(sc->item)->deleteObject();
492         sc->item = NULL;
493     }
495     sc->within_tolerance = false;
496     sc->xp = 0;
497     sc->yp = 0;
498     sc->item_to_select = NULL;
500     sp_canvas_end_forced_full_redraws(desktop->canvas);
502     sp_document_cancel(sp_desktop_document(desktop));
505 /*
506   Local Variables:
507   mode:c++
508   c-file-style:"stroustrup"
509   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
510   indent-tabs-mode:nil
511   fill-column:99
512   End:
513 */
514 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :