Code

switch to sigc++ signal for "release"
[inkscape.git] / src / pen-context.cpp
1 /** \file
2  * Pen event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
20 #include "pen-context.h"
21 #include "sp-namedview.h"
22 #include "sp-metrics.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "draw-anchor.h"
27 #include "message-stack.h"
28 #include "message-context.h"
29 #include "prefs-utils.h"
30 #include "sp-path.h"
32 #include "pixmaps/cursor-pen.xpm"
33 #include "pixmaps/cursor-pen.pixbuf"
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);
53 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
54 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
55 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
56 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
58 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
60 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
61 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
62 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
63 static gint pen_handle_2button_press(SPPenContext *const pc);
64 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
65 static void spdc_reset_colors(SPPenContext *pc);
68 static NR::Point pen_drag_origin_w(0, 0);
69 static bool pen_within_tolerance = false;
71 static SPDrawContextClass *pen_parent_class;
73 /**
74  * Register SPPenContext with Gdk and return its type.
75  */
76 GType
77 sp_pen_context_get_type(void)
78 {
79     static GType type = 0;
80     if (!type) {
81         GTypeInfo info = {
82             sizeof(SPPenContextClass),
83             NULL, NULL,
84             (GClassInitFunc) sp_pen_context_class_init,
85             NULL, NULL,
86             sizeof(SPPenContext),
87             4,
88             (GInstanceInitFunc) sp_pen_context_init,
89             NULL,   /* value_table */
90         };
91         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
92     }
93     return type;
94 }
96 /**
97  * Initialize the SPPenContext vtable.
98  */
99 static void
100 sp_pen_context_class_init(SPPenContextClass *klass)
102     GObjectClass *object_class;
103     SPEventContextClass *event_context_class;
105     object_class = (GObjectClass *) klass;
106     event_context_class = (SPEventContextClass *) klass;
108     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
110     object_class->dispose = sp_pen_context_dispose;
112     event_context_class->setup = sp_pen_context_setup;
113     event_context_class->finish = sp_pen_context_finish;
114     event_context_class->set = sp_pen_context_set;
115     event_context_class->root_handler = sp_pen_context_root_handler;
118 /**
119  * Callback to initialize SPPenContext object.
120  */
121 static void
122 sp_pen_context_init(SPPenContext *pc)
125     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
127     event_context->cursor_shape = cursor_pen_xpm;
128     event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
129             -1,
130             cursor_pen_pixbuf,
131             FALSE,
132             NULL);  
133     event_context->hot_x = 4;
134     event_context->hot_y = 4;
136     pc->npoints = 0;
137     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
138     pc->state = SP_PEN_CONTEXT_POINT;
140     pc->c0 = NULL;
141     pc->c1 = NULL;
142     pc->cl0 = NULL;
143     pc->cl1 = NULL;
146 /**
147  * Callback to destroy the SPPenContext object's members and itself.
148  */
149 static void
150 sp_pen_context_dispose(GObject *object)
152     SPPenContext *pc;
154     pc = SP_PEN_CONTEXT(object);
156     if (pc->c0) {
157         gtk_object_destroy(GTK_OBJECT(pc->c0));
158         pc->c0 = NULL;
159     }
160     if (pc->c1) {
161         gtk_object_destroy(GTK_OBJECT(pc->c1));
162         pc->c1 = NULL;
163     }
164     if (pc->cl0) {
165         gtk_object_destroy(GTK_OBJECT(pc->cl0));
166         pc->cl0 = NULL;
167     }
168     if (pc->cl1) {
169         gtk_object_destroy(GTK_OBJECT(pc->cl1));
170         pc->cl1 = NULL;
171     }
173     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
176 /**
177  * Callback to initialize SPPenContext object.
178  */
179 static void
180 sp_pen_context_setup(SPEventContext *ec)
182     SPPenContext *pc;
184     pc = SP_PEN_CONTEXT(ec);
186     if (((SPEventContextClass *) pen_parent_class)->setup) {
187         ((SPEventContextClass *) pen_parent_class)->setup(ec);
188     }
190     /* Pen indicators */
191     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
192                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
193     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
194                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
195     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
196     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
197     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
198     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
200     sp_canvas_item_hide(pc->c0);
201     sp_canvas_item_hide(pc->c1);
202     sp_canvas_item_hide(pc->cl0);
203     sp_canvas_item_hide(pc->cl1);
205     sp_event_context_read(ec, "mode");
207     pc->anchor_statusbar = false;
209     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
210         ec->enableSelectionCue();
211     }
214 static void
215 pen_cancel (SPPenContext *const pc) 
217     pc->state = SP_PEN_CONTEXT_STOP;
218     spdc_reset_colors(pc);
219     sp_canvas_item_hide(pc->c0);
220     sp_canvas_item_hide(pc->c1);
221     sp_canvas_item_hide(pc->cl0);
222     sp_canvas_item_hide(pc->cl1);
223     pc->_message_context->clear();
224     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
227 /**
228  * Finalization callback.
229  */
230 static void
231 sp_pen_context_finish(SPEventContext *ec)
233     SPPenContext *pc = SP_PEN_CONTEXT(ec);
235     if (pc->npoints != 0) {
236         pen_cancel (pc);
237     }
239     if (((SPEventContextClass *) pen_parent_class)->finish) {
240         ((SPEventContextClass *) pen_parent_class)->finish(ec);
241     }
244 /**
245  * Callback that sets key to value in pen context.
246  */
247 static void
248 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
250     SPPenContext *pc = SP_PEN_CONTEXT(ec);
252     if (!strcmp(key, "mode")) {
253         if ( val && !strcmp(val, "drag") ) {
254             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
255         } else {
256             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
257         }
258     }
261 /**
262  * Snaps new node relative to the previous node.
263  */
264 static void
265 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
267     if (pc->npoints > 0) {
268         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
269     }
271     spdc_endpoint_snap_free(pc, p, state);
274 /**
275  * Snaps new node's handle relative to the new node.
276  */
277 static void
278 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
280     g_return_if_fail(( pc->npoints == 2 ||
281                        pc->npoints == 5   ));
283     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
284     spdc_endpoint_snap_free(pc, p, state);
287 /**
288  * Callback to handle all pen events.
289  */
290 static gint
291 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
293     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
295     gint ret = FALSE;
297     switch (event->type) {
298         case GDK_BUTTON_PRESS:
299             ret = pen_handle_button_press(pc, event->button);
300             break;
302         case GDK_MOTION_NOTIFY:
303             ret = pen_handle_motion_notify(pc, event->motion);
304             break;
306         case GDK_BUTTON_RELEASE:
307             ret = pen_handle_button_release(pc, event->button);
308             break;
310         case GDK_2BUTTON_PRESS:
311             ret = pen_handle_2button_press(pc);
312             break;
314         case GDK_KEY_PRESS:
315             ret = pen_handle_key_press(pc, event);
316             break;
318         default:
319             break;
320     }
322     if (!ret) {
323         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
324             = ((SPEventContextClass *) pen_parent_class)->root_handler;
325         if (parent_root_handler) {
326             ret = parent_root_handler(ec, event);
327         }
328     }
330     return ret;
333 /**
334  * Handle mouse button press event.
335  */
336 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
338     gint ret = FALSE;
339     if (bevent.button == 1) {
341         SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
342         SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
344         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
345             return TRUE;
346         }
348         NR::Point const event_w(bevent.x, bevent.y);
349         pen_drag_origin_w = event_w;
350         pen_within_tolerance = true;
352         /* Test whether we hit any anchor. */
353         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
355         NR::Point const event_dt(desktop->w2d(event_w));
356         switch (pc->mode) {
357             case SP_PEN_CONTEXT_MODE_CLICK:
358                 /* In click mode we add point on release */
359                 switch (pc->state) {
360                     case SP_PEN_CONTEXT_POINT:
361                     case SP_PEN_CONTEXT_CONTROL:
362                     case SP_PEN_CONTEXT_CLOSE:
363                         break;
364                     case SP_PEN_CONTEXT_STOP:
365                         /* This is allowed, if we just cancelled curve */
366                         pc->state = SP_PEN_CONTEXT_POINT;
367                         break;
368                     default:
369                         break;
370                 }
371                 break;
372             case SP_PEN_CONTEXT_MODE_DRAG:
373                 switch (pc->state) {
374                     case SP_PEN_CONTEXT_STOP:
375                         /* This is allowed, if we just cancelled curve */
376                     case SP_PEN_CONTEXT_POINT:
377                         if (pc->npoints == 0) {
379                             /* Set start anchor */
380                             pc->sa = anchor;
381                             NR::Point p;
382                             if (anchor) {
384                                 /* Adjust point to anchor if needed */
385                                 p = anchor->dp;
386                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
388                             } else {
390                                 // This is the first click of a new curve; deselect item so that
391                                 // this curve is not combined with it (unless it is drawn from its
392                                 // anchor, which is handled by the sibling branch above)
393                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
394                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
396                                     selection->clear();
397                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
399                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
401                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
402                                 }
404                                 /* Create green anchor */
405                                 p = event_dt;
406                                 spdc_endpoint_snap(pc, p, bevent.state);
407                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
408                             }
409                             spdc_pen_set_initial_point(pc, p);
410                         } else {
412                             /* Set end anchor */
413                             pc->ea = anchor;
414                             NR::Point p;
415                             if (anchor) {
417                                 p = anchor->dp;
418                                 // we hit an anchor, will finish the curve (either with or without closing)
419                                 // in release handler
420                                 pc->state = SP_PEN_CONTEXT_CLOSE;
422                                 if (pc->green_anchor && pc->green_anchor->active) {
423                                     // we clicked on the current curve start, so close it even if
424                                     // we drag a handle away from it
425                                     dc->green_closed = TRUE;
426                                 }
427                                 ret = TRUE;
428                                 break;
430                             } else {
432                                 p = event_dt;
433                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
434                                 spdc_pen_set_subsequent_point(pc, p, true);
435                             }
437                         }
438                         pc->state = SP_PEN_CONTEXT_CONTROL;
439                         ret = TRUE;
440                         break;
441                     case SP_PEN_CONTEXT_CONTROL:
442                         g_warning("Button down in CONTROL state");
443                         break;
444                     case SP_PEN_CONTEXT_CLOSE:
445                         g_warning("Button down in CLOSE state");
446                         break;
447                     default:
448                         break;
449                 }
450                 break;
451             default:
452                 break;
453         }
454     } else if (bevent.button == 3) {
455         if (pc->npoints != 0) {
456             spdc_pen_finish(pc, FALSE);
457             ret = TRUE;
458         }
459     }
461     return ret;
464 /**
465  * Handle motion_notify event.
466  */
467 static gint
468 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
470     gint ret = FALSE;
472     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
473         // allow middle-button scrolling
474         return FALSE;
475     }
477     NR::Point const event_w(mevent.x,
478                             mevent.y);
479     if (pen_within_tolerance) {
480         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
481                                                                "value", 0, 0, 100);
482         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
483             return FALSE;   // Do not drag if we're within tolerance from origin.
484         }
485     }
486     // Once the user has moved farther than tolerance from the original location
487     // (indicating they intend to move the object, not click), then always process the
488     // motion notify coordinates as given (no snapping back to origin)
489     pen_within_tolerance = false;
491     SPDesktop *const dt = pc->desktop;
492     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
493         /* Grab mouse, so release will not pass unnoticed */
494         pc->grab = SP_CANVAS_ITEM(dt->acetate);
495         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
496                                         GDK_BUTTON_RELEASE_MASK |
497                                         GDK_POINTER_MOTION_MASK  ),
498                             NULL, mevent.time);
499     }
501     /* Find desktop coordinates */
502     NR::Point p = dt->w2d(event_w);
504     /* Test, whether we hit any anchor */
505     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
507     switch (pc->mode) {
508         case SP_PEN_CONTEXT_MODE_CLICK:
509             switch (pc->state) {
510                 case SP_PEN_CONTEXT_POINT:
511                     if ( pc->npoints != 0 ) {
512                         /* Only set point, if we are already appending */
513                         spdc_endpoint_snap(pc, p, mevent.state);
514                         spdc_pen_set_subsequent_point(pc, p, true);
515                         ret = TRUE;
516                     }
517                     break;
518                 case SP_PEN_CONTEXT_CONTROL:
519                 case SP_PEN_CONTEXT_CLOSE:
520                     /* Placing controls is last operation in CLOSE state */
521                     spdc_endpoint_snap(pc, p, mevent.state);
522                     spdc_pen_set_ctrl(pc, p, mevent.state);
523                     ret = TRUE;
524                     break;
525                 case SP_PEN_CONTEXT_STOP:
526                     /* This is perfectly valid */
527                     break;
528                 default:
529                     break;
530             }
531             break;
532         case SP_PEN_CONTEXT_MODE_DRAG:
533             switch (pc->state) {
534                 case SP_PEN_CONTEXT_POINT:
535                     if ( pc->npoints > 0 ) {
536                         /* Only set point, if we are already appending */
538                         if (!anchor) {   /* Snap node only if not hitting anchor */
539                             spdc_endpoint_snap(pc, p, mevent.state);
540                         }
542                         spdc_pen_set_subsequent_point(pc, p, !anchor);
544                         if (anchor && !pc->anchor_statusbar) {
545                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
546                             pc->anchor_statusbar = true;
547                         } else if (!anchor && pc->anchor_statusbar) {
548                             pc->_message_context->clear();
549                             pc->anchor_statusbar = false;
550                         }
552                         ret = TRUE;
553                     } else {
554                         if (anchor && !pc->anchor_statusbar) {
555                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
556                             pc->anchor_statusbar = true;
557                         } else if (!anchor && pc->anchor_statusbar) {
558                             pc->_message_context->clear();
559                             pc->anchor_statusbar = false;
560                         }
561                     }
562                     break;
563                 case SP_PEN_CONTEXT_CONTROL:
564                 case SP_PEN_CONTEXT_CLOSE:
565                     /* Placing controls is last operation in CLOSE state */
567                     // snap the handle
568                     spdc_endpoint_snap_handle(pc, p, mevent.state);
570                     spdc_pen_set_ctrl(pc, p, mevent.state);
571                     ret = TRUE;
572                     break;
573                 case SP_PEN_CONTEXT_STOP:
574                     /* This is perfectly valid */
575                     break;
576                 default:
577                     break;
578             }
579             break;
580         default:
581             break;
582     }
583     return ret;
586 /**
587  * Handle mouse button release event.
588  */
589 static gint
590 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
592     gint ret = FALSE;
593     if ( revent.button == 1 ) {
595         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
597         NR::Point const event_w(revent.x,
598                                 revent.y);
599         /* Find desktop coordinates */
600         NR::Point p = pc->desktop->w2d(event_w);
602         /* Test whether we hit any anchor. */
603         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
605         switch (pc->mode) {
606             case SP_PEN_CONTEXT_MODE_CLICK:
607                 switch (pc->state) {
608                     case SP_PEN_CONTEXT_POINT:
609                         if ( pc->npoints == 0 ) {
610                             /* Start new thread only with button release */
611                             if (anchor) {
612                                 p = anchor->dp;
613                             }
614                             pc->sa = anchor;
615                             spdc_pen_set_initial_point(pc, p);
616                         } else {
617                             /* Set end anchor here */
618                             pc->ea = anchor;
619                             if (anchor) {
620                                 p = anchor->dp;
621                             }
622                         }
623                         pc->state = SP_PEN_CONTEXT_CONTROL;
624                         ret = TRUE;
625                         break;
626                     case SP_PEN_CONTEXT_CONTROL:
627                         /* End current segment */
628                         spdc_endpoint_snap(pc, p, revent.state);
629                         spdc_pen_finish_segment(pc, p, revent.state);
630                         pc->state = SP_PEN_CONTEXT_POINT;
631                         ret = TRUE;
632                         break;
633                     case SP_PEN_CONTEXT_CLOSE:
634                         /* End current segment */
635                         if (!anchor) {   /* Snap node only if not hitting anchor */
636                             spdc_endpoint_snap(pc, p, revent.state);
637                         }
638                         spdc_pen_finish_segment(pc, p, revent.state);
639                         spdc_pen_finish(pc, TRUE);
640                         pc->state = SP_PEN_CONTEXT_POINT;
641                         ret = TRUE;
642                         break;
643                     case SP_PEN_CONTEXT_STOP:
644                         /* This is allowed, if we just cancelled curve */
645                         pc->state = SP_PEN_CONTEXT_POINT;
646                         ret = TRUE;
647                         break;
648                     default:
649                         break;
650                 }
651                 break;
652             case SP_PEN_CONTEXT_MODE_DRAG:
653                 switch (pc->state) {
654                     case SP_PEN_CONTEXT_POINT:
655                     case SP_PEN_CONTEXT_CONTROL:
656                         spdc_endpoint_snap(pc, p, revent.state);
657                         spdc_pen_finish_segment(pc, p, revent.state);
658                         break;
659                     case SP_PEN_CONTEXT_CLOSE:
660                         spdc_endpoint_snap(pc, p, revent.state);
661                         spdc_pen_finish_segment(pc, p, revent.state);
662                         if (pc->green_closed) {
663                             // finishing at the start anchor, close curve
664                             spdc_pen_finish(pc, TRUE);
665                         } else {
666                             // finishing at some other anchor, finish curve but not close
667                             spdc_pen_finish(pc, FALSE);
668                         }
669                         break;
670                     case SP_PEN_CONTEXT_STOP:
671                         /* This is allowed, if we just cancelled curve */
672                         break;
673                     default:
674                         break;
675                 }
676                 pc->state = SP_PEN_CONTEXT_POINT;
677                 ret = TRUE;
678                 break;
679             default:
680                 break;
681         }
683         if (pc->grab) {
684             /* Release grab now */
685             sp_canvas_item_ungrab(pc->grab, revent.time);
686             pc->grab = NULL;
687         }
689         ret = TRUE;
691         dc->green_closed = FALSE;
692     }
694     return ret;
697 static gint
698 pen_handle_2button_press(SPPenContext *const pc)
700     gint ret = FALSE;
701     if (pc->npoints != 0) {
702         spdc_pen_finish(pc, FALSE);
703         ret = TRUE;
704     }
705     return ret;
708 void
709 pen_redraw_all (SPPenContext *const pc)
711     // green
712     if (pc->green_bpaths) {
713         // remove old piecewise green canvasitems
714         while (pc->green_bpaths) {
715             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
716             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
717         }
718         // one canvas bpath for all of green_curve
719         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
720         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
721         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
723         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
724     }
726     if (pc->green_anchor)
727         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
729     sp_curve_reset(pc->red_curve);
730     sp_curve_moveto(pc->red_curve, pc->p[0]);
731     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
732     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
734     // handles
735     if (pc->p[0] != pc->p[1]) {
736         SP_CTRL(pc->c1)->moveto(pc->p[1]);
737         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
738         sp_canvas_item_show (pc->c1);
739         sp_canvas_item_show (pc->cl1);
740     } else {
741         sp_canvas_item_hide (pc->c1);
742         sp_canvas_item_hide (pc->cl1);
743     }
745     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
746     if (bpath) {
747         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
748             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
749             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
750             sp_canvas_item_show (pc->c0);
751             sp_canvas_item_show (pc->cl0);
752         } else {
753             sp_canvas_item_hide (pc->c0);
754             sp_canvas_item_hide (pc->cl0);
755         }
756     }
759 void
760 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
762     if (pc->npoints != 5)
763         return;
765     // green
766     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
767     if (bpath) {
768         if (bpath->code == NR_CURVETO) {
769             bpath->x2 += x;
770             bpath->y2 += y;
771         }
772         bpath->x3 += x;
773         bpath->y3 += y;
774     } else {
775         // start anchor too
776         if (pc->green_anchor) {
777             pc->green_anchor->dp += NR::Point(x, y);
778         }
779     }
781     // red
782     pc->p[0] += NR::Point(x, y);
783     pc->p[1] += NR::Point(x, y);
784     pen_redraw_all(pc);
787 void
788 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
790     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
793 void
794 pen_lastpoint_tocurve (SPPenContext *const pc)
796     if (pc->npoints != 5)
797         return;
799     // red
800     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
801     if (bpath && bpath->code == NR_CURVETO) {
802         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
803     } else {
804         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
805     }
807     pen_redraw_all(pc);
810 void
811 pen_lastpoint_toline (SPPenContext *const pc)
813     if (pc->npoints != 5)
814         return;
816     pc->p[1] = pc->p[0];
818     pen_redraw_all(pc);
822 static gint
823 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
825     gint ret = FALSE;
826     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
828     switch (get_group0_keyval (&event->key)) {
830         case GDK_Left: // move last point left
831         case GDK_KP_Left:
832         case GDK_KP_4:
833             if (!MOD__CTRL) { // not ctrl
834                 if (MOD__ALT) { // alt
835                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
836                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
837                 }
838                 else { // no alt
839                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
840                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
841                 }
842                 ret = TRUE;
843             }
844             break;
845         case GDK_Up: // move last point up
846         case GDK_KP_Up:
847         case GDK_KP_8:
848             if (!MOD__CTRL) { // not ctrl
849                 if (MOD__ALT) { // alt
850                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
851                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
852                 }
853                 else { // no alt
854                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
855                     else pen_lastpoint_move(pc, 0, nudge); // no shift
856                 }
857                 ret = TRUE;
858             }
859             break;
860         case GDK_Right: // move last point right
861         case GDK_KP_Right:
862         case GDK_KP_6:
863             if (!MOD__CTRL) { // not ctrl
864                 if (MOD__ALT) { // alt
865                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
866                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
867                 }
868                 else { // no alt
869                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
870                     else pen_lastpoint_move(pc, nudge, 0); // no shift
871                 }
872                 ret = TRUE;
873             }
874             break;
875         case GDK_Down: // move last point down
876         case GDK_KP_Down:
877         case GDK_KP_2:
878             if (!MOD__CTRL) { // not ctrl
879                 if (MOD__ALT) { // alt
880                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
881                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
882                 }
883                 else { // no alt
884                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
885                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
886                 }
887                 ret = TRUE;
888             }
889             break;
891         case GDK_U:
892         case GDK_u:
893             if (MOD__SHIFT_ONLY) {
894                 pen_lastpoint_tocurve(pc);
895                 ret = TRUE;
896             }
897             break;
898         case GDK_L:
899         case GDK_l:
900             if (MOD__SHIFT_ONLY) {
901                 pen_lastpoint_toline(pc);
902                 ret = TRUE;
903             }
904             break;
906         case GDK_Return:
907         case GDK_KP_Enter:
908             if (pc->npoints != 0) {
909                 spdc_pen_finish(pc, FALSE);
910                 ret = TRUE;
911             }
912             break;
913         case GDK_Escape:
914             if (pc->npoints != 0) {
915                 // if drawing, cancel, otherwise pass it up for deselecting
916                 pen_cancel (pc);
917                 ret = TRUE;
918             }
919             break;
920         case GDK_z:
921         case GDK_Z:
922             if (MOD__CTRL_ONLY && pc->npoints != 0) {
923                 // if drawing, cancel, otherwise pass it up for undo
924                 pen_cancel (pc);
925                 ret = TRUE;
926             }
927             break;
928         case GDK_BackSpace:
929         case GDK_Delete:
930         case GDK_KP_Delete:
931             if (sp_curve_is_empty(pc->green_curve)) {
932                 pen_cancel (pc);
933                 ret = TRUE;
934             } else {
935                 /* Reset red curve */
936                 sp_curve_reset(pc->red_curve);
937                 /* Destroy topmost green bpath */
938                 if (pc->green_bpaths) {
939                     if (pc->green_bpaths->data)
940                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
941                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
942                 }
943                 /* Get last segment */
944                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
945                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
946                 if ( e < 2 ) {
947                     g_warning("Green curve length is %d", e);
948                     break;
949                 }
950                 pc->p[0] = p[e - 2].c(3);
951                 if (p[e - 1].code == NR_CURVETO) {
952                     pc->p[1] = p[e - 1].c(1);
953                 } else {
954                     pc->p[1] = pc->p[0];
955                 }
956                 NR::Point const pt(( pc->npoints < 4
957                                      ? p[e - 1].c(3)
958                                      : pc->p[3] ));
959                 pc->npoints = 2;
960                 sp_curve_backspace(pc->green_curve);
961                 sp_canvas_item_hide(pc->c0);
962                 sp_canvas_item_hide(pc->c1);
963                 sp_canvas_item_hide(pc->cl0);
964                 sp_canvas_item_hide(pc->cl1);
965                 pc->state = SP_PEN_CONTEXT_POINT;
966                 spdc_pen_set_subsequent_point(pc, pt, true);
967                 ret = TRUE;
968             }
969             break;
970         default:
971             break;
972     }
973     return ret;
976 static void
977 spdc_reset_colors(SPPenContext *pc)
979     /* Red */
980     sp_curve_reset(pc->red_curve);
981     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
982     /* Blue */
983     sp_curve_reset(pc->blue_curve);
984     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
985     /* Green */
986     while (pc->green_bpaths) {
987         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
988         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
989     }
990     sp_curve_reset(pc->green_curve);
991     if (pc->green_anchor) {
992         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
993     }
994     pc->sa = NULL;
995     pc->ea = NULL;
996     pc->npoints = 0;
997     pc->red_curve_is_valid = false;
1001 static void
1002 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1004     g_assert( pc->npoints == 0 );
1006     pc->p[0] = p;
1007     pc->p[1] = p;
1008     pc->npoints = 2;
1009     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1012 static void
1013 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1015     g_assert( pc->npoints != 0 );
1016     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1018     pc->p[2] = p;
1019     pc->p[3] = p;
1020     pc->p[4] = p;
1021     pc->npoints = 5;
1022     sp_curve_reset(pc->red_curve);
1023     sp_curve_moveto(pc->red_curve, pc->p[0]);
1024     bool is_curve;
1025     if ( (pc->onlycurves)
1026          || ( pc->p[1] != pc->p[0] ) )
1027     {
1028         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1029         is_curve = true;
1030     } else {
1031         sp_curve_lineto(pc->red_curve, p);
1032         is_curve = false;
1033     }
1034     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1036     if (statusbar) {
1037         // status text
1038         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1039         NR::Point rel = p - pc->p[0];
1040         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1041         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1042         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1043             angle = angle_to_compass (angle);
1044         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>%s</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path"), is_curve? "Curve segment" : "Line segment", angle, dist->str);
1045         g_string_free(dist, FALSE);
1046     }
1049 static void
1050 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1052     sp_canvas_item_show(pc->c1);
1053     sp_canvas_item_show(pc->cl1);
1055     if ( pc->npoints == 2 ) {
1056         pc->p[1] = p;
1057         sp_canvas_item_hide(pc->c0);
1058         sp_canvas_item_hide(pc->cl0);
1059         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1060         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1062         // status text
1063         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1064         NR::Point rel = p - pc->p[0];
1065         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1066         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1067         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1068             angle = angle_to_compass (angle);
1069         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1070         g_string_free(dist, FALSE);
1072     } else if ( pc->npoints == 5 ) {
1073         pc->p[4] = p;
1074         sp_canvas_item_show(pc->c0);
1075         sp_canvas_item_show(pc->cl0);
1076         bool is_symm = false;
1077         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1078              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1079             NR::Point delta = p - pc->p[3];
1080             pc->p[2] = pc->p[3] - delta;
1081             is_symm = true;
1082             sp_curve_reset(pc->red_curve);
1083             sp_curve_moveto(pc->red_curve, pc->p[0]);
1084             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1085             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1086         }
1087         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1088         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1089         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1090         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1092         // status text
1093         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1094         NR::Point rel = p - pc->p[3];
1095         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1096         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1097         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1098             angle = angle_to_compass (angle);
1099         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>%s</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only"), is_symm? "Curve handle, symmetric" : "Curve handle", angle, dist->str);
1100         g_string_free(dist, FALSE);
1102     } else {
1103         g_warning("Something bad happened - npoints is %d", pc->npoints);
1104     }
1107 static void
1108 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1110     if (!sp_curve_empty(pc->red_curve)) {
1111         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1112         SPCurve *curve = sp_curve_copy(pc->red_curve);
1113         /// \todo fixme:
1114         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1115         sp_curve_unref(curve);
1116         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1118         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1120         pc->p[0] = pc->p[3];
1121         pc->p[1] = pc->p[4];
1122         pc->npoints = 2;
1124         sp_curve_reset(pc->red_curve);
1125     }
1128 static void
1129 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1131     SPDesktop *const desktop = pc->desktop;
1132     pc->_message_context->clear();
1133     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1135     sp_curve_reset(pc->red_curve);
1136     spdc_concat_colors_and_flush(pc, closed);
1137     pc->sa = NULL;
1138     pc->ea = NULL;
1140     pc->npoints = 0;
1141     pc->state = SP_PEN_CONTEXT_POINT;
1143     sp_canvas_item_hide(pc->c0);
1144     sp_canvas_item_hide(pc->c1);
1145     sp_canvas_item_hide(pc->cl0);
1146     sp_canvas_item_hide(pc->cl1);
1148     if (pc->green_anchor) {
1149         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1150     }
1154 /*
1155   Local Variables:
1156   mode:c++
1157   c-file-style:"stroustrup"
1158   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1159   indent-tabs-mode:nil
1160   fill-column:99
1161   End:
1162 */
1163 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :