Code

Move single-dot-creation function to a better location (and rename it accordingly)
[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 "prefs-utils.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, gchar const *key, gchar const *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, NR::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status = 0);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, NR::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 NR::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, NR::Point const &pt, NR::Point const &origin, guint state);
79 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state);
80 static NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt, guint const state);
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;
151     
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     guint mode = prefs_get_int_attribute("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     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
237         ec->enableSelectionCue();
238     }
241 static void
242 pen_cancel (SPPenContext *const pc) 
244     pc->num_clicks = 0;
245     pc->state = SP_PEN_CONTEXT_STOP;
246     spdc_reset_colors(pc);
247     sp_canvas_item_hide(pc->c0);
248     sp_canvas_item_hide(pc->c1);
249     sp_canvas_item_hide(pc->cl0);
250     sp_canvas_item_hide(pc->cl1);
251     pc->_message_context->clear();
252     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
254     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
257 /**
258  * Finalization callback.
259  */
260 static void
261 sp_pen_context_finish(SPEventContext *ec)
263     SPPenContext *pc = SP_PEN_CONTEXT(ec);
265     if (pc->npoints != 0) {
266         pen_cancel (pc);
267     }
269     if (((SPEventContextClass *) pen_parent_class)->finish) {
270         ((SPEventContextClass *) pen_parent_class)->finish(ec);
271     }
274 /**
275  * Callback that sets key to value in pen context.
276  */
277 static void
278 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
280     SPPenContext *pc = SP_PEN_CONTEXT(ec);
282     if (!strcmp(key, "mode")) {
283         if ( val && !strcmp(val, "drag") ) {
284             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
285         } else {
286             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
287         }
288     }
291 /**
292  * Snaps new node relative to the previous node.
293  */
294 static void
295 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
297     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
298         if (pc->npoints > 0) {
299             spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
300         }
301     } else {
302         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
303                                          //After all, the user explicitely asked for angular snapping by
304                                          //pressing CTRL
305             spdc_endpoint_snap_free(pc, p, state);
306         }
307     }
308     if (pc->polylines_paraxial) {
309         // TODO: must we avoid one of the snaps in the previous case distinction in some situations?
310         pen_set_to_nearest_horiz_vert(pc, p, state);
311     }
314 /**
315  * Snaps new node's handle relative to the new node.
316  */
317 static void
318 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
320     g_return_if_fail(( pc->npoints == 2 ||
321                        pc->npoints == 5   ));
323     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
324         spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
325     } else {
326         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
327             spdc_endpoint_snap_free(pc, p, state);
328         }
329     }
332 static gint 
333 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
335     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
337     gint ret = FALSE;
339     switch (event->type) {
340         case GDK_BUTTON_PRESS:
341             ret = pen_handle_button_press(pc, event->button);
342             break;
343         case GDK_BUTTON_RELEASE:
344             ret = pen_handle_button_release(pc, event->button);
345             break;
346         default:
347             break;
348     }
350     if (!ret) {
351         if (((SPEventContextClass *) pen_parent_class)->item_handler)
352             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
353     }
355     return ret;
358 /**
359  * Callback to handle all pen events.
360  */
361 static gint
362 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
364     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
366     gint ret = FALSE;
368     switch (event->type) {
369         case GDK_BUTTON_PRESS:
370             ret = pen_handle_button_press(pc, event->button);
371             break;
373         case GDK_MOTION_NOTIFY:
374             ret = pen_handle_motion_notify(pc, event->motion);
375             break;
377         case GDK_BUTTON_RELEASE:
378             ret = pen_handle_button_release(pc, event->button);
379             break;
381         case GDK_2BUTTON_PRESS:
382             ret = pen_handle_2button_press(pc, event->button);
383             break;
385         case GDK_KEY_PRESS:
386             ret = pen_handle_key_press(pc, event);
387             break;
389         default:
390             break;
391     }
393     if (!ret) {
394         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
395             = ((SPEventContextClass *) pen_parent_class)->root_handler;
396         if (parent_root_handler) {
397             ret = parent_root_handler(ec, event);
398         }
399     }
401     return ret;
404 /**
405  * Handle mouse button press event.
406  */
407 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
409     if (pc->events_disabled) {
410         // skip event processing if events are disabled
411         return FALSE;
412     }
414     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
415     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
416     NR::Point const event_w(bevent.x, bevent.y);
417     NR::Point const event_dt(desktop->w2d(event_w));
418     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
420     gint ret = FALSE;
421     if (bevent.button == 1 && !event_context->space_panning
422         // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
423         && pc->expecting_clicks_for_LPE != 1) {
425         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
426             return TRUE;
427         }
429         if (!pc->grab ) {
430             /* Grab mouse, so release will not pass unnoticed */
431             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
432             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
433                                             GDK_BUTTON_RELEASE_MASK |
434                                             GDK_POINTER_MOTION_MASK  ),
435                                 NULL, bevent.time);
436         }
438         pen_drag_origin_w = event_w;
439         pen_within_tolerance = true;
441         /* Test whether we hit any anchor. */
442         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
444         switch (pc->mode) {
445             case SP_PEN_CONTEXT_MODE_CLICK:
446                 /* In click mode we add point on release */
447                 switch (pc->state) {
448                     case SP_PEN_CONTEXT_POINT:
449                     case SP_PEN_CONTEXT_CONTROL:
450                     case SP_PEN_CONTEXT_CLOSE:
451                         break;
452                     case SP_PEN_CONTEXT_STOP:
453                         /* This is allowed, if we just cancelled curve */
454                         pc->state = SP_PEN_CONTEXT_POINT;
455                         break;
456                     default:
457                         break;
458                 }
459                 break;
460             case SP_PEN_CONTEXT_MODE_DRAG:
461                 switch (pc->state) {
462                     case SP_PEN_CONTEXT_STOP:
463                         /* This is allowed, if we just cancelled curve */
464                     case SP_PEN_CONTEXT_POINT:
465                         if (pc->npoints == 0) {
467                             if (bevent.state & GDK_CONTROL_MASK) {
468                                 spdc_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
469                                 ret = TRUE;
470                                 break;
471                             }
473                             // TODO: Perhaps it would be nicer to rearrange the following case
474                             // distinction so that the case of a waiting LPE is treated separately
476                             /* Set start anchor */
477                             pc->sa = anchor;
478                             NR::Point p;
479                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
480                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
481                                    a fresh path to be created so don't continue an existing one */
482                                 p = anchor->dp;
483                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
484                             } else {
485                                 // This is the first click of a new curve; deselect item so that
486                                 // this curve is not combined with it (unless it is drawn from its
487                                 // anchor, which is handled by the sibling branch above)
488                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
489                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
490                                     /* if we have a waiting LPE, we need a fresh path to be created
491                                        so don't append to an existing one */
492                                     selection->clear();
493                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
494                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
495                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
496                                 }
498                                 /* Create green anchor */
499                                 p = event_dt;
500                                 if (!pc->polylines_paraxial) {
501                                     // only snap the starting point if we're not in horizontal/vertical mode
502                                     // because otherwise it gets shifted; TODO: why do we snap here at all??
503                                     spdc_endpoint_snap(pc, p, bevent.state);
504                                 }
505                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
506                             }
507                             spdc_pen_set_initial_point(pc, p);
508                         } else {
510                             /* Set end anchor */
511                             pc->ea = anchor;
512                             NR::Point p;
513                             if (anchor) {
514                                 p = anchor->dp;
515                                 // we hit an anchor, will finish the curve (either with or without closing)
516                                 // in release handler
517                                 pc->state = SP_PEN_CONTEXT_CLOSE;
519                                 if (pc->green_anchor && pc->green_anchor->active) {
520                                     // we clicked on the current curve start, so close it even if
521                                     // we drag a handle away from it
522                                     dc->green_closed = TRUE;
523                                 }
524                                 ret = TRUE;
525                                 break;
527                             } else {
528                                 p = event_dt;
529                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
530                                 spdc_pen_set_subsequent_point(pc, p, true);
531                                 if (pc->polylines_only) {
532                                     spdc_pen_finish_segment(pc, p, bevent.state);
533                                 }
534                             }
535                         }
537                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
538                         ret = TRUE;
539                         break;
540                     case SP_PEN_CONTEXT_CONTROL:
541                         g_warning("Button down in CONTROL state");
542                         break;
543                     case SP_PEN_CONTEXT_CLOSE:
544                         g_warning("Button down in CLOSE state");
545                         break;
546                     default:
547                         break;
548                 }
549                 break;
550             default:
551                 break;
552         }
553     } else if (bevent.button == 3 || pc->expecting_clicks_for_LPE == 1) { // when the last click for a waiting LPE occurs we want to finish the path
554         if (pc->npoints != 0) {
556             spdc_pen_finish_segment(pc, event_dt, bevent.state);
557             if (pc->green_closed) {
558                 // finishing at the start anchor, close curve
559                 spdc_pen_finish(pc, TRUE);
560             } else {
561                 // finishing at some other anchor, finish curve but not close
562                 spdc_pen_finish(pc, FALSE);
563             }
565             ret = TRUE;
566         }
567     }
569     if (pc->expecting_clicks_for_LPE > 0) {
570         --pc->expecting_clicks_for_LPE;
571     }
573     return ret;
576 /**
577  * Handle motion_notify event.
578  */
579 static gint
580 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
582     gint ret = FALSE;
584     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
585     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
587     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
588         // allow scrolling
589         return FALSE;
590     }
591    
592     if (pc->events_disabled) {
593         // skip motion events if pen events are disabled
594         return FALSE;
595     }
597     NR::Point const event_w(mevent.x,
598                             mevent.y);
599     if (pen_within_tolerance) {
600         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
601                                                                "value", 0, 0, 100);
602         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
603             return FALSE;   // Do not drag if we're within tolerance from origin.
604         }
605     }
606     // Once the user has moved farther than tolerance from the original location
607     // (indicating they intend to move the object, not click), then always process the
608     // motion notify coordinates as given (no snapping back to origin)
609     pen_within_tolerance = false;
611     /* Find desktop coordinates */
612     NR::Point p = dt->w2d(event_w);
614     /* Test, whether we hit any anchor */
615     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
617     switch (pc->mode) {
618         case SP_PEN_CONTEXT_MODE_CLICK:
619             switch (pc->state) {
620                 case SP_PEN_CONTEXT_POINT:
621                     if ( pc->npoints != 0 ) {
622                         /* Only set point, if we are already appending */
623                         spdc_endpoint_snap(pc, p, mevent.state);
624                         spdc_pen_set_subsequent_point(pc, p, true);
625                         ret = TRUE;
626                     }
627                     break;
628                 case SP_PEN_CONTEXT_CONTROL:
629                 case SP_PEN_CONTEXT_CLOSE:
630                     /* Placing controls is last operation in CLOSE state */
631                     spdc_endpoint_snap(pc, p, mevent.state);
632                     spdc_pen_set_ctrl(pc, p, mevent.state);
633                     ret = TRUE;
634                     break;
635                 case SP_PEN_CONTEXT_STOP:
636                     /* This is perfectly valid */
637                     break;
638                 default:
639                     break;
640             }
641             break;
642         case SP_PEN_CONTEXT_MODE_DRAG:
643             switch (pc->state) {
644                 case SP_PEN_CONTEXT_POINT:
645                     if ( pc->npoints > 0 ) {
646                         /* Only set point, if we are already appending */
648                         if (!anchor) {   /* Snap node only if not hitting anchor */
649                             spdc_endpoint_snap(pc, p, mevent.state);
650                         }
652                         spdc_pen_set_subsequent_point(pc, p, !anchor, mevent.state);
654                         if (anchor && !pc->anchor_statusbar) {
655                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
656                             pc->anchor_statusbar = true;
657                         } else if (!anchor && pc->anchor_statusbar) {
658                             pc->_message_context->clear();
659                             pc->anchor_statusbar = false;
660                         }
662                         ret = TRUE;
663                     } else {
664                         if (anchor && !pc->anchor_statusbar) {
665                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
666                             pc->anchor_statusbar = true;
667                         } else if (!anchor && pc->anchor_statusbar) {
668                             pc->_message_context->clear();
669                             pc->anchor_statusbar = false;
670                         }
671                     }
672                     break;
673                 case SP_PEN_CONTEXT_CONTROL:
674                 case SP_PEN_CONTEXT_CLOSE:
675                     /* Placing controls is last operation in CLOSE state */
677                     // snap the handle
678                     spdc_endpoint_snap_handle(pc, p, mevent.state);
680                     if (!pc->polylines_only) {
681                         spdc_pen_set_ctrl(pc, p, mevent.state);
682                     } else {
683                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
684                     }
685                     gobble_motion_events(GDK_BUTTON1_MASK);
686                     ret = TRUE;
687                     break;
688                 case SP_PEN_CONTEXT_STOP:
689                     /* This is perfectly valid */
690                     break;
691                 default:
692                     break;
693             }
694             break;
695         default:
696             break;
697     }
698     return ret;
701 /**
702  * Handle mouse button release event.
703  */
704 static gint
705 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
707     if (pc->events_disabled) {
708         // skip event processing if events are disabled
709         return FALSE;
710     }
712     gint ret = FALSE;
713     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
714     if ( revent.button == 1  && !event_context->space_panning) {
716         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
718         NR::Point const event_w(revent.x,
719                                 revent.y);
720         /* Find desktop coordinates */
721         NR::Point p = pc->desktop->w2d(event_w);
723         /* Test whether we hit any anchor. */
724         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
726         switch (pc->mode) {
727             case SP_PEN_CONTEXT_MODE_CLICK:
728                 switch (pc->state) {
729                     case SP_PEN_CONTEXT_POINT:
730                         if ( pc->npoints == 0 ) {
731                             /* Start new thread only with button release */
732                             if (anchor) {
733                                 p = anchor->dp;
734                             }
735                             pc->sa = anchor;
736                             spdc_pen_set_initial_point(pc, p);
737                         } else {
738                             /* Set end anchor here */
739                             pc->ea = anchor;
740                             if (anchor) {
741                                 p = anchor->dp;
742                             }
743                         }
744                         pc->state = SP_PEN_CONTEXT_CONTROL;
745                         ret = TRUE;
746                         break;
747                     case SP_PEN_CONTEXT_CONTROL:
748                         /* End current segment */
749                         spdc_endpoint_snap(pc, p, revent.state);
750                         spdc_pen_finish_segment(pc, p, revent.state);
751                         pc->state = SP_PEN_CONTEXT_POINT;
752                         ret = TRUE;
753                         break;
754                     case SP_PEN_CONTEXT_CLOSE:
755                         /* End current segment */
756                         if (!anchor) {   /* Snap node only if not hitting anchor */
757                             spdc_endpoint_snap(pc, p, revent.state);
758                         }
759                         spdc_pen_finish_segment(pc, p, revent.state);
760                         spdc_pen_finish(pc, TRUE);
761                         pc->state = SP_PEN_CONTEXT_POINT;
762                         ret = TRUE;
763                         break;
764                     case SP_PEN_CONTEXT_STOP:
765                         /* This is allowed, if we just cancelled curve */
766                         pc->state = SP_PEN_CONTEXT_POINT;
767                         ret = TRUE;
768                         break;
769                     default:
770                         break;
771                 }
772                 break;
773             case SP_PEN_CONTEXT_MODE_DRAG:
774                 switch (pc->state) {
775                     case SP_PEN_CONTEXT_POINT:
776                     case SP_PEN_CONTEXT_CONTROL:
777                         if (!pc->polylines_only) {
778                             spdc_endpoint_snap(pc, p, revent.state);
779                             spdc_pen_finish_segment(pc, p, revent.state);
780                         }
781                         break;
782                     case SP_PEN_CONTEXT_CLOSE:
783                         spdc_endpoint_snap(pc, p, revent.state);
784                         spdc_pen_finish_segment(pc, p, revent.state);
785                         if (pc->green_closed) {
786                             // finishing at the start anchor, close curve
787                             spdc_pen_finish(pc, TRUE);
788                         } else {
789                             // finishing at some other anchor, finish curve but not close
790                             spdc_pen_finish(pc, FALSE);
791                         }
792                         break;
793                     case SP_PEN_CONTEXT_STOP:
794                         /* This is allowed, if we just cancelled curve */
795                         break;
796                     default:
797                         break;
798                 }
799                 pc->state = SP_PEN_CONTEXT_POINT;
800                 ret = TRUE;
801                 break;
802             default:
803                 break;
804         }
806         if (pc->grab) {
807             /* Release grab now */
808             sp_canvas_item_ungrab(pc->grab, revent.time);
809             pc->grab = NULL;
810         }
812         ret = TRUE;
814         dc->green_closed = FALSE;
815     }
817     // TODO: can we be sure that the path was created correctly?
818     // TODO: should we offer an option to collect the clicks in a list?
819     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
820         sp_pen_context_set_polyline_mode(pc);
822         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
823         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
825         if (pc->waiting_LPE) {
826             // we have an already created LPE waiting for a path
827             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
828             selection->add(SP_OBJECT(pc->waiting_item));
829             pc->waiting_LPE = NULL;
830         } else {
831             // the case that we need to create a new LPE and apply it to the just-drawn path is
832             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
833         }
834     }
836     return ret;
839 static gint
840 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
842     gint ret = FALSE;
843     if (pc->npoints != 0 && bevent.button != 2) {
844         spdc_pen_finish(pc, FALSE);
845         ret = TRUE;
846     }
847     return ret;
850 void
851 pen_redraw_all (SPPenContext *const pc)
853     // green
854     if (pc->green_bpaths) {
855         // remove old piecewise green canvasitems
856         while (pc->green_bpaths) {
857             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
858             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
859         }
860         // one canvas bpath for all of green_curve
861         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
862         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
863         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
865         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
866     }
868     if (pc->green_anchor)
869         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
871     pc->red_curve->reset();
872     pc->red_curve->moveto(pc->p[0]);
873     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
874     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
876     // handles
877     if (pc->p[0] != pc->p[1]) {
878         SP_CTRL(pc->c1)->moveto(pc->p[1]);
879         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
880         sp_canvas_item_show (pc->c1);
881         sp_canvas_item_show (pc->cl1);
882     } else {
883         sp_canvas_item_hide (pc->c1);
884         sp_canvas_item_hide (pc->cl1);
885     }
887     Geom::Curve const * last_seg = pc->green_curve->last_segment();
888     if (last_seg) {
889         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
890         if ( cubic &&
891              (*cubic)[2] != to_2geom(pc->p[0]) )
892         {
893             NR::Point p2 = (*cubic)[2];
894             SP_CTRL(pc->c0)->moveto(p2);
895             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
896             sp_canvas_item_show (pc->c0);
897             sp_canvas_item_show (pc->cl0);
898         } else {
899             sp_canvas_item_hide (pc->c0);
900             sp_canvas_item_hide (pc->cl0);
901         }
902     }
905 void
906 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
908     if (pc->npoints != 5)
909         return;
911     // green
912     if (!pc->green_curve->is_empty()) {
913         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
914     } else {
915         // start anchor too
916         if (pc->green_anchor) {
917             pc->green_anchor->dp += NR::Point(x, y);
918         }
919     }
921     // red
922     pc->p[0] += NR::Point(x, y);
923     pc->p[1] += NR::Point(x, y);
924     pen_redraw_all(pc);
927 void
928 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
930     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
933 void
934 pen_lastpoint_tocurve (SPPenContext *const pc)
936     if (pc->npoints != 5)
937         return;
939     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
940     if ( cubic ) {
941         pc->p[1] = pc->p[0] + (NR::Point)( (*cubic)[3] - (*cubic)[2] );
942     } else {
943         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
944     }
946     pen_redraw_all(pc);
949 void
950 pen_lastpoint_toline (SPPenContext *const pc)
952     if (pc->npoints != 5)
953         return;
955     pc->p[1] = pc->p[0];
957     pen_redraw_all(pc);
961 static gint
962 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
964     gint ret = FALSE;
965     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
967     switch (get_group0_keyval (&event->key)) {
969         case GDK_Left: // move last point left
970         case GDK_KP_Left:
971         case GDK_KP_4:
972             if (!MOD__CTRL) { // not ctrl
973                 if (MOD__ALT) { // alt
974                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
975                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
976                 }
977                 else { // no alt
978                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
979                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
980                 }
981                 ret = TRUE;
982             }
983             break;
984         case GDK_Up: // move last point up
985         case GDK_KP_Up:
986         case GDK_KP_8:
987             if (!MOD__CTRL) { // not ctrl
988                 if (MOD__ALT) { // alt
989                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
990                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
991                 }
992                 else { // no alt
993                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
994                     else pen_lastpoint_move(pc, 0, nudge); // no shift
995                 }
996                 ret = TRUE;
997             }
998             break;
999         case GDK_Right: // move last point right
1000         case GDK_KP_Right:
1001         case GDK_KP_6:
1002             if (!MOD__CTRL) { // not ctrl
1003                 if (MOD__ALT) { // alt
1004                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1005                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1006                 }
1007                 else { // no alt
1008                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1009                     else pen_lastpoint_move(pc, nudge, 0); // no shift
1010                 }
1011                 ret = TRUE;
1012             }
1013             break;
1014         case GDK_Down: // move last point down
1015         case GDK_KP_Down:
1016         case GDK_KP_2:
1017             if (!MOD__CTRL) { // not ctrl
1018                 if (MOD__ALT) { // alt
1019                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1020                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1021                 }
1022                 else { // no alt
1023                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1024                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
1025                 }
1026                 ret = TRUE;
1027             }
1028             break;
1030         case GDK_P:
1031         case GDK_p:
1032             if (MOD__SHIFT_ONLY) {
1033                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1034                 ret = TRUE;
1035             }
1036             break;
1038         case GDK_C:
1039         case GDK_c:
1040             if (MOD__SHIFT_ONLY) {
1041                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1042                 ret = TRUE;
1043             }
1044             break;
1046         case GDK_B:
1047         case GDK_b:
1048             if (MOD__SHIFT_ONLY) {
1049                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1050                 ret = TRUE;
1051             }
1052             break;
1054         case GDK_A:
1055         case GDK_a:
1056             if (MOD__SHIFT_ONLY) {
1057                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1058                 ret = TRUE;
1059             }
1060             break;
1062         case GDK_U:
1063         case GDK_u:
1064             if (MOD__SHIFT_ONLY) {
1065                 pen_lastpoint_tocurve(pc);
1066                 ret = TRUE;
1067             }
1068             break;
1069         case GDK_L:
1070         case GDK_l:
1071             if (MOD__SHIFT_ONLY) {
1072                 pen_lastpoint_toline(pc);
1073                 ret = TRUE;
1074             }
1075             break;
1077         case GDK_Return:
1078         case GDK_KP_Enter:
1079             if (pc->npoints != 0) {
1080                 spdc_pen_finish(pc, FALSE);
1081                 ret = TRUE;
1082             }
1083             break;
1084         case GDK_Escape:
1085             if (pc->npoints != 0) {
1086                 // if drawing, cancel, otherwise pass it up for deselecting
1087                 pen_cancel (pc);
1088                 ret = TRUE;
1089             }
1090             break;
1091         case GDK_z:
1092         case GDK_Z:
1093             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1094                 // if drawing, cancel, otherwise pass it up for undo
1095                 pen_cancel (pc);
1096                 ret = TRUE;
1097             }
1098             break;
1099         case GDK_g:
1100         case GDK_G:
1101             if (MOD__SHIFT_ONLY) {
1102                 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
1103                 ret = true;
1104             }
1105             break;
1106         case GDK_BackSpace:
1107         case GDK_Delete:
1108         case GDK_KP_Delete:
1109             if (pc->green_curve->is_empty()) {
1110                 if (!pc->red_curve->is_empty()) {
1111                     pen_cancel (pc);
1112                     ret = TRUE;
1113                 } else {
1114                     // do nothing; this event should be handled upstream
1115                 }
1116             } else {
1117                 /* Reset red curve */
1118                 pc->red_curve->reset();
1119                 /* Destroy topmost green bpath */
1120                 if (pc->green_bpaths) {
1121                     if (pc->green_bpaths->data)
1122                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1123                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1124                 }
1125                 /* Get last segment */
1126                 if ( pc->green_curve->is_empty() ) {
1127                     g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1128                     break;
1129                 }
1130                 // The code below assumes that pc->green_curve has only ONE path !
1131                 Geom::Path const & path = pc->green_curve->get_pathvector().back();
1132                 Geom::Curve const * crv = &path.back_default();
1133                 pc->p[0] = crv->initialPoint();
1134                 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1135                     pc->p[1] = (*cubic)[1];
1136                 } else {
1137                     pc->p[1] = pc->p[0];
1138                 }
1139                 NR::Point const pt(( pc->npoints < 4
1140                                      ? (NR::Point)(crv->finalPoint())
1141                                      : pc->p[3] ));
1142                 pc->npoints = 2;
1143                 pc->green_curve->backspace();
1144                 sp_canvas_item_hide(pc->c0);
1145                 sp_canvas_item_hide(pc->c1);
1146                 sp_canvas_item_hide(pc->cl0);
1147                 sp_canvas_item_hide(pc->cl1);
1148                 pc->state = SP_PEN_CONTEXT_POINT;
1149                 spdc_pen_set_subsequent_point(pc, pt, true);
1150                 ret = TRUE;
1151             }
1152             break;
1153         default:
1154             break;
1155     }
1156     return ret;
1159 static void
1160 spdc_reset_colors(SPPenContext *pc)
1162     /* Red */
1163     pc->red_curve->reset();
1164     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1165     /* Blue */
1166     pc->blue_curve->reset();
1167     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1168     /* Green */
1169     while (pc->green_bpaths) {
1170         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1171         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1172     }
1173     pc->green_curve->reset();
1174     if (pc->green_anchor) {
1175         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1176     }
1177     pc->sa = NULL;
1178     pc->ea = NULL;
1179     pc->npoints = 0;
1180     pc->red_curve_is_valid = false;
1184 static void
1185 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1187     g_assert( pc->npoints == 0 );
1189     pc->p[0] = p;
1190     pc->p[1] = p;
1191     pc->npoints = 2;
1192     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1194     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1197 /**
1198  * Show the status message for the current line/curve segment.
1199  * This type of message always shows angle/distance as the last
1200  * two parameters ("angle %3.2f&#176;, distance %s").
1201  */ 
1202 static void
1203 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1205     g_assert(pc != NULL);
1206     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1207     g_assert(message != NULL);
1209     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1210     NR::Point rel = p - pc->p[pc_point_to_compare];
1211     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1212     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1213     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1214         angle = angle_to_compass (angle);
1216     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1217     g_string_free(dist, FALSE);
1220 static void
1221 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status)
1223     g_assert( pc->npoints != 0 );
1224     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1226     pc->p[2] = p;
1227     pc->p[3] = p;
1228     pc->p[4] = p;
1229     pc->npoints = 5;
1230     pc->red_curve->reset();
1231     bool is_curve;
1232     pc->red_curve->moveto(pc->p[0]);
1233     if (pc->polylines_paraxial && !statusbar) {
1234         // we are drawing horizontal/vertical lines and hit an anchor; draw an L-shaped path
1235         NR::Point intermed = p;
1236         pen_set_to_nearest_horiz_vert(pc, intermed, status);
1237         pc->red_curve->lineto(intermed);
1238         pc->red_curve->lineto(p);
1239         is_curve = false;
1240     } else {
1241         // one of the 'regular' modes
1242         if (pc->p[1] != pc->p[0])
1243         {
1244             pc->red_curve->curveto(pc->p[1], p, p);
1245             is_curve = true;
1246         } else {
1247             pc->red_curve->lineto(p);
1248             is_curve = false;
1249         }
1250     }
1252     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1254     if (statusbar) {
1255         gchar *message = is_curve ?
1256             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1257             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1258         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1259     }
1262 static void
1263 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1265     sp_canvas_item_show(pc->c1);
1266     sp_canvas_item_show(pc->cl1);
1268     if ( pc->npoints == 2 ) {
1269         pc->p[1] = p;
1270         sp_canvas_item_hide(pc->c0);
1271         sp_canvas_item_hide(pc->cl0);
1272         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1273         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1275         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"));
1276     } else if ( pc->npoints == 5 ) {
1277         pc->p[4] = p;
1278         sp_canvas_item_show(pc->c0);
1279         sp_canvas_item_show(pc->cl0);
1280         bool is_symm = false;
1281         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1282              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1283             NR::Point delta = p - pc->p[3];
1284             pc->p[2] = pc->p[3] - delta;
1285             is_symm = true;
1286             pc->red_curve->reset();
1287             pc->red_curve->moveto(pc->p[0]);
1288             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1289             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1290         }
1291         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1292         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1293         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1294         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1296         gchar *message = is_symm ?
1297             _("<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") :
1298             _("<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");
1299         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1300     } else {
1301         g_warning("Something bad happened - npoints is %d", pc->npoints);
1302     }
1305 static void
1306 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1308     if (pc->polylines_paraxial) {
1309         pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1310     }
1311     ++pc->num_clicks;
1313     if (!pc->red_curve->is_empty()) {
1314         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1315         SPCurve *curve = pc->red_curve->copy();
1316         /// \todo fixme:
1317         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1318         curve->unref();
1319         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1321         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1323         pc->p[0] = pc->p[3];
1324         pc->p[1] = pc->p[4];
1325         pc->npoints = 2;
1327         pc->red_curve->reset();
1328     }
1331 static void
1332 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1334     if (pc->expecting_clicks_for_LPE > 1) {
1335         // don't let the path be finished before we have collected the required number of mouse clicks
1336         return;
1337     }
1339     pc->num_clicks = 0;
1341     pen_disable_events(pc);
1342     
1343     SPDesktop *const desktop = pc->desktop;
1344     pc->_message_context->clear();
1345     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1347     pc->red_curve->reset();
1348     spdc_concat_colors_and_flush(pc, closed);
1349     pc->sa = NULL;
1350     pc->ea = NULL;
1352     pc->npoints = 0;
1353     pc->state = SP_PEN_CONTEXT_POINT;
1355     sp_canvas_item_hide(pc->c0);
1356     sp_canvas_item_hide(pc->c1);
1357     sp_canvas_item_hide(pc->cl0);
1358     sp_canvas_item_hide(pc->cl1);
1360     if (pc->green_anchor) {
1361         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1362     }
1365     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1367     pen_enable_events(pc);
1370 static void
1371 pen_disable_events(SPPenContext *const pc) {
1372   pc->events_disabled++;
1375 static void
1376 pen_enable_events(SPPenContext *const pc) {
1377   g_return_if_fail(pc->events_disabled != 0);
1378   
1379   pc->events_disabled--;
1382 void
1383 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1384                                          unsigned int num_clicks, bool use_polylines)
1386     if (effect_type == Inkscape::LivePathEffect::INVALID_LPE)
1387         return;
1389     pc->waiting_LPE_type = effect_type;
1390     pc->expecting_clicks_for_LPE = num_clicks;
1391     pc->polylines_only = use_polylines;
1392     pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
1395 void
1396 sp_pen_context_cancel_waiting_for_LPE(SPPenContext *pc)
1398     pc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
1399     pc->expecting_clicks_for_LPE = 0;
1400     sp_pen_context_set_polyline_mode(pc);
1403 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1404                                        NR::Point const &pt, NR::Point const &origin, guint state) {
1405     /*
1406      * after the first mouse click we determine whether the mouse pointer is closest to a
1407      * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1408      * orthogonal to the last one; pressing Shift toggles the direction
1409      */
1410     if (pc->num_clicks == 0) {
1411         // first mouse click
1412         double dist_h = fabs(pt[NR::X] - origin[NR::X]);
1413         double dist_v = fabs(pt[NR::Y] - origin[NR::Y]);
1414         int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1415         pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1416         return pen_last_paraxial_dir;
1417     } else {
1418         // subsequent mouse click
1419         return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1420     }
1423 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state)
1425     NR::Point const &origin = pc->p[0];
1427     int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1429     if (next_dir == 0) {
1430         // line is forced to be horizontal
1431         pt[NR::Y] = origin[NR::Y];
1432     } else {
1433         // line is forced to be vertical
1434         pt[NR::X] = origin[NR::X];
1435     }
1438 NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt)
1440     NR::Point const &origin = pc->p[0];
1442     if (pen_last_paraxial_dir == 0) {
1443         return NR::Point (origin[NR::X], pt[NR::Y]);
1444     } else {
1445         return NR::Point (pt[NR::X], origin[NR::Y]);
1446     }
1449 /*
1450   Local Variables:
1451   mode:c++
1452   c-file-style:"stroustrup"
1453   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1454   indent-tabs-mode:nil
1455   fill-column:99
1456   End:
1457 */
1458 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :