Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[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/sp-canvas.h"
35 #include "display/curve.h"
36 #include "pixmaps/cursor-pen.xpm"
37 #include "display/canvas-bpath.h"
38 #include "display/sp-ctrlline.h"
39 #include "display/sodipodi-ctrl.h"
40 #include <glibmm/i18n.h>
41 #include "libnr/nr-point-ops.h"
42 #include "helper/units.h"
43 #include "macros.h"
44 #include "context-fns.h"
45 #include "tools-switch.h"
47 static void sp_pen_context_class_init(SPPenContextClass *klass);
48 static void sp_pen_context_init(SPPenContext *pc);
49 static void sp_pen_context_dispose(GObject *object);
51 static void sp_pen_context_setup(SPEventContext *ec);
52 static void sp_pen_context_finish(SPEventContext *ec);
53 static void sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
54 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
55 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
57 static void spdc_pen_set_initial_point(SPPenContext *pc, Geom::Point const p);
58 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status = 0);
59 static void spdc_pen_set_ctrl(SPPenContext *pc, Geom::Point const p, guint state);
60 static void spdc_pen_finish_segment(SPPenContext *pc, Geom::Point p, guint state);
62 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
64 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
65 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
66 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
67 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
68 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
69 static void spdc_reset_colors(SPPenContext *pc);
71 static void pen_disable_events(SPPenContext *const pc);
72 static void pen_enable_events(SPPenContext *const pc);
74 static Geom::Point pen_drag_origin_w(0, 0);
75 static bool pen_within_tolerance = false;
77 static SPDrawContextClass *pen_parent_class;
79 static int pen_next_paraxial_direction(const SPPenContext *const pc, Geom::Point const &pt, Geom::Point const &origin, guint state);
80 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap);
82 static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
84 /**
85  * Register SPPenContext with Gdk and return its type.
86  */
87 GType
88 sp_pen_context_get_type(void)
89 {
90     static GType type = 0;
91     if (!type) {
92         GTypeInfo info = {
93             sizeof(SPPenContextClass),
94             NULL, NULL,
95             (GClassInitFunc) sp_pen_context_class_init,
96             NULL, NULL,
97             sizeof(SPPenContext),
98             4,
99             (GInstanceInitFunc) sp_pen_context_init,
100             NULL,   /* value_table */
101         };
102         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
103     }
104     return type;
107 /**
108  * Initialize the SPPenContext vtable.
109  */
110 static void
111 sp_pen_context_class_init(SPPenContextClass *klass)
113     GObjectClass *object_class;
114     SPEventContextClass *event_context_class;
116     object_class = (GObjectClass *) klass;
117     event_context_class = (SPEventContextClass *) klass;
119     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
121     object_class->dispose = sp_pen_context_dispose;
123     event_context_class->setup = sp_pen_context_setup;
124     event_context_class->finish = sp_pen_context_finish;
125     event_context_class->set = sp_pen_context_set;
126     event_context_class->root_handler = sp_pen_context_root_handler;
127     event_context_class->item_handler = sp_pen_context_item_handler;
130 /**
131  * Callback to initialize SPPenContext object.
132  */
133 static void
134 sp_pen_context_init(SPPenContext *pc)
137     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
139     event_context->cursor_shape = cursor_pen_xpm;
140     event_context->hot_x = 4;
141     event_context->hot_y = 4;
143     pc->npoints = 0;
144     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
145     pc->state = SP_PEN_CONTEXT_POINT;
147     pc->c0 = NULL;
148     pc->c1 = NULL;
149     pc->cl0 = NULL;
150     pc->cl1 = NULL;
152     pc->events_disabled = 0;
154     pc->num_clicks = 0;
155     pc->waiting_LPE = NULL;
156     pc->waiting_item = NULL;
159 /**
160  * Callback to destroy the SPPenContext object's members and itself.
161  */
162 static void
163 sp_pen_context_dispose(GObject *object)
165     SPPenContext *pc;
167     pc = SP_PEN_CONTEXT(object);
169     if (pc->c0) {
170         gtk_object_destroy(GTK_OBJECT(pc->c0));
171         pc->c0 = NULL;
172     }
173     if (pc->c1) {
174         gtk_object_destroy(GTK_OBJECT(pc->c1));
175         pc->c1 = NULL;
176     }
177     if (pc->cl0) {
178         gtk_object_destroy(GTK_OBJECT(pc->cl0));
179         pc->cl0 = NULL;
180     }
181     if (pc->cl1) {
182         gtk_object_destroy(GTK_OBJECT(pc->cl1));
183         pc->cl1 = NULL;
184     }
186     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
188     if (pc->expecting_clicks_for_LPE > 0) {
189         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
190         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
191     }
194 void
195 sp_pen_context_set_polyline_mode(SPPenContext *const pc) {
196     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
197     guint mode = prefs->getInt("/tools/freehand/pen/freehand-mode", 0);
198     pc->polylines_only = (mode == 2 || mode == 3);
199     pc->polylines_paraxial = (mode == 3);
202 /**
203  * Callback to initialize SPPenContext object.
204  */
205 static void
206 sp_pen_context_setup(SPEventContext *ec)
208     SPPenContext *pc;
210     pc = SP_PEN_CONTEXT(ec);
212     if (((SPEventContextClass *) pen_parent_class)->setup) {
213         ((SPEventContextClass *) pen_parent_class)->setup(ec);
214     }
216     /* Pen indicators */
217     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
218                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
219     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
220                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
221     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
222     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
223     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
224     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
226     sp_canvas_item_hide(pc->c0);
227     sp_canvas_item_hide(pc->c1);
228     sp_canvas_item_hide(pc->cl0);
229     sp_canvas_item_hide(pc->cl1);
231     sp_event_context_read(ec, "mode");
233     pc->anchor_statusbar = false;
235     sp_pen_context_set_polyline_mode(pc);
237     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
238     if (prefs->getBool("/tools/freehand/pen/selcue")) {
239         ec->enableSelectionCue();
240     }
243 static void
244 pen_cancel (SPPenContext *const pc)
246     pc->num_clicks = 0;
247     pc->state = SP_PEN_CONTEXT_STOP;
248     spdc_reset_colors(pc);
249     sp_canvas_item_hide(pc->c0);
250     sp_canvas_item_hide(pc->c1);
251     sp_canvas_item_hide(pc->cl0);
252     sp_canvas_item_hide(pc->cl1);
253     pc->_message_context->clear();
254     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
256     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
259 /**
260  * Finalization callback.
261  */
262 static void
263 sp_pen_context_finish(SPEventContext *ec)
265     SPPenContext *pc = SP_PEN_CONTEXT(ec);
267     sp_event_context_discard_delayed_snap_event(ec);
269     if (pc->npoints != 0) {
270         pen_cancel (pc);
271     }
273     if (((SPEventContextClass *) pen_parent_class)->finish) {
274         ((SPEventContextClass *) pen_parent_class)->finish(ec);
275     }
278 /**
279  * Callback that sets key to value in pen context.
280  */
281 static void
282 sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
284     SPPenContext *pc = SP_PEN_CONTEXT(ec);
285     Glib::ustring name = val->getEntryName();
287     if (name == "mode") {
288         if ( val->getString() == "drag" ) {
289             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
290         } else {
291             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
292         }
293     }
296 /**
297  * Snaps new node relative to the previous node.
298  */
299 static void
300 spdc_endpoint_snap(SPPenContext const *const pc, Geom::Point &p, guint const state)
302     if ((state & GDK_CONTROL_MASK) && !pc->polylines_paraxial) { //CTRL enables angular snapping
303         if (pc->npoints > 0) {
304             spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
305         }
306     } else {
307         // We cannot use shift here to disable snapping because the shift-key is already used
308         // to toggle the paraxial direction; if the user wants to disable snapping (s)he will
309         // have to use the %-key, the menu, or the snap toolbar
310         if ((pc->npoints > 0) && pc->polylines_paraxial) {
311             // snap constrained
312             pen_set_to_nearest_horiz_vert(pc, p, state, true);
313         } else {
314             // snap freely
315             spdc_endpoint_snap_free(pc, p, state);
316         }
317     }
320 /**
321  * Snaps new node's handle relative to the new node.
322  */
323 static void
324 spdc_endpoint_snap_handle(SPPenContext const *const pc, Geom::Point &p, guint const state)
326     g_return_if_fail(( pc->npoints == 2 ||
327                        pc->npoints == 5   ));
329     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
330         spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
331     } else {
332         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
333             spdc_endpoint_snap_free(pc, p, state);
334         }
335     }
338 static gint
339 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
341     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
343     gint ret = FALSE;
345     switch (event->type) {
346         case GDK_BUTTON_PRESS:
347             ret = pen_handle_button_press(pc, event->button);
348             break;
349         case GDK_BUTTON_RELEASE:
350             ret = pen_handle_button_release(pc, event->button);
351             break;
352         default:
353             break;
354     }
356     if (!ret) {
357         if (((SPEventContextClass *) pen_parent_class)->item_handler)
358             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
359     }
361     return ret;
364 /**
365  * Callback to handle all pen events.
366  */
367 static gint
368 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
370     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
372     gint ret = FALSE;
374     switch (event->type) {
375         case GDK_BUTTON_PRESS:
376             ret = pen_handle_button_press(pc, event->button);
377             break;
379         case GDK_MOTION_NOTIFY:
380             ret = pen_handle_motion_notify(pc, event->motion);
381             break;
383         case GDK_BUTTON_RELEASE:
384             ret = pen_handle_button_release(pc, event->button);
385             break;
387         case GDK_2BUTTON_PRESS:
388             ret = pen_handle_2button_press(pc, event->button);
389             break;
391         case GDK_KEY_PRESS:
392             ret = pen_handle_key_press(pc, event);
393             break;
395         default:
396             break;
397     }
399     if (!ret) {
400         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
401             = ((SPEventContextClass *) pen_parent_class)->root_handler;
402         if (parent_root_handler) {
403             ret = parent_root_handler(ec, event);
404         }
405     }
407     return ret;
410 /**
411  * Handle mouse button press event.
412  */
413 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
415     if (pc->events_disabled) {
416         // skip event processing if events are disabled
417         return FALSE;
418     }
420     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
421     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
422     Geom::Point const event_w(bevent.x, bevent.y);
423     Geom::Point event_dt(desktop->w2d(event_w));
424     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
426     gint ret = FALSE;
427     if (bevent.button == 1 && !event_context->space_panning
428         // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
429         && pc->expecting_clicks_for_LPE != 1) {
431         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
432             return TRUE;
433         }
435         if (!pc->grab ) {
436             /* Grab mouse, so release will not pass unnoticed */
437             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
438             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
439                                             GDK_BUTTON_RELEASE_MASK |
440                                             GDK_POINTER_MOTION_MASK  ),
441                                 NULL, bevent.time);
442         }
444         pen_drag_origin_w = event_w;
445         pen_within_tolerance = true;
447         /* Test whether we hit any anchor. */
448         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
450         switch (pc->mode) {
451             case SP_PEN_CONTEXT_MODE_CLICK:
452                 /* In click mode we add point on release */
453                 switch (pc->state) {
454                     case SP_PEN_CONTEXT_POINT:
455                     case SP_PEN_CONTEXT_CONTROL:
456                     case SP_PEN_CONTEXT_CLOSE:
457                         break;
458                     case SP_PEN_CONTEXT_STOP:
459                         /* This is allowed, if we just canceled curve */
460                         pc->state = SP_PEN_CONTEXT_POINT;
461                         break;
462                     default:
463                         break;
464                 }
465                 break;
466             case SP_PEN_CONTEXT_MODE_DRAG:
467                 switch (pc->state) {
468                     case SP_PEN_CONTEXT_STOP:
469                         /* This is allowed, if we just canceled curve */
470                     case SP_PEN_CONTEXT_POINT:
471                         if (pc->npoints == 0) {
473                             Geom::Point p;
474                             if ((bevent.state & GDK_CONTROL_MASK) && (pc->polylines_only || pc->polylines_paraxial)) {
475                                 p = event_dt;
476                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
477                                     SnapManager &m = desktop->namedview->snap_manager;
478                                     m.setup(desktop);
479                                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
480                                     m.unSetup();
481                                 }
482                               spdc_create_single_dot(event_context, p, "/tools/freehand/pen", bevent.state);
483                               ret = TRUE;
484                               break;
485                             }
487                             // TODO: Perhaps it would be nicer to rearrange the following case
488                             // distinction so that the case of a waiting LPE is treated separately
490                             /* Set start anchor */
491                             pc->sa = anchor;
492                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
493                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
494                                    a fresh path to be created so don't continue an existing one */
495                                 p = anchor->dp;
496                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
497                             } else {
498                                 // This is the first click of a new curve; deselect item so that
499                                 // this curve is not combined with it (unless it is drawn from its
500                                 // anchor, which is handled by the sibling branch above)
501                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
502                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
503                                     /* if we have a waiting LPE, we need a fresh path to be created
504                                        so don't append to an existing one */
505                                     selection->clear();
506                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
507                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
508                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
509                                 }
511                                 /* Create green anchor */
512                                 p = event_dt;
513                                 spdc_endpoint_snap(pc, p, bevent.state);
514                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
515                             }
516                             spdc_pen_set_initial_point(pc, p);
517                         } else {
519                             /* Set end anchor */
520                             pc->ea = anchor;
521                             Geom::Point p;
522                             if (anchor) {
523                                 p = anchor->dp;
524                                 // we hit an anchor, will finish the curve (either with or without closing)
525                                 // in release handler
526                                 pc->state = SP_PEN_CONTEXT_CLOSE;
528                                 if (pc->green_anchor && pc->green_anchor->active) {
529                                     // we clicked on the current curve start, so close it even if
530                                     // we drag a handle away from it
531                                     dc->green_closed = TRUE;
532                                 }
533                                 ret = TRUE;
534                                 break;
536                             } else {
537                                 p = event_dt;
538                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
539                                 spdc_pen_set_subsequent_point(pc, p, true);
540                             }
541                         }
543                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
544                         ret = TRUE;
545                         break;
546                     case SP_PEN_CONTEXT_CONTROL:
547                         g_warning("Button down in CONTROL state");
548                         break;
549                     case SP_PEN_CONTEXT_CLOSE:
550                         g_warning("Button down in CLOSE state");
551                         break;
552                     default:
553                         break;
554                 }
555                 break;
556             default:
557                 break;
558         }
559     } else if (pc->expecting_clicks_for_LPE == 1 && pc->npoints != 0) {
560         // when the last click for a waiting LPE occurs we want to finish the path
561         spdc_pen_finish_segment(pc, event_dt, bevent.state);
562         if (pc->green_closed) {
563             // finishing at the start anchor, close curve
564             spdc_pen_finish(pc, TRUE);
565         } else {
566             // finishing at some other anchor, finish curve but not close
567             spdc_pen_finish(pc, FALSE);
568         }
570         ret = TRUE;
571     } else if (bevent.button == 3 && pc->npoints != 0) {
572         // right click - finish path
573         spdc_pen_finish(pc, FALSE);
574         ret = TRUE;
575     }
577     if (pc->expecting_clicks_for_LPE > 0) {
578         --pc->expecting_clicks_for_LPE;
579     }
581     return ret;
584 /**
585  * Handle motion_notify event.
586  */
587 static gint
588 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
590     gint ret = FALSE;
592     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
593     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
595     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
596         // allow scrolling
597         return FALSE;
598     }
600     if (pc->events_disabled) {
601         // skip motion events if pen events are disabled
602         return FALSE;
603     }
605     Geom::Point const event_w(mevent.x,
606                             mevent.y);
607     if (pen_within_tolerance) {
608         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
609         gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
610         if ( Geom::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
611             return FALSE;   // Do not drag if we're within tolerance from origin.
612         }
613     }
614     // Once the user has moved farther than tolerance from the original location
615     // (indicating they intend to move the object, not click), then always process the
616     // motion notify coordinates as given (no snapping back to origin)
617     pen_within_tolerance = false;
619     /* Find desktop coordinates */
620     Geom::Point p = dt->w2d(event_w);
622     /* Test, whether we hit any anchor */
623     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
625     switch (pc->mode) {
626         case SP_PEN_CONTEXT_MODE_CLICK:
627             switch (pc->state) {
628                 case SP_PEN_CONTEXT_POINT:
629                     if ( pc->npoints != 0 ) {
630                         /* Only set point, if we are already appending */
631                         spdc_endpoint_snap(pc, p, mevent.state);
632                         spdc_pen_set_subsequent_point(pc, p, true);
633                         ret = TRUE;
634                     } else if (!sp_event_context_knot_mouseover(pc)) {
635                         SnapManager &m = dt->namedview->snap_manager;
636                         m.setup(dt);
637                         m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
638                         m.unSetup();
639                     }
640                     break;
641                 case SP_PEN_CONTEXT_CONTROL:
642                 case SP_PEN_CONTEXT_CLOSE:
643                     /* Placing controls is last operation in CLOSE state */
644                     spdc_endpoint_snap(pc, p, mevent.state);
645                     spdc_pen_set_ctrl(pc, p, mevent.state);
646                     ret = TRUE;
647                     break;
648                 case SP_PEN_CONTEXT_STOP:
649                     /* This is perfectly valid */
650                     break;
651                 default:
652                     break;
653             }
654             break;
655         case SP_PEN_CONTEXT_MODE_DRAG:
656             switch (pc->state) {
657                 case SP_PEN_CONTEXT_POINT:
658                     if ( pc->npoints > 0 ) {
659                         /* Only set point, if we are already appending */
661                         if (!anchor) {   /* Snap node only if not hitting anchor */
662                             spdc_endpoint_snap(pc, p, mevent.state);
663                             spdc_pen_set_subsequent_point(pc, p, true, mevent.state);
664                         } else {
665                             spdc_pen_set_subsequent_point(pc, anchor->dp, false, mevent.state);
666                         }
668                         if (anchor && !pc->anchor_statusbar) {
669                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
670                             pc->anchor_statusbar = true;
671                         } else if (!anchor && pc->anchor_statusbar) {
672                             pc->_message_context->clear();
673                             pc->anchor_statusbar = false;
674                         }
676                         ret = TRUE;
677                     } else {
678                         if (anchor && !pc->anchor_statusbar) {
679                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
680                             pc->anchor_statusbar = true;
681                         } else if (!anchor && pc->anchor_statusbar) {
682                             pc->_message_context->clear();
683                             pc->anchor_statusbar = false;
684                         }
685                         if (!sp_event_context_knot_mouseover(pc)) {
686                             SnapManager &m = dt->namedview->snap_manager;
687                             m.setup(dt);
688                             m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
689                             m.unSetup();
690                         }
691                     }
692                     break;
693                 case SP_PEN_CONTEXT_CONTROL:
694                 case SP_PEN_CONTEXT_CLOSE:
695                     /* Placing controls is last operation in CLOSE state */
697                     // snap the handle
698                     spdc_endpoint_snap_handle(pc, p, mevent.state);
700                     if (!pc->polylines_only) {
701                         spdc_pen_set_ctrl(pc, p, mevent.state);
702                     } else {
703                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
704                     }
705                     gobble_motion_events(GDK_BUTTON1_MASK);
706                     ret = TRUE;
707                     break;
708                 case SP_PEN_CONTEXT_STOP:
709                     /* This is perfectly valid */
710                     break;
711                 default:
712                     if (!sp_event_context_knot_mouseover(pc)) {
713                         SnapManager &m = dt->namedview->snap_manager;
714                         m.setup(dt);
715                         m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
716                         m.unSetup();
717                     }
718                     break;
719             }
720             break;
721         default:
722             break;
723     }
724     return ret;
727 /**
728  * Handle mouse button release event.
729  */
730 static gint
731 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
733     if (pc->events_disabled) {
734         // skip event processing if events are disabled
735         return FALSE;
736     }
738     gint ret = FALSE;
739     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
740     if ( revent.button == 1  && !event_context->space_panning) {
742         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
744         Geom::Point const event_w(revent.x,
745                                 revent.y);
746         /* Find desktop coordinates */
747         Geom::Point p = pc->desktop->w2d(event_w);
749         /* Test whether we hit any anchor. */
750         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
752         switch (pc->mode) {
753             case SP_PEN_CONTEXT_MODE_CLICK:
754                 switch (pc->state) {
755                     case SP_PEN_CONTEXT_POINT:
756                         if ( pc->npoints == 0 ) {
757                             /* Start new thread only with button release */
758                             if (anchor) {
759                                 p = anchor->dp;
760                             }
761                             pc->sa = anchor;
762                             spdc_pen_set_initial_point(pc, p);
763                         } else {
764                             /* Set end anchor here */
765                             pc->ea = anchor;
766                             if (anchor) {
767                                 p = anchor->dp;
768                             }
769                         }
770                         pc->state = SP_PEN_CONTEXT_CONTROL;
771                         ret = TRUE;
772                         break;
773                     case SP_PEN_CONTEXT_CONTROL:
774                         /* End current segment */
775                         spdc_endpoint_snap(pc, p, revent.state);
776                         spdc_pen_finish_segment(pc, p, revent.state);
777                         pc->state = SP_PEN_CONTEXT_POINT;
778                         ret = TRUE;
779                         break;
780                     case SP_PEN_CONTEXT_CLOSE:
781                         /* End current segment */
782                         if (!anchor) {   /* Snap node only if not hitting anchor */
783                             spdc_endpoint_snap(pc, p, revent.state);
784                         }
785                         spdc_pen_finish_segment(pc, p, revent.state);
786                         spdc_pen_finish(pc, TRUE);
787                         pc->state = SP_PEN_CONTEXT_POINT;
788                         ret = TRUE;
789                         break;
790                     case SP_PEN_CONTEXT_STOP:
791                         /* This is allowed, if we just canceled curve */
792                         pc->state = SP_PEN_CONTEXT_POINT;
793                         ret = TRUE;
794                         break;
795                     default:
796                         break;
797                 }
798                 break;
799             case SP_PEN_CONTEXT_MODE_DRAG:
800                 switch (pc->state) {
801                     case SP_PEN_CONTEXT_POINT:
802                     case SP_PEN_CONTEXT_CONTROL:
803                         spdc_endpoint_snap(pc, p, revent.state);
804                         spdc_pen_finish_segment(pc, p, revent.state);
805                         break;
806                     case SP_PEN_CONTEXT_CLOSE:
807                         spdc_endpoint_snap(pc, p, revent.state);
808                         spdc_pen_finish_segment(pc, p, revent.state);
809                         if (pc->green_closed) {
810                             // finishing at the start anchor, close curve
811                             spdc_pen_finish(pc, TRUE);
812                         } else {
813                             // finishing at some other anchor, finish curve but not close
814                             spdc_pen_finish(pc, FALSE);
815                         }
816                         break;
817                     case SP_PEN_CONTEXT_STOP:
818                         /* This is allowed, if we just cancelled curve */
819                         break;
820                     default:
821                         break;
822                 }
823                 pc->state = SP_PEN_CONTEXT_POINT;
824                 ret = TRUE;
825                 break;
826             default:
827                 break;
828         }
830         if (pc->grab) {
831             /* Release grab now */
832             sp_canvas_item_ungrab(pc->grab, revent.time);
833             pc->grab = NULL;
834         }
836         ret = TRUE;
838         dc->green_closed = FALSE;
839     }
841     // TODO: can we be sure that the path was created correctly?
842     // TODO: should we offer an option to collect the clicks in a list?
843     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
844         sp_pen_context_set_polyline_mode(pc);
846         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
847         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
849         if (pc->waiting_LPE) {
850             // we have an already created LPE waiting for a path
851             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
852             selection->add(SP_OBJECT(pc->waiting_item));
853             pc->waiting_LPE = NULL;
854         } else {
855             // the case that we need to create a new LPE and apply it to the just-drawn path is
856             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
857         }
858     }
860     return ret;
863 static gint
864 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
866     gint ret = FALSE;
867     // only end on LMB double click. Otherwise horizontal scrolling causes ending of the path
868     if (pc->npoints != 0 && bevent.button == 1) {
869         spdc_pen_finish(pc, FALSE);
870         ret = TRUE;
871     }
872     return ret;
875 void
876 pen_redraw_all (SPPenContext *const pc)
878     // green
879     if (pc->green_bpaths) {
880         // remove old piecewise green canvasitems
881         while (pc->green_bpaths) {
882             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
883             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
884         }
885         // one canvas bpath for all of green_curve
886         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
887         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
888         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
890         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
891     }
893     if (pc->green_anchor)
894         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
896     pc->red_curve->reset();
897     pc->red_curve->moveto(pc->p[0]);
898     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
899     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
901     // handles
902     if (pc->p[0] != pc->p[1]) {
903         SP_CTRL(pc->c1)->moveto(pc->p[1]);
904         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
905         sp_canvas_item_show (pc->c1);
906         sp_canvas_item_show (pc->cl1);
907     } else {
908         sp_canvas_item_hide (pc->c1);
909         sp_canvas_item_hide (pc->cl1);
910     }
912     Geom::Curve const * last_seg = pc->green_curve->last_segment();
913     if (last_seg) {
914         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
915         if ( cubic &&
916              (*cubic)[2] != to_2geom(pc->p[0]) )
917         {
918             Geom::Point p2 = (*cubic)[2];
919             SP_CTRL(pc->c0)->moveto(p2);
920             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
921             sp_canvas_item_show (pc->c0);
922             sp_canvas_item_show (pc->cl0);
923         } else {
924             sp_canvas_item_hide (pc->c0);
925             sp_canvas_item_hide (pc->cl0);
926         }
927     }
930 void
931 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
933     if (pc->npoints != 5)
934         return;
936     // green
937     if (!pc->green_curve->is_empty()) {
938         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
939     } else {
940         // start anchor too
941         if (pc->green_anchor) {
942             pc->green_anchor->dp += Geom::Point(x, y);
943         }
944     }
946     // red
947     pc->p[0] += Geom::Point(x, y);
948     pc->p[1] += Geom::Point(x, y);
949     pen_redraw_all(pc);
952 void
953 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
955     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
958 void
959 pen_lastpoint_tocurve (SPPenContext *const pc)
961     if (pc->npoints != 5)
962         return;
964     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
965     if ( cubic ) {
966         pc->p[1] = pc->p[0] + (Geom::Point)( (*cubic)[3] - (*cubic)[2] );
967     } else {
968         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
969     }
971     pen_redraw_all(pc);
974 void
975 pen_lastpoint_toline (SPPenContext *const pc)
977     if (pc->npoints != 5)
978         return;
980     pc->p[1] = pc->p[0];
982     pen_redraw_all(pc);
986 static gint
987 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
990     gint ret = FALSE;
991     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
992     gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
994     switch (get_group0_keyval (&event->key)) {
996         case GDK_Left: // move last point left
997         case GDK_KP_Left:
998         case GDK_KP_4:
999             if (!MOD__CTRL) { // not ctrl
1000                 if (MOD__ALT) { // alt
1001                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
1002                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
1003                 }
1004                 else { // no alt
1005                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
1006                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
1007                 }
1008                 ret = TRUE;
1009             }
1010             break;
1011         case GDK_Up: // move last point up
1012         case GDK_KP_Up:
1013         case GDK_KP_8:
1014             if (!MOD__CTRL) { // not ctrl
1015                 if (MOD__ALT) { // alt
1016                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
1017                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
1018                 }
1019                 else { // no alt
1020                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
1021                     else pen_lastpoint_move(pc, 0, nudge); // no shift
1022                 }
1023                 ret = TRUE;
1024             }
1025             break;
1026         case GDK_Right: // move last point right
1027         case GDK_KP_Right:
1028         case GDK_KP_6:
1029             if (!MOD__CTRL) { // not ctrl
1030                 if (MOD__ALT) { // alt
1031                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1032                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1033                 }
1034                 else { // no alt
1035                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1036                     else pen_lastpoint_move(pc, nudge, 0); // no shift
1037                 }
1038                 ret = TRUE;
1039             }
1040             break;
1041         case GDK_Down: // move last point down
1042         case GDK_KP_Down:
1043         case GDK_KP_2:
1044             if (!MOD__CTRL) { // not ctrl
1045                 if (MOD__ALT) { // alt
1046                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1047                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1048                 }
1049                 else { // no alt
1050                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1051                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
1052                 }
1053                 ret = TRUE;
1054             }
1055             break;
1057 /* TODO: this is not yet enabled?? looks like some traces of the Geometry tool
1058         case GDK_P:
1059         case GDK_p:
1060             if (MOD__SHIFT_ONLY) {
1061                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1062                 ret = TRUE;
1063             }
1064             break;
1066         case GDK_C:
1067         case GDK_c:
1068             if (MOD__SHIFT_ONLY) {
1069                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1070                 ret = TRUE;
1071             }
1072             break;
1074         case GDK_B:
1075         case GDK_b:
1076             if (MOD__SHIFT_ONLY) {
1077                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1078                 ret = TRUE;
1079             }
1080             break;
1082         case GDK_A:
1083         case GDK_a:
1084             if (MOD__SHIFT_ONLY) {
1085                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1086                 ret = TRUE;
1087             }
1088             break;
1089 */
1091         case GDK_U:
1092         case GDK_u:
1093             if (MOD__SHIFT_ONLY) {
1094                 pen_lastpoint_tocurve(pc);
1095                 ret = TRUE;
1096             }
1097             break;
1098         case GDK_L:
1099         case GDK_l:
1100             if (MOD__SHIFT_ONLY) {
1101                 pen_lastpoint_toline(pc);
1102                 ret = TRUE;
1103             }
1104             break;
1106         case GDK_Return:
1107         case GDK_KP_Enter:
1108             if (pc->npoints != 0) {
1109                 spdc_pen_finish(pc, FALSE);
1110                 ret = TRUE;
1111             }
1112             break;
1113         case GDK_Escape:
1114             if (pc->npoints != 0) {
1115                 // if drawing, cancel, otherwise pass it up for deselecting
1116                 pen_cancel (pc);
1117                 ret = TRUE;
1118             }
1119             break;
1120         case GDK_z:
1121         case GDK_Z:
1122             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1123                 // if drawing, cancel, otherwise pass it up for undo
1124                 pen_cancel (pc);
1125                 ret = TRUE;
1126             }
1127             break;
1128         case GDK_g:
1129         case GDK_G:
1130             if (MOD__SHIFT_ONLY) {
1131                 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
1132                 ret = true;
1133             }
1134             break;
1135         case GDK_BackSpace:
1136         case GDK_Delete:
1137         case GDK_KP_Delete:
1138             if ( pc->green_curve->is_empty() || (pc->green_curve->last_segment() == NULL) ) {
1139                 if (!pc->red_curve->is_empty()) {
1140                     pen_cancel (pc);
1141                     ret = TRUE;
1142                 } else {
1143                     // do nothing; this event should be handled upstream
1144                 }
1145             } else {
1146                 /* Reset red curve */
1147                 pc->red_curve->reset();
1148                 /* Destroy topmost green bpath */
1149                 if (pc->green_bpaths) {
1150                     if (pc->green_bpaths->data)
1151                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1152                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1153                 }
1154                 /* Get last segment */
1155                 if ( pc->green_curve->is_empty() ) {
1156                     g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1157                     break;
1158                 }
1159                 // The code below assumes that pc->green_curve has only ONE path !
1160                 Geom::Curve const * crv = pc->green_curve->last_segment();
1161                 pc->p[0] = crv->initialPoint();
1162                 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1163                     pc->p[1] = (*cubic)[1];
1164                 } else {
1165                     pc->p[1] = pc->p[0];
1166                 }
1167                 Geom::Point const pt(( pc->npoints < 4
1168                                      ? (Geom::Point)(crv->finalPoint())
1169                                      : pc->p[3] ));
1170                 pc->npoints = 2;
1171                 pc->green_curve->backspace();
1172                 sp_canvas_item_hide(pc->c0);
1173                 sp_canvas_item_hide(pc->c1);
1174                 sp_canvas_item_hide(pc->cl0);
1175                 sp_canvas_item_hide(pc->cl1);
1176                 pc->state = SP_PEN_CONTEXT_POINT;
1177                 spdc_pen_set_subsequent_point(pc, pt, true);
1178                 pen_last_paraxial_dir = !pen_last_paraxial_dir;
1179                 ret = TRUE;
1180             }
1181             break;
1182         default:
1183             break;
1184     }
1185     return ret;
1188 static void
1189 spdc_reset_colors(SPPenContext *pc)
1191     /* Red */
1192     pc->red_curve->reset();
1193     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1194     /* Blue */
1195     pc->blue_curve->reset();
1196     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1197     /* Green */
1198     while (pc->green_bpaths) {
1199         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1200         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1201     }
1202     pc->green_curve->reset();
1203     if (pc->green_anchor) {
1204         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1205     }
1206     pc->sa = NULL;
1207     pc->ea = NULL;
1208     pc->npoints = 0;
1209     pc->red_curve_is_valid = false;
1213 static void
1214 spdc_pen_set_initial_point(SPPenContext *const pc, Geom::Point const p)
1216     g_assert( pc->npoints == 0 );
1218     pc->p[0] = p;
1219     pc->p[1] = p;
1220     pc->npoints = 2;
1221     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1223     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1226 /**
1227  * Show the status message for the current line/curve segment.
1228  * This type of message always shows angle/distance as the last
1229  * two parameters ("angle %3.2f&#176;, distance %s").
1230  */
1231 static void
1232 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, Geom::Point const p, int pc_point_to_compare, gchar const *message)
1234     g_assert(pc != NULL);
1235     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1236     g_assert(message != NULL);
1238     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1239     Geom::Point rel = p - pc->p[pc_point_to_compare];
1240     GString *dist = SP_PX_TO_METRIC_STRING(Geom::L2(rel), desktop->namedview->getDefaultMetric());
1241     double angle = atan2(rel[Geom::Y], rel[Geom::X]) * 180 / M_PI;
1242     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1243     if (prefs->getBool("/options/compassangledisplay/value", 0) != 0)
1244         angle = angle_to_compass (angle);
1246     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1247     g_string_free(dist, FALSE);
1250 static void
1251 spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status)
1253     g_assert( pc->npoints != 0 );
1254     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1256     pc->p[2] = p;
1257     pc->p[3] = p;
1258     pc->p[4] = p;
1259     pc->npoints = 5;
1260     pc->red_curve->reset();
1261     bool is_curve;
1262     pc->red_curve->moveto(pc->p[0]);
1263     if (pc->polylines_paraxial && !statusbar) {
1264         // we are drawing horizontal/vertical lines and hit an anchor;
1265         Geom::Point const origin = pc->p[0];
1266         // if the previous point and the anchor are not aligned either horizontally or vertically...
1267         if ((abs(p[Geom::X] - origin[Geom::X]) > 1e-9) && (abs(p[Geom::Y] - origin[Geom::Y]) > 1e-9)) {
1268             // ...then we should draw an L-shaped path, consisting of two paraxial segments
1269             Geom::Point intermed = p;
1270             pen_set_to_nearest_horiz_vert(pc, intermed, status, false);
1271             pc->red_curve->lineto(intermed);
1272         }
1273         pc->red_curve->lineto(p);
1274         is_curve = false;
1275     } else {
1276         // one of the 'regular' modes
1277         if (pc->p[1] != pc->p[0]) {
1278             pc->red_curve->curveto(pc->p[1], p, p);
1279             is_curve = true;
1280         } else {
1281             pc->red_curve->lineto(p);
1282             is_curve = false;
1283         }
1284     }
1286     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1288     if (statusbar) {
1289         gchar *message = is_curve ?
1290             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1291             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1292         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1293     }
1296 static void
1297 spdc_pen_set_ctrl(SPPenContext *const pc, Geom::Point const p, guint const state)
1299     sp_canvas_item_show(pc->c1);
1300     sp_canvas_item_show(pc->cl1);
1302     if ( pc->npoints == 2 ) {
1303         pc->p[1] = p;
1304         sp_canvas_item_hide(pc->c0);
1305         sp_canvas_item_hide(pc->cl0);
1306         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1307         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1309         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"));
1310     } else if ( pc->npoints == 5 ) {
1311         pc->p[4] = p;
1312         sp_canvas_item_show(pc->c0);
1313         sp_canvas_item_show(pc->cl0);
1314         bool is_symm = false;
1315         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1316              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1317             Geom::Point delta = p - pc->p[3];
1318             pc->p[2] = pc->p[3] - delta;
1319             is_symm = true;
1320             pc->red_curve->reset();
1321             pc->red_curve->moveto(pc->p[0]);
1322             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1323             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1324         }
1325         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1326         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1327         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1328         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1330         gchar *message = is_symm ?
1331             _("<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") :
1332             _("<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");
1333         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1334     } else {
1335         g_warning("Something bad happened - npoints is %d", pc->npoints);
1336     }
1339 static void
1340 spdc_pen_finish_segment(SPPenContext *const pc, Geom::Point const p, guint const state)
1342     if (pc->polylines_paraxial) {
1343         pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1344     }
1346     ++pc->num_clicks;
1348     if (!pc->red_curve->is_empty()) {
1349         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1350         SPCurve *curve = pc->red_curve->copy();
1351         /// \todo fixme:
1352         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1353         curve->unref();
1354         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1356         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1358         pc->p[0] = pc->p[3];
1359         pc->p[1] = pc->p[4];
1360         pc->npoints = 2;
1362         pc->red_curve->reset();
1363     }
1366 static void
1367 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1369     if (pc->expecting_clicks_for_LPE > 1) {
1370         // don't let the path be finished before we have collected the required number of mouse clicks
1371         return;
1372     }
1374     pc->num_clicks = 0;
1376     pen_disable_events(pc);
1378     SPDesktop *const desktop = pc->desktop;
1379     pc->_message_context->clear();
1380     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1382     pc->red_curve->reset();
1383     spdc_concat_colors_and_flush(pc, closed);
1384     pc->sa = NULL;
1385     pc->ea = NULL;
1387     pc->npoints = 0;
1388     pc->state = SP_PEN_CONTEXT_POINT;
1390     sp_canvas_item_hide(pc->c0);
1391     sp_canvas_item_hide(pc->c1);
1392     sp_canvas_item_hide(pc->cl0);
1393     sp_canvas_item_hide(pc->cl1);
1395     if (pc->green_anchor) {
1396         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1397     }
1400     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1402     pen_enable_events(pc);
1405 static void
1406 pen_disable_events(SPPenContext *const pc) {
1407   pc->events_disabled++;
1410 static void
1411 pen_enable_events(SPPenContext *const pc) {
1412   g_return_if_fail(pc->events_disabled != 0);
1414   pc->events_disabled--;
1417 void
1418 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1419                                          unsigned int num_clicks, bool use_polylines)
1421     if (effect_type == Inkscape::LivePathEffect::INVALID_LPE)
1422         return;
1424     pc->waiting_LPE_type = effect_type;
1425     pc->expecting_clicks_for_LPE = num_clicks;
1426     pc->polylines_only = use_polylines;
1427     pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
1430 void
1431 sp_pen_context_cancel_waiting_for_LPE(SPPenContext *pc)
1433     pc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
1434     pc->expecting_clicks_for_LPE = 0;
1435     sp_pen_context_set_polyline_mode(pc);
1438 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1439                                        Geom::Point const &pt, Geom::Point const &origin, guint state) {
1440     /*
1441      * after the first mouse click we determine whether the mouse pointer is closest to a
1442      * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1443      * orthogonal to the last one; pressing Shift toggles the direction
1444      */
1445     // num_clicks is not reliable because spdc_pen_finish_segment is sometimes called too early
1446     // (on first mouse release), in which case num_clicks immediately becomes 1.
1447     // if (pc->num_clicks == 0) {
1449     if (pc->green_curve->is_empty()) {
1450         // first mouse click
1451         double dist_h = fabs(pt[Geom::X] - origin[Geom::X]);
1452         double dist_v = fabs(pt[Geom::Y] - origin[Geom::Y]);
1453         int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1454         pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1455         return pen_last_paraxial_dir;
1456     } else {
1457         // subsequent mouse click
1458         return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1459     }
1462 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state, bool snap)
1464     Geom::Point const origin = pc->p[0];
1466     int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1468     if (!snap) {
1469         if (next_dir == 0) {
1470             // line is forced to be horizontal
1471             pt[Geom::Y] = origin[Geom::Y];
1472         } else {
1473             // line is forced to be vertical
1474             pt[Geom::X] = origin[Geom::X];
1475         }
1476     } else {
1477         // Create a horizontal or vertical constraint line
1478         Inkscape::Snapper::SnapConstraint cl(origin, next_dir ? Geom::Point(0, 1) : Geom::Point(1, 0));
1480         // Snap along the constraint line; if we didn't snap then still the constraint will be applied
1481         SnapManager &m = pc->desktop->namedview->snap_manager;
1483         Inkscape::Selection *selection = sp_desktop_selection (pc->desktop);
1484         // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
1485         // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
1487         m.setup(pc->desktop, true, selection->singleItem());
1488         m.constrainedSnapReturnByRef(pt, Inkscape::SNAPSOURCE_NODE_HANDLE, cl);
1489         m.unSetup();
1490     }
1493 /*
1494   Local Variables:
1495   mode:c++
1496   c-file-style:"stroustrup"
1497   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1498   indent-tabs-mode:nil
1499   fill-column:99
1500   End:
1501 */
1502 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :