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;
107 }
109 static void
110 sp_draw_context_init(SPDrawContext *dc)
111 {
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->red_bpath = NULL;
120 dc->red_curve = NULL;
122 dc->blue_bpath = NULL;
123 dc->blue_curve = NULL;
125 dc->green_bpaths = NULL;
126 dc->green_curve = NULL;
127 dc->green_anchor = NULL;
128 dc->green_closed = false;
130 dc->white_item = NULL;
131 dc->white_curves = NULL;
132 dc->white_anchors = NULL;
134 dc->sa = NULL;
135 dc->ea = NULL;
137 dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
139 new (&dc->sel_changed_connection) sigc::connection();
140 new (&dc->sel_modified_connection) sigc::connection();
141 }
143 static void
144 sp_draw_context_dispose(GObject *object)
145 {
146 SPDrawContext *dc = SP_DRAW_CONTEXT(object);
148 dc->sel_changed_connection.~connection();
149 dc->sel_modified_connection.~connection();
151 if (dc->grab) {
152 sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
153 dc->grab = NULL;
154 }
156 if (dc->selection) {
157 dc->selection = NULL;
158 }
160 spdc_free_colors(dc);
162 G_OBJECT_CLASS(draw_parent_class)->dispose(object);
163 }
165 static void
166 sp_draw_context_setup(SPEventContext *ec)
167 {
168 SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
169 SPDesktop *dt = ec->desktop;
171 if (((SPEventContextClass *) draw_parent_class)->setup) {
172 ((SPEventContextClass *) draw_parent_class)->setup(ec);
173 }
175 dc->selection = sp_desktop_selection(dt);
177 /* Connect signals to track selection changes */
178 dc->sel_changed_connection = dc->selection->connectChanged(
179 sigc::bind(sigc::ptr_fun(&spdc_selection_changed), dc)
180 );
181 dc->sel_modified_connection = dc->selection->connectModified(
182 sigc::bind(sigc::ptr_fun(&spdc_selection_modified), dc)
183 );
185 /* Create red bpath */
186 dc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
187 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->red_bpath), dc->red_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
188 /* Create red curve */
189 dc->red_curve = new SPCurve();
191 /* Create blue bpath */
192 dc->blue_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
193 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->blue_bpath), dc->blue_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
194 /* Create blue curve */
195 dc->blue_curve = new SPCurve();
197 /* Create green curve */
198 dc->green_curve = new SPCurve();
199 /* No green anchor by default */
200 dc->green_anchor = NULL;
201 dc->green_closed = FALSE;
203 dc->attach = TRUE;
204 spdc_attach_selection(dc, dc->selection);
205 }
207 static void
208 sp_draw_context_finish(SPEventContext *ec)
209 {
210 SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
212 dc->sel_changed_connection.disconnect();
213 dc->sel_modified_connection.disconnect();
215 if (dc->grab) {
216 sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
217 }
219 if (dc->selection) {
220 dc->selection = NULL;
221 }
223 spdc_free_colors(dc);
224 }
226 static void
227 sp_draw_context_set(SPEventContext */*ec*/, const gchar */*key*/, const gchar */*value*/)
228 {
229 }
231 gint
232 sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
233 {
234 gint ret = FALSE;
236 switch (event->type) {
237 case GDK_KEY_PRESS:
238 switch (get_group0_keyval (&event->key)) {
239 case GDK_Up:
240 case GDK_Down:
241 case GDK_KP_Up:
242 case GDK_KP_Down:
243 // prevent the zoom field from activation
244 if (!MOD__CTRL_ONLY) {
245 ret = TRUE;
246 }
247 break;
248 default:
249 break;
250 }
251 break;
252 default:
253 break;
254 }
256 if (!ret) {
257 if (((SPEventContextClass *) draw_parent_class)->root_handler) {
258 ret = ((SPEventContextClass *) draw_parent_class)->root_handler(ec, event);
259 }
260 }
262 return ret;
263 }
265 static char const *
266 tool_name(SPDrawContext *dc)
267 {
268 return ( SP_IS_PEN_CONTEXT(dc)
269 ? "tools.freehand.pen"
270 : "tools.freehand.pencil" );
271 }
273 static void
274 spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
275 {
276 using namespace Inkscape::LivePathEffect;
278 // TODO: Don't paste path if nothing is on the clipboard
280 Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
281 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
282 gchar *svgd = sp_svg_write_path(c->get_pathvector());
283 static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
284 }
286 /*
287 * If we have an item and a waiting LPE, apply the effect to the item
288 * (spiro spline mode is treated separately)
289 */
290 void
291 spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
292 {
293 using namespace Inkscape::LivePathEffect;
295 if (item && SP_IS_LPE_ITEM(item)) {
296 if (prefs_get_int_attribute(tool_name(dc), "freehand-mode", 0) == 1) {
297 Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
298 }
300 int shape = prefs_get_int_attribute(tool_name(dc), "shape", 0);
301 bool shape_applied = false;
302 SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
303 const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
305 #define SHAPE_LENGTH 100
306 #define SHAPE_HEIGHT 10
308 switch (shape) {
309 case 0:
310 // don't apply any shape
311 break;
312 case 1:
313 {
314 // "triangle in"
315 // TODO: this is only for illustration (we create a "decrescendo"-shaped path
316 // manually; eventually we should read the path from a separate file)
317 SPCurve *c = new SPCurve();
318 c->moveto(0,0);
319 c->lineto(0, SHAPE_HEIGHT);
320 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT/2);
321 c->closepath();
322 spdc_paste_curve_as_freehand_shape(c, dc, item);
323 c->unref();
325 shape_applied = true;
326 break;
327 }
328 case 2:
329 {
330 // "triangle out"
331 SPCurve *c = new SPCurve();
332 c->moveto(0, SHAPE_HEIGHT/2);
333 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT);
334 c->lineto(SHAPE_LENGTH, 0);
335 c->closepath();
336 spdc_paste_curve_as_freehand_shape(c, dc, item);
337 c->unref();
339 shape_applied = true;
340 break;
341 }
342 case 3:
343 {
344 // "ellipse"
345 SPCurve *c = new SPCurve();
346 const double C1 = 0.552;
347 c->moveto(0, SHAPE_HEIGHT/2);
348 c->curveto(0, (1 - C1) * SHAPE_HEIGHT/2, (1 - C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH/2, 0);
349 c->curveto((1 + C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH, (1 - C1) * SHAPE_HEIGHT/2, SHAPE_LENGTH, SHAPE_HEIGHT/2);
350 c->curveto(SHAPE_LENGTH, (1 + C1) * SHAPE_HEIGHT/2, (1 + C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, SHAPE_LENGTH/2, SHAPE_HEIGHT);
351 c->curveto((1 - C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, 0, (1 + C1) * SHAPE_HEIGHT/2, 0, SHAPE_HEIGHT/2);
352 c->closepath();
353 spdc_paste_curve_as_freehand_shape(c, dc, item);
354 c->unref();
355 shape_applied = true;
356 break;
357 }
358 case 4:
359 {
360 // take shape from clipboard; TODO: catch the case where clipboard is empty
361 Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
362 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
363 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
365 shape_applied = true;
366 break;
367 }
368 default:
369 break;
370 }
371 if (shape_applied) {
372 // apply original stroke color as fill and unset stroke; then return
373 SPCSSAttr *css = sp_repr_css_attr_new();
374 sp_repr_css_set_property (css, "fill", cstroke);
375 sp_repr_css_set_property (css, "stroke", "none");
376 sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
377 sp_repr_css_attr_unref(css);
378 return;
379 }
381 if (dc->waiting_LPE_type != INVALID_LPE) {
382 Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
383 dc->waiting_LPE_type = INVALID_LPE;
384 }
385 if (SP_IS_PEN_CONTEXT(dc)) {
386 sp_pen_context_set_polyline_mode(SP_PEN_CONTEXT(dc));
387 }
388 }
389 }
391 /*
392 * Selection handlers
393 */
395 static void
396 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
397 {
398 if (dc->attach) {
399 spdc_attach_selection(dc, sel);
400 }
401 }
403 /* fixme: We have to ensure this is not delayed (Lauris) */
405 static void
406 spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
407 {
408 if (dc->attach) {
409 spdc_attach_selection(dc, sel);
410 }
411 }
413 static void
414 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
415 {
416 /* We reset white and forget white/start/end anchors */
417 spdc_reset_white(dc);
418 dc->sa = NULL;
419 dc->ea = NULL;
421 SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
423 if ( item && SP_IS_PATH(item) ) {
424 /* Create new white data */
425 /* Item */
426 dc->white_item = item;
427 /* Curve list */
428 /* We keep it in desktop coordinates to eliminate calculation errors */
429 SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
430 norm->transform(sp_item_i2d_affine(dc->white_item));
431 g_return_if_fail( norm != NULL );
432 dc->white_curves = g_slist_reverse(norm->split());
433 norm->unref();
434 /* Anchor list */
435 for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
436 SPCurve *c;
437 c = (SPCurve*)l->data;
438 g_return_if_fail( c->get_segment_count() > 0 );
439 if ( !c->is_closed() ) {
440 SPDrawAnchor *a;
441 a = sp_draw_anchor_new(dc, c, TRUE, *(c->first_point()));
442 if (a)
443 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
444 a = sp_draw_anchor_new(dc, c, FALSE, *(c->last_point()));
445 if (a)
446 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
447 }
448 }
449 /* fixme: recalculate active anchor? */
450 }
451 }
454 /**
455 * Snaps node or handle to PI/rotationsnapsperpi degree increments.
456 *
457 * \param dc draw context
458 * \param p cursor point (to be changed by snapping)
459 * \param o origin point
460 * \param state keyboard state to check if ctrl was pressed
461 */
463 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, NR::Point &p, NR::Point const &o,
464 guint state)
465 {
466 unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
467 /* 0 means no snapping. */
469 /* mirrored by fabs, so this corresponds to 15 degrees */
470 NR::Point best; /* best solution */
471 double bn = NR_HUGE; /* best normal */
472 double bdot = 0;
473 NR::Point v = NR::Point(0, 1);
474 double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
475 double const r10 = -r01, r11 = r00;
477 NR::Point delta = p - o;
479 for (unsigned i = 0; i < snaps; i++) {
480 double const ndot = fabs(dot(v,NR::rot90(delta)));
481 NR::Point t(r00*v[NR::X] + r01*v[NR::Y],
482 r10*v[NR::X] + r11*v[NR::Y]);
483 if (ndot < bn) {
484 /* I think it is better numerically to use the normal, rather than the dot product
485 * to assess solutions, but I haven't proven it. */
486 bn = ndot;
487 best = v;
488 bdot = dot(v, delta);
489 }
490 v = t;
491 }
493 if (fabs(bdot) > 0) {
494 p = o + bdot * best;
496 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
497 //After all, the user explicitely asked for angular snapping by
498 //pressing CTRL
499 /* Snap it along best vector */
500 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
501 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
502 Geom::Point pt2g = to_2geom(p);
503 m.constrainedSnapReturnByRef( Inkscape::Snapper::SNAPPOINT_NODE, pt2g, Inkscape::Snapper::ConstraintLine(best));
504 p = from_2geom(pt2g);
505 }
506 }
507 }
510 void spdc_endpoint_snap_free(SPEventContext const * const ec, NR::Point& p, guint const /*state*/)
511 {
512 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
513 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
514 Geom::Point pt2g = to_2geom(p);
515 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
516 p = from_2geom(pt2g);
517 }
519 static SPCurve *
520 reverse_then_unref(SPCurve *orig)
521 {
522 SPCurve *ret = orig->create_reverse();
523 orig->unref();
524 return ret;
525 }
527 /**
528 * Concats red, blue and green.
529 * If any anchors are defined, process these, optionally removing curves from white list
530 * Invoke _flush_white to write result back to object.
531 */
532 void
533 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
534 {
535 /* Concat RBG */
536 SPCurve *c = dc->green_curve;
538 /* Green */
539 dc->green_curve = new SPCurve();
540 while (dc->green_bpaths) {
541 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
542 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
543 }
544 /* Blue */
545 c->append_continuous(dc->blue_curve, 0.0625);
546 dc->blue_curve->reset();
547 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
548 /* Red */
549 if (dc->red_curve_is_valid) {
550 c->append_continuous(dc->red_curve, 0.0625);
551 }
552 dc->red_curve->reset();
553 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
555 if (c->is_empty()) {
556 c->unref();
557 return;
558 }
560 /* Step A - test, whether we ended on green anchor */
561 if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
562 // We hit green anchor, closing Green-Blue-Red
563 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
564 c->closepath_current();
565 /* Closed path, just flush */
566 spdc_flush_white(dc, c);
567 c->unref();
568 return;
569 }
571 /* Step B - both start and end anchored to same curve */
572 if ( dc->sa && dc->ea
573 && ( dc->sa->curve == dc->ea->curve )
574 && ( ( dc->sa != dc->ea )
575 || dc->sa->curve->is_closed() ) )
576 {
577 // We hit bot start and end of single curve, closing paths
578 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
579 if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
580 c = reverse_then_unref(c);
581 }
582 dc->sa->curve->append_continuous(c, 0.0625);
583 c->unref();
584 dc->sa->curve->closepath_current();
585 spdc_flush_white(dc, NULL);
586 return;
587 }
589 /* Step C - test start */
590 if (dc->sa) {
591 SPCurve *s = dc->sa->curve;
592 dc->white_curves = g_slist_remove(dc->white_curves, s);
593 if (dc->sa->start) {
594 s = reverse_then_unref(s);
595 }
596 s->append_continuous(c, 0.0625);
597 c->unref();
598 c = s;
599 } else /* Step D - test end */ if (dc->ea) {
600 SPCurve *e = dc->ea->curve;
601 dc->white_curves = g_slist_remove(dc->white_curves, e);
602 if (!dc->ea->start) {
603 e = reverse_then_unref(e);
604 }
605 c->append_continuous(e, 0.0625);
606 e->unref();
607 }
610 spdc_flush_white(dc, c);
612 c->unref();
613 }
615 /*
616 * Flushes white curve(s) and additional curve into object
617 *
618 * No cleaning of colored curves - this has to be done by caller
619 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
620 *
621 */
623 static void
624 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
625 {
626 SPCurve *c;
628 if (dc->white_curves) {
629 g_assert(dc->white_item);
630 c = SPCurve::concat(dc->white_curves);
631 g_slist_free(dc->white_curves);
632 dc->white_curves = NULL;
633 if (gc) {
634 c->append(gc, FALSE);
635 }
636 } else if (gc) {
637 c = gc;
638 c->ref();
639 } else {
640 return;
641 }
643 /* Now we have to go back to item coordinates at last */
644 c->transform( dc->white_item
645 ? sp_item_dt2i_affine(dc->white_item)
646 : to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) );
648 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
649 SPDocument *doc = sp_desktop_document(desktop);
650 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
652 if ( c && !c->is_empty() ) {
653 /* We actually have something to write */
655 bool has_lpe = false;
656 Inkscape::XML::Node *repr;
657 if (dc->white_item) {
658 repr = SP_OBJECT_REPR(dc->white_item);
659 has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
660 } else {
661 repr = xml_doc->createElement("svg:path");
662 /* Set style */
663 sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
664 }
666 gchar *str = sp_svg_write_path( c->get_pathvector() );
667 g_assert( str != NULL );
668 if (has_lpe)
669 repr->setAttribute("inkscape:original-d", str);
670 else
671 repr->setAttribute("d", str);
672 g_free(str);
674 if (!dc->white_item) {
675 /* Attach repr */
676 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
677 dc->selection->set(repr);
678 Inkscape::GC::release(repr);
679 item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
680 item->updateRepr();
681 }
684 // we finished the path; now apply any waiting LPEs or freehand shapes
685 spdc_check_for_and_apply_waiting_LPE(dc, dc->selection->singleItem());
687 sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
688 _("Draw path"));
690 // When quickly drawing several subpaths with Shift, the next subpath may be finished and
691 // flushed before the selection_modified signal is fired by the previous change, which
692 // results in the tool losing all of the selected path's curve except that last subpath. To
693 // fix this, we force the selection_modified callback now, to make sure the tool's curve is
694 // in sync immediately.
695 spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
696 }
698 c->unref();
700 /* Flush pending updates */
701 sp_document_ensure_up_to_date(doc);
702 }
704 /**
705 * Returns FIRST active anchor (the activated one).
706 */
707 SPDrawAnchor *
708 spdc_test_inside(SPDrawContext *dc, NR::Point p)
709 {
710 SPDrawAnchor *active = NULL;
712 /* Test green anchor */
713 if (dc->green_anchor) {
714 active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
715 }
717 for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
718 SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
719 if ( !active && na ) {
720 active = na;
721 }
722 }
724 return active;
725 }
727 static void
728 spdc_reset_white(SPDrawContext *dc)
729 {
730 if (dc->white_item) {
731 /* We do not hold refcount */
732 dc->white_item = NULL;
733 }
734 while (dc->white_curves) {
735 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
736 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
737 }
738 while (dc->white_anchors) {
739 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
740 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
741 }
742 }
744 static void
745 spdc_free_colors(SPDrawContext *dc)
746 {
747 /* Red */
748 if (dc->red_bpath) {
749 gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
750 dc->red_bpath = NULL;
751 }
752 if (dc->red_curve) {
753 dc->red_curve = dc->red_curve->unref();
754 }
755 /* Blue */
756 if (dc->blue_bpath) {
757 gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
758 dc->blue_bpath = NULL;
759 }
760 if (dc->blue_curve) {
761 dc->blue_curve = dc->blue_curve->unref();
762 }
763 /* Green */
764 while (dc->green_bpaths) {
765 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
766 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
767 }
768 if (dc->green_curve) {
769 dc->green_curve = dc->green_curve->unref();
770 }
771 if (dc->green_anchor) {
772 dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
773 }
774 /* White */
775 if (dc->white_item) {
776 /* We do not hold refcount */
777 dc->white_item = NULL;
778 }
779 while (dc->white_curves) {
780 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
781 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
782 }
783 while (dc->white_anchors) {
784 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
785 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
786 }
787 }
790 /*
791 Local Variables:
792 mode:c++
793 c-file-style:"stroustrup"
794 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
795 indent-tabs-mode:nil
796 fill-column:99
797 End:
798 */
799 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :