Code

Improve comment in pen-context.cpp
[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"
45 static void sp_pen_context_class_init(SPPenContextClass *klass);
46 static void sp_pen_context_init(SPPenContext *pc);
47 static void sp_pen_context_dispose(GObject *object);
49 static void sp_pen_context_setup(SPEventContext *ec);
50 static void sp_pen_context_finish(SPEventContext *ec);
51 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
52 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
53 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
55 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
56 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status = 0);
57 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
58 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
60 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
62 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
63 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
64 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
65 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
66 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
67 static void spdc_reset_colors(SPPenContext *pc);
69 static void pen_disable_events(SPPenContext *const pc);
70 static void pen_enable_events(SPPenContext *const pc);
72 static NR::Point pen_drag_origin_w(0, 0);
73 static bool pen_within_tolerance = false;
75 static SPDrawContextClass *pen_parent_class;
77 static int pen_next_paraxial_direction(const SPPenContext *const pc, NR::Point const &pt, NR::Point const &origin, guint state);
78 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state);
79 static NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt, guint const state);
81 static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
83 /**
84  * Register SPPenContext with Gdk and return its type.
85  */
86 GType
87 sp_pen_context_get_type(void)
88 {
89     static GType type = 0;
90     if (!type) {
91         GTypeInfo info = {
92             sizeof(SPPenContextClass),
93             NULL, NULL,
94             (GClassInitFunc) sp_pen_context_class_init,
95             NULL, NULL,
96             sizeof(SPPenContext),
97             4,
98             (GInstanceInitFunc) sp_pen_context_init,
99             NULL,   /* value_table */
100         };
101         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
102     }
103     return type;
106 /**
107  * Initialize the SPPenContext vtable.
108  */
109 static void
110 sp_pen_context_class_init(SPPenContextClass *klass)
112     GObjectClass *object_class;
113     SPEventContextClass *event_context_class;
115     object_class = (GObjectClass *) klass;
116     event_context_class = (SPEventContextClass *) klass;
118     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
120     object_class->dispose = sp_pen_context_dispose;
122     event_context_class->setup = sp_pen_context_setup;
123     event_context_class->finish = sp_pen_context_finish;
124     event_context_class->set = sp_pen_context_set;
125     event_context_class->root_handler = sp_pen_context_root_handler;
126     event_context_class->item_handler = sp_pen_context_item_handler;
129 /**
130  * Callback to initialize SPPenContext object.
131  */
132 static void
133 sp_pen_context_init(SPPenContext *pc)
136     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
138     event_context->cursor_shape = cursor_pen_xpm;
139     event_context->hot_x = 4;
140     event_context->hot_y = 4;
142     pc->npoints = 0;
143     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
144     pc->state = SP_PEN_CONTEXT_POINT;
146     pc->c0 = NULL;
147     pc->c1 = NULL;
148     pc->cl0 = NULL;
149     pc->cl1 = NULL;
150     
151     pc->events_disabled = 0;
153     pc->num_clicks = 0;
154     pc->waiting_LPE = NULL;
157 /**
158  * Callback to destroy the SPPenContext object's members and itself.
159  */
160 static void
161 sp_pen_context_dispose(GObject *object)
163     SPPenContext *pc;
165     pc = SP_PEN_CONTEXT(object);
167     if (pc->c0) {
168         gtk_object_destroy(GTK_OBJECT(pc->c0));
169         pc->c0 = NULL;
170     }
171     if (pc->c1) {
172         gtk_object_destroy(GTK_OBJECT(pc->c1));
173         pc->c1 = NULL;
174     }
175     if (pc->cl0) {
176         gtk_object_destroy(GTK_OBJECT(pc->cl0));
177         pc->cl0 = NULL;
178     }
179     if (pc->cl1) {
180         gtk_object_destroy(GTK_OBJECT(pc->cl1));
181         pc->cl1 = NULL;
182     }
184     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
186     if (pc->expecting_clicks_for_LPE > 0) {
187         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
188         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
189     }
192 void
193 sp_pen_context_set_polyline_mode(SPPenContext *const pc) {
194     guint mode = prefs_get_int_attribute("tools.freehand.pen", "freehand-mode", 0);
195     pc->polylines_only = (mode == 2 || mode == 3);
196     pc->polylines_paraxial = (mode == 3);
199 /**
200  * Callback to initialize SPPenContext object.
201  */
202 static void
203 sp_pen_context_setup(SPEventContext *ec)
205     SPPenContext *pc;
207     pc = SP_PEN_CONTEXT(ec);
209     if (((SPEventContextClass *) pen_parent_class)->setup) {
210         ((SPEventContextClass *) pen_parent_class)->setup(ec);
211     }
213     /* Pen indicators */
214     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
215                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
216     pc->c1 = 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->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
219     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
220     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
221     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
223     sp_canvas_item_hide(pc->c0);
224     sp_canvas_item_hide(pc->c1);
225     sp_canvas_item_hide(pc->cl0);
226     sp_canvas_item_hide(pc->cl1);
228     sp_event_context_read(ec, "mode");
230     pc->anchor_statusbar = false;
232     sp_pen_context_set_polyline_mode(pc);
234     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
235         ec->enableSelectionCue();
236     }
239 static void
240 pen_cancel (SPPenContext *const pc) 
242     pc->num_clicks = 0;
243     pc->state = SP_PEN_CONTEXT_STOP;
244     spdc_reset_colors(pc);
245     sp_canvas_item_hide(pc->c0);
246     sp_canvas_item_hide(pc->c1);
247     sp_canvas_item_hide(pc->cl0);
248     sp_canvas_item_hide(pc->cl1);
249     pc->_message_context->clear();
250     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
252     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
255 /**
256  * Finalization callback.
257  */
258 static void
259 sp_pen_context_finish(SPEventContext *ec)
261     SPPenContext *pc = SP_PEN_CONTEXT(ec);
263     if (pc->npoints != 0) {
264         pen_cancel (pc);
265     }
267     if (((SPEventContextClass *) pen_parent_class)->finish) {
268         ((SPEventContextClass *) pen_parent_class)->finish(ec);
269     }
272 /**
273  * Callback that sets key to value in pen context.
274  */
275 static void
276 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
278     SPPenContext *pc = SP_PEN_CONTEXT(ec);
280     if (!strcmp(key, "mode")) {
281         if ( val && !strcmp(val, "drag") ) {
282             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
283         } else {
284             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
285         }
286     }
289 /**
290  * Snaps new node relative to the previous node.
291  */
292 static void
293 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
295     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
296         if (pc->npoints > 0) {
297             spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
298         }
299     } else {
300         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
301                                          //After all, the user explicitely asked for angular snapping by
302                                          //pressing CTRL
303             spdc_endpoint_snap_free(pc, p, state);
304         }
305     }
306     if (pc->polylines_paraxial) {
307         // TODO: must we avoid one of the snaps in the previous case distinction in some situations?
308         pen_set_to_nearest_horiz_vert(pc, p, state);
309     }
312 /**
313  * Snaps new node's handle relative to the new node.
314  */
315 static void
316 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
318     g_return_if_fail(( pc->npoints == 2 ||
319                        pc->npoints == 5   ));
321     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
322         spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
323     } else {
324         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
325             spdc_endpoint_snap_free(pc, p, state);
326         }
327     }
330 static gint 
331 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
333     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
335     gint ret = FALSE;
337     switch (event->type) {
338         case GDK_BUTTON_PRESS:
339             ret = pen_handle_button_press(pc, event->button);
340             break;
341         case GDK_BUTTON_RELEASE:
342             ret = pen_handle_button_release(pc, event->button);
343             break;
344         default:
345             break;
346     }
348     if (!ret) {
349         if (((SPEventContextClass *) pen_parent_class)->item_handler)
350             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
351     }
353     return ret;
356 /**
357  * Callback to handle all pen events.
358  */
359 static gint
360 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
362     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
364     gint ret = FALSE;
366     switch (event->type) {
367         case GDK_BUTTON_PRESS:
368             ret = pen_handle_button_press(pc, event->button);
369             break;
371         case GDK_MOTION_NOTIFY:
372             ret = pen_handle_motion_notify(pc, event->motion);
373             break;
375         case GDK_BUTTON_RELEASE:
376             ret = pen_handle_button_release(pc, event->button);
377             break;
379         case GDK_2BUTTON_PRESS:
380             ret = pen_handle_2button_press(pc, event->button);
381             break;
383         case GDK_KEY_PRESS:
384             ret = pen_handle_key_press(pc, event);
385             break;
387         default:
388             break;
389     }
391     if (!ret) {
392         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
393             = ((SPEventContextClass *) pen_parent_class)->root_handler;
394         if (parent_root_handler) {
395             ret = parent_root_handler(ec, event);
396         }
397     }
399     return ret;
402 /**
403  * Handle mouse button press event.
404  */
405 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
407     if (pc->events_disabled) {
408         // skip event processing if events are disabled
409         return FALSE;
410     }
412     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
413     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
414     NR::Point const event_w(bevent.x, bevent.y);
415     NR::Point const event_dt(desktop->w2d(event_w));
416     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
418     gint ret = FALSE;
419     if (bevent.button == 1 && !event_context->space_panning
420         // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
421         && pc->expecting_clicks_for_LPE != 1) {
423         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
424             return TRUE;
425         }
427         if (!pc->grab ) {
428             /* Grab mouse, so release will not pass unnoticed */
429             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
430             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
431                                             GDK_BUTTON_RELEASE_MASK |
432                                             GDK_POINTER_MOTION_MASK  ),
433                                 NULL, bevent.time);
434         }
436         pen_drag_origin_w = event_w;
437         pen_within_tolerance = true;
439         /* Test whether we hit any anchor. */
440         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
442         switch (pc->mode) {
443             case SP_PEN_CONTEXT_MODE_CLICK:
444                 /* In click mode we add point on release */
445                 switch (pc->state) {
446                     case SP_PEN_CONTEXT_POINT:
447                     case SP_PEN_CONTEXT_CONTROL:
448                     case SP_PEN_CONTEXT_CLOSE:
449                         break;
450                     case SP_PEN_CONTEXT_STOP:
451                         /* This is allowed, if we just cancelled curve */
452                         pc->state = SP_PEN_CONTEXT_POINT;
453                         break;
454                     default:
455                         break;
456                 }
457                 break;
458             case SP_PEN_CONTEXT_MODE_DRAG:
459                 switch (pc->state) {
460                     case SP_PEN_CONTEXT_STOP:
461                         /* This is allowed, if we just cancelled curve */
462                     case SP_PEN_CONTEXT_POINT:
463                         if (pc->npoints == 0) {
465                             if (bevent.state & GDK_CONTROL_MASK) {
466                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
467                                 ret = TRUE;
468                                 break;
469                             }
471                             // TODO: Perhaps it would be nicer to rearrange the following case
472                             // distinction so that the case of a waiting LPE is treated separately
474                             /* Set start anchor */
475                             pc->sa = anchor;
476                             NR::Point p;
477                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
478                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
479                                    a fresh path to be created so don't continue an existing one */
480                                 p = anchor->dp;
481                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
482                             } else {
483                                 // This is the first click of a new curve; deselect item so that
484                                 // this curve is not combined with it (unless it is drawn from its
485                                 // anchor, which is handled by the sibling branch above)
486                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
487                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
488                                     /* if we have a waiting LPE, we need a fresh path to be created
489                                        so don't append to an existing one */
490                                     selection->clear();
491                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
492                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
493                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
494                                 }
496                                 /* Create green anchor */
497                                 p = event_dt;
498                                 if (!pc->polylines_paraxial) {
499                                     // only snap the starting point if we're not in horizontal/vertical mode
500                                     // because otherwise it gets shifted; TODO: why do we snap here at all??
501                                     spdc_endpoint_snap(pc, p, bevent.state);
502                                 }
503                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
504                             }
505                             spdc_pen_set_initial_point(pc, p);
506                         } else {
508                             /* Set end anchor */
509                             pc->ea = anchor;
510                             NR::Point p;
511                             if (anchor) {
512                                 p = anchor->dp;
513                                 // we hit an anchor, will finish the curve (either with or without closing)
514                                 // in release handler
515                                 pc->state = SP_PEN_CONTEXT_CLOSE;
517                                 if (pc->green_anchor && pc->green_anchor->active) {
518                                     // we clicked on the current curve start, so close it even if
519                                     // we drag a handle away from it
520                                     dc->green_closed = TRUE;
521                                 }
522                                 ret = TRUE;
523                                 break;
525                             } else {
526                                 p = event_dt;
527                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
528                                 spdc_pen_set_subsequent_point(pc, p, true);
529                                 if (pc->polylines_only) {
530                                     spdc_pen_finish_segment(pc, p, bevent.state);
531                                 }
532                             }
533                         }
535                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
536                         ret = TRUE;
537                         break;
538                     case SP_PEN_CONTEXT_CONTROL:
539                         g_warning("Button down in CONTROL state");
540                         break;
541                     case SP_PEN_CONTEXT_CLOSE:
542                         g_warning("Button down in CLOSE state");
543                         break;
544                     default:
545                         break;
546                 }
547                 break;
548             default:
549                 break;
550         }
551     } 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
552         if (pc->npoints != 0) {
554             spdc_pen_finish_segment(pc, event_dt, bevent.state);
555             if (pc->green_closed) {
556                 // finishing at the start anchor, close curve
557                 spdc_pen_finish(pc, TRUE);
558             } else {
559                 // finishing at some other anchor, finish curve but not close
560                 spdc_pen_finish(pc, FALSE);
561             }
563             ret = TRUE;
564         }
565     }
567     if (pc->expecting_clicks_for_LPE) {
568         --pc->expecting_clicks_for_LPE;
569     }
571     return ret;
574 /**
575  * Handle motion_notify event.
576  */
577 static gint
578 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
580     gint ret = FALSE;
582     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
583     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
585     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
586         // allow scrolling
587         return FALSE;
588     }
589    
590     if (pc->events_disabled) {
591         // skip motion events if pen events are disabled
592         return FALSE;
593     }
595     NR::Point const event_w(mevent.x,
596                             mevent.y);
597     if (pen_within_tolerance) {
598         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
599                                                                "value", 0, 0, 100);
600         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
601             return FALSE;   // Do not drag if we're within tolerance from origin.
602         }
603     }
604     // Once the user has moved farther than tolerance from the original location
605     // (indicating they intend to move the object, not click), then always process the
606     // motion notify coordinates as given (no snapping back to origin)
607     pen_within_tolerance = false;
609     /* Find desktop coordinates */
610     NR::Point p = dt->w2d(event_w);
612     /* Test, whether we hit any anchor */
613     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
615     switch (pc->mode) {
616         case SP_PEN_CONTEXT_MODE_CLICK:
617             switch (pc->state) {
618                 case SP_PEN_CONTEXT_POINT:
619                     if ( pc->npoints != 0 ) {
620                         /* Only set point, if we are already appending */
621                         spdc_endpoint_snap(pc, p, mevent.state);
622                         spdc_pen_set_subsequent_point(pc, p, true);
623                         ret = TRUE;
624                     }
625                     break;
626                 case SP_PEN_CONTEXT_CONTROL:
627                 case SP_PEN_CONTEXT_CLOSE:
628                     /* Placing controls is last operation in CLOSE state */
629                     spdc_endpoint_snap(pc, p, mevent.state);
630                     spdc_pen_set_ctrl(pc, p, mevent.state);
631                     ret = TRUE;
632                     break;
633                 case SP_PEN_CONTEXT_STOP:
634                     /* This is perfectly valid */
635                     break;
636                 default:
637                     break;
638             }
639             break;
640         case SP_PEN_CONTEXT_MODE_DRAG:
641             switch (pc->state) {
642                 case SP_PEN_CONTEXT_POINT:
643                     if ( pc->npoints > 0 ) {
644                         /* Only set point, if we are already appending */
646                         if (!anchor) {   /* Snap node only if not hitting anchor */
647                             spdc_endpoint_snap(pc, p, mevent.state);
648                         }
650                         spdc_pen_set_subsequent_point(pc, p, !anchor, mevent.state);
652                         if (anchor && !pc->anchor_statusbar) {
653                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
654                             pc->anchor_statusbar = true;
655                         } else if (!anchor && pc->anchor_statusbar) {
656                             pc->_message_context->clear();
657                             pc->anchor_statusbar = false;
658                         }
660                         ret = TRUE;
661                     } else {
662                         if (anchor && !pc->anchor_statusbar) {
663                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
664                             pc->anchor_statusbar = true;
665                         } else if (!anchor && pc->anchor_statusbar) {
666                             pc->_message_context->clear();
667                             pc->anchor_statusbar = false;
668                         }
669                     }
670                     break;
671                 case SP_PEN_CONTEXT_CONTROL:
672                 case SP_PEN_CONTEXT_CLOSE:
673                     /* Placing controls is last operation in CLOSE state */
675                     // snap the handle
676                     spdc_endpoint_snap_handle(pc, p, mevent.state);
678                     if (!pc->polylines_only) {
679                         spdc_pen_set_ctrl(pc, p, mevent.state);
680                     } else {
681                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
682                     }
683                     gobble_motion_events(GDK_BUTTON1_MASK);
684                     ret = TRUE;
685                     break;
686                 case SP_PEN_CONTEXT_STOP:
687                     /* This is perfectly valid */
688                     break;
689                 default:
690                     break;
691             }
692             break;
693         default:
694             break;
695     }
696     return ret;
699 /**
700  * Handle mouse button release event.
701  */
702 static gint
703 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
705     if (pc->events_disabled) {
706         // skip event processing if events are disabled
707         return FALSE;
708     }
710     gint ret = FALSE;
711     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
712     if ( revent.button == 1  && !event_context->space_panning) {
714         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
716         NR::Point const event_w(revent.x,
717                                 revent.y);
718         /* Find desktop coordinates */
719         NR::Point p = pc->desktop->w2d(event_w);
721         /* Test whether we hit any anchor. */
722         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
724         switch (pc->mode) {
725             case SP_PEN_CONTEXT_MODE_CLICK:
726                 switch (pc->state) {
727                     case SP_PEN_CONTEXT_POINT:
728                         if ( pc->npoints == 0 ) {
729                             /* Start new thread only with button release */
730                             if (anchor) {
731                                 p = anchor->dp;
732                             }
733                             pc->sa = anchor;
734                             spdc_pen_set_initial_point(pc, p);
735                         } else {
736                             /* Set end anchor here */
737                             pc->ea = anchor;
738                             if (anchor) {
739                                 p = anchor->dp;
740                             }
741                         }
742                         pc->state = SP_PEN_CONTEXT_CONTROL;
743                         ret = TRUE;
744                         break;
745                     case SP_PEN_CONTEXT_CONTROL:
746                         /* End current segment */
747                         spdc_endpoint_snap(pc, p, revent.state);
748                         spdc_pen_finish_segment(pc, p, revent.state);
749                         pc->state = SP_PEN_CONTEXT_POINT;
750                         ret = TRUE;
751                         break;
752                     case SP_PEN_CONTEXT_CLOSE:
753                         /* End current segment */
754                         if (!anchor) {   /* Snap node only if not hitting anchor */
755                             spdc_endpoint_snap(pc, p, revent.state);
756                         }
757                         spdc_pen_finish_segment(pc, p, revent.state);
758                         spdc_pen_finish(pc, TRUE);
759                         pc->state = SP_PEN_CONTEXT_POINT;
760                         ret = TRUE;
761                         break;
762                     case SP_PEN_CONTEXT_STOP:
763                         /* This is allowed, if we just cancelled curve */
764                         pc->state = SP_PEN_CONTEXT_POINT;
765                         ret = TRUE;
766                         break;
767                     default:
768                         break;
769                 }
770                 break;
771             case SP_PEN_CONTEXT_MODE_DRAG:
772                 switch (pc->state) {
773                     case SP_PEN_CONTEXT_POINT:
774                     case SP_PEN_CONTEXT_CONTROL:
775                         if (!pc->polylines_only) {
776                             spdc_endpoint_snap(pc, p, revent.state);
777                             spdc_pen_finish_segment(pc, p, revent.state);
778                         }
779                         break;
780                     case SP_PEN_CONTEXT_CLOSE:
781                         spdc_endpoint_snap(pc, p, revent.state);
782                         spdc_pen_finish_segment(pc, p, revent.state);
783                         if (pc->green_closed) {
784                             // finishing at the start anchor, close curve
785                             spdc_pen_finish(pc, TRUE);
786                         } else {
787                             // finishing at some other anchor, finish curve but not close
788                             spdc_pen_finish(pc, FALSE);
789                         }
790                         break;
791                     case SP_PEN_CONTEXT_STOP:
792                         /* This is allowed, if we just cancelled curve */
793                         break;
794                     default:
795                         break;
796                 }
797                 pc->state = SP_PEN_CONTEXT_POINT;
798                 ret = TRUE;
799                 break;
800             default:
801                 break;
802         }
804         if (pc->grab) {
805             /* Release grab now */
806             sp_canvas_item_ungrab(pc->grab, revent.time);
807             pc->grab = NULL;
808         }
810         ret = TRUE;
812         dc->green_closed = FALSE;
813     }
815     // TODO: can we be sure that the path was created correctly?
816     // TODO: should we offer an option to collect the clicks in a list?
817     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
818         sp_pen_context_set_polyline_mode(pc);
820         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
821         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
823         if (pc->waiting_LPE) {
824             // we have an already created LPE waiting for a path
825             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
826             selection->add(SP_OBJECT(pc->waiting_item));
827             pc->waiting_LPE = NULL;
828         } else {
829             // the case that we need to create a new LPE and apply it to the just-drawn path is
830             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
831         }
832     }
834     return ret;
837 static gint
838 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
840     gint ret = FALSE;
841     if (pc->npoints != 0 && bevent.button != 2) {
842         spdc_pen_finish(pc, FALSE);
843         ret = TRUE;
844     }
845     return ret;
848 void
849 pen_redraw_all (SPPenContext *const pc)
851     // green
852     if (pc->green_bpaths) {
853         // remove old piecewise green canvasitems
854         while (pc->green_bpaths) {
855             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
856             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
857         }
858         // one canvas bpath for all of green_curve
859         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
860         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
861         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
863         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
864     }
866     if (pc->green_anchor)
867         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
869     pc->red_curve->reset();
870     pc->red_curve->moveto(pc->p[0]);
871     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
872     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
874     // handles
875     if (pc->p[0] != pc->p[1]) {
876         SP_CTRL(pc->c1)->moveto(pc->p[1]);
877         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
878         sp_canvas_item_show (pc->c1);
879         sp_canvas_item_show (pc->cl1);
880     } else {
881         sp_canvas_item_hide (pc->c1);
882         sp_canvas_item_hide (pc->cl1);
883     }
885     Geom::Curve const * last_seg = pc->green_curve->last_segment();
886     if (last_seg) {
887         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
888         if ( cubic &&
889              (*cubic)[2] != to_2geom(pc->p[0]) )
890         {
891             NR::Point p2 = from_2geom((*cubic)[2]);
892             SP_CTRL(pc->c0)->moveto(p2);
893             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
894             sp_canvas_item_show (pc->c0);
895             sp_canvas_item_show (pc->cl0);
896         } else {
897             sp_canvas_item_hide (pc->c0);
898             sp_canvas_item_hide (pc->cl0);
899         }
900     }
903 void
904 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
906     if (pc->npoints != 5)
907         return;
909     // green
910     if (!pc->green_curve->is_empty()) {
911         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
912     } else {
913         // start anchor too
914         if (pc->green_anchor) {
915             pc->green_anchor->dp += NR::Point(x, y);
916         }
917     }
919     // red
920     pc->p[0] += NR::Point(x, y);
921     pc->p[1] += NR::Point(x, y);
922     pen_redraw_all(pc);
925 void
926 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
928     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
931 void
932 pen_lastpoint_tocurve (SPPenContext *const pc)
934     if (pc->npoints != 5)
935         return;
937     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
938     if ( cubic ) {
939         pc->p[1] = pc->p[0] + from_2geom( (*cubic)[3] - (*cubic)[2] );
940     } else {
941         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
942     }
944     pen_redraw_all(pc);
947 void
948 pen_lastpoint_toline (SPPenContext *const pc)
950     if (pc->npoints != 5)
951         return;
953     pc->p[1] = pc->p[0];
955     pen_redraw_all(pc);
959 static gint
960 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
962     gint ret = FALSE;
963     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
965     switch (get_group0_keyval (&event->key)) {
967         case GDK_Left: // move last point left
968         case GDK_KP_Left:
969         case GDK_KP_4:
970             if (!MOD__CTRL) { // not ctrl
971                 if (MOD__ALT) { // alt
972                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
973                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
974                 }
975                 else { // no alt
976                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
977                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
978                 }
979                 ret = TRUE;
980             }
981             break;
982         case GDK_Up: // move last point up
983         case GDK_KP_Up:
984         case GDK_KP_8:
985             if (!MOD__CTRL) { // not ctrl
986                 if (MOD__ALT) { // alt
987                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
988                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
989                 }
990                 else { // no alt
991                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
992                     else pen_lastpoint_move(pc, 0, nudge); // no shift
993                 }
994                 ret = TRUE;
995             }
996             break;
997         case GDK_Right: // move last point right
998         case GDK_KP_Right:
999         case GDK_KP_6:
1000             if (!MOD__CTRL) { // not ctrl
1001                 if (MOD__ALT) { // alt
1002                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1003                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1004                 }
1005                 else { // no alt
1006                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1007                     else pen_lastpoint_move(pc, nudge, 0); // no shift
1008                 }
1009                 ret = TRUE;
1010             }
1011             break;
1012         case GDK_Down: // move last point down
1013         case GDK_KP_Down:
1014         case GDK_KP_2:
1015             if (!MOD__CTRL) { // not ctrl
1016                 if (MOD__ALT) { // alt
1017                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1018                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1019                 }
1020                 else { // no alt
1021                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1022                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
1023                 }
1024                 ret = TRUE;
1025             }
1026             break;
1028         case GDK_P:
1029         case GDK_p:
1030             if (MOD__SHIFT_ONLY) {
1031                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1032                 ret = TRUE;
1033             }
1034             break;
1036         case GDK_C:
1037         case GDK_c:
1038             if (MOD__SHIFT_ONLY) {
1039                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1040                 ret = TRUE;
1041             }
1042             break;
1044         case GDK_B:
1045         case GDK_b:
1046             if (MOD__SHIFT_ONLY) {
1047                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1048                 ret = TRUE;
1049             }
1050             break;
1052         case GDK_A:
1053         case GDK_a:
1054             if (MOD__SHIFT_ONLY) {
1055                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1056                 ret = TRUE;
1057             }
1058             break;
1060         case GDK_U:
1061         case GDK_u:
1062             if (MOD__SHIFT_ONLY) {
1063                 pen_lastpoint_tocurve(pc);
1064                 ret = TRUE;
1065             }
1066             break;
1067         case GDK_L:
1068         case GDK_l:
1069             if (MOD__SHIFT_ONLY) {
1070                 pen_lastpoint_toline(pc);
1071                 ret = TRUE;
1072             }
1073             break;
1075         case GDK_Return:
1076         case GDK_KP_Enter:
1077             if (pc->npoints != 0) {
1078                 spdc_pen_finish(pc, FALSE);
1079                 ret = TRUE;
1080             }
1081             break;
1082         case GDK_Escape:
1083             if (pc->npoints != 0) {
1084                 // if drawing, cancel, otherwise pass it up for deselecting
1085                 pen_cancel (pc);
1086                 ret = TRUE;
1087             }
1088             break;
1089         case GDK_z:
1090         case GDK_Z:
1091             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1092                 // if drawing, cancel, otherwise pass it up for undo
1093                 pen_cancel (pc);
1094                 ret = TRUE;
1095             }
1096             break;
1097         case GDK_g:
1098         case GDK_G:
1099             if (MOD__SHIFT_ONLY) {
1100                 sp_selection_to_guides();
1101                 ret = true;
1102             }
1103             break;
1104         case GDK_BackSpace:
1105         case GDK_Delete:
1106         case GDK_KP_Delete:
1107             if (pc->green_curve->is_empty()) {
1108                 if (!pc->red_curve->is_empty()) {
1109                     pen_cancel (pc);
1110                     ret = TRUE;
1111                 } else {
1112                     // do nothing; this event should be handled upstream
1113                 }
1114             } else {
1115                 /* Reset red curve */
1116                 pc->red_curve->reset();
1117                 /* Destroy topmost green bpath */
1118                 if (pc->green_bpaths) {
1119                     if (pc->green_bpaths->data)
1120                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1121                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1122                 }
1123                 /* Get last segment */
1124                 if ( pc->green_curve->is_empty() ) {
1125                     g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1126                     break;
1127                 }
1128                 // The code below assumes that pc->green_curve has only ONE path !
1129                 Geom::Path const & path = pc->green_curve->get_pathvector().back();
1130                 Geom::Curve const * crv = &path.back_default();
1131                 pc->p[0] = crv->initialPoint();
1132                 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1133                     pc->p[1] = from_2geom( (*cubic)[1] );
1134                 } else {
1135                     pc->p[1] = pc->p[0];
1136                 }
1137                 NR::Point const pt(( pc->npoints < 4
1138                                      ? from_2geom(crv->finalPoint())
1139                                      : pc->p[3] ));
1140                 pc->npoints = 2;
1141                 pc->green_curve->backspace();
1142                 sp_canvas_item_hide(pc->c0);
1143                 sp_canvas_item_hide(pc->c1);
1144                 sp_canvas_item_hide(pc->cl0);
1145                 sp_canvas_item_hide(pc->cl1);
1146                 pc->state = SP_PEN_CONTEXT_POINT;
1147                 spdc_pen_set_subsequent_point(pc, pt, true);
1148                 ret = TRUE;
1149             }
1150             break;
1151         default:
1152             break;
1153     }
1154     return ret;
1157 static void
1158 spdc_reset_colors(SPPenContext *pc)
1160     /* Red */
1161     pc->red_curve->reset();
1162     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1163     /* Blue */
1164     pc->blue_curve->reset();
1165     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1166     /* Green */
1167     while (pc->green_bpaths) {
1168         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1169         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1170     }
1171     pc->green_curve->reset();
1172     if (pc->green_anchor) {
1173         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1174     }
1175     pc->sa = NULL;
1176     pc->ea = NULL;
1177     pc->npoints = 0;
1178     pc->red_curve_is_valid = false;
1182 static void
1183 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1185     g_assert( pc->npoints == 0 );
1187     pc->p[0] = p;
1188     pc->p[1] = p;
1189     pc->npoints = 2;
1190     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1192     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1195 /**
1196  * Show the status message for the current line/curve segment.
1197  * This type of message always shows angle/distance as the last
1198  * two parameters ("angle %3.2f&#176;, distance %s").
1199  */ 
1200 static void
1201 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1203     g_assert(pc != NULL);
1204     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1205     g_assert(message != NULL);
1207     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1208     NR::Point rel = p - pc->p[pc_point_to_compare];
1209     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1210     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1211     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1212         angle = angle_to_compass (angle);
1214     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1215     g_string_free(dist, FALSE);
1218 static void
1219 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status)
1221     g_assert( pc->npoints != 0 );
1222     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1224     pc->p[2] = p;
1225     pc->p[3] = p;
1226     pc->p[4] = p;
1227     pc->npoints = 5;
1228     pc->red_curve->reset();
1229     bool is_curve;
1230     pc->red_curve->moveto(pc->p[0]);
1231     if (pc->polylines_paraxial && !statusbar) {
1232         // we are drawing horizontal/vertical lines and hit an anchor; draw an L-shaped path
1233         NR::Point intermed = p;
1234         pen_set_to_nearest_horiz_vert(pc, intermed, status);
1235         pc->red_curve->lineto(intermed);
1236         pc->red_curve->lineto(p);
1237         is_curve = false;
1238     } else {
1239         // one of the 'regular' modes
1240         if (pc->p[1] != pc->p[0])
1241         {
1242             pc->red_curve->curveto(pc->p[1], p, p);
1243             is_curve = true;
1244         } else {
1245             pc->red_curve->lineto(p);
1246             is_curve = false;
1247         }
1248     }
1250     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1252     if (statusbar) {
1253         gchar *message = is_curve ?
1254             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1255             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1256         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1257     }
1260 static void
1261 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1263     sp_canvas_item_show(pc->c1);
1264     sp_canvas_item_show(pc->cl1);
1266     if ( pc->npoints == 2 ) {
1267         pc->p[1] = p;
1268         sp_canvas_item_hide(pc->c0);
1269         sp_canvas_item_hide(pc->cl0);
1270         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1271         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1273         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"));
1274     } else if ( pc->npoints == 5 ) {
1275         pc->p[4] = p;
1276         sp_canvas_item_show(pc->c0);
1277         sp_canvas_item_show(pc->cl0);
1278         bool is_symm = false;
1279         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1280              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1281             NR::Point delta = p - pc->p[3];
1282             pc->p[2] = pc->p[3] - delta;
1283             is_symm = true;
1284             pc->red_curve->reset();
1285             pc->red_curve->moveto(pc->p[0]);
1286             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1287             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1288         }
1289         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1290         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1291         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1292         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1294         gchar *message = is_symm ?
1295             _("<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") :
1296             _("<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");
1297         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1298     } else {
1299         g_warning("Something bad happened - npoints is %d", pc->npoints);
1300     }
1303 static void
1304 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1306     if (pc->polylines_paraxial) {
1307         pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1308     }
1309     ++pc->num_clicks;
1311     if (!pc->red_curve->is_empty()) {
1312         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1313         SPCurve *curve = pc->red_curve->copy();
1314         /// \todo fixme:
1315         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1316         curve->unref();
1317         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1319         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1321         pc->p[0] = pc->p[3];
1322         pc->p[1] = pc->p[4];
1323         pc->npoints = 2;
1325         pc->red_curve->reset();
1326     }
1329 static void
1330 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1332     if (pc->expecting_clicks_for_LPE > 1) {
1333         // don't let the path be finished before we have collected the required number of mouse clicks
1334         return;
1335     }
1337     pc->num_clicks = 0;
1339     pen_disable_events(pc);
1340     
1341     SPDesktop *const desktop = pc->desktop;
1342     pc->_message_context->clear();
1343     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1345     pc->red_curve->reset();
1346     spdc_concat_colors_and_flush(pc, closed);
1347     pc->sa = NULL;
1348     pc->ea = NULL;
1350     pc->npoints = 0;
1351     pc->state = SP_PEN_CONTEXT_POINT;
1353     sp_canvas_item_hide(pc->c0);
1354     sp_canvas_item_hide(pc->c1);
1355     sp_canvas_item_hide(pc->cl0);
1356     sp_canvas_item_hide(pc->cl1);
1358     if (pc->green_anchor) {
1359         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1360     }
1363     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1365     pen_enable_events(pc);
1368 static void
1369 pen_disable_events(SPPenContext *const pc) {
1370   pc->events_disabled++;
1373 static void
1374 pen_enable_events(SPPenContext *const pc) {
1375   g_return_if_fail(pc->events_disabled != 0);
1376   
1377   pc->events_disabled--;
1380 void
1381 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1382                                          unsigned int num_clicks, bool use_polylines)
1384     g_print ("Now waiting for %s to be applied\n",
1385              Inkscape::LivePathEffect::LPETypeConverter.get_label(effect_type).c_str());
1386     pc->expecting_clicks_for_LPE = num_clicks;
1387     pc->polylines_only = use_polylines;
1388     pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
1389     pc->waiting_LPE_type = effect_type;
1392 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1393                                        NR::Point const &pt, NR::Point const &origin, guint state) {
1394     /*
1395      * after the first mouse click we determine whether the mouse pointer is closest to a
1396      * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1397      * orthogonal to the last one; pressing Shift toggles the direction
1398      */
1399     if (pc->num_clicks == 0) {
1400         // first mouse click
1401         double dist_h = fabs(pt[NR::X] - origin[NR::X]);
1402         double dist_v = fabs(pt[NR::Y] - origin[NR::Y]);
1403         int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1404         pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1405         return pen_last_paraxial_dir;
1406     } else {
1407         // subsequent mouse click
1408         return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1409     }
1412 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state)
1414     NR::Point const &origin = pc->p[0];
1416     int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1418     if (next_dir == 0) {
1419         // line is forced to be horizontal
1420         pt[NR::Y] = origin[NR::Y];
1421     } else {
1422         // line is forced to be vertical
1423         pt[NR::X] = origin[NR::X];
1424     }
1427 NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt)
1429     NR::Point const &origin = pc->p[0];
1431     if (pen_last_paraxial_dir == 0) {
1432         return NR::Point (origin[NR::X], pt[NR::Y]);
1433     } else {
1434         return NR::Point (pt[NR::X], origin[NR::Y]);
1435     }
1438 /*
1439   Local Variables:
1440   mode:c++
1441   c-file-style:"stroustrup"
1442   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1443   indent-tabs-mode:nil
1444   fill-column:99
1445   End:
1446 */
1447 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :