Code

fix 1214286, 1482209
[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);
63 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
64 static void spdc_reset_colors(SPPenContext *pc);
67 static NR::Point pen_drag_origin_w(0, 0);
68 static bool pen_within_tolerance = false;
70 static SPDrawContextClass *pen_parent_class;
72 /**
73  * Register SPPenContext with Gdk and return its type.
74  */
75 GType
76 sp_pen_context_get_type(void)
77 {
78     static GType type = 0;
79     if (!type) {
80         GTypeInfo info = {
81             sizeof(SPPenContextClass),
82             NULL, NULL,
83             (GClassInitFunc) sp_pen_context_class_init,
84             NULL, NULL,
85             sizeof(SPPenContext),
86             4,
87             (GInstanceInitFunc) sp_pen_context_init,
88             NULL,   /* value_table */
89         };
90         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
91     }
92     return type;
93 }
95 /**
96  * Initialize the SPPenContext vtable.
97  */
98 static void
99 sp_pen_context_class_init(SPPenContextClass *klass)
101     GObjectClass *object_class;
102     SPEventContextClass *event_context_class;
104     object_class = (GObjectClass *) klass;
105     event_context_class = (SPEventContextClass *) klass;
107     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
109     object_class->dispose = sp_pen_context_dispose;
111     event_context_class->setup = sp_pen_context_setup;
112     event_context_class->finish = sp_pen_context_finish;
113     event_context_class->set = sp_pen_context_set;
114     event_context_class->root_handler = sp_pen_context_root_handler;
117 /**
118  * Callback to initialize SPPenContext object.
119  */
120 static void
121 sp_pen_context_init(SPPenContext *pc)
124     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
126     event_context->cursor_shape = cursor_pen_xpm;
127     event_context->hot_x = 4;
128     event_context->hot_y = 4;
130     pc->npoints = 0;
131     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
132     pc->state = SP_PEN_CONTEXT_POINT;
134     pc->c0 = NULL;
135     pc->c1 = NULL;
136     pc->cl0 = NULL;
137     pc->cl1 = NULL;
140 /**
141  * Callback to destroy the SPPenContext object's members and itself.
142  */
143 static void
144 sp_pen_context_dispose(GObject *object)
146     SPPenContext *pc;
148     pc = SP_PEN_CONTEXT(object);
150     if (pc->c0) {
151         gtk_object_destroy(GTK_OBJECT(pc->c0));
152         pc->c0 = NULL;
153     }
154     if (pc->c1) {
155         gtk_object_destroy(GTK_OBJECT(pc->c1));
156         pc->c1 = NULL;
157     }
158     if (pc->cl0) {
159         gtk_object_destroy(GTK_OBJECT(pc->cl0));
160         pc->cl0 = NULL;
161     }
162     if (pc->cl1) {
163         gtk_object_destroy(GTK_OBJECT(pc->cl1));
164         pc->cl1 = NULL;
165     }
167     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
170 /**
171  * Callback to initialize SPPenContext object.
172  */
173 static void
174 sp_pen_context_setup(SPEventContext *ec)
176     SPPenContext *pc;
178     pc = SP_PEN_CONTEXT(ec);
180     if (((SPEventContextClass *) pen_parent_class)->setup) {
181         ((SPEventContextClass *) pen_parent_class)->setup(ec);
182     }
184     /* Pen indicators */
185     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
186                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
187     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
188                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
189     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
190     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
191     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
192     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
194     sp_canvas_item_hide(pc->c0);
195     sp_canvas_item_hide(pc->c1);
196     sp_canvas_item_hide(pc->cl0);
197     sp_canvas_item_hide(pc->cl1);
199     sp_event_context_read(ec, "mode");
201     pc->anchor_statusbar = false;
203     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
204         ec->enableSelectionCue();
205     }
208 static void
209 pen_cancel (SPPenContext *const pc) 
211     pc->state = SP_PEN_CONTEXT_STOP;
212     spdc_reset_colors(pc);
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);
217     pc->_message_context->clear();
218     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
221 /**
222  * Finalization callback.
223  */
224 static void
225 sp_pen_context_finish(SPEventContext *ec)
227     SPPenContext *pc = SP_PEN_CONTEXT(ec);
229     if (pc->npoints != 0) {
230         pen_cancel (pc);
231     }
233     if (((SPEventContextClass *) pen_parent_class)->finish) {
234         ((SPEventContextClass *) pen_parent_class)->finish(ec);
235     }
238 /**
239  * Callback that sets key to value in pen context.
240  */
241 static void
242 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
244     SPPenContext *pc = SP_PEN_CONTEXT(ec);
246     if (!strcmp(key, "mode")) {
247         if ( val && !strcmp(val, "drag") ) {
248             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
249         } else {
250             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
251         }
252     }
255 /**
256  * Snaps new node relative to the previous node.
257  */
258 static void
259 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
261     if (pc->npoints > 0) {
262         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
263     }
265     spdc_endpoint_snap_free(pc, p, state);
268 /**
269  * Snaps new node's handle relative to the new node.
270  */
271 static void
272 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
274     g_return_if_fail(( pc->npoints == 2 ||
275                        pc->npoints == 5   ));
277     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
278     spdc_endpoint_snap_free(pc, p, state);
281 /**
282  * Callback to handle all pen events.
283  */
284 static gint
285 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
287     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
289     gint ret = FALSE;
291     switch (event->type) {
292         case GDK_BUTTON_PRESS:
293             ret = pen_handle_button_press(pc, event->button);
294             break;
296         case GDK_MOTION_NOTIFY:
297             ret = pen_handle_motion_notify(pc, event->motion);
298             break;
300         case GDK_BUTTON_RELEASE:
301             ret = pen_handle_button_release(pc, event->button);
302             break;
304         case GDK_2BUTTON_PRESS:
305             ret = pen_handle_2button_press(pc);
306             break;
308         case GDK_KEY_PRESS:
309             ret = pen_handle_key_press(pc, event);
310             break;
312         default:
313             break;
314     }
316     if (!ret) {
317         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
318             = ((SPEventContextClass *) pen_parent_class)->root_handler;
319         if (parent_root_handler) {
320             ret = parent_root_handler(ec, event);
321         }
322     }
324     return ret;
327 /**
328  * Handle mouse button press event.
329  */
330 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
332     gint ret = FALSE;
333     if (bevent.button == 1) {
335         SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
336         SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
338         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
339             return TRUE;
340         }
342         NR::Point const event_w(bevent.x, bevent.y);
343         pen_drag_origin_w = event_w;
344         pen_within_tolerance = true;
346         /* Test whether we hit any anchor. */
347         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
349         NR::Point const event_dt(desktop->w2d(event_w));
350         switch (pc->mode) {
351             case SP_PEN_CONTEXT_MODE_CLICK:
352                 /* In click mode we add point on release */
353                 switch (pc->state) {
354                     case SP_PEN_CONTEXT_POINT:
355                     case SP_PEN_CONTEXT_CONTROL:
356                     case SP_PEN_CONTEXT_CLOSE:
357                         break;
358                     case SP_PEN_CONTEXT_STOP:
359                         /* This is allowed, if we just cancelled curve */
360                         pc->state = SP_PEN_CONTEXT_POINT;
361                         break;
362                     default:
363                         break;
364                 }
365                 break;
366             case SP_PEN_CONTEXT_MODE_DRAG:
367                 switch (pc->state) {
368                     case SP_PEN_CONTEXT_STOP:
369                         /* This is allowed, if we just cancelled curve */
370                     case SP_PEN_CONTEXT_POINT:
371                         if (pc->npoints == 0) {
373                             /* Set start anchor */
374                             pc->sa = anchor;
375                             NR::Point p;
376                             if (anchor) {
378                                 /* Adjust point to anchor if needed */
379                                 p = anchor->dp;
380                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
382                             } else {
384                                 // This is the first click of a new curve; deselect item so that
385                                 // this curve is not combined with it (unless it is drawn from its
386                                 // anchor, which is handled by the sibling branch above)
387                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
388                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
390                                     selection->clear();
391                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
393                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
395                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
396                                 }
398                                 /* Create green anchor */
399                                 p = event_dt;
400                                 spdc_endpoint_snap(pc, p, bevent.state);
401                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
402                             }
403                             spdc_pen_set_initial_point(pc, p);
404                         } else {
406                             /* Set end anchor */
407                             pc->ea = anchor;
408                             NR::Point p;
409                             if (anchor) {
411                                 p = anchor->dp;
412                                 // we hit an anchor, will finish the curve (either with or without closing)
413                                 // in release handler
414                                 pc->state = SP_PEN_CONTEXT_CLOSE;
416                                 if (pc->green_anchor && pc->green_anchor->active) {
417                                     // we clicked on the current curve start, so close it even if
418                                     // we drag a handle away from it
419                                     dc->green_closed = TRUE;
420                                 }
421                                 ret = TRUE;
422                                 break;
424                             } else {
426                                 p = event_dt;
427                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
428                                 spdc_pen_set_subsequent_point(pc, p, true);
429                             }
431                         }
432                         pc->state = SP_PEN_CONTEXT_CONTROL;
433                         ret = TRUE;
434                         break;
435                     case SP_PEN_CONTEXT_CONTROL:
436                         g_warning("Button down in CONTROL state");
437                         break;
438                     case SP_PEN_CONTEXT_CLOSE:
439                         g_warning("Button down in CLOSE state");
440                         break;
441                     default:
442                         break;
443                 }
444                 break;
445             default:
446                 break;
447         }
448     } else if (bevent.button == 3) {
449         if (pc->npoints != 0) {
450             spdc_pen_finish(pc, FALSE);
451             ret = TRUE;
452         }
453     }
455     return ret;
458 /**
459  * Handle motion_notify event.
460  */
461 static gint
462 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
464     gint ret = FALSE;
466     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
467         // allow middle-button scrolling
468         return FALSE;
469     }
471     NR::Point const event_w(mevent.x,
472                             mevent.y);
473     if (pen_within_tolerance) {
474         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
475                                                                "value", 0, 0, 100);
476         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
477             return FALSE;   // Do not drag if we're within tolerance from origin.
478         }
479     }
480     // Once the user has moved farther than tolerance from the original location
481     // (indicating they intend to move the object, not click), then always process the
482     // motion notify coordinates as given (no snapping back to origin)
483     pen_within_tolerance = false;
485     SPDesktop *const dt = pc->desktop;
486     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
487         /* Grab mouse, so release will not pass unnoticed */
488         pc->grab = SP_CANVAS_ITEM(dt->acetate);
489         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
490                                         GDK_BUTTON_RELEASE_MASK |
491                                         GDK_POINTER_MOTION_MASK  ),
492                             NULL, mevent.time);
493     }
495     /* Find desktop coordinates */
496     NR::Point p = dt->w2d(event_w);
498     /* Test, whether we hit any anchor */
499     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
501     switch (pc->mode) {
502         case SP_PEN_CONTEXT_MODE_CLICK:
503             switch (pc->state) {
504                 case SP_PEN_CONTEXT_POINT:
505                     if ( pc->npoints != 0 ) {
506                         /* Only set point, if we are already appending */
507                         spdc_endpoint_snap(pc, p, mevent.state);
508                         spdc_pen_set_subsequent_point(pc, p, true);
509                         ret = TRUE;
510                     }
511                     break;
512                 case SP_PEN_CONTEXT_CONTROL:
513                 case SP_PEN_CONTEXT_CLOSE:
514                     /* Placing controls is last operation in CLOSE state */
515                     spdc_endpoint_snap(pc, p, mevent.state);
516                     spdc_pen_set_ctrl(pc, p, mevent.state);
517                     ret = TRUE;
518                     break;
519                 case SP_PEN_CONTEXT_STOP:
520                     /* This is perfectly valid */
521                     break;
522                 default:
523                     break;
524             }
525             break;
526         case SP_PEN_CONTEXT_MODE_DRAG:
527             switch (pc->state) {
528                 case SP_PEN_CONTEXT_POINT:
529                     if ( pc->npoints > 0 ) {
530                         /* Only set point, if we are already appending */
532                         if (!anchor) {   /* Snap node only if not hitting anchor */
533                             spdc_endpoint_snap(pc, p, mevent.state);
534                         }
536                         spdc_pen_set_subsequent_point(pc, p, !anchor);
538                         if (anchor && !pc->anchor_statusbar) {
539                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
540                             pc->anchor_statusbar = true;
541                         } else if (!anchor && pc->anchor_statusbar) {
542                             pc->_message_context->clear();
543                             pc->anchor_statusbar = false;
544                         }
546                         ret = TRUE;
547                     } else {
548                         if (anchor && !pc->anchor_statusbar) {
549                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
550                             pc->anchor_statusbar = true;
551                         } else if (!anchor && pc->anchor_statusbar) {
552                             pc->_message_context->clear();
553                             pc->anchor_statusbar = false;
554                         }
555                     }
556                     break;
557                 case SP_PEN_CONTEXT_CONTROL:
558                 case SP_PEN_CONTEXT_CLOSE:
559                     /* Placing controls is last operation in CLOSE state */
561                     // snap the handle
562                     spdc_endpoint_snap_handle(pc, p, mevent.state);
564                     spdc_pen_set_ctrl(pc, p, mevent.state);
565                     ret = TRUE;
566                     break;
567                 case SP_PEN_CONTEXT_STOP:
568                     /* This is perfectly valid */
569                     break;
570                 default:
571                     break;
572             }
573             break;
574         default:
575             break;
576     }
577     return ret;
580 /**
581  * Handle mouse button release event.
582  */
583 static gint
584 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
586     gint ret = FALSE;
587     if ( revent.button == 1 ) {
589         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
591         NR::Point const event_w(revent.x,
592                                 revent.y);
593         /* Find desktop coordinates */
594         NR::Point p = pc->desktop->w2d(event_w);
596         /* Test whether we hit any anchor. */
597         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
599         switch (pc->mode) {
600             case SP_PEN_CONTEXT_MODE_CLICK:
601                 switch (pc->state) {
602                     case SP_PEN_CONTEXT_POINT:
603                         if ( pc->npoints == 0 ) {
604                             /* Start new thread only with button release */
605                             if (anchor) {
606                                 p = anchor->dp;
607                             }
608                             pc->sa = anchor;
609                             spdc_pen_set_initial_point(pc, p);
610                         } else {
611                             /* Set end anchor here */
612                             pc->ea = anchor;
613                             if (anchor) {
614                                 p = anchor->dp;
615                             }
616                         }
617                         pc->state = SP_PEN_CONTEXT_CONTROL;
618                         ret = TRUE;
619                         break;
620                     case SP_PEN_CONTEXT_CONTROL:
621                         /* End current segment */
622                         spdc_endpoint_snap(pc, p, revent.state);
623                         spdc_pen_finish_segment(pc, p, revent.state);
624                         pc->state = SP_PEN_CONTEXT_POINT;
625                         ret = TRUE;
626                         break;
627                     case SP_PEN_CONTEXT_CLOSE:
628                         /* End current segment */
629                         if (!anchor) {   /* Snap node only if not hitting anchor */
630                             spdc_endpoint_snap(pc, p, revent.state);
631                         }
632                         spdc_pen_finish_segment(pc, p, revent.state);
633                         spdc_pen_finish(pc, TRUE);
634                         pc->state = SP_PEN_CONTEXT_POINT;
635                         ret = TRUE;
636                         break;
637                     case SP_PEN_CONTEXT_STOP:
638                         /* This is allowed, if we just cancelled curve */
639                         pc->state = SP_PEN_CONTEXT_POINT;
640                         ret = TRUE;
641                         break;
642                     default:
643                         break;
644                 }
645                 break;
646             case SP_PEN_CONTEXT_MODE_DRAG:
647                 switch (pc->state) {
648                     case SP_PEN_CONTEXT_POINT:
649                     case SP_PEN_CONTEXT_CONTROL:
650                         spdc_endpoint_snap(pc, p, revent.state);
651                         spdc_pen_finish_segment(pc, p, revent.state);
652                         break;
653                     case SP_PEN_CONTEXT_CLOSE:
654                         spdc_endpoint_snap(pc, p, revent.state);
655                         spdc_pen_finish_segment(pc, p, revent.state);
656                         if (pc->green_closed) {
657                             // finishing at the start anchor, close curve
658                             spdc_pen_finish(pc, TRUE);
659                         } else {
660                             // finishing at some other anchor, finish curve but not close
661                             spdc_pen_finish(pc, FALSE);
662                         }
663                         break;
664                     case SP_PEN_CONTEXT_STOP:
665                         /* This is allowed, if we just cancelled curve */
666                         break;
667                     default:
668                         break;
669                 }
670                 pc->state = SP_PEN_CONTEXT_POINT;
671                 ret = TRUE;
672                 break;
673             default:
674                 break;
675         }
677         if (pc->grab) {
678             /* Release grab now */
679             sp_canvas_item_ungrab(pc->grab, revent.time);
680             pc->grab = NULL;
681         }
683         ret = TRUE;
685         dc->green_closed = FALSE;
686     }
688     return ret;
691 static gint
692 pen_handle_2button_press(SPPenContext *const pc)
694     gint ret = FALSE;
695     if (pc->npoints != 0) {
696         spdc_pen_finish(pc, FALSE);
697         ret = TRUE;
698     }
699     return ret;
702 void
703 pen_redraw_all (SPPenContext *const pc)
705     // green
706     if (pc->green_bpaths) {
707         // remove old piecewise green canvasitems
708         while (pc->green_bpaths) {
709             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
710             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
711         }
712         // one canvas bpath for all of green_curve
713         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
714         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
715         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
717         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
718     }
720     if (pc->green_anchor)
721         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
723     sp_curve_reset(pc->red_curve);
724     sp_curve_moveto(pc->red_curve, pc->p[0]);
725     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
726     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
728     // handles
729     if (pc->p[0] != pc->p[1]) {
730         SP_CTRL(pc->c1)->moveto(pc->p[1]);
731         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
732         sp_canvas_item_show (pc->c1);
733         sp_canvas_item_show (pc->cl1);
734     } else {
735         sp_canvas_item_hide (pc->c1);
736         sp_canvas_item_hide (pc->cl1);
737     }
739     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
740     if (bpath) {
741         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
742             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
743             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
744             sp_canvas_item_show (pc->c0);
745             sp_canvas_item_show (pc->cl0);
746         } else {
747             sp_canvas_item_hide (pc->c0);
748             sp_canvas_item_hide (pc->cl0);
749         }
750     }
753 void
754 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
756     if (pc->npoints != 5)
757         return;
759     // green
760     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
761     if (bpath) {
762         if (bpath->code == NR_CURVETO) {
763             bpath->x2 += x;
764             bpath->y2 += y;
765         }
766         bpath->x3 += x;
767         bpath->y3 += y;
768     } else {
769         // start anchor too
770         if (pc->green_anchor) {
771             pc->green_anchor->dp += NR::Point(x, y);
772         }
773     }
775     // red
776     pc->p[0] += NR::Point(x, y);
777     pc->p[1] += NR::Point(x, y);
778     pen_redraw_all(pc);
781 void
782 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
784     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
787 void
788 pen_lastpoint_tocurve (SPPenContext *const pc)
790     if (pc->npoints != 5)
791         return;
793     // red
794     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
795     if (bpath && bpath->code == NR_CURVETO) {
796         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
797     } else {
798         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
799     }
801     pen_redraw_all(pc);
804 void
805 pen_lastpoint_toline (SPPenContext *const pc)
807     if (pc->npoints != 5)
808         return;
810     pc->p[1] = pc->p[0];
812     pen_redraw_all(pc);
816 static gint
817 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
819     gint ret = FALSE;
820     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
822     switch (get_group0_keyval (&event->key)) {
824         case GDK_Left: // move last point left
825         case GDK_KP_Left:
826         case GDK_KP_4:
827             if (!MOD__CTRL) { // not ctrl
828                 if (MOD__ALT) { // alt
829                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
830                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
831                 }
832                 else { // no alt
833                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
834                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
835                 }
836                 ret = TRUE;
837             }
838             break;
839         case GDK_Up: // move last point up
840         case GDK_KP_Up:
841         case GDK_KP_8:
842             if (!MOD__CTRL) { // not ctrl
843                 if (MOD__ALT) { // alt
844                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
845                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
846                 }
847                 else { // no alt
848                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
849                     else pen_lastpoint_move(pc, 0, nudge); // no shift
850                 }
851                 ret = TRUE;
852             }
853             break;
854         case GDK_Right: // move last point right
855         case GDK_KP_Right:
856         case GDK_KP_6:
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_Down: // move last point down
870         case GDK_KP_Down:
871         case GDK_KP_2:
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;
885         case GDK_U:
886         case GDK_u:
887             if (MOD__SHIFT_ONLY) {
888                 pen_lastpoint_tocurve(pc);
889                 ret = TRUE;
890             }
891             break;
892         case GDK_L:
893         case GDK_l:
894             if (MOD__SHIFT_ONLY) {
895                 pen_lastpoint_toline(pc);
896                 ret = TRUE;
897             }
898             break;
900         case GDK_Return:
901         case GDK_KP_Enter:
902             if (pc->npoints != 0) {
903                 spdc_pen_finish(pc, FALSE);
904                 ret = TRUE;
905             }
906             break;
907         case GDK_Escape:
908             if (pc->npoints != 0) {
909                 // if drawing, cancel, otherwise pass it up for deselecting
910                 pen_cancel (pc);
911                 ret = TRUE;
912             }
913             break;
914         case GDK_z:
915         case GDK_Z:
916             if (MOD__CTRL_ONLY && pc->npoints != 0) {
917                 // if drawing, cancel, otherwise pass it up for undo
918                 pen_cancel (pc);
919                 ret = TRUE;
920             }
921             break;
922         case GDK_BackSpace:
923         case GDK_Delete:
924         case GDK_KP_Delete:
925             if (sp_curve_is_empty(pc->green_curve)) {
926                 pen_cancel (pc);
927                 ret = TRUE;
928             } else {
929                 /* Reset red curve */
930                 sp_curve_reset(pc->red_curve);
931                 /* Destroy topmost green bpath */
932                 if (pc->green_bpaths) {
933                     if (pc->green_bpaths->data)
934                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
935                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
936                 }
937                 /* Get last segment */
938                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
939                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
940                 if ( e < 2 ) {
941                     g_warning("Green curve length is %d", e);
942                     break;
943                 }
944                 pc->p[0] = p[e - 2].c(3);
945                 if (p[e - 1].code == NR_CURVETO) {
946                     pc->p[1] = p[e - 1].c(1);
947                 } else {
948                     pc->p[1] = pc->p[0];
949                 }
950                 NR::Point const pt(( pc->npoints < 4
951                                      ? p[e - 1].c(3)
952                                      : pc->p[3] ));
953                 pc->npoints = 2;
954                 sp_curve_backspace(pc->green_curve);
955                 sp_canvas_item_hide(pc->c0);
956                 sp_canvas_item_hide(pc->c1);
957                 sp_canvas_item_hide(pc->cl0);
958                 sp_canvas_item_hide(pc->cl1);
959                 pc->state = SP_PEN_CONTEXT_POINT;
960                 spdc_pen_set_subsequent_point(pc, pt, true);
961                 ret = TRUE;
962             }
963             break;
964         default:
965             break;
966     }
967     return ret;
970 static void
971 spdc_reset_colors(SPPenContext *pc)
973     /* Red */
974     sp_curve_reset(pc->red_curve);
975     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
976     /* Blue */
977     sp_curve_reset(pc->blue_curve);
978     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
979     /* Green */
980     while (pc->green_bpaths) {
981         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
982         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
983     }
984     sp_curve_reset(pc->green_curve);
985     if (pc->green_anchor) {
986         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
987     }
988     pc->sa = NULL;
989     pc->ea = NULL;
990     pc->npoints = 0;
991     pc->red_curve_is_valid = false;
995 static void
996 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
998     g_assert( pc->npoints == 0 );
1000     pc->p[0] = p;
1001     pc->p[1] = p;
1002     pc->npoints = 2;
1003     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1006 static void
1007 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1009     g_assert( pc->npoints != 0 );
1010     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1012     pc->p[2] = p;
1013     pc->p[3] = p;
1014     pc->p[4] = p;
1015     pc->npoints = 5;
1016     sp_curve_reset(pc->red_curve);
1017     sp_curve_moveto(pc->red_curve, pc->p[0]);
1018     bool is_curve;
1019     if ( (pc->onlycurves)
1020          || ( pc->p[1] != pc->p[0] ) )
1021     {
1022         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1023         is_curve = true;
1024     } else {
1025         sp_curve_lineto(pc->red_curve, p);
1026         is_curve = false;
1027     }
1028     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1030     if (statusbar) {
1031         // status text
1032         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1033         NR::Point rel = p - pc->p[0];
1034         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1035         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1036         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1037             angle = angle_to_compass (angle);
1038         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);
1039         g_string_free(dist, FALSE);
1040     }
1043 static void
1044 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1046     sp_canvas_item_show(pc->c1);
1047     sp_canvas_item_show(pc->cl1);
1049     if ( pc->npoints == 2 ) {
1050         pc->p[1] = p;
1051         sp_canvas_item_hide(pc->c0);
1052         sp_canvas_item_hide(pc->cl0);
1053         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1054         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1056         // status text
1057         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1058         NR::Point rel = p - pc->p[0];
1059         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1060         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1061         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1062             angle = angle_to_compass (angle);
1063         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);
1064         g_string_free(dist, FALSE);
1066     } else if ( pc->npoints == 5 ) {
1067         pc->p[4] = p;
1068         sp_canvas_item_show(pc->c0);
1069         sp_canvas_item_show(pc->cl0);
1070         bool is_symm = false;
1071         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1072              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1073             NR::Point delta = p - pc->p[3];
1074             pc->p[2] = pc->p[3] - delta;
1075             is_symm = true;
1076             sp_curve_reset(pc->red_curve);
1077             sp_curve_moveto(pc->red_curve, pc->p[0]);
1078             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1079             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1080         }
1081         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1082         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1083         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1084         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1086         // status text
1087         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1088         NR::Point rel = p - pc->p[3];
1089         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1090         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1091         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1092             angle = angle_to_compass (angle);
1093         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);
1094         g_string_free(dist, FALSE);
1096     } else {
1097         g_warning("Something bad happened - npoints is %d", pc->npoints);
1098     }
1101 static void
1102 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1104     if (!sp_curve_empty(pc->red_curve)) {
1105         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1106         SPCurve *curve = sp_curve_copy(pc->red_curve);
1107         /// \todo fixme:
1108         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1109         sp_curve_unref(curve);
1110         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1112         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1114         pc->p[0] = pc->p[3];
1115         pc->p[1] = pc->p[4];
1116         pc->npoints = 2;
1118         sp_curve_reset(pc->red_curve);
1119     }
1122 static void
1123 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1125     SPDesktop *const desktop = pc->desktop;
1126     pc->_message_context->clear();
1127     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1129     sp_curve_reset(pc->red_curve);
1130     spdc_concat_colors_and_flush(pc, closed);
1131     pc->sa = NULL;
1132     pc->ea = NULL;
1134     pc->npoints = 0;
1135     pc->state = SP_PEN_CONTEXT_POINT;
1137     sp_canvas_item_hide(pc->c0);
1138     sp_canvas_item_hide(pc->c1);
1139     sp_canvas_item_hide(pc->cl0);
1140     sp_canvas_item_hide(pc->cl1);
1142     if (pc->green_anchor) {
1143         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1144     }
1148 /*
1149   Local Variables:
1150   mode:c++
1151   c-file-style:"stroustrup"
1152   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1153   indent-tabs-mode:nil
1154   fill-column:99
1155   End:
1156 */
1157 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :