Code

fix the snapindicator stealing release event by grabbing the acetate on click, not...
[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         case GDK_BUTTON_RELEASE:
314             ret = pen_handle_button_release(pc, event->button);
315             break;
316         default:
317             break;
318     }
320     if (!ret) {
321         if (((SPEventContextClass *) pen_parent_class)->item_handler)
322             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
323     }
325     return ret;
328 /**
329  * Callback to handle all pen events.
330  */
331 static gint
332 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
334     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
336     gint ret = FALSE;
338     switch (event->type) {
339         case GDK_BUTTON_PRESS:
340             ret = pen_handle_button_press(pc, event->button);
341             break;
343         case GDK_MOTION_NOTIFY:
344             ret = pen_handle_motion_notify(pc, event->motion);
345             break;
347         case GDK_BUTTON_RELEASE:
348             ret = pen_handle_button_release(pc, event->button);
349             break;
351         case GDK_2BUTTON_PRESS:
352             ret = pen_handle_2button_press(pc, event->button);
353             break;
355         case GDK_KEY_PRESS:
356             ret = pen_handle_key_press(pc, event);
357             break;
359         default:
360             break;
361     }
363     if (!ret) {
364         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
365             = ((SPEventContextClass *) pen_parent_class)->root_handler;
366         if (parent_root_handler) {
367             ret = parent_root_handler(ec, event);
368         }
369     }
371     return ret;
374 /**
375  * Handle mouse button press event.
376  */
377 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
379     if (pc->events_disabled) {
380         // skip event processing if events are disabled
381         return FALSE;
382     }
384     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
385     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
386     NR::Point const event_w(bevent.x, bevent.y);
387     NR::Point const event_dt(desktop->w2d(event_w));
388     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
390     gint ret = FALSE;
391     if (bevent.button == 1 && !event_context->space_panning
392         // when the last click for a waiting LPE occurs we want to finish the path
393         && pc->expecting_clicks_for_LPE != 1) {
395         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
396             return TRUE;
397         }
399         if (!pc->grab ) {
400             /* Grab mouse, so release will not pass unnoticed */
401             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
402             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
403                                             GDK_BUTTON_RELEASE_MASK |
404                                             GDK_POINTER_MOTION_MASK  ),
405                                 NULL, bevent.time);
406         }
408         pen_drag_origin_w = event_w;
409         pen_within_tolerance = true;
411         /* Test whether we hit any anchor. */
412         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
414         switch (pc->mode) {
415             case SP_PEN_CONTEXT_MODE_CLICK:
416                 /* In click mode we add point on release */
417                 switch (pc->state) {
418                     case SP_PEN_CONTEXT_POINT:
419                     case SP_PEN_CONTEXT_CONTROL:
420                     case SP_PEN_CONTEXT_CLOSE:
421                         break;
422                     case SP_PEN_CONTEXT_STOP:
423                         /* This is allowed, if we just cancelled curve */
424                         pc->state = SP_PEN_CONTEXT_POINT;
425                         break;
426                     default:
427                         break;
428                 }
429                 break;
430             case SP_PEN_CONTEXT_MODE_DRAG:
431                 switch (pc->state) {
432                     case SP_PEN_CONTEXT_STOP:
433                         /* This is allowed, if we just cancelled curve */
434                     case SP_PEN_CONTEXT_POINT:
435                         if (pc->npoints == 0) {
437                             if (bevent.state & GDK_CONTROL_MASK) {
438                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
439                                 ret = TRUE;
440                                 break;
441                             }
443                             // TODO: Perhaps it would be nicer to rearrange the following case
444                             // distinction so that the case of a waiting LPE is treated separately
446                             /* Set start anchor */
447                             pc->sa = anchor;
448                             NR::Point p;
449                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
450                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
451                                    a fresh path to be created so don't continue an existing one */
452                                 p = anchor->dp;
453                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
454                             } else {
455                                 // This is the first click of a new curve; deselect item so that
456                                 // this curve is not combined with it (unless it is drawn from its
457                                 // anchor, which is handled by the sibling branch above)
458                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
459                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
460                                     /* if we have a waiting LPE, we need a fresh path to be created
461                                        so don't append to an existing one */
462                                     selection->clear();
463                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
464                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
465                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
466                                 }
468                                 /* Create green anchor */
469                                 p = event_dt;
470                                 spdc_endpoint_snap(pc, p, bevent.state);
471                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
472                             }
473                             spdc_pen_set_initial_point(pc, p);
474                         } else {
476                             /* Set end anchor */
477                             pc->ea = anchor;
478                             NR::Point p;
479                             if (anchor) {
480                                 p = anchor->dp;
481                                 // we hit an anchor, will finish the curve (either with or without closing)
482                                 // in release handler
483                                 pc->state = SP_PEN_CONTEXT_CLOSE;
485                                 if (pc->green_anchor && pc->green_anchor->active) {
486                                     // we clicked on the current curve start, so close it even if
487                                     // we drag a handle away from it
488                                     dc->green_closed = TRUE;
489                                 }
490                                 ret = TRUE;
491                                 break;
493                             } else {
494                                 p = event_dt;
495                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
496                                 spdc_pen_set_subsequent_point(pc, p, true);
497                                 if (pc->polylines_only) {
498                                     spdc_pen_finish_segment(pc, p, bevent.state);
499                                 }
500                             }
501                         }
503                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
504                         ret = TRUE;
505                         break;
506                     case SP_PEN_CONTEXT_CONTROL:
507                         g_warning("Button down in CONTROL state");
508                         break;
509                     case SP_PEN_CONTEXT_CLOSE:
510                         g_warning("Button down in CLOSE state");
511                         break;
512                     default:
513                         break;
514                 }
515                 break;
516             default:
517                 break;
518         }
519     } 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
520         if (pc->npoints != 0) {
522             spdc_pen_finish_segment(pc, event_dt, bevent.state);
523             if (pc->green_closed) {
524                 // finishing at the start anchor, close curve
525                 spdc_pen_finish(pc, TRUE);
526             } else {
527                 // finishing at some other anchor, finish curve but not close
528                 spdc_pen_finish(pc, FALSE);
529             }
531             ret = TRUE;
532         }
533     }
535     if (pc->expecting_clicks_for_LPE) {
536         --pc->expecting_clicks_for_LPE;
537     }
539     return ret;
542 /**
543  * Handle motion_notify event.
544  */
545 static gint
546 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
548     gint ret = FALSE;
550     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
551     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
553     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
554         // allow scrolling
555         return FALSE;
556     }
557    
558     if (pc->events_disabled) {
559         // skip motion events if pen events are disabled
560         return FALSE;
561     }
563     NR::Point const event_w(mevent.x,
564                             mevent.y);
565     if (pen_within_tolerance) {
566         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
567                                                                "value", 0, 0, 100);
568         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
569             return FALSE;   // Do not drag if we're within tolerance from origin.
570         }
571     }
572     // Once the user has moved farther than tolerance from the original location
573     // (indicating they intend to move the object, not click), then always process the
574     // motion notify coordinates as given (no snapping back to origin)
575     pen_within_tolerance = false;
577     /* Find desktop coordinates */
578     NR::Point p = dt->w2d(event_w);
580     /* Test, whether we hit any anchor */
581     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
583     switch (pc->mode) {
584         case SP_PEN_CONTEXT_MODE_CLICK:
585             switch (pc->state) {
586                 case SP_PEN_CONTEXT_POINT:
587                     if ( pc->npoints != 0 ) {
588                         /* Only set point, if we are already appending */
589                         spdc_endpoint_snap(pc, p, mevent.state);
590                         spdc_pen_set_subsequent_point(pc, p, true);
591                         ret = TRUE;
592                     }
593                     break;
594                 case SP_PEN_CONTEXT_CONTROL:
595                 case SP_PEN_CONTEXT_CLOSE:
596                     /* Placing controls is last operation in CLOSE state */
597                     spdc_endpoint_snap(pc, p, mevent.state);
598                     spdc_pen_set_ctrl(pc, p, mevent.state);
599                     ret = TRUE;
600                     break;
601                 case SP_PEN_CONTEXT_STOP:
602                     /* This is perfectly valid */
603                     break;
604                 default:
605                     break;
606             }
607             break;
608         case SP_PEN_CONTEXT_MODE_DRAG:
609             switch (pc->state) {
610                 case SP_PEN_CONTEXT_POINT:
611                     if ( pc->npoints > 0 ) {
612                         /* Only set point, if we are already appending */
614                         if (!anchor) {   /* Snap node only if not hitting anchor */
615                             spdc_endpoint_snap(pc, p, mevent.state);
616                         }
618                         spdc_pen_set_subsequent_point(pc, p, !anchor);
620                         if (anchor && !pc->anchor_statusbar) {
621                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
622                             pc->anchor_statusbar = true;
623                         } else if (!anchor && pc->anchor_statusbar) {
624                             pc->_message_context->clear();
625                             pc->anchor_statusbar = false;
626                         }
628                         ret = TRUE;
629                     } else {
630                         if (anchor && !pc->anchor_statusbar) {
631                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
632                             pc->anchor_statusbar = true;
633                         } else if (!anchor && pc->anchor_statusbar) {
634                             pc->_message_context->clear();
635                             pc->anchor_statusbar = false;
636                         }
637                     }
638                     break;
639                 case SP_PEN_CONTEXT_CONTROL:
640                 case SP_PEN_CONTEXT_CLOSE:
641                     /* Placing controls is last operation in CLOSE state */
643                     // snap the handle
644                     spdc_endpoint_snap_handle(pc, p, mevent.state);
646                     if (!pc->polylines_only) {
647                         spdc_pen_set_ctrl(pc, p, mevent.state);
648                     } else {
649                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
650                     }
651                     gobble_motion_events(GDK_BUTTON1_MASK);
652                     ret = TRUE;
653                     break;
654                 case SP_PEN_CONTEXT_STOP:
655                     /* This is perfectly valid */
656                     break;
657                 default:
658                     break;
659             }
660             break;
661         default:
662             break;
663     }
664     return ret;
667 /**
668  * Handle mouse button release event.
669  */
670 static gint
671 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
673     if (pc->events_disabled) {
674         // skip event processing if events are disabled
675         return FALSE;
676     }
678     gint ret = FALSE;
679     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
680     if ( revent.button == 1  && !event_context->space_panning) {
682         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
684         NR::Point const event_w(revent.x,
685                                 revent.y);
686         /* Find desktop coordinates */
687         NR::Point p = pc->desktop->w2d(event_w);
689         /* Test whether we hit any anchor. */
690         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
692         switch (pc->mode) {
693             case SP_PEN_CONTEXT_MODE_CLICK:
694                 switch (pc->state) {
695                     case SP_PEN_CONTEXT_POINT:
696                         if ( pc->npoints == 0 ) {
697                             /* Start new thread only with button release */
698                             if (anchor) {
699                                 p = anchor->dp;
700                             }
701                             pc->sa = anchor;
702                             spdc_pen_set_initial_point(pc, p);
703                         } else {
704                             /* Set end anchor here */
705                             pc->ea = anchor;
706                             if (anchor) {
707                                 p = anchor->dp;
708                             }
709                         }
710                         pc->state = SP_PEN_CONTEXT_CONTROL;
711                         ret = TRUE;
712                         break;
713                     case SP_PEN_CONTEXT_CONTROL:
714                         /* End current segment */
715                         spdc_endpoint_snap(pc, p, revent.state);
716                         spdc_pen_finish_segment(pc, p, revent.state);
717                         pc->state = SP_PEN_CONTEXT_POINT;
718                         ret = TRUE;
719                         break;
720                     case SP_PEN_CONTEXT_CLOSE:
721                         /* End current segment */
722                         if (!anchor) {   /* Snap node only if not hitting anchor */
723                             spdc_endpoint_snap(pc, p, revent.state);
724                         }
725                         spdc_pen_finish_segment(pc, p, revent.state);
726                         spdc_pen_finish(pc, TRUE);
727                         pc->state = SP_PEN_CONTEXT_POINT;
728                         ret = TRUE;
729                         break;
730                     case SP_PEN_CONTEXT_STOP:
731                         /* This is allowed, if we just cancelled curve */
732                         pc->state = SP_PEN_CONTEXT_POINT;
733                         ret = TRUE;
734                         break;
735                     default:
736                         break;
737                 }
738                 break;
739             case SP_PEN_CONTEXT_MODE_DRAG:
740                 switch (pc->state) {
741                     case SP_PEN_CONTEXT_POINT:
742                     case SP_PEN_CONTEXT_CONTROL:
743                         if (!pc->polylines_only) {
744                             spdc_endpoint_snap(pc, p, revent.state);
745                             spdc_pen_finish_segment(pc, p, revent.state);
746                         }
747                         break;
748                     case SP_PEN_CONTEXT_CLOSE:
749                         spdc_endpoint_snap(pc, p, revent.state);
750                         spdc_pen_finish_segment(pc, p, revent.state);
751                         if (pc->green_closed) {
752                             // finishing at the start anchor, close curve
753                             spdc_pen_finish(pc, TRUE);
754                         } else {
755                             // finishing at some other anchor, finish curve but not close
756                             spdc_pen_finish(pc, FALSE);
757                         }
758                         break;
759                     case SP_PEN_CONTEXT_STOP:
760                         /* This is allowed, if we just cancelled curve */
761                         break;
762                     default:
763                         break;
764                 }
765                 pc->state = SP_PEN_CONTEXT_POINT;
766                 ret = TRUE;
767                 break;
768             default:
769                 break;
770         }
772         if (pc->grab) {
773             /* Release grab now */
774             sp_canvas_item_ungrab(pc->grab, revent.time);
775             pc->grab = NULL;
776         }
778         ret = TRUE;
780         dc->green_closed = FALSE;
781     }
783     // TODO: can we be sure that the path was created correctly?
784     // TODO: should we offer an option to collect the clicks in a list?
785     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
786         pc->polylines_only = false;
788         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
789         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
791         if (pc->waiting_LPE) {
792             // we have an already created LPE waiting for a path
793             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
794             selection->add(SP_OBJECT(pc->waiting_item));
795             pc->waiting_LPE = NULL;
796             pc->polylines_only = false;
797         } else {
798             // the case that we need to create a new LPE and apply it to the just-drawn path is
799             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
800         }
801     }
803     return ret;
806 static gint
807 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
809     gint ret = FALSE;
810     if (pc->npoints != 0 && bevent.button != 2) {
811         spdc_pen_finish(pc, FALSE);
812         ret = TRUE;
813     }
814     return ret;
817 void
818 pen_redraw_all (SPPenContext *const pc)
820     // green
821     if (pc->green_bpaths) {
822         // remove old piecewise green canvasitems
823         while (pc->green_bpaths) {
824             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
825             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
826         }
827         // one canvas bpath for all of green_curve
828         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
829         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
830         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
832         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
833     }
835     if (pc->green_anchor)
836         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
838     pc->red_curve->reset();
839     pc->red_curve->moveto(pc->p[0]);
840     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
841     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
843     // handles
844     if (pc->p[0] != pc->p[1]) {
845         SP_CTRL(pc->c1)->moveto(pc->p[1]);
846         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
847         sp_canvas_item_show (pc->c1);
848         sp_canvas_item_show (pc->cl1);
849     } else {
850         sp_canvas_item_hide (pc->c1);
851         sp_canvas_item_hide (pc->cl1);
852     }
854     Geom::Curve const * last_seg = pc->green_curve->last_segment();
855     if (last_seg) {
856         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
857         if ( cubic &&
858              (*cubic)[2] != to_2geom(pc->p[0]) )
859         {
860             NR::Point p2 = from_2geom((*cubic)[2]);
861             SP_CTRL(pc->c0)->moveto(p2);
862             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
863             sp_canvas_item_show (pc->c0);
864             sp_canvas_item_show (pc->cl0);
865         } else {
866             sp_canvas_item_hide (pc->c0);
867             sp_canvas_item_hide (pc->cl0);
868         }
869     }
872 void
873 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
875     if (pc->npoints != 5)
876         return;
878     // green
879     if (!pc->green_curve->is_empty()) {
880         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
881     } else {
882         // start anchor too
883         if (pc->green_anchor) {
884             pc->green_anchor->dp += NR::Point(x, y);
885         }
886     }
888     // red
889     pc->p[0] += NR::Point(x, y);
890     pc->p[1] += NR::Point(x, y);
891     pen_redraw_all(pc);
894 void
895 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
897     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
900 void
901 pen_lastpoint_tocurve (SPPenContext *const pc)
903     if (pc->npoints != 5)
904         return;
906     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
907     if ( cubic ) {
908         pc->p[1] = pc->p[0] + from_2geom( (*cubic)[3] - (*cubic)[2] );
909     } else {
910         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
911     }
913     pen_redraw_all(pc);
916 void
917 pen_lastpoint_toline (SPPenContext *const pc)
919     if (pc->npoints != 5)
920         return;
922     pc->p[1] = pc->p[0];
924     pen_redraw_all(pc);
928 static gint
929 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
931     gint ret = FALSE;
932     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
934     switch (get_group0_keyval (&event->key)) {
936         case GDK_Left: // move last point left
937         case GDK_KP_Left:
938         case GDK_KP_4:
939             if (!MOD__CTRL) { // not ctrl
940                 if (MOD__ALT) { // alt
941                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
942                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
943                 }
944                 else { // no alt
945                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
946                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
947                 }
948                 ret = TRUE;
949             }
950             break;
951         case GDK_Up: // move last point up
952         case GDK_KP_Up:
953         case GDK_KP_8:
954             if (!MOD__CTRL) { // not ctrl
955                 if (MOD__ALT) { // alt
956                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
957                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
958                 }
959                 else { // no alt
960                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
961                     else pen_lastpoint_move(pc, 0, nudge); // no shift
962                 }
963                 ret = TRUE;
964             }
965             break;
966         case GDK_Right: // move last point right
967         case GDK_KP_Right:
968         case GDK_KP_6:
969             if (!MOD__CTRL) { // not ctrl
970                 if (MOD__ALT) { // alt
971                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
972                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
973                 }
974                 else { // no alt
975                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
976                     else pen_lastpoint_move(pc, nudge, 0); // no shift
977                 }
978                 ret = TRUE;
979             }
980             break;
981         case GDK_Down: // move last point down
982         case GDK_KP_Down:
983         case GDK_KP_2:
984             if (!MOD__CTRL) { // not ctrl
985                 if (MOD__ALT) { // alt
986                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
987                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
988                 }
989                 else { // no alt
990                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
991                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
992                 }
993                 ret = TRUE;
994             }
995             break;
997         case GDK_P:
998         case GDK_p:
999             if (MOD__SHIFT_ONLY) {
1000                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1001                 ret = TRUE;
1002             }
1003             break;
1005         case GDK_C:
1006         case GDK_c:
1007             if (MOD__SHIFT_ONLY) {
1008                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1009                 ret = TRUE;
1010             }
1011             break;
1013         case GDK_B:
1014         case GDK_b:
1015             if (MOD__SHIFT_ONLY) {
1016                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1017                 ret = TRUE;
1018             }
1019             break;
1021         case GDK_A:
1022         case GDK_a:
1023             if (MOD__SHIFT_ONLY) {
1024                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1025                 ret = TRUE;
1026             }
1027             break;
1029         case GDK_U:
1030         case GDK_u:
1031             if (MOD__SHIFT_ONLY) {
1032                 pen_lastpoint_tocurve(pc);
1033                 ret = TRUE;
1034             }
1035             break;
1036         case GDK_L:
1037         case GDK_l:
1038             if (MOD__SHIFT_ONLY) {
1039                 pen_lastpoint_toline(pc);
1040                 ret = TRUE;
1041             }
1042             break;
1044         case GDK_Return:
1045         case GDK_KP_Enter:
1046             if (pc->npoints != 0) {
1047                 spdc_pen_finish(pc, FALSE);
1048                 ret = TRUE;
1049             }
1050             break;
1051         case GDK_Escape:
1052             if (pc->npoints != 0) {
1053                 // if drawing, cancel, otherwise pass it up for deselecting
1054                 pen_cancel (pc);
1055                 ret = TRUE;
1056             }
1057             break;
1058         case GDK_z:
1059         case GDK_Z:
1060             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1061                 // if drawing, cancel, otherwise pass it up for undo
1062                 pen_cancel (pc);
1063                 ret = TRUE;
1064             }
1065             break;
1066         case GDK_g:
1067         case GDK_G:
1068             if (MOD__SHIFT_ONLY) {
1069                 sp_selection_to_guides();
1070                 ret = true;
1071             }
1072             break;
1073         case GDK_BackSpace:
1074         case GDK_Delete:
1075         case GDK_KP_Delete:
1076             if (pc->green_curve->is_empty()) {
1077                 if (!pc->red_curve->is_empty()) {
1078                     pen_cancel (pc);
1079                     ret = TRUE;
1080                 } else {
1081                     // do nothing; this event should be handled upstream
1082                 }
1083             } else {
1084                 /* Reset red curve */
1085                 pc->red_curve->reset();
1086                 /* Destroy topmost green bpath */
1087                 if (pc->green_bpaths) {
1088                     if (pc->green_bpaths->data)
1089                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1090                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1091                 }
1092                 /* Get last segment */
1093                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1094                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1095                 if ( e < 2 ) {
1096                     g_warning("Green curve length is %d", e);
1097                     break;
1098                 }
1099                 pc->p[0] = p[e - 2].c(3);
1100                 if (p[e - 1].code == NR_CURVETO) {
1101                     pc->p[1] = p[e - 1].c(1);
1102                 } else {
1103                     pc->p[1] = pc->p[0];
1104                 }
1105                 NR::Point const pt(( pc->npoints < 4
1106                                      ? p[e - 1].c(3)
1107                                      : pc->p[3] ));
1108                 pc->npoints = 2;
1109                 pc->green_curve->backspace();
1110                 sp_canvas_item_hide(pc->c0);
1111                 sp_canvas_item_hide(pc->c1);
1112                 sp_canvas_item_hide(pc->cl0);
1113                 sp_canvas_item_hide(pc->cl1);
1114                 pc->state = SP_PEN_CONTEXT_POINT;
1115                 spdc_pen_set_subsequent_point(pc, pt, true);
1116                 ret = TRUE;
1117             }
1118             break;
1119         default:
1120             break;
1121     }
1122     return ret;
1125 static void
1126 spdc_reset_colors(SPPenContext *pc)
1128     /* Red */
1129     pc->red_curve->reset();
1130     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1131     /* Blue */
1132     pc->blue_curve->reset();
1133     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1134     /* Green */
1135     while (pc->green_bpaths) {
1136         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1137         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1138     }
1139     pc->green_curve->reset();
1140     if (pc->green_anchor) {
1141         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1142     }
1143     pc->sa = NULL;
1144     pc->ea = NULL;
1145     pc->npoints = 0;
1146     pc->red_curve_is_valid = false;
1150 static void
1151 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1153     g_assert( pc->npoints == 0 );
1155     pc->p[0] = p;
1156     pc->p[1] = p;
1157     pc->npoints = 2;
1158     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1160     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1163 /**
1164  * Show the status message for the current line/curve segment.
1165  * This type of message always shows angle/distance as the last
1166  * two parameters ("angle %3.2f&#176;, distance %s").
1167  */ 
1168 static void
1169 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1171     g_assert(pc != NULL);
1172     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1173     g_assert(message != NULL);
1175     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1176     NR::Point rel = p - pc->p[pc_point_to_compare];
1177     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1178     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1179     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1180         angle = angle_to_compass (angle);
1182     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1183     g_string_free(dist, FALSE);
1186 static void
1187 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1189     g_assert( pc->npoints != 0 );
1190     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1192     pc->p[2] = p;
1193     pc->p[3] = p;
1194     pc->p[4] = p;
1195     pc->npoints = 5;
1196     pc->red_curve->reset();
1197     pc->red_curve->moveto(pc->p[0]);
1198     bool is_curve;
1199     if (pc->p[1] != pc->p[0])
1200     {
1201         pc->red_curve->curveto(pc->p[1], p, p);
1202         is_curve = true;
1203     } else {
1204         pc->red_curve->lineto(p);
1205         is_curve = false;
1206     }
1208     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1210     if (statusbar) {
1211         gchar *message = is_curve ?
1212             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1213             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1214         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1215     }
1218 static void
1219 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1221     sp_canvas_item_show(pc->c1);
1222     sp_canvas_item_show(pc->cl1);
1224     if ( pc->npoints == 2 ) {
1225         pc->p[1] = p;
1226         sp_canvas_item_hide(pc->c0);
1227         sp_canvas_item_hide(pc->cl0);
1228         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1229         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1231         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"));
1232     } else if ( pc->npoints == 5 ) {
1233         pc->p[4] = p;
1234         sp_canvas_item_show(pc->c0);
1235         sp_canvas_item_show(pc->cl0);
1236         bool is_symm = false;
1237         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1238              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1239             NR::Point delta = p - pc->p[3];
1240             pc->p[2] = pc->p[3] - delta;
1241             is_symm = true;
1242             pc->red_curve->reset();
1243             pc->red_curve->moveto(pc->p[0]);
1244             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1245             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1246         }
1247         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1248         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1249         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1250         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1252         gchar *message = is_symm ?
1253             _("<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") :
1254             _("<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");
1255         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1256     } else {
1257         g_warning("Something bad happened - npoints is %d", pc->npoints);
1258     }
1261 static void
1262 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1264     if (!pc->red_curve->is_empty()) {
1265         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1266         SPCurve *curve = pc->red_curve->copy();
1267         /// \todo fixme:
1268         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1269         curve->unref();
1270         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1272         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1274         pc->p[0] = pc->p[3];
1275         pc->p[1] = pc->p[4];
1276         pc->npoints = 2;
1278         pc->red_curve->reset();
1279     }
1282 static void
1283 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1285     if (pc->expecting_clicks_for_LPE > 1) {
1286         // don't let the path be finished before we have collected the required number of mouse clicks
1287         return;
1288     }
1290     pen_disable_events(pc);
1291     
1292     SPDesktop *const desktop = pc->desktop;
1293     pc->_message_context->clear();
1294     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1296     pc->red_curve->reset();
1297     spdc_concat_colors_and_flush(pc, closed);
1298     pc->sa = NULL;
1299     pc->ea = NULL;
1301     pc->npoints = 0;
1302     pc->state = SP_PEN_CONTEXT_POINT;
1304     sp_canvas_item_hide(pc->c0);
1305     sp_canvas_item_hide(pc->c1);
1306     sp_canvas_item_hide(pc->cl0);
1307     sp_canvas_item_hide(pc->cl1);
1309     if (pc->green_anchor) {
1310         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1311     }
1314     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1316     pen_enable_events(pc);
1319 static void
1320 pen_disable_events(SPPenContext *const pc) {
1321   pc->events_disabled++;
1324 static void
1325 pen_enable_events(SPPenContext *const pc) {
1326   g_return_if_fail(pc->events_disabled != 0);
1327   
1328   pc->events_disabled--;
1331 void
1332 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1333                                          unsigned int num_clicks, bool use_polylines)
1335     g_print ("Now waiting for %s to be applied\n",
1336              Inkscape::LivePathEffect::LPETypeConverter.get_label(effect_type).c_str());
1337     pc->expecting_clicks_for_LPE = num_clicks;
1338     pc->polylines_only = use_polylines;
1339     pc->waiting_LPE_type = effect_type;
1342 /*
1343   Local Variables:
1344   mode:c++
1345   c-file-style:"stroustrup"
1346   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1347   indent-tabs-mode:nil
1348   fill-column:99
1349   End:
1350 */
1351 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :