Code

bulk trailing spaces removal. consistency through MD5 of binary
[inkscape.git] / src / pen-context.cpp
1 /** \file
2  * Pen event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
20 #include "pen-context.h"
21 #include "sp-namedview.h"
22 #include "sp-metrics.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "draw-anchor.h"
27 #include "message-stack.h"
28 #include "message-context.h"
29 #include "prefs-utils.h"
30 #include "sp-path.h"
32 #include "pixmaps/cursor-pen.xpm"
33 #include "display/canvas-bpath.h"
34 #include "display/sp-ctrlline.h"
35 #include "display/sodipodi-ctrl.h"
36 #include <glibmm/i18n.h>
37 #include "libnr/n-art-bpath.h"
38 #include "helper/units.h"
39 #include "macros.h"
40 #include "context-fns.h"
43 static void sp_pen_context_class_init(SPPenContextClass *klass);
44 static void sp_pen_context_init(SPPenContext *pc);
45 static void sp_pen_context_dispose(GObject *object);
47 static void sp_pen_context_setup(SPEventContext *ec);
48 static void sp_pen_context_finish(SPEventContext *ec);
49 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
52 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
53 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
54 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
55 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
57 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
59 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
60 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
61 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
62 static gint pen_handle_2button_press(SPPenContext *const pc);
63 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
64 static void spdc_reset_colors(SPPenContext *pc);
67 static NR::Point pen_drag_origin_w(0, 0);
68 static bool pen_within_tolerance = false;
70 static SPDrawContextClass *pen_parent_class;
72 /**
73  * Register SPPenContext with Gdk and return its type.
74  */
75 GType
76 sp_pen_context_get_type(void)
77 {
78     static GType type = 0;
79     if (!type) {
80         GTypeInfo info = {
81             sizeof(SPPenContextClass),
82             NULL, NULL,
83             (GClassInitFunc) sp_pen_context_class_init,
84             NULL, NULL,
85             sizeof(SPPenContext),
86             4,
87             (GInstanceInitFunc) sp_pen_context_init,
88             NULL,   /* value_table */
89         };
90         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
91     }
92     return type;
93 }
95 /**
96  * Initialize the SPPenContext vtable.
97  */
98 static void
99 sp_pen_context_class_init(SPPenContextClass *klass)
101     GObjectClass *object_class;
102     SPEventContextClass *event_context_class;
104     object_class = (GObjectClass *) klass;
105     event_context_class = (SPEventContextClass *) klass;
107     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
109     object_class->dispose = sp_pen_context_dispose;
111     event_context_class->setup = sp_pen_context_setup;
112     event_context_class->finish = sp_pen_context_finish;
113     event_context_class->set = sp_pen_context_set;
114     event_context_class->root_handler = sp_pen_context_root_handler;
117 /**
118  * Callback to initialize SPPenContext object.
119  */
120 static void
121 sp_pen_context_init(SPPenContext *pc)
124     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
126     event_context->cursor_shape = cursor_pen_xpm;
127     event_context->hot_x = 4;
128     event_context->hot_y = 4;
130     pc->npoints = 0;
131     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
132     pc->state = SP_PEN_CONTEXT_POINT;
134     pc->c0 = NULL;
135     pc->c1 = NULL;
136     pc->cl0 = NULL;
137     pc->cl1 = NULL;
140 /**
141  * Callback to destroy the SPPenContext object's members and itself.
142  */
143 static void
144 sp_pen_context_dispose(GObject *object)
146     SPPenContext *pc;
148     pc = SP_PEN_CONTEXT(object);
150     if (pc->c0) {
151         gtk_object_destroy(GTK_OBJECT(pc->c0));
152         pc->c0 = NULL;
153     }
154     if (pc->c1) {
155         gtk_object_destroy(GTK_OBJECT(pc->c1));
156         pc->c1 = NULL;
157     }
158     if (pc->cl0) {
159         gtk_object_destroy(GTK_OBJECT(pc->cl0));
160         pc->cl0 = NULL;
161     }
162     if (pc->cl1) {
163         gtk_object_destroy(GTK_OBJECT(pc->cl1));
164         pc->cl1 = NULL;
165     }
167     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
170 /**
171  * Callback to initialize SPPenContext object.
172  */
173 static void
174 sp_pen_context_setup(SPEventContext *ec)
176     SPPenContext *pc;
178     pc = SP_PEN_CONTEXT(ec);
180     if (((SPEventContextClass *) pen_parent_class)->setup) {
181         ((SPEventContextClass *) pen_parent_class)->setup(ec);
182     }
184     /* Pen indicators */
185     pc->c0 = sp_canvas_item_new(SP_DT_CONTROLS(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
186                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
187     pc->c1 = sp_canvas_item_new(SP_DT_CONTROLS(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
188                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
189     pc->cl0 = sp_canvas_item_new(SP_DT_CONTROLS(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
190     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
191     pc->cl1 = sp_canvas_item_new(SP_DT_CONTROLS(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
192     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
194     sp_canvas_item_hide(pc->c0);
195     sp_canvas_item_hide(pc->c1);
196     sp_canvas_item_hide(pc->cl0);
197     sp_canvas_item_hide(pc->cl1);
199     sp_event_context_read(ec, "mode");
201     pc->anchor_statusbar = false;
203     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
204         ec->enableSelectionCue();
205     }
208 /**
209  * Finalization callback.
210  */
211 static void
212 sp_pen_context_finish(SPEventContext *ec)
214     spdc_pen_finish(SP_PEN_CONTEXT(ec), FALSE);
216     if (((SPEventContextClass *) pen_parent_class)->finish) {
217         ((SPEventContextClass *) pen_parent_class)->finish(ec);
218     }
221 /**
222  * Callback that sets key to value in pen context.
223  */
224 static void
225 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
227     SPPenContext *pc = SP_PEN_CONTEXT(ec);
229     if (!strcmp(key, "mode")) {
230         if ( val && !strcmp(val, "drag") ) {
231             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
232         } else {
233             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
234         }
235     }
238 /**
239  * Snaps new node relative to the previous node.
240  */
241 static void
242 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
244     if (pc->npoints > 0) {
245         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
246     }
248     spdc_endpoint_snap_free(pc, p, state);
251 /**
252  * Snaps new node's handle relative to the new node.
253  */
254 static void
255 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
257     g_return_if_fail(( pc->npoints == 2 ||
258                        pc->npoints == 5   ));
260     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
261     spdc_endpoint_snap_free(pc, p, state);
264 /**
265  * Callback to handle all pen events.
266  */
267 static gint
268 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
270     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
272     gint ret = FALSE;
274     switch (event->type) {
275         case GDK_BUTTON_PRESS:
276             ret = pen_handle_button_press(pc, event->button);
277             break;
279         case GDK_MOTION_NOTIFY:
280             ret = pen_handle_motion_notify(pc, event->motion);
281             break;
283         case GDK_BUTTON_RELEASE:
284             ret = pen_handle_button_release(pc, event->button);
285             break;
287         case GDK_2BUTTON_PRESS:
288             ret = pen_handle_2button_press(pc);
289             break;
291         case GDK_KEY_PRESS:
292             ret = pen_handle_key_press(pc, event);
293             break;
295         default:
296             break;
297     }
299     if (!ret) {
300         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
301             = ((SPEventContextClass *) pen_parent_class)->root_handler;
302         if (parent_root_handler) {
303             ret = parent_root_handler(ec, event);
304         }
305     }
307     return ret;
310 /**
311  * Handle mouse button press event.
312  */
313 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
315     gint ret = FALSE;
316     if (bevent.button == 1) {
318         SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
319         SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
321         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
322             return TRUE;
323         }
325         NR::Point const event_w(bevent.x, bevent.y);
326         pen_drag_origin_w = event_w;
327         pen_within_tolerance = true;
329         /* Test whether we hit any anchor. */
330         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
332         NR::Point const event_dt(desktop->w2d(event_w));
333         switch (pc->mode) {
334             case SP_PEN_CONTEXT_MODE_CLICK:
335                 /* In click mode we add point on release */
336                 switch (pc->state) {
337                     case SP_PEN_CONTEXT_POINT:
338                     case SP_PEN_CONTEXT_CONTROL:
339                     case SP_PEN_CONTEXT_CLOSE:
340                         break;
341                     case SP_PEN_CONTEXT_STOP:
342                         /* This is allowed, if we just cancelled curve */
343                         pc->state = SP_PEN_CONTEXT_POINT;
344                         break;
345                     default:
346                         break;
347                 }
348                 break;
349             case SP_PEN_CONTEXT_MODE_DRAG:
350                 switch (pc->state) {
351                     case SP_PEN_CONTEXT_STOP:
352                         /* This is allowed, if we just cancelled curve */
353                     case SP_PEN_CONTEXT_POINT:
354                         if (pc->npoints == 0) {
356                             /* Set start anchor */
357                             pc->sa = anchor;
358                             NR::Point p;
359                             if (anchor) {
361                                 /* Adjust point to anchor if needed */
362                                 p = anchor->dp;
363                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
365                             } else {
367                                 // This is the first click of a new curve; deselect item so that
368                                 // this curve is not combined with it (unless it is drawn from its
369                                 // anchor, which is handled by the sibling branch above)
370                                 Inkscape::Selection * const selection = SP_DT_SELECTION(desktop);
371                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
373                                     selection->clear();
374                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
376                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
378                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
379                                 }
381                                 /* Create green anchor */
382                                 p = event_dt;
383                                 spdc_endpoint_snap(pc, p, bevent.state);
384                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
385                             }
386                             spdc_pen_set_initial_point(pc, p);
387                         } else {
389                             /* Set end anchor */
390                             pc->ea = anchor;
391                             NR::Point p;
392                             if (anchor) {
394                                 p = anchor->dp;
395                                 // we hit an anchor, will finish the curve (either with or without closing)
396                                 // in release handler
397                                 pc->state = SP_PEN_CONTEXT_CLOSE;
399                                 if (pc->green_anchor && pc->green_anchor->active) {
400                                     // we clicked on the current curve start, so close it even if
401                                     // we drag a handle away from it
402                                     dc->green_closed = TRUE;
403                                 }
404                                 ret = TRUE;
405                                 break;
407                             } else {
409                                 p = event_dt;
410                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
411                                 spdc_pen_set_subsequent_point(pc, p, true);
412                             }
414                         }
415                         pc->state = SP_PEN_CONTEXT_CONTROL;
416                         ret = TRUE;
417                         break;
418                     case SP_PEN_CONTEXT_CONTROL:
419                         g_warning("Button down in CONTROL state");
420                         break;
421                     case SP_PEN_CONTEXT_CLOSE:
422                         g_warning("Button down in CLOSE state");
423                         break;
424                     default:
425                         break;
426                 }
427                 break;
428             default:
429                 break;
430         }
431     } else if (bevent.button == 3) {
432         if (pc->npoints != 0) {
433             spdc_pen_finish(pc, FALSE);
434             ret = TRUE;
435         }
436     }
438     return ret;
441 /**
442  * Handle motion_notify event.
443  */
444 static gint
445 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
447     gint ret = FALSE;
449     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
450         // allow middle-button scrolling
451         return FALSE;
452     }
454     NR::Point const event_w(mevent.x,
455                             mevent.y);
456     if (pen_within_tolerance) {
457         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
458                                                                "value", 0, 0, 100);
459         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
460             return FALSE;   // Do not drag if we're within tolerance from origin.
461         }
462     }
463     // Once the user has moved farther than tolerance from the original location
464     // (indicating they intend to move the object, not click), then always process the
465     // motion notify coordinates as given (no snapping back to origin)
466     pen_within_tolerance = false;
468     SPDesktop *const dt = pc->desktop;
469     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
470         /* Grab mouse, so release will not pass unnoticed */
471         pc->grab = SP_CANVAS_ITEM(dt->acetate);
472         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
473                                         GDK_BUTTON_RELEASE_MASK |
474                                         GDK_POINTER_MOTION_MASK  ),
475                             NULL, mevent.time);
476     }
478     /* Find desktop coordinates */
479     NR::Point p = dt->w2d(event_w);
481     /* Test, whether we hit any anchor */
482     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
484     switch (pc->mode) {
485         case SP_PEN_CONTEXT_MODE_CLICK:
486             switch (pc->state) {
487                 case SP_PEN_CONTEXT_POINT:
488                     if ( pc->npoints != 0 ) {
489                         /* Only set point, if we are already appending */
490                         spdc_endpoint_snap(pc, p, mevent.state);
491                         spdc_pen_set_subsequent_point(pc, p, true);
492                         ret = TRUE;
493                     }
494                     break;
495                 case SP_PEN_CONTEXT_CONTROL:
496                 case SP_PEN_CONTEXT_CLOSE:
497                     /* Placing controls is last operation in CLOSE state */
498                     spdc_endpoint_snap(pc, p, mevent.state);
499                     spdc_pen_set_ctrl(pc, p, mevent.state);
500                     ret = TRUE;
501                     break;
502                 case SP_PEN_CONTEXT_STOP:
503                     /* This is perfectly valid */
504                     break;
505                 default:
506                     break;
507             }
508             break;
509         case SP_PEN_CONTEXT_MODE_DRAG:
510             switch (pc->state) {
511                 case SP_PEN_CONTEXT_POINT:
512                     if ( pc->npoints > 0 ) {
513                         /* Only set point, if we are already appending */
515                         if (!anchor) {   /* Snap node only if not hitting anchor */
516                             spdc_endpoint_snap(pc, p, mevent.state);
517                         }
519                         spdc_pen_set_subsequent_point(pc, p, !anchor);
521                         if (anchor && !pc->anchor_statusbar) {
522                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
523                             pc->anchor_statusbar = true;
524                         } else if (!anchor && pc->anchor_statusbar) {
525                             pc->_message_context->clear();
526                             pc->anchor_statusbar = false;
527                         }
529                         ret = TRUE;
530                     } else {
531                         if (anchor && !pc->anchor_statusbar) {
532                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
533                             pc->anchor_statusbar = true;
534                         } else if (!anchor && pc->anchor_statusbar) {
535                             pc->_message_context->clear();
536                             pc->anchor_statusbar = false;
537                         }
538                     }
539                     break;
540                 case SP_PEN_CONTEXT_CONTROL:
541                 case SP_PEN_CONTEXT_CLOSE:
542                     /* Placing controls is last operation in CLOSE state */
544                     // snap the handle
545                     spdc_endpoint_snap_handle(pc, p, mevent.state);
547                     spdc_pen_set_ctrl(pc, p, mevent.state);
548                     ret = TRUE;
549                     break;
550                 case SP_PEN_CONTEXT_STOP:
551                     /* This is perfectly valid */
552                     break;
553                 default:
554                     break;
555             }
556             break;
557         default:
558             break;
559     }
560     return ret;
563 /**
564  * Handle mouse button release event.
565  */
566 static gint
567 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
569     gint ret = FALSE;
570     if ( revent.button == 1 ) {
572         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
574         NR::Point const event_w(revent.x,
575                                 revent.y);
576         /* Find desktop coordinates */
577         NR::Point p = pc->desktop->w2d(event_w);
579         /* Test whether we hit any anchor. */
580         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
582         switch (pc->mode) {
583             case SP_PEN_CONTEXT_MODE_CLICK:
584                 switch (pc->state) {
585                     case SP_PEN_CONTEXT_POINT:
586                         if ( pc->npoints == 0 ) {
587                             /* Start new thread only with button release */
588                             if (anchor) {
589                                 p = anchor->dp;
590                             }
591                             pc->sa = anchor;
592                             spdc_pen_set_initial_point(pc, p);
593                         } else {
594                             /* Set end anchor here */
595                             pc->ea = anchor;
596                             if (anchor) {
597                                 p = anchor->dp;
598                             }
599                         }
600                         pc->state = SP_PEN_CONTEXT_CONTROL;
601                         ret = TRUE;
602                         break;
603                     case SP_PEN_CONTEXT_CONTROL:
604                         /* End current segment */
605                         spdc_endpoint_snap(pc, p, revent.state);
606                         spdc_pen_finish_segment(pc, p, revent.state);
607                         pc->state = SP_PEN_CONTEXT_POINT;
608                         ret = TRUE;
609                         break;
610                     case SP_PEN_CONTEXT_CLOSE:
611                         /* End current segment */
612                         if (!anchor) {   /* Snap node only if not hitting anchor */
613                             spdc_endpoint_snap(pc, p, revent.state);
614                         }
615                         spdc_pen_finish_segment(pc, p, revent.state);
616                         spdc_pen_finish(pc, TRUE);
617                         pc->state = SP_PEN_CONTEXT_POINT;
618                         ret = TRUE;
619                         break;
620                     case SP_PEN_CONTEXT_STOP:
621                         /* This is allowed, if we just cancelled curve */
622                         pc->state = SP_PEN_CONTEXT_POINT;
623                         ret = TRUE;
624                         break;
625                     default:
626                         break;
627                 }
628                 break;
629             case SP_PEN_CONTEXT_MODE_DRAG:
630                 switch (pc->state) {
631                     case SP_PEN_CONTEXT_POINT:
632                     case SP_PEN_CONTEXT_CONTROL:
633                         spdc_endpoint_snap(pc, p, revent.state);
634                         spdc_pen_finish_segment(pc, p, revent.state);
635                         break;
636                     case SP_PEN_CONTEXT_CLOSE:
637                         spdc_endpoint_snap(pc, p, revent.state);
638                         spdc_pen_finish_segment(pc, p, revent.state);
639                         if (pc->green_closed) {
640                             // finishing at the start anchor, close curve
641                             spdc_pen_finish(pc, TRUE);
642                         } else {
643                             // finishing at some other anchor, finish curve but not close
644                             spdc_pen_finish(pc, FALSE);
645                         }
646                         break;
647                     case SP_PEN_CONTEXT_STOP:
648                         /* This is allowed, if we just cancelled curve */
649                         break;
650                     default:
651                         break;
652                 }
653                 pc->state = SP_PEN_CONTEXT_POINT;
654                 ret = TRUE;
655                 break;
656             default:
657                 break;
658         }
660         if (pc->grab) {
661             /* Release grab now */
662             sp_canvas_item_ungrab(pc->grab, revent.time);
663             pc->grab = NULL;
664         }
666         ret = TRUE;
668         dc->green_closed = FALSE;
669     }
671     return ret;
674 static gint
675 pen_handle_2button_press(SPPenContext *const pc)
677     gint ret = FALSE;
678     if (pc->npoints != 0) {
679         spdc_pen_finish(pc, FALSE);
680         ret = TRUE;
681     }
682     return ret;
685 void
686 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
688     if (pc->npoints != 5)
689         return;
691     // green
692     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
693     if (bpath) {
694         if (bpath->code == NR_CURVETO) {
695             bpath->x2 += x;
696             bpath->y2 += y;
697         }
698         bpath->x3 += x;
699         bpath->y3 += y;
700         if (pc->green_bpaths && pc->green_bpaths->data) {
701             // remove old piecewise green canvasitems
702             while (pc->green_bpaths) {
703                 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
704                 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
705             }
706             // one canvas bpath for all of green_curve
707             SPCanvasItem *cshape = sp_canvas_bpath_new(SP_DT_SKETCH(pc->desktop), pc->green_curve);
708             sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
709             sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
711             pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
712         }
713     } else {
714         // start anchor too
715         if (pc->green_anchor) {
716             pc->green_anchor->dp += NR::Point(x, y);
717             SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
718         }
719     }
721     // red
722     pc->p[0] += NR::Point(x, y);
723     pc->p[1] += NR::Point(x, y);
724     sp_curve_reset(pc->red_curve);
725     sp_curve_moveto(pc->red_curve, pc->p[0]);
726     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
727     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
729     // handles
730     if (pc->p[0] != pc->p[1]) {
731         SP_CTRL(pc->c1)->moveto(pc->p[1]);
732         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
733     }
734     if (bpath && bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
735         SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
736         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
737     }
740 void
741 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
743     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
746 static gint
747 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
749     gint ret = FALSE;
750     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
752     switch (get_group0_keyval (&event->key)) {
754         case GDK_Left: // move last point left
755         case GDK_KP_Left:
756         case GDK_KP_4:
757             if (!MOD__CTRL) { // not ctrl
758                 if (MOD__ALT) { // alt
759                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
760                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
761                 }
762                 else { // no alt
763                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
764                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
765                 }
766                 ret = TRUE;
767             }
768             break;
769         case GDK_Up: // move last point up
770         case GDK_KP_Up:
771         case GDK_KP_8:
772             if (!MOD__CTRL) { // not ctrl
773                 if (MOD__ALT) { // alt
774                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
775                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
776                 }
777                 else { // no alt
778                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
779                     else pen_lastpoint_move(pc, 0, nudge); // no shift
780                 }
781                 ret = TRUE;
782             }
783             break;
784         case GDK_Right: // move last point right
785         case GDK_KP_Right:
786         case GDK_KP_6:
787             if (!MOD__CTRL) { // not ctrl
788                 if (MOD__ALT) { // alt
789                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
790                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
791                 }
792                 else { // no alt
793                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
794                     else pen_lastpoint_move(pc, nudge, 0); // no shift
795                 }
796                 ret = TRUE;
797             }
798             break;
799         case GDK_Down: // move last point down
800         case GDK_KP_Down:
801         case GDK_KP_2:
802             if (!MOD__CTRL) { // not ctrl
803                 if (MOD__ALT) { // alt
804                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
805                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
806                 }
807                 else { // no alt
808                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
809                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
810                 }
811                 ret = TRUE;
812             }
813             break;
815         case GDK_Return:
816         case GDK_KP_Enter:
817             if (pc->npoints != 0) {
818                 spdc_pen_finish(pc, FALSE);
819                 ret = TRUE;
820             }
821             break;
822         case GDK_Escape:
823             if (pc->npoints != 0) {
824                 // if drawing, cancel, otherwise pass it up for deselecting
825                 pc->state = SP_PEN_CONTEXT_STOP;
826                 spdc_reset_colors(pc);
827                 sp_canvas_item_hide(pc->c0);
828                 sp_canvas_item_hide(pc->c1);
829                 sp_canvas_item_hide(pc->cl0);
830                 sp_canvas_item_hide(pc->cl1);
831                 ret = TRUE;
832             }
833             break;
834         case GDK_z:
835         case GDK_Z:
836             if (MOD__CTRL_ONLY && pc->npoints != 0) {
837                 // if drawing, cancel, otherwise pass it up for undo
838                 pc->state = SP_PEN_CONTEXT_STOP;
839                 spdc_reset_colors(pc);
840                 sp_canvas_item_hide(pc->c0);
841                 sp_canvas_item_hide(pc->c1);
842                 sp_canvas_item_hide(pc->cl0);
843                 sp_canvas_item_hide(pc->cl1);
844                 ret = TRUE;
845             }
846             break;
847         case GDK_BackSpace:
848         case GDK_Delete:
849         case GDK_KP_Delete:
850             if (sp_curve_is_empty(pc->green_curve)) {
851                 /* Same as cancel */
852                 pc->state = SP_PEN_CONTEXT_STOP;
853                 spdc_reset_colors(pc);
854                 sp_canvas_item_hide(pc->c0);
855                 sp_canvas_item_hide(pc->c1);
856                 sp_canvas_item_hide(pc->cl0);
857                 sp_canvas_item_hide(pc->cl1);
858                 ret = TRUE;
859             } else {
860                 /* Reset red curve */
861                 sp_curve_reset(pc->red_curve);
862                 /* Destroy topmost green bpath */
863                 if (pc->green_bpaths) {
864                     if (pc->green_bpaths->data)
865                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
866                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
867                 }
868                 /* Get last segment */
869                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
870                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
871                 if ( e < 2 ) {
872                     g_warning("Green curve length is %d", e);
873                     break;
874                 }
875                 pc->p[0] = p[e - 2].c(3);
876                 pc->p[1] = p[e - 1].c(1);
877                 NR::Point const pt(( pc->npoints < 4
878                                      ? p[e - 1].c(3)
879                                      : pc->p[3] ));
880                 pc->npoints = 2;
881                 sp_curve_backspace(pc->green_curve);
882                 sp_canvas_item_hide(pc->c0);
883                 sp_canvas_item_hide(pc->c1);
884                 sp_canvas_item_hide(pc->cl0);
885                 sp_canvas_item_hide(pc->cl1);
886                 pc->state = SP_PEN_CONTEXT_POINT;
887                 spdc_pen_set_subsequent_point(pc, pt, true);
888                 ret = TRUE;
889             }
890             break;
891         default:
892             break;
893     }
894     return ret;
897 static void
898 spdc_reset_colors(SPPenContext *pc)
900     /* Red */
901     sp_curve_reset(pc->red_curve);
902     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
903     /* Blue */
904     sp_curve_reset(pc->blue_curve);
905     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
906     /* Green */
907     while (pc->green_bpaths) {
908         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
909         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
910     }
911     sp_curve_reset(pc->green_curve);
912     if (pc->green_anchor) {
913         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
914     }
915     pc->sa = NULL;
916     pc->ea = NULL;
917     pc->npoints = 0;
918     pc->red_curve_is_valid = false;
922 static void
923 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
925     g_assert( pc->npoints == 0 );
927     pc->p[0] = p;
928     pc->p[1] = p;
929     pc->npoints = 2;
930     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
933 static void
934 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
936     g_assert( pc->npoints != 0 );
937     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
939     pc->p[2] = p;
940     pc->p[3] = p;
941     pc->p[4] = p;
942     pc->npoints = 5;
943     sp_curve_reset(pc->red_curve);
944     sp_curve_moveto(pc->red_curve, pc->p[0]);
945     bool is_curve;
946     if ( (pc->onlycurves)
947          || ( pc->p[1] != pc->p[0] ) )
948     {
949         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
950         is_curve = true;
951     } else {
952         sp_curve_lineto(pc->red_curve, p);
953         is_curve = false;
954     }
955     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
957     if (statusbar) {
958         // status text
959         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
960         NR::Point rel = p - pc->p[0];
961         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
962         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
963         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
964             angle = angle_to_compass (angle);
965         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);
966         g_string_free(dist, FALSE);
967     }
970 static void
971 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
973     sp_canvas_item_show(pc->c1);
974     sp_canvas_item_show(pc->cl1);
976     if ( pc->npoints == 2 ) {
977         pc->p[1] = p;
978         sp_canvas_item_hide(pc->c0);
979         sp_canvas_item_hide(pc->cl0);
980         SP_CTRL(pc->c1)->moveto(pc->p[1]);
981         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
983         // status text
984         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
985         NR::Point rel = p - pc->p[0];
986         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
987         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
988         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
989             angle = angle_to_compass (angle);
990         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);
991         g_string_free(dist, FALSE);
993     } else if ( pc->npoints == 5 ) {
994         pc->p[4] = p;
995         sp_canvas_item_show(pc->c0);
996         sp_canvas_item_show(pc->cl0);
997         bool is_symm = false;
998         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
999              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1000             NR::Point delta = p - pc->p[3];
1001             pc->p[2] = pc->p[3] - delta;
1002             is_symm = true;
1003             sp_curve_reset(pc->red_curve);
1004             sp_curve_moveto(pc->red_curve, pc->p[0]);
1005             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1006             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1007         }
1008         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1009         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1010         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1011         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1013         // status text
1014         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1015         NR::Point rel = p - pc->p[3];
1016         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1017         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1018         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1019             angle = angle_to_compass (angle);
1020         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);
1021         g_string_free(dist, FALSE);
1023     } else {
1024         g_warning("Something bad happened - npoints is %d", pc->npoints);
1025     }
1028 static void
1029 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1031     if (!sp_curve_empty(pc->red_curve)) {
1032         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1033         SPCurve *curve = sp_curve_copy(pc->red_curve);
1034         /// \todo fixme:
1035         SPCanvasItem *cshape = sp_canvas_bpath_new(SP_DT_SKETCH(pc->desktop), curve);
1036         sp_curve_unref(curve);
1037         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1039         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1041         pc->p[0] = pc->p[3];
1042         pc->p[1] = pc->p[4];
1043         pc->npoints = 2;
1045         sp_curve_reset(pc->red_curve);
1046     }
1049 static void
1050 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1052     SPDesktop *const desktop = pc->desktop;
1053     pc->_message_context->clear();
1054     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing pen"));
1056     sp_curve_reset(pc->red_curve);
1057     spdc_concat_colors_and_flush(pc, closed);
1058     pc->sa = NULL;
1059     pc->ea = NULL;
1061     pc->npoints = 0;
1062     pc->state = SP_PEN_CONTEXT_POINT;
1064     sp_canvas_item_hide(pc->c0);
1065     sp_canvas_item_hide(pc->c1);
1066     sp_canvas_item_hide(pc->cl0);
1067     sp_canvas_item_hide(pc->cl1);
1069     if (pc->green_anchor) {
1070         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1071     }
1075 /*
1076   Local Variables:
1077   mode:c++
1078   c-file-style:"stroustrup"
1079   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1080   indent-tabs-mode:nil
1081   fill-column:99
1082   End:
1083 */
1084 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :