Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / star-context.cpp
1 /*
2  * Star drawing context
3  *
4  * Authors:
5  *   Mitsuru Oka <oka326@parkcity.ne.jp>
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Jon A. Cruz <jon@joncruz.org>
9  *   Abhishek Sharma
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 using Inkscape::DocumentUndo;
52 static void sp_star_context_class_init (SPStarContextClass * klass);
53 static void sp_star_context_init (SPStarContext * star_context);
54 static void sp_star_context_dispose (GObject *object);
56 static void sp_star_context_setup (SPEventContext *ec);
57 static void sp_star_context_finish(SPEventContext *ec);
58 static void sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val);
59 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
61 static void sp_star_drag (SPStarContext * sc, Geom::Point p, guint state);
62 static void sp_star_finish (SPStarContext * sc);
63 static void sp_star_cancel(SPStarContext * sc);
65 static SPEventContextClass * parent_class;
67 GtkType
68 sp_star_context_get_type (void)
69 {
70     static GType type = 0;
71     if (!type) {
72         GTypeInfo info = {
73             sizeof (SPStarContextClass),
74             NULL, NULL,
75             (GClassInitFunc) sp_star_context_class_init,
76             NULL, NULL,
77             sizeof (SPStarContext),
78             4,
79             (GInstanceInitFunc) sp_star_context_init,
80             NULL,    /* value_table */
81         };
82         type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
83     }
84     return type;
85 }
87 static void
88 sp_star_context_class_init (SPStarContextClass * klass)
89 {
90     GObjectClass *object_class = (GObjectClass *) klass;
91     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
93     parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
95     object_class->dispose = sp_star_context_dispose;
97     event_context_class->setup = sp_star_context_setup;
98     event_context_class->finish = sp_star_context_finish;
99     event_context_class->set = sp_star_context_set;
100     event_context_class->root_handler = sp_star_context_root_handler;
103 static void
104 sp_star_context_init (SPStarContext * star_context)
106     SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
108     event_context->cursor_shape = cursor_star_xpm;
109     event_context->hot_x = 4;
110     event_context->hot_y = 4;
111     event_context->xp = 0;
112     event_context->yp = 0;
113     event_context->tolerance = 0;
114     event_context->within_tolerance = false;
115     event_context->item_to_select = NULL;
116     event_context->tool_url = "/tools/shapes/star";
118     star_context->item = NULL;
120     star_context->magnitude = 5;
121     star_context->proportion = 0.5;
122     star_context->isflatsided = false;
124     new (&star_context->sel_changed_connection) sigc::connection();
127 static void sp_star_context_finish(SPEventContext *ec)
129     SPStarContext *sc = SP_STAR_CONTEXT(ec);
130         SPDesktop *desktop = ec->desktop;
132         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
133         sp_star_finish(sc);
134     sc->sel_changed_connection.disconnect();
136     if (((SPEventContextClass *) parent_class)->finish) {
137                 ((SPEventContextClass *) parent_class)->finish(ec);
138         }
142 static void
143 sp_star_context_dispose (GObject *object)
145     SPEventContext *ec = SP_EVENT_CONTEXT (object);
146     SPStarContext *sc = SP_STAR_CONTEXT (object);
148     ec->enableGrDrag(false);
150     sc->sel_changed_connection.disconnect();
151     sc->sel_changed_connection.~connection();
153     delete ec->shape_editor;
154     ec->shape_editor = NULL;
156     /* fixme: This is necessary because we do not grab */
157     if (sc->item) sp_star_finish (sc);
159     if (sc->_message_context) {
160         delete sc->_message_context;
161     }
163     G_OBJECT_CLASS (parent_class)->dispose (object);
166 /**
167 \brief  Callback that processes the "changed" signal on the selection;
168 destroys old and creates new knotholder
169 \param  selection Should not be NULL.
170 */
171 void
172 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
174     g_assert (selection != NULL);
176     SPStarContext *sc = SP_STAR_CONTEXT (data);
177     SPEventContext *ec = SP_EVENT_CONTEXT (sc);
179     ec->shape_editor->unset_item(SH_KNOTHOLDER);
180     SPItem *item = selection->singleItem();
181     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
184 static void
185 sp_star_context_setup (SPEventContext *ec)
187    SPStarContext *sc = SP_STAR_CONTEXT (ec);
189     if (((SPEventContextClass *) parent_class)->setup)
190         ((SPEventContextClass *) parent_class)->setup (ec);
192     sp_event_context_read (ec, "magnitude");
193     sp_event_context_read (ec, "proportion");
194     sp_event_context_read (ec, "isflatsided");
195     sp_event_context_read (ec, "rounded");
196     sp_event_context_read (ec, "randomized");
198     ec->shape_editor = new ShapeEditor(ec->desktop);
200     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
201     if (item) {
202         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
203     }
205     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
206     sc->sel_changed_connection.disconnect();
207     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
209     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
210     if (prefs->getBool("/tools/shapes/selcue")) {
211         ec->enableSelectionCue();
212     }
214     if (prefs->getBool("/tools/shapes/gradientdrag")) {
215         ec->enableGrDrag();
216     }
218     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
221 static void
222 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
224     SPStarContext *sc = SP_STAR_CONTEXT (ec);
225     Glib::ustring path = val->getEntryName();
227     if (path == "magnitude") {
228         sc->magnitude = NR_CLAMP(val->getInt(5), 3, 1024);
229     } else if (path == "proportion") {
230         sc->proportion = NR_CLAMP(val->getDouble(0.5), 0.01, 2.0);
231     } else if (path == "isflatsided") {
232         sc->isflatsided = val->getBool();
233     } else if (path == "rounded") {
234         sc->rounded = val->getDouble();
235     } else if (path == "randomized") {
236         sc->randomized = val->getDouble();
237     }
240 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
242     static gboolean dragging;
244     SPDesktop *desktop = event_context->desktop;
245     Inkscape::Selection *selection = sp_desktop_selection (desktop);
246     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
248     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
250     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
252     gint ret = FALSE;
254     switch (event->type) {
255     case GDK_BUTTON_PRESS:
256         if (event->button.button == 1 && !event_context->space_panning) {
258             dragging = TRUE;
260             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
262             /* Snap center */
263             SnapManager &m = desktop->namedview->snap_manager;
264             m.setup(desktop, true);
265             m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
266             m.unSetup();
267             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
268                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
269                                 GDK_POINTER_MOTION_MASK |
270                                 GDK_POINTER_MOTION_HINT_MASK |
271                                 GDK_BUTTON_PRESS_MASK,
272                                 NULL, event->button.time);
273             ret = TRUE;
274         }
275         break;
276     case GDK_MOTION_NOTIFY:
277         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
279             if ( event_context->within_tolerance
280                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
281                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
282                 break; // do not drag if we're within tolerance from origin
283             }
284             // Once the user has moved farther than tolerance from the original location
285             // (indicating they intend to draw, not click), then always process the
286             // motion notify coordinates as given (no snapping back to origin)
287             event_context->within_tolerance = false;
289             Geom::Point const motion_w(event->motion.x, event->motion.y);
290             Geom::Point motion_dt(desktop->w2d(motion_w));
292             sp_star_drag (sc, motion_dt, event->motion.state);
294             gobble_motion_events(GDK_BUTTON1_MASK);
296             ret = TRUE;
297         } else if (!sp_event_context_knot_mouseover(event_context)) {
298             SnapManager &m = desktop->namedview->snap_manager;
299             m.setup(desktop);
301             Geom::Point const motion_w(event->motion.x, event->motion.y);
302             Geom::Point motion_dt(desktop->w2d(motion_w));
304             m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
305             m.unSetup();
306         }
307         break;
308     case GDK_BUTTON_RELEASE:
309         event_context->xp = event_context->yp = 0;
310         if (event->button.button == 1 && !event_context->space_panning) {
311             dragging = FALSE;
312             sp_event_context_discard_delayed_snap_event(event_context);
313             if (!event_context->within_tolerance) {
314                 // we've been dragging, finish the star
315                 sp_star_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; keep rays radial"),
344                                        NULL,
345                                        NULL);
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-star");
359                 ret = TRUE;
360             }
361             break;
362         case GDK_Escape:
363                 if (dragging) {
364                         dragging = false;
365                         sp_event_context_discard_delayed_snap_event(event_context);
366                         // if drawing, cancel, otherwise pass it up for deselecting
367                         sp_star_cancel(sc);
368                         ret = TRUE;
369                 }
370                 break;
371         case GDK_space:
372             if (dragging) {
373                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
374                                       event->button.time);
375                 dragging = false;
376                 sp_event_context_discard_delayed_snap_event(event_context);
377                 if (!event_context->within_tolerance) {
378                     // we've been dragging, finish the star
379                     sp_star_finish(sc);
380                 }
381                 // do not return true, so that space would work switching to selector
382             }
383             break;
385         default:
386             break;
387         }
388         break;
389     case GDK_KEY_RELEASE:
390         switch (get_group0_keyval (&event->key)) {
391         case GDK_Alt_L:
392         case GDK_Alt_R:
393         case GDK_Control_L:
394         case GDK_Control_R:
395         case GDK_Shift_L:
396         case GDK_Shift_R:
397         case GDK_Meta_L:  // Meta is when you press Shift+Alt
398         case GDK_Meta_R:
399             event_context->defaultMessageContext()->clear();
400             break;
401         default:
402             break;
403         }
404         break;
405     default:
406         break;
407     }
409     if (!ret) {
410         if (((SPEventContextClass *) parent_class)->root_handler)
411             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
412     }
414     return ret;
417 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
419     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
421     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
422     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
424     if (!sc->item) {
426         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
427             return;
428         }
430         // Create object
431         Inkscape::XML::Document *xml_doc = SP_EVENT_CONTEXT_DOCUMENT(sc)->getReprDoc();
432         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
433         repr->setAttribute("sodipodi:type", "star");
435         // Set style
436         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
438         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
439         Inkscape::GC::release(repr);
440         sc->item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
441         sc->item->updateRepr();
443         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
444     }
446     /* Snap corner point with no constraints */
447     SnapManager &m = desktop->namedview->snap_manager;
448     m.setup(desktop, true, sc->item);
449     Geom::Point pt2g = to_2geom(p);
450     m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
451     m.unSetup();
452     Geom::Point const p0 = desktop->dt2doc(sc->center);
453     Geom::Point const p1 = desktop->dt2doc(pt2g);
455     SPStar *star = SP_STAR(sc->item);
457     double const sides = (gdouble) sc->magnitude;
458     Geom::Point const d = p1 - p0;
459     Geom::Coord const r1 = Geom::L2(d);
460     double arg1 = atan2(from_2geom(d));
462     if (state & GDK_CONTROL_MASK) {
463         /* Snap angle */
464         arg1 = sp_round(arg1, M_PI / snaps);
465     }
467     sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
468                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
470     /* status text */
471     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
472     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
473                                ( sc->isflatsided?
474                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
475                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
476                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
478     g_string_free(rads, FALSE);
481 static void
482 sp_star_finish (SPStarContext * sc)
484     sc->_message_context->clear();
486     if (sc->item != NULL) {
487         SPStar *star = SP_STAR(sc->item);
488         if (star->r[1] == 0) {
489             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
490             return;
491         }
493         // Set transform center, so that odd stars rotate correctly
494         // LP #462157
495         sc->item->setCenter(sc->center);
497         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
498         SPObject *object = SP_OBJECT(sc->item);
500         (SP_SHAPE(sc->item))->setShape();
502         object->updateRepr(SP_OBJECT_WRITE_EXT);
504         sp_canvas_end_forced_full_redraws(desktop->canvas);
506         sp_desktop_selection(desktop)->set(sc->item);
507         DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
508                            _("Create star"));
510         sc->item = NULL;
511     }
514 static void sp_star_cancel(SPStarContext *sc)
516     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
518     sp_desktop_selection(desktop)->clear();
519     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
521     if (sc->item != NULL) {
522         SP_OBJECT(sc->item)->deleteObject();
523         sc->item = NULL;
524     }
526     sc->within_tolerance = false;
527     sc->xp = 0;
528     sc->yp = 0;
529     sc->item_to_select = NULL;
531     sp_canvas_end_forced_full_redraws(desktop->canvas);
533     DocumentUndo::cancel(sp_desktop_document(desktop));
536 /*
537   Local Variables:
538   mode:c++
539   c-file-style:"stroustrup"
540   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
541   indent-tabs-mode:nil
542   fill-column:99
543   End:
544 */
545 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :