Code

Write/read perspectives to/from SVG; store ratios of the distances from corners to...
[inkscape.git] / src / pen-context.cpp
1 /** \file
2  * Pen event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
20 #include "pen-context.h"
21 #include "sp-namedview.h"
22 #include "sp-metrics.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "draw-anchor.h"
27 #include "message-stack.h"
28 #include "message-context.h"
29 #include "prefs-utils.h"
30 #include "sp-path.h"
32 #include "pixmaps/cursor-pen.xpm"
33 #include "display/canvas-bpath.h"
34 #include "display/sp-ctrlline.h"
35 #include "display/sodipodi-ctrl.h"
36 #include <glibmm/i18n.h>
37 #include "libnr/n-art-bpath.h"
38 #include "helper/units.h"
39 #include "macros.h"
40 #include "context-fns.h"
43 static void sp_pen_context_class_init(SPPenContextClass *klass);
44 static void sp_pen_context_init(SPPenContext *pc);
45 static void sp_pen_context_dispose(GObject *object);
47 static void sp_pen_context_setup(SPEventContext *ec);
48 static void sp_pen_context_finish(SPEventContext *ec);
49 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
51 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
54 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
55 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
56 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
58 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
60 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
61 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
62 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
63 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
65 static void spdc_reset_colors(SPPenContext *pc);
67 static void pen_disable_events(SPPenContext *const pc);
68 static void pen_enable_events(SPPenContext *const pc);
70 static NR::Point pen_drag_origin_w(0, 0);
71 static bool pen_within_tolerance = false;
73 static SPDrawContextClass *pen_parent_class;
75 /**
76  * Register SPPenContext with Gdk and return its type.
77  */
78 GType
79 sp_pen_context_get_type(void)
80 {
81     static GType type = 0;
82     if (!type) {
83         GTypeInfo info = {
84             sizeof(SPPenContextClass),
85             NULL, NULL,
86             (GClassInitFunc) sp_pen_context_class_init,
87             NULL, NULL,
88             sizeof(SPPenContext),
89             4,
90             (GInstanceInitFunc) sp_pen_context_init,
91             NULL,   /* value_table */
92         };
93         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
94     }
95     return type;
96 }
98 /**
99  * Initialize the SPPenContext vtable.
100  */
101 static void
102 sp_pen_context_class_init(SPPenContextClass *klass)
104     GObjectClass *object_class;
105     SPEventContextClass *event_context_class;
107     object_class = (GObjectClass *) klass;
108     event_context_class = (SPEventContextClass *) klass;
110     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
112     object_class->dispose = sp_pen_context_dispose;
114     event_context_class->setup = sp_pen_context_setup;
115     event_context_class->finish = sp_pen_context_finish;
116     event_context_class->set = sp_pen_context_set;
117     event_context_class->root_handler = sp_pen_context_root_handler;
118     event_context_class->item_handler = sp_pen_context_item_handler;
121 /**
122  * Callback to initialize SPPenContext object.
123  */
124 static void
125 sp_pen_context_init(SPPenContext *pc)
128     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
130     event_context->cursor_shape = cursor_pen_xpm;
131     event_context->hot_x = 4;
132     event_context->hot_y = 4;
134     pc->npoints = 0;
135     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
136     pc->state = SP_PEN_CONTEXT_POINT;
138     pc->c0 = NULL;
139     pc->c1 = NULL;
140     pc->cl0 = NULL;
141     pc->cl1 = NULL;
142     
143     pc->events_disabled = 0;
146 /**
147  * Callback to destroy the SPPenContext object's members and itself.
148  */
149 static void
150 sp_pen_context_dispose(GObject *object)
152     SPPenContext *pc;
154     pc = SP_PEN_CONTEXT(object);
156     if (pc->c0) {
157         gtk_object_destroy(GTK_OBJECT(pc->c0));
158         pc->c0 = NULL;
159     }
160     if (pc->c1) {
161         gtk_object_destroy(GTK_OBJECT(pc->c1));
162         pc->c1 = NULL;
163     }
164     if (pc->cl0) {
165         gtk_object_destroy(GTK_OBJECT(pc->cl0));
166         pc->cl0 = NULL;
167     }
168     if (pc->cl1) {
169         gtk_object_destroy(GTK_OBJECT(pc->cl1));
170         pc->cl1 = NULL;
171     }
173     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
176 /**
177  * Callback to initialize SPPenContext object.
178  */
179 static void
180 sp_pen_context_setup(SPEventContext *ec)
182     SPPenContext *pc;
184     pc = SP_PEN_CONTEXT(ec);
186     if (((SPEventContextClass *) pen_parent_class)->setup) {
187         ((SPEventContextClass *) pen_parent_class)->setup(ec);
188     }
190     /* Pen indicators */
191     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
192                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
193     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
194                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
195     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
196     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
197     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
198     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
200     sp_canvas_item_hide(pc->c0);
201     sp_canvas_item_hide(pc->c1);
202     sp_canvas_item_hide(pc->cl0);
203     sp_canvas_item_hide(pc->cl1);
205     sp_event_context_read(ec, "mode");
207     pc->anchor_statusbar = false;
209     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
210         ec->enableSelectionCue();
211     }
214 static void
215 pen_cancel (SPPenContext *const pc) 
217     pc->state = SP_PEN_CONTEXT_STOP;
218     spdc_reset_colors(pc);
219     sp_canvas_item_hide(pc->c0);
220     sp_canvas_item_hide(pc->c1);
221     sp_canvas_item_hide(pc->cl0);
222     sp_canvas_item_hide(pc->cl1);
223     pc->_message_context->clear();
224     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
226     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
229 /**
230  * Finalization callback.
231  */
232 static void
233 sp_pen_context_finish(SPEventContext *ec)
235     SPPenContext *pc = SP_PEN_CONTEXT(ec);
237     if (pc->npoints != 0) {
238         pen_cancel (pc);
239     }
241     if (((SPEventContextClass *) pen_parent_class)->finish) {
242         ((SPEventContextClass *) pen_parent_class)->finish(ec);
243     }
246 /**
247  * Callback that sets key to value in pen context.
248  */
249 static void
250 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
252     SPPenContext *pc = SP_PEN_CONTEXT(ec);
254     if (!strcmp(key, "mode")) {
255         if ( val && !strcmp(val, "drag") ) {
256             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
257         } else {
258             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
259         }
260     }
263 /**
264  * Snaps new node relative to the previous node.
265  */
266 static void
267 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
269     if (pc->npoints > 0) {
270         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
271     }
273     spdc_endpoint_snap_free(pc, p, state);
276 /**
277  * Snaps new node's handle relative to the new node.
278  */
279 static void
280 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
282     g_return_if_fail(( pc->npoints == 2 ||
283                        pc->npoints == 5   ));
285     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
286     spdc_endpoint_snap_free(pc, p, state);
289 static gint 
290 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
292     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
294     gint ret = FALSE;
296     switch (event->type) {
297         case GDK_BUTTON_PRESS:
298             ret = pen_handle_button_press(pc, event->button);
299             break;
300         default:
301             break;
302     }
304     if (!ret) {
305         if (((SPEventContextClass *) pen_parent_class)->item_handler)
306             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
307     }
309     return ret;
312 /**
313  * Callback to handle all pen events.
314  */
315 static gint
316 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
318     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
320     gint ret = FALSE;
322     switch (event->type) {
323         case GDK_BUTTON_PRESS:
324             ret = pen_handle_button_press(pc, event->button);
325             break;
327         case GDK_MOTION_NOTIFY:
328             ret = pen_handle_motion_notify(pc, event->motion);
329             break;
331         case GDK_BUTTON_RELEASE:
332             ret = pen_handle_button_release(pc, event->button);
333             break;
335         case GDK_2BUTTON_PRESS:
336             ret = pen_handle_2button_press(pc, event->button);
337             break;
339         case GDK_KEY_PRESS:
340             ret = pen_handle_key_press(pc, event);
341             break;
343         default:
344             break;
345     }
347     if (!ret) {
348         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
349             = ((SPEventContextClass *) pen_parent_class)->root_handler;
350         if (parent_root_handler) {
351             ret = parent_root_handler(ec, event);
352         }
353     }
355     return ret;
358 /**
359  * Handle mouse button press event.
360  */
361 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
363     if (pc->events_disabled) {
364         // skip event processing if events are disabled
365         return FALSE;
366     }
368     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
369     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
370     NR::Point const event_w(bevent.x, bevent.y);
371     NR::Point const event_dt(desktop->w2d(event_w));
372     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
374     gint ret = FALSE;
375     if (bevent.button == 1 && !event_context->space_panning) {
377         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
378             return TRUE;
379         }
381         pen_drag_origin_w = event_w;
382         pen_within_tolerance = true;
384         /* Test whether we hit any anchor. */
385         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
387         switch (pc->mode) {
388             case SP_PEN_CONTEXT_MODE_CLICK:
389                 /* In click mode we add point on release */
390                 switch (pc->state) {
391                     case SP_PEN_CONTEXT_POINT:
392                     case SP_PEN_CONTEXT_CONTROL:
393                     case SP_PEN_CONTEXT_CLOSE:
394                         break;
395                     case SP_PEN_CONTEXT_STOP:
396                         /* This is allowed, if we just cancelled curve */
397                         pc->state = SP_PEN_CONTEXT_POINT;
398                         break;
399                     default:
400                         break;
401                 }
402                 break;
403             case SP_PEN_CONTEXT_MODE_DRAG:
404                 switch (pc->state) {
405                     case SP_PEN_CONTEXT_STOP:
406                         /* This is allowed, if we just cancelled curve */
407                     case SP_PEN_CONTEXT_POINT:
408                         if (pc->npoints == 0) {
410                             /* Set start anchor */
411                             pc->sa = anchor;
412                             NR::Point p;
413                             if (anchor) {
415                                 /* Adjust point to anchor if needed */
416                                 p = anchor->dp;
417                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
419                             } else {
421                                 // This is the first click of a new curve; deselect item so that
422                                 // this curve is not combined with it (unless it is drawn from its
423                                 // anchor, which is handled by the sibling branch above)
424                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
425                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
427                                     selection->clear();
428                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
430                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
432                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
433                                 }
435                                 /* Create green anchor */
436                                 p = event_dt;
437                                 spdc_endpoint_snap(pc, p, bevent.state);
438                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
439                             }
440                             spdc_pen_set_initial_point(pc, p);
441                         } else {
443                             /* Set end anchor */
444                             pc->ea = anchor;
445                             NR::Point p;
446                             if (anchor) {
448                                 p = anchor->dp;
449                                 // we hit an anchor, will finish the curve (either with or without closing)
450                                 // in release handler
451                                 pc->state = SP_PEN_CONTEXT_CLOSE;
453                                 if (pc->green_anchor && pc->green_anchor->active) {
454                                     // we clicked on the current curve start, so close it even if
455                                     // we drag a handle away from it
456                                     dc->green_closed = TRUE;
457                                 }
458                                 ret = TRUE;
459                                 break;
461                             } else {
463                                 p = event_dt;
464                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
465                                 spdc_pen_set_subsequent_point(pc, p, true);
466                             }
468                         }
469                         pc->state = SP_PEN_CONTEXT_CONTROL;
470                         ret = TRUE;
471                         break;
472                     case SP_PEN_CONTEXT_CONTROL:
473                         g_warning("Button down in CONTROL state");
474                         break;
475                     case SP_PEN_CONTEXT_CLOSE:
476                         g_warning("Button down in CLOSE state");
477                         break;
478                     default:
479                         break;
480                 }
481                 break;
482             default:
483                 break;
484         }
485     } else if (bevent.button == 3) {
486         if (pc->npoints != 0) {
488             spdc_pen_finish_segment(pc, event_dt, bevent.state);
489             if (pc->green_closed) {
490                 // finishing at the start anchor, close curve
491                 spdc_pen_finish(pc, TRUE);
492             } else {
493                 // finishing at some other anchor, finish curve but not close
494                 spdc_pen_finish(pc, FALSE);
495             }
497             ret = TRUE;
498         }
499     }
501     return ret;
504 /**
505  * Handle motion_notify event.
506  */
507 static gint
508 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
510     gint ret = FALSE;
512     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
514     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
515         // allow scrolling
516         return FALSE;
517     }
518     
519     if (pc->events_disabled) {
520         // skip motion events if pen events are disabled
521         return FALSE;
522     }
524     NR::Point const event_w(mevent.x,
525                             mevent.y);
526     if (pen_within_tolerance) {
527         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
528                                                                "value", 0, 0, 100);
529         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
530             return FALSE;   // Do not drag if we're within tolerance from origin.
531         }
532     }
533     // Once the user has moved farther than tolerance from the original location
534     // (indicating they intend to move the object, not click), then always process the
535     // motion notify coordinates as given (no snapping back to origin)
536     pen_within_tolerance = false;
538     SPDesktop *const dt = pc->desktop;
539     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
540         /* Grab mouse, so release will not pass unnoticed */
541         pc->grab = SP_CANVAS_ITEM(dt->acetate);
542         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
543                                         GDK_BUTTON_RELEASE_MASK |
544                                         GDK_POINTER_MOTION_MASK  ),
545                             NULL, mevent.time);
546     }
548     /* Find desktop coordinates */
549     NR::Point p = dt->w2d(event_w);
551     /* Test, whether we hit any anchor */
552     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
554     switch (pc->mode) {
555         case SP_PEN_CONTEXT_MODE_CLICK:
556             switch (pc->state) {
557                 case SP_PEN_CONTEXT_POINT:
558                     if ( pc->npoints != 0 ) {
559                         /* Only set point, if we are already appending */
560                         spdc_endpoint_snap(pc, p, mevent.state);
561                         spdc_pen_set_subsequent_point(pc, p, true);
562                         ret = TRUE;
563                     }
564                     break;
565                 case SP_PEN_CONTEXT_CONTROL:
566                 case SP_PEN_CONTEXT_CLOSE:
567                     /* Placing controls is last operation in CLOSE state */
568                     spdc_endpoint_snap(pc, p, mevent.state);
569                     spdc_pen_set_ctrl(pc, p, mevent.state);
570                     ret = TRUE;
571                     break;
572                 case SP_PEN_CONTEXT_STOP:
573                     /* This is perfectly valid */
574                     break;
575                 default:
576                     break;
577             }
578             break;
579         case SP_PEN_CONTEXT_MODE_DRAG:
580             switch (pc->state) {
581                 case SP_PEN_CONTEXT_POINT:
582                     if ( pc->npoints > 0 ) {
583                         /* Only set point, if we are already appending */
585                         if (!anchor) {   /* Snap node only if not hitting anchor */
586                             spdc_endpoint_snap(pc, p, mevent.state);
587                         }
589                         spdc_pen_set_subsequent_point(pc, p, !anchor);
591                         if (anchor && !pc->anchor_statusbar) {
592                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
593                             pc->anchor_statusbar = true;
594                         } else if (!anchor && pc->anchor_statusbar) {
595                             pc->_message_context->clear();
596                             pc->anchor_statusbar = false;
597                         }
599                         ret = TRUE;
600                     } else {
601                         if (anchor && !pc->anchor_statusbar) {
602                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
603                             pc->anchor_statusbar = true;
604                         } else if (!anchor && pc->anchor_statusbar) {
605                             pc->_message_context->clear();
606                             pc->anchor_statusbar = false;
607                         }
608                     }
609                     break;
610                 case SP_PEN_CONTEXT_CONTROL:
611                 case SP_PEN_CONTEXT_CLOSE:
612                     /* Placing controls is last operation in CLOSE state */
614                     // snap the handle
615                     spdc_endpoint_snap_handle(pc, p, mevent.state);
617                     spdc_pen_set_ctrl(pc, p, mevent.state);
618                     ret = TRUE;
619                     break;
620                 case SP_PEN_CONTEXT_STOP:
621                     /* This is perfectly valid */
622                     break;
623                 default:
624                     break;
625             }
626             break;
627         default:
628             break;
629     }
630     return ret;
633 /**
634  * Handle mouse button release event.
635  */
636 static gint
637 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
639     if (pc->events_disabled) {
640         // skip event processing if events are disabled
641         return FALSE;
642     }
644     gint ret = FALSE;
645     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
646     if ( revent.button == 1  && !event_context->space_panning) {
648         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
650         NR::Point const event_w(revent.x,
651                                 revent.y);
652         /* Find desktop coordinates */
653         NR::Point p = pc->desktop->w2d(event_w);
655         /* Test whether we hit any anchor. */
656         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
658         switch (pc->mode) {
659             case SP_PEN_CONTEXT_MODE_CLICK:
660                 switch (pc->state) {
661                     case SP_PEN_CONTEXT_POINT:
662                         if ( pc->npoints == 0 ) {
663                             /* Start new thread only with button release */
664                             if (anchor) {
665                                 p = anchor->dp;
666                             }
667                             pc->sa = anchor;
668                             spdc_pen_set_initial_point(pc, p);
669                         } else {
670                             /* Set end anchor here */
671                             pc->ea = anchor;
672                             if (anchor) {
673                                 p = anchor->dp;
674                             }
675                         }
676                         pc->state = SP_PEN_CONTEXT_CONTROL;
677                         ret = TRUE;
678                         break;
679                     case SP_PEN_CONTEXT_CONTROL:
680                         /* End current segment */
681                         spdc_endpoint_snap(pc, p, revent.state);
682                         spdc_pen_finish_segment(pc, p, revent.state);
683                         pc->state = SP_PEN_CONTEXT_POINT;
684                         ret = TRUE;
685                         break;
686                     case SP_PEN_CONTEXT_CLOSE:
687                         /* End current segment */
688                         if (!anchor) {   /* Snap node only if not hitting anchor */
689                             spdc_endpoint_snap(pc, p, revent.state);
690                         }
691                         spdc_pen_finish_segment(pc, p, revent.state);
692                         spdc_pen_finish(pc, TRUE);
693                         pc->state = SP_PEN_CONTEXT_POINT;
694                         ret = TRUE;
695                         break;
696                     case SP_PEN_CONTEXT_STOP:
697                         /* This is allowed, if we just cancelled curve */
698                         pc->state = SP_PEN_CONTEXT_POINT;
699                         ret = TRUE;
700                         break;
701                     default:
702                         break;
703                 }
704                 break;
705             case SP_PEN_CONTEXT_MODE_DRAG:
706                 switch (pc->state) {
707                     case SP_PEN_CONTEXT_POINT:
708                     case SP_PEN_CONTEXT_CONTROL:
709                         spdc_endpoint_snap(pc, p, revent.state);
710                         spdc_pen_finish_segment(pc, p, revent.state);
711                         break;
712                     case SP_PEN_CONTEXT_CLOSE:
713                         spdc_endpoint_snap(pc, p, revent.state);
714                         spdc_pen_finish_segment(pc, p, revent.state);
715                         if (pc->green_closed) {
716                             // finishing at the start anchor, close curve
717                             spdc_pen_finish(pc, TRUE);
718                         } else {
719                             // finishing at some other anchor, finish curve but not close
720                             spdc_pen_finish(pc, FALSE);
721                         }
722                         break;
723                     case SP_PEN_CONTEXT_STOP:
724                         /* This is allowed, if we just cancelled curve */
725                         break;
726                     default:
727                         break;
728                 }
729                 pc->state = SP_PEN_CONTEXT_POINT;
730                 ret = TRUE;
731                 break;
732             default:
733                 break;
734         }
736         if (pc->grab) {
737             /* Release grab now */
738             sp_canvas_item_ungrab(pc->grab, revent.time);
739             pc->grab = NULL;
740         }
742         ret = TRUE;
744         dc->green_closed = FALSE;
745     }
747     return ret;
750 static gint
751 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
753     gint ret = FALSE;
754     if (pc->npoints != 0 && bevent.button != 2) {
755         spdc_pen_finish(pc, FALSE);
756         ret = TRUE;
757     }
758     return ret;
761 void
762 pen_redraw_all (SPPenContext *const pc)
764     // green
765     if (pc->green_bpaths) {
766         // remove old piecewise green canvasitems
767         while (pc->green_bpaths) {
768             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
769             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
770         }
771         // one canvas bpath for all of green_curve
772         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
773         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
774         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
776         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
777     }
779     if (pc->green_anchor)
780         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
782     sp_curve_reset(pc->red_curve);
783     sp_curve_moveto(pc->red_curve, pc->p[0]);
784     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
785     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
787     // handles
788     if (pc->p[0] != pc->p[1]) {
789         SP_CTRL(pc->c1)->moveto(pc->p[1]);
790         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
791         sp_canvas_item_show (pc->c1);
792         sp_canvas_item_show (pc->cl1);
793     } else {
794         sp_canvas_item_hide (pc->c1);
795         sp_canvas_item_hide (pc->cl1);
796     }
798     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
799     if (bpath) {
800         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
801             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
802             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
803             sp_canvas_item_show (pc->c0);
804             sp_canvas_item_show (pc->cl0);
805         } else {
806             sp_canvas_item_hide (pc->c0);
807             sp_canvas_item_hide (pc->cl0);
808         }
809     }
812 void
813 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
815     if (pc->npoints != 5)
816         return;
818     // green
819     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
820     if (bpath) {
821         if (bpath->code == NR_CURVETO) {
822             bpath->x2 += x;
823             bpath->y2 += y;
824         }
825         bpath->x3 += x;
826         bpath->y3 += y;
827     } else {
828         // start anchor too
829         if (pc->green_anchor) {
830             pc->green_anchor->dp += NR::Point(x, y);
831         }
832     }
834     // red
835     pc->p[0] += NR::Point(x, y);
836     pc->p[1] += NR::Point(x, y);
837     pen_redraw_all(pc);
840 void
841 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
843     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
846 void
847 pen_lastpoint_tocurve (SPPenContext *const pc)
849     if (pc->npoints != 5)
850         return;
852     // red
853     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
854     if (bpath && bpath->code == NR_CURVETO) {
855         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
856     } else {
857         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
858     }
860     pen_redraw_all(pc);
863 void
864 pen_lastpoint_toline (SPPenContext *const pc)
866     if (pc->npoints != 5)
867         return;
869     pc->p[1] = pc->p[0];
871     pen_redraw_all(pc);
875 static gint
876 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
878     gint ret = FALSE;
879     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
881     switch (get_group0_keyval (&event->key)) {
883         case GDK_Left: // move last point left
884         case GDK_KP_Left:
885         case GDK_KP_4:
886             if (!MOD__CTRL) { // not ctrl
887                 if (MOD__ALT) { // alt
888                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
889                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
890                 }
891                 else { // no alt
892                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
893                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
894                 }
895                 ret = TRUE;
896             }
897             break;
898         case GDK_Up: // move last point up
899         case GDK_KP_Up:
900         case GDK_KP_8:
901             if (!MOD__CTRL) { // not ctrl
902                 if (MOD__ALT) { // alt
903                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
904                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
905                 }
906                 else { // no alt
907                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
908                     else pen_lastpoint_move(pc, 0, nudge); // no shift
909                 }
910                 ret = TRUE;
911             }
912             break;
913         case GDK_Right: // move last point right
914         case GDK_KP_Right:
915         case GDK_KP_6:
916             if (!MOD__CTRL) { // not ctrl
917                 if (MOD__ALT) { // alt
918                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
919                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
920                 }
921                 else { // no alt
922                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
923                     else pen_lastpoint_move(pc, nudge, 0); // no shift
924                 }
925                 ret = TRUE;
926             }
927             break;
928         case GDK_Down: // move last point down
929         case GDK_KP_Down:
930         case GDK_KP_2:
931             if (!MOD__CTRL) { // not ctrl
932                 if (MOD__ALT) { // alt
933                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
934                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
935                 }
936                 else { // no alt
937                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
938                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
939                 }
940                 ret = TRUE;
941             }
942             break;
944         case GDK_U:
945         case GDK_u:
946             if (MOD__SHIFT_ONLY) {
947                 pen_lastpoint_tocurve(pc);
948                 ret = TRUE;
949             }
950             break;
951         case GDK_L:
952         case GDK_l:
953             if (MOD__SHIFT_ONLY) {
954                 pen_lastpoint_toline(pc);
955                 ret = TRUE;
956             }
957             break;
959         case GDK_Return:
960         case GDK_KP_Enter:
961             if (pc->npoints != 0) {
962                 spdc_pen_finish(pc, FALSE);
963                 ret = TRUE;
964             }
965             break;
966         case GDK_Escape:
967             if (pc->npoints != 0) {
968                 // if drawing, cancel, otherwise pass it up for deselecting
969                 pen_cancel (pc);
970                 ret = TRUE;
971             }
972             break;
973         case GDK_z:
974         case GDK_Z:
975             if (MOD__CTRL_ONLY && pc->npoints != 0) {
976                 // if drawing, cancel, otherwise pass it up for undo
977                 pen_cancel (pc);
978                 ret = TRUE;
979             }
980             break;
981         case GDK_BackSpace:
982         case GDK_Delete:
983         case GDK_KP_Delete:
984             if (sp_curve_is_empty(pc->green_curve)) {
985                 pen_cancel (pc);
986                 ret = TRUE;
987             } else {
988                 /* Reset red curve */
989                 sp_curve_reset(pc->red_curve);
990                 /* Destroy topmost green bpath */
991                 if (pc->green_bpaths) {
992                     if (pc->green_bpaths->data)
993                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
994                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
995                 }
996                 /* Get last segment */
997                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
998                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
999                 if ( e < 2 ) {
1000                     g_warning("Green curve length is %d", e);
1001                     break;
1002                 }
1003                 pc->p[0] = p[e - 2].c(3);
1004                 if (p[e - 1].code == NR_CURVETO) {
1005                     pc->p[1] = p[e - 1].c(1);
1006                 } else {
1007                     pc->p[1] = pc->p[0];
1008                 }
1009                 NR::Point const pt(( pc->npoints < 4
1010                                      ? p[e - 1].c(3)
1011                                      : pc->p[3] ));
1012                 pc->npoints = 2;
1013                 sp_curve_backspace(pc->green_curve);
1014                 sp_canvas_item_hide(pc->c0);
1015                 sp_canvas_item_hide(pc->c1);
1016                 sp_canvas_item_hide(pc->cl0);
1017                 sp_canvas_item_hide(pc->cl1);
1018                 pc->state = SP_PEN_CONTEXT_POINT;
1019                 spdc_pen_set_subsequent_point(pc, pt, true);
1020                 ret = TRUE;
1021             }
1022             break;
1023         default:
1024             break;
1025     }
1026     return ret;
1029 static void
1030 spdc_reset_colors(SPPenContext *pc)
1032     /* Red */
1033     sp_curve_reset(pc->red_curve);
1034     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1035     /* Blue */
1036     sp_curve_reset(pc->blue_curve);
1037     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1038     /* Green */
1039     while (pc->green_bpaths) {
1040         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1041         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1042     }
1043     sp_curve_reset(pc->green_curve);
1044     if (pc->green_anchor) {
1045         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1046     }
1047     pc->sa = NULL;
1048     pc->ea = NULL;
1049     pc->npoints = 0;
1050     pc->red_curve_is_valid = false;
1054 static void
1055 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1057     g_assert( pc->npoints == 0 );
1059     pc->p[0] = p;
1060     pc->p[1] = p;
1061     pc->npoints = 2;
1062     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1064     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1067 static void
1068 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1070     g_assert( pc->npoints != 0 );
1071     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1073     pc->p[2] = p;
1074     pc->p[3] = p;
1075     pc->p[4] = p;
1076     pc->npoints = 5;
1077     sp_curve_reset(pc->red_curve);
1078     sp_curve_moveto(pc->red_curve, pc->p[0]);
1079     bool is_curve;
1080     if ( (pc->onlycurves)
1081          || ( pc->p[1] != pc->p[0] ) )
1082     {
1083         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1084         is_curve = true;
1085     } else {
1086         sp_curve_lineto(pc->red_curve, p);
1087         is_curve = false;
1088     }
1090     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1092     if (statusbar) {
1093         // status text
1094         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1095         NR::Point rel = p - pc->p[0];
1096         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1097         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1098         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1099             angle = angle_to_compass (angle);
1100         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>%s</b>: angle %3.2f&#176;, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path"), is_curve? "Curve segment" : "Line segment", angle, dist->str);
1101         g_string_free(dist, FALSE);
1102     }
1105 static void
1106 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1108     sp_canvas_item_show(pc->c1);
1109     sp_canvas_item_show(pc->cl1);
1111     if ( pc->npoints == 2 ) {
1112         pc->p[1] = p;
1113         sp_canvas_item_hide(pc->c0);
1114         sp_canvas_item_hide(pc->cl0);
1115         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1116         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1118         // status text
1119         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1120         NR::Point rel = p - pc->p[0];
1121         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1122         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1123         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1124             angle = angle_to_compass (angle);
1125         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1126         g_string_free(dist, FALSE);
1128     } else if ( pc->npoints == 5 ) {
1129         pc->p[4] = p;
1130         sp_canvas_item_show(pc->c0);
1131         sp_canvas_item_show(pc->cl0);
1132         bool is_symm = false;
1133         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1134              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1135             NR::Point delta = p - pc->p[3];
1136             pc->p[2] = pc->p[3] - delta;
1137             is_symm = true;
1138             sp_curve_reset(pc->red_curve);
1139             sp_curve_moveto(pc->red_curve, pc->p[0]);
1140             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1141             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1142         }
1143         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1144         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1145         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1146         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1148         // status text
1149         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1150         NR::Point rel = p - pc->p[3];
1151         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1152         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1153         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1154             angle = angle_to_compass (angle);
1155         pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>%s</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only"), is_symm? "Curve handle, symmetric" : "Curve handle", angle, dist->str);
1156         g_string_free(dist, FALSE);
1158     } else {
1159         g_warning("Something bad happened - npoints is %d", pc->npoints);
1160     }
1163 static void
1164 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1166     if (!sp_curve_empty(pc->red_curve)) {
1167         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1168         SPCurve *curve = sp_curve_copy(pc->red_curve);
1169         /// \todo fixme:
1170         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1171         sp_curve_unref(curve);
1172         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1174         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1176         pc->p[0] = pc->p[3];
1177         pc->p[1] = pc->p[4];
1178         pc->npoints = 2;
1180         sp_curve_reset(pc->red_curve);
1181     }
1184 static void
1185 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1187     pen_disable_events(pc);
1188     
1189     SPDesktop *const desktop = pc->desktop;
1190     pc->_message_context->clear();
1191     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1193     sp_curve_reset(pc->red_curve);
1194     spdc_concat_colors_and_flush(pc, closed);
1195     pc->sa = NULL;
1196     pc->ea = NULL;
1198     pc->npoints = 0;
1199     pc->state = SP_PEN_CONTEXT_POINT;
1201     sp_canvas_item_hide(pc->c0);
1202     sp_canvas_item_hide(pc->c1);
1203     sp_canvas_item_hide(pc->cl0);
1204     sp_canvas_item_hide(pc->cl1);
1206     if (pc->green_anchor) {
1207         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1208     }
1211     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1213     pen_enable_events(pc);
1216 static void
1217 pen_disable_events(SPPenContext *const pc) {
1218   pc->events_disabled++;
1221 static void
1222 pen_enable_events(SPPenContext *const pc) {
1223   g_return_if_fail(pc->events_disabled != 0);
1224   
1225   pc->events_disabled--;
1228 /*
1229   Local Variables:
1230   mode:c++
1231   c-file-style:"stroustrup"
1232   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1233   indent-tabs-mode:nil
1234   fill-column:99
1235   End:
1236 */
1237 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :