Code

Ctrl+click in pen/pencil tool: Created dots are now selected; Alt is used for randomn...
[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 "selection-chemistry.h"
27 #include "draw-anchor.h"
28 #include "message-stack.h"
29 #include "message-context.h"
30 #include "prefs-utils.h"
31 #include "sp-path.h"
33 #include "pixmaps/cursor-pen.xpm"
34 #include "display/canvas-bpath.h"
35 #include "display/sp-ctrlline.h"
36 #include "display/sodipodi-ctrl.h"
37 #include <glibmm/i18n.h>
38 #include "libnr/n-art-bpath.h"
39 #include "helper/units.h"
40 #include "macros.h"
41 #include "context-fns.h"
44 static void sp_pen_context_class_init(SPPenContextClass *klass);
45 static void sp_pen_context_init(SPPenContext *pc);
46 static void sp_pen_context_dispose(GObject *object);
48 static void sp_pen_context_setup(SPEventContext *ec);
49 static void sp_pen_context_finish(SPEventContext *ec);
50 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
51 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
52 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
54 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
55 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
56 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
57 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
59 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
61 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
62 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
63 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
64 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
65 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
66 static void spdc_reset_colors(SPPenContext *pc);
68 static void pen_disable_events(SPPenContext *const pc);
69 static void pen_enable_events(SPPenContext *const pc);
71 static NR::Point pen_drag_origin_w(0, 0);
72 static bool pen_within_tolerance = false;
74 static SPDrawContextClass *pen_parent_class;
76 /**
77  * Register SPPenContext with Gdk and return its type.
78  */
79 GType
80 sp_pen_context_get_type(void)
81 {
82     static GType type = 0;
83     if (!type) {
84         GTypeInfo info = {
85             sizeof(SPPenContextClass),
86             NULL, NULL,
87             (GClassInitFunc) sp_pen_context_class_init,
88             NULL, NULL,
89             sizeof(SPPenContext),
90             4,
91             (GInstanceInitFunc) sp_pen_context_init,
92             NULL,   /* value_table */
93         };
94         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
95     }
96     return type;
97 }
99 /**
100  * Initialize the SPPenContext vtable.
101  */
102 static void
103 sp_pen_context_class_init(SPPenContextClass *klass)
105     GObjectClass *object_class;
106     SPEventContextClass *event_context_class;
108     object_class = (GObjectClass *) klass;
109     event_context_class = (SPEventContextClass *) klass;
111     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
113     object_class->dispose = sp_pen_context_dispose;
115     event_context_class->setup = sp_pen_context_setup;
116     event_context_class->finish = sp_pen_context_finish;
117     event_context_class->set = sp_pen_context_set;
118     event_context_class->root_handler = sp_pen_context_root_handler;
119     event_context_class->item_handler = sp_pen_context_item_handler;
122 /**
123  * Callback to initialize SPPenContext object.
124  */
125 static void
126 sp_pen_context_init(SPPenContext *pc)
129     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
131     event_context->cursor_shape = cursor_pen_xpm;
132     event_context->hot_x = 4;
133     event_context->hot_y = 4;
135     pc->npoints = 0;
136     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
137     pc->state = SP_PEN_CONTEXT_POINT;
139     pc->c0 = NULL;
140     pc->c1 = NULL;
141     pc->cl0 = NULL;
142     pc->cl1 = NULL;
143     
144     pc->events_disabled = 0;
147 /**
148  * Callback to destroy the SPPenContext object's members and itself.
149  */
150 static void
151 sp_pen_context_dispose(GObject *object)
153     SPPenContext *pc;
155     pc = SP_PEN_CONTEXT(object);
157     if (pc->c0) {
158         gtk_object_destroy(GTK_OBJECT(pc->c0));
159         pc->c0 = NULL;
160     }
161     if (pc->c1) {
162         gtk_object_destroy(GTK_OBJECT(pc->c1));
163         pc->c1 = NULL;
164     }
165     if (pc->cl0) {
166         gtk_object_destroy(GTK_OBJECT(pc->cl0));
167         pc->cl0 = NULL;
168     }
169     if (pc->cl1) {
170         gtk_object_destroy(GTK_OBJECT(pc->cl1));
171         pc->cl1 = NULL;
172     }
174     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
177 /**
178  * Callback to initialize SPPenContext object.
179  */
180 static void
181 sp_pen_context_setup(SPEventContext *ec)
183     SPPenContext *pc;
185     pc = SP_PEN_CONTEXT(ec);
187     if (((SPEventContextClass *) pen_parent_class)->setup) {
188         ((SPEventContextClass *) pen_parent_class)->setup(ec);
189     }
191     /* Pen indicators */
192     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
193                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
194     pc->c1 = 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->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
197     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
198     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
199     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
201     sp_canvas_item_hide(pc->c0);
202     sp_canvas_item_hide(pc->c1);
203     sp_canvas_item_hide(pc->cl0);
204     sp_canvas_item_hide(pc->cl1);
206     sp_event_context_read(ec, "mode");
208     pc->anchor_statusbar = false;
210     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
211         ec->enableSelectionCue();
212     }
215 static void
216 pen_cancel (SPPenContext *const pc) 
218     pc->state = SP_PEN_CONTEXT_STOP;
219     spdc_reset_colors(pc);
220     sp_canvas_item_hide(pc->c0);
221     sp_canvas_item_hide(pc->c1);
222     sp_canvas_item_hide(pc->cl0);
223     sp_canvas_item_hide(pc->cl1);
224     pc->_message_context->clear();
225     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
227     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
230 /**
231  * Finalization callback.
232  */
233 static void
234 sp_pen_context_finish(SPEventContext *ec)
236     SPPenContext *pc = SP_PEN_CONTEXT(ec);
238     if (pc->npoints != 0) {
239         pen_cancel (pc);
240     }
242     if (((SPEventContextClass *) pen_parent_class)->finish) {
243         ((SPEventContextClass *) pen_parent_class)->finish(ec);
244     }
247 /**
248  * Callback that sets key to value in pen context.
249  */
250 static void
251 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
253     SPPenContext *pc = SP_PEN_CONTEXT(ec);
255     if (!strcmp(key, "mode")) {
256         if ( val && !strcmp(val, "drag") ) {
257             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
258         } else {
259             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
260         }
261     }
264 /**
265  * Snaps new node relative to the previous node.
266  */
267 static void
268 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
270     if (pc->npoints > 0) {
271         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
272     }
274     spdc_endpoint_snap_free(pc, p, state);
277 /**
278  * Snaps new node's handle relative to the new node.
279  */
280 static void
281 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
283     g_return_if_fail(( pc->npoints == 2 ||
284                        pc->npoints == 5   ));
286     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
287     spdc_endpoint_snap_free(pc, p, state);
290 static gint 
291 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, 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;
301         default:
302             break;
303     }
305     if (!ret) {
306         if (((SPEventContextClass *) pen_parent_class)->item_handler)
307             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
308     }
310     return ret;
313 /**
314  * Callback to handle all pen events.
315  */
316 static gint
317 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
319     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
321     gint ret = FALSE;
323     switch (event->type) {
324         case GDK_BUTTON_PRESS:
325             ret = pen_handle_button_press(pc, event->button);
326             break;
328         case GDK_MOTION_NOTIFY:
329             ret = pen_handle_motion_notify(pc, event->motion);
330             break;
332         case GDK_BUTTON_RELEASE:
333             ret = pen_handle_button_release(pc, event->button);
334             break;
336         case GDK_2BUTTON_PRESS:
337             ret = pen_handle_2button_press(pc, event->button);
338             break;
340         case GDK_KEY_PRESS:
341             ret = pen_handle_key_press(pc, event);
342             break;
344         default:
345             break;
346     }
348     if (!ret) {
349         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
350             = ((SPEventContextClass *) pen_parent_class)->root_handler;
351         if (parent_root_handler) {
352             ret = parent_root_handler(ec, event);
353         }
354     }
356     return ret;
359 /**
360  * Handle mouse button press event.
361  */
362 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
364     if (pc->events_disabled) {
365         // skip event processing if events are disabled
366         return FALSE;
367     }
369     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
370     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
371     NR::Point const event_w(bevent.x, bevent.y);
372     NR::Point const event_dt(desktop->w2d(event_w));
373     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
375     gint ret = FALSE;
376     if (bevent.button == 1 && !event_context->space_panning) {
378         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
379             return TRUE;
380         }
382         pen_drag_origin_w = event_w;
383         pen_within_tolerance = true;
385         /* Test whether we hit any anchor. */
386         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
388         switch (pc->mode) {
389             case SP_PEN_CONTEXT_MODE_CLICK:
390                 /* In click mode we add point on release */
391                 switch (pc->state) {
392                     case SP_PEN_CONTEXT_POINT:
393                     case SP_PEN_CONTEXT_CONTROL:
394                     case SP_PEN_CONTEXT_CLOSE:
395                         break;
396                     case SP_PEN_CONTEXT_STOP:
397                         /* This is allowed, if we just cancelled curve */
398                         pc->state = SP_PEN_CONTEXT_POINT;
399                         break;
400                     default:
401                         break;
402                 }
403                 break;
404             case SP_PEN_CONTEXT_MODE_DRAG:
405                 switch (pc->state) {
406                     case SP_PEN_CONTEXT_STOP:
407                         /* This is allowed, if we just cancelled curve */
408                     case SP_PEN_CONTEXT_POINT:
409                         if (pc->npoints == 0) {
411                             if (bevent.state & GDK_CONTROL_MASK) {
412                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
413                                 ret = TRUE;
414                                 break;
415                             }
417                             /* Set start anchor */
418                             pc->sa = anchor;
419                             NR::Point p;
420                             if (anchor) {
422                                 /* Adjust point to anchor if needed */
423                                 p = anchor->dp;
424                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
426                             } else {
428                                 // This is the first click of a new curve; deselect item so that
429                                 // this curve is not combined with it (unless it is drawn from its
430                                 // anchor, which is handled by the sibling branch above)
431                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
432                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
434                                     selection->clear();
435                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
437                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
439                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
440                                 }
442                                 /* Create green anchor */
443                                 p = event_dt;
444                                 spdc_endpoint_snap(pc, p, bevent.state);
445                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
446                             }
447                             spdc_pen_set_initial_point(pc, p);
448                         } else {
450                             /* Set end anchor */
451                             pc->ea = anchor;
452                             NR::Point p;
453                             if (anchor) {
455                                 p = anchor->dp;
456                                 // we hit an anchor, will finish the curve (either with or without closing)
457                                 // in release handler
458                                 pc->state = SP_PEN_CONTEXT_CLOSE;
460                                 if (pc->green_anchor && pc->green_anchor->active) {
461                                     // we clicked on the current curve start, so close it even if
462                                     // we drag a handle away from it
463                                     dc->green_closed = TRUE;
464                                 }
465                                 ret = TRUE;
466                                 break;
468                             } else {
470                                 p = event_dt;
471                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
472                                 spdc_pen_set_subsequent_point(pc, p, true);
473                             }
475                         }
476                         pc->state = SP_PEN_CONTEXT_CONTROL;
477                         ret = TRUE;
478                         break;
479                     case SP_PEN_CONTEXT_CONTROL:
480                         g_warning("Button down in CONTROL state");
481                         break;
482                     case SP_PEN_CONTEXT_CLOSE:
483                         g_warning("Button down in CLOSE state");
484                         break;
485                     default:
486                         break;
487                 }
488                 break;
489             default:
490                 break;
491         }
492     } else if (bevent.button == 3) {
493         if (pc->npoints != 0) {
495             spdc_pen_finish_segment(pc, event_dt, bevent.state);
496             if (pc->green_closed) {
497                 // finishing at the start anchor, close curve
498                 spdc_pen_finish(pc, TRUE);
499             } else {
500                 // finishing at some other anchor, finish curve but not close
501                 spdc_pen_finish(pc, FALSE);
502             }
504             ret = TRUE;
505         }
506     }
508     return ret;
511 /**
512  * Handle motion_notify event.
513  */
514 static gint
515 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
517     gint ret = FALSE;
519     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
521     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
522         // allow scrolling
523         return FALSE;
524     }
525     
526     if (pc->events_disabled) {
527         // skip motion events if pen events are disabled
528         return FALSE;
529     }
531     NR::Point const event_w(mevent.x,
532                             mevent.y);
533     if (pen_within_tolerance) {
534         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
535                                                                "value", 0, 0, 100);
536         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
537             return FALSE;   // Do not drag if we're within tolerance from origin.
538         }
539     }
540     // Once the user has moved farther than tolerance from the original location
541     // (indicating they intend to move the object, not click), then always process the
542     // motion notify coordinates as given (no snapping back to origin)
543     pen_within_tolerance = false;
545     SPDesktop *const dt = pc->desktop;
546     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
547         /* Grab mouse, so release will not pass unnoticed */
548         pc->grab = SP_CANVAS_ITEM(dt->acetate);
549         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
550                                         GDK_BUTTON_RELEASE_MASK |
551                                         GDK_POINTER_MOTION_MASK  ),
552                             NULL, mevent.time);
553     }
555     /* Find desktop coordinates */
556     NR::Point p = dt->w2d(event_w);
558     /* Test, whether we hit any anchor */
559     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
561     switch (pc->mode) {
562         case SP_PEN_CONTEXT_MODE_CLICK:
563             switch (pc->state) {
564                 case SP_PEN_CONTEXT_POINT:
565                     if ( pc->npoints != 0 ) {
566                         /* Only set point, if we are already appending */
567                         spdc_endpoint_snap(pc, p, mevent.state);
568                         spdc_pen_set_subsequent_point(pc, p, true);
569                         ret = TRUE;
570                     }
571                     break;
572                 case SP_PEN_CONTEXT_CONTROL:
573                 case SP_PEN_CONTEXT_CLOSE:
574                     /* Placing controls is last operation in CLOSE state */
575                     spdc_endpoint_snap(pc, p, mevent.state);
576                     spdc_pen_set_ctrl(pc, p, mevent.state);
577                     ret = TRUE;
578                     break;
579                 case SP_PEN_CONTEXT_STOP:
580                     /* This is perfectly valid */
581                     break;
582                 default:
583                     break;
584             }
585             break;
586         case SP_PEN_CONTEXT_MODE_DRAG:
587             switch (pc->state) {
588                 case SP_PEN_CONTEXT_POINT:
589                     if ( pc->npoints > 0 ) {
590                         /* Only set point, if we are already appending */
592                         if (!anchor) {   /* Snap node only if not hitting anchor */
593                             spdc_endpoint_snap(pc, p, mevent.state);
594                         }
596                         spdc_pen_set_subsequent_point(pc, p, !anchor);
598                         if (anchor && !pc->anchor_statusbar) {
599                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
600                             pc->anchor_statusbar = true;
601                         } else if (!anchor && pc->anchor_statusbar) {
602                             pc->_message_context->clear();
603                             pc->anchor_statusbar = false;
604                         }
606                         ret = TRUE;
607                     } else {
608                         if (anchor && !pc->anchor_statusbar) {
609                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
610                             pc->anchor_statusbar = true;
611                         } else if (!anchor && pc->anchor_statusbar) {
612                             pc->_message_context->clear();
613                             pc->anchor_statusbar = false;
614                         }
615                     }
616                     break;
617                 case SP_PEN_CONTEXT_CONTROL:
618                 case SP_PEN_CONTEXT_CLOSE:
619                     /* Placing controls is last operation in CLOSE state */
621                     // snap the handle
622                     spdc_endpoint_snap_handle(pc, p, mevent.state);
624                     spdc_pen_set_ctrl(pc, p, mevent.state);
625                     gobble_motion_events(GDK_BUTTON1_MASK);
626                     ret = TRUE;
627                     break;
628                 case SP_PEN_CONTEXT_STOP:
629                     /* This is perfectly valid */
630                     break;
631                 default:
632                     break;
633             }
634             break;
635         default:
636             break;
637     }
638     return ret;
641 /**
642  * Handle mouse button release event.
643  */
644 static gint
645 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
647     if (pc->events_disabled) {
648         // skip event processing if events are disabled
649         return FALSE;
650     }
652     gint ret = FALSE;
653     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
654     if ( revent.button == 1  && !event_context->space_panning) {
656         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
658         NR::Point const event_w(revent.x,
659                                 revent.y);
660         /* Find desktop coordinates */
661         NR::Point p = pc->desktop->w2d(event_w);
663         /* Test whether we hit any anchor. */
664         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
666         switch (pc->mode) {
667             case SP_PEN_CONTEXT_MODE_CLICK:
668                 switch (pc->state) {
669                     case SP_PEN_CONTEXT_POINT:
670                         if ( pc->npoints == 0 ) {
671                             /* Start new thread only with button release */
672                             if (anchor) {
673                                 p = anchor->dp;
674                             }
675                             pc->sa = anchor;
676                             spdc_pen_set_initial_point(pc, p);
677                         } else {
678                             /* Set end anchor here */
679                             pc->ea = anchor;
680                             if (anchor) {
681                                 p = anchor->dp;
682                             }
683                         }
684                         pc->state = SP_PEN_CONTEXT_CONTROL;
685                         ret = TRUE;
686                         break;
687                     case SP_PEN_CONTEXT_CONTROL:
688                         /* End current segment */
689                         spdc_endpoint_snap(pc, p, revent.state);
690                         spdc_pen_finish_segment(pc, p, revent.state);
691                         pc->state = SP_PEN_CONTEXT_POINT;
692                         ret = TRUE;
693                         break;
694                     case SP_PEN_CONTEXT_CLOSE:
695                         /* End current segment */
696                         if (!anchor) {   /* Snap node only if not hitting anchor */
697                             spdc_endpoint_snap(pc, p, revent.state);
698                         }
699                         spdc_pen_finish_segment(pc, p, revent.state);
700                         spdc_pen_finish(pc, TRUE);
701                         pc->state = SP_PEN_CONTEXT_POINT;
702                         ret = TRUE;
703                         break;
704                     case SP_PEN_CONTEXT_STOP:
705                         /* This is allowed, if we just cancelled curve */
706                         pc->state = SP_PEN_CONTEXT_POINT;
707                         ret = TRUE;
708                         break;
709                     default:
710                         break;
711                 }
712                 break;
713             case SP_PEN_CONTEXT_MODE_DRAG:
714                 switch (pc->state) {
715                     case SP_PEN_CONTEXT_POINT:
716                     case SP_PEN_CONTEXT_CONTROL:
717                         spdc_endpoint_snap(pc, p, revent.state);
718                         spdc_pen_finish_segment(pc, p, revent.state);
719                         break;
720                     case SP_PEN_CONTEXT_CLOSE:
721                         spdc_endpoint_snap(pc, p, revent.state);
722                         spdc_pen_finish_segment(pc, p, revent.state);
723                         if (pc->green_closed) {
724                             // finishing at the start anchor, close curve
725                             spdc_pen_finish(pc, TRUE);
726                         } else {
727                             // finishing at some other anchor, finish curve but not close
728                             spdc_pen_finish(pc, FALSE);
729                         }
730                         break;
731                     case SP_PEN_CONTEXT_STOP:
732                         /* This is allowed, if we just cancelled curve */
733                         break;
734                     default:
735                         break;
736                 }
737                 pc->state = SP_PEN_CONTEXT_POINT;
738                 ret = TRUE;
739                 break;
740             default:
741                 break;
742         }
744         if (pc->grab) {
745             /* Release grab now */
746             sp_canvas_item_ungrab(pc->grab, revent.time);
747             pc->grab = NULL;
748         }
750         ret = TRUE;
752         dc->green_closed = FALSE;
753     }
755     return ret;
758 static gint
759 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
761     gint ret = FALSE;
762     if (pc->npoints != 0 && bevent.button != 2) {
763         spdc_pen_finish(pc, FALSE);
764         ret = TRUE;
765     }
766     return ret;
769 void
770 pen_redraw_all (SPPenContext *const pc)
772     // green
773     if (pc->green_bpaths) {
774         // remove old piecewise green canvasitems
775         while (pc->green_bpaths) {
776             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
777             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
778         }
779         // one canvas bpath for all of green_curve
780         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
781         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
782         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
784         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
785     }
787     if (pc->green_anchor)
788         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
790     sp_curve_reset(pc->red_curve);
791     sp_curve_moveto(pc->red_curve, pc->p[0]);
792     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
793     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
795     // handles
796     if (pc->p[0] != pc->p[1]) {
797         SP_CTRL(pc->c1)->moveto(pc->p[1]);
798         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
799         sp_canvas_item_show (pc->c1);
800         sp_canvas_item_show (pc->cl1);
801     } else {
802         sp_canvas_item_hide (pc->c1);
803         sp_canvas_item_hide (pc->cl1);
804     }
806     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
807     if (bpath) {
808         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
809             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
810             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
811             sp_canvas_item_show (pc->c0);
812             sp_canvas_item_show (pc->cl0);
813         } else {
814             sp_canvas_item_hide (pc->c0);
815             sp_canvas_item_hide (pc->cl0);
816         }
817     }
820 void
821 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
823     if (pc->npoints != 5)
824         return;
826     // green
827     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
828     if (bpath) {
829         if (bpath->code == NR_CURVETO) {
830             bpath->x2 += x;
831             bpath->y2 += y;
832         }
833         bpath->x3 += x;
834         bpath->y3 += y;
835     } else {
836         // start anchor too
837         if (pc->green_anchor) {
838             pc->green_anchor->dp += NR::Point(x, y);
839         }
840     }
842     // red
843     pc->p[0] += NR::Point(x, y);
844     pc->p[1] += NR::Point(x, y);
845     pen_redraw_all(pc);
848 void
849 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
851     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
854 void
855 pen_lastpoint_tocurve (SPPenContext *const pc)
857     if (pc->npoints != 5)
858         return;
860     // red
861     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
862     if (bpath && bpath->code == NR_CURVETO) {
863         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
864     } else {
865         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
866     }
868     pen_redraw_all(pc);
871 void
872 pen_lastpoint_toline (SPPenContext *const pc)
874     if (pc->npoints != 5)
875         return;
877     pc->p[1] = pc->p[0];
879     pen_redraw_all(pc);
883 static gint
884 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
886     gint ret = FALSE;
887     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
889     switch (get_group0_keyval (&event->key)) {
891         case GDK_Left: // move last point left
892         case GDK_KP_Left:
893         case GDK_KP_4:
894             if (!MOD__CTRL) { // not ctrl
895                 if (MOD__ALT) { // alt
896                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
897                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
898                 }
899                 else { // no alt
900                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
901                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
902                 }
903                 ret = TRUE;
904             }
905             break;
906         case GDK_Up: // move last point up
907         case GDK_KP_Up:
908         case GDK_KP_8:
909             if (!MOD__CTRL) { // not ctrl
910                 if (MOD__ALT) { // alt
911                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
912                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
913                 }
914                 else { // no alt
915                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
916                     else pen_lastpoint_move(pc, 0, nudge); // no shift
917                 }
918                 ret = TRUE;
919             }
920             break;
921         case GDK_Right: // move last point right
922         case GDK_KP_Right:
923         case GDK_KP_6:
924             if (!MOD__CTRL) { // not ctrl
925                 if (MOD__ALT) { // alt
926                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
927                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
928                 }
929                 else { // no alt
930                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
931                     else pen_lastpoint_move(pc, nudge, 0); // no shift
932                 }
933                 ret = TRUE;
934             }
935             break;
936         case GDK_Down: // move last point down
937         case GDK_KP_Down:
938         case GDK_KP_2:
939             if (!MOD__CTRL) { // not ctrl
940                 if (MOD__ALT) { // alt
941                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
942                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
943                 }
944                 else { // no alt
945                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
946                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
947                 }
948                 ret = TRUE;
949             }
950             break;
952         case GDK_U:
953         case GDK_u:
954             if (MOD__SHIFT_ONLY) {
955                 pen_lastpoint_tocurve(pc);
956                 ret = TRUE;
957             }
958             break;
959         case GDK_L:
960         case GDK_l:
961             if (MOD__SHIFT_ONLY) {
962                 pen_lastpoint_toline(pc);
963                 ret = TRUE;
964             }
965             break;
967         case GDK_Return:
968         case GDK_KP_Enter:
969             if (pc->npoints != 0) {
970                 spdc_pen_finish(pc, FALSE);
971                 ret = TRUE;
972             }
973             break;
974         case GDK_Escape:
975             if (pc->npoints != 0) {
976                 // if drawing, cancel, otherwise pass it up for deselecting
977                 pen_cancel (pc);
978                 ret = TRUE;
979             }
980             break;
981         case GDK_z:
982         case GDK_Z:
983             if (MOD__CTRL_ONLY && pc->npoints != 0) {
984                 // if drawing, cancel, otherwise pass it up for undo
985                 pen_cancel (pc);
986                 ret = TRUE;
987             }
988             break;
989         case GDK_g:
990         case GDK_G:
991             if (MOD__SHIFT_ONLY) {
992                 sp_selection_to_guides();
993                 ret = true;
994             }
995             break;
996         case GDK_BackSpace:
997         case GDK_Delete:
998         case GDK_KP_Delete:
999             if (sp_curve_is_empty(pc->green_curve)) {
1000                 pen_cancel (pc);
1001                 ret = TRUE;
1002             } else {
1003                 /* Reset red curve */
1004                 sp_curve_reset(pc->red_curve);
1005                 /* Destroy topmost green bpath */
1006                 if (pc->green_bpaths) {
1007                     if (pc->green_bpaths->data)
1008                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1009                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1010                 }
1011                 /* Get last segment */
1012                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1013                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1014                 if ( e < 2 ) {
1015                     g_warning("Green curve length is %d", e);
1016                     break;
1017                 }
1018                 pc->p[0] = p[e - 2].c(3);
1019                 if (p[e - 1].code == NR_CURVETO) {
1020                     pc->p[1] = p[e - 1].c(1);
1021                 } else {
1022                     pc->p[1] = pc->p[0];
1023                 }
1024                 NR::Point const pt(( pc->npoints < 4
1025                                      ? p[e - 1].c(3)
1026                                      : pc->p[3] ));
1027                 pc->npoints = 2;
1028                 sp_curve_backspace(pc->green_curve);
1029                 sp_canvas_item_hide(pc->c0);
1030                 sp_canvas_item_hide(pc->c1);
1031                 sp_canvas_item_hide(pc->cl0);
1032                 sp_canvas_item_hide(pc->cl1);
1033                 pc->state = SP_PEN_CONTEXT_POINT;
1034                 spdc_pen_set_subsequent_point(pc, pt, true);
1035                 ret = TRUE;
1036             }
1037             break;
1038         default:
1039             break;
1040     }
1041     return ret;
1044 static void
1045 spdc_reset_colors(SPPenContext *pc)
1047     /* Red */
1048     sp_curve_reset(pc->red_curve);
1049     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1050     /* Blue */
1051     sp_curve_reset(pc->blue_curve);
1052     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1053     /* Green */
1054     while (pc->green_bpaths) {
1055         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1056         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1057     }
1058     sp_curve_reset(pc->green_curve);
1059     if (pc->green_anchor) {
1060         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1061     }
1062     pc->sa = NULL;
1063     pc->ea = NULL;
1064     pc->npoints = 0;
1065     pc->red_curve_is_valid = false;
1069 static void
1070 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1072     g_assert( pc->npoints == 0 );
1074     pc->p[0] = p;
1075     pc->p[1] = p;
1076     pc->npoints = 2;
1077     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1079     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1082 static void
1083 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1085     g_assert( pc->npoints != 0 );
1086     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1088     pc->p[2] = p;
1089     pc->p[3] = p;
1090     pc->p[4] = p;
1091     pc->npoints = 5;
1092     sp_curve_reset(pc->red_curve);
1093     sp_curve_moveto(pc->red_curve, pc->p[0]);
1094     bool is_curve;
1095     if ( (pc->onlycurves)
1096          || ( pc->p[1] != pc->p[0] ) )
1097     {
1098         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1099         is_curve = true;
1100     } else {
1101         sp_curve_lineto(pc->red_curve, p);
1102         is_curve = false;
1103     }
1105     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1107     if (statusbar) {
1108         // status text
1109         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1110         NR::Point rel = p - pc->p[0];
1111         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1112         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1113         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1114             angle = angle_to_compass (angle);
1115         pc->_message_context->setF(Inkscape::IMMEDIATE_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);
1116         g_string_free(dist, FALSE);
1117     }
1120 static void
1121 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1123     sp_canvas_item_show(pc->c1);
1124     sp_canvas_item_show(pc->cl1);
1126     if ( pc->npoints == 2 ) {
1127         pc->p[1] = p;
1128         sp_canvas_item_hide(pc->c0);
1129         sp_canvas_item_hide(pc->cl0);
1130         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1131         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1133         // status text
1134         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1135         NR::Point rel = p - pc->p[0];
1136         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1137         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1138         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1139             angle = angle_to_compass (angle);
1140         pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1141         g_string_free(dist, FALSE);
1143     } else if ( pc->npoints == 5 ) {
1144         pc->p[4] = p;
1145         sp_canvas_item_show(pc->c0);
1146         sp_canvas_item_show(pc->cl0);
1147         bool is_symm = false;
1148         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1149              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1150             NR::Point delta = p - pc->p[3];
1151             pc->p[2] = pc->p[3] - delta;
1152             is_symm = true;
1153             sp_curve_reset(pc->red_curve);
1154             sp_curve_moveto(pc->red_curve, pc->p[0]);
1155             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1156             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1157         }
1158         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1159         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1160         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1161         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1163         // status text
1164         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1165         NR::Point rel = p - pc->p[3];
1166         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1167         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1168         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1169             angle = angle_to_compass (angle);
1170         pc->_message_context->setF(Inkscape::IMMEDIATE_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);
1171         g_string_free(dist, FALSE);
1173     } else {
1174         g_warning("Something bad happened - npoints is %d", pc->npoints);
1175     }
1178 static void
1179 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1181     if (!sp_curve_empty(pc->red_curve)) {
1182         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1183         SPCurve *curve = sp_curve_copy(pc->red_curve);
1184         /// \todo fixme:
1185         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1186         sp_curve_unref(curve);
1187         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1189         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1191         pc->p[0] = pc->p[3];
1192         pc->p[1] = pc->p[4];
1193         pc->npoints = 2;
1195         sp_curve_reset(pc->red_curve);
1196     }
1199 static void
1200 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1202     pen_disable_events(pc);
1203     
1204     SPDesktop *const desktop = pc->desktop;
1205     pc->_message_context->clear();
1206     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1208     sp_curve_reset(pc->red_curve);
1209     spdc_concat_colors_and_flush(pc, closed);
1210     pc->sa = NULL;
1211     pc->ea = NULL;
1213     pc->npoints = 0;
1214     pc->state = SP_PEN_CONTEXT_POINT;
1216     sp_canvas_item_hide(pc->c0);
1217     sp_canvas_item_hide(pc->c1);
1218     sp_canvas_item_hide(pc->cl0);
1219     sp_canvas_item_hide(pc->cl1);
1221     if (pc->green_anchor) {
1222         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1223     }
1226     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1228     pen_enable_events(pc);
1231 static void
1232 pen_disable_events(SPPenContext *const pc) {
1233   pc->events_disabled++;
1236 static void
1237 pen_enable_events(SPPenContext *const pc) {
1238   g_return_if_fail(pc->events_disabled != 0);
1239   
1240   pc->events_disabled--;
1243 /*
1244   Local Variables:
1245   mode:c++
1246   c-file-style:"stroustrup"
1247   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1248   indent-tabs-mode:nil
1249   fill-column:99
1250   End:
1251 */
1252 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :