Code

replace some spcurve->last_bpath calls with spcurve->is_empty
[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/n-art-bpath.h"
41 #include "libnr/nr-point-ops.h"
42 #include "helper/units.h"
43 #include "macros.h"
44 #include "context-fns.h"
46 static void sp_pen_context_class_init(SPPenContextClass *klass);
47 static void sp_pen_context_init(SPPenContext *pc);
48 static void sp_pen_context_dispose(GObject *object);
50 static void sp_pen_context_setup(SPEventContext *ec);
51 static void sp_pen_context_finish(SPEventContext *ec);
52 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
53 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
54 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
61 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
63 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
65 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
66 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
67 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
68 static void spdc_reset_colors(SPPenContext *pc);
70 static void pen_disable_events(SPPenContext *const pc);
71 static void pen_enable_events(SPPenContext *const pc);
73 static NR::Point pen_drag_origin_w(0, 0);
74 static bool pen_within_tolerance = false;
76 static SPDrawContextClass *pen_parent_class;
78 /**
79  * Register SPPenContext with Gdk and return its type.
80  */
81 GType
82 sp_pen_context_get_type(void)
83 {
84     static GType type = 0;
85     if (!type) {
86         GTypeInfo info = {
87             sizeof(SPPenContextClass),
88             NULL, NULL,
89             (GClassInitFunc) sp_pen_context_class_init,
90             NULL, NULL,
91             sizeof(SPPenContext),
92             4,
93             (GInstanceInitFunc) sp_pen_context_init,
94             NULL,   /* value_table */
95         };
96         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
97     }
98     return type;
99 }
101 /**
102  * Initialize the SPPenContext vtable.
103  */
104 static void
105 sp_pen_context_class_init(SPPenContextClass *klass)
107     GObjectClass *object_class;
108     SPEventContextClass *event_context_class;
110     object_class = (GObjectClass *) klass;
111     event_context_class = (SPEventContextClass *) klass;
113     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
115     object_class->dispose = sp_pen_context_dispose;
117     event_context_class->setup = sp_pen_context_setup;
118     event_context_class->finish = sp_pen_context_finish;
119     event_context_class->set = sp_pen_context_set;
120     event_context_class->root_handler = sp_pen_context_root_handler;
121     event_context_class->item_handler = sp_pen_context_item_handler;
124 /**
125  * Callback to initialize SPPenContext object.
126  */
127 static void
128 sp_pen_context_init(SPPenContext *pc)
131     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
133     event_context->cursor_shape = cursor_pen_xpm;
134     event_context->hot_x = 4;
135     event_context->hot_y = 4;
137     pc->npoints = 0;
138     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
139     pc->state = SP_PEN_CONTEXT_POINT;
141     pc->c0 = NULL;
142     pc->c1 = NULL;
143     pc->cl0 = NULL;
144     pc->cl1 = NULL;
145     
146     pc->events_disabled = 0;
148     pc->polylines_only = false;
149     pc->waiting_LPE = NULL;
152 /**
153  * Callback to destroy the SPPenContext object's members and itself.
154  */
155 static void
156 sp_pen_context_dispose(GObject *object)
158     SPPenContext *pc;
160     pc = SP_PEN_CONTEXT(object);
162     if (pc->c0) {
163         gtk_object_destroy(GTK_OBJECT(pc->c0));
164         pc->c0 = NULL;
165     }
166     if (pc->c1) {
167         gtk_object_destroy(GTK_OBJECT(pc->c1));
168         pc->c1 = NULL;
169     }
170     if (pc->cl0) {
171         gtk_object_destroy(GTK_OBJECT(pc->cl0));
172         pc->cl0 = NULL;
173     }
174     if (pc->cl1) {
175         gtk_object_destroy(GTK_OBJECT(pc->cl1));
176         pc->cl1 = NULL;
177     }
179     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
181     pc->polylines_only = false;
182     pc->waiting_LPE = NULL;
183     if (pc->expecting_clicks_for_LPE > 0) {
184         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
185         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
186     }
189 /**
190  * Callback to initialize SPPenContext object.
191  */
192 static void
193 sp_pen_context_setup(SPEventContext *ec)
195     SPPenContext *pc;
197     pc = SP_PEN_CONTEXT(ec);
199     if (((SPEventContextClass *) pen_parent_class)->setup) {
200         ((SPEventContextClass *) pen_parent_class)->setup(ec);
201     }
203     /* Pen indicators */
204     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
205                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
206     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
207                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
208     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
209     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
210     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
211     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
213     sp_canvas_item_hide(pc->c0);
214     sp_canvas_item_hide(pc->c1);
215     sp_canvas_item_hide(pc->cl0);
216     sp_canvas_item_hide(pc->cl1);
218     sp_event_context_read(ec, "mode");
220     pc->anchor_statusbar = false;
222     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
223         ec->enableSelectionCue();
224     }
227 static void
228 pen_cancel (SPPenContext *const pc) 
230     pc->state = SP_PEN_CONTEXT_STOP;
231     spdc_reset_colors(pc);
232     sp_canvas_item_hide(pc->c0);
233     sp_canvas_item_hide(pc->c1);
234     sp_canvas_item_hide(pc->cl0);
235     sp_canvas_item_hide(pc->cl1);
236     pc->_message_context->clear();
237     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
239     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
242 /**
243  * Finalization callback.
244  */
245 static void
246 sp_pen_context_finish(SPEventContext *ec)
248     SPPenContext *pc = SP_PEN_CONTEXT(ec);
250     if (pc->npoints != 0) {
251         pen_cancel (pc);
252     }
254     if (((SPEventContextClass *) pen_parent_class)->finish) {
255         ((SPEventContextClass *) pen_parent_class)->finish(ec);
256     }
259 /**
260  * Callback that sets key to value in pen context.
261  */
262 static void
263 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
265     SPPenContext *pc = SP_PEN_CONTEXT(ec);
267     if (!strcmp(key, "mode")) {
268         if ( val && !strcmp(val, "drag") ) {
269             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
270         } else {
271             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
272         }
273     }
276 /**
277  * Snaps new node relative to the previous node.
278  */
279 static void
280 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
282     if (pc->npoints > 0) {
283         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
284     }
286     spdc_endpoint_snap_free(pc, p, state);
289 /**
290  * Snaps new node's handle relative to the new node.
291  */
292 static void
293 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
295     g_return_if_fail(( pc->npoints == 2 ||
296                        pc->npoints == 5   ));
298     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
299     spdc_endpoint_snap_free(pc, p, state);
302 static gint 
303 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
305     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
307     gint ret = FALSE;
309     switch (event->type) {
310         case GDK_BUTTON_PRESS:
311             ret = pen_handle_button_press(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         pen_drag_origin_w = event_w;
397         pen_within_tolerance = true;
399         /* Test whether we hit any anchor. */
400         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
402         switch (pc->mode) {
403             case SP_PEN_CONTEXT_MODE_CLICK:
404                 /* In click mode we add point on release */
405                 switch (pc->state) {
406                     case SP_PEN_CONTEXT_POINT:
407                     case SP_PEN_CONTEXT_CONTROL:
408                     case SP_PEN_CONTEXT_CLOSE:
409                         break;
410                     case SP_PEN_CONTEXT_STOP:
411                         /* This is allowed, if we just cancelled curve */
412                         pc->state = SP_PEN_CONTEXT_POINT;
413                         break;
414                     default:
415                         break;
416                 }
417                 break;
418             case SP_PEN_CONTEXT_MODE_DRAG:
419                 switch (pc->state) {
420                     case SP_PEN_CONTEXT_STOP:
421                         /* This is allowed, if we just cancelled curve */
422                     case SP_PEN_CONTEXT_POINT:
423                         if (pc->npoints == 0) {
425                             if (bevent.state & GDK_CONTROL_MASK) {
426                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
427                                 ret = TRUE;
428                                 break;
429                             }
431                             // TODO: Perhaps it would be nicer to rearrange the following case
432                             // distinction so that the case of a waiting LPE is treated separately
434                             /* Set start anchor */
435                             pc->sa = anchor;
436                             NR::Point p;
437                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
438                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
439                                    a fresh path to be created so don't continue an existing one */
440                                 p = anchor->dp;
441                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
442                             } else {
443                                 // This is the first click of a new curve; deselect item so that
444                                 // this curve is not combined with it (unless it is drawn from its
445                                 // anchor, which is handled by the sibling branch above)
446                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
447                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
448                                     /* if we have a waiting LPE, we need a fresh path to be created
449                                        so don't append to an existing one */
450                                     selection->clear();
451                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
452                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
453                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
454                                 }
456                                 /* Create green anchor */
457                                 p = event_dt;
458                                 spdc_endpoint_snap(pc, p, bevent.state);
459                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
460                             }
461                             spdc_pen_set_initial_point(pc, p);
462                         } else {
464                             /* Set end anchor */
465                             pc->ea = anchor;
466                             NR::Point p;
467                             if (anchor) {
469                                 p = anchor->dp;
470                                 // we hit an anchor, will finish the curve (either with or without closing)
471                                 // in release handler
472                                 pc->state = SP_PEN_CONTEXT_CLOSE;
474                                 if (pc->green_anchor && pc->green_anchor->active) {
475                                     // we clicked on the current curve start, so close it even if
476                                     // we drag a handle away from it
477                                     dc->green_closed = TRUE;
478                                 }
479                                 ret = TRUE;
480                                 break;
482                             } else {
484                                 p = event_dt;
485                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
486                                 spdc_pen_set_subsequent_point(pc, p, true);
487                                 if (pc->polylines_only) {
488                                     spdc_pen_finish_segment(pc, p, bevent.state);
489                                 }
490                             }
492                         }
493                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
494                         ret = TRUE;
495                         break;
496                     case SP_PEN_CONTEXT_CONTROL:
497                         g_warning("Button down in CONTROL state");
498                         break;
499                     case SP_PEN_CONTEXT_CLOSE:
500                         g_warning("Button down in CLOSE state");
501                         break;
502                     default:
503                         break;
504                 }
505                 break;
506             default:
507                 break;
508         }
509     } 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
510         if (pc->npoints != 0) {
512             spdc_pen_finish_segment(pc, event_dt, bevent.state);
513             if (pc->green_closed) {
514                 // finishing at the start anchor, close curve
515                 spdc_pen_finish(pc, TRUE);
516             } else {
517                 // finishing at some other anchor, finish curve but not close
518                 spdc_pen_finish(pc, FALSE);
519             }
521             ret = TRUE;
522         }
523     }
525     if (pc->expecting_clicks_for_LPE) {
526         --pc->expecting_clicks_for_LPE;
527     }
529     return ret;
532 /**
533  * Handle motion_notify event.
534  */
535 static gint
536 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
538     gint ret = FALSE;
540     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
542     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
543         // allow scrolling
544         return FALSE;
545     }
546     
547     if (pc->events_disabled) {
548         // skip motion events if pen events are disabled
549         return FALSE;
550     }
552     NR::Point const event_w(mevent.x,
553                             mevent.y);
554     if (pen_within_tolerance) {
555         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
556                                                                "value", 0, 0, 100);
557         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
558             return FALSE;   // Do not drag if we're within tolerance from origin.
559         }
560     }
561     // Once the user has moved farther than tolerance from the original location
562     // (indicating they intend to move the object, not click), then always process the
563     // motion notify coordinates as given (no snapping back to origin)
564     pen_within_tolerance = false;
566     SPDesktop *const dt = pc->desktop;
567     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
568         /* Grab mouse, so release will not pass unnoticed */
569         pc->grab = SP_CANVAS_ITEM(dt->acetate);
570         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
571                                         GDK_BUTTON_RELEASE_MASK |
572                                         GDK_POINTER_MOTION_MASK  ),
573                             NULL, mevent.time);
574     }
576     /* Find desktop coordinates */
577     NR::Point p = dt->w2d(event_w);
579     /* Test, whether we hit any anchor */
580     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
582     switch (pc->mode) {
583         case SP_PEN_CONTEXT_MODE_CLICK:
584             switch (pc->state) {
585                 case SP_PEN_CONTEXT_POINT:
586                     if ( pc->npoints != 0 ) {
587                         /* Only set point, if we are already appending */
588                         spdc_endpoint_snap(pc, p, mevent.state);
589                         spdc_pen_set_subsequent_point(pc, p, true);
590                         ret = TRUE;
591                     }
592                     break;
593                 case SP_PEN_CONTEXT_CONTROL:
594                 case SP_PEN_CONTEXT_CLOSE:
595                     /* Placing controls is last operation in CLOSE state */
596                     spdc_endpoint_snap(pc, p, mevent.state);
597                     spdc_pen_set_ctrl(pc, p, mevent.state);
598                     ret = TRUE;
599                     break;
600                 case SP_PEN_CONTEXT_STOP:
601                     /* This is perfectly valid */
602                     break;
603                 default:
604                     break;
605             }
606             break;
607         case SP_PEN_CONTEXT_MODE_DRAG:
608             switch (pc->state) {
609                 case SP_PEN_CONTEXT_POINT:
610                     if ( pc->npoints > 0 ) {
611                         /* Only set point, if we are already appending */
613                         if (!anchor) {   /* Snap node only if not hitting anchor */
614                             spdc_endpoint_snap(pc, p, mevent.state);
615                         }
617                         spdc_pen_set_subsequent_point(pc, p, !anchor);
619                         if (anchor && !pc->anchor_statusbar) {
620                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
621                             pc->anchor_statusbar = true;
622                         } else if (!anchor && pc->anchor_statusbar) {
623                             pc->_message_context->clear();
624                             pc->anchor_statusbar = false;
625                         }
627                         ret = TRUE;
628                     } else {
629                         if (anchor && !pc->anchor_statusbar) {
630                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
631                             pc->anchor_statusbar = true;
632                         } else if (!anchor && pc->anchor_statusbar) {
633                             pc->_message_context->clear();
634                             pc->anchor_statusbar = false;
635                         }
636                     }
637                     break;
638                 case SP_PEN_CONTEXT_CONTROL:
639                 case SP_PEN_CONTEXT_CLOSE:
640                     /* Placing controls is last operation in CLOSE state */
642                     // snap the handle
643                     spdc_endpoint_snap_handle(pc, p, mevent.state);
645                     if (!pc->polylines_only) {
646                         spdc_pen_set_ctrl(pc, p, mevent.state);
647                     } else {
648                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
649                     }
650                     gobble_motion_events(GDK_BUTTON1_MASK);
651                     ret = TRUE;
652                     break;
653                 case SP_PEN_CONTEXT_STOP:
654                     /* This is perfectly valid */
655                     break;
656                 default:
657                     break;
658             }
659             break;
660         default:
661             break;
662     }
663     return ret;
666 /**
667  * Handle mouse button release event.
668  */
669 static gint
670 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
672     if (pc->events_disabled) {
673         // skip event processing if events are disabled
674         return FALSE;
675     }
677     gint ret = FALSE;
678     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
679     if ( revent.button == 1  && !event_context->space_panning) {
681         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
683         NR::Point const event_w(revent.x,
684                                 revent.y);
685         /* Find desktop coordinates */
686         NR::Point p = pc->desktop->w2d(event_w);
688         /* Test whether we hit any anchor. */
689         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
691         switch (pc->mode) {
692             case SP_PEN_CONTEXT_MODE_CLICK:
693                 switch (pc->state) {
694                     case SP_PEN_CONTEXT_POINT:
695                         if ( pc->npoints == 0 ) {
696                             /* Start new thread only with button release */
697                             if (anchor) {
698                                 p = anchor->dp;
699                             }
700                             pc->sa = anchor;
701                             spdc_pen_set_initial_point(pc, p);
702                         } else {
703                             /* Set end anchor here */
704                             pc->ea = anchor;
705                             if (anchor) {
706                                 p = anchor->dp;
707                             }
708                         }
709                         pc->state = SP_PEN_CONTEXT_CONTROL;
710                         ret = TRUE;
711                         break;
712                     case SP_PEN_CONTEXT_CONTROL:
713                         /* End current segment */
714                         spdc_endpoint_snap(pc, p, revent.state);
715                         spdc_pen_finish_segment(pc, p, revent.state);
716                         pc->state = SP_PEN_CONTEXT_POINT;
717                         ret = TRUE;
718                         break;
719                     case SP_PEN_CONTEXT_CLOSE:
720                         /* End current segment */
721                         if (!anchor) {   /* Snap node only if not hitting anchor */
722                             spdc_endpoint_snap(pc, p, revent.state);
723                         }
724                         spdc_pen_finish_segment(pc, p, revent.state);
725                         spdc_pen_finish(pc, TRUE);
726                         pc->state = SP_PEN_CONTEXT_POINT;
727                         ret = TRUE;
728                         break;
729                     case SP_PEN_CONTEXT_STOP:
730                         /* This is allowed, if we just cancelled curve */
731                         pc->state = SP_PEN_CONTEXT_POINT;
732                         ret = TRUE;
733                         break;
734                     default:
735                         break;
736                 }
737                 break;
738             case SP_PEN_CONTEXT_MODE_DRAG:
739                 switch (pc->state) {
740                     case SP_PEN_CONTEXT_POINT:
741                     case SP_PEN_CONTEXT_CONTROL:
742                         if (!pc->polylines_only) {
743                             spdc_endpoint_snap(pc, p, revent.state);
744                             spdc_pen_finish_segment(pc, p, revent.state);
745                         }
746                         break;
747                     case SP_PEN_CONTEXT_CLOSE:
748                         spdc_endpoint_snap(pc, p, revent.state);
749                         spdc_pen_finish_segment(pc, p, revent.state);
750                         if (pc->green_closed) {
751                             // finishing at the start anchor, close curve
752                             spdc_pen_finish(pc, TRUE);
753                         } else {
754                             // finishing at some other anchor, finish curve but not close
755                             spdc_pen_finish(pc, FALSE);
756                         }
757                         break;
758                     case SP_PEN_CONTEXT_STOP:
759                         /* This is allowed, if we just cancelled curve */
760                         break;
761                     default:
762                         break;
763                 }
764                 pc->state = SP_PEN_CONTEXT_POINT;
765                 ret = TRUE;
766                 break;
767             default:
768                 break;
769         }
771         if (pc->grab) {
772             /* Release grab now */
773             sp_canvas_item_ungrab(pc->grab, revent.time);
774             pc->grab = NULL;
775         }
777         ret = TRUE;
779         dc->green_closed = FALSE;
780     }
782     // TODO: can we be sure that the path was created correctly?
783     // TODO: should we offer an option to collect the clicks in a list?
784     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
785         pc->polylines_only = false;
787         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
788         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
790         if (pc->waiting_LPE) {
791             // we have an already created LPE waiting for a path
792             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
793             selection->add(SP_OBJECT(pc->waiting_item));
794             pc->waiting_LPE = NULL;
795             pc->polylines_only = false;
796         } else {
797             // the case that we need to create a new LPE and apply it to the just-drawn path is
798             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
799         }
800     }
802     return ret;
805 static gint
806 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
808     gint ret = FALSE;
809     if (pc->npoints != 0 && bevent.button != 2) {
810         spdc_pen_finish(pc, FALSE);
811         ret = TRUE;
812     }
813     return ret;
816 void
817 pen_redraw_all (SPPenContext *const pc)
819     // green
820     if (pc->green_bpaths) {
821         // remove old piecewise green canvasitems
822         while (pc->green_bpaths) {
823             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
824             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
825         }
826         // one canvas bpath for all of green_curve
827         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
828         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
829         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
831         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
832     }
834     if (pc->green_anchor)
835         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
837     pc->red_curve->reset();
838     pc->red_curve->moveto(pc->p[0]);
839     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
840     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
842     // handles
843     if (pc->p[0] != pc->p[1]) {
844         SP_CTRL(pc->c1)->moveto(pc->p[1]);
845         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
846         sp_canvas_item_show (pc->c1);
847         sp_canvas_item_show (pc->cl1);
848     } else {
849         sp_canvas_item_hide (pc->c1);
850         sp_canvas_item_hide (pc->cl1);
851     }
853     NArtBpath const * bpath = pc->green_curve->last_bpath();
854     if (bpath) {
855         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
856             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
857             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
858             sp_canvas_item_show (pc->c0);
859             sp_canvas_item_show (pc->cl0);
860         } else {
861             sp_canvas_item_hide (pc->c0);
862             sp_canvas_item_hide (pc->cl0);
863         }
864     }
867 void
868 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
870     if (pc->npoints != 5)
871         return;
873     // green
874     if (!pc->green_curve->is_empty()) {
875         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
876     } else {
877         // start anchor too
878         if (pc->green_anchor) {
879             pc->green_anchor->dp += NR::Point(x, y);
880         }
881     }
883     // red
884     pc->p[0] += NR::Point(x, y);
885     pc->p[1] += NR::Point(x, y);
886     pen_redraw_all(pc);
889 void
890 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
892     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
895 void
896 pen_lastpoint_tocurve (SPPenContext *const pc)
898     if (pc->npoints != 5)
899         return;
901     // red
902     NArtBpath const * bpath = pc->green_curve->last_bpath();
903     if (bpath && bpath->code == NR_CURVETO) {
904         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
905     } else {
906         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
907     }
909     pen_redraw_all(pc);
912 void
913 pen_lastpoint_toline (SPPenContext *const pc)
915     if (pc->npoints != 5)
916         return;
918     pc->p[1] = pc->p[0];
920     pen_redraw_all(pc);
924 static gint
925 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
927     gint ret = FALSE;
928     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
930     switch (get_group0_keyval (&event->key)) {
932         case GDK_Left: // move last point left
933         case GDK_KP_Left:
934         case GDK_KP_4:
935             if (!MOD__CTRL) { // not ctrl
936                 if (MOD__ALT) { // alt
937                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
938                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
939                 }
940                 else { // no alt
941                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
942                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
943                 }
944                 ret = TRUE;
945             }
946             break;
947         case GDK_Up: // move last point up
948         case GDK_KP_Up:
949         case GDK_KP_8:
950             if (!MOD__CTRL) { // not ctrl
951                 if (MOD__ALT) { // alt
952                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
953                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
954                 }
955                 else { // no alt
956                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
957                     else pen_lastpoint_move(pc, 0, nudge); // no shift
958                 }
959                 ret = TRUE;
960             }
961             break;
962         case GDK_Right: // move last point right
963         case GDK_KP_Right:
964         case GDK_KP_6:
965             if (!MOD__CTRL) { // not ctrl
966                 if (MOD__ALT) { // alt
967                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
968                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
969                 }
970                 else { // no alt
971                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
972                     else pen_lastpoint_move(pc, nudge, 0); // no shift
973                 }
974                 ret = TRUE;
975             }
976             break;
977         case GDK_Down: // move last point down
978         case GDK_KP_Down:
979         case GDK_KP_2:
980             if (!MOD__CTRL) { // not ctrl
981                 if (MOD__ALT) { // alt
982                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
983                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
984                 }
985                 else { // no alt
986                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
987                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
988                 }
989                 ret = TRUE;
990             }
991             break;
993         case GDK_P:
994         case GDK_p:
995             if (MOD__SHIFT_ONLY) {
996                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
997                 ret = TRUE;
998             }
999             break;
1001         case GDK_C:
1002         case GDK_c:
1003             if (MOD__SHIFT_ONLY) {
1004                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1005                 ret = TRUE;
1006             }
1007             break;
1009         case GDK_B:
1010         case GDK_b:
1011             if (MOD__SHIFT_ONLY) {
1012                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1013                 ret = TRUE;
1014             }
1015             break;
1017         case GDK_A:
1018         case GDK_a:
1019             if (MOD__SHIFT_ONLY) {
1020                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1021                 ret = TRUE;
1022             }
1023             break;
1025         case GDK_U:
1026         case GDK_u:
1027             if (MOD__SHIFT_ONLY) {
1028                 pen_lastpoint_tocurve(pc);
1029                 ret = TRUE;
1030             }
1031             break;
1032         case GDK_L:
1033         case GDK_l:
1034             if (MOD__SHIFT_ONLY) {
1035                 pen_lastpoint_toline(pc);
1036                 ret = TRUE;
1037             }
1038             break;
1040         case GDK_Return:
1041         case GDK_KP_Enter:
1042             if (pc->npoints != 0) {
1043                 spdc_pen_finish(pc, FALSE);
1044                 ret = TRUE;
1045             }
1046             break;
1047         case GDK_Escape:
1048             if (pc->npoints != 0) {
1049                 // if drawing, cancel, otherwise pass it up for deselecting
1050                 pen_cancel (pc);
1051                 ret = TRUE;
1052             }
1053             break;
1054         case GDK_z:
1055         case GDK_Z:
1056             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1057                 // if drawing, cancel, otherwise pass it up for undo
1058                 pen_cancel (pc);
1059                 ret = TRUE;
1060             }
1061             break;
1062         case GDK_g:
1063         case GDK_G:
1064             if (MOD__SHIFT_ONLY) {
1065                 sp_selection_to_guides();
1066                 ret = true;
1067             }
1068             break;
1069         case GDK_BackSpace:
1070         case GDK_Delete:
1071         case GDK_KP_Delete:
1072             if (pc->green_curve->is_empty()) {
1073                 if (!pc->red_curve->is_empty()) {
1074                     pen_cancel (pc);
1075                     ret = TRUE;
1076                 } else {
1077                     // do nothing; this event should be handled upstream
1078                 }
1079             } else {
1080                 /* Reset red curve */
1081                 pc->red_curve->reset();
1082                 /* Destroy topmost green bpath */
1083                 if (pc->green_bpaths) {
1084                     if (pc->green_bpaths->data)
1085                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1086                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1087                 }
1088                 /* Get last segment */
1089                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1090                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1091                 if ( e < 2 ) {
1092                     g_warning("Green curve length is %d", e);
1093                     break;
1094                 }
1095                 pc->p[0] = p[e - 2].c(3);
1096                 if (p[e - 1].code == NR_CURVETO) {
1097                     pc->p[1] = p[e - 1].c(1);
1098                 } else {
1099                     pc->p[1] = pc->p[0];
1100                 }
1101                 NR::Point const pt(( pc->npoints < 4
1102                                      ? p[e - 1].c(3)
1103                                      : pc->p[3] ));
1104                 pc->npoints = 2;
1105                 pc->green_curve->backspace();
1106                 sp_canvas_item_hide(pc->c0);
1107                 sp_canvas_item_hide(pc->c1);
1108                 sp_canvas_item_hide(pc->cl0);
1109                 sp_canvas_item_hide(pc->cl1);
1110                 pc->state = SP_PEN_CONTEXT_POINT;
1111                 spdc_pen_set_subsequent_point(pc, pt, true);
1112                 ret = TRUE;
1113             }
1114             break;
1115         default:
1116             break;
1117     }
1118     return ret;
1121 static void
1122 spdc_reset_colors(SPPenContext *pc)
1124     /* Red */
1125     pc->red_curve->reset();
1126     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1127     /* Blue */
1128     pc->blue_curve->reset();
1129     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1130     /* Green */
1131     while (pc->green_bpaths) {
1132         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1133         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1134     }
1135     pc->green_curve->reset();
1136     if (pc->green_anchor) {
1137         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1138     }
1139     pc->sa = NULL;
1140     pc->ea = NULL;
1141     pc->npoints = 0;
1142     pc->red_curve_is_valid = false;
1146 static void
1147 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1149     g_assert( pc->npoints == 0 );
1151     pc->p[0] = p;
1152     pc->p[1] = p;
1153     pc->npoints = 2;
1154     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1156     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1159 /**
1160  * Show the status message for the current line/curve segment.
1161  * This type of message always shows angle/distance as the last
1162  * two parameters ("angle %3.2f&#176;, distance %s").
1163  */ 
1164 static void
1165 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1167     g_assert(pc != NULL);
1168     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1169     g_assert(message != NULL);
1171     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1172     NR::Point rel = p - pc->p[pc_point_to_compare];
1173     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1174     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1175     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1176         angle = angle_to_compass (angle);
1178     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1179     g_string_free(dist, FALSE);
1182 static void
1183 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1185     g_assert( pc->npoints != 0 );
1186     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1188     pc->p[2] = p;
1189     pc->p[3] = p;
1190     pc->p[4] = p;
1191     pc->npoints = 5;
1192     pc->red_curve->reset();
1193     pc->red_curve->moveto(pc->p[0]);
1194     bool is_curve;
1195     if (pc->p[1] != pc->p[0])
1196     {
1197         pc->red_curve->curveto(pc->p[1], p, p);
1198         is_curve = true;
1199     } else {
1200         pc->red_curve->lineto(p);
1201         is_curve = false;
1202     }
1204     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1206     if (statusbar) {
1207         gchar *message = is_curve ?
1208             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1209             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1210         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1211     }
1214 static void
1215 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1217     sp_canvas_item_show(pc->c1);
1218     sp_canvas_item_show(pc->cl1);
1220     if ( pc->npoints == 2 ) {
1221         pc->p[1] = p;
1222         sp_canvas_item_hide(pc->c0);
1223         sp_canvas_item_hide(pc->cl0);
1224         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1225         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1227         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"));
1228     } else if ( pc->npoints == 5 ) {
1229         pc->p[4] = p;
1230         sp_canvas_item_show(pc->c0);
1231         sp_canvas_item_show(pc->cl0);
1232         bool is_symm = false;
1233         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1234              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1235             NR::Point delta = p - pc->p[3];
1236             pc->p[2] = pc->p[3] - delta;
1237             is_symm = true;
1238             pc->red_curve->reset();
1239             pc->red_curve->moveto(pc->p[0]);
1240             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1241             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1242         }
1243         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1244         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1245         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1246         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1248         gchar *message = is_symm ?
1249             _("<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") :
1250             _("<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");
1251         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1252     } else {
1253         g_warning("Something bad happened - npoints is %d", pc->npoints);
1254     }
1257 static void
1258 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1260     if (!pc->red_curve->is_empty()) {
1261         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1262         SPCurve *curve = pc->red_curve->copy();
1263         /// \todo fixme:
1264         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1265         curve->unref();
1266         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1268         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1270         pc->p[0] = pc->p[3];
1271         pc->p[1] = pc->p[4];
1272         pc->npoints = 2;
1274         pc->red_curve->reset();
1275     }
1278 static void
1279 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1281     if (pc->expecting_clicks_for_LPE > 1) {
1282         // don't let the path be finished before we have collected the required number of mouse clicks
1283         return;
1284     }
1286     pen_disable_events(pc);
1287     
1288     SPDesktop *const desktop = pc->desktop;
1289     pc->_message_context->clear();
1290     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1292     pc->red_curve->reset();
1293     spdc_concat_colors_and_flush(pc, closed);
1294     pc->sa = NULL;
1295     pc->ea = NULL;
1297     pc->npoints = 0;
1298     pc->state = SP_PEN_CONTEXT_POINT;
1300     sp_canvas_item_hide(pc->c0);
1301     sp_canvas_item_hide(pc->c1);
1302     sp_canvas_item_hide(pc->cl0);
1303     sp_canvas_item_hide(pc->cl1);
1305     if (pc->green_anchor) {
1306         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1307     }
1310     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1312     pen_enable_events(pc);
1315 static void
1316 pen_disable_events(SPPenContext *const pc) {
1317   pc->events_disabled++;
1320 static void
1321 pen_enable_events(SPPenContext *const pc) {
1322   g_return_if_fail(pc->events_disabled != 0);
1323   
1324   pc->events_disabled--;
1327 void
1328 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1329                                          unsigned int num_clicks, bool use_polylines)
1331     g_print ("Now waiting for %s to be applied\n",
1332              Inkscape::LivePathEffect::LPETypeConverter.get_label(effect_type).c_str());
1333     pc->expecting_clicks_for_LPE = num_clicks;
1334     pc->polylines_only = use_polylines;
1335     pc->waiting_LPE_type = effect_type;
1338 /*
1339   Local Variables:
1340   mode:c++
1341   c-file-style:"stroustrup"
1342   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1343   indent-tabs-mode:nil
1344   fill-column:99
1345   End:
1346 */
1347 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :