Code

New 'polylines only' mode in pen context; to be used later when LPEs are waiting...
[inkscape.git] / src / pen-context.cpp
1 /** \file
2  * Pen event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
19 #include <cstring>
20 #include <string>
22 #include "pen-context.h"
23 #include "sp-namedview.h"
24 #include "sp-metrics.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "selection.h"
28 #include "selection-chemistry.h"
29 #include "draw-anchor.h"
30 #include "message-stack.h"
31 #include "message-context.h"
32 #include "prefs-utils.h"
33 #include "sp-path.h"
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/n-art-bpath.h"
41 #include "libnr/nr-point-ops.h"
42 #include "helper/units.h"
43 #include "macros.h"
44 #include "context-fns.h"
47 static void sp_pen_context_class_init(SPPenContextClass *klass);
48 static void sp_pen_context_init(SPPenContext *pc);
49 static void sp_pen_context_dispose(GObject *object);
51 static void sp_pen_context_setup(SPEventContext *ec);
52 static void sp_pen_context_finish(SPEventContext *ec);
53 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
54 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
55 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
57 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
58 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
59 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
60 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
62 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
64 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
65 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
66 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
67 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
68 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
69 static void spdc_reset_colors(SPPenContext *pc);
71 static void pen_disable_events(SPPenContext *const pc);
72 static void pen_enable_events(SPPenContext *const pc);
74 static NR::Point pen_drag_origin_w(0, 0);
75 static bool pen_within_tolerance = false;
77 static SPDrawContextClass *pen_parent_class;
79 /**
80  * Register SPPenContext with Gdk and return its type.
81  */
82 GType
83 sp_pen_context_get_type(void)
84 {
85     static GType type = 0;
86     if (!type) {
87         GTypeInfo info = {
88             sizeof(SPPenContextClass),
89             NULL, NULL,
90             (GClassInitFunc) sp_pen_context_class_init,
91             NULL, NULL,
92             sizeof(SPPenContext),
93             4,
94             (GInstanceInitFunc) sp_pen_context_init,
95             NULL,   /* value_table */
96         };
97         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
98     }
99     return type;
102 /**
103  * Initialize the SPPenContext vtable.
104  */
105 static void
106 sp_pen_context_class_init(SPPenContextClass *klass)
108     GObjectClass *object_class;
109     SPEventContextClass *event_context_class;
111     object_class = (GObjectClass *) klass;
112     event_context_class = (SPEventContextClass *) klass;
114     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
116     object_class->dispose = sp_pen_context_dispose;
118     event_context_class->setup = sp_pen_context_setup;
119     event_context_class->finish = sp_pen_context_finish;
120     event_context_class->set = sp_pen_context_set;
121     event_context_class->root_handler = sp_pen_context_root_handler;
122     event_context_class->item_handler = sp_pen_context_item_handler;
125 /**
126  * Callback to initialize SPPenContext object.
127  */
128 static void
129 sp_pen_context_init(SPPenContext *pc)
132     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
134     event_context->cursor_shape = cursor_pen_xpm;
135     event_context->hot_x = 4;
136     event_context->hot_y = 4;
138     pc->npoints = 0;
139     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
140     pc->state = SP_PEN_CONTEXT_POINT;
142     pc->c0 = NULL;
143     pc->c1 = NULL;
144     pc->cl0 = NULL;
145     pc->cl1 = NULL;
146     
147     pc->events_disabled = 0;
149     pc->polylines_only = false;
152 /**
153  * Callback to destroy the SPPenContext object's members and itself.
154  */
155 static void
156 sp_pen_context_dispose(GObject *object)
158     SPPenContext *pc;
160     pc = SP_PEN_CONTEXT(object);
162     if (pc->c0) {
163         gtk_object_destroy(GTK_OBJECT(pc->c0));
164         pc->c0 = NULL;
165     }
166     if (pc->c1) {
167         gtk_object_destroy(GTK_OBJECT(pc->c1));
168         pc->c1 = NULL;
169     }
170     if (pc->cl0) {
171         gtk_object_destroy(GTK_OBJECT(pc->cl0));
172         pc->cl0 = NULL;
173     }
174     if (pc->cl1) {
175         gtk_object_destroy(GTK_OBJECT(pc->cl1));
176         pc->cl1 = NULL;
177     }
179     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
182 /**
183  * Callback to initialize SPPenContext object.
184  */
185 static void
186 sp_pen_context_setup(SPEventContext *ec)
188     SPPenContext *pc;
190     pc = SP_PEN_CONTEXT(ec);
192     if (((SPEventContextClass *) pen_parent_class)->setup) {
193         ((SPEventContextClass *) pen_parent_class)->setup(ec);
194     }
196     /* Pen indicators */
197     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
198                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
199     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
200                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
201     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
202     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
203     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
204     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
206     sp_canvas_item_hide(pc->c0);
207     sp_canvas_item_hide(pc->c1);
208     sp_canvas_item_hide(pc->cl0);
209     sp_canvas_item_hide(pc->cl1);
211     sp_event_context_read(ec, "mode");
213     pc->anchor_statusbar = false;
215     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
216         ec->enableSelectionCue();
217     }
220 static void
221 pen_cancel (SPPenContext *const pc) 
223     pc->state = SP_PEN_CONTEXT_STOP;
224     spdc_reset_colors(pc);
225     sp_canvas_item_hide(pc->c0);
226     sp_canvas_item_hide(pc->c1);
227     sp_canvas_item_hide(pc->cl0);
228     sp_canvas_item_hide(pc->cl1);
229     pc->_message_context->clear();
230     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
232     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
235 /**
236  * Finalization callback.
237  */
238 static void
239 sp_pen_context_finish(SPEventContext *ec)
241     SPPenContext *pc = SP_PEN_CONTEXT(ec);
243     if (pc->npoints != 0) {
244         pen_cancel (pc);
245     }
247     if (((SPEventContextClass *) pen_parent_class)->finish) {
248         ((SPEventContextClass *) pen_parent_class)->finish(ec);
249     }
252 /**
253  * Callback that sets key to value in pen context.
254  */
255 static void
256 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
258     SPPenContext *pc = SP_PEN_CONTEXT(ec);
260     if (!strcmp(key, "mode")) {
261         if ( val && !strcmp(val, "drag") ) {
262             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
263         } else {
264             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
265         }
266     }
269 /**
270  * Snaps new node relative to the previous node.
271  */
272 static void
273 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
275     if (pc->npoints > 0) {
276         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
277     }
279     spdc_endpoint_snap_free(pc, p, state);
282 /**
283  * Snaps new node's handle relative to the new node.
284  */
285 static void
286 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
288     g_return_if_fail(( pc->npoints == 2 ||
289                        pc->npoints == 5   ));
291     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
292     spdc_endpoint_snap_free(pc, p, state);
295 static gint 
296 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
298     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
300     gint ret = FALSE;
302     switch (event->type) {
303         case GDK_BUTTON_PRESS:
304             ret = pen_handle_button_press(pc, event->button);
305             break;
306         default:
307             break;
308     }
310     if (!ret) {
311         if (((SPEventContextClass *) pen_parent_class)->item_handler)
312             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
313     }
315     return ret;
318 /**
319  * Callback to handle all pen events.
320  */
321 static gint
322 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
324     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
326     gint ret = FALSE;
328     switch (event->type) {
329         case GDK_BUTTON_PRESS:
330             ret = pen_handle_button_press(pc, event->button);
331             break;
333         case GDK_MOTION_NOTIFY:
334             ret = pen_handle_motion_notify(pc, event->motion);
335             break;
337         case GDK_BUTTON_RELEASE:
338             ret = pen_handle_button_release(pc, event->button);
339             break;
341         case GDK_2BUTTON_PRESS:
342             ret = pen_handle_2button_press(pc, event->button);
343             break;
345         case GDK_KEY_PRESS:
346             ret = pen_handle_key_press(pc, event);
347             break;
349         default:
350             break;
351     }
353     if (!ret) {
354         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
355             = ((SPEventContextClass *) pen_parent_class)->root_handler;
356         if (parent_root_handler) {
357             ret = parent_root_handler(ec, event);
358         }
359     }
361     return ret;
364 /**
365  * Handle mouse button press event.
366  */
367 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
369     if (pc->events_disabled) {
370         // skip event processing if events are disabled
371         return FALSE;
372     }
374     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
375     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
376     NR::Point const event_w(bevent.x, bevent.y);
377     NR::Point const event_dt(desktop->w2d(event_w));
378     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
380     gint ret = FALSE;
381     if (bevent.button == 1 && !event_context->space_panning) {
383         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
384             return TRUE;
385         }
387         pen_drag_origin_w = event_w;
388         pen_within_tolerance = true;
390         /* Test whether we hit any anchor. */
391         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
393         switch (pc->mode) {
394             case SP_PEN_CONTEXT_MODE_CLICK:
395                 /* In click mode we add point on release */
396                 switch (pc->state) {
397                     case SP_PEN_CONTEXT_POINT:
398                     case SP_PEN_CONTEXT_CONTROL:
399                     case SP_PEN_CONTEXT_CLOSE:
400                         break;
401                     case SP_PEN_CONTEXT_STOP:
402                         /* This is allowed, if we just cancelled curve */
403                         pc->state = SP_PEN_CONTEXT_POINT;
404                         break;
405                     default:
406                         break;
407                 }
408                 break;
409             case SP_PEN_CONTEXT_MODE_DRAG:
410                 switch (pc->state) {
411                     case SP_PEN_CONTEXT_STOP:
412                         /* This is allowed, if we just cancelled curve */
413                     case SP_PEN_CONTEXT_POINT:
414                         if (pc->npoints == 0) {
416                             if (bevent.state & GDK_CONTROL_MASK) {
417                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
418                                 ret = TRUE;
419                                 break;
420                             }
422                             /* Set start anchor */
423                             pc->sa = anchor;
424                             NR::Point p;
425                             if (anchor) {
427                                 /* Adjust point to anchor if needed */
428                                 p = anchor->dp;
429                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
431                             } else {
433                                 // This is the first click of a new curve; deselect item so that
434                                 // this curve is not combined with it (unless it is drawn from its
435                                 // anchor, which is handled by the sibling branch above)
436                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
437                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
439                                     selection->clear();
440                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
442                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
444                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
445                                 }
447                                 /* Create green anchor */
448                                 p = event_dt;
449                                 spdc_endpoint_snap(pc, p, bevent.state);
450                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
451                             }
452                             spdc_pen_set_initial_point(pc, p);
453                         } else {
455                             /* Set end anchor */
456                             pc->ea = anchor;
457                             NR::Point p;
458                             if (anchor) {
460                                 p = anchor->dp;
461                                 // we hit an anchor, will finish the curve (either with or without closing)
462                                 // in release handler
463                                 pc->state = SP_PEN_CONTEXT_CLOSE;
465                                 if (pc->green_anchor && pc->green_anchor->active) {
466                                     // we clicked on the current curve start, so close it even if
467                                     // we drag a handle away from it
468                                     dc->green_closed = TRUE;
469                                 }
470                                 ret = TRUE;
471                                 break;
473                             } else {
475                                 p = event_dt;
476                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
477                                 spdc_pen_set_subsequent_point(pc, p, true);
478                                 if (pc->polylines_only) {
479                                     spdc_pen_finish_segment(pc, p, bevent.state);
480                                 }
481                             }
483                         }
484                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
485                         ret = TRUE;
486                         break;
487                     case SP_PEN_CONTEXT_CONTROL:
488                         g_warning("Button down in CONTROL state");
489                         break;
490                     case SP_PEN_CONTEXT_CLOSE:
491                         g_warning("Button down in CLOSE state");
492                         break;
493                     default:
494                         break;
495                 }
496                 break;
497             default:
498                 break;
499         }
500     } else if (bevent.button == 3) {
501         if (pc->npoints != 0) {
503             spdc_pen_finish_segment(pc, event_dt, bevent.state);
504             if (pc->green_closed) {
505                 // finishing at the start anchor, close curve
506                 spdc_pen_finish(pc, TRUE);
507             } else {
508                 // finishing at some other anchor, finish curve but not close
509                 spdc_pen_finish(pc, FALSE);
510             }
512             ret = TRUE;
513         }
514     }
516     return ret;
519 /**
520  * Handle motion_notify event.
521  */
522 static gint
523 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
525     gint ret = FALSE;
527     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
529     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
530         // allow scrolling
531         return FALSE;
532     }
533     
534     if (pc->events_disabled) {
535         // skip motion events if pen events are disabled
536         return FALSE;
537     }
539     NR::Point const event_w(mevent.x,
540                             mevent.y);
541     if (pen_within_tolerance) {
542         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
543                                                                "value", 0, 0, 100);
544         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
545             return FALSE;   // Do not drag if we're within tolerance from origin.
546         }
547     }
548     // Once the user has moved farther than tolerance from the original location
549     // (indicating they intend to move the object, not click), then always process the
550     // motion notify coordinates as given (no snapping back to origin)
551     pen_within_tolerance = false;
553     SPDesktop *const dt = pc->desktop;
554     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
555         /* Grab mouse, so release will not pass unnoticed */
556         pc->grab = SP_CANVAS_ITEM(dt->acetate);
557         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
558                                         GDK_BUTTON_RELEASE_MASK |
559                                         GDK_POINTER_MOTION_MASK  ),
560                             NULL, mevent.time);
561     }
563     /* Find desktop coordinates */
564     NR::Point p = dt->w2d(event_w);
566     /* Test, whether we hit any anchor */
567     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
569     switch (pc->mode) {
570         case SP_PEN_CONTEXT_MODE_CLICK:
571             switch (pc->state) {
572                 case SP_PEN_CONTEXT_POINT:
573                     if ( pc->npoints != 0 ) {
574                         /* Only set point, if we are already appending */
575                         spdc_endpoint_snap(pc, p, mevent.state);
576                         spdc_pen_set_subsequent_point(pc, p, true);
577                         ret = TRUE;
578                     }
579                     break;
580                 case SP_PEN_CONTEXT_CONTROL:
581                 case SP_PEN_CONTEXT_CLOSE:
582                     /* Placing controls is last operation in CLOSE state */
583                     spdc_endpoint_snap(pc, p, mevent.state);
584                     spdc_pen_set_ctrl(pc, p, mevent.state);
585                     ret = TRUE;
586                     break;
587                 case SP_PEN_CONTEXT_STOP:
588                     /* This is perfectly valid */
589                     break;
590                 default:
591                     break;
592             }
593             break;
594         case SP_PEN_CONTEXT_MODE_DRAG:
595             switch (pc->state) {
596                 case SP_PEN_CONTEXT_POINT:
597                     if ( pc->npoints > 0 ) {
598                         /* Only set point, if we are already appending */
600                         if (!anchor) {   /* Snap node only if not hitting anchor */
601                             spdc_endpoint_snap(pc, p, mevent.state);
602                         }
604                         spdc_pen_set_subsequent_point(pc, p, !anchor);
606                         if (anchor && !pc->anchor_statusbar) {
607                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
608                             pc->anchor_statusbar = true;
609                         } else if (!anchor && pc->anchor_statusbar) {
610                             pc->_message_context->clear();
611                             pc->anchor_statusbar = false;
612                         }
614                         ret = TRUE;
615                     } else {
616                         if (anchor && !pc->anchor_statusbar) {
617                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
618                             pc->anchor_statusbar = true;
619                         } else if (!anchor && pc->anchor_statusbar) {
620                             pc->_message_context->clear();
621                             pc->anchor_statusbar = false;
622                         }
623                     }
624                     break;
625                 case SP_PEN_CONTEXT_CONTROL:
626                 case SP_PEN_CONTEXT_CLOSE:
627                     /* Placing controls is last operation in CLOSE state */
629                     // snap the handle
630                     spdc_endpoint_snap_handle(pc, p, mevent.state);
632                     if (!pc->polylines_only) {
633                         spdc_pen_set_ctrl(pc, p, mevent.state);
634                     } else {
635                         spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
636                     }
637                     gobble_motion_events(GDK_BUTTON1_MASK);
638                     ret = TRUE;
639                     break;
640                 case SP_PEN_CONTEXT_STOP:
641                     /* This is perfectly valid */
642                     break;
643                 default:
644                     break;
645             }
646             break;
647         default:
648             break;
649     }
650     return ret;
653 /**
654  * Handle mouse button release event.
655  */
656 static gint
657 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
659     if (pc->events_disabled) {
660         // skip event processing if events are disabled
661         return FALSE;
662     }
664     gint ret = FALSE;
665     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
666     if ( revent.button == 1  && !event_context->space_panning) {
668         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
670         NR::Point const event_w(revent.x,
671                                 revent.y);
672         /* Find desktop coordinates */
673         NR::Point p = pc->desktop->w2d(event_w);
675         /* Test whether we hit any anchor. */
676         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
678         switch (pc->mode) {
679             case SP_PEN_CONTEXT_MODE_CLICK:
680                 switch (pc->state) {
681                     case SP_PEN_CONTEXT_POINT:
682                         if ( pc->npoints == 0 ) {
683                             /* Start new thread only with button release */
684                             if (anchor) {
685                                 p = anchor->dp;
686                             }
687                             pc->sa = anchor;
688                             spdc_pen_set_initial_point(pc, p);
689                         } else {
690                             /* Set end anchor here */
691                             pc->ea = anchor;
692                             if (anchor) {
693                                 p = anchor->dp;
694                             }
695                         }
696                         pc->state = SP_PEN_CONTEXT_CONTROL;
697                         ret = TRUE;
698                         break;
699                     case SP_PEN_CONTEXT_CONTROL:
700                         /* End current segment */
701                         spdc_endpoint_snap(pc, p, revent.state);
702                         spdc_pen_finish_segment(pc, p, revent.state);
703                         pc->state = SP_PEN_CONTEXT_POINT;
704                         ret = TRUE;
705                         break;
706                     case SP_PEN_CONTEXT_CLOSE:
707                         /* End current segment */
708                         if (!anchor) {   /* Snap node only if not hitting anchor */
709                             spdc_endpoint_snap(pc, p, revent.state);
710                         }
711                         spdc_pen_finish_segment(pc, p, revent.state);
712                         spdc_pen_finish(pc, TRUE);
713                         pc->state = SP_PEN_CONTEXT_POINT;
714                         ret = TRUE;
715                         break;
716                     case SP_PEN_CONTEXT_STOP:
717                         /* This is allowed, if we just cancelled curve */
718                         pc->state = SP_PEN_CONTEXT_POINT;
719                         ret = TRUE;
720                         break;
721                     default:
722                         break;
723                 }
724                 break;
725             case SP_PEN_CONTEXT_MODE_DRAG:
726                 switch (pc->state) {
727                     case SP_PEN_CONTEXT_POINT:
728                     case SP_PEN_CONTEXT_CONTROL:
729                         if (!pc->polylines_only) {
730                             spdc_endpoint_snap(pc, p, revent.state);
731                             spdc_pen_finish_segment(pc, p, revent.state);
732                         }
733                         break;
734                     case SP_PEN_CONTEXT_CLOSE:
735                         spdc_endpoint_snap(pc, p, revent.state);
736                         spdc_pen_finish_segment(pc, p, revent.state);
737                         if (pc->green_closed) {
738                             // finishing at the start anchor, close curve
739                             spdc_pen_finish(pc, TRUE);
740                         } else {
741                             // finishing at some other anchor, finish curve but not close
742                             spdc_pen_finish(pc, FALSE);
743                         }
744                         break;
745                     case SP_PEN_CONTEXT_STOP:
746                         /* This is allowed, if we just cancelled curve */
747                         break;
748                     default:
749                         break;
750                 }
751                 pc->state = SP_PEN_CONTEXT_POINT;
752                 ret = TRUE;
753                 break;
754             default:
755                 break;
756         }
758         if (pc->grab) {
759             /* Release grab now */
760             sp_canvas_item_ungrab(pc->grab, revent.time);
761             pc->grab = NULL;
762         }
764         ret = TRUE;
766         dc->green_closed = FALSE;
767     }
769     return ret;
772 static gint
773 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
775     gint ret = FALSE;
776     if (pc->npoints != 0 && bevent.button != 2) {
777         spdc_pen_finish(pc, FALSE);
778         ret = TRUE;
779     }
780     return ret;
783 void
784 pen_redraw_all (SPPenContext *const pc)
786     // green
787     if (pc->green_bpaths) {
788         // remove old piecewise green canvasitems
789         while (pc->green_bpaths) {
790             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
791             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
792         }
793         // one canvas bpath for all of green_curve
794         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
795         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
796         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
798         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
799     }
801     if (pc->green_anchor)
802         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
804     pc->red_curve->reset();
805     pc->red_curve->moveto(pc->p[0]);
806     pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
807     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
809     // handles
810     if (pc->p[0] != pc->p[1]) {
811         SP_CTRL(pc->c1)->moveto(pc->p[1]);
812         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
813         sp_canvas_item_show (pc->c1);
814         sp_canvas_item_show (pc->cl1);
815     } else {
816         sp_canvas_item_hide (pc->c1);
817         sp_canvas_item_hide (pc->cl1);
818     }
820     NArtBpath const * bpath = pc->green_curve->last_bpath();
821     if (bpath) {
822         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
823             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
824             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
825             sp_canvas_item_show (pc->c0);
826             sp_canvas_item_show (pc->cl0);
827         } else {
828             sp_canvas_item_hide (pc->c0);
829             sp_canvas_item_hide (pc->cl0);
830         }
831     }
834 void
835 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
837     if (pc->npoints != 5)
838         return;
840     // green
841     NArtBpath const * bpath = pc->green_curve->last_bpath();
842     if (bpath) {
843         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
844     } else {
845         // start anchor too
846         if (pc->green_anchor) {
847             pc->green_anchor->dp += NR::Point(x, y);
848         }
849     }
851     // red
852     pc->p[0] += NR::Point(x, y);
853     pc->p[1] += NR::Point(x, y);
854     pen_redraw_all(pc);
857 void
858 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
860     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
863 void
864 pen_lastpoint_tocurve (SPPenContext *const pc)
866     if (pc->npoints != 5)
867         return;
869     // red
870     NArtBpath const * bpath = pc->green_curve->last_bpath();
871     if (bpath && bpath->code == NR_CURVETO) {
872         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
873     } else {
874         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
875     }
877     pen_redraw_all(pc);
880 void
881 pen_lastpoint_toline (SPPenContext *const pc)
883     if (pc->npoints != 5)
884         return;
886     pc->p[1] = pc->p[0];
888     pen_redraw_all(pc);
892 static gint
893 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
895     gint ret = FALSE;
896     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
898     switch (get_group0_keyval (&event->key)) {
900         case GDK_Left: // move last point left
901         case GDK_KP_Left:
902         case GDK_KP_4:
903             if (!MOD__CTRL) { // not ctrl
904                 if (MOD__ALT) { // alt
905                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
906                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
907                 }
908                 else { // no alt
909                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
910                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
911                 }
912                 ret = TRUE;
913             }
914             break;
915         case GDK_Up: // move last point up
916         case GDK_KP_Up:
917         case GDK_KP_8:
918             if (!MOD__CTRL) { // not ctrl
919                 if (MOD__ALT) { // alt
920                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
921                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
922                 }
923                 else { // no alt
924                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
925                     else pen_lastpoint_move(pc, 0, nudge); // no shift
926                 }
927                 ret = TRUE;
928             }
929             break;
930         case GDK_Right: // move last point right
931         case GDK_KP_Right:
932         case GDK_KP_6:
933             if (!MOD__CTRL) { // not ctrl
934                 if (MOD__ALT) { // alt
935                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
936                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
937                 }
938                 else { // no alt
939                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
940                     else pen_lastpoint_move(pc, nudge, 0); // no shift
941                 }
942                 ret = TRUE;
943             }
944             break;
945         case GDK_Down: // move last point down
946         case GDK_KP_Down:
947         case GDK_KP_2:
948             if (!MOD__CTRL) { // not ctrl
949                 if (MOD__ALT) { // alt
950                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
951                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
952                 }
953                 else { // no alt
954                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
955                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
956                 }
957                 ret = TRUE;
958             }
959             break;
961         case GDK_U:
962         case GDK_u:
963             if (MOD__SHIFT_ONLY) {
964                 pen_lastpoint_tocurve(pc);
965                 ret = TRUE;
966             }
967             break;
968         case GDK_L:
969         case GDK_l:
970             if (MOD__SHIFT_ONLY) {
971                 pen_lastpoint_toline(pc);
972                 ret = TRUE;
973             }
974             break;
976         case GDK_Return:
977         case GDK_KP_Enter:
978             if (pc->npoints != 0) {
979                 spdc_pen_finish(pc, FALSE);
980                 ret = TRUE;
981             }
982             break;
983         case GDK_Escape:
984             if (pc->npoints != 0) {
985                 // if drawing, cancel, otherwise pass it up for deselecting
986                 pen_cancel (pc);
987                 ret = TRUE;
988             }
989             break;
990         case GDK_z:
991         case GDK_Z:
992             if (MOD__CTRL_ONLY && pc->npoints != 0) {
993                 // if drawing, cancel, otherwise pass it up for undo
994                 pen_cancel (pc);
995                 ret = TRUE;
996             }
997             break;
998         case GDK_g:
999         case GDK_G:
1000             if (MOD__SHIFT_ONLY) {
1001                 sp_selection_to_guides();
1002                 ret = true;
1003             }
1004             break;
1005         case GDK_BackSpace:
1006         case GDK_Delete:
1007         case GDK_KP_Delete:
1008             if (pc->green_curve->is_empty()) {
1009                 if (!pc->red_curve->is_empty()) {
1010                     pen_cancel (pc);
1011                     ret = TRUE;
1012                 } else {
1013                     // do nothing; this event should be handled upstream
1014                 }
1015             } else {
1016                 /* Reset red curve */
1017                 pc->red_curve->reset();
1018                 /* Destroy topmost green bpath */
1019                 if (pc->green_bpaths) {
1020                     if (pc->green_bpaths->data)
1021                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1022                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1023                 }
1024                 /* Get last segment */
1025                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1026                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1027                 if ( e < 2 ) {
1028                     g_warning("Green curve length is %d", e);
1029                     break;
1030                 }
1031                 pc->p[0] = p[e - 2].c(3);
1032                 if (p[e - 1].code == NR_CURVETO) {
1033                     pc->p[1] = p[e - 1].c(1);
1034                 } else {
1035                     pc->p[1] = pc->p[0];
1036                 }
1037                 NR::Point const pt(( pc->npoints < 4
1038                                      ? p[e - 1].c(3)
1039                                      : pc->p[3] ));
1040                 pc->npoints = 2;
1041                 pc->green_curve->backspace();
1042                 sp_canvas_item_hide(pc->c0);
1043                 sp_canvas_item_hide(pc->c1);
1044                 sp_canvas_item_hide(pc->cl0);
1045                 sp_canvas_item_hide(pc->cl1);
1046                 pc->state = SP_PEN_CONTEXT_POINT;
1047                 spdc_pen_set_subsequent_point(pc, pt, true);
1048                 ret = TRUE;
1049             }
1050             break;
1051         default:
1052             break;
1053     }
1054     return ret;
1057 static void
1058 spdc_reset_colors(SPPenContext *pc)
1060     /* Red */
1061     pc->red_curve->reset();
1062     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1063     /* Blue */
1064     pc->blue_curve->reset();
1065     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1066     /* Green */
1067     while (pc->green_bpaths) {
1068         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1069         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1070     }
1071     pc->green_curve->reset();
1072     if (pc->green_anchor) {
1073         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1074     }
1075     pc->sa = NULL;
1076     pc->ea = NULL;
1077     pc->npoints = 0;
1078     pc->red_curve_is_valid = false;
1082 static void
1083 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1085     g_assert( pc->npoints == 0 );
1087     pc->p[0] = p;
1088     pc->p[1] = p;
1089     pc->npoints = 2;
1090     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1092     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1095 /**
1096  * Show the status message for the current line/curve segment.
1097  * This type of message always shows angle/distance as the last
1098  * two parameters ("angle %3.2f&#176;, distance %s").
1099  */ 
1100 static void
1101 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1103     g_assert(pc != NULL);
1104     g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1105     g_assert(message != NULL);
1107     SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1108     NR::Point rel = p - pc->p[pc_point_to_compare];
1109     GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1110     double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1111     if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1112         angle = angle_to_compass (angle);
1114     pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1115     g_string_free(dist, FALSE);
1118 static void
1119 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1121     g_assert( pc->npoints != 0 );
1122     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1124     pc->p[2] = p;
1125     pc->p[3] = p;
1126     pc->p[4] = p;
1127     pc->npoints = 5;
1128     pc->red_curve->reset();
1129     pc->red_curve->moveto(pc->p[0]);
1130     bool is_curve;
1131     if ( (pc->onlycurves)
1132          || ( pc->p[1] != pc->p[0] ) )
1133     {
1134         pc->red_curve->curveto(pc->p[1], p, p);
1135         is_curve = true;
1136     } else {
1137         pc->red_curve->lineto(p);
1138         is_curve = false;
1139     }
1141     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1143     if (statusbar) {
1144         gchar *message = is_curve ?
1145             _("<b>Curve segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1146             _("<b>Line segment</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1147         spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1148     }
1151 static void
1152 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1154     sp_canvas_item_show(pc->c1);
1155     sp_canvas_item_show(pc->cl1);
1157     if ( pc->npoints == 2 ) {
1158         pc->p[1] = p;
1159         sp_canvas_item_hide(pc->c0);
1160         sp_canvas_item_hide(pc->cl0);
1161         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1162         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1164         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"));
1165     } else if ( pc->npoints == 5 ) {
1166         pc->p[4] = p;
1167         sp_canvas_item_show(pc->c0);
1168         sp_canvas_item_show(pc->cl0);
1169         bool is_symm = false;
1170         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1171              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1172             NR::Point delta = p - pc->p[3];
1173             pc->p[2] = pc->p[3] - delta;
1174             is_symm = true;
1175             pc->red_curve->reset();
1176             pc->red_curve->moveto(pc->p[0]);
1177             pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1178             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1179         }
1180         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1181         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1182         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1183         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1185         gchar *message = is_symm ?
1186             _("<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") :
1187             _("<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");
1188         spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1189     } else {
1190         g_warning("Something bad happened - npoints is %d", pc->npoints);
1191     }
1194 static void
1195 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1197     if (!pc->red_curve->is_empty()) {
1198         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1199         SPCurve *curve = pc->red_curve->copy();
1200         /// \todo fixme:
1201         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1202         curve->unref();
1203         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1205         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1207         pc->p[0] = pc->p[3];
1208         pc->p[1] = pc->p[4];
1209         pc->npoints = 2;
1211         pc->red_curve->reset();
1212     }
1215 static void
1216 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1218     pen_disable_events(pc);
1219     
1220     SPDesktop *const desktop = pc->desktop;
1221     pc->_message_context->clear();
1222     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1224     pc->red_curve->reset();
1225     spdc_concat_colors_and_flush(pc, closed);
1226     pc->sa = NULL;
1227     pc->ea = NULL;
1229     pc->npoints = 0;
1230     pc->state = SP_PEN_CONTEXT_POINT;
1232     sp_canvas_item_hide(pc->c0);
1233     sp_canvas_item_hide(pc->c1);
1234     sp_canvas_item_hide(pc->cl0);
1235     sp_canvas_item_hide(pc->cl1);
1237     if (pc->green_anchor) {
1238         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1239     }
1242     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1244     pen_enable_events(pc);
1247 static void
1248 pen_disable_events(SPPenContext *const pc) {
1249   pc->events_disabled++;
1252 static void
1253 pen_enable_events(SPPenContext *const pc) {
1254   g_return_if_fail(pc->events_disabled != 0);
1255   
1256   pc->events_disabled--;
1259 /*
1260   Local Variables:
1261   mode:c++
1262   c-file-style:"stroustrup"
1263   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1264   indent-tabs-mode:nil
1265   fill-column:99
1266   End:
1267 */
1268 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :