Code

Pot and Dutch translation update
[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;
114     event_context->tool_url = "/tools/shapes/star";
116     star_context->item = NULL;
118     star_context->magnitude = 5;
119     star_context->proportion = 0.5;
120     star_context->isflatsided = false;
122     new (&star_context->sel_changed_connection) sigc::connection();
125 static void sp_star_context_finish(SPEventContext *ec)
127     SPStarContext *sc = SP_STAR_CONTEXT(ec);
128         SPDesktop *desktop = ec->desktop;
130         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
131         sp_star_finish(sc);
132     sc->sel_changed_connection.disconnect();
134     if (((SPEventContextClass *) parent_class)->finish) {
135                 ((SPEventContextClass *) parent_class)->finish(ec);
136         }
140 static void
141 sp_star_context_dispose (GObject *object)
143     SPEventContext *ec = SP_EVENT_CONTEXT (object);
144     SPStarContext *sc = SP_STAR_CONTEXT (object);
146     ec->enableGrDrag(false);
148     sc->sel_changed_connection.disconnect();
149     sc->sel_changed_connection.~connection();
151     delete ec->shape_editor;
152     ec->shape_editor = NULL;
154     /* fixme: This is necessary because we do not grab */
155     if (sc->item) sp_star_finish (sc);
157     if (sc->_message_context) {
158         delete sc->_message_context;
159     }
161     G_OBJECT_CLASS (parent_class)->dispose (object);
164 /**
165 \brief  Callback that processes the "changed" signal on the selection;
166 destroys old and creates new knotholder
167 \param  selection Should not be NULL.
168 */
169 void
170 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
172     g_assert (selection != NULL);
174     SPStarContext *sc = SP_STAR_CONTEXT (data);
175     SPEventContext *ec = SP_EVENT_CONTEXT (sc);
177     ec->shape_editor->unset_item(SH_KNOTHOLDER);
178     SPItem *item = selection->singleItem();
179     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
182 static void
183 sp_star_context_setup (SPEventContext *ec)
185    SPStarContext *sc = SP_STAR_CONTEXT (ec);
187     if (((SPEventContextClass *) parent_class)->setup)
188         ((SPEventContextClass *) parent_class)->setup (ec);
190     sp_event_context_read (ec, "magnitude");
191     sp_event_context_read (ec, "proportion");
192     sp_event_context_read (ec, "isflatsided");
193     sp_event_context_read (ec, "rounded");
194     sp_event_context_read (ec, "randomized");
196     ec->shape_editor = new ShapeEditor(ec->desktop);
198     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
199     if (item) {
200         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
201     }
203     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
204     sc->sel_changed_connection.disconnect();
205     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
207     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
208     if (prefs->getBool("/tools/shapes/selcue")) {
209         ec->enableSelectionCue();
210     }
212     if (prefs->getBool("/tools/shapes/gradientdrag")) {
213         ec->enableGrDrag();
214     }
216     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
219 static void
220 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
222     SPStarContext *sc = SP_STAR_CONTEXT (ec);
223     Glib::ustring path = val->getEntryName();
225     if (path == "magnitude") {
226         sc->magnitude = NR_CLAMP(val->getInt(5), 3, 1024);
227     } else if (path == "proportion") {
228         sc->proportion = NR_CLAMP(val->getDouble(0.5), 0.01, 2.0);
229     } else if (path == "isflatsided") {
230         sc->isflatsided = val->getBool();
231     } else if (path == "rounded") {
232         sc->rounded = val->getDouble();
233     } else if (path == "randomized") {
234         sc->randomized = val->getDouble();
235     }
238 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
240     static gboolean dragging;
242     SPDesktop *desktop = event_context->desktop;
243     Inkscape::Selection *selection = sp_desktop_selection (desktop);
244     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
246     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
248     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
250     gint ret = FALSE;
252     switch (event->type) {
253     case GDK_BUTTON_PRESS:
254         if (event->button.button == 1 && !event_context->space_panning) {
256             dragging = TRUE;
258             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
260             /* Snap center */
261             SnapManager &m = desktop->namedview->snap_manager;
262             m.setup(desktop, true);
263             m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
264             m.unSetup();
265             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
266                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
267                                 GDK_POINTER_MOTION_MASK |
268                                 GDK_POINTER_MOTION_HINT_MASK |
269                                 GDK_BUTTON_PRESS_MASK,
270                                 NULL, event->button.time);
271             ret = TRUE;
272         }
273         break;
274     case GDK_MOTION_NOTIFY:
275         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
277             if ( event_context->within_tolerance
278                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
279                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
280                 break; // do not drag if we're within tolerance from origin
281             }
282             // Once the user has moved farther than tolerance from the original location
283             // (indicating they intend to draw, not click), then always process the
284             // motion notify coordinates as given (no snapping back to origin)
285             event_context->within_tolerance = false;
287             Geom::Point const motion_w(event->motion.x, event->motion.y);
288             Geom::Point motion_dt(desktop->w2d(motion_w));
290             sp_star_drag (sc, motion_dt, event->motion.state);
292             gobble_motion_events(GDK_BUTTON1_MASK);
294             ret = TRUE;
295         } else if (!sp_event_context_knot_mouseover(event_context)) {
296             SnapManager &m = desktop->namedview->snap_manager;
297             m.setup(desktop);
299             Geom::Point const motion_w(event->motion.x, event->motion.y);
300             Geom::Point motion_dt(desktop->w2d(motion_w));
302             m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
303             m.unSetup();
304         }
305         break;
306     case GDK_BUTTON_RELEASE:
307         event_context->xp = event_context->yp = 0;
308         if (event->button.button == 1 && !event_context->space_panning) {
309             dragging = FALSE;
310             sp_event_context_discard_delayed_snap_event(event_context);
311             if (!event_context->within_tolerance) {
312                 // we've been dragging, finish the star
313                 sp_star_finish (sc);
314             } else if (event_context->item_to_select) {
315                 // no dragging, select clicked item if any
316                 if (event->button.state & GDK_SHIFT_MASK) {
317                     selection->toggle(event_context->item_to_select);
318                 } else {
319                     selection->set(event_context->item_to_select);
320                 }
321             } else {
322                 // click in an empty space
323                 selection->clear();
324             }
326             event_context->item_to_select = NULL;
327             ret = TRUE;
328             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
329         }
330         break;
331     case GDK_KEY_PRESS:
332         switch (get_group0_keyval(&event->key)) {
333         case GDK_Alt_R:
334         case GDK_Control_L:
335         case GDK_Control_R:
336         case GDK_Shift_L:
337         case GDK_Shift_R:
338         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
339         case GDK_Meta_R:
340             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
341                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
342                                        NULL,
343                                        NULL);
344             break;
345         case GDK_Up:
346         case GDK_Down:
347         case GDK_KP_Up:
348         case GDK_KP_Down:
349             // prevent the zoom field from activation
350             if (!MOD__CTRL_ONLY)
351                 ret = TRUE;
352             break;
353         case GDK_x:
354         case GDK_X:
355             if (MOD__ALT_ONLY) {
356                 desktop->setToolboxFocusTo ("altx-star");
357                 ret = TRUE;
358             }
359             break;
360         case GDK_Escape:
361                 if (dragging) {
362                         dragging = false;
363                         sp_event_context_discard_delayed_snap_event(event_context);
364                         // if drawing, cancel, otherwise pass it up for deselecting
365                         sp_star_cancel(sc);
366                         ret = TRUE;
367                 }
368                 break;
369         case GDK_space:
370             if (dragging) {
371                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
372                                       event->button.time);
373                 dragging = false;
374                 sp_event_context_discard_delayed_snap_event(event_context);
375                 if (!event_context->within_tolerance) {
376                     // we've been dragging, finish the star
377                     sp_star_finish(sc);
378                 }
379                 // do not return true, so that space would work switching to selector
380             }
381             break;
383         default:
384             break;
385         }
386         break;
387     case GDK_KEY_RELEASE:
388         switch (get_group0_keyval (&event->key)) {
389         case GDK_Alt_L:
390         case GDK_Alt_R:
391         case GDK_Control_L:
392         case GDK_Control_R:
393         case GDK_Shift_L:
394         case GDK_Shift_R:
395         case GDK_Meta_L:  // Meta is when you press Shift+Alt
396         case GDK_Meta_R:
397             event_context->defaultMessageContext()->clear();
398             break;
399         default:
400             break;
401         }
402         break;
403     default:
404         break;
405     }
407     if (!ret) {
408         if (((SPEventContextClass *) parent_class)->root_handler)
409             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
410     }
412     return ret;
415 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
417     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
419     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
420     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
422     if (!sc->item) {
424         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
425             return;
426         }
428         /* Create object */
429         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
430         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
431         repr->setAttribute("sodipodi:type", "star");
433         /* Set style */
434         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
436         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
437         Inkscape::GC::release(repr);
438         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
439         sc->item->updateRepr();
441         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
442     }
444     /* Snap corner point with no constraints */
445     SnapManager &m = desktop->namedview->snap_manager;
446     m.setup(desktop, true, sc->item);
447     Geom::Point pt2g = to_2geom(p);
448     m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
449     m.unSetup();
450     Geom::Point const p0 = desktop->dt2doc(sc->center);
451     Geom::Point const p1 = desktop->dt2doc(pt2g);
453     SPStar *star = SP_STAR(sc->item);
455     double const sides = (gdouble) sc->magnitude;
456     Geom::Point const d = p1 - p0;
457     Geom::Coord const r1 = Geom::L2(d);
458     double arg1 = atan2(from_2geom(d));
460     if (state & GDK_CONTROL_MASK) {
461         /* Snap angle */
462         arg1 = sp_round(arg1, M_PI / snaps);
463     }
465     sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
466                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
468     /* status text */
469     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
470     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
471                                ( sc->isflatsided?
472                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
473                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
474                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
476     g_string_free(rads, FALSE);
479 static void
480 sp_star_finish (SPStarContext * sc)
482     sc->_message_context->clear();
484     if (sc->item != NULL) {
485         SPStar *star = SP_STAR(sc->item);
486         if (star->r[1] == 0) {
487             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
488             return;
489         }
491         // Set transform center, so that odd stars rotate correctly
492         // LP #462157
493         sc->item->setCenter(sc->center);
495         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
496         SPObject *object = SP_OBJECT(sc->item);
498         sp_shape_set_shape(SP_SHAPE(sc->item));
500         object->updateRepr(SP_OBJECT_WRITE_EXT);
502         sp_canvas_end_forced_full_redraws(desktop->canvas);
504         sp_desktop_selection(desktop)->set(sc->item);
505         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
506                          _("Create star"));
508         sc->item = NULL;
509     }
512 static void sp_star_cancel(SPStarContext *sc)
514     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
516     sp_desktop_selection(desktop)->clear();
517     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
519     if (sc->item != NULL) {
520         SP_OBJECT(sc->item)->deleteObject();
521         sc->item = NULL;
522     }
524     sc->within_tolerance = false;
525     sc->xp = 0;
526     sc->yp = 0;
527     sc->item_to_select = NULL;
529     sp_canvas_end_forced_full_redraws(desktop->canvas);
531     sp_document_cancel(sp_desktop_document(desktop));
534 /*
535   Local Variables:
536   mode:c++
537   c-file-style:"stroustrup"
538   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
539   indent-tabs-mode:nil
540   fill-column:99
541   End:
542 */
543 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :