Code

fix 1704038: finishing on right-click assumed we are finishing unclosed, which is...
[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>
20 #include "pen-context.h"
21 #include "sp-namedview.h"
22 #include "sp-metrics.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "draw-anchor.h"
27 #include "message-stack.h"
28 #include "message-context.h"
29 #include "prefs-utils.h"
30 #include "sp-path.h"
32 #include "pixmaps/cursor-pen.xpm"
33 #include "display/canvas-bpath.h"
34 #include "display/sp-ctrlline.h"
35 #include "display/sodipodi-ctrl.h"
36 #include <glibmm/i18n.h>
37 #include "libnr/n-art-bpath.h"
38 #include "helper/units.h"
39 #include "macros.h"
40 #include "context-fns.h"
43 static void sp_pen_context_class_init(SPPenContextClass *klass);
44 static void sp_pen_context_init(SPPenContext *pc);
45 static void sp_pen_context_dispose(GObject *object);
47 static void sp_pen_context_setup(SPEventContext *ec);
48 static void sp_pen_context_finish(SPEventContext *ec);
49 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
52 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
53 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
54 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
55 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
57 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
59 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
60 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
61 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
62 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
63 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
64 static void spdc_reset_colors(SPPenContext *pc);
66 static void pen_disable_events(SPPenContext *const pc);
67 static void pen_enable_events(SPPenContext *const pc);
69 static NR::Point pen_drag_origin_w(0, 0);
70 static bool pen_within_tolerance = false;
72 static SPDrawContextClass *pen_parent_class;
74 /**
75  * Register SPPenContext with Gdk and return its type.
76  */
77 GType
78 sp_pen_context_get_type(void)
79 {
80     static GType type = 0;
81     if (!type) {
82         GTypeInfo info = {
83             sizeof(SPPenContextClass),
84             NULL, NULL,
85             (GClassInitFunc) sp_pen_context_class_init,
86             NULL, NULL,
87             sizeof(SPPenContext),
88             4,
89             (GInstanceInitFunc) sp_pen_context_init,
90             NULL,   /* value_table */
91         };
92         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
93     }
94     return type;
95 }
97 /**
98  * Initialize the SPPenContext vtable.
99  */
100 static void
101 sp_pen_context_class_init(SPPenContextClass *klass)
103     GObjectClass *object_class;
104     SPEventContextClass *event_context_class;
106     object_class = (GObjectClass *) klass;
107     event_context_class = (SPEventContextClass *) klass;
109     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
111     object_class->dispose = sp_pen_context_dispose;
113     event_context_class->setup = sp_pen_context_setup;
114     event_context_class->finish = sp_pen_context_finish;
115     event_context_class->set = sp_pen_context_set;
116     event_context_class->root_handler = sp_pen_context_root_handler;
119 /**
120  * Callback to initialize SPPenContext object.
121  */
122 static void
123 sp_pen_context_init(SPPenContext *pc)
126     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
128     event_context->cursor_shape = cursor_pen_xpm;
129     event_context->hot_x = 4;
130     event_context->hot_y = 4;
132     pc->npoints = 0;
133     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
134     pc->state = SP_PEN_CONTEXT_POINT;
136     pc->c0 = NULL;
137     pc->c1 = NULL;
138     pc->cl0 = NULL;
139     pc->cl1 = NULL;
140     
141     pc->events_disabled = 0;
144 /**
145  * Callback to destroy the SPPenContext object's members and itself.
146  */
147 static void
148 sp_pen_context_dispose(GObject *object)
150     SPPenContext *pc;
152     pc = SP_PEN_CONTEXT(object);
154     if (pc->c0) {
155         gtk_object_destroy(GTK_OBJECT(pc->c0));
156         pc->c0 = NULL;
157     }
158     if (pc->c1) {
159         gtk_object_destroy(GTK_OBJECT(pc->c1));
160         pc->c1 = NULL;
161     }
162     if (pc->cl0) {
163         gtk_object_destroy(GTK_OBJECT(pc->cl0));
164         pc->cl0 = NULL;
165     }
166     if (pc->cl1) {
167         gtk_object_destroy(GTK_OBJECT(pc->cl1));
168         pc->cl1 = NULL;
169     }
171     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
174 /**
175  * Callback to initialize SPPenContext object.
176  */
177 static void
178 sp_pen_context_setup(SPEventContext *ec)
180     SPPenContext *pc;
182     pc = SP_PEN_CONTEXT(ec);
184     if (((SPEventContextClass *) pen_parent_class)->setup) {
185         ((SPEventContextClass *) pen_parent_class)->setup(ec);
186     }
188     /* Pen indicators */
189     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
190                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
191     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
192                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
193     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
194     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
195     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
196     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
198     sp_canvas_item_hide(pc->c0);
199     sp_canvas_item_hide(pc->c1);
200     sp_canvas_item_hide(pc->cl0);
201     sp_canvas_item_hide(pc->cl1);
203     sp_event_context_read(ec, "mode");
205     pc->anchor_statusbar = false;
207     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
208         ec->enableSelectionCue();
209     }
212 static void
213 pen_cancel (SPPenContext *const pc) 
215     pc->state = SP_PEN_CONTEXT_STOP;
216     spdc_reset_colors(pc);
217     sp_canvas_item_hide(pc->c0);
218     sp_canvas_item_hide(pc->c1);
219     sp_canvas_item_hide(pc->cl0);
220     sp_canvas_item_hide(pc->cl1);
221     pc->_message_context->clear();
222     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
224     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
227 /**
228  * Finalization callback.
229  */
230 static void
231 sp_pen_context_finish(SPEventContext *ec)
233     SPPenContext *pc = SP_PEN_CONTEXT(ec);
235     if (pc->npoints != 0) {
236         pen_cancel (pc);
237     }
239     if (((SPEventContextClass *) pen_parent_class)->finish) {
240         ((SPEventContextClass *) pen_parent_class)->finish(ec);
241     }
244 /**
245  * Callback that sets key to value in pen context.
246  */
247 static void
248 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
250     SPPenContext *pc = SP_PEN_CONTEXT(ec);
252     if (!strcmp(key, "mode")) {
253         if ( val && !strcmp(val, "drag") ) {
254             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
255         } else {
256             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
257         }
258     }
261 /**
262  * Snaps new node relative to the previous node.
263  */
264 static void
265 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
267     if (pc->npoints > 0) {
268         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
269     }
271     spdc_endpoint_snap_free(pc, p, state);
274 /**
275  * Snaps new node's handle relative to the new node.
276  */
277 static void
278 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
280     g_return_if_fail(( pc->npoints == 2 ||
281                        pc->npoints == 5   ));
283     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
284     spdc_endpoint_snap_free(pc, p, state);
287 /**
288  * Callback to handle all pen events.
289  */
290 static gint
291 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
293     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
295     gint ret = FALSE;
297     switch (event->type) {
298         case GDK_BUTTON_PRESS:
299             ret = pen_handle_button_press(pc, event->button);
300             break;
302         case GDK_MOTION_NOTIFY:
303             ret = pen_handle_motion_notify(pc, event->motion);
304             break;
306         case GDK_BUTTON_RELEASE:
307             ret = pen_handle_button_release(pc, event->button);
308             break;
310         case GDK_2BUTTON_PRESS:
311             ret = pen_handle_2button_press(pc, event->button);
312             break;
314         case GDK_KEY_PRESS:
315             ret = pen_handle_key_press(pc, event);
316             break;
318         default:
319             break;
320     }
322     if (!ret) {
323         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
324             = ((SPEventContextClass *) pen_parent_class)->root_handler;
325         if (parent_root_handler) {
326             ret = parent_root_handler(ec, event);
327         }
328     }
330     return ret;
333 /**
334  * Handle mouse button press event.
335  */
336 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
338     if (pc->events_disabled) {
339         // skip event processing if events are disabled
340         return FALSE;
341     }
343     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
344     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
345     NR::Point const event_w(bevent.x, bevent.y);
346     NR::Point const event_dt(desktop->w2d(event_w));
348     gint ret = FALSE;
349     if (bevent.button == 1) {
351         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
352             return TRUE;
353         }
355         pen_drag_origin_w = event_w;
356         pen_within_tolerance = true;
358         /* Test whether we hit any anchor. */
359         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
361         switch (pc->mode) {
362             case SP_PEN_CONTEXT_MODE_CLICK:
363                 /* In click mode we add point on release */
364                 switch (pc->state) {
365                     case SP_PEN_CONTEXT_POINT:
366                     case SP_PEN_CONTEXT_CONTROL:
367                     case SP_PEN_CONTEXT_CLOSE:
368                         break;
369                     case SP_PEN_CONTEXT_STOP:
370                         /* This is allowed, if we just cancelled curve */
371                         pc->state = SP_PEN_CONTEXT_POINT;
372                         break;
373                     default:
374                         break;
375                 }
376                 break;
377             case SP_PEN_CONTEXT_MODE_DRAG:
378                 switch (pc->state) {
379                     case SP_PEN_CONTEXT_STOP:
380                         /* This is allowed, if we just cancelled curve */
381                     case SP_PEN_CONTEXT_POINT:
382                         if (pc->npoints == 0) {
384                             /* Set start anchor */
385                             pc->sa = anchor;
386                             NR::Point p;
387                             if (anchor) {
389                                 /* Adjust point to anchor if needed */
390                                 p = anchor->dp;
391                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
393                             } else {
395                                 // This is the first click of a new curve; deselect item so that
396                                 // this curve is not combined with it (unless it is drawn from its
397                                 // anchor, which is handled by the sibling branch above)
398                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
399                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
401                                     selection->clear();
402                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
404                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
406                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
407                                 }
409                                 /* Create green anchor */
410                                 p = event_dt;
411                                 spdc_endpoint_snap(pc, p, bevent.state);
412                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
413                             }
414                             spdc_pen_set_initial_point(pc, p);
415                         } else {
417                             /* Set end anchor */
418                             pc->ea = anchor;
419                             NR::Point p;
420                             if (anchor) {
422                                 p = anchor->dp;
423                                 // we hit an anchor, will finish the curve (either with or without closing)
424                                 // in release handler
425                                 pc->state = SP_PEN_CONTEXT_CLOSE;
427                                 if (pc->green_anchor && pc->green_anchor->active) {
428                                     // we clicked on the current curve start, so close it even if
429                                     // we drag a handle away from it
430                                     dc->green_closed = TRUE;
431                                 }
432                                 ret = TRUE;
433                                 break;
435                             } else {
437                                 p = event_dt;
438                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
439                                 spdc_pen_set_subsequent_point(pc, p, true);
440                             }
442                         }
443                         pc->state = SP_PEN_CONTEXT_CONTROL;
444                         ret = TRUE;
445                         break;
446                     case SP_PEN_CONTEXT_CONTROL:
447                         g_warning("Button down in CONTROL state");
448                         break;
449                     case SP_PEN_CONTEXT_CLOSE:
450                         g_warning("Button down in CLOSE state");
451                         break;
452                     default:
453                         break;
454                 }
455                 break;
456             default:
457                 break;
458         }
459     } else if (bevent.button == 3) {
460         if (pc->npoints != 0) {
462             spdc_pen_finish_segment(pc, event_dt, bevent.state);
463             if (pc->green_closed) {
464                 // finishing at the start anchor, close curve
465                 spdc_pen_finish(pc, TRUE);
466             } else {
467                 // finishing at some other anchor, finish curve but not close
468                 spdc_pen_finish(pc, FALSE);
469             }
471             ret = TRUE;
472         }
473     }
475     return ret;
478 /**
479  * Handle motion_notify event.
480  */
481 static gint
482 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
484     gint ret = FALSE;
486     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
487         // allow middle-button scrolling
488         return FALSE;
489     }
490     
491     if (pc->events_disabled) {
492         // skip motion events if pen events are disabled
493         return FALSE;
494     }
496     NR::Point const event_w(mevent.x,
497                             mevent.y);
498     if (pen_within_tolerance) {
499         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
500                                                                "value", 0, 0, 100);
501         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
502             return FALSE;   // Do not drag if we're within tolerance from origin.
503         }
504     }
505     // Once the user has moved farther than tolerance from the original location
506     // (indicating they intend to move the object, not click), then always process the
507     // motion notify coordinates as given (no snapping back to origin)
508     pen_within_tolerance = false;
510     SPDesktop *const dt = pc->desktop;
511     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
512         /* Grab mouse, so release will not pass unnoticed */
513         pc->grab = SP_CANVAS_ITEM(dt->acetate);
514         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
515                                         GDK_BUTTON_RELEASE_MASK |
516                                         GDK_POINTER_MOTION_MASK  ),
517                             NULL, mevent.time);
518     }
520     /* Find desktop coordinates */
521     NR::Point p = dt->w2d(event_w);
523     /* Test, whether we hit any anchor */
524     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
526     switch (pc->mode) {
527         case SP_PEN_CONTEXT_MODE_CLICK:
528             switch (pc->state) {
529                 case SP_PEN_CONTEXT_POINT:
530                     if ( pc->npoints != 0 ) {
531                         /* Only set point, if we are already appending */
532                         spdc_endpoint_snap(pc, p, mevent.state);
533                         spdc_pen_set_subsequent_point(pc, p, true);
534                         ret = TRUE;
535                     }
536                     break;
537                 case SP_PEN_CONTEXT_CONTROL:
538                 case SP_PEN_CONTEXT_CLOSE:
539                     /* Placing controls is last operation in CLOSE state */
540                     spdc_endpoint_snap(pc, p, mevent.state);
541                     spdc_pen_set_ctrl(pc, p, mevent.state);
542                     ret = TRUE;
543                     break;
544                 case SP_PEN_CONTEXT_STOP:
545                     /* This is perfectly valid */
546                     break;
547                 default:
548                     break;
549             }
550             break;
551         case SP_PEN_CONTEXT_MODE_DRAG:
552             switch (pc->state) {
553                 case SP_PEN_CONTEXT_POINT:
554                     if ( pc->npoints > 0 ) {
555                         /* Only set point, if we are already appending */
557                         if (!anchor) {   /* Snap node only if not hitting anchor */
558                             spdc_endpoint_snap(pc, p, mevent.state);
559                         }
561                         spdc_pen_set_subsequent_point(pc, p, !anchor);
563                         if (anchor && !pc->anchor_statusbar) {
564                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
565                             pc->anchor_statusbar = true;
566                         } else if (!anchor && pc->anchor_statusbar) {
567                             pc->_message_context->clear();
568                             pc->anchor_statusbar = false;
569                         }
571                         ret = TRUE;
572                     } else {
573                         if (anchor && !pc->anchor_statusbar) {
574                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
575                             pc->anchor_statusbar = true;
576                         } else if (!anchor && pc->anchor_statusbar) {
577                             pc->_message_context->clear();
578                             pc->anchor_statusbar = false;
579                         }
580                     }
581                     break;
582                 case SP_PEN_CONTEXT_CONTROL:
583                 case SP_PEN_CONTEXT_CLOSE:
584                     /* Placing controls is last operation in CLOSE state */
586                     // snap the handle
587                     spdc_endpoint_snap_handle(pc, p, mevent.state);
589                     spdc_pen_set_ctrl(pc, p, mevent.state);
590                     ret = TRUE;
591                     break;
592                 case SP_PEN_CONTEXT_STOP:
593                     /* This is perfectly valid */
594                     break;
595                 default:
596                     break;
597             }
598             break;
599         default:
600             break;
601     }
602     return ret;
605 /**
606  * Handle mouse button release event.
607  */
608 static gint
609 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
611     if (pc->events_disabled) {
612         // skip event processing if events are disabled
613         return FALSE;
614     }
616     gint ret = FALSE;
617     if ( revent.button == 1 ) {
619         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
621         NR::Point const event_w(revent.x,
622                                 revent.y);
623         /* Find desktop coordinates */
624         NR::Point p = pc->desktop->w2d(event_w);
626         /* Test whether we hit any anchor. */
627         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
629         switch (pc->mode) {
630             case SP_PEN_CONTEXT_MODE_CLICK:
631                 switch (pc->state) {
632                     case SP_PEN_CONTEXT_POINT:
633                         if ( pc->npoints == 0 ) {
634                             /* Start new thread only with button release */
635                             if (anchor) {
636                                 p = anchor->dp;
637                             }
638                             pc->sa = anchor;
639                             spdc_pen_set_initial_point(pc, p);
640                         } else {
641                             /* Set end anchor here */
642                             pc->ea = anchor;
643                             if (anchor) {
644                                 p = anchor->dp;
645                             }
646                         }
647                         pc->state = SP_PEN_CONTEXT_CONTROL;
648                         ret = TRUE;
649                         break;
650                     case SP_PEN_CONTEXT_CONTROL:
651                         /* End current segment */
652                         spdc_endpoint_snap(pc, p, revent.state);
653                         spdc_pen_finish_segment(pc, p, revent.state);
654                         pc->state = SP_PEN_CONTEXT_POINT;
655                         ret = TRUE;
656                         break;
657                     case SP_PEN_CONTEXT_CLOSE:
658                         /* End current segment */
659                         if (!anchor) {   /* Snap node only if not hitting anchor */
660                             spdc_endpoint_snap(pc, p, revent.state);
661                         }
662                         spdc_pen_finish_segment(pc, p, revent.state);
663                         spdc_pen_finish(pc, TRUE);
664                         pc->state = SP_PEN_CONTEXT_POINT;
665                         ret = TRUE;
666                         break;
667                     case SP_PEN_CONTEXT_STOP:
668                         /* This is allowed, if we just cancelled curve */
669                         pc->state = SP_PEN_CONTEXT_POINT;
670                         ret = TRUE;
671                         break;
672                     default:
673                         break;
674                 }
675                 break;
676             case SP_PEN_CONTEXT_MODE_DRAG:
677                 switch (pc->state) {
678                     case SP_PEN_CONTEXT_POINT:
679                     case SP_PEN_CONTEXT_CONTROL:
680                         spdc_endpoint_snap(pc, p, revent.state);
681                         spdc_pen_finish_segment(pc, p, revent.state);
682                         break;
683                     case SP_PEN_CONTEXT_CLOSE:
684                         spdc_endpoint_snap(pc, p, revent.state);
685                         spdc_pen_finish_segment(pc, p, revent.state);
686                         if (pc->green_closed) {
687                             // finishing at the start anchor, close curve
688                             spdc_pen_finish(pc, TRUE);
689                         } else {
690                             // finishing at some other anchor, finish curve but not close
691                             spdc_pen_finish(pc, FALSE);
692                         }
693                         break;
694                     case SP_PEN_CONTEXT_STOP:
695                         /* This is allowed, if we just cancelled curve */
696                         break;
697                     default:
698                         break;
699                 }
700                 pc->state = SP_PEN_CONTEXT_POINT;
701                 ret = TRUE;
702                 break;
703             default:
704                 break;
705         }
707         if (pc->grab) {
708             /* Release grab now */
709             sp_canvas_item_ungrab(pc->grab, revent.time);
710             pc->grab = NULL;
711         }
713         ret = TRUE;
715         dc->green_closed = FALSE;
716     }
718     return ret;
721 static gint
722 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
724     gint ret = FALSE;
725     if (pc->npoints != 0 && bevent.button != 2) {
726         spdc_pen_finish(pc, FALSE);
727         ret = TRUE;
728     }
729     return ret;
732 void
733 pen_redraw_all (SPPenContext *const pc)
735     // green
736     if (pc->green_bpaths) {
737         // remove old piecewise green canvasitems
738         while (pc->green_bpaths) {
739             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
740             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
741         }
742         // one canvas bpath for all of green_curve
743         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
744         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
745         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
747         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
748     }
750     if (pc->green_anchor)
751         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
753     sp_curve_reset(pc->red_curve);
754     sp_curve_moveto(pc->red_curve, pc->p[0]);
755     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
756     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
758     // handles
759     if (pc->p[0] != pc->p[1]) {
760         SP_CTRL(pc->c1)->moveto(pc->p[1]);
761         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
762         sp_canvas_item_show (pc->c1);
763         sp_canvas_item_show (pc->cl1);
764     } else {
765         sp_canvas_item_hide (pc->c1);
766         sp_canvas_item_hide (pc->cl1);
767     }
769     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
770     if (bpath) {
771         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
772             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
773             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
774             sp_canvas_item_show (pc->c0);
775             sp_canvas_item_show (pc->cl0);
776         } else {
777             sp_canvas_item_hide (pc->c0);
778             sp_canvas_item_hide (pc->cl0);
779         }
780     }
783 void
784 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
786     if (pc->npoints != 5)
787         return;
789     // green
790     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
791     if (bpath) {
792         if (bpath->code == NR_CURVETO) {
793             bpath->x2 += x;
794             bpath->y2 += y;
795         }
796         bpath->x3 += x;
797         bpath->y3 += y;
798     } else {
799         // start anchor too
800         if (pc->green_anchor) {
801             pc->green_anchor->dp += NR::Point(x, y);
802         }
803     }
805     // red
806     pc->p[0] += NR::Point(x, y);
807     pc->p[1] += NR::Point(x, y);
808     pen_redraw_all(pc);
811 void
812 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
814     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
817 void
818 pen_lastpoint_tocurve (SPPenContext *const pc)
820     if (pc->npoints != 5)
821         return;
823     // red
824     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
825     if (bpath && bpath->code == NR_CURVETO) {
826         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
827     } else {
828         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
829     }
831     pen_redraw_all(pc);
834 void
835 pen_lastpoint_toline (SPPenContext *const pc)
837     if (pc->npoints != 5)
838         return;
840     pc->p[1] = pc->p[0];
842     pen_redraw_all(pc);
846 static gint
847 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
849     gint ret = FALSE;
850     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
852     switch (get_group0_keyval (&event->key)) {
854         case GDK_Left: // move last point left
855         case GDK_KP_Left:
856         case GDK_KP_4:
857             if (!MOD__CTRL) { // not ctrl
858                 if (MOD__ALT) { // alt
859                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
860                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
861                 }
862                 else { // no alt
863                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
864                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
865                 }
866                 ret = TRUE;
867             }
868             break;
869         case GDK_Up: // move last point up
870         case GDK_KP_Up:
871         case GDK_KP_8:
872             if (!MOD__CTRL) { // not ctrl
873                 if (MOD__ALT) { // alt
874                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
875                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
876                 }
877                 else { // no alt
878                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
879                     else pen_lastpoint_move(pc, 0, nudge); // no shift
880                 }
881                 ret = TRUE;
882             }
883             break;
884         case GDK_Right: // move last point right
885         case GDK_KP_Right:
886         case GDK_KP_6:
887             if (!MOD__CTRL) { // not ctrl
888                 if (MOD__ALT) { // alt
889                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
890                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
891                 }
892                 else { // no alt
893                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
894                     else pen_lastpoint_move(pc, nudge, 0); // no shift
895                 }
896                 ret = TRUE;
897             }
898             break;
899         case GDK_Down: // move last point down
900         case GDK_KP_Down:
901         case GDK_KP_2:
902             if (!MOD__CTRL) { // not ctrl
903                 if (MOD__ALT) { // alt
904                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
905                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
906                 }
907                 else { // no alt
908                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
909                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
910                 }
911                 ret = TRUE;
912             }
913             break;
915         case GDK_U:
916         case GDK_u:
917             if (MOD__SHIFT_ONLY) {
918                 pen_lastpoint_tocurve(pc);
919                 ret = TRUE;
920             }
921             break;
922         case GDK_L:
923         case GDK_l:
924             if (MOD__SHIFT_ONLY) {
925                 pen_lastpoint_toline(pc);
926                 ret = TRUE;
927             }
928             break;
930         case GDK_Return:
931         case GDK_KP_Enter:
932             if (pc->npoints != 0) {
933                 spdc_pen_finish(pc, FALSE);
934                 ret = TRUE;
935             }
936             break;
937         case GDK_Escape:
938             if (pc->npoints != 0) {
939                 // if drawing, cancel, otherwise pass it up for deselecting
940                 pen_cancel (pc);
941                 ret = TRUE;
942             }
943             break;
944         case GDK_z:
945         case GDK_Z:
946             if (MOD__CTRL_ONLY && pc->npoints != 0) {
947                 // if drawing, cancel, otherwise pass it up for undo
948                 pen_cancel (pc);
949                 ret = TRUE;
950             }
951             break;
952         case GDK_BackSpace:
953         case GDK_Delete:
954         case GDK_KP_Delete:
955             if (sp_curve_is_empty(pc->green_curve)) {
956                 pen_cancel (pc);
957                 ret = TRUE;
958             } else {
959                 /* Reset red curve */
960                 sp_curve_reset(pc->red_curve);
961                 /* Destroy topmost green bpath */
962                 if (pc->green_bpaths) {
963                     if (pc->green_bpaths->data)
964                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
965                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
966                 }
967                 /* Get last segment */
968                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
969                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
970                 if ( e < 2 ) {
971                     g_warning("Green curve length is %d", e);
972                     break;
973                 }
974                 pc->p[0] = p[e - 2].c(3);
975                 if (p[e - 1].code == NR_CURVETO) {
976                     pc->p[1] = p[e - 1].c(1);
977                 } else {
978                     pc->p[1] = pc->p[0];
979                 }
980                 NR::Point const pt(( pc->npoints < 4
981                                      ? p[e - 1].c(3)
982                                      : pc->p[3] ));
983                 pc->npoints = 2;
984                 sp_curve_backspace(pc->green_curve);
985                 sp_canvas_item_hide(pc->c0);
986                 sp_canvas_item_hide(pc->c1);
987                 sp_canvas_item_hide(pc->cl0);
988                 sp_canvas_item_hide(pc->cl1);
989                 pc->state = SP_PEN_CONTEXT_POINT;
990                 spdc_pen_set_subsequent_point(pc, pt, true);
991                 ret = TRUE;
992             }
993             break;
994         default:
995             break;
996     }
997     return ret;
1000 static void
1001 spdc_reset_colors(SPPenContext *pc)
1003     /* Red */
1004     sp_curve_reset(pc->red_curve);
1005     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1006     /* Blue */
1007     sp_curve_reset(pc->blue_curve);
1008     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1009     /* Green */
1010     while (pc->green_bpaths) {
1011         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1012         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1013     }
1014     sp_curve_reset(pc->green_curve);
1015     if (pc->green_anchor) {
1016         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1017     }
1018     pc->sa = NULL;
1019     pc->ea = NULL;
1020     pc->npoints = 0;
1021     pc->red_curve_is_valid = false;
1025 static void
1026 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1028     g_assert( pc->npoints == 0 );
1030     pc->p[0] = p;
1031     pc->p[1] = p;
1032     pc->npoints = 2;
1033     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1035     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1038 static void
1039 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1041     g_assert( pc->npoints != 0 );
1042     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1044     pc->p[2] = p;
1045     pc->p[3] = p;
1046     pc->p[4] = p;
1047     pc->npoints = 5;
1048     sp_curve_reset(pc->red_curve);
1049     sp_curve_moveto(pc->red_curve, pc->p[0]);
1050     bool is_curve;
1051     if ( (pc->onlycurves)
1052          || ( pc->p[1] != pc->p[0] ) )
1053     {
1054         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1055         is_curve = true;
1056     } else {
1057         sp_curve_lineto(pc->red_curve, p);
1058         is_curve = false;
1059     }
1061     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1063     if (statusbar) {
1064         // status text
1065         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1066         NR::Point rel = p - pc->p[0];
1067         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1068         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1069         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1070             angle = angle_to_compass (angle);
1071         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>%s</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path"), is_curve? "Curve segment" : "Line segment", angle, dist->str);
1072         g_string_free(dist, FALSE);
1073     }
1076 static void
1077 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1079     sp_canvas_item_show(pc->c1);
1080     sp_canvas_item_show(pc->cl1);
1082     if ( pc->npoints == 2 ) {
1083         pc->p[1] = p;
1084         sp_canvas_item_hide(pc->c0);
1085         sp_canvas_item_hide(pc->cl0);
1086         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1087         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1089         // status text
1090         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1091         NR::Point rel = p - pc->p[0];
1092         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1093         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1094         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1095             angle = angle_to_compass (angle);
1096         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1097         g_string_free(dist, FALSE);
1099     } else if ( pc->npoints == 5 ) {
1100         pc->p[4] = p;
1101         sp_canvas_item_show(pc->c0);
1102         sp_canvas_item_show(pc->cl0);
1103         bool is_symm = false;
1104         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1105              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1106             NR::Point delta = p - pc->p[3];
1107             pc->p[2] = pc->p[3] - delta;
1108             is_symm = true;
1109             sp_curve_reset(pc->red_curve);
1110             sp_curve_moveto(pc->red_curve, pc->p[0]);
1111             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1112             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1113         }
1114         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1115         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1116         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1117         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1119         // status text
1120         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1121         NR::Point rel = p - pc->p[3];
1122         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1123         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1124         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1125             angle = angle_to_compass (angle);
1126         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>%s</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only"), is_symm? "Curve handle, symmetric" : "Curve handle", angle, dist->str);
1127         g_string_free(dist, FALSE);
1129     } else {
1130         g_warning("Something bad happened - npoints is %d", pc->npoints);
1131     }
1134 static void
1135 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1137     if (!sp_curve_empty(pc->red_curve)) {
1138         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1139         SPCurve *curve = sp_curve_copy(pc->red_curve);
1140         /// \todo fixme:
1141         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1142         sp_curve_unref(curve);
1143         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1145         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1147         pc->p[0] = pc->p[3];
1148         pc->p[1] = pc->p[4];
1149         pc->npoints = 2;
1151         sp_curve_reset(pc->red_curve);
1152     }
1155 static void
1156 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1158     pen_disable_events(pc);
1159     
1160     SPDesktop *const desktop = pc->desktop;
1161     pc->_message_context->clear();
1162     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1164     sp_curve_reset(pc->red_curve);
1165     spdc_concat_colors_and_flush(pc, closed);
1166     pc->sa = NULL;
1167     pc->ea = NULL;
1169     pc->npoints = 0;
1170     pc->state = SP_PEN_CONTEXT_POINT;
1172     sp_canvas_item_hide(pc->c0);
1173     sp_canvas_item_hide(pc->c1);
1174     sp_canvas_item_hide(pc->cl0);
1175     sp_canvas_item_hide(pc->cl1);
1177     if (pc->green_anchor) {
1178         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1179     }
1182     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1184     pen_enable_events(pc);
1187 static void
1188 pen_disable_events(SPPenContext *const pc) {
1189   pc->events_disabled++;
1192 static void
1193 pen_enable_events(SPPenContext *const pc) {
1194   g_return_if_fail(pc->events_disabled != 0);
1195   
1196   pc->events_disabled--;
1199 /*
1200   Local Variables:
1201   mode:c++
1202   c-file-style:"stroustrup"
1203   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1204   indent-tabs-mode:nil
1205   fill-column:99
1206   End:
1207 */
1208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :