Code

Don't steal 'del' key in pen context when there is no curve being drawn (closes LP...
[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"
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 "helper/units.h"
42 #include "macros.h"
43 #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;
149 /**
150  * Callback to destroy the SPPenContext object's members and itself.
151  */
152 static void
153 sp_pen_context_dispose(GObject *object)
155     SPPenContext *pc;
157     pc = SP_PEN_CONTEXT(object);
159     if (pc->c0) {
160         gtk_object_destroy(GTK_OBJECT(pc->c0));
161         pc->c0 = NULL;
162     }
163     if (pc->c1) {
164         gtk_object_destroy(GTK_OBJECT(pc->c1));
165         pc->c1 = NULL;
166     }
167     if (pc->cl0) {
168         gtk_object_destroy(GTK_OBJECT(pc->cl0));
169         pc->cl0 = NULL;
170     }
171     if (pc->cl1) {
172         gtk_object_destroy(GTK_OBJECT(pc->cl1));
173         pc->cl1 = NULL;
174     }
176     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
179 /**
180  * Callback to initialize SPPenContext object.
181  */
182 static void
183 sp_pen_context_setup(SPEventContext *ec)
185     SPPenContext *pc;
187     pc = SP_PEN_CONTEXT(ec);
189     if (((SPEventContextClass *) pen_parent_class)->setup) {
190         ((SPEventContextClass *) pen_parent_class)->setup(ec);
191     }
193     /* Pen indicators */
194     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
195                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
196     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
197                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
198     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
199     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
200     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
201     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
203     sp_canvas_item_hide(pc->c0);
204     sp_canvas_item_hide(pc->c1);
205     sp_canvas_item_hide(pc->cl0);
206     sp_canvas_item_hide(pc->cl1);
208     sp_event_context_read(ec, "mode");
210     pc->anchor_statusbar = false;
212     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
213         ec->enableSelectionCue();
214     }
217 static void
218 pen_cancel (SPPenContext *const pc) 
220     pc->state = SP_PEN_CONTEXT_STOP;
221     spdc_reset_colors(pc);
222     sp_canvas_item_hide(pc->c0);
223     sp_canvas_item_hide(pc->c1);
224     sp_canvas_item_hide(pc->cl0);
225     sp_canvas_item_hide(pc->cl1);
226     pc->_message_context->clear();
227     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
229     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
232 /**
233  * Finalization callback.
234  */
235 static void
236 sp_pen_context_finish(SPEventContext *ec)
238     SPPenContext *pc = SP_PEN_CONTEXT(ec);
240     if (pc->npoints != 0) {
241         pen_cancel (pc);
242     }
244     if (((SPEventContextClass *) pen_parent_class)->finish) {
245         ((SPEventContextClass *) pen_parent_class)->finish(ec);
246     }
249 /**
250  * Callback that sets key to value in pen context.
251  */
252 static void
253 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
255     SPPenContext *pc = SP_PEN_CONTEXT(ec);
257     if (!strcmp(key, "mode")) {
258         if ( val && !strcmp(val, "drag") ) {
259             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
260         } else {
261             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
262         }
263     }
266 /**
267  * Snaps new node relative to the previous node.
268  */
269 static void
270 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
272     if (pc->npoints > 0) {
273         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
274     }
276     spdc_endpoint_snap_free(pc, p, state);
279 /**
280  * Snaps new node's handle relative to the new node.
281  */
282 static void
283 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
285     g_return_if_fail(( pc->npoints == 2 ||
286                        pc->npoints == 5   ));
288     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
289     spdc_endpoint_snap_free(pc, p, state);
292 static gint 
293 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
295     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
297     gint ret = FALSE;
299     switch (event->type) {
300         case GDK_BUTTON_PRESS:
301             ret = pen_handle_button_press(pc, event->button);
302             break;
303         default:
304             break;
305     }
307     if (!ret) {
308         if (((SPEventContextClass *) pen_parent_class)->item_handler)
309             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
310     }
312     return ret;
315 /**
316  * Callback to handle all pen events.
317  */
318 static gint
319 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
321     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
323     gint ret = FALSE;
325     switch (event->type) {
326         case GDK_BUTTON_PRESS:
327             ret = pen_handle_button_press(pc, event->button);
328             break;
330         case GDK_MOTION_NOTIFY:
331             ret = pen_handle_motion_notify(pc, event->motion);
332             break;
334         case GDK_BUTTON_RELEASE:
335             ret = pen_handle_button_release(pc, event->button);
336             break;
338         case GDK_2BUTTON_PRESS:
339             ret = pen_handle_2button_press(pc, event->button);
340             break;
342         case GDK_KEY_PRESS:
343             ret = pen_handle_key_press(pc, event);
344             break;
346         default:
347             break;
348     }
350     if (!ret) {
351         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
352             = ((SPEventContextClass *) pen_parent_class)->root_handler;
353         if (parent_root_handler) {
354             ret = parent_root_handler(ec, event);
355         }
356     }
358     return ret;
361 /**
362  * Handle mouse button press event.
363  */
364 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
366     if (pc->events_disabled) {
367         // skip event processing if events are disabled
368         return FALSE;
369     }
371     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
372     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
373     NR::Point const event_w(bevent.x, bevent.y);
374     NR::Point const event_dt(desktop->w2d(event_w));
375     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
377     gint ret = FALSE;
378     if (bevent.button == 1 && !event_context->space_panning) {
380         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
381             return TRUE;
382         }
384         pen_drag_origin_w = event_w;
385         pen_within_tolerance = true;
387         /* Test whether we hit any anchor. */
388         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
390         switch (pc->mode) {
391             case SP_PEN_CONTEXT_MODE_CLICK:
392                 /* In click mode we add point on release */
393                 switch (pc->state) {
394                     case SP_PEN_CONTEXT_POINT:
395                     case SP_PEN_CONTEXT_CONTROL:
396                     case SP_PEN_CONTEXT_CLOSE:
397                         break;
398                     case SP_PEN_CONTEXT_STOP:
399                         /* This is allowed, if we just cancelled curve */
400                         pc->state = SP_PEN_CONTEXT_POINT;
401                         break;
402                     default:
403                         break;
404                 }
405                 break;
406             case SP_PEN_CONTEXT_MODE_DRAG:
407                 switch (pc->state) {
408                     case SP_PEN_CONTEXT_STOP:
409                         /* This is allowed, if we just cancelled curve */
410                     case SP_PEN_CONTEXT_POINT:
411                         if (pc->npoints == 0) {
413                             if (bevent.state & GDK_CONTROL_MASK) {
414                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
415                                 ret = TRUE;
416                                 break;
417                             }
419                             /* Set start anchor */
420                             pc->sa = anchor;
421                             NR::Point p;
422                             if (anchor) {
424                                 /* Adjust point to anchor if needed */
425                                 p = anchor->dp;
426                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
428                             } else {
430                                 // This is the first click of a new curve; deselect item so that
431                                 // this curve is not combined with it (unless it is drawn from its
432                                 // anchor, which is handled by the sibling branch above)
433                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
434                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
436                                     selection->clear();
437                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
439                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
441                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
442                                 }
444                                 /* Create green anchor */
445                                 p = event_dt;
446                                 spdc_endpoint_snap(pc, p, bevent.state);
447                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
448                             }
449                             spdc_pen_set_initial_point(pc, p);
450                         } else {
452                             /* Set end anchor */
453                             pc->ea = anchor;
454                             NR::Point p;
455                             if (anchor) {
457                                 p = anchor->dp;
458                                 // we hit an anchor, will finish the curve (either with or without closing)
459                                 // in release handler
460                                 pc->state = SP_PEN_CONTEXT_CLOSE;
462                                 if (pc->green_anchor && pc->green_anchor->active) {
463                                     // we clicked on the current curve start, so close it even if
464                                     // we drag a handle away from it
465                                     dc->green_closed = TRUE;
466                                 }
467                                 ret = TRUE;
468                                 break;
470                             } else {
472                                 p = event_dt;
473                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
474                                 spdc_pen_set_subsequent_point(pc, p, true);
475                             }
477                         }
478                         pc->state = SP_PEN_CONTEXT_CONTROL;
479                         ret = TRUE;
480                         break;
481                     case SP_PEN_CONTEXT_CONTROL:
482                         g_warning("Button down in CONTROL state");
483                         break;
484                     case SP_PEN_CONTEXT_CLOSE:
485                         g_warning("Button down in CLOSE state");
486                         break;
487                     default:
488                         break;
489                 }
490                 break;
491             default:
492                 break;
493         }
494     } else if (bevent.button == 3) {
495         if (pc->npoints != 0) {
497             spdc_pen_finish_segment(pc, event_dt, bevent.state);
498             if (pc->green_closed) {
499                 // finishing at the start anchor, close curve
500                 spdc_pen_finish(pc, TRUE);
501             } else {
502                 // finishing at some other anchor, finish curve but not close
503                 spdc_pen_finish(pc, FALSE);
504             }
506             ret = TRUE;
507         }
508     }
510     return ret;
513 /**
514  * Handle motion_notify event.
515  */
516 static gint
517 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
519     gint ret = FALSE;
521     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
523     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
524         // allow scrolling
525         return FALSE;
526     }
527     
528     if (pc->events_disabled) {
529         // skip motion events if pen events are disabled
530         return FALSE;
531     }
533     NR::Point const event_w(mevent.x,
534                             mevent.y);
535     if (pen_within_tolerance) {
536         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
537                                                                "value", 0, 0, 100);
538         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
539             return FALSE;   // Do not drag if we're within tolerance from origin.
540         }
541     }
542     // Once the user has moved farther than tolerance from the original location
543     // (indicating they intend to move the object, not click), then always process the
544     // motion notify coordinates as given (no snapping back to origin)
545     pen_within_tolerance = false;
547     SPDesktop *const dt = pc->desktop;
548     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
549         /* Grab mouse, so release will not pass unnoticed */
550         pc->grab = SP_CANVAS_ITEM(dt->acetate);
551         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
552                                         GDK_BUTTON_RELEASE_MASK |
553                                         GDK_POINTER_MOTION_MASK  ),
554                             NULL, mevent.time);
555     }
557     /* Find desktop coordinates */
558     NR::Point p = dt->w2d(event_w);
560     /* Test, whether we hit any anchor */
561     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
563     switch (pc->mode) {
564         case SP_PEN_CONTEXT_MODE_CLICK:
565             switch (pc->state) {
566                 case SP_PEN_CONTEXT_POINT:
567                     if ( pc->npoints != 0 ) {
568                         /* Only set point, if we are already appending */
569                         spdc_endpoint_snap(pc, p, mevent.state);
570                         spdc_pen_set_subsequent_point(pc, p, true);
571                         ret = TRUE;
572                     }
573                     break;
574                 case SP_PEN_CONTEXT_CONTROL:
575                 case SP_PEN_CONTEXT_CLOSE:
576                     /* Placing controls is last operation in CLOSE state */
577                     spdc_endpoint_snap(pc, p, mevent.state);
578                     spdc_pen_set_ctrl(pc, p, mevent.state);
579                     ret = TRUE;
580                     break;
581                 case SP_PEN_CONTEXT_STOP:
582                     /* This is perfectly valid */
583                     break;
584                 default:
585                     break;
586             }
587             break;
588         case SP_PEN_CONTEXT_MODE_DRAG:
589             switch (pc->state) {
590                 case SP_PEN_CONTEXT_POINT:
591                     if ( pc->npoints > 0 ) {
592                         /* Only set point, if we are already appending */
594                         if (!anchor) {   /* Snap node only if not hitting anchor */
595                             spdc_endpoint_snap(pc, p, mevent.state);
596                         }
598                         spdc_pen_set_subsequent_point(pc, p, !anchor);
600                         if (anchor && !pc->anchor_statusbar) {
601                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
602                             pc->anchor_statusbar = true;
603                         } else if (!anchor && pc->anchor_statusbar) {
604                             pc->_message_context->clear();
605                             pc->anchor_statusbar = false;
606                         }
608                         ret = TRUE;
609                     } else {
610                         if (anchor && !pc->anchor_statusbar) {
611                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
612                             pc->anchor_statusbar = true;
613                         } else if (!anchor && pc->anchor_statusbar) {
614                             pc->_message_context->clear();
615                             pc->anchor_statusbar = false;
616                         }
617                     }
618                     break;
619                 case SP_PEN_CONTEXT_CONTROL:
620                 case SP_PEN_CONTEXT_CLOSE:
621                     /* Placing controls is last operation in CLOSE state */
623                     // snap the handle
624                     spdc_endpoint_snap_handle(pc, p, mevent.state);
626                     spdc_pen_set_ctrl(pc, p, mevent.state);
627                     gobble_motion_events(GDK_BUTTON1_MASK);
628                     ret = TRUE;
629                     break;
630                 case SP_PEN_CONTEXT_STOP:
631                     /* This is perfectly valid */
632                     break;
633                 default:
634                     break;
635             }
636             break;
637         default:
638             break;
639     }
640     return ret;
643 /**
644  * Handle mouse button release event.
645  */
646 static gint
647 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
649     if (pc->events_disabled) {
650         // skip event processing if events are disabled
651         return FALSE;
652     }
654     gint ret = FALSE;
655     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
656     if ( revent.button == 1  && !event_context->space_panning) {
658         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
660         NR::Point const event_w(revent.x,
661                                 revent.y);
662         /* Find desktop coordinates */
663         NR::Point p = pc->desktop->w2d(event_w);
665         /* Test whether we hit any anchor. */
666         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
668         switch (pc->mode) {
669             case SP_PEN_CONTEXT_MODE_CLICK:
670                 switch (pc->state) {
671                     case SP_PEN_CONTEXT_POINT:
672                         if ( pc->npoints == 0 ) {
673                             /* Start new thread only with button release */
674                             if (anchor) {
675                                 p = anchor->dp;
676                             }
677                             pc->sa = anchor;
678                             spdc_pen_set_initial_point(pc, p);
679                         } else {
680                             /* Set end anchor here */
681                             pc->ea = anchor;
682                             if (anchor) {
683                                 p = anchor->dp;
684                             }
685                         }
686                         pc->state = SP_PEN_CONTEXT_CONTROL;
687                         ret = TRUE;
688                         break;
689                     case SP_PEN_CONTEXT_CONTROL:
690                         /* End current segment */
691                         spdc_endpoint_snap(pc, p, revent.state);
692                         spdc_pen_finish_segment(pc, p, revent.state);
693                         pc->state = SP_PEN_CONTEXT_POINT;
694                         ret = TRUE;
695                         break;
696                     case SP_PEN_CONTEXT_CLOSE:
697                         /* End current segment */
698                         if (!anchor) {   /* Snap node only if not hitting anchor */
699                             spdc_endpoint_snap(pc, p, revent.state);
700                         }
701                         spdc_pen_finish_segment(pc, p, revent.state);
702                         spdc_pen_finish(pc, TRUE);
703                         pc->state = SP_PEN_CONTEXT_POINT;
704                         ret = TRUE;
705                         break;
706                     case SP_PEN_CONTEXT_STOP:
707                         /* This is allowed, if we just cancelled curve */
708                         pc->state = SP_PEN_CONTEXT_POINT;
709                         ret = TRUE;
710                         break;
711                     default:
712                         break;
713                 }
714                 break;
715             case SP_PEN_CONTEXT_MODE_DRAG:
716                 switch (pc->state) {
717                     case SP_PEN_CONTEXT_POINT:
718                     case SP_PEN_CONTEXT_CONTROL:
719                         spdc_endpoint_snap(pc, p, revent.state);
720                         spdc_pen_finish_segment(pc, p, revent.state);
721                         break;
722                     case SP_PEN_CONTEXT_CLOSE:
723                         spdc_endpoint_snap(pc, p, revent.state);
724                         spdc_pen_finish_segment(pc, p, revent.state);
725                         if (pc->green_closed) {
726                             // finishing at the start anchor, close curve
727                             spdc_pen_finish(pc, TRUE);
728                         } else {
729                             // finishing at some other anchor, finish curve but not close
730                             spdc_pen_finish(pc, FALSE);
731                         }
732                         break;
733                     case SP_PEN_CONTEXT_STOP:
734                         /* This is allowed, if we just cancelled curve */
735                         break;
736                     default:
737                         break;
738                 }
739                 pc->state = SP_PEN_CONTEXT_POINT;
740                 ret = TRUE;
741                 break;
742             default:
743                 break;
744         }
746         if (pc->grab) {
747             /* Release grab now */
748             sp_canvas_item_ungrab(pc->grab, revent.time);
749             pc->grab = NULL;
750         }
752         ret = TRUE;
754         dc->green_closed = FALSE;
755     }
757     return ret;
760 static gint
761 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
763     gint ret = FALSE;
764     if (pc->npoints != 0 && bevent.button != 2) {
765         spdc_pen_finish(pc, FALSE);
766         ret = TRUE;
767     }
768     return ret;
771 void
772 pen_redraw_all (SPPenContext *const pc)
774     // green
775     if (pc->green_bpaths) {
776         // remove old piecewise green canvasitems
777         while (pc->green_bpaths) {
778             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
779             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
780         }
781         // one canvas bpath for all of green_curve
782         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
783         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
784         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
786         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
787     }
789     if (pc->green_anchor)
790         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
792     sp_curve_reset(pc->red_curve);
793     sp_curve_moveto(pc->red_curve, pc->p[0]);
794     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
795     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
797     // handles
798     if (pc->p[0] != pc->p[1]) {
799         SP_CTRL(pc->c1)->moveto(pc->p[1]);
800         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
801         sp_canvas_item_show (pc->c1);
802         sp_canvas_item_show (pc->cl1);
803     } else {
804         sp_canvas_item_hide (pc->c1);
805         sp_canvas_item_hide (pc->cl1);
806     }
808     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
809     if (bpath) {
810         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
811             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
812             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
813             sp_canvas_item_show (pc->c0);
814             sp_canvas_item_show (pc->cl0);
815         } else {
816             sp_canvas_item_hide (pc->c0);
817             sp_canvas_item_hide (pc->cl0);
818         }
819     }
822 void
823 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
825     if (pc->npoints != 5)
826         return;
828     // green
829     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
830     if (bpath) {
831         if (bpath->code == NR_CURVETO) {
832             bpath->x2 += x;
833             bpath->y2 += y;
834         }
835         bpath->x3 += x;
836         bpath->y3 += y;
837     } else {
838         // start anchor too
839         if (pc->green_anchor) {
840             pc->green_anchor->dp += NR::Point(x, y);
841         }
842     }
844     // red
845     pc->p[0] += NR::Point(x, y);
846     pc->p[1] += NR::Point(x, y);
847     pen_redraw_all(pc);
850 void
851 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
853     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
856 void
857 pen_lastpoint_tocurve (SPPenContext *const pc)
859     if (pc->npoints != 5)
860         return;
862     // red
863     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
864     if (bpath && bpath->code == NR_CURVETO) {
865         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
866     } else {
867         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
868     }
870     pen_redraw_all(pc);
873 void
874 pen_lastpoint_toline (SPPenContext *const pc)
876     if (pc->npoints != 5)
877         return;
879     pc->p[1] = pc->p[0];
881     pen_redraw_all(pc);
885 static gint
886 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
888     gint ret = FALSE;
889     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
891     switch (get_group0_keyval (&event->key)) {
893         case GDK_Left: // move last point left
894         case GDK_KP_Left:
895         case GDK_KP_4:
896             if (!MOD__CTRL) { // not ctrl
897                 if (MOD__ALT) { // alt
898                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
899                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
900                 }
901                 else { // no alt
902                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
903                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
904                 }
905                 ret = TRUE;
906             }
907             break;
908         case GDK_Up: // move last point up
909         case GDK_KP_Up:
910         case GDK_KP_8:
911             if (!MOD__CTRL) { // not ctrl
912                 if (MOD__ALT) { // alt
913                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
914                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
915                 }
916                 else { // no alt
917                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
918                     else pen_lastpoint_move(pc, 0, nudge); // no shift
919                 }
920                 ret = TRUE;
921             }
922             break;
923         case GDK_Right: // move last point right
924         case GDK_KP_Right:
925         case GDK_KP_6:
926             if (!MOD__CTRL) { // not ctrl
927                 if (MOD__ALT) { // alt
928                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
929                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
930                 }
931                 else { // no alt
932                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
933                     else pen_lastpoint_move(pc, nudge, 0); // no shift
934                 }
935                 ret = TRUE;
936             }
937             break;
938         case GDK_Down: // move last point down
939         case GDK_KP_Down:
940         case GDK_KP_2:
941             if (!MOD__CTRL) { // not ctrl
942                 if (MOD__ALT) { // alt
943                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
944                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
945                 }
946                 else { // no alt
947                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
948                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
949                 }
950                 ret = TRUE;
951             }
952             break;
954         case GDK_U:
955         case GDK_u:
956             if (MOD__SHIFT_ONLY) {
957                 pen_lastpoint_tocurve(pc);
958                 ret = TRUE;
959             }
960             break;
961         case GDK_L:
962         case GDK_l:
963             if (MOD__SHIFT_ONLY) {
964                 pen_lastpoint_toline(pc);
965                 ret = TRUE;
966             }
967             break;
969         case GDK_Return:
970         case GDK_KP_Enter:
971             if (pc->npoints != 0) {
972                 spdc_pen_finish(pc, FALSE);
973                 ret = TRUE;
974             }
975             break;
976         case GDK_Escape:
977             if (pc->npoints != 0) {
978                 // if drawing, cancel, otherwise pass it up for deselecting
979                 pen_cancel (pc);
980                 ret = TRUE;
981             }
982             break;
983         case GDK_z:
984         case GDK_Z:
985             if (MOD__CTRL_ONLY && pc->npoints != 0) {
986                 // if drawing, cancel, otherwise pass it up for undo
987                 pen_cancel (pc);
988                 ret = TRUE;
989             }
990             break;
991         case GDK_g:
992         case GDK_G:
993             if (MOD__SHIFT_ONLY) {
994                 sp_selection_to_guides();
995                 ret = true;
996             }
997             break;
998         case GDK_BackSpace:
999         case GDK_Delete:
1000         case GDK_KP_Delete:
1001             if (sp_curve_is_empty(pc->green_curve)) {
1002                 if (!sp_curve_is_empty(pc->red_curve)) {
1003                     pen_cancel (pc);
1004                     ret = TRUE;
1005                 } else {
1006                     // do nothing; this event should be handled upstream
1007                 }
1008             } else {
1009                 /* Reset red curve */
1010                 sp_curve_reset(pc->red_curve);
1011                 /* Destroy topmost green bpath */
1012                 if (pc->green_bpaths) {
1013                     if (pc->green_bpaths->data)
1014                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1015                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1016                 }
1017                 /* Get last segment */
1018                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1019                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1020                 if ( e < 2 ) {
1021                     g_warning("Green curve length is %d", e);
1022                     break;
1023                 }
1024                 pc->p[0] = p[e - 2].c(3);
1025                 if (p[e - 1].code == NR_CURVETO) {
1026                     pc->p[1] = p[e - 1].c(1);
1027                 } else {
1028                     pc->p[1] = pc->p[0];
1029                 }
1030                 NR::Point const pt(( pc->npoints < 4
1031                                      ? p[e - 1].c(3)
1032                                      : pc->p[3] ));
1033                 pc->npoints = 2;
1034                 sp_curve_backspace(pc->green_curve);
1035                 sp_canvas_item_hide(pc->c0);
1036                 sp_canvas_item_hide(pc->c1);
1037                 sp_canvas_item_hide(pc->cl0);
1038                 sp_canvas_item_hide(pc->cl1);
1039                 pc->state = SP_PEN_CONTEXT_POINT;
1040                 spdc_pen_set_subsequent_point(pc, pt, true);
1041                 ret = TRUE;
1042             }
1043             break;
1044         default:
1045             break;
1046     }
1047     return ret;
1050 static void
1051 spdc_reset_colors(SPPenContext *pc)
1053     /* Red */
1054     sp_curve_reset(pc->red_curve);
1055     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1056     /* Blue */
1057     sp_curve_reset(pc->blue_curve);
1058     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1059     /* Green */
1060     while (pc->green_bpaths) {
1061         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1062         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1063     }
1064     sp_curve_reset(pc->green_curve);
1065     if (pc->green_anchor) {
1066         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1067     }
1068     pc->sa = NULL;
1069     pc->ea = NULL;
1070     pc->npoints = 0;
1071     pc->red_curve_is_valid = false;
1075 static void
1076 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1078     g_assert( pc->npoints == 0 );
1080     pc->p[0] = p;
1081     pc->p[1] = p;
1082     pc->npoints = 2;
1083     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1085     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1088 /**
1089  * Show the status message for the current line/curve segment.
1090  * This type of message always shows angle/distance as the last
1091  * two parameters ("angle %3.2f&#176;, distance %s").
1092  */ 
1093 static void
1094 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1096     g_assert(pc != NULL);
1097     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1098     g_assert(message != NULL);
1100     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1101     NR::Point rel = p - pc->p[pc_point_to_compare];
1102     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1103     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1104     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1105         angle = angle_to_compass (angle);
1107     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1108     g_string_free(dist, FALSE);
1111 static void
1112 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1114     g_assert( pc->npoints != 0 );
1115     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1117     pc->p[2] = p;
1118     pc->p[3] = p;
1119     pc->p[4] = p;
1120     pc->npoints = 5;
1121     sp_curve_reset(pc->red_curve);
1122     sp_curve_moveto(pc->red_curve, pc->p[0]);
1123     bool is_curve;
1124     if ( (pc->onlycurves)
1125          || ( pc->p[1] != pc->p[0] ) )
1126     {
1127         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1128         is_curve = true;
1129     } else {
1130         sp_curve_lineto(pc->red_curve, p);
1131         is_curve = false;
1132     }
1134     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1136     if (statusbar) {
1137         gchar *message = g_strconcat(
1138                              "<b>",
1139                              is_curve ? _("Curve segment") : _("Line segment"),
1140                              "</b>: ",
1141                              _("angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path"),
1142                              NULL
1143                          );
1145         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1146         g_free(message);
1147     }
1150 static void
1151 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1153     sp_canvas_item_show(pc->c1);
1154     sp_canvas_item_show(pc->cl1);
1156     if ( pc->npoints == 2 ) {
1157         pc->p[1] = p;
1158         sp_canvas_item_hide(pc->c0);
1159         sp_canvas_item_hide(pc->cl0);
1160         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1161         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1163         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"));
1164     } else if ( pc->npoints == 5 ) {
1165         pc->p[4] = p;
1166         sp_canvas_item_show(pc->c0);
1167         sp_canvas_item_show(pc->cl0);
1168         bool is_symm = false;
1169         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1170              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1171             NR::Point delta = p - pc->p[3];
1172             pc->p[2] = pc->p[3] - delta;
1173             is_symm = true;
1174             sp_curve_reset(pc->red_curve);
1175             sp_curve_moveto(pc->red_curve, pc->p[0]);
1176             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1177             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1178         }
1179         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1180         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1181         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1182         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1184         gchar *message = g_strconcat(
1185                              "<b>",
1186                              is_symm ? _("Curve handle, symmetric") : _("Curve handle"),
1187                              "</b>: ",
1188                              _("angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only"),
1189                              NULL
1190                          );
1192         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1193         g_free(message);
1194     } else {
1195         g_warning("Something bad happened - npoints is %d", pc->npoints);
1196     }
1199 static void
1200 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1202     if (!sp_curve_empty(pc->red_curve)) {
1203         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1204         SPCurve *curve = sp_curve_copy(pc->red_curve);
1205         /// \todo fixme:
1206         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1207         sp_curve_unref(curve);
1208         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1210         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1212         pc->p[0] = pc->p[3];
1213         pc->p[1] = pc->p[4];
1214         pc->npoints = 2;
1216         sp_curve_reset(pc->red_curve);
1217     }
1220 static void
1221 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1223     pen_disable_events(pc);
1224     
1225     SPDesktop *const desktop = pc->desktop;
1226     pc->_message_context->clear();
1227     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1229     sp_curve_reset(pc->red_curve);
1230     spdc_concat_colors_and_flush(pc, closed);
1231     pc->sa = NULL;
1232     pc->ea = NULL;
1234     pc->npoints = 0;
1235     pc->state = SP_PEN_CONTEXT_POINT;
1237     sp_canvas_item_hide(pc->c0);
1238     sp_canvas_item_hide(pc->c1);
1239     sp_canvas_item_hide(pc->cl0);
1240     sp_canvas_item_hide(pc->cl1);
1242     if (pc->green_anchor) {
1243         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1244     }
1247     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1249     pen_enable_events(pc);
1252 static void
1253 pen_disable_events(SPPenContext *const pc) {
1254   pc->events_disabled++;
1257 static void
1258 pen_enable_events(SPPenContext *const pc) {
1259   g_return_if_fail(pc->events_disabled != 0);
1260   
1261   pc->events_disabled--;
1264 /*
1265   Local Variables:
1266   mode:c++
1267   c-file-style:"stroustrup"
1268   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1269   indent-tabs-mode:nil
1270   fill-column:99
1271   End:
1272 */
1273 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :