Code

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