Code

remove many needless references to n-art-bpath.h
[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 *pc, NR::Point const p, bool statusbar);
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 /**
78  * Register SPPenContext with Gdk and return its type.
79  */
80 GType
81 sp_pen_context_get_type(void)
82 {
83     static GType type = 0;
84     if (!type) {
85         GTypeInfo info = {
86             sizeof(SPPenContextClass),
87             NULL, NULL,
88             (GClassInitFunc) sp_pen_context_class_init,
89             NULL, NULL,
90             sizeof(SPPenContext),
91             4,
92             (GInstanceInitFunc) sp_pen_context_init,
93             NULL,   /* value_table */
94         };
95         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
96     }
97     return type;
98 }
100 /**
101  * Initialize the SPPenContext vtable.
102  */
103 static void
104 sp_pen_context_class_init(SPPenContextClass *klass)
106     GObjectClass *object_class;
107     SPEventContextClass *event_context_class;
109     object_class = (GObjectClass *) klass;
110     event_context_class = (SPEventContextClass *) klass;
112     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
114     object_class->dispose = sp_pen_context_dispose;
116     event_context_class->setup = sp_pen_context_setup;
117     event_context_class->finish = sp_pen_context_finish;
118     event_context_class->set = sp_pen_context_set;
119     event_context_class->root_handler = sp_pen_context_root_handler;
120     event_context_class->item_handler = sp_pen_context_item_handler;
123 /**
124  * Callback to initialize SPPenContext object.
125  */
126 static void
127 sp_pen_context_init(SPPenContext *pc)
130     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
132     event_context->cursor_shape = cursor_pen_xpm;
133     event_context->hot_x = 4;
134     event_context->hot_y = 4;
136     pc->npoints = 0;
137     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
138     pc->state = SP_PEN_CONTEXT_POINT;
140     pc->c0 = NULL;
141     pc->c1 = NULL;
142     pc->cl0 = NULL;
143     pc->cl1 = NULL;
144     
145     pc->events_disabled = 0;
147     pc->polylines_only = prefs_get_int_attribute("tools.freehand.pen", "freehand-mode", 0);
148     pc->waiting_LPE = NULL;
151 /**
152  * Callback to destroy the SPPenContext object's members and itself.
153  */
154 static void
155 sp_pen_context_dispose(GObject *object)
157     SPPenContext *pc;
159     pc = SP_PEN_CONTEXT(object);
161     if (pc->c0) {
162         gtk_object_destroy(GTK_OBJECT(pc->c0));
163         pc->c0 = NULL;
164     }
165     if (pc->c1) {
166         gtk_object_destroy(GTK_OBJECT(pc->c1));
167         pc->c1 = NULL;
168     }
169     if (pc->cl0) {
170         gtk_object_destroy(GTK_OBJECT(pc->cl0));
171         pc->cl0 = NULL;
172     }
173     if (pc->cl1) {
174         gtk_object_destroy(GTK_OBJECT(pc->cl1));
175         pc->cl1 = NULL;
176     }
178     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
180     if (pc->expecting_clicks_for_LPE > 0) {
181         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
182         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
183     }
186 /**
187  * Callback to initialize SPPenContext object.
188  */
189 static void
190 sp_pen_context_setup(SPEventContext *ec)
192     SPPenContext *pc;
194     pc = SP_PEN_CONTEXT(ec);
196     if (((SPEventContextClass *) pen_parent_class)->setup) {
197         ((SPEventContextClass *) pen_parent_class)->setup(ec);
198     }
200     /* Pen indicators */
201     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
202                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
203     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
204                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
205     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
206     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
207     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
208     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
210     sp_canvas_item_hide(pc->c0);
211     sp_canvas_item_hide(pc->c1);
212     sp_canvas_item_hide(pc->cl0);
213     sp_canvas_item_hide(pc->cl1);
215     sp_event_context_read(ec, "mode");
217     pc->anchor_statusbar = false;
219     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
220         ec->enableSelectionCue();
221     }
224 static void
225 pen_cancel (SPPenContext *const pc) 
227     pc->state = SP_PEN_CONTEXT_STOP;
228     spdc_reset_colors(pc);
229     sp_canvas_item_hide(pc->c0);
230     sp_canvas_item_hide(pc->c1);
231     sp_canvas_item_hide(pc->cl0);
232     sp_canvas_item_hide(pc->cl1);
233     pc->_message_context->clear();
234     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
236     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
239 /**
240  * Finalization callback.
241  */
242 static void
243 sp_pen_context_finish(SPEventContext *ec)
245     SPPenContext *pc = SP_PEN_CONTEXT(ec);
247     if (pc->npoints != 0) {
248         pen_cancel (pc);
249     }
251     if (((SPEventContextClass *) pen_parent_class)->finish) {
252         ((SPEventContextClass *) pen_parent_class)->finish(ec);
253     }
256 /**
257  * Callback that sets key to value in pen context.
258  */
259 static void
260 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
262     SPPenContext *pc = SP_PEN_CONTEXT(ec);
264     if (!strcmp(key, "mode")) {
265         if ( val && !strcmp(val, "drag") ) {
266             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
267         } else {
268             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
269         }
270     }
273 /**
274  * Snaps new node relative to the previous node.
275  */
276 static void
277 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
279     if (pc->npoints > 0) {
280         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
281     }
283     spdc_endpoint_snap_free(pc, p, state);
286 /**
287  * Snaps new node's handle relative to the new node.
288  */
289 static void
290 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
292     g_return_if_fail(( pc->npoints == 2 ||
293                        pc->npoints == 5   ));
295     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
296     spdc_endpoint_snap_free(pc, p, state);
299 static gint 
300 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
302     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
304     gint ret = FALSE;
306     switch (event->type) {
307         case GDK_BUTTON_PRESS:
308             ret = pen_handle_button_press(pc, event->button);
309             break;
310         case GDK_BUTTON_RELEASE:
311             ret = pen_handle_button_release(pc, event->button);
312             break;
313         default:
314             break;
315     }
317     if (!ret) {
318         if (((SPEventContextClass *) pen_parent_class)->item_handler)
319             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
320     }
322     return ret;
325 /**
326  * Callback to handle all pen events.
327  */
328 static gint
329 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
331     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
333     gint ret = FALSE;
335     switch (event->type) {
336         case GDK_BUTTON_PRESS:
337             ret = pen_handle_button_press(pc, event->button);
338             break;
340         case GDK_MOTION_NOTIFY:
341             ret = pen_handle_motion_notify(pc, event->motion);
342             break;
344         case GDK_BUTTON_RELEASE:
345             ret = pen_handle_button_release(pc, event->button);
346             break;
348         case GDK_2BUTTON_PRESS:
349             ret = pen_handle_2button_press(pc, event->button);
350             break;
352         case GDK_KEY_PRESS:
353             ret = pen_handle_key_press(pc, event);
354             break;
356         default:
357             break;
358     }
360     if (!ret) {
361         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
362             = ((SPEventContextClass *) pen_parent_class)->root_handler;
363         if (parent_root_handler) {
364             ret = parent_root_handler(ec, event);
365         }
366     }
368     return ret;
371 /**
372  * Handle mouse button press event.
373  */
374 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
376     if (pc->events_disabled) {
377         // skip event processing if events are disabled
378         return FALSE;
379     }
381     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
382     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
383     NR::Point const event_w(bevent.x, bevent.y);
384     NR::Point const event_dt(desktop->w2d(event_w));
385     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
387     gint ret = FALSE;
388     if (bevent.button == 1 && !event_context->space_panning
389         // when the last click for a waiting LPE occurs we want to finish the path
390         && pc->expecting_clicks_for_LPE != 1) {
392         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
393             return TRUE;
394         }
396         if (!pc->grab ) {
397             /* Grab mouse, so release will not pass unnoticed */
398             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
399             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
400                                             GDK_BUTTON_RELEASE_MASK |
401                                             GDK_POINTER_MOTION_MASK  ),
402                                 NULL, bevent.time);
403         }
405         pen_drag_origin_w = event_w;
406         pen_within_tolerance = true;
408         /* Test whether we hit any anchor. */
409         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
411         switch (pc->mode) {
412             case SP_PEN_CONTEXT_MODE_CLICK:
413                 /* In click mode we add point on release */
414                 switch (pc->state) {
415                     case SP_PEN_CONTEXT_POINT:
416                     case SP_PEN_CONTEXT_CONTROL:
417                     case SP_PEN_CONTEXT_CLOSE:
418                         break;
419                     case SP_PEN_CONTEXT_STOP:
420                         /* This is allowed, if we just cancelled curve */
421                         pc->state = SP_PEN_CONTEXT_POINT;
422                         break;
423                     default:
424                         break;
425                 }
426                 break;
427             case SP_PEN_CONTEXT_MODE_DRAG:
428                 switch (pc->state) {
429                     case SP_PEN_CONTEXT_STOP:
430                         /* This is allowed, if we just cancelled curve */
431                     case SP_PEN_CONTEXT_POINT:
432                         if (pc->npoints == 0) {
434                             if (bevent.state & GDK_CONTROL_MASK) {
435                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
436                                 ret = TRUE;
437                                 break;
438                             }
440                             // TODO: Perhaps it would be nicer to rearrange the following case
441                             // distinction so that the case of a waiting LPE is treated separately
443                             /* Set start anchor */
444                             pc->sa = anchor;
445                             NR::Point p;
446                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
447                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
448                                    a fresh path to be created so don't continue an existing one */
449                                 p = anchor->dp;
450                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
451                             } else {
452                                 // This is the first click of a new curve; deselect item so that
453                                 // this curve is not combined with it (unless it is drawn from its
454                                 // anchor, which is handled by the sibling branch above)
455                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
456                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
457                                     /* if we have a waiting LPE, we need a fresh path to be created
458                                        so don't append to an existing one */
459                                     selection->clear();
460                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
461                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
462                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
463                                 }
465                                 /* Create green anchor */
466                                 p = event_dt;
467                                 spdc_endpoint_snap(pc, p, bevent.state);
468                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
469                             }
470                             spdc_pen_set_initial_point(pc, p);
471                         } else {
473                             /* Set end anchor */
474                             pc->ea = anchor;
475                             NR::Point p;
476                             if (anchor) {
477                                 p = anchor->dp;
478                                 // we hit an anchor, will finish the curve (either with or without closing)
479                                 // in release handler
480                                 pc->state = SP_PEN_CONTEXT_CLOSE;
482                                 if (pc->green_anchor && pc->green_anchor->active) {
483                                     // we clicked on the current curve start, so close it even if
484                                     // we drag a handle away from it
485                                     dc->green_closed = TRUE;
486                                 }
487                                 ret = TRUE;
488                                 break;
490                             } else {
491                                 p = event_dt;
492                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
493                                 spdc_pen_set_subsequent_point(pc, p, true);
494                                 if (pc->polylines_only) {
495                                     spdc_pen_finish_segment(pc, p, bevent.state);
496                                 }
497                             }
498                         }
500                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
501                         ret = TRUE;
502                         break;
503                     case SP_PEN_CONTEXT_CONTROL:
504                         g_warning("Button down in CONTROL state");
505                         break;
506                     case SP_PEN_CONTEXT_CLOSE:
507                         g_warning("Button down in CLOSE state");
508                         break;
509                     default:
510                         break;
511                 }
512                 break;
513             default:
514                 break;
515         }
516     } 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
517         if (pc->npoints != 0) {
519             spdc_pen_finish_segment(pc, event_dt, bevent.state);
520             if (pc->green_closed) {
521                 // finishing at the start anchor, close curve
522                 spdc_pen_finish(pc, TRUE);
523             } else {
524                 // finishing at some other anchor, finish curve but not close
525                 spdc_pen_finish(pc, FALSE);
526             }
528             ret = TRUE;
529         }
530     }
532     if (pc->expecting_clicks_for_LPE) {
533         --pc->expecting_clicks_for_LPE;
534     }
536     return ret;
539 /**
540  * Handle motion_notify event.
541  */
542 static gint
543 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
545     gint ret = FALSE;
547     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
548     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
550     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
551         // allow scrolling
552         return FALSE;
553     }
554    
555     if (pc->events_disabled) {
556         // skip motion events if pen events are disabled
557         return FALSE;
558     }
560     NR::Point const event_w(mevent.x,
561                             mevent.y);
562     if (pen_within_tolerance) {
563         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
564                                                                "value", 0, 0, 100);
565         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
566             return FALSE;   // Do not drag if we're within tolerance from origin.
567         }
568     }
569     // Once the user has moved farther than tolerance from the original location
570     // (indicating they intend to move the object, not click), then always process the
571     // motion notify coordinates as given (no snapping back to origin)
572     pen_within_tolerance = false;
574     /* Find desktop coordinates */
575     NR::Point p = dt->w2d(event_w);
577     /* Test, whether we hit any anchor */
578     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
580     switch (pc->mode) {
581         case SP_PEN_CONTEXT_MODE_CLICK:
582             switch (pc->state) {
583                 case SP_PEN_CONTEXT_POINT:
584                     if ( pc->npoints != 0 ) {
585                         /* Only set point, if we are already appending */
586                         spdc_endpoint_snap(pc, p, mevent.state);
587                         spdc_pen_set_subsequent_point(pc, p, true);
588                         ret = TRUE;
589                     }
590                     break;
591                 case SP_PEN_CONTEXT_CONTROL:
592                 case SP_PEN_CONTEXT_CLOSE:
593                     /* Placing controls is last operation in CLOSE state */
594                     spdc_endpoint_snap(pc, p, mevent.state);
595                     spdc_pen_set_ctrl(pc, p, mevent.state);
596                     ret = TRUE;
597                     break;
598                 case SP_PEN_CONTEXT_STOP:
599                     /* This is perfectly valid */
600                     break;
601                 default:
602                     break;
603             }
604             break;
605         case SP_PEN_CONTEXT_MODE_DRAG:
606             switch (pc->state) {
607                 case SP_PEN_CONTEXT_POINT:
608                     if ( pc->npoints > 0 ) {
609                         /* Only set point, if we are already appending */
611                         if (!anchor) {   /* Snap node only if not hitting anchor */
612                             spdc_endpoint_snap(pc, p, mevent.state);
613                         }
615                         spdc_pen_set_subsequent_point(pc, p, !anchor);
617                         if (anchor && !pc->anchor_statusbar) {
618                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
619                             pc->anchor_statusbar = true;
620                         } else if (!anchor && pc->anchor_statusbar) {
621                             pc->_message_context->clear();
622                             pc->anchor_statusbar = false;
623                         }
625                         ret = TRUE;
626                     } else {
627                         if (anchor && !pc->anchor_statusbar) {
628                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
629                             pc->anchor_statusbar = true;
630                         } else if (!anchor && pc->anchor_statusbar) {
631                             pc->_message_context->clear();
632                             pc->anchor_statusbar = false;
633                         }
634                     }
635                     break;
636                 case SP_PEN_CONTEXT_CONTROL:
637                 case SP_PEN_CONTEXT_CLOSE:
638                     /* Placing controls is last operation in CLOSE state */
640                     // snap the handle
641                     spdc_endpoint_snap_handle(pc, p, mevent.state);
643                     if (!pc->polylines_only) {
644                         spdc_pen_set_ctrl(pc, p, mevent.state);
645                     } else {
646                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
647                     }
648                     gobble_motion_events(GDK_BUTTON1_MASK);
649                     ret = TRUE;
650                     break;
651                 case SP_PEN_CONTEXT_STOP:
652                     /* This is perfectly valid */
653                     break;
654                 default:
655                     break;
656             }
657             break;
658         default:
659             break;
660     }
661     return ret;
664 /**
665  * Handle mouse button release event.
666  */
667 static gint
668 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
670     if (pc->events_disabled) {
671         // skip event processing if events are disabled
672         return FALSE;
673     }
675     gint ret = FALSE;
676     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
677     if ( revent.button == 1  && !event_context->space_panning) {
679         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
681         NR::Point const event_w(revent.x,
682                                 revent.y);
683         /* Find desktop coordinates */
684         NR::Point p = pc->desktop->w2d(event_w);
686         /* Test whether we hit any anchor. */
687         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
689         switch (pc->mode) {
690             case SP_PEN_CONTEXT_MODE_CLICK:
691                 switch (pc->state) {
692                     case SP_PEN_CONTEXT_POINT:
693                         if ( pc->npoints == 0 ) {
694                             /* Start new thread only with button release */
695                             if (anchor) {
696                                 p = anchor->dp;
697                             }
698                             pc->sa = anchor;
699                             spdc_pen_set_initial_point(pc, p);
700                         } else {
701                             /* Set end anchor here */
702                             pc->ea = anchor;
703                             if (anchor) {
704                                 p = anchor->dp;
705                             }
706                         }
707                         pc->state = SP_PEN_CONTEXT_CONTROL;
708                         ret = TRUE;
709                         break;
710                     case SP_PEN_CONTEXT_CONTROL:
711                         /* End current segment */
712                         spdc_endpoint_snap(pc, p, revent.state);
713                         spdc_pen_finish_segment(pc, p, revent.state);
714                         pc->state = SP_PEN_CONTEXT_POINT;
715                         ret = TRUE;
716                         break;
717                     case SP_PEN_CONTEXT_CLOSE:
718                         /* End current segment */
719                         if (!anchor) {   /* Snap node only if not hitting anchor */
720                             spdc_endpoint_snap(pc, p, revent.state);
721                         }
722                         spdc_pen_finish_segment(pc, p, revent.state);
723                         spdc_pen_finish(pc, TRUE);
724                         pc->state = SP_PEN_CONTEXT_POINT;
725                         ret = TRUE;
726                         break;
727                     case SP_PEN_CONTEXT_STOP:
728                         /* This is allowed, if we just cancelled curve */
729                         pc->state = SP_PEN_CONTEXT_POINT;
730                         ret = TRUE;
731                         break;
732                     default:
733                         break;
734                 }
735                 break;
736             case SP_PEN_CONTEXT_MODE_DRAG:
737                 switch (pc->state) {
738                     case SP_PEN_CONTEXT_POINT:
739                     case SP_PEN_CONTEXT_CONTROL:
740                         if (!pc->polylines_only) {
741                             spdc_endpoint_snap(pc, p, revent.state);
742                             spdc_pen_finish_segment(pc, p, revent.state);
743                         }
744                         break;
745                     case SP_PEN_CONTEXT_CLOSE:
746                         spdc_endpoint_snap(pc, p, revent.state);
747                         spdc_pen_finish_segment(pc, p, revent.state);
748                         if (pc->green_closed) {
749                             // finishing at the start anchor, close curve
750                             spdc_pen_finish(pc, TRUE);
751                         } else {
752                             // finishing at some other anchor, finish curve but not close
753                             spdc_pen_finish(pc, FALSE);
754                         }
755                         break;
756                     case SP_PEN_CONTEXT_STOP:
757                         /* This is allowed, if we just cancelled curve */
758                         break;
759                     default:
760                         break;
761                 }
762                 pc->state = SP_PEN_CONTEXT_POINT;
763                 ret = TRUE;
764                 break;
765             default:
766                 break;
767         }
769         if (pc->grab) {
770             /* Release grab now */
771             sp_canvas_item_ungrab(pc->grab, revent.time);
772             pc->grab = NULL;
773         }
775         ret = TRUE;
777         dc->green_closed = FALSE;
778     }
780     // TODO: can we be sure that the path was created correctly?
781     // TODO: should we offer an option to collect the clicks in a list?
782     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
783         pc->polylines_only = prefs_get_int_attribute("tools.freehand.pen", "freehand-mode", 0);
785         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
786         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
788         if (pc->waiting_LPE) {
789             // we have an already created LPE waiting for a path
790             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
791             selection->add(SP_OBJECT(pc->waiting_item));
792             pc->waiting_LPE = NULL;
793             pc->polylines_only = prefs_get_int_attribute("tools.freehand.pen", "freehand-mode", 0);
794         } else {
795             // the case that we need to create a new LPE and apply it to the just-drawn path is
796             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
797         }
798     }
800     return ret;
803 static gint
804 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
806     gint ret = FALSE;
807     if (pc->npoints != 0 && bevent.button != 2) {
808         spdc_pen_finish(pc, FALSE);
809         ret = TRUE;
810     }
811     return ret;
814 void
815 pen_redraw_all (SPPenContext *const pc)
817     // green
818     if (pc->green_bpaths) {
819         // remove old piecewise green canvasitems
820         while (pc->green_bpaths) {
821             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
822             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
823         }
824         // one canvas bpath for all of green_curve
825         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
826         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
827         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
829         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
830     }
832     if (pc->green_anchor)
833         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
835     pc->red_curve->reset();
836     pc->red_curve->moveto(pc->p[0]);
837     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
838     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
840     // handles
841     if (pc->p[0] != pc->p[1]) {
842         SP_CTRL(pc->c1)->moveto(pc->p[1]);
843         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
844         sp_canvas_item_show (pc->c1);
845         sp_canvas_item_show (pc->cl1);
846     } else {
847         sp_canvas_item_hide (pc->c1);
848         sp_canvas_item_hide (pc->cl1);
849     }
851     Geom::Curve const * last_seg = pc->green_curve->last_segment();
852     if (last_seg) {
853         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
854         if ( cubic &&
855              (*cubic)[2] != to_2geom(pc->p[0]) )
856         {
857             NR::Point p2 = from_2geom((*cubic)[2]);
858             SP_CTRL(pc->c0)->moveto(p2);
859             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
860             sp_canvas_item_show (pc->c0);
861             sp_canvas_item_show (pc->cl0);
862         } else {
863             sp_canvas_item_hide (pc->c0);
864             sp_canvas_item_hide (pc->cl0);
865         }
866     }
869 void
870 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
872     if (pc->npoints != 5)
873         return;
875     // green
876     if (!pc->green_curve->is_empty()) {
877         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
878     } else {
879         // start anchor too
880         if (pc->green_anchor) {
881             pc->green_anchor->dp += NR::Point(x, y);
882         }
883     }
885     // red
886     pc->p[0] += NR::Point(x, y);
887     pc->p[1] += NR::Point(x, y);
888     pen_redraw_all(pc);
891 void
892 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
894     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
897 void
898 pen_lastpoint_tocurve (SPPenContext *const pc)
900     if (pc->npoints != 5)
901         return;
903     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
904     if ( cubic ) {
905         pc->p[1] = pc->p[0] + from_2geom( (*cubic)[3] - (*cubic)[2] );
906     } else {
907         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
908     }
910     pen_redraw_all(pc);
913 void
914 pen_lastpoint_toline (SPPenContext *const pc)
916     if (pc->npoints != 5)
917         return;
919     pc->p[1] = pc->p[0];
921     pen_redraw_all(pc);
925 static gint
926 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
928     gint ret = FALSE;
929     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
931     switch (get_group0_keyval (&event->key)) {
933         case GDK_Left: // move last point left
934         case GDK_KP_Left:
935         case GDK_KP_4:
936             if (!MOD__CTRL) { // not ctrl
937                 if (MOD__ALT) { // alt
938                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
939                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
940                 }
941                 else { // no alt
942                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
943                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
944                 }
945                 ret = TRUE;
946             }
947             break;
948         case GDK_Up: // move last point up
949         case GDK_KP_Up:
950         case GDK_KP_8:
951             if (!MOD__CTRL) { // not ctrl
952                 if (MOD__ALT) { // alt
953                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
954                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
955                 }
956                 else { // no alt
957                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
958                     else pen_lastpoint_move(pc, 0, nudge); // no shift
959                 }
960                 ret = TRUE;
961             }
962             break;
963         case GDK_Right: // move last point right
964         case GDK_KP_Right:
965         case GDK_KP_6:
966             if (!MOD__CTRL) { // not ctrl
967                 if (MOD__ALT) { // alt
968                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
969                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
970                 }
971                 else { // no alt
972                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
973                     else pen_lastpoint_move(pc, nudge, 0); // no shift
974                 }
975                 ret = TRUE;
976             }
977             break;
978         case GDK_Down: // move last point down
979         case GDK_KP_Down:
980         case GDK_KP_2:
981             if (!MOD__CTRL) { // not ctrl
982                 if (MOD__ALT) { // alt
983                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
984                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
985                 }
986                 else { // no alt
987                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
988                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
989                 }
990                 ret = TRUE;
991             }
992             break;
994         case GDK_P:
995         case GDK_p:
996             if (MOD__SHIFT_ONLY) {
997                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
998                 ret = TRUE;
999             }
1000             break;
1002         case GDK_C:
1003         case GDK_c:
1004             if (MOD__SHIFT_ONLY) {
1005                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1006                 ret = TRUE;
1007             }
1008             break;
1010         case GDK_B:
1011         case GDK_b:
1012             if (MOD__SHIFT_ONLY) {
1013                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1014                 ret = TRUE;
1015             }
1016             break;
1018         case GDK_A:
1019         case GDK_a:
1020             if (MOD__SHIFT_ONLY) {
1021                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1022                 ret = TRUE;
1023             }
1024             break;
1026         case GDK_U:
1027         case GDK_u:
1028             if (MOD__SHIFT_ONLY) {
1029                 pen_lastpoint_tocurve(pc);
1030                 ret = TRUE;
1031             }
1032             break;
1033         case GDK_L:
1034         case GDK_l:
1035             if (MOD__SHIFT_ONLY) {
1036                 pen_lastpoint_toline(pc);
1037                 ret = TRUE;
1038             }
1039             break;
1041         case GDK_Return:
1042         case GDK_KP_Enter:
1043             if (pc->npoints != 0) {
1044                 spdc_pen_finish(pc, FALSE);
1045                 ret = TRUE;
1046             }
1047             break;
1048         case GDK_Escape:
1049             if (pc->npoints != 0) {
1050                 // if drawing, cancel, otherwise pass it up for deselecting
1051                 pen_cancel (pc);
1052                 ret = TRUE;
1053             }
1054             break;
1055         case GDK_z:
1056         case GDK_Z:
1057             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1058                 // if drawing, cancel, otherwise pass it up for undo
1059                 pen_cancel (pc);
1060                 ret = TRUE;
1061             }
1062             break;
1063         case GDK_g:
1064         case GDK_G:
1065             if (MOD__SHIFT_ONLY) {
1066                 sp_selection_to_guides();
1067                 ret = true;
1068             }
1069             break;
1070         case GDK_BackSpace:
1071         case GDK_Delete:
1072         case GDK_KP_Delete:
1073             if (pc->green_curve->is_empty()) {
1074                 if (!pc->red_curve->is_empty()) {
1075                     pen_cancel (pc);
1076                     ret = TRUE;
1077                 } else {
1078                     // do nothing; this event should be handled upstream
1079                 }
1080             } else {
1081                 /* Reset red curve */
1082                 pc->red_curve->reset();
1083                 /* Destroy topmost green bpath */
1084                 if (pc->green_bpaths) {
1085                     if (pc->green_bpaths->data)
1086                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1087                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1088                 }
1089                 /* Get last segment */
1090                 if ( pc->green_curve->is_empty() ) {
1091                     g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1092                     break;
1093                 }
1094                 // The code below assumes that pc->green_curve has only ONE path !
1095                 Geom::Path const & path = pc->green_curve->get_pathvector().back();
1096                 Geom::Curve const * crv = &path.back_default();
1097                 pc->p[0] = crv->initialPoint();
1098                 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1099                     pc->p[1] = from_2geom( (*cubic)[1] );
1100                 } else {
1101                     pc->p[1] = pc->p[0];
1102                 }
1103                 NR::Point const pt(( pc->npoints < 4
1104                                      ? from_2geom(crv->finalPoint())
1105                                      : pc->p[3] ));
1106                 pc->npoints = 2;
1107                 pc->green_curve->backspace();
1108                 sp_canvas_item_hide(pc->c0);
1109                 sp_canvas_item_hide(pc->c1);
1110                 sp_canvas_item_hide(pc->cl0);
1111                 sp_canvas_item_hide(pc->cl1);
1112                 pc->state = SP_PEN_CONTEXT_POINT;
1113                 spdc_pen_set_subsequent_point(pc, pt, true);
1114                 ret = TRUE;
1115             }
1116             break;
1117         default:
1118             break;
1119     }
1120     return ret;
1123 static void
1124 spdc_reset_colors(SPPenContext *pc)
1126     /* Red */
1127     pc->red_curve->reset();
1128     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1129     /* Blue */
1130     pc->blue_curve->reset();
1131     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1132     /* Green */
1133     while (pc->green_bpaths) {
1134         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1135         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1136     }
1137     pc->green_curve->reset();
1138     if (pc->green_anchor) {
1139         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1140     }
1141     pc->sa = NULL;
1142     pc->ea = NULL;
1143     pc->npoints = 0;
1144     pc->red_curve_is_valid = false;
1148 static void
1149 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1151     g_assert( pc->npoints == 0 );
1153     pc->p[0] = p;
1154     pc->p[1] = p;
1155     pc->npoints = 2;
1156     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1158     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1161 /**
1162  * Show the status message for the current line/curve segment.
1163  * This type of message always shows angle/distance as the last
1164  * two parameters ("angle %3.2f&#176;, distance %s").
1165  */ 
1166 static void
1167 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1169     g_assert(pc != NULL);
1170     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1171     g_assert(message != NULL);
1173     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1174     NR::Point rel = p - pc->p[pc_point_to_compare];
1175     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1176     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1177     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1178         angle = angle_to_compass (angle);
1180     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1181     g_string_free(dist, FALSE);
1184 static void
1185 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1187     g_assert( pc->npoints != 0 );
1188     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1190     pc->p[2] = p;
1191     pc->p[3] = p;
1192     pc->p[4] = p;
1193     pc->npoints = 5;
1194     pc->red_curve->reset();
1195     pc->red_curve->moveto(pc->p[0]);
1196     bool is_curve;
1197     if (pc->p[1] != pc->p[0])
1198     {
1199         pc->red_curve->curveto(pc->p[1], p, p);
1200         is_curve = true;
1201     } else {
1202         pc->red_curve->lineto(p);
1203         is_curve = false;
1204     }
1206     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1208     if (statusbar) {
1209         gchar *message = is_curve ?
1210             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1211             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1212         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1213     }
1216 static void
1217 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1219     sp_canvas_item_show(pc->c1);
1220     sp_canvas_item_show(pc->cl1);
1222     if ( pc->npoints == 2 ) {
1223         pc->p[1] = p;
1224         sp_canvas_item_hide(pc->c0);
1225         sp_canvas_item_hide(pc->cl0);
1226         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1227         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1229         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"));
1230     } else if ( pc->npoints == 5 ) {
1231         pc->p[4] = p;
1232         sp_canvas_item_show(pc->c0);
1233         sp_canvas_item_show(pc->cl0);
1234         bool is_symm = false;
1235         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1236              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1237             NR::Point delta = p - pc->p[3];
1238             pc->p[2] = pc->p[3] - delta;
1239             is_symm = true;
1240             pc->red_curve->reset();
1241             pc->red_curve->moveto(pc->p[0]);
1242             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1243             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1244         }
1245         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1246         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1247         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1248         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1250         gchar *message = is_symm ?
1251             _("<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") :
1252             _("<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");
1253         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1254     } else {
1255         g_warning("Something bad happened - npoints is %d", pc->npoints);
1256     }
1259 static void
1260 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1262     if (!pc->red_curve->is_empty()) {
1263         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1264         SPCurve *curve = pc->red_curve->copy();
1265         /// \todo fixme:
1266         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1267         curve->unref();
1268         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1270         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1272         pc->p[0] = pc->p[3];
1273         pc->p[1] = pc->p[4];
1274         pc->npoints = 2;
1276         pc->red_curve->reset();
1277     }
1280 static void
1281 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1283     if (pc->expecting_clicks_for_LPE > 1) {
1284         // don't let the path be finished before we have collected the required number of mouse clicks
1285         return;
1286     }
1288     pen_disable_events(pc);
1289     
1290     SPDesktop *const desktop = pc->desktop;
1291     pc->_message_context->clear();
1292     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1294     pc->red_curve->reset();
1295     spdc_concat_colors_and_flush(pc, closed);
1296     pc->sa = NULL;
1297     pc->ea = NULL;
1299     pc->npoints = 0;
1300     pc->state = SP_PEN_CONTEXT_POINT;
1302     sp_canvas_item_hide(pc->c0);
1303     sp_canvas_item_hide(pc->c1);
1304     sp_canvas_item_hide(pc->cl0);
1305     sp_canvas_item_hide(pc->cl1);
1307     if (pc->green_anchor) {
1308         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1309     }
1312     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1314     pen_enable_events(pc);
1317 static void
1318 pen_disable_events(SPPenContext *const pc) {
1319   pc->events_disabled++;
1322 static void
1323 pen_enable_events(SPPenContext *const pc) {
1324   g_return_if_fail(pc->events_disabled != 0);
1325   
1326   pc->events_disabled--;
1329 void
1330 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1331                                          unsigned int num_clicks, bool use_polylines)
1333     g_print ("Now waiting for %s to be applied\n",
1334              Inkscape::LivePathEffect::LPETypeConverter.get_label(effect_type).c_str());
1335     pc->expecting_clicks_for_LPE = num_clicks;
1336     pc->polylines_only = use_polylines;
1337     pc->waiting_LPE_type = effect_type;
1340 /*
1341   Local Variables:
1342   mode:c++
1343   c-file-style:"stroustrup"
1344   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1345   indent-tabs-mode:nil
1346   fill-column:99
1347   End:
1348 */
1349 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :