Code

Move all of the snapper code to 2geom
[inkscape.git] / src / draw-context.cpp
1 #define __SP_DRAW_CONTEXT_C__
3 /*
4  * Generic drawing context
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2000 Lauris Kaplinski
10  * Copyright (C) 2000-2001 Ximian, Inc.
11  * Copyright (C) 2002 Lauris Kaplinski
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #define DRAW_VERBOSE
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "display/canvas-bpath.h"
24 #include "xml/repr.h"
25 #include "svg/svg.h"
26 #include <glibmm/i18n.h>
27 #include "display/curve.h"
28 #include "desktop.h"
29 #include "desktop-affine.h"
30 #include "desktop-handles.h"
31 #include "desktop-style.h"
32 #include "document.h"
33 #include "draw-anchor.h"
34 #include "macros.h"
35 #include "message-stack.h"
36 #include "pen-context.h"
37 #include "prefs-utils.h"
38 #include "selection.h"
39 #include "selection-chemistry.h"
40 #include "snap.h"
41 #include "sp-path.h"
42 #include "sp-namedview.h"
43 #include "live_effects/lpe-patternalongpath.h"
44 #include "style.h"
46 static void sp_draw_context_class_init(SPDrawContextClass *klass);
47 static void sp_draw_context_init(SPDrawContext *dc);
48 static void sp_draw_context_dispose(GObject *object);
50 static void sp_draw_context_setup(SPEventContext *ec);
51 static void sp_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *value);
52 static void sp_draw_context_finish(SPEventContext *ec);
54 static gint sp_draw_context_root_handler(SPEventContext *event_context, GdkEvent *event);
56 static void spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc);
57 static void spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc);
59 static void spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel);
61 static void spdc_flush_white(SPDrawContext *dc, SPCurve *gc);
63 static void spdc_reset_white(SPDrawContext *dc);
64 static void spdc_free_colors(SPDrawContext *dc);
67 static SPEventContextClass *draw_parent_class;
70 GType
71 sp_draw_context_get_type(void)
72 {
73     static GType type = 0;
74     if (!type) {
75         GTypeInfo info = {
76             sizeof(SPDrawContextClass),
77             NULL, NULL,
78             (GClassInitFunc) sp_draw_context_class_init,
79             NULL, NULL,
80             sizeof(SPDrawContext),
81             4,
82             (GInstanceInitFunc) sp_draw_context_init,
83             NULL,   /* value_table */
84         };
85         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDrawContext", &info, (GTypeFlags)0);
86     }
87     return type;
88 }
90 static void
91 sp_draw_context_class_init(SPDrawContextClass *klass)
92 {
93     GObjectClass *object_class;
94     SPEventContextClass *ec_class;
96     object_class = (GObjectClass *)klass;
97     ec_class = (SPEventContextClass *) klass;
99     draw_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
101     object_class->dispose = sp_draw_context_dispose;
103     ec_class->setup = sp_draw_context_setup;
104     ec_class->set = sp_draw_context_set;
105     ec_class->finish = sp_draw_context_finish;
106     ec_class->root_handler = sp_draw_context_root_handler;
109 static void
110 sp_draw_context_init(SPDrawContext *dc)
112     dc->attach = FALSE;
114     dc->red_color = 0xff00007f;
115     dc->blue_color = 0x0000ff7f;
116     dc->green_color = 0x00ff007f;
117     dc->red_curve_is_valid = false;
119     dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
121     new (&dc->sel_changed_connection) sigc::connection();
122     new (&dc->sel_modified_connection) sigc::connection();
125 static void
126 sp_draw_context_dispose(GObject *object)
128     SPDrawContext *dc = SP_DRAW_CONTEXT(object);
130     dc->sel_changed_connection.~connection();
131     dc->sel_modified_connection.~connection();
133     if (dc->grab) {
134         sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
135         dc->grab = NULL;
136     }
138     if (dc->selection) {
139         dc->selection = NULL;
140     }
142     dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
144     spdc_free_colors(dc);
146     G_OBJECT_CLASS(draw_parent_class)->dispose(object);
149 static void
150 sp_draw_context_setup(SPEventContext *ec)
152     SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
153     SPDesktop *dt = ec->desktop;
155     if (((SPEventContextClass *) draw_parent_class)->setup) {
156         ((SPEventContextClass *) draw_parent_class)->setup(ec);
157     }
159     dc->selection = sp_desktop_selection(dt);
161     /* Connect signals to track selection changes */
162     dc->sel_changed_connection = dc->selection->connectChanged(
163         sigc::bind(sigc::ptr_fun(&spdc_selection_changed), dc)
164     );
165     dc->sel_modified_connection = dc->selection->connectModified(
166         sigc::bind(sigc::ptr_fun(&spdc_selection_modified), dc)
167     );
169     /* Create red bpath */
170     dc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
171     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->red_bpath), dc->red_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
172     /* Create red curve */
173     dc->red_curve = new SPCurve();
175     /* Create blue bpath */
176     dc->blue_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
177     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->blue_bpath), dc->blue_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
178     /* Create blue curve */
179     dc->blue_curve = new SPCurve();
181     /* Create green curve */
182     dc->green_curve = new SPCurve();
183     /* No green anchor by default */
184     dc->green_anchor = NULL;
185     dc->green_closed = FALSE;
187     dc->attach = TRUE;
188     spdc_attach_selection(dc, dc->selection);
191 static void
192 sp_draw_context_finish(SPEventContext *ec)
194     SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
196     dc->sel_changed_connection.disconnect();
197     dc->sel_modified_connection.disconnect();
199     if (dc->grab) {
200         sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
201     }
203     if (dc->selection) {
204         dc->selection = NULL;
205     }
207     spdc_free_colors(dc);
210 static void
211 sp_draw_context_set(SPEventContext */*ec*/, const gchar */*key*/, const gchar */*value*/)
215 gint
216 sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
218     gint ret = FALSE;
220     switch (event->type) {
221         case GDK_KEY_PRESS:
222             switch (get_group0_keyval (&event->key)) {
223                 case GDK_Up:
224                 case GDK_Down:
225                 case GDK_KP_Up:
226                 case GDK_KP_Down:
227                     // prevent the zoom field from activation
228                     if (!MOD__CTRL_ONLY) {
229                         ret = TRUE;
230                     }
231                     break;
232                 default:
233             break;
234         }
235         break;
236     default:
237         break;
238     }
240     if (!ret) {
241         if (((SPEventContextClass *) draw_parent_class)->root_handler) {
242             ret = ((SPEventContextClass *) draw_parent_class)->root_handler(ec, event);
243         }
244     }
246     return ret;
249 static char const *
250 tool_name(SPDrawContext *dc)
252     return ( SP_IS_PEN_CONTEXT(dc)
253              ? "tools.freehand.pen"
254              : "tools.freehand.pencil" );
257 static void
258 spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
260     using namespace Inkscape::LivePathEffect;
262     // TODO: Don't paste path if nothing is on the clipboard
264     Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
265     Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
266     gchar *svgd = sp_svg_write_path(c->get_pathvector());
267     static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
270 /*
271  * If we have an item and a waiting LPE, apply the effect to the item
272  * (spiro spline mode is treated separately)
273  */
274 void
275 spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
277     using namespace Inkscape::LivePathEffect;
279     if (item && SP_IS_LPE_ITEM(item)) {
280         if (prefs_get_int_attribute(tool_name(dc), "freehand-mode", 0) == 1) {
281             Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
282         }
284         int shape = prefs_get_int_attribute(tool_name(dc), "shape", 0);
285         bool shape_applied = false;
286         SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
287         const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
289 #define SHAPE_LENGTH 100
290 #define SHAPE_HEIGHT 10
292         switch (shape) {
293             case 0:
294                 // don't apply any shape
295                 break;
296             case 1:
297             {
298                 // "triangle in"
299                 // TODO: this is only for illustration (we create a "decrescendo"-shaped path
300                 //       manually; eventually we should read the path from a separate file)
301                 SPCurve *c = new SPCurve();
302                 c->moveto(0,0);
303                 c->lineto(0, SHAPE_HEIGHT);
304                 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT/2);
305                 c->closepath();
306                 spdc_paste_curve_as_freehand_shape(c, dc, item);
307                 c->unref();
309                 shape_applied = true;
310                 break;
311             }
312             case 2:
313             {
314                 // "triangle out"
315                 SPCurve *c = new SPCurve();
316                 c->moveto(0, SHAPE_HEIGHT/2);
317                 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT);
318                 c->lineto(SHAPE_LENGTH, 0);
319                 c->closepath();
320                 spdc_paste_curve_as_freehand_shape(c, dc, item);
321                 c->unref();
323                 shape_applied = true;
324                 break;
325             }
326             case 3:
327             {
328                 // "ellipse"
329                 SPCurve *c = new SPCurve();
330                 const double C1 = 0.552;
331                 c->moveto(0, SHAPE_HEIGHT/2);
332                 c->curveto(0, (1 - C1) * SHAPE_HEIGHT/2, (1 - C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH/2, 0);
333                 c->curveto((1 + C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH, (1 - C1) * SHAPE_HEIGHT/2, SHAPE_LENGTH, SHAPE_HEIGHT/2);
334                 c->curveto(SHAPE_LENGTH, (1 + C1) * SHAPE_HEIGHT/2, (1 + C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, SHAPE_LENGTH/2, SHAPE_HEIGHT);
335                 c->curveto((1 - C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, 0, (1 + C1) * SHAPE_HEIGHT/2, 0, SHAPE_HEIGHT/2);
336                 c->closepath();
337                 spdc_paste_curve_as_freehand_shape(c, dc, item);
338                 c->unref();
339                 shape_applied = true;
340                 break;
341             }
342             case 4:
343             {
344                 // take shape from clipboard; TODO: catch the case where clipboard is empty
345                 Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
346                 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
347                 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
349                 shape_applied = true;
350                 break;
351             }
352             default:
353                 break;
354         }
355         if (shape_applied) {
356             // apply original stroke color as fill and unset stroke; then return
357             SPCSSAttr *css = sp_repr_css_attr_new();
358             sp_repr_css_set_property (css, "fill", cstroke);
359             sp_repr_css_set_property (css, "stroke", "none");
360             sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
361             sp_repr_css_attr_unref(css);
362             return;
363         }
365         if (dc->waiting_LPE_type != INVALID_LPE) {
366             Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
367             dc->waiting_LPE_type = INVALID_LPE;
368         }
369         if (SP_IS_PEN_CONTEXT(dc)) {
370             sp_pen_context_set_polyline_mode(SP_PEN_CONTEXT(dc));
371         }
372     }
375 /*
376  * Selection handlers
377  */
379 static void
380 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
382     if (dc->attach) {
383         spdc_attach_selection(dc, sel);
384     }
387 /* fixme: We have to ensure this is not delayed (Lauris) */
389 static void
390 spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
392     if (dc->attach) {
393         spdc_attach_selection(dc, sel);
394     }
397 static void
398 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
400     /* We reset white and forget white/start/end anchors */
401     spdc_reset_white(dc);
402     dc->sa = NULL;
403     dc->ea = NULL;
405     SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
407     if ( item && SP_IS_PATH(item) ) {
408         /* Create new white data */
409         /* Item */
410         dc->white_item = item;
411         /* Curve list */
412         /* We keep it in desktop coordinates to eliminate calculation errors */
413         SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
414         norm->transform(sp_item_i2d_affine(dc->white_item));
415         g_return_if_fail( norm != NULL );
416         dc->white_curves = g_slist_reverse(norm->split());
417         norm->unref();
418         /* Anchor list */
419         for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
420             SPCurve *c;
421             c = (SPCurve*)l->data;
422             g_return_if_fail( c->get_segment_count() > 0 );
423             if ( !c->is_closed() ) {
424                 SPDrawAnchor *a;
425                 a = sp_draw_anchor_new(dc, c, TRUE, c->first_point());
426                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
427                 a = sp_draw_anchor_new(dc, c, FALSE, c->last_point());
428                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
429             }
430         }
431         /* fixme: recalculate active anchor? */
432     }
436 /**
437  *  Snaps node or handle to PI/rotationsnapsperpi degree increments.
438  *
439  *  \param dc draw context
440  *  \param p cursor point (to be changed by snapping)
441  *  \param o origin point
442  *  \param state  keyboard state to check if ctrl was pressed
443 */
445 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, NR::Point &p, NR::Point const &o,
446                                  guint state)
448     unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
449     /* 0 means no snapping. */
451     /* mirrored by fabs, so this corresponds to 15 degrees */
452     NR::Point best; /* best solution */
453     double bn = NR_HUGE; /* best normal */
454     double bdot = 0;
455     NR::Point v = NR::Point(0, 1);
456     double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
457     double const r10 = -r01, r11 = r00;
459     NR::Point delta = p - o;
461     for (unsigned i = 0; i < snaps; i++) {
462         double const ndot = fabs(dot(v,NR::rot90(delta)));
463         NR::Point t(r00*v[NR::X] + r01*v[NR::Y],
464                     r10*v[NR::X] + r11*v[NR::Y]);
465         if (ndot < bn) {
466             /* I think it is better numerically to use the normal, rather than the dot product
467              * to assess solutions, but I haven't proven it. */
468             bn = ndot;
469             best = v;
470             bdot = dot(v, delta);
471         }
472         v = t;
473     }
475     if (fabs(bdot) > 0) {
476         p = o + bdot * best;
478         if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
479                                          //After all, the user explicitely asked for angular snapping by
480                                          //pressing CTRL
481             /* Snap it along best vector */
482             SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
483             m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
484             Geom::Point pt2g = to_2geom(p);
485             m.constrainedSnapReturnByRef( Inkscape::Snapper::SNAPPOINT_NODE, pt2g, Inkscape::Snapper::ConstraintLine(best));
486             p = from_2geom(pt2g);
487         }
488     }
492 void spdc_endpoint_snap_free(SPEventContext const * const ec, NR::Point& p, guint const /*state*/)
494     SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
495     m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
496     Geom::Point pt2g = to_2geom(p);
497     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
498     p = from_2geom(pt2g);
501 static SPCurve *
502 reverse_then_unref(SPCurve *orig)
504     SPCurve *ret = orig->create_reverse();
505     orig->unref();
506     return ret;
509 /**
510  * Concats red, blue and green.
511  * If any anchors are defined, process these, optionally removing curves from white list
512  * Invoke _flush_white to write result back to object.
513  */
514 void
515 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
517     /* Concat RBG */
518     SPCurve *c = dc->green_curve;
520     /* Green */
521     dc->green_curve = new SPCurve();
522     while (dc->green_bpaths) {
523         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
524         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
525     }
526     /* Blue */
527     c->append_continuous(dc->blue_curve, 0.0625);
528     dc->blue_curve->reset();
529     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
530     /* Red */
531     if (dc->red_curve_is_valid) {
532         c->append_continuous(dc->red_curve, 0.0625);
533     }
534     dc->red_curve->reset();
535     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
537     if (c->is_empty()) {
538         c->unref();
539         return;
540     }
542     /* Step A - test, whether we ended on green anchor */
543     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
544         // We hit green anchor, closing Green-Blue-Red
545         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
546         c->closepath_current();
547         /* Closed path, just flush */
548         spdc_flush_white(dc, c);
549         c->unref();
550         return;
551     }
553     /* Step B - both start and end anchored to same curve */
554     if ( dc->sa && dc->ea
555          && ( dc->sa->curve == dc->ea->curve )
556          && ( ( dc->sa != dc->ea )
557               || dc->sa->curve->is_closed() ) )
558     {
559         // We hit bot start and end of single curve, closing paths
560         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
561         if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
562             c = reverse_then_unref(c);
563         }
564         dc->sa->curve->append_continuous(c, 0.0625);
565         c->unref();
566         dc->sa->curve->closepath_current();
567         spdc_flush_white(dc, NULL);
568         return;
569     }
571     /* Step C - test start */
572     if (dc->sa) {
573         SPCurve *s = dc->sa->curve;
574         dc->white_curves = g_slist_remove(dc->white_curves, s);
575         if (dc->sa->start) {
576             s = reverse_then_unref(s);
577         }
578         s->append_continuous(c, 0.0625);
579         c->unref();
580         c = s;
581     } else /* Step D - test end */ if (dc->ea) {
582         SPCurve *e = dc->ea->curve;
583         dc->white_curves = g_slist_remove(dc->white_curves, e);
584         if (!dc->ea->start) {
585             e = reverse_then_unref(e);
586         }
587         c->append_continuous(e, 0.0625);
588         e->unref();
589     }
592     spdc_flush_white(dc, c);
594     c->unref();
597 /*
598  * Flushes white curve(s) and additional curve into object
599  *
600  * No cleaning of colored curves - this has to be done by caller
601  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
602  *
603  */
605 static void
606 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
608     SPCurve *c;
610     if (dc->white_curves) {
611         g_assert(dc->white_item);
612         c = SPCurve::concat(dc->white_curves);
613         g_slist_free(dc->white_curves);
614         dc->white_curves = NULL;
615         if (gc) {
616             c->append(gc, FALSE);
617         }
618     } else if (gc) {
619         c = gc;
620         c->ref();
621     } else {
622         return;
623     }
625     /* Now we have to go back to item coordinates at last */
626     c->transform( dc->white_item
627                             ? sp_item_dt2i_affine(dc->white_item)
628                             : to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) );
630     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
631     SPDocument *doc = sp_desktop_document(desktop);
632     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
634     if ( c && !c->is_empty() ) {
635         /* We actually have something to write */
637         bool has_lpe = false;
638         Inkscape::XML::Node *repr;
639         if (dc->white_item) {
640             repr = SP_OBJECT_REPR(dc->white_item);
641             has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
642         } else {
643             repr = xml_doc->createElement("svg:path");
644             /* Set style */
645             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
646         }
648         gchar *str = sp_svg_write_path( c->get_pathvector() );
649         g_assert( str != NULL );
650         if (has_lpe)
651             repr->setAttribute("inkscape:original-d", str);
652         else
653             repr->setAttribute("d", str);
654         g_free(str);
656         if (!dc->white_item) {
657             /* Attach repr */
658             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
659             dc->selection->set(repr);
660             Inkscape::GC::release(repr);
661             item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
662             item->updateRepr();
663         }
666         // we finished the path; now apply any waiting LPEs or freehand shapes
667         // FIXME: placing this here seems to cause issues with undo!
668         spdc_check_for_and_apply_waiting_LPE(dc, dc->selection->singleItem());
670         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, 
671                          _("Draw path"));
673         // When quickly drawing several subpaths with Shift, the next subpath may be finished and
674         // flushed before the selection_modified signal is fired by the previous change, which
675         // results in the tool losing all of the selected path's curve except that last subpath. To
676         // fix this, we force the selection_modified callback now, to make sure the tool's curve is
677         // in sync immediately.
678         spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
679     }
681     c->unref();
683     /* Flush pending updates */
684     sp_document_ensure_up_to_date(doc);
687 /**
688  * Returns FIRST active anchor (the activated one).
689  */
690 SPDrawAnchor *
691 spdc_test_inside(SPDrawContext *dc, NR::Point p)
693     SPDrawAnchor *active = NULL;
695     /* Test green anchor */
696     if (dc->green_anchor) {
697         active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
698     }
700     for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
701         SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
702         if ( !active && na ) {
703             active = na;
704         }
705     }
707     return active;
710 static void
711 spdc_reset_white(SPDrawContext *dc)
713     if (dc->white_item) {
714         /* We do not hold refcount */
715         dc->white_item = NULL;
716     }
717     while (dc->white_curves) {
718         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
719         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
720     }
721     while (dc->white_anchors) {
722         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
723         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
724     }
727 static void
728 spdc_free_colors(SPDrawContext *dc)
730     /* Red */
731     if (dc->red_bpath) {
732         gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
733         dc->red_bpath = NULL;
734     }
735     if (dc->red_curve) {
736         dc->red_curve = dc->red_curve->unref();
737     }
738     /* Blue */
739     if (dc->blue_bpath) {
740         gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
741         dc->blue_bpath = NULL;
742     }
743     if (dc->blue_curve) {
744         dc->blue_curve = dc->blue_curve->unref();
745     }
746     /* Green */
747     while (dc->green_bpaths) {
748         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
749         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
750     }
751     if (dc->green_curve) {
752         dc->green_curve = dc->green_curve->unref();
753     }
754     if (dc->green_anchor) {
755         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
756     }
757     /* White */
758     if (dc->white_item) {
759         /* We do not hold refcount */
760         dc->white_item = NULL;
761     }
762     while (dc->white_curves) {
763         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
764         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
765     }
766     while (dc->white_anchors) {
767         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
768         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
769     }
773 /*
774   Local Variables:
775   mode:c++
776   c-file-style:"stroustrup"
777   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
778   indent-tabs-mode:nil
779   fill-column:99
780   End:
781 */
782 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :