Code

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