Code

Galician translation update by Leandro Regueiro (Bug #612697)
[inkscape.git] / src / pen-context.cpp
1 /** \file
2  * Pen event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
19 #include <cstring>
20 #include <string>
22 #include "pen-context.h"
23 #include "sp-namedview.h"
24 #include "sp-metrics.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "selection.h"
28 #include "selection-chemistry.h"
29 #include "draw-anchor.h"
30 #include "message-stack.h"
31 #include "message-context.h"
32 #include "preferences.h"
33 #include "sp-path.h"
34 #include "display/curve.h"
35 #include "pixmaps/cursor-pen.xpm"
36 #include "display/canvas-bpath.h"
37 #include "display/sp-ctrlline.h"
38 #include "display/sodipodi-ctrl.h"
39 #include <glibmm/i18n.h>
40 #include "libnr/nr-point-ops.h"
41 #include "helper/units.h"
42 #include "macros.h"
43 #include "context-fns.h"
44 #include "tools-switch.h"
46 static void sp_pen_context_class_init(SPPenContextClass *klass);
47 static void sp_pen_context_init(SPPenContext *pc);
48 static void sp_pen_context_dispose(GObject *object);
50 static void sp_pen_context_setup(SPEventContext *ec);
51 static void sp_pen_context_finish(SPEventContext *ec);
52 static void sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
53 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
54 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void spdc_pen_set_initial_point(SPPenContext *pc, Geom::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status = 0);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, Geom::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, Geom::Point p, guint state);
61 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
63 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
65 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
66 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
67 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
68 static void spdc_reset_colors(SPPenContext *pc);
70 static void pen_disable_events(SPPenContext *const pc);
71 static void pen_enable_events(SPPenContext *const pc);
73 static Geom::Point pen_drag_origin_w(0, 0);
74 static bool pen_within_tolerance = false;
76 static SPDrawContextClass *pen_parent_class;
78 static int pen_next_paraxial_direction(const SPPenContext *const pc, Geom::Point const &pt, Geom::Point const &origin, guint state);
79 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap);
81 static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
83 /**
84  * Register SPPenContext with Gdk and return its type.
85  */
86 GType
87 sp_pen_context_get_type(void)
88 {
89     static GType type = 0;
90     if (!type) {
91         GTypeInfo info = {
92             sizeof(SPPenContextClass),
93             NULL, NULL,
94             (GClassInitFunc) sp_pen_context_class_init,
95             NULL, NULL,
96             sizeof(SPPenContext),
97             4,
98             (GInstanceInitFunc) sp_pen_context_init,
99             NULL,   /* value_table */
100         };
101         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
102     }
103     return type;
106 /**
107  * Initialize the SPPenContext vtable.
108  */
109 static void
110 sp_pen_context_class_init(SPPenContextClass *klass)
112     GObjectClass *object_class;
113     SPEventContextClass *event_context_class;
115     object_class = (GObjectClass *) klass;
116     event_context_class = (SPEventContextClass *) klass;
118     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
120     object_class->dispose = sp_pen_context_dispose;
122     event_context_class->setup = sp_pen_context_setup;
123     event_context_class->finish = sp_pen_context_finish;
124     event_context_class->set = sp_pen_context_set;
125     event_context_class->root_handler = sp_pen_context_root_handler;
126     event_context_class->item_handler = sp_pen_context_item_handler;
129 /**
130  * Callback to initialize SPPenContext object.
131  */
132 static void
133 sp_pen_context_init(SPPenContext *pc)
136     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
138     event_context->cursor_shape = cursor_pen_xpm;
139     event_context->hot_x = 4;
140     event_context->hot_y = 4;
142     pc->npoints = 0;
143     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
144     pc->state = SP_PEN_CONTEXT_POINT;
146     pc->c0 = NULL;
147     pc->c1 = NULL;
148     pc->cl0 = NULL;
149     pc->cl1 = NULL;
151     pc->events_disabled = 0;
153     pc->num_clicks = 0;
154     pc->waiting_LPE = NULL;
155     pc->waiting_item = NULL;
158 /**
159  * Callback to destroy the SPPenContext object's members and itself.
160  */
161 static void
162 sp_pen_context_dispose(GObject *object)
164     SPPenContext *pc;
166     pc = SP_PEN_CONTEXT(object);
168     if (pc->c0) {
169         gtk_object_destroy(GTK_OBJECT(pc->c0));
170         pc->c0 = NULL;
171     }
172     if (pc->c1) {
173         gtk_object_destroy(GTK_OBJECT(pc->c1));
174         pc->c1 = NULL;
175     }
176     if (pc->cl0) {
177         gtk_object_destroy(GTK_OBJECT(pc->cl0));
178         pc->cl0 = NULL;
179     }
180     if (pc->cl1) {
181         gtk_object_destroy(GTK_OBJECT(pc->cl1));
182         pc->cl1 = NULL;
183     }
185     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
187     if (pc->expecting_clicks_for_LPE > 0) {
188         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
189         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
190     }
193 void
194 sp_pen_context_set_polyline_mode(SPPenContext *const pc) {
195     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
196     guint mode = prefs->getInt("/tools/freehand/pen/freehand-mode", 0);
197     pc->polylines_only = (mode == 2 || mode == 3);
198     pc->polylines_paraxial = (mode == 3);
201 /**
202  * Callback to initialize SPPenContext object.
203  */
204 static void
205 sp_pen_context_setup(SPEventContext *ec)
207     SPPenContext *pc;
209     pc = SP_PEN_CONTEXT(ec);
211     if (((SPEventContextClass *) pen_parent_class)->setup) {
212         ((SPEventContextClass *) pen_parent_class)->setup(ec);
213     }
215     /* Pen indicators */
216     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
217                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
218     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
219                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
220     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
221     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
222     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
223     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
225     sp_canvas_item_hide(pc->c0);
226     sp_canvas_item_hide(pc->c1);
227     sp_canvas_item_hide(pc->cl0);
228     sp_canvas_item_hide(pc->cl1);
230     sp_event_context_read(ec, "mode");
232     pc->anchor_statusbar = false;
234     sp_pen_context_set_polyline_mode(pc);
236     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
237     if (prefs->getBool("/tools/freehand/pen/selcue")) {
238         ec->enableSelectionCue();
239     }
242 static void
243 pen_cancel (SPPenContext *const pc)
245     pc->num_clicks = 0;
246     pc->state = SP_PEN_CONTEXT_STOP;
247     spdc_reset_colors(pc);
248     sp_canvas_item_hide(pc->c0);
249     sp_canvas_item_hide(pc->c1);
250     sp_canvas_item_hide(pc->cl0);
251     sp_canvas_item_hide(pc->cl1);
252     pc->_message_context->clear();
253     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
255     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
258 /**
259  * Finalization callback.
260  */
261 static void
262 sp_pen_context_finish(SPEventContext *ec)
264     SPPenContext *pc = SP_PEN_CONTEXT(ec);
266     sp_event_context_discard_delayed_snap_event(ec);
268     if (pc->npoints != 0) {
269         pen_cancel (pc);
270     }
272     if (((SPEventContextClass *) pen_parent_class)->finish) {
273         ((SPEventContextClass *) pen_parent_class)->finish(ec);
274     }
277 /**
278  * Callback that sets key to value in pen context.
279  */
280 static void
281 sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
283     SPPenContext *pc = SP_PEN_CONTEXT(ec);
284     Glib::ustring name = val->getEntryName();
286     if (name == "mode") {
287         if ( val->getString() == "drag" ) {
288             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
289         } else {
290             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
291         }
292     }
295 /**
296  * Snaps new node relative to the previous node.
297  */
298 static void
299 spdc_endpoint_snap(SPPenContext const *const pc, Geom::Point &p, guint const state)
301     if ((state & GDK_CONTROL_MASK) && !pc->polylines_paraxial) { //CTRL enables angular snapping
302         if (pc->npoints > 0) {
303             spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
304         }
305     } else {
306         // We cannot use shift here to disable snapping because the shift-key is already used
307         // to toggle the paraxial direction; if the user wants to disable snapping (s)he will
308         // have to use the %-key, the menu, or the snap toolbar
309         if ((pc->npoints > 0) && pc->polylines_paraxial) {
310             // snap constrained
311             pen_set_to_nearest_horiz_vert(pc, p, state, true);
312         } else {
313             // snap freely
314             spdc_endpoint_snap_free(pc, p, state);
315         }
316     }
319 /**
320  * Snaps new node's handle relative to the new node.
321  */
322 static void
323 spdc_endpoint_snap_handle(SPPenContext const *const pc, Geom::Point &p, guint const state)
325     g_return_if_fail(( pc->npoints == 2 ||
326                        pc->npoints == 5   ));
328     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
329         spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
330     } else {
331         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
332             spdc_endpoint_snap_free(pc, p, state);
333         }
334     }
337 static gint
338 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
340     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
342     gint ret = FALSE;
344     switch (event->type) {
345         case GDK_BUTTON_PRESS:
346             ret = pen_handle_button_press(pc, event->button);
347             break;
348         case GDK_BUTTON_RELEASE:
349             ret = pen_handle_button_release(pc, event->button);
350             break;
351         default:
352             break;
353     }
355     if (!ret) {
356         if (((SPEventContextClass *) pen_parent_class)->item_handler)
357             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
358     }
360     return ret;
363 /**
364  * Callback to handle all pen events.
365  */
366 static gint
367 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
369     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
371     gint ret = FALSE;
373     switch (event->type) {
374         case GDK_BUTTON_PRESS:
375             ret = pen_handle_button_press(pc, event->button);
376             break;
378         case GDK_MOTION_NOTIFY:
379             ret = pen_handle_motion_notify(pc, event->motion);
380             break;
382         case GDK_BUTTON_RELEASE:
383             ret = pen_handle_button_release(pc, event->button);
384             break;
386         case GDK_2BUTTON_PRESS:
387             ret = pen_handle_2button_press(pc, event->button);
388             break;
390         case GDK_KEY_PRESS:
391             ret = pen_handle_key_press(pc, event);
392             break;
394         default:
395             break;
396     }
398     if (!ret) {
399         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
400             = ((SPEventContextClass *) pen_parent_class)->root_handler;
401         if (parent_root_handler) {
402             ret = parent_root_handler(ec, event);
403         }
404     }
406     return ret;
409 /**
410  * Handle mouse button press event.
411  */
412 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
414     if (pc->events_disabled) {
415         // skip event processing if events are disabled
416         return FALSE;
417     }
419     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
420     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
421     Geom::Point const event_w(bevent.x, bevent.y);
422     Geom::Point event_dt(desktop->w2d(event_w));
423     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
425     gint ret = FALSE;
426     if (bevent.button == 1 && !event_context->space_panning
427         // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
428         && pc->expecting_clicks_for_LPE != 1) {
430         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
431             return TRUE;
432         }
434         if (!pc->grab ) {
435             /* Grab mouse, so release will not pass unnoticed */
436             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
437             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
438                                             GDK_BUTTON_RELEASE_MASK |
439                                             GDK_POINTER_MOTION_MASK  ),
440                                 NULL, bevent.time);
441         }
443         pen_drag_origin_w = event_w;
444         pen_within_tolerance = true;
446         /* Test whether we hit any anchor. */
447         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
449         switch (pc->mode) {
450             case SP_PEN_CONTEXT_MODE_CLICK:
451                 /* In click mode we add point on release */
452                 switch (pc->state) {
453                     case SP_PEN_CONTEXT_POINT:
454                     case SP_PEN_CONTEXT_CONTROL:
455                     case SP_PEN_CONTEXT_CLOSE:
456                         break;
457                     case SP_PEN_CONTEXT_STOP:
458                         /* This is allowed, if we just canceled curve */
459                         pc->state = SP_PEN_CONTEXT_POINT;
460                         break;
461                     default:
462                         break;
463                 }
464                 break;
465             case SP_PEN_CONTEXT_MODE_DRAG:
466                 switch (pc->state) {
467                     case SP_PEN_CONTEXT_STOP:
468                         /* This is allowed, if we just canceled curve */
469                     case SP_PEN_CONTEXT_POINT:
470                         if (pc->npoints == 0) {
472                             Geom::Point p;
473                             if ((bevent.state & GDK_CONTROL_MASK) && (pc->polylines_only || pc->polylines_paraxial)) {
474                                 p = event_dt;
475                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
476                                     SnapManager &m = desktop->namedview->snap_manager;
477                                     m.setup(desktop);
478                                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
479                                 }
480                               spdc_create_single_dot(event_context, p, "/tools/freehand/pen", bevent.state);
481                               ret = TRUE;
482                               break;
483                             }
485                             // TODO: Perhaps it would be nicer to rearrange the following case
486                             // distinction so that the case of a waiting LPE is treated separately
488                             /* Set start anchor */
489                             pc->sa = anchor;
490                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
491                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
492                                    a fresh path to be created so don't continue an existing one */
493                                 p = anchor->dp;
494                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
495                             } else {
496                                 // This is the first click of a new curve; deselect item so that
497                                 // this curve is not combined with it (unless it is drawn from its
498                                 // anchor, which is handled by the sibling branch above)
499                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
500                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
501                                     /* if we have a waiting LPE, we need a fresh path to be created
502                                        so don't append to an existing one */
503                                     selection->clear();
504                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
505                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
506                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
507                                 }
509                                 /* Create green anchor */
510                                 p = event_dt;
511                                 spdc_endpoint_snap(pc, p, bevent.state);
512                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
513                             }
514                             spdc_pen_set_initial_point(pc, p);
515                         } else {
517                             /* Set end anchor */
518                             pc->ea = anchor;
519                             Geom::Point p;
520                             if (anchor) {
521                                 p = anchor->dp;
522                                 // we hit an anchor, will finish the curve (either with or without closing)
523                                 // in release handler
524                                 pc->state = SP_PEN_CONTEXT_CLOSE;
526                                 if (pc->green_anchor && pc->green_anchor->active) {
527                                     // we clicked on the current curve start, so close it even if
528                                     // we drag a handle away from it
529                                     dc->green_closed = TRUE;
530                                 }
531                                 ret = TRUE;
532                                 break;
534                             } else {
535                                 p = event_dt;
536                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
537                                 spdc_pen_set_subsequent_point(pc, p, true);
538                             }
539                         }
541                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
542                         ret = TRUE;
543                         break;
544                     case SP_PEN_CONTEXT_CONTROL:
545                         g_warning("Button down in CONTROL state");
546                         break;
547                     case SP_PEN_CONTEXT_CLOSE:
548                         g_warning("Button down in CLOSE state");
549                         break;
550                     default:
551                         break;
552                 }
553                 break;
554             default:
555                 break;
556         }
557     } else if (pc->expecting_clicks_for_LPE == 1 && pc->npoints != 0) {
558         // when the last click for a waiting LPE occurs we want to finish the path
559         spdc_pen_finish_segment(pc, event_dt, bevent.state);
560         if (pc->green_closed) {
561             // finishing at the start anchor, close curve
562             spdc_pen_finish(pc, TRUE);
563         } else {
564             // finishing at some other anchor, finish curve but not close
565             spdc_pen_finish(pc, FALSE);
566         }
568         ret = TRUE;
569     } else if (bevent.button == 3 && pc->npoints != 0) {
570         // right click - finish path
571         spdc_pen_finish(pc, FALSE);
572         ret = TRUE;
573     }
575     if (pc->expecting_clicks_for_LPE > 0) {
576         --pc->expecting_clicks_for_LPE;
577     }
579     return ret;
582 /**
583  * Handle motion_notify event.
584  */
585 static gint
586 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
588     gint ret = FALSE;
590     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
591     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
593     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
594         // allow scrolling
595         return FALSE;
596     }
598     if (pc->events_disabled) {
599         // skip motion events if pen events are disabled
600         return FALSE;
601     }
603     Geom::Point const event_w(mevent.x,
604                             mevent.y);
605     if (pen_within_tolerance) {
606         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
607         gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
608         if ( Geom::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
609             return FALSE;   // Do not drag if we're within tolerance from origin.
610         }
611     }
612     // Once the user has moved farther than tolerance from the original location
613     // (indicating they intend to move the object, not click), then always process the
614     // motion notify coordinates as given (no snapping back to origin)
615     pen_within_tolerance = false;
617     /* Find desktop coordinates */
618     Geom::Point p = dt->w2d(event_w);
620     /* Test, whether we hit any anchor */
621     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
623     switch (pc->mode) {
624         case SP_PEN_CONTEXT_MODE_CLICK:
625             switch (pc->state) {
626                 case SP_PEN_CONTEXT_POINT:
627                     if ( pc->npoints != 0 ) {
628                         /* Only set point, if we are already appending */
629                         spdc_endpoint_snap(pc, p, mevent.state);
630                         spdc_pen_set_subsequent_point(pc, p, true);
631                         ret = TRUE;
632                     } else if (!sp_event_context_knot_mouseover(pc)) {
633                         SnapManager &m = dt->namedview->snap_manager;
634                         m.setup(dt);
635                         m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
636                     }
637                     break;
638                 case SP_PEN_CONTEXT_CONTROL:
639                 case SP_PEN_CONTEXT_CLOSE:
640                     /* Placing controls is last operation in CLOSE state */
641                     spdc_endpoint_snap(pc, p, mevent.state);
642                     spdc_pen_set_ctrl(pc, p, mevent.state);
643                     ret = TRUE;
644                     break;
645                 case SP_PEN_CONTEXT_STOP:
646                     /* This is perfectly valid */
647                     break;
648                 default:
649                     break;
650             }
651             break;
652         case SP_PEN_CONTEXT_MODE_DRAG:
653             switch (pc->state) {
654                 case SP_PEN_CONTEXT_POINT:
655                     if ( pc->npoints > 0 ) {
656                         /* Only set point, if we are already appending */
658                         if (!anchor) {   /* Snap node only if not hitting anchor */
659                             spdc_endpoint_snap(pc, p, mevent.state);
660                             spdc_pen_set_subsequent_point(pc, p, true, mevent.state);
661                         } else {
662                             spdc_pen_set_subsequent_point(pc, anchor->dp, false, mevent.state);
663                         }
665                         if (anchor && !pc->anchor_statusbar) {
666                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
667                             pc->anchor_statusbar = true;
668                         } else if (!anchor && pc->anchor_statusbar) {
669                             pc->_message_context->clear();
670                             pc->anchor_statusbar = false;
671                         }
673                         ret = TRUE;
674                     } else {
675                         if (anchor && !pc->anchor_statusbar) {
676                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
677                             pc->anchor_statusbar = true;
678                         } else if (!anchor && pc->anchor_statusbar) {
679                             pc->_message_context->clear();
680                             pc->anchor_statusbar = false;
681                         }
682                         if (!sp_event_context_knot_mouseover(pc)) {
683                             SnapManager &m = dt->namedview->snap_manager;
684                             m.setup(dt);
685                             m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
686                         }
687                     }
688                     break;
689                 case SP_PEN_CONTEXT_CONTROL:
690                 case SP_PEN_CONTEXT_CLOSE:
691                     /* Placing controls is last operation in CLOSE state */
693                     // snap the handle
694                     spdc_endpoint_snap_handle(pc, p, mevent.state);
696                     if (!pc->polylines_only) {
697                         spdc_pen_set_ctrl(pc, p, mevent.state);
698                     } else {
699                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
700                     }
701                     gobble_motion_events(GDK_BUTTON1_MASK);
702                     ret = TRUE;
703                     break;
704                 case SP_PEN_CONTEXT_STOP:
705                     /* This is perfectly valid */
706                     break;
707                 default:
708                     if (!sp_event_context_knot_mouseover(pc)) {
709                         SnapManager &m = dt->namedview->snap_manager;
710                         m.setup(dt);
711                         m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
712                     }
713                     break;
714             }
715             break;
716         default:
717             break;
718     }
719     return ret;
722 /**
723  * Handle mouse button release event.
724  */
725 static gint
726 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
728     if (pc->events_disabled) {
729         // skip event processing if events are disabled
730         return FALSE;
731     }
733     gint ret = FALSE;
734     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
735     if ( revent.button == 1  && !event_context->space_panning) {
737         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
739         Geom::Point const event_w(revent.x,
740                                 revent.y);
741         /* Find desktop coordinates */
742         Geom::Point p = pc->desktop->w2d(event_w);
744         /* Test whether we hit any anchor. */
745         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
747         switch (pc->mode) {
748             case SP_PEN_CONTEXT_MODE_CLICK:
749                 switch (pc->state) {
750                     case SP_PEN_CONTEXT_POINT:
751                         if ( pc->npoints == 0 ) {
752                             /* Start new thread only with button release */
753                             if (anchor) {
754                                 p = anchor->dp;
755                             }
756                             pc->sa = anchor;
757                             spdc_pen_set_initial_point(pc, p);
758                         } else {
759                             /* Set end anchor here */
760                             pc->ea = anchor;
761                             if (anchor) {
762                                 p = anchor->dp;
763                             }
764                         }
765                         pc->state = SP_PEN_CONTEXT_CONTROL;
766                         ret = TRUE;
767                         break;
768                     case SP_PEN_CONTEXT_CONTROL:
769                         /* End current segment */
770                         spdc_endpoint_snap(pc, p, revent.state);
771                         spdc_pen_finish_segment(pc, p, revent.state);
772                         pc->state = SP_PEN_CONTEXT_POINT;
773                         ret = TRUE;
774                         break;
775                     case SP_PEN_CONTEXT_CLOSE:
776                         /* End current segment */
777                         if (!anchor) {   /* Snap node only if not hitting anchor */
778                             spdc_endpoint_snap(pc, p, revent.state);
779                         }
780                         spdc_pen_finish_segment(pc, p, revent.state);
781                         spdc_pen_finish(pc, TRUE);
782                         pc->state = SP_PEN_CONTEXT_POINT;
783                         ret = TRUE;
784                         break;
785                     case SP_PEN_CONTEXT_STOP:
786                         /* This is allowed, if we just canceled curve */
787                         pc->state = SP_PEN_CONTEXT_POINT;
788                         ret = TRUE;
789                         break;
790                     default:
791                         break;
792                 }
793                 break;
794             case SP_PEN_CONTEXT_MODE_DRAG:
795                 switch (pc->state) {
796                     case SP_PEN_CONTEXT_POINT:
797                     case SP_PEN_CONTEXT_CONTROL:
798                         spdc_endpoint_snap(pc, p, revent.state);
799                         spdc_pen_finish_segment(pc, p, revent.state);
800                         break;
801                     case SP_PEN_CONTEXT_CLOSE:
802                         spdc_endpoint_snap(pc, p, revent.state);
803                         spdc_pen_finish_segment(pc, p, revent.state);
804                         if (pc->green_closed) {
805                             // finishing at the start anchor, close curve
806                             spdc_pen_finish(pc, TRUE);
807                         } else {
808                             // finishing at some other anchor, finish curve but not close
809                             spdc_pen_finish(pc, FALSE);
810                         }
811                         break;
812                     case SP_PEN_CONTEXT_STOP:
813                         /* This is allowed, if we just cancelled curve */
814                         break;
815                     default:
816                         break;
817                 }
818                 pc->state = SP_PEN_CONTEXT_POINT;
819                 ret = TRUE;
820                 break;
821             default:
822                 break;
823         }
825         if (pc->grab) {
826             /* Release grab now */
827             sp_canvas_item_ungrab(pc->grab, revent.time);
828             pc->grab = NULL;
829         }
831         ret = TRUE;
833         dc->green_closed = FALSE;
834     }
836     // TODO: can we be sure that the path was created correctly?
837     // TODO: should we offer an option to collect the clicks in a list?
838     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
839         sp_pen_context_set_polyline_mode(pc);
841         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
842         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
844         if (pc->waiting_LPE) {
845             // we have an already created LPE waiting for a path
846             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
847             selection->add(SP_OBJECT(pc->waiting_item));
848             pc->waiting_LPE = NULL;
849         } else {
850             // the case that we need to create a new LPE and apply it to the just-drawn path is
851             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
852         }
853     }
855     return ret;
858 static gint
859 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
861     gint ret = FALSE;
862     // only end on LMB double click. Otherwise horizontal scrolling causes ending of the path
863     if (pc->npoints != 0 && bevent.button == 1) {
864         spdc_pen_finish(pc, FALSE);
865         ret = TRUE;
866     }
867     return ret;
870 void
871 pen_redraw_all (SPPenContext *const pc)
873     // green
874     if (pc->green_bpaths) {
875         // remove old piecewise green canvasitems
876         while (pc->green_bpaths) {
877             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
878             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
879         }
880         // one canvas bpath for all of green_curve
881         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
882         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
883         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
885         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
886     }
888     if (pc->green_anchor)
889         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
891     pc->red_curve->reset();
892     pc->red_curve->moveto(pc->p[0]);
893     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
894     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
896     // handles
897     if (pc->p[0] != pc->p[1]) {
898         SP_CTRL(pc->c1)->moveto(pc->p[1]);
899         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
900         sp_canvas_item_show (pc->c1);
901         sp_canvas_item_show (pc->cl1);
902     } else {
903         sp_canvas_item_hide (pc->c1);
904         sp_canvas_item_hide (pc->cl1);
905     }
907     Geom::Curve const * last_seg = pc->green_curve->last_segment();
908     if (last_seg) {
909         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
910         if ( cubic &&
911              (*cubic)[2] != to_2geom(pc->p[0]) )
912         {
913             Geom::Point p2 = (*cubic)[2];
914             SP_CTRL(pc->c0)->moveto(p2);
915             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
916             sp_canvas_item_show (pc->c0);
917             sp_canvas_item_show (pc->cl0);
918         } else {
919             sp_canvas_item_hide (pc->c0);
920             sp_canvas_item_hide (pc->cl0);
921         }
922     }
925 void
926 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
928     if (pc->npoints != 5)
929         return;
931     // green
932     if (!pc->green_curve->is_empty()) {
933         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
934     } else {
935         // start anchor too
936         if (pc->green_anchor) {
937             pc->green_anchor->dp += Geom::Point(x, y);
938         }
939     }
941     // red
942     pc->p[0] += Geom::Point(x, y);
943     pc->p[1] += Geom::Point(x, y);
944     pen_redraw_all(pc);
947 void
948 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
950     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
953 void
954 pen_lastpoint_tocurve (SPPenContext *const pc)
956     if (pc->npoints != 5)
957         return;
959     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
960     if ( cubic ) {
961         pc->p[1] = pc->p[0] + (Geom::Point)( (*cubic)[3] - (*cubic)[2] );
962     } else {
963         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
964     }
966     pen_redraw_all(pc);
969 void
970 pen_lastpoint_toline (SPPenContext *const pc)
972     if (pc->npoints != 5)
973         return;
975     pc->p[1] = pc->p[0];
977     pen_redraw_all(pc);
981 static gint
982 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
985     gint ret = FALSE;
986     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
987     gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
989     switch (get_group0_keyval (&event->key)) {
991         case GDK_Left: // move last point left
992         case GDK_KP_Left:
993         case GDK_KP_4:
994             if (!MOD__CTRL) { // not ctrl
995                 if (MOD__ALT) { // alt
996                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
997                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
998                 }
999                 else { // no alt
1000                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
1001                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
1002                 }
1003                 ret = TRUE;
1004             }
1005             break;
1006         case GDK_Up: // move last point up
1007         case GDK_KP_Up:
1008         case GDK_KP_8:
1009             if (!MOD__CTRL) { // not ctrl
1010                 if (MOD__ALT) { // alt
1011                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
1012                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
1013                 }
1014                 else { // no alt
1015                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
1016                     else pen_lastpoint_move(pc, 0, nudge); // no shift
1017                 }
1018                 ret = TRUE;
1019             }
1020             break;
1021         case GDK_Right: // move last point right
1022         case GDK_KP_Right:
1023         case GDK_KP_6:
1024             if (!MOD__CTRL) { // not ctrl
1025                 if (MOD__ALT) { // alt
1026                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1027                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1028                 }
1029                 else { // no alt
1030                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1031                     else pen_lastpoint_move(pc, nudge, 0); // no shift
1032                 }
1033                 ret = TRUE;
1034             }
1035             break;
1036         case GDK_Down: // move last point down
1037         case GDK_KP_Down:
1038         case GDK_KP_2:
1039             if (!MOD__CTRL) { // not ctrl
1040                 if (MOD__ALT) { // alt
1041                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1042                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1043                 }
1044                 else { // no alt
1045                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1046                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
1047                 }
1048                 ret = TRUE;
1049             }
1050             break;
1052 /* TODO: this is not yet enabled?? looks like some traces of the Geometry tool
1053         case GDK_P:
1054         case GDK_p:
1055             if (MOD__SHIFT_ONLY) {
1056                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1057                 ret = TRUE;
1058             }
1059             break;
1061         case GDK_C:
1062         case GDK_c:
1063             if (MOD__SHIFT_ONLY) {
1064                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1065                 ret = TRUE;
1066             }
1067             break;
1069         case GDK_B:
1070         case GDK_b:
1071             if (MOD__SHIFT_ONLY) {
1072                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1073                 ret = TRUE;
1074             }
1075             break;
1077         case GDK_A:
1078         case GDK_a:
1079             if (MOD__SHIFT_ONLY) {
1080                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1081                 ret = TRUE;
1082             }
1083             break;
1084 */
1086         case GDK_U:
1087         case GDK_u:
1088             if (MOD__SHIFT_ONLY) {
1089                 pen_lastpoint_tocurve(pc);
1090                 ret = TRUE;
1091             }
1092             break;
1093         case GDK_L:
1094         case GDK_l:
1095             if (MOD__SHIFT_ONLY) {
1096                 pen_lastpoint_toline(pc);
1097                 ret = TRUE;
1098             }
1099             break;
1101         case GDK_Return:
1102         case GDK_KP_Enter:
1103             if (pc->npoints != 0) {
1104                 spdc_pen_finish(pc, FALSE);
1105                 ret = TRUE;
1106             }
1107             break;
1108         case GDK_Escape:
1109             if (pc->npoints != 0) {
1110                 // if drawing, cancel, otherwise pass it up for deselecting
1111                 pen_cancel (pc);
1112                 ret = TRUE;
1113             }
1114             break;
1115         case GDK_z:
1116         case GDK_Z:
1117             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1118                 // if drawing, cancel, otherwise pass it up for undo
1119                 pen_cancel (pc);
1120                 ret = TRUE;
1121             }
1122             break;
1123         case GDK_g:
1124         case GDK_G:
1125             if (MOD__SHIFT_ONLY) {
1126                 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
1127                 ret = true;
1128             }
1129             break;
1130         case GDK_BackSpace:
1131         case GDK_Delete:
1132         case GDK_KP_Delete:
1133             if ( pc->green_curve->is_empty() || (pc->green_curve->last_segment() == NULL) ) {
1134                 if (!pc->red_curve->is_empty()) {
1135                     pen_cancel (pc);
1136                     ret = TRUE;
1137                 } else {
1138                     // do nothing; this event should be handled upstream
1139                 }
1140             } else {
1141                 /* Reset red curve */
1142                 pc->red_curve->reset();
1143                 /* Destroy topmost green bpath */
1144                 if (pc->green_bpaths) {
1145                     if (pc->green_bpaths->data)
1146                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1147                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1148                 }
1149                 /* Get last segment */
1150                 if ( pc->green_curve->is_empty() ) {
1151                     g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1152                     break;
1153                 }
1154                 // The code below assumes that pc->green_curve has only ONE path !
1155                 Geom::Curve const * crv = pc->green_curve->last_segment();
1156                 pc->p[0] = crv->initialPoint();
1157                 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1158                     pc->p[1] = (*cubic)[1];
1159                 } else {
1160                     pc->p[1] = pc->p[0];
1161                 }
1162                 Geom::Point const pt(( pc->npoints < 4
1163                                      ? (Geom::Point)(crv->finalPoint())
1164                                      : pc->p[3] ));
1165                 pc->npoints = 2;
1166                 pc->green_curve->backspace();
1167                 sp_canvas_item_hide(pc->c0);
1168                 sp_canvas_item_hide(pc->c1);
1169                 sp_canvas_item_hide(pc->cl0);
1170                 sp_canvas_item_hide(pc->cl1);
1171                 pc->state = SP_PEN_CONTEXT_POINT;
1172                 spdc_pen_set_subsequent_point(pc, pt, true);
1173                 pen_last_paraxial_dir = !pen_last_paraxial_dir;
1174                 ret = TRUE;
1175             }
1176             break;
1177         default:
1178             break;
1179     }
1180     return ret;
1183 static void
1184 spdc_reset_colors(SPPenContext *pc)
1186     /* Red */
1187     pc->red_curve->reset();
1188     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1189     /* Blue */
1190     pc->blue_curve->reset();
1191     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1192     /* Green */
1193     while (pc->green_bpaths) {
1194         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1195         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1196     }
1197     pc->green_curve->reset();
1198     if (pc->green_anchor) {
1199         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1200     }
1201     pc->sa = NULL;
1202     pc->ea = NULL;
1203     pc->npoints = 0;
1204     pc->red_curve_is_valid = false;
1208 static void
1209 spdc_pen_set_initial_point(SPPenContext *const pc, Geom::Point const p)
1211     g_assert( pc->npoints == 0 );
1213     pc->p[0] = p;
1214     pc->p[1] = p;
1215     pc->npoints = 2;
1216     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1218     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1221 /**
1222  * Show the status message for the current line/curve segment.
1223  * This type of message always shows angle/distance as the last
1224  * two parameters ("angle %3.2f&#176;, distance %s").
1225  */
1226 static void
1227 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, Geom::Point const p, int pc_point_to_compare, gchar const *message)
1229     g_assert(pc != NULL);
1230     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1231     g_assert(message != NULL);
1233     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1234     Geom::Point rel = p - pc->p[pc_point_to_compare];
1235     GString *dist = SP_PX_TO_METRIC_STRING(Geom::L2(rel), desktop->namedview->getDefaultMetric());
1236     double angle = atan2(rel[Geom::Y], rel[Geom::X]) * 180 / M_PI;
1237     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1238     if (prefs->getBool("/options/compassangledisplay/value", 0) != 0)
1239         angle = angle_to_compass (angle);
1241     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1242     g_string_free(dist, FALSE);
1245 static void
1246 spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status)
1248     g_assert( pc->npoints != 0 );
1249     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1251     pc->p[2] = p;
1252     pc->p[3] = p;
1253     pc->p[4] = p;
1254     pc->npoints = 5;
1255     pc->red_curve->reset();
1256     bool is_curve;
1257     pc->red_curve->moveto(pc->p[0]);
1258     if (pc->polylines_paraxial && !statusbar) {
1259         // we are drawing horizontal/vertical lines and hit an anchor;
1260         Geom::Point const origin = pc->p[0];
1261         // if the previous point and the anchor are not aligned either horizontally or vertically...
1262         if ((abs(p[Geom::X] - origin[Geom::X]) > 1e-9) && (abs(p[Geom::Y] - origin[Geom::Y]) > 1e-9)) {
1263             // ...then we should draw an L-shaped path, consisting of two paraxial segments
1264             Geom::Point intermed = p;
1265             pen_set_to_nearest_horiz_vert(pc, intermed, status, false);
1266             pc->red_curve->lineto(intermed);
1267         }
1268         pc->red_curve->lineto(p);
1269         is_curve = false;
1270     } else {
1271         // one of the 'regular' modes
1272         if (pc->p[1] != pc->p[0]) {
1273             pc->red_curve->curveto(pc->p[1], p, p);
1274             is_curve = true;
1275         } else {
1276             pc->red_curve->lineto(p);
1277             is_curve = false;
1278         }
1279     }
1281     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1283     if (statusbar) {
1284         gchar *message = is_curve ?
1285             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1286             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1287         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1288     }
1291 static void
1292 spdc_pen_set_ctrl(SPPenContext *const pc, Geom::Point const p, guint const state)
1294     sp_canvas_item_show(pc->c1);
1295     sp_canvas_item_show(pc->cl1);
1297     if ( pc->npoints == 2 ) {
1298         pc->p[1] = p;
1299         sp_canvas_item_hide(pc->c0);
1300         sp_canvas_item_hide(pc->cl0);
1301         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1302         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1304         spdc_pen_set_angle_distance_status_message(pc, p, 0, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"));
1305     } else if ( pc->npoints == 5 ) {
1306         pc->p[4] = p;
1307         sp_canvas_item_show(pc->c0);
1308         sp_canvas_item_show(pc->cl0);
1309         bool is_symm = false;
1310         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1311              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1312             Geom::Point delta = p - pc->p[3];
1313             pc->p[2] = pc->p[3] - delta;
1314             is_symm = true;
1315             pc->red_curve->reset();
1316             pc->red_curve->moveto(pc->p[0]);
1317             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1318             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1319         }
1320         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1321         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1322         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1323         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1325         gchar *message = is_symm ?
1326             _("<b>Curve handle, symmetric</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only") :
1327             _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only");
1328         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1329     } else {
1330         g_warning("Something bad happened - npoints is %d", pc->npoints);
1331     }
1334 static void
1335 spdc_pen_finish_segment(SPPenContext *const pc, Geom::Point const p, guint const state)
1337     if (pc->polylines_paraxial) {
1338         pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1339     }
1341     ++pc->num_clicks;
1343     if (!pc->red_curve->is_empty()) {
1344         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1345         SPCurve *curve = pc->red_curve->copy();
1346         /// \todo fixme:
1347         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1348         curve->unref();
1349         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1351         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1353         pc->p[0] = pc->p[3];
1354         pc->p[1] = pc->p[4];
1355         pc->npoints = 2;
1357         pc->red_curve->reset();
1358     }
1361 static void
1362 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1364     if (pc->expecting_clicks_for_LPE > 1) {
1365         // don't let the path be finished before we have collected the required number of mouse clicks
1366         return;
1367     }
1369     pc->num_clicks = 0;
1371     pen_disable_events(pc);
1373     SPDesktop *const desktop = pc->desktop;
1374     pc->_message_context->clear();
1375     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1377     pc->red_curve->reset();
1378     spdc_concat_colors_and_flush(pc, closed);
1379     pc->sa = NULL;
1380     pc->ea = NULL;
1382     pc->npoints = 0;
1383     pc->state = SP_PEN_CONTEXT_POINT;
1385     sp_canvas_item_hide(pc->c0);
1386     sp_canvas_item_hide(pc->c1);
1387     sp_canvas_item_hide(pc->cl0);
1388     sp_canvas_item_hide(pc->cl1);
1390     if (pc->green_anchor) {
1391         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1392     }
1395     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1397     pen_enable_events(pc);
1400 static void
1401 pen_disable_events(SPPenContext *const pc) {
1402   pc->events_disabled++;
1405 static void
1406 pen_enable_events(SPPenContext *const pc) {
1407   g_return_if_fail(pc->events_disabled != 0);
1409   pc->events_disabled--;
1412 void
1413 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1414                                          unsigned int num_clicks, bool use_polylines)
1416     if (effect_type == Inkscape::LivePathEffect::INVALID_LPE)
1417         return;
1419     pc->waiting_LPE_type = effect_type;
1420     pc->expecting_clicks_for_LPE = num_clicks;
1421     pc->polylines_only = use_polylines;
1422     pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
1425 void
1426 sp_pen_context_cancel_waiting_for_LPE(SPPenContext *pc)
1428     pc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
1429     pc->expecting_clicks_for_LPE = 0;
1430     sp_pen_context_set_polyline_mode(pc);
1433 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1434                                        Geom::Point const &pt, Geom::Point const &origin, guint state) {
1435     /*
1436      * after the first mouse click we determine whether the mouse pointer is closest to a
1437      * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1438      * orthogonal to the last one; pressing Shift toggles the direction
1439      */
1440     // num_clicks is not reliable because spdc_pen_finish_segment is sometimes called too early
1441     // (on first mouse release), in which case num_clicks immediately becomes 1.
1442     // if (pc->num_clicks == 0) {
1444     if (pc->green_curve->is_empty()) {
1445         // first mouse click
1446         double dist_h = fabs(pt[Geom::X] - origin[Geom::X]);
1447         double dist_v = fabs(pt[Geom::Y] - origin[Geom::Y]);
1448         int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1449         pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1450         return pen_last_paraxial_dir;
1451     } else {
1452         // subsequent mouse click
1453         return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1454     }
1457 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap)
1459     Geom::Point const origin = pc->p[0];
1461     int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1463     if (!snap) {
1464         if (next_dir == 0) {
1465             // line is forced to be horizontal
1466             pt[Geom::Y] = origin[Geom::Y];
1467         } else {
1468             // line is forced to be vertical
1469             pt[Geom::X] = origin[Geom::X];
1470         }
1471     } else {
1472         // Create a horizontal or vertical constraint line
1473         Inkscape::Snapper::ConstraintLine cl(origin, next_dir ? Geom::Point(0, 1) : Geom::Point(1, 0));
1475         // Snap along the constraint line; if we didn't snap then still the constraint will be applied
1476         SnapManager &m = pc->desktop->namedview->snap_manager;
1478         Inkscape::Selection *selection = sp_desktop_selection (pc->desktop);
1479         // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
1480         // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
1482         m.setup(pc->desktop, true, selection->singleItem());
1483         m.constrainedSnapReturnByRef(pt, Inkscape::SNAPSOURCE_NODE_HANDLE, cl);
1484     }
1487 /*
1488   Local Variables:
1489   mode:c++
1490   c-file-style:"stroustrup"
1491   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1492   indent-tabs-mode:nil
1493   fill-column:99
1494   End:
1495 */
1496 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :