Code

Line-end fix
[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"
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 "helper/units.h"
42 #include "macros.h"
43 #include "context-fns.h"
46 static void sp_pen_context_class_init(SPPenContextClass *klass);
47 static void sp_pen_context_init(SPPenContext *pc);
48 static void sp_pen_context_dispose(GObject *object);
50 static void sp_pen_context_setup(SPEventContext *ec);
51 static void sp_pen_context_finish(SPEventContext *ec);
52 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
53 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
54 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
61 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
63 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
65 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
66 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
67 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
68 static void spdc_reset_colors(SPPenContext *pc);
70 static void pen_disable_events(SPPenContext *const pc);
71 static void pen_enable_events(SPPenContext *const pc);
73 static NR::Point pen_drag_origin_w(0, 0);
74 static bool pen_within_tolerance = false;
76 static SPDrawContextClass *pen_parent_class;
78 /**
79  * Register SPPenContext with Gdk and return its type.
80  */
81 GType
82 sp_pen_context_get_type(void)
83 {
84     static GType type = 0;
85     if (!type) {
86         GTypeInfo info = {
87             sizeof(SPPenContextClass),
88             NULL, NULL,
89             (GClassInitFunc) sp_pen_context_class_init,
90             NULL, NULL,
91             sizeof(SPPenContext),
92             4,
93             (GInstanceInitFunc) sp_pen_context_init,
94             NULL,   /* value_table */
95         };
96         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
97     }
98     return type;
99 }
101 /**
102  * Initialize the SPPenContext vtable.
103  */
104 static void
105 sp_pen_context_class_init(SPPenContextClass *klass)
107     GObjectClass *object_class;
108     SPEventContextClass *event_context_class;
110     object_class = (GObjectClass *) klass;
111     event_context_class = (SPEventContextClass *) klass;
113     pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
115     object_class->dispose = sp_pen_context_dispose;
117     event_context_class->setup = sp_pen_context_setup;
118     event_context_class->finish = sp_pen_context_finish;
119     event_context_class->set = sp_pen_context_set;
120     event_context_class->root_handler = sp_pen_context_root_handler;
121     event_context_class->item_handler = sp_pen_context_item_handler;
124 /**
125  * Callback to initialize SPPenContext object.
126  */
127 static void
128 sp_pen_context_init(SPPenContext *pc)
131     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
133     event_context->cursor_shape = cursor_pen_xpm;
134     event_context->hot_x = 4;
135     event_context->hot_y = 4;
137     pc->npoints = 0;
138     pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
139     pc->state = SP_PEN_CONTEXT_POINT;
141     pc->c0 = NULL;
142     pc->c1 = NULL;
143     pc->cl0 = NULL;
144     pc->cl1 = NULL;
145     
146     pc->events_disabled = 0;
149 /**
150  * Callback to destroy the SPPenContext object's members and itself.
151  */
152 static void
153 sp_pen_context_dispose(GObject *object)
155     SPPenContext *pc;
157     pc = SP_PEN_CONTEXT(object);
159     if (pc->c0) {
160         gtk_object_destroy(GTK_OBJECT(pc->c0));
161         pc->c0 = NULL;
162     }
163     if (pc->c1) {
164         gtk_object_destroy(GTK_OBJECT(pc->c1));
165         pc->c1 = NULL;
166     }
167     if (pc->cl0) {
168         gtk_object_destroy(GTK_OBJECT(pc->cl0));
169         pc->cl0 = NULL;
170     }
171     if (pc->cl1) {
172         gtk_object_destroy(GTK_OBJECT(pc->cl1));
173         pc->cl1 = NULL;
174     }
176     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
179 /**
180  * Callback to initialize SPPenContext object.
181  */
182 static void
183 sp_pen_context_setup(SPEventContext *ec)
185     SPPenContext *pc;
187     pc = SP_PEN_CONTEXT(ec);
189     if (((SPEventContextClass *) pen_parent_class)->setup) {
190         ((SPEventContextClass *) pen_parent_class)->setup(ec);
191     }
193     /* Pen indicators */
194     pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
195                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
196     pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
197                                 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
198     pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
199     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
200     pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
201     sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
203     sp_canvas_item_hide(pc->c0);
204     sp_canvas_item_hide(pc->c1);
205     sp_canvas_item_hide(pc->cl0);
206     sp_canvas_item_hide(pc->cl1);
208     sp_event_context_read(ec, "mode");
210     pc->anchor_statusbar = false;
212     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
213         ec->enableSelectionCue();
214     }
217 static void
218 pen_cancel (SPPenContext *const pc) 
220     pc->state = SP_PEN_CONTEXT_STOP;
221     spdc_reset_colors(pc);
222     sp_canvas_item_hide(pc->c0);
223     sp_canvas_item_hide(pc->c1);
224     sp_canvas_item_hide(pc->cl0);
225     sp_canvas_item_hide(pc->cl1);
226     pc->_message_context->clear();
227     pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
229     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
232 /**
233  * Finalization callback.
234  */
235 static void
236 sp_pen_context_finish(SPEventContext *ec)
238     SPPenContext *pc = SP_PEN_CONTEXT(ec);
240     if (pc->npoints != 0) {
241         pen_cancel (pc);
242     }
244     if (((SPEventContextClass *) pen_parent_class)->finish) {
245         ((SPEventContextClass *) pen_parent_class)->finish(ec);
246     }
249 /**
250  * Callback that sets key to value in pen context.
251  */
252 static void
253 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
255     SPPenContext *pc = SP_PEN_CONTEXT(ec);
257     if (!strcmp(key, "mode")) {
258         if ( val && !strcmp(val, "drag") ) {
259             pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
260         } else {
261             pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
262         }
263     }
266 /**
267  * Snaps new node relative to the previous node.
268  */
269 static void
270 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
272     if (pc->npoints > 0) {
273         spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
274     }
276     spdc_endpoint_snap_free(pc, p, state);
279 /**
280  * Snaps new node's handle relative to the new node.
281  */
282 static void
283 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
285     g_return_if_fail(( pc->npoints == 2 ||
286                        pc->npoints == 5   ));
288     spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
289     spdc_endpoint_snap_free(pc, p, state);
292 static gint 
293 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
295     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
297     gint ret = FALSE;
299     switch (event->type) {
300         case GDK_BUTTON_PRESS:
301             ret = pen_handle_button_press(pc, event->button);
302             break;
303         default:
304             break;
305     }
307     if (!ret) {
308         if (((SPEventContextClass *) pen_parent_class)->item_handler)
309             ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
310     }
312     return ret;
315 /**
316  * Callback to handle all pen events.
317  */
318 static gint
319 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
321     SPPenContext *const pc = SP_PEN_CONTEXT(ec);
323     gint ret = FALSE;
325     switch (event->type) {
326         case GDK_BUTTON_PRESS:
327             ret = pen_handle_button_press(pc, event->button);
328             break;
330         case GDK_MOTION_NOTIFY:
331             ret = pen_handle_motion_notify(pc, event->motion);
332             break;
334         case GDK_BUTTON_RELEASE:
335             ret = pen_handle_button_release(pc, event->button);
336             break;
338         case GDK_2BUTTON_PRESS:
339             ret = pen_handle_2button_press(pc, event->button);
340             break;
342         case GDK_KEY_PRESS:
343             ret = pen_handle_key_press(pc, event);
344             break;
346         default:
347             break;
348     }
350     if (!ret) {
351         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
352             = ((SPEventContextClass *) pen_parent_class)->root_handler;
353         if (parent_root_handler) {
354             ret = parent_root_handler(ec, event);
355         }
356     }
358     return ret;
361 /**
362  * Handle mouse button press event.
363  */
364 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
366     if (pc->events_disabled) {
367         // skip event processing if events are disabled
368         return FALSE;
369     }
371     SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
372     SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
373     NR::Point const event_w(bevent.x, bevent.y);
374     NR::Point const event_dt(desktop->w2d(event_w));
375     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
377     gint ret = FALSE;
378     if (bevent.button == 1 && !event_context->space_panning) {
380         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
381             return TRUE;
382         }
384         pen_drag_origin_w = event_w;
385         pen_within_tolerance = true;
387         /* Test whether we hit any anchor. */
388         SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
390         switch (pc->mode) {
391             case SP_PEN_CONTEXT_MODE_CLICK:
392                 /* In click mode we add point on release */
393                 switch (pc->state) {
394                     case SP_PEN_CONTEXT_POINT:
395                     case SP_PEN_CONTEXT_CONTROL:
396                     case SP_PEN_CONTEXT_CLOSE:
397                         break;
398                     case SP_PEN_CONTEXT_STOP:
399                         /* This is allowed, if we just cancelled curve */
400                         pc->state = SP_PEN_CONTEXT_POINT;
401                         break;
402                     default:
403                         break;
404                 }
405                 break;
406             case SP_PEN_CONTEXT_MODE_DRAG:
407                 switch (pc->state) {
408                     case SP_PEN_CONTEXT_STOP:
409                         /* This is allowed, if we just cancelled curve */
410                     case SP_PEN_CONTEXT_POINT:
411                         if (pc->npoints == 0) {
413                             if (bevent.state & GDK_CONTROL_MASK) {
414                                 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
415                                 ret = TRUE;
416                                 break;
417                             }
419                             /* Set start anchor */
420                             pc->sa = anchor;
421                             NR::Point p;
422                             if (anchor) {
424                                 /* Adjust point to anchor if needed */
425                                 p = anchor->dp;
426                                 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
428                             } else {
430                                 // This is the first click of a new curve; deselect item so that
431                                 // this curve is not combined with it (unless it is drawn from its
432                                 // anchor, which is handled by the sibling branch above)
433                                 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
434                                 if (!(bevent.state & GDK_SHIFT_MASK)) {
436                                     selection->clear();
437                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
439                                 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
441                                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
442                                 }
444                                 /* Create green anchor */
445                                 p = event_dt;
446                                 spdc_endpoint_snap(pc, p, bevent.state);
447                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
448                             }
449                             spdc_pen_set_initial_point(pc, p);
450                         } else {
452                             /* Set end anchor */
453                             pc->ea = anchor;
454                             NR::Point p;
455                             if (anchor) {
457                                 p = anchor->dp;
458                                 // we hit an anchor, will finish the curve (either with or without closing)
459                                 // in release handler
460                                 pc->state = SP_PEN_CONTEXT_CLOSE;
462                                 if (pc->green_anchor && pc->green_anchor->active) {
463                                     // we clicked on the current curve start, so close it even if
464                                     // we drag a handle away from it
465                                     dc->green_closed = TRUE;
466                                 }
467                                 ret = TRUE;
468                                 break;
470                             } else {
472                                 p = event_dt;
473                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
474                                 spdc_pen_set_subsequent_point(pc, p, true);
475                             }
477                         }
478                         pc->state = SP_PEN_CONTEXT_CONTROL;
479                         ret = TRUE;
480                         break;
481                     case SP_PEN_CONTEXT_CONTROL:
482                         g_warning("Button down in CONTROL state");
483                         break;
484                     case SP_PEN_CONTEXT_CLOSE:
485                         g_warning("Button down in CLOSE state");
486                         break;
487                     default:
488                         break;
489                 }
490                 break;
491             default:
492                 break;
493         }
494     } else if (bevent.button == 3) {
495         if (pc->npoints != 0) {
497             spdc_pen_finish_segment(pc, event_dt, bevent.state);
498             if (pc->green_closed) {
499                 // finishing at the start anchor, close curve
500                 spdc_pen_finish(pc, TRUE);
501             } else {
502                 // finishing at some other anchor, finish curve but not close
503                 spdc_pen_finish(pc, FALSE);
504             }
506             ret = TRUE;
507         }
508     }
510     return ret;
513 /**
514  * Handle motion_notify event.
515  */
516 static gint
517 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
519     gint ret = FALSE;
521     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
523     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
524         // allow scrolling
525         return FALSE;
526     }
527     
528     if (pc->events_disabled) {
529         // skip motion events if pen events are disabled
530         return FALSE;
531     }
533     NR::Point const event_w(mevent.x,
534                             mevent.y);
535     if (pen_within_tolerance) {
536         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
537                                                                "value", 0, 0, 100);
538         if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
539             return FALSE;   // Do not drag if we're within tolerance from origin.
540         }
541     }
542     // Once the user has moved farther than tolerance from the original location
543     // (indicating they intend to move the object, not click), then always process the
544     // motion notify coordinates as given (no snapping back to origin)
545     pen_within_tolerance = false;
547     SPDesktop *const dt = pc->desktop;
548     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
549         /* Grab mouse, so release will not pass unnoticed */
550         pc->grab = SP_CANVAS_ITEM(dt->acetate);
551         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
552                                         GDK_BUTTON_RELEASE_MASK |
553                                         GDK_POINTER_MOTION_MASK  ),
554                             NULL, mevent.time);
555     }
557     /* Find desktop coordinates */
558     NR::Point p = dt->w2d(event_w);
560     /* Test, whether we hit any anchor */
561     SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
563     switch (pc->mode) {
564         case SP_PEN_CONTEXT_MODE_CLICK:
565             switch (pc->state) {
566                 case SP_PEN_CONTEXT_POINT:
567                     if ( pc->npoints != 0 ) {
568                         /* Only set point, if we are already appending */
569                         spdc_endpoint_snap(pc, p, mevent.state);
570                         spdc_pen_set_subsequent_point(pc, p, true);
571                         ret = TRUE;
572                     }
573                     break;
574                 case SP_PEN_CONTEXT_CONTROL:
575                 case SP_PEN_CONTEXT_CLOSE:
576                     /* Placing controls is last operation in CLOSE state */
577                     spdc_endpoint_snap(pc, p, mevent.state);
578                     spdc_pen_set_ctrl(pc, p, mevent.state);
579                     ret = TRUE;
580                     break;
581                 case SP_PEN_CONTEXT_STOP:
582                     /* This is perfectly valid */
583                     break;
584                 default:
585                     break;
586             }
587             break;
588         case SP_PEN_CONTEXT_MODE_DRAG:
589             switch (pc->state) {
590                 case SP_PEN_CONTEXT_POINT:
591                     if ( pc->npoints > 0 ) {
592                         /* Only set point, if we are already appending */
594                         if (!anchor) {   /* Snap node only if not hitting anchor */
595                             spdc_endpoint_snap(pc, p, mevent.state);
596                         }
598                         spdc_pen_set_subsequent_point(pc, p, !anchor);
600                         if (anchor && !pc->anchor_statusbar) {
601                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
602                             pc->anchor_statusbar = true;
603                         } else if (!anchor && pc->anchor_statusbar) {
604                             pc->_message_context->clear();
605                             pc->anchor_statusbar = false;
606                         }
608                         ret = TRUE;
609                     } else {
610                         if (anchor && !pc->anchor_statusbar) {
611                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
612                             pc->anchor_statusbar = true;
613                         } else if (!anchor && pc->anchor_statusbar) {
614                             pc->_message_context->clear();
615                             pc->anchor_statusbar = false;
616                         }
617                     }
618                     break;
619                 case SP_PEN_CONTEXT_CONTROL:
620                 case SP_PEN_CONTEXT_CLOSE:
621                     /* Placing controls is last operation in CLOSE state */
623                     // snap the handle
624                     spdc_endpoint_snap_handle(pc, p, mevent.state);
626                     spdc_pen_set_ctrl(pc, p, mevent.state);
627                     gobble_motion_events(GDK_BUTTON1_MASK);
628                     ret = TRUE;
629                     break;
630                 case SP_PEN_CONTEXT_STOP:
631                     /* This is perfectly valid */
632                     break;
633                 default:
634                     break;
635             }
636             break;
637         default:
638             break;
639     }
640     return ret;
643 /**
644  * Handle mouse button release event.
645  */
646 static gint
647 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
649     if (pc->events_disabled) {
650         // skip event processing if events are disabled
651         return FALSE;
652     }
654     gint ret = FALSE;
655     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
656     if ( revent.button == 1  && !event_context->space_panning) {
658         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
660         NR::Point const event_w(revent.x,
661                                 revent.y);
662         /* Find desktop coordinates */
663         NR::Point p = pc->desktop->w2d(event_w);
665         /* Test whether we hit any anchor. */
666         SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
668         switch (pc->mode) {
669             case SP_PEN_CONTEXT_MODE_CLICK:
670                 switch (pc->state) {
671                     case SP_PEN_CONTEXT_POINT:
672                         if ( pc->npoints == 0 ) {
673                             /* Start new thread only with button release */
674                             if (anchor) {
675                                 p = anchor->dp;
676                             }
677                             pc->sa = anchor;
678                             spdc_pen_set_initial_point(pc, p);
679                         } else {
680                             /* Set end anchor here */
681                             pc->ea = anchor;
682                             if (anchor) {
683                                 p = anchor->dp;
684                             }
685                         }
686                         pc->state = SP_PEN_CONTEXT_CONTROL;
687                         ret = TRUE;
688                         break;
689                     case SP_PEN_CONTEXT_CONTROL:
690                         /* End current segment */
691                         spdc_endpoint_snap(pc, p, revent.state);
692                         spdc_pen_finish_segment(pc, p, revent.state);
693                         pc->state = SP_PEN_CONTEXT_POINT;
694                         ret = TRUE;
695                         break;
696                     case SP_PEN_CONTEXT_CLOSE:
697                         /* End current segment */
698                         if (!anchor) {   /* Snap node only if not hitting anchor */
699                             spdc_endpoint_snap(pc, p, revent.state);
700                         }
701                         spdc_pen_finish_segment(pc, p, revent.state);
702                         spdc_pen_finish(pc, TRUE);
703                         pc->state = SP_PEN_CONTEXT_POINT;
704                         ret = TRUE;
705                         break;
706                     case SP_PEN_CONTEXT_STOP:
707                         /* This is allowed, if we just cancelled curve */
708                         pc->state = SP_PEN_CONTEXT_POINT;
709                         ret = TRUE;
710                         break;
711                     default:
712                         break;
713                 }
714                 break;
715             case SP_PEN_CONTEXT_MODE_DRAG:
716                 switch (pc->state) {
717                     case SP_PEN_CONTEXT_POINT:
718                     case SP_PEN_CONTEXT_CONTROL:
719                         spdc_endpoint_snap(pc, p, revent.state);
720                         spdc_pen_finish_segment(pc, p, revent.state);
721                         break;
722                     case SP_PEN_CONTEXT_CLOSE:
723                         spdc_endpoint_snap(pc, p, revent.state);
724                         spdc_pen_finish_segment(pc, p, revent.state);
725                         if (pc->green_closed) {
726                             // finishing at the start anchor, close curve
727                             spdc_pen_finish(pc, TRUE);
728                         } else {
729                             // finishing at some other anchor, finish curve but not close
730                             spdc_pen_finish(pc, FALSE);
731                         }
732                         break;
733                     case SP_PEN_CONTEXT_STOP:
734                         /* This is allowed, if we just cancelled curve */
735                         break;
736                     default:
737                         break;
738                 }
739                 pc->state = SP_PEN_CONTEXT_POINT;
740                 ret = TRUE;
741                 break;
742             default:
743                 break;
744         }
746         if (pc->grab) {
747             /* Release grab now */
748             sp_canvas_item_ungrab(pc->grab, revent.time);
749             pc->grab = NULL;
750         }
752         ret = TRUE;
754         dc->green_closed = FALSE;
755     }
757     return ret;
760 static gint
761 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
763     gint ret = FALSE;
764     if (pc->npoints != 0 && bevent.button != 2) {
765         spdc_pen_finish(pc, FALSE);
766         ret = TRUE;
767     }
768     return ret;
771 void
772 pen_redraw_all (SPPenContext *const pc)
774     // green
775     if (pc->green_bpaths) {
776         // remove old piecewise green canvasitems
777         while (pc->green_bpaths) {
778             gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
779             pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
780         }
781         // one canvas bpath for all of green_curve
782         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
783         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
784         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
786         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
787     }
789     if (pc->green_anchor)
790         SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
792     sp_curve_reset(pc->red_curve);
793     sp_curve_moveto(pc->red_curve, pc->p[0]);
794     sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
795     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
797     // handles
798     if (pc->p[0] != pc->p[1]) {
799         SP_CTRL(pc->c1)->moveto(pc->p[1]);
800         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
801         sp_canvas_item_show (pc->c1);
802         sp_canvas_item_show (pc->cl1);
803     } else {
804         sp_canvas_item_hide (pc->c1);
805         sp_canvas_item_hide (pc->cl1);
806     }
808     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
809     if (bpath) {
810         if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
811             SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
812             sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
813             sp_canvas_item_show (pc->c0);
814             sp_canvas_item_show (pc->cl0);
815         } else {
816             sp_canvas_item_hide (pc->c0);
817             sp_canvas_item_hide (pc->cl0);
818         }
819     }
822 void
823 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
825     if (pc->npoints != 5)
826         return;
828     // green
829     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
830     if (bpath) {
831         if (bpath->code == NR_CURVETO) {
832             bpath->x2 += x;
833             bpath->y2 += y;
834         }
835         bpath->x3 += x;
836         bpath->y3 += y;
837     } else {
838         // start anchor too
839         if (pc->green_anchor) {
840             pc->green_anchor->dp += NR::Point(x, y);
841         }
842     }
844     // red
845     pc->p[0] += NR::Point(x, y);
846     pc->p[1] += NR::Point(x, y);
847     pen_redraw_all(pc);
850 void
851 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
853     pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
856 void
857 pen_lastpoint_tocurve (SPPenContext *const pc)
859     if (pc->npoints != 5)
860         return;
862     // red
863     NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
864     if (bpath && bpath->code == NR_CURVETO) {
865         pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
866     } else {
867         pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
868     }
870     pen_redraw_all(pc);
873 void
874 pen_lastpoint_toline (SPPenContext *const pc)
876     if (pc->npoints != 5)
877         return;
879     pc->p[1] = pc->p[0];
881     pen_redraw_all(pc);
885 static gint
886 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
888     gint ret = FALSE;
889     gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
891     switch (get_group0_keyval (&event->key)) {
893         case GDK_Left: // move last point left
894         case GDK_KP_Left:
895         case GDK_KP_4:
896             if (!MOD__CTRL) { // not ctrl
897                 if (MOD__ALT) { // alt
898                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
899                     else pen_lastpoint_move_screen(pc, -1, 0); // no shift
900                 }
901                 else { // no alt
902                     if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
903                     else pen_lastpoint_move(pc, -nudge, 0); // no shift
904                 }
905                 ret = TRUE;
906             }
907             break;
908         case GDK_Up: // move last point up
909         case GDK_KP_Up:
910         case GDK_KP_8:
911             if (!MOD__CTRL) { // not ctrl
912                 if (MOD__ALT) { // alt
913                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
914                     else pen_lastpoint_move_screen(pc, 0, 1); // no shift
915                 }
916                 else { // no alt
917                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
918                     else pen_lastpoint_move(pc, 0, nudge); // no shift
919                 }
920                 ret = TRUE;
921             }
922             break;
923         case GDK_Right: // move last point right
924         case GDK_KP_Right:
925         case GDK_KP_6:
926             if (!MOD__CTRL) { // not ctrl
927                 if (MOD__ALT) { // alt
928                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
929                     else pen_lastpoint_move_screen(pc, 1, 0); // no shift
930                 }
931                 else { // no alt
932                     if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
933                     else pen_lastpoint_move(pc, nudge, 0); // no shift
934                 }
935                 ret = TRUE;
936             }
937             break;
938         case GDK_Down: // move last point down
939         case GDK_KP_Down:
940         case GDK_KP_2:
941             if (!MOD__CTRL) { // not ctrl
942                 if (MOD__ALT) { // alt
943                     if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
944                     else pen_lastpoint_move_screen(pc, 0, -1); // no shift
945                 }
946                 else { // no alt
947                     if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
948                     else pen_lastpoint_move(pc, 0, -nudge); // no shift
949                 }
950                 ret = TRUE;
951             }
952             break;
954         case GDK_U:
955         case GDK_u:
956             if (MOD__SHIFT_ONLY) {
957                 pen_lastpoint_tocurve(pc);
958                 ret = TRUE;
959             }
960             break;
961         case GDK_L:
962         case GDK_l:
963             if (MOD__SHIFT_ONLY) {
964                 pen_lastpoint_toline(pc);
965                 ret = TRUE;
966             }
967             break;
969         case GDK_Return:
970         case GDK_KP_Enter:
971             if (pc->npoints != 0) {
972                 spdc_pen_finish(pc, FALSE);
973                 ret = TRUE;
974             }
975             break;
976         case GDK_Escape:
977             if (pc->npoints != 0) {
978                 // if drawing, cancel, otherwise pass it up for deselecting
979                 pen_cancel (pc);
980                 ret = TRUE;
981             }
982             break;
983         case GDK_z:
984         case GDK_Z:
985             if (MOD__CTRL_ONLY && pc->npoints != 0) {
986                 // if drawing, cancel, otherwise pass it up for undo
987                 pen_cancel (pc);
988                 ret = TRUE;
989             }
990             break;
991         case GDK_g:
992         case GDK_G:
993             if (MOD__SHIFT_ONLY) {
994                 sp_selection_to_guides();
995                 ret = true;
996             }
997             break;
998         case GDK_BackSpace:
999         case GDK_Delete:
1000         case GDK_KP_Delete:
1001             if (sp_curve_is_empty(pc->green_curve)) {
1002                 pen_cancel (pc);
1003                 ret = TRUE;
1004             } else {
1005                 /* Reset red curve */
1006                 sp_curve_reset(pc->red_curve);
1007                 /* Destroy topmost green bpath */
1008                 if (pc->green_bpaths) {
1009                     if (pc->green_bpaths->data)
1010                         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1011                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1012                 }
1013                 /* Get last segment */
1014                 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1015                 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1016                 if ( e < 2 ) {
1017                     g_warning("Green curve length is %d", e);
1018                     break;
1019                 }
1020                 pc->p[0] = p[e - 2].c(3);
1021                 if (p[e - 1].code == NR_CURVETO) {
1022                     pc->p[1] = p[e - 1].c(1);
1023                 } else {
1024                     pc->p[1] = pc->p[0];
1025                 }
1026                 NR::Point const pt(( pc->npoints < 4
1027                                      ? p[e - 1].c(3)
1028                                      : pc->p[3] ));
1029                 pc->npoints = 2;
1030                 sp_curve_backspace(pc->green_curve);
1031                 sp_canvas_item_hide(pc->c0);
1032                 sp_canvas_item_hide(pc->c1);
1033                 sp_canvas_item_hide(pc->cl0);
1034                 sp_canvas_item_hide(pc->cl1);
1035                 pc->state = SP_PEN_CONTEXT_POINT;
1036                 spdc_pen_set_subsequent_point(pc, pt, true);
1037                 ret = TRUE;
1038             }
1039             break;
1040         default:
1041             break;
1042     }
1043     return ret;
1046 static void
1047 spdc_reset_colors(SPPenContext *pc)
1049     /* Red */
1050     sp_curve_reset(pc->red_curve);
1051     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1052     /* Blue */
1053     sp_curve_reset(pc->blue_curve);
1054     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1055     /* Green */
1056     while (pc->green_bpaths) {
1057         gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1058         pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1059     }
1060     sp_curve_reset(pc->green_curve);
1061     if (pc->green_anchor) {
1062         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1063     }
1064     pc->sa = NULL;
1065     pc->ea = NULL;
1066     pc->npoints = 0;
1067     pc->red_curve_is_valid = false;
1071 static void
1072 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1074     g_assert( pc->npoints == 0 );
1076     pc->p[0] = p;
1077     pc->p[1] = p;
1078     pc->npoints = 2;
1079     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1081     sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1084 static void
1085 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1087     g_assert( pc->npoints != 0 );
1088     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1090     pc->p[2] = p;
1091     pc->p[3] = p;
1092     pc->p[4] = p;
1093     pc->npoints = 5;
1094     sp_curve_reset(pc->red_curve);
1095     sp_curve_moveto(pc->red_curve, pc->p[0]);
1096     bool is_curve;
1097     if ( (pc->onlycurves)
1098          || ( pc->p[1] != pc->p[0] ) )
1099     {
1100         sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1101         is_curve = true;
1102     } else {
1103         sp_curve_lineto(pc->red_curve, p);
1104         is_curve = false;
1105     }
1107     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1109     if (statusbar) {
1110         // status text
1111         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1112         NR::Point rel = p - pc->p[0];
1113         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1114         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1115         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1116             angle = angle_to_compass (angle);
1117         pc->_message_context->setF(Inkscape::IMMEDIATE_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);
1118         g_string_free(dist, FALSE);
1119     }
1122 static void
1123 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1125     sp_canvas_item_show(pc->c1);
1126     sp_canvas_item_show(pc->cl1);
1128     if ( pc->npoints == 2 ) {
1129         pc->p[1] = p;
1130         sp_canvas_item_hide(pc->c0);
1131         sp_canvas_item_hide(pc->cl0);
1132         SP_CTRL(pc->c1)->moveto(pc->p[1]);
1133         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1135         // status text
1136         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1137         NR::Point rel = p - pc->p[0];
1138         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1139         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1140         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1141             angle = angle_to_compass (angle);
1142         pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Curve handle</b>: angle %3.2f&#176;, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1143         g_string_free(dist, FALSE);
1145     } else if ( pc->npoints == 5 ) {
1146         pc->p[4] = p;
1147         sp_canvas_item_show(pc->c0);
1148         sp_canvas_item_show(pc->cl0);
1149         bool is_symm = false;
1150         if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1151              ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) &&  !( state & GDK_SHIFT_MASK  ) ) ) {
1152             NR::Point delta = p - pc->p[3];
1153             pc->p[2] = pc->p[3] - delta;
1154             is_symm = true;
1155             sp_curve_reset(pc->red_curve);
1156             sp_curve_moveto(pc->red_curve, pc->p[0]);
1157             sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1158             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1159         }
1160         SP_CTRL(pc->c0)->moveto(pc->p[2]);
1161         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1162         SP_CTRL(pc->c1)->moveto(pc->p[4]);
1163         sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1165         // status text
1166         SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1167         NR::Point rel = p - pc->p[3];
1168         GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1169         double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1170         if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1171             angle = angle_to_compass (angle);
1172         pc->_message_context->setF(Inkscape::IMMEDIATE_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);
1173         g_string_free(dist, FALSE);
1175     } else {
1176         g_warning("Something bad happened - npoints is %d", pc->npoints);
1177     }
1180 static void
1181 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1183     if (!sp_curve_empty(pc->red_curve)) {
1184         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1185         SPCurve *curve = sp_curve_copy(pc->red_curve);
1186         /// \todo fixme:
1187         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1188         sp_curve_unref(curve);
1189         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1191         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1193         pc->p[0] = pc->p[3];
1194         pc->p[1] = pc->p[4];
1195         pc->npoints = 2;
1197         sp_curve_reset(pc->red_curve);
1198     }
1201 static void
1202 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1204     pen_disable_events(pc);
1205     
1206     SPDesktop *const desktop = pc->desktop;
1207     pc->_message_context->clear();
1208     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1210     sp_curve_reset(pc->red_curve);
1211     spdc_concat_colors_and_flush(pc, closed);
1212     pc->sa = NULL;
1213     pc->ea = NULL;
1215     pc->npoints = 0;
1216     pc->state = SP_PEN_CONTEXT_POINT;
1218     sp_canvas_item_hide(pc->c0);
1219     sp_canvas_item_hide(pc->c1);
1220     sp_canvas_item_hide(pc->cl0);
1221     sp_canvas_item_hide(pc->cl1);
1223     if (pc->green_anchor) {
1224         pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1225     }
1228     sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1230     pen_enable_events(pc);
1233 static void
1234 pen_disable_events(SPPenContext *const pc) {
1235   pc->events_disabled++;
1238 static void
1239 pen_enable_events(SPPenContext *const pc) {
1240   g_return_if_fail(pc->events_disabled != 0);
1241   
1242   pc->events_disabled--;
1245 /*
1246   Local Variables:
1247   mode:c++
1248   c-file-style:"stroustrup"
1249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1250   indent-tabs-mode:nil
1251   fill-column:99
1252   End:
1253 */
1254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :