Code

Added missing translation.
[inkscape.git] / src / pen-context.cpp
1 /** \file
2  * Pen event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
19 #include <cstring>
20 #include <string>
22 #include "pen-context.h"
23 #include "sp-namedview.h"
24 #include "sp-metrics.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "selection.h"
28 #include "selection-chemistry.h"
29 #include "draw-anchor.h"
30 #include "message-stack.h"
31 #include "message-context.h"
32 #include "preferences.h"
33 #include "sp-path.h"
34 #include "display/curve.h"
35 #include "pixmaps/cursor-pen.xpm"
36 #include "display/canvas-bpath.h"
37 #include "display/sp-ctrlline.h"
38 #include "display/sodipodi-ctrl.h"
39 #include <glibmm/i18n.h>
40 #include "libnr/nr-point-ops.h"
41 #include "helper/units.h"
42 #include "macros.h"
43 #include "context-fns.h"
44 #include "tools-switch.h"
46 static void sp_pen_context_class_init(SPPenContextClass *klass);
47 static void sp_pen_context_init(SPPenContext *pc);
48 static void sp_pen_context_dispose(GObject *object);
50 static void sp_pen_context_setup(SPEventContext *ec);
51 static void sp_pen_context_finish(SPEventContext *ec);
52 static void sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
53 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
54 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void spdc_pen_set_initial_point(SPPenContext *pc, Geom::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status = 0);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, Geom::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, Geom::Point p, guint state);
61 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
63 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
65 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
66 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
67 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
68 static void spdc_reset_colors(SPPenContext *pc);
70 static void pen_disable_events(SPPenContext *const pc);
71 static void pen_enable_events(SPPenContext *const pc);
73 static Geom::Point pen_drag_origin_w(0, 0);
74 static bool pen_within_tolerance = false;
76 static SPDrawContextClass *pen_parent_class;
78 static int pen_next_paraxial_direction(const SPPenContext *const pc, Geom::Point const &pt, Geom::Point const &origin, guint state);
79 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state);
80 static Geom::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, Geom::Point const &pt, guint const state);
82 static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
84 /**
85  * Register SPPenContext with Gdk and return its type.
86  */
87 GType
88 sp_pen_context_get_type(void)
89 {
90     static GType type = 0;
91     if (!type) {
92         GTypeInfo info = {
93             sizeof(SPPenContextClass),
94             NULL, NULL,
95             (GClassInitFunc) sp_pen_context_class_init,
96             NULL, NULL,
97             sizeof(SPPenContext),
98             4,
99             (GInstanceInitFunc) sp_pen_context_init,
100             NULL,   /* value_table */
101         };
102         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
103     }
104     return type;
107 /**
108  * Initialize the SPPenContext vtable.
109  */
110 static void
111 sp_pen_context_class_init(SPPenContextClass *klass)
113     GObjectClass *object_class;
114     SPEventContextClass *event_context_class;
116     object_class = (GObjectClass *) klass;
117     event_context_class = (SPEventContextClass *) klass;
119     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
121     object_class->dispose = sp_pen_context_dispose;
123     event_context_class->setup = sp_pen_context_setup;
124     event_context_class->finish = sp_pen_context_finish;
125     event_context_class->set = sp_pen_context_set;
126     event_context_class->root_handler = sp_pen_context_root_handler;
127     event_context_class->item_handler = sp_pen_context_item_handler;
130 /**
131  * Callback to initialize SPPenContext object.
132  */
133 static void
134 sp_pen_context_init(SPPenContext *pc)
137     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
139     event_context->cursor_shape = cursor_pen_xpm;
140     event_context->hot_x = 4;
141     event_context->hot_y = 4;
143     pc->npoints = 0;
144     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
145     pc->state = SP_PEN_CONTEXT_POINT;
147     pc->c0 = NULL;
148     pc->c1 = NULL;
149     pc->cl0 = NULL;
150     pc->cl1 = NULL;
152     pc->events_disabled = 0;
154     pc->num_clicks = 0;
155     pc->waiting_LPE = NULL;
156     pc->waiting_item = NULL;
159 /**
160  * Callback to destroy the SPPenContext object's members and itself.
161  */
162 static void
163 sp_pen_context_dispose(GObject *object)
165     SPPenContext *pc;
167     pc = SP_PEN_CONTEXT(object);
169     if (pc->c0) {
170         gtk_object_destroy(GTK_OBJECT(pc->c0));
171         pc->c0 = NULL;
172     }
173     if (pc->c1) {
174         gtk_object_destroy(GTK_OBJECT(pc->c1));
175         pc->c1 = NULL;
176     }
177     if (pc->cl0) {
178         gtk_object_destroy(GTK_OBJECT(pc->cl0));
179         pc->cl0 = NULL;
180     }
181     if (pc->cl1) {
182         gtk_object_destroy(GTK_OBJECT(pc->cl1));
183         pc->cl1 = NULL;
184     }
186     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
188     if (pc->expecting_clicks_for_LPE > 0) {
189         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
190         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
191     }
194 void
195 sp_pen_context_set_polyline_mode(SPPenContext *const pc) {
196     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
197     guint mode = prefs->getInt("/tools/freehand/pen/freehand-mode", 0);
198     pc->polylines_only = (mode == 2 || mode == 3);
199     pc->polylines_paraxial = (mode == 3);
202 /**
203  * Callback to initialize SPPenContext object.
204  */
205 static void
206 sp_pen_context_setup(SPEventContext *ec)
208     SPPenContext *pc;
210     pc = SP_PEN_CONTEXT(ec);
212     sp_canvas_set_snap_delay_active(pc->desktop->canvas, true);
214     if (((SPEventContextClass *) pen_parent_class)->setup) {
215         ((SPEventContextClass *) pen_parent_class)->setup(ec);
216     }
218     /* Pen indicators */
219     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
220                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
221     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
222                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
223     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
224     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
225     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
226     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
228     sp_canvas_item_hide(pc->c0);
229     sp_canvas_item_hide(pc->c1);
230     sp_canvas_item_hide(pc->cl0);
231     sp_canvas_item_hide(pc->cl1);
233     sp_event_context_read(ec, "mode");
235     pc->anchor_statusbar = false;
237     sp_pen_context_set_polyline_mode(pc);
239     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
240     if (prefs->getBool("/tools/freehand/pen/selcue")) {
241         ec->enableSelectionCue();
242     }
245 static void
246 pen_cancel (SPPenContext *const pc)
248     pc->num_clicks = 0;
249     pc->state = SP_PEN_CONTEXT_STOP;
250     spdc_reset_colors(pc);
251     sp_canvas_item_hide(pc->c0);
252     sp_canvas_item_hide(pc->c1);
253     sp_canvas_item_hide(pc->cl0);
254     sp_canvas_item_hide(pc->cl1);
255     pc->_message_context->clear();
256     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
258     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
261 /**
262  * Finalization callback.
263  */
264 static void
265 sp_pen_context_finish(SPEventContext *ec)
267     SPPenContext *pc = SP_PEN_CONTEXT(ec);
269     sp_canvas_set_snap_delay_active(pc->desktop->canvas, false);
271     if (pc->npoints != 0) {
272         pen_cancel (pc);
273     }
275     if (((SPEventContextClass *) pen_parent_class)->finish) {
276         ((SPEventContextClass *) pen_parent_class)->finish(ec);
277     }
280 /**
281  * Callback that sets key to value in pen context.
282  */
283 static void
284 sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
286     SPPenContext *pc = SP_PEN_CONTEXT(ec);
287     Glib::ustring name = val->getEntryName();
289     if (name == "mode") {
290         if ( val->getString() == "drag" ) {
291             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
292         } else {
293             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
294         }
295     }
298 /**
299  * Snaps new node relative to the previous node.
300  */
301 static void
302 spdc_endpoint_snap(SPPenContext const *const pc, Geom::Point &p, guint const state)
304     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
305         if (pc->npoints > 0) {
306             spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
307         }
308     } else {
309         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
310                                          //After all, the user explicitely asked for angular snapping by
311                                          //pressing CTRL
312             spdc_endpoint_snap_free(pc, p, state);
313         }
314     }
315     if (pc->polylines_paraxial) {
316         // TODO: must we avoid one of the snaps in the previous case distinction in some situations?
317         pen_set_to_nearest_horiz_vert(pc, p, state);
318     }
321 /**
322  * Snaps new node's handle relative to the new node.
323  */
324 static void
325 spdc_endpoint_snap_handle(SPPenContext const *const pc, Geom::Point &p, guint const state)
327     g_return_if_fail(( pc->npoints == 2 ||
328                        pc->npoints == 5   ));
330     if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
331         spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
332     } else {
333         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
334             spdc_endpoint_snap_free(pc, p, state);
335         }
336     }
339 static gint
340 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
342     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
344     gint ret = FALSE;
346     switch (event->type) {
347         case GDK_BUTTON_PRESS:
348             ret = pen_handle_button_press(pc, event->button);
349             break;
350         case GDK_BUTTON_RELEASE:
351             ret = pen_handle_button_release(pc, event->button);
352             break;
353         default:
354             break;
355     }
357     if (!ret) {
358         if (((SPEventContextClass *) pen_parent_class)->item_handler)
359             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
360     }
362     return ret;
365 /**
366  * Callback to handle all pen events.
367  */
368 static gint
369 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
371     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
373     gint ret = FALSE;
375     switch (event->type) {
376         case GDK_BUTTON_PRESS:
377             ret = pen_handle_button_press(pc, event->button);
378             break;
380         case GDK_MOTION_NOTIFY:
381             ret = pen_handle_motion_notify(pc, event->motion);
382             break;
384         case GDK_BUTTON_RELEASE:
385             ret = pen_handle_button_release(pc, event->button);
386             break;
388         case GDK_2BUTTON_PRESS:
389             ret = pen_handle_2button_press(pc, event->button);
390             break;
392         case GDK_KEY_PRESS:
393             ret = pen_handle_key_press(pc, event);
394             break;
396         default:
397             break;
398     }
400     if (!ret) {
401         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
402             = ((SPEventContextClass *) pen_parent_class)->root_handler;
403         if (parent_root_handler) {
404             ret = parent_root_handler(ec, event);
405         }
406     }
408     return ret;
411 /**
412  * Handle mouse button press event.
413  */
414 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
416     if (pc->events_disabled) {
417         // skip event processing if events are disabled
418         return FALSE;
419     }
421     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
422     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
423     Geom::Point const event_w(bevent.x, bevent.y);
424     Geom::Point event_dt(desktop->w2d(event_w));
425     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
427     gint ret = FALSE;
428     if (bevent.button == 1 && !event_context->space_panning
429         // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
430         && pc->expecting_clicks_for_LPE != 1) {
432         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
433             return TRUE;
434         }
436         if (!pc->grab ) {
437             /* Grab mouse, so release will not pass unnoticed */
438             pc->grab = SP_CANVAS_ITEM(desktop->acetate);
439             sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
440                                             GDK_BUTTON_RELEASE_MASK |
441                                             GDK_POINTER_MOTION_MASK  ),
442                                 NULL, bevent.time);
443         }
445         pen_drag_origin_w = event_w;
446         pen_within_tolerance = true;
448         /* Test whether we hit any anchor. */
449         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
451         switch (pc->mode) {
452             case SP_PEN_CONTEXT_MODE_CLICK:
453                 /* In click mode we add point on release */
454                 switch (pc->state) {
455                     case SP_PEN_CONTEXT_POINT:
456                     case SP_PEN_CONTEXT_CONTROL:
457                     case SP_PEN_CONTEXT_CLOSE:
458                         break;
459                     case SP_PEN_CONTEXT_STOP:
460                         /* This is allowed, if we just canceled curve */
461                         pc->state = SP_PEN_CONTEXT_POINT;
462                         break;
463                     default:
464                         break;
465                 }
466                 break;
467             case SP_PEN_CONTEXT_MODE_DRAG:
468                 switch (pc->state) {
469                     case SP_PEN_CONTEXT_STOP:
470                         /* This is allowed, if we just canceled curve */
471                     case SP_PEN_CONTEXT_POINT:
472                         if (pc->npoints == 0) {
474                                 Geom::Point p;
475                             if (bevent.state & GDK_CONTROL_MASK) {
476                                 p = event_dt;
477                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
478                                         SnapManager &m = desktop->namedview->snap_manager;
479                                         m.setup(desktop);
480                                                                         m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p);
481                                 }
482                                                                 spdc_create_single_dot(event_context, p, "/tools/freehand/pen", bevent.state);
483                                                                 ret = TRUE;
484                                 break;
485                             }
487                             // TODO: Perhaps it would be nicer to rearrange the following case
488                             // distinction so that the case of a waiting LPE is treated separately
490                             /* Set start anchor */
491                             pc->sa = anchor;
492                             if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
493                                 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
494                                    a fresh path to be created so don't continue an existing one */
495                                 p = anchor->dp;
496                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
497                             } else {
498                                 // This is the first click of a new curve; deselect item so that
499                                 // this curve is not combined with it (unless it is drawn from its
500                                 // anchor, which is handled by the sibling branch above)
501                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
502                                 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
503                                     /* if we have a waiting LPE, we need a fresh path to be created
504                                        so don't append to an existing one */
505                                     selection->clear();
506                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
507                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
508                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
509                                 }
511                                 /* Create green anchor */
512                                 p = event_dt;
513                                 if (!pc->polylines_paraxial) {
514                                     // only snap the starting point if we're not in horizontal/vertical mode
515                                     // because otherwise it gets shifted; TODO: why do we snap here at all??
516                                     spdc_endpoint_snap(pc, p, bevent.state);
517                                 }
518                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
519                             }
520                             spdc_pen_set_initial_point(pc, p);
521                         } else {
523                             /* Set end anchor */
524                             pc->ea = anchor;
525                             Geom::Point p;
526                             if (anchor) {
527                                 p = anchor->dp;
528                                 // we hit an anchor, will finish the curve (either with or without closing)
529                                 // in release handler
530                                 pc->state = SP_PEN_CONTEXT_CLOSE;
532                                 if (pc->green_anchor && pc->green_anchor->active) {
533                                     // we clicked on the current curve start, so close it even if
534                                     // we drag a handle away from it
535                                     dc->green_closed = TRUE;
536                                 }
537                                 ret = TRUE;
538                                 break;
540                             } else {
541                                 p = event_dt;
542                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
543                                 spdc_pen_set_subsequent_point(pc, p, true);
544                                 if (pc->polylines_only) {
545                                     spdc_pen_finish_segment(pc, p, bevent.state);
546                                 }
547                             }
548                         }
550                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
551                         ret = TRUE;
552                         break;
553                     case SP_PEN_CONTEXT_CONTROL:
554                         g_warning("Button down in CONTROL state");
555                         break;
556                     case SP_PEN_CONTEXT_CLOSE:
557                         g_warning("Button down in CLOSE state");
558                         break;
559                     default:
560                         break;
561                 }
562                 break;
563             default:
564                 break;
565         }
566     } else if (bevent.button == 3 || pc->expecting_clicks_for_LPE == 1) { // when the last click for a waiting LPE occurs we want to finish the path
567         if (pc->npoints != 0) {
569             spdc_pen_finish_segment(pc, event_dt, bevent.state);
570             if (pc->green_closed) {
571                 // finishing at the start anchor, close curve
572                 spdc_pen_finish(pc, TRUE);
573             } else {
574                 // finishing at some other anchor, finish curve but not close
575                 spdc_pen_finish(pc, FALSE);
576             }
578             ret = TRUE;
579         }
580     }
582     if (pc->expecting_clicks_for_LPE > 0) {
583         --pc->expecting_clicks_for_LPE;
584     }
586     return ret;
589 /**
590  * Handle motion_notify event.
591  */
592 static gint
593 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
595     gint ret = FALSE;
597     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
598     SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
600     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
601         // allow scrolling
602         return FALSE;
603     }
605     if (pc->events_disabled) {
606         // skip motion events if pen events are disabled
607         return FALSE;
608     }
610     Geom::Point const event_w(mevent.x,
611                             mevent.y);
612     if (pen_within_tolerance) {
613         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
614         gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
615         if ( Geom::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
616             return FALSE;   // Do not drag if we're within tolerance from origin.
617         }
618     }
619     // Once the user has moved farther than tolerance from the original location
620     // (indicating they intend to move the object, not click), then always process the
621     // motion notify coordinates as given (no snapping back to origin)
622     pen_within_tolerance = false;
624     /* Find desktop coordinates */
625     Geom::Point p = dt->w2d(event_w);
627     /* Test, whether we hit any anchor */
628     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
630     switch (pc->mode) {
631         case SP_PEN_CONTEXT_MODE_CLICK:
632             switch (pc->state) {
633                 case SP_PEN_CONTEXT_POINT:
634                     if ( pc->npoints != 0 ) {
635                         /* Only set point, if we are already appending */
636                         spdc_endpoint_snap(pc, p, mevent.state);
637                         spdc_pen_set_subsequent_point(pc, p, true);
638                         ret = TRUE;
639                     }
640                     break;
641                 case SP_PEN_CONTEXT_CONTROL:
642                 case SP_PEN_CONTEXT_CLOSE:
643                     /* Placing controls is last operation in CLOSE state */
644                     spdc_endpoint_snap(pc, p, mevent.state);
645                     spdc_pen_set_ctrl(pc, p, mevent.state);
646                     ret = TRUE;
647                     break;
648                 case SP_PEN_CONTEXT_STOP:
649                     /* This is perfectly valid */
650                     break;
651                 default:
652                     break;
653             }
654             break;
655         case SP_PEN_CONTEXT_MODE_DRAG:
656             switch (pc->state) {
657                 case SP_PEN_CONTEXT_POINT:
658                     if ( pc->npoints > 0 ) {
659                         /* Only set point, if we are already appending */
661                         if (!anchor) {   /* Snap node only if not hitting anchor */
662                             spdc_endpoint_snap(pc, p, mevent.state);
663                         }
665                         spdc_pen_set_subsequent_point(pc, p, !anchor, mevent.state);
667                         if (anchor && !pc->anchor_statusbar) {
668                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
669                             pc->anchor_statusbar = true;
670                         } else if (!anchor && pc->anchor_statusbar) {
671                             pc->_message_context->clear();
672                             pc->anchor_statusbar = false;
673                         }
675                         ret = TRUE;
676                     } else {
677                         if (anchor && !pc->anchor_statusbar) {
678                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
679                             pc->anchor_statusbar = true;
680                         } else if (!anchor && pc->anchor_statusbar) {
681                             pc->_message_context->clear();
682                             pc->anchor_statusbar = false;
683                         }
684                     }
685                     break;
686                 case SP_PEN_CONTEXT_CONTROL:
687                 case SP_PEN_CONTEXT_CLOSE:
688                     /* Placing controls is last operation in CLOSE state */
690                     // snap the handle
691                     spdc_endpoint_snap_handle(pc, p, mevent.state);
693                     if (!pc->polylines_only) {
694                         spdc_pen_set_ctrl(pc, p, mevent.state);
695                     } else {
696                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
697                     }
698                     gobble_motion_events(GDK_BUTTON1_MASK);
699                     ret = TRUE;
700                     break;
701                 case SP_PEN_CONTEXT_STOP:
702                     /* This is perfectly valid */
703                     break;
704                 default:
705                     break;
706             }
707             break;
708         default:
709             break;
710     }
711     return ret;
714 /**
715  * Handle mouse button release event.
716  */
717 static gint
718 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
720     if (pc->events_disabled) {
721         // skip event processing if events are disabled
722         return FALSE;
723     }
725     gint ret = FALSE;
726     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
727     if ( revent.button == 1  && !event_context->space_panning) {
729         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
731         Geom::Point const event_w(revent.x,
732                                 revent.y);
733         /* Find desktop coordinates */
734         Geom::Point p = pc->desktop->w2d(event_w);
736         /* Test whether we hit any anchor. */
737         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
739         switch (pc->mode) {
740             case SP_PEN_CONTEXT_MODE_CLICK:
741                 switch (pc->state) {
742                     case SP_PEN_CONTEXT_POINT:
743                         if ( pc->npoints == 0 ) {
744                             /* Start new thread only with button release */
745                             if (anchor) {
746                                 p = anchor->dp;
747                             }
748                             pc->sa = anchor;
749                             spdc_pen_set_initial_point(pc, p);
750                         } else {
751                             /* Set end anchor here */
752                             pc->ea = anchor;
753                             if (anchor) {
754                                 p = anchor->dp;
755                             }
756                         }
757                         pc->state = SP_PEN_CONTEXT_CONTROL;
758                         ret = TRUE;
759                         break;
760                     case SP_PEN_CONTEXT_CONTROL:
761                         /* End current segment */
762                         spdc_endpoint_snap(pc, p, revent.state);
763                         spdc_pen_finish_segment(pc, p, revent.state);
764                         pc->state = SP_PEN_CONTEXT_POINT;
765                         ret = TRUE;
766                         break;
767                     case SP_PEN_CONTEXT_CLOSE:
768                         /* End current segment */
769                         if (!anchor) {   /* Snap node only if not hitting anchor */
770                             spdc_endpoint_snap(pc, p, revent.state);
771                         }
772                         spdc_pen_finish_segment(pc, p, revent.state);
773                         spdc_pen_finish(pc, TRUE);
774                         pc->state = SP_PEN_CONTEXT_POINT;
775                         ret = TRUE;
776                         break;
777                     case SP_PEN_CONTEXT_STOP:
778                         /* This is allowed, if we just canceled curve */
779                         pc->state = SP_PEN_CONTEXT_POINT;
780                         ret = TRUE;
781                         break;
782                     default:
783                         break;
784                 }
785                 break;
786             case SP_PEN_CONTEXT_MODE_DRAG:
787                 switch (pc->state) {
788                     case SP_PEN_CONTEXT_POINT:
789                     case SP_PEN_CONTEXT_CONTROL:
790                         if (!pc->polylines_only) {
791                             spdc_endpoint_snap(pc, p, revent.state);
792                             spdc_pen_finish_segment(pc, p, revent.state);
793                         }
794                         break;
795                     case SP_PEN_CONTEXT_CLOSE:
796                         spdc_endpoint_snap(pc, p, revent.state);
797                         spdc_pen_finish_segment(pc, p, revent.state);
798                         if (pc->green_closed) {
799                             // finishing at the start anchor, close curve
800                             spdc_pen_finish(pc, TRUE);
801                         } else {
802                             // finishing at some other anchor, finish curve but not close
803                             spdc_pen_finish(pc, FALSE);
804                         }
805                         break;
806                     case SP_PEN_CONTEXT_STOP:
807                         /* This is allowed, if we just cancelled curve */
808                         break;
809                     default:
810                         break;
811                 }
812                 pc->state = SP_PEN_CONTEXT_POINT;
813                 ret = TRUE;
814                 break;
815             default:
816                 break;
817         }
819         if (pc->grab) {
820             /* Release grab now */
821             sp_canvas_item_ungrab(pc->grab, revent.time);
822             pc->grab = NULL;
823         }
825         ret = TRUE;
827         dc->green_closed = FALSE;
828     }
830     // TODO: can we be sure that the path was created correctly?
831     // TODO: should we offer an option to collect the clicks in a list?
832     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
833         sp_pen_context_set_polyline_mode(pc);
835         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
836         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
838         if (pc->waiting_LPE) {
839             // we have an already created LPE waiting for a path
840             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
841             selection->add(SP_OBJECT(pc->waiting_item));
842             pc->waiting_LPE = NULL;
843         } else {
844             // the case that we need to create a new LPE and apply it to the just-drawn path is
845             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
846         }
847     }
849     return ret;
852 static gint
853 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
855     gint ret = FALSE;
856     if (pc->npoints != 0 && bevent.button != 2) {
857         spdc_pen_finish(pc, FALSE);
858         ret = TRUE;
859     }
860     return ret;
863 void
864 pen_redraw_all (SPPenContext *const pc)
866     // green
867     if (pc->green_bpaths) {
868         // remove old piecewise green canvasitems
869         while (pc->green_bpaths) {
870             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
871             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
872         }
873         // one canvas bpath for all of green_curve
874         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
875         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
876         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
878         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
879     }
881     if (pc->green_anchor)
882         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
884     pc->red_curve->reset();
885     pc->red_curve->moveto(pc->p[0]);
886     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
887     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
889     // handles
890     if (pc->p[0] != pc->p[1]) {
891         SP_CTRL(pc->c1)->moveto(pc->p[1]);
892         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
893         sp_canvas_item_show (pc->c1);
894         sp_canvas_item_show (pc->cl1);
895     } else {
896         sp_canvas_item_hide (pc->c1);
897         sp_canvas_item_hide (pc->cl1);
898     }
900     Geom::Curve const * last_seg = pc->green_curve->last_segment();
901     if (last_seg) {
902         Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
903         if ( cubic &&
904              (*cubic)[2] != to_2geom(pc->p[0]) )
905         {
906             Geom::Point p2 = (*cubic)[2];
907             SP_CTRL(pc->c0)->moveto(p2);
908             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
909             sp_canvas_item_show (pc->c0);
910             sp_canvas_item_show (pc->cl0);
911         } else {
912             sp_canvas_item_hide (pc->c0);
913             sp_canvas_item_hide (pc->cl0);
914         }
915     }
918 void
919 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
921     if (pc->npoints != 5)
922         return;
924     // green
925     if (!pc->green_curve->is_empty()) {
926         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
927     } else {
928         // start anchor too
929         if (pc->green_anchor) {
930             pc->green_anchor->dp += Geom::Point(x, y);
931         }
932     }
934     // red
935     pc->p[0] += Geom::Point(x, y);
936     pc->p[1] += Geom::Point(x, y);
937     pen_redraw_all(pc);
940 void
941 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
943     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
946 void
947 pen_lastpoint_tocurve (SPPenContext *const pc)
949     if (pc->npoints != 5)
950         return;
952     Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
953     if ( cubic ) {
954         pc->p[1] = pc->p[0] + (Geom::Point)( (*cubic)[3] - (*cubic)[2] );
955     } else {
956         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
957     }
959     pen_redraw_all(pc);
962 void
963 pen_lastpoint_toline (SPPenContext *const pc)
965     if (pc->npoints != 5)
966         return;
968     pc->p[1] = pc->p[0];
970     pen_redraw_all(pc);
974 static gint
975 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
978         gint ret = FALSE;
979     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
980     gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
982     switch (get_group0_keyval (&event->key)) {
984         case GDK_Left: // move last point left
985         case GDK_KP_Left:
986         case GDK_KP_4:
987             if (!MOD__CTRL) { // not ctrl
988                 if (MOD__ALT) { // alt
989                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
990                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
991                 }
992                 else { // no alt
993                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
994                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
995                 }
996                 ret = TRUE;
997             }
998             break;
999         case GDK_Up: // move last point up
1000         case GDK_KP_Up:
1001         case GDK_KP_8:
1002             if (!MOD__CTRL) { // not ctrl
1003                 if (MOD__ALT) { // alt
1004                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
1005                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
1006                 }
1007                 else { // no alt
1008                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
1009                     else pen_lastpoint_move(pc, 0, nudge); // no shift
1010                 }
1011                 ret = TRUE;
1012             }
1013             break;
1014         case GDK_Right: // move last point right
1015         case GDK_KP_Right:
1016         case GDK_KP_6:
1017             if (!MOD__CTRL) { // not ctrl
1018                 if (MOD__ALT) { // alt
1019                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1020                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1021                 }
1022                 else { // no alt
1023                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1024                     else pen_lastpoint_move(pc, nudge, 0); // no shift
1025                 }
1026                 ret = TRUE;
1027             }
1028             break;
1029         case GDK_Down: // move last point down
1030         case GDK_KP_Down:
1031         case GDK_KP_2:
1032             if (!MOD__CTRL) { // not ctrl
1033                 if (MOD__ALT) { // alt
1034                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1035                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1036                 }
1037                 else { // no alt
1038                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1039                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
1040                 }
1041                 ret = TRUE;
1042             }
1043             break;
1045         case GDK_P:
1046         case GDK_p:
1047             if (MOD__SHIFT_ONLY) {
1048                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1049                 ret = TRUE;
1050             }
1051             break;
1053         case GDK_C:
1054         case GDK_c:
1055             if (MOD__SHIFT_ONLY) {
1056                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1057                 ret = TRUE;
1058             }
1059             break;
1061         case GDK_B:
1062         case GDK_b:
1063             if (MOD__SHIFT_ONLY) {
1064                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1065                 ret = TRUE;
1066             }
1067             break;
1069         case GDK_A:
1070         case GDK_a:
1071             if (MOD__SHIFT_ONLY) {
1072                 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1073                 ret = TRUE;
1074             }
1075             break;
1077         case GDK_U:
1078         case GDK_u:
1079             if (MOD__SHIFT_ONLY) {
1080                 pen_lastpoint_tocurve(pc);
1081                 ret = TRUE;
1082             }
1083             break;
1084         case GDK_L:
1085         case GDK_l:
1086             if (MOD__SHIFT_ONLY) {
1087                 pen_lastpoint_toline(pc);
1088                 ret = TRUE;
1089             }
1090             break;
1092         case GDK_Return:
1093         case GDK_KP_Enter:
1094             if (pc->npoints != 0) {
1095                 spdc_pen_finish(pc, FALSE);
1096                 ret = TRUE;
1097             }
1098             break;
1099         case GDK_Escape:
1100             if (pc->npoints != 0) {
1101                 // if drawing, cancel, otherwise pass it up for deselecting
1102                 pen_cancel (pc);
1103                 ret = TRUE;
1104             }
1105             break;
1106         case GDK_z:
1107         case GDK_Z:
1108             if (MOD__CTRL_ONLY && pc->npoints != 0) {
1109                 // if drawing, cancel, otherwise pass it up for undo
1110                 pen_cancel (pc);
1111                 ret = TRUE;
1112             }
1113             break;
1114         case GDK_g:
1115         case GDK_G:
1116             if (MOD__SHIFT_ONLY) {
1117                 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
1118                 ret = true;
1119             }
1120             break;
1121         case GDK_BackSpace:
1122         case GDK_Delete:
1123         case GDK_KP_Delete:
1124             if ( pc->green_curve->is_empty() || (pc->green_curve->last_segment() == NULL) ) {
1125                 if (!pc->red_curve->is_empty()) {
1126                     pen_cancel (pc);
1127                     ret = TRUE;
1128                 } else {
1129                     // do nothing; this event should be handled upstream
1130                 }
1131             } else {
1132                 /* Reset red curve */
1133                 pc->red_curve->reset();
1134                 /* Destroy topmost green bpath */
1135                 if (pc->green_bpaths) {
1136                     if (pc->green_bpaths->data)
1137                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1138                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1139                 }
1140                 /* Get last segment */
1141                 if ( pc->green_curve->is_empty() ) {
1142                     g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1143                     break;
1144                 }
1145                 // The code below assumes that pc->green_curve has only ONE path !
1146                 Geom::Curve const * crv = pc->green_curve->last_segment();
1147                 pc->p[0] = crv->initialPoint();
1148                 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1149                     pc->p[1] = (*cubic)[1];
1150                 } else {
1151                     pc->p[1] = pc->p[0];
1152                 }
1153                 Geom::Point const pt(( pc->npoints < 4
1154                                      ? (Geom::Point)(crv->finalPoint())
1155                                      : pc->p[3] ));
1156                 pc->npoints = 2;
1157                 pc->green_curve->backspace();
1158                 sp_canvas_item_hide(pc->c0);
1159                 sp_canvas_item_hide(pc->c1);
1160                 sp_canvas_item_hide(pc->cl0);
1161                 sp_canvas_item_hide(pc->cl1);
1162                 pc->state = SP_PEN_CONTEXT_POINT;
1163                 spdc_pen_set_subsequent_point(pc, pt, true);
1164                 ret = TRUE;
1165             }
1166             break;
1167         default:
1168             break;
1169     }
1170     return ret;
1173 static void
1174 spdc_reset_colors(SPPenContext *pc)
1176     /* Red */
1177     pc->red_curve->reset();
1178     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1179     /* Blue */
1180     pc->blue_curve->reset();
1181     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1182     /* Green */
1183     while (pc->green_bpaths) {
1184         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1185         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1186     }
1187     pc->green_curve->reset();
1188     if (pc->green_anchor) {
1189         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1190     }
1191     pc->sa = NULL;
1192     pc->ea = NULL;
1193     pc->npoints = 0;
1194     pc->red_curve_is_valid = false;
1198 static void
1199 spdc_pen_set_initial_point(SPPenContext *const pc, Geom::Point const p)
1201     g_assert( pc->npoints == 0 );
1203     pc->p[0] = p;
1204     pc->p[1] = p;
1205     pc->npoints = 2;
1206     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1208     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1211 /**
1212  * Show the status message for the current line/curve segment.
1213  * This type of message always shows angle/distance as the last
1214  * two parameters ("angle %3.2f&#176;, distance %s").
1215  */
1216 static void
1217 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, Geom::Point const p, int pc_point_to_compare, gchar const *message)
1219     g_assert(pc != NULL);
1220     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1221     g_assert(message != NULL);
1223     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1224     Geom::Point rel = p - pc->p[pc_point_to_compare];
1225     GString *dist = SP_PX_TO_METRIC_STRING(Geom::L2(rel), desktop->namedview->getDefaultMetric());
1226     double angle = atan2(rel[Geom::Y], rel[Geom::X]) * 180 / M_PI;
1227     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1228     if (prefs->getBool("/options/compassangledisplay/value", 0) != 0)
1229         angle = angle_to_compass (angle);
1231     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1232     g_string_free(dist, FALSE);
1235 static void
1236 spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status)
1238     g_assert( pc->npoints != 0 );
1239     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1241     pc->p[2] = p;
1242     pc->p[3] = p;
1243     pc->p[4] = p;
1244     pc->npoints = 5;
1245     pc->red_curve->reset();
1246     bool is_curve;
1247     pc->red_curve->moveto(pc->p[0]);
1248     if (pc->polylines_paraxial && !statusbar) {
1249         // we are drawing horizontal/vertical lines and hit an anchor; draw an L-shaped path
1250         Geom::Point intermed = p;
1251         pen_set_to_nearest_horiz_vert(pc, intermed, status);
1252         pc->red_curve->lineto(intermed);
1253         pc->red_curve->lineto(p);
1254         is_curve = false;
1255     } else {
1256         // one of the 'regular' modes
1257         if (pc->p[1] != pc->p[0])
1258         {
1259             pc->red_curve->curveto(pc->p[1], p, p);
1260             is_curve = true;
1261         } else {
1262             pc->red_curve->lineto(p);
1263             is_curve = false;
1264         }
1265     }
1267     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1269     if (statusbar) {
1270         gchar *message = is_curve ?
1271             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1272             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1273         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1274     }
1277 static void
1278 spdc_pen_set_ctrl(SPPenContext *const pc, Geom::Point const p, guint const state)
1280     sp_canvas_item_show(pc->c1);
1281     sp_canvas_item_show(pc->cl1);
1283     if ( pc->npoints == 2 ) {
1284         pc->p[1] = p;
1285         sp_canvas_item_hide(pc->c0);
1286         sp_canvas_item_hide(pc->cl0);
1287         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1288         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1290         spdc_pen_set_angle_distance_status_message(pc, p, 0, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"));
1291     } else if ( pc->npoints == 5 ) {
1292         pc->p[4] = p;
1293         sp_canvas_item_show(pc->c0);
1294         sp_canvas_item_show(pc->cl0);
1295         bool is_symm = false;
1296         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1297              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1298             Geom::Point delta = p - pc->p[3];
1299             pc->p[2] = pc->p[3] - delta;
1300             is_symm = true;
1301             pc->red_curve->reset();
1302             pc->red_curve->moveto(pc->p[0]);
1303             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1304             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1305         }
1306         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1307         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1308         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1309         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1311         gchar *message = is_symm ?
1312             _("<b>Curve handle, symmetric</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only") :
1313             _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only");
1314         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1315     } else {
1316         g_warning("Something bad happened - npoints is %d", pc->npoints);
1317     }
1320 static void
1321 spdc_pen_finish_segment(SPPenContext *const pc, Geom::Point const p, guint const state)
1323     if (pc->polylines_paraxial) {
1324         pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1325     }
1326     ++pc->num_clicks;
1328     if (!pc->red_curve->is_empty()) {
1329         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1330         SPCurve *curve = pc->red_curve->copy();
1331         /// \todo fixme:
1332         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1333         curve->unref();
1334         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1336         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1338         pc->p[0] = pc->p[3];
1339         pc->p[1] = pc->p[4];
1340         pc->npoints = 2;
1342         pc->red_curve->reset();
1343     }
1346 static void
1347 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1349     if (pc->expecting_clicks_for_LPE > 1) {
1350         // don't let the path be finished before we have collected the required number of mouse clicks
1351         return;
1352     }
1354     pc->num_clicks = 0;
1356     pen_disable_events(pc);
1358     SPDesktop *const desktop = pc->desktop;
1359     pc->_message_context->clear();
1360     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1362     pc->red_curve->reset();
1363     spdc_concat_colors_and_flush(pc, closed);
1364     pc->sa = NULL;
1365     pc->ea = NULL;
1367     pc->npoints = 0;
1368     pc->state = SP_PEN_CONTEXT_POINT;
1370     sp_canvas_item_hide(pc->c0);
1371     sp_canvas_item_hide(pc->c1);
1372     sp_canvas_item_hide(pc->cl0);
1373     sp_canvas_item_hide(pc->cl1);
1375     if (pc->green_anchor) {
1376         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1377     }
1380     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1382     pen_enable_events(pc);
1385 static void
1386 pen_disable_events(SPPenContext *const pc) {
1387   pc->events_disabled++;
1390 static void
1391 pen_enable_events(SPPenContext *const pc) {
1392   g_return_if_fail(pc->events_disabled != 0);
1394   pc->events_disabled--;
1397 void
1398 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1399                                          unsigned int num_clicks, bool use_polylines)
1401     if (effect_type == Inkscape::LivePathEffect::INVALID_LPE)
1402         return;
1404     pc->waiting_LPE_type = effect_type;
1405     pc->expecting_clicks_for_LPE = num_clicks;
1406     pc->polylines_only = use_polylines;
1407     pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
1410 void
1411 sp_pen_context_cancel_waiting_for_LPE(SPPenContext *pc)
1413     pc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
1414     pc->expecting_clicks_for_LPE = 0;
1415     sp_pen_context_set_polyline_mode(pc);
1418 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1419                                        Geom::Point const &pt, Geom::Point const &origin, guint state) {
1420     /*
1421      * after the first mouse click we determine whether the mouse pointer is closest to a
1422      * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1423      * orthogonal to the last one; pressing Shift toggles the direction
1424      */
1425     if (pc->num_clicks == 0) {
1426         // first mouse click
1427         double dist_h = fabs(pt[Geom::X] - origin[Geom::X]);
1428         double dist_v = fabs(pt[Geom::Y] - origin[Geom::Y]);
1429         int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1430         pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1431         return pen_last_paraxial_dir;
1432     } else {
1433         // subsequent mouse click
1434         return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1435     }
1438 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state)
1440     Geom::Point const &origin = pc->p[0];
1442     int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1444     if (next_dir == 0) {
1445         // line is forced to be horizontal
1446         pt[Geom::Y] = origin[Geom::Y];
1447     } else {
1448         // line is forced to be vertical
1449         pt[Geom::X] = origin[Geom::X];
1450     }
1453 Geom::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, Geom::Point const &pt)
1455     Geom::Point const &origin = pc->p[0];
1457     if (pen_last_paraxial_dir == 0) {
1458         return Geom::Point (origin[Geom::X], pt[Geom::Y]);
1459     } else {
1460         return Geom::Point (pt[Geom::X], origin[Geom::Y]);
1461     }
1464 /*
1465   Local Variables:
1466   mode:c++
1467   c-file-style:"stroustrup"
1468   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1469   indent-tabs-mode:nil
1470   fill-column:99
1471   End:
1472 */
1473 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :