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 "libnr/n-art-bpath.h"
28 #include "display/curve.h"
29 #include "desktop.h"
30 #include "desktop-affine.h"
31 #include "desktop-handles.h"
32 #include "desktop-style.h"
33 #include "document.h"
34 #include "draw-anchor.h"
35 #include "macros.h"
36 #include "message-stack.h"
37 #include "pen-context.h"
38 #include "prefs-utils.h"
39 #include "selection.h"
40 #include "selection-chemistry.h"
41 #include "snap.h"
42 #include "sp-path.h"
43 #include "sp-namedview.h"
44 #include "live_effects/lpe-patternalongpath.h"
45 #include "style.h"
47 static void sp_draw_context_class_init(SPDrawContextClass *klass);
48 static void sp_draw_context_init(SPDrawContext *dc);
49 static void sp_draw_context_dispose(GObject *object);
51 static void sp_draw_context_setup(SPEventContext *ec);
52 static void sp_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *value);
53 static void sp_draw_context_finish(SPEventContext *ec);
55 static gint sp_draw_context_root_handler(SPEventContext *event_context, GdkEvent *event);
57 static void spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc);
58 static void spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc);
60 static void spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel);
62 static void spdc_flush_white(SPDrawContext *dc, SPCurve *gc);
64 static void spdc_reset_white(SPDrawContext *dc);
65 static void spdc_free_colors(SPDrawContext *dc);
68 static SPEventContextClass *draw_parent_class;
71 GType
72 sp_draw_context_get_type(void)
73 {
74 static GType type = 0;
75 if (!type) {
76 GTypeInfo info = {
77 sizeof(SPDrawContextClass),
78 NULL, NULL,
79 (GClassInitFunc) sp_draw_context_class_init,
80 NULL, NULL,
81 sizeof(SPDrawContext),
82 4,
83 (GInstanceInitFunc) sp_draw_context_init,
84 NULL, /* value_table */
85 };
86 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDrawContext", &info, (GTypeFlags)0);
87 }
88 return type;
89 }
91 static void
92 sp_draw_context_class_init(SPDrawContextClass *klass)
93 {
94 GObjectClass *object_class;
95 SPEventContextClass *ec_class;
97 object_class = (GObjectClass *)klass;
98 ec_class = (SPEventContextClass *) klass;
100 draw_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
102 object_class->dispose = sp_draw_context_dispose;
104 ec_class->setup = sp_draw_context_setup;
105 ec_class->set = sp_draw_context_set;
106 ec_class->finish = sp_draw_context_finish;
107 ec_class->root_handler = sp_draw_context_root_handler;
108 }
110 static void
111 sp_draw_context_init(SPDrawContext *dc)
112 {
113 dc->attach = FALSE;
115 dc->red_color = 0xff00007f;
116 dc->blue_color = 0x0000ff7f;
117 dc->green_color = 0x00ff007f;
118 dc->red_curve_is_valid = false;
120 dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
122 new (&dc->sel_changed_connection) sigc::connection();
123 new (&dc->sel_modified_connection) sigc::connection();
124 }
126 static void
127 sp_draw_context_dispose(GObject *object)
128 {
129 SPDrawContext *dc = SP_DRAW_CONTEXT(object);
131 dc->sel_changed_connection.~connection();
132 dc->sel_modified_connection.~connection();
134 if (dc->grab) {
135 sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
136 dc->grab = NULL;
137 }
139 if (dc->selection) {
140 dc->selection = NULL;
141 }
143 dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
145 spdc_free_colors(dc);
147 G_OBJECT_CLASS(draw_parent_class)->dispose(object);
148 }
150 static void
151 sp_draw_context_setup(SPEventContext *ec)
152 {
153 SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
154 SPDesktop *dt = ec->desktop;
156 if (((SPEventContextClass *) draw_parent_class)->setup) {
157 ((SPEventContextClass *) draw_parent_class)->setup(ec);
158 }
160 dc->selection = sp_desktop_selection(dt);
162 /* Connect signals to track selection changes */
163 dc->sel_changed_connection = dc->selection->connectChanged(
164 sigc::bind(sigc::ptr_fun(&spdc_selection_changed), dc)
165 );
166 dc->sel_modified_connection = dc->selection->connectModified(
167 sigc::bind(sigc::ptr_fun(&spdc_selection_modified), dc)
168 );
170 /* Create red bpath */
171 dc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
172 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->red_bpath), dc->red_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
173 /* Create red curve */
174 dc->red_curve = new SPCurve();
176 /* Create blue bpath */
177 dc->blue_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
178 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->blue_bpath), dc->blue_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
179 /* Create blue curve */
180 dc->blue_curve = new SPCurve();
182 /* Create green curve */
183 dc->green_curve = new SPCurve();
184 /* No green anchor by default */
185 dc->green_anchor = NULL;
186 dc->green_closed = FALSE;
188 dc->attach = TRUE;
189 spdc_attach_selection(dc, dc->selection);
190 }
192 static void
193 sp_draw_context_finish(SPEventContext *ec)
194 {
195 SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
197 dc->sel_changed_connection.disconnect();
198 dc->sel_modified_connection.disconnect();
200 if (dc->grab) {
201 sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
202 }
204 if (dc->selection) {
205 dc->selection = NULL;
206 }
208 spdc_free_colors(dc);
209 }
211 static void
212 sp_draw_context_set(SPEventContext */*ec*/, const gchar */*key*/, const gchar */*value*/)
213 {
214 }
216 gint
217 sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
218 {
219 gint ret = FALSE;
221 switch (event->type) {
222 case GDK_KEY_PRESS:
223 switch (get_group0_keyval (&event->key)) {
224 case GDK_Up:
225 case GDK_Down:
226 case GDK_KP_Up:
227 case GDK_KP_Down:
228 // prevent the zoom field from activation
229 if (!MOD__CTRL_ONLY) {
230 ret = TRUE;
231 }
232 break;
233 default:
234 break;
235 }
236 break;
237 default:
238 break;
239 }
241 if (!ret) {
242 if (((SPEventContextClass *) draw_parent_class)->root_handler) {
243 ret = ((SPEventContextClass *) draw_parent_class)->root_handler(ec, event);
244 }
245 }
247 return ret;
248 }
250 static char const *
251 tool_name(SPDrawContext *dc)
252 {
253 return ( SP_IS_PEN_CONTEXT(dc)
254 ? "tools.freehand.pen"
255 : "tools.freehand.pencil" );
256 }
258 static void
259 spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
260 {
261 using namespace Inkscape::LivePathEffect;
263 // TODO: Don't paste path if nothing is on the clipboard
265 Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
266 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
267 gchar *svgd = sp_svg_write_path(c->get_pathvector());
268 static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
269 }
271 /*
272 * If we have an item and a waiting LPE, apply the effect to the item
273 * (spiro spline mode is treated separately)
274 */
275 void
276 spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
277 {
278 using namespace Inkscape::LivePathEffect;
280 if (item && SP_IS_LPE_ITEM(item)) {
281 if (prefs_get_int_attribute(tool_name(dc), "spiro-spline-mode", 0)) {
282 Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
283 }
285 int shape = prefs_get_int_attribute(tool_name(dc), "shape", 0);
286 bool shape_applied = false;
287 SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
288 const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
290 #define SHAPE_LENGTH 100
291 #define SHAPE_HEIGHT 10
293 switch (shape) {
294 case 0:
295 // don't apply any shape
296 break;
297 case 1:
298 {
299 // "triangle in"
300 // TODO: this is only for illustration (we create a "decrescendo"-shaped path
301 // manually; eventually we should read the path from a separate file)
302 SPCurve *c = new SPCurve();
303 c->moveto(0,0);
304 c->lineto(0, SHAPE_HEIGHT);
305 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT/2);
306 c->closepath();
307 spdc_paste_curve_as_freehand_shape(c, dc, item);
308 c->unref();
310 shape_applied = true;
311 break;
312 }
313 case 2:
314 {
315 // "triangle out"
316 SPCurve *c = new SPCurve();
317 c->moveto(0, SHAPE_HEIGHT/2);
318 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT);
319 c->lineto(SHAPE_LENGTH, 0);
320 c->closepath();
321 spdc_paste_curve_as_freehand_shape(c, dc, item);
322 c->unref();
324 shape_applied = true;
325 break;
326 }
327 case 3:
328 {
329 // "ellipse"
330 SPCurve *c = new SPCurve();
331 const double C1 = 0.552;
332 c->moveto(0, SHAPE_HEIGHT/2);
333 c->curveto(0, (1 - C1) * SHAPE_HEIGHT/2, (1 - C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH/2, 0);
334 c->curveto((1 + C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH, (1 - C1) * SHAPE_HEIGHT/2, SHAPE_LENGTH, SHAPE_HEIGHT/2);
335 c->curveto(SHAPE_LENGTH, (1 + C1) * SHAPE_HEIGHT/2, (1 + C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, SHAPE_LENGTH/2, SHAPE_HEIGHT);
336 c->curveto((1 - C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, 0, (1 + C1) * SHAPE_HEIGHT/2, 0, SHAPE_HEIGHT/2);
337 c->closepath();
338 spdc_paste_curve_as_freehand_shape(c, dc, item);
339 c->unref();
340 shape_applied = true;
341 break;
342 }
343 case 4:
344 {
345 // take shape from clipboard; TODO: catch the case where clipboard is empty
346 Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
347 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
348 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
350 shape_applied = true;
351 break;
352 }
353 default:
354 break;
355 }
356 if (shape_applied) {
357 // apply original stroke color as fill and unset stroke; then return
358 SPCSSAttr *css = sp_repr_css_attr_new();
359 sp_repr_css_set_property (css, "fill", cstroke);
360 sp_repr_css_set_property (css, "stroke", "none");
361 sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
362 sp_repr_css_attr_unref(css);
363 return;
364 }
366 if (dc->waiting_LPE_type != INVALID_LPE) {
367 Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
368 dc->waiting_LPE_type = INVALID_LPE;
369 }
370 if (SP_IS_PEN_CONTEXT(dc)) {
371 SP_PEN_CONTEXT(dc)->polylines_only = false;
372 }
373 }
374 }
376 /*
377 * Selection handlers
378 */
380 static void
381 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
382 {
383 if (dc->attach) {
384 spdc_attach_selection(dc, sel);
385 }
386 }
388 /* fixme: We have to ensure this is not delayed (Lauris) */
390 static void
391 spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
392 {
393 if (dc->attach) {
394 spdc_attach_selection(dc, sel);
395 }
396 }
398 static void
399 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
400 {
401 /* We reset white and forget white/start/end anchors */
402 spdc_reset_white(dc);
403 dc->sa = NULL;
404 dc->ea = NULL;
406 SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
408 if ( item && SP_IS_PATH(item) ) {
409 /* Create new white data */
410 /* Item */
411 dc->white_item = item;
412 /* Curve list */
413 /* We keep it in desktop coordinates to eliminate calculation errors */
414 SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
415 norm->transform(sp_item_i2d_affine(dc->white_item));
416 g_return_if_fail( norm != NULL );
417 dc->white_curves = g_slist_reverse(norm->split());
418 norm->unref();
419 /* Anchor list */
420 for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
421 SPCurve *c;
422 c = (SPCurve*)l->data;
423 g_return_if_fail( c->get_segment_count() > 0 );
424 if ( !c->is_closed() ) {
425 SPDrawAnchor *a;
426 a = sp_draw_anchor_new(dc, c, TRUE, c->first_point());
427 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
428 a = sp_draw_anchor_new(dc, c, FALSE, c->last_point());
429 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
430 }
431 }
432 /* fixme: recalculate active anchor? */
433 }
434 }
437 /**
438 * Snaps node or handle to PI/rotationsnapsperpi degree increments.
439 *
440 * \param dc draw context
441 * \param p cursor point (to be changed by snapping)
442 * \param o origin point
443 * \param state keyboard state to check if ctrl was pressed
444 */
446 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, NR::Point &p, NR::Point const o,
447 guint state)
448 {
449 /* Control must be down for this snap to work */
450 if ((state & GDK_CONTROL_MASK) == 0) {
451 return;
452 }
454 unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
455 /* 0 means no snapping. */
457 /* mirrored by fabs, so this corresponds to 15 degrees */
458 NR::Point best; /* best solution */
459 double bn = NR_HUGE; /* best normal */
460 double bdot = 0;
461 NR::Point v = NR::Point(0, 1);
462 double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
463 double const r10 = -r01, r11 = r00;
465 NR::Point delta = p - o;
467 for (unsigned i = 0; i < snaps; i++) {
468 double const ndot = fabs(dot(v,NR::rot90(delta)));
469 NR::Point t(r00*v[NR::X] + r01*v[NR::Y],
470 r10*v[NR::X] + r11*v[NR::Y]);
471 if (ndot < bn) {
472 /* I think it is better numerically to use the normal, rather than the dot product
473 * to assess solutions, but I haven't proven it. */
474 bn = ndot;
475 best = v;
476 bdot = dot(v, delta);
477 }
478 v = t;
479 }
481 if (fabs(bdot) > 0) {
482 p = o + bdot * best;
484 /* Snap it along best vector */
485 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
486 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
487 m.constrainedSnapReturnByRef( Inkscape::Snapper::SNAPPOINT_NODE, p, Inkscape::Snapper::ConstraintLine(best));
488 }
489 }
492 void spdc_endpoint_snap_free(SPEventContext const * const ec, NR::Point& p, guint const state)
493 {
494 /* Shift disables this snap */
495 if (state & GDK_SHIFT_MASK) {
496 return;
497 }
499 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
500 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
501 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p);
502 }
504 static SPCurve *
505 reverse_then_unref(SPCurve *orig)
506 {
507 SPCurve *ret = orig->create_reverse();
508 orig->unref();
509 return ret;
510 }
512 /**
513 * Concats red, blue and green.
514 * If any anchors are defined, process these, optionally removing curves from white list
515 * Invoke _flush_white to write result back to object.
516 */
517 void
518 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
519 {
520 /* Concat RBG */
521 SPCurve *c = dc->green_curve;
523 /* Green */
524 dc->green_curve = new SPCurve();
525 while (dc->green_bpaths) {
526 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
527 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
528 }
529 /* Blue */
530 c->append_continuous(dc->blue_curve, 0.0625);
531 dc->blue_curve->reset();
532 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
533 /* Red */
534 if (dc->red_curve_is_valid) {
535 c->append_continuous(dc->red_curve, 0.0625);
536 }
537 dc->red_curve->reset();
538 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
540 if (c->is_empty()) {
541 c->unref();
542 return;
543 }
545 /* Step A - test, whether we ended on green anchor */
546 if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
547 // We hit green anchor, closing Green-Blue-Red
548 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
549 c->closepath_current();
550 /* Closed path, just flush */
551 spdc_flush_white(dc, c);
552 c->unref();
553 return;
554 }
556 /* Step B - both start and end anchored to same curve */
557 if ( dc->sa && dc->ea
558 && ( dc->sa->curve == dc->ea->curve )
559 && ( ( dc->sa != dc->ea )
560 || dc->sa->curve->is_closed() ) )
561 {
562 // We hit bot start and end of single curve, closing paths
563 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
564 if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
565 c = reverse_then_unref(c);
566 }
567 dc->sa->curve->append_continuous(c, 0.0625);
568 c->unref();
569 dc->sa->curve->closepath_current();
570 spdc_flush_white(dc, NULL);
571 return;
572 }
574 /* Step C - test start */
575 if (dc->sa) {
576 SPCurve *s = dc->sa->curve;
577 dc->white_curves = g_slist_remove(dc->white_curves, s);
578 if (dc->sa->start) {
579 s = reverse_then_unref(s);
580 }
581 s->append_continuous(c, 0.0625);
582 c->unref();
583 c = s;
584 } else /* Step D - test end */ if (dc->ea) {
585 SPCurve *e = dc->ea->curve;
586 dc->white_curves = g_slist_remove(dc->white_curves, e);
587 if (!dc->ea->start) {
588 e = reverse_then_unref(e);
589 }
590 c->append_continuous(e, 0.0625);
591 e->unref();
592 }
595 spdc_flush_white(dc, c);
597 c->unref();
598 }
600 /*
601 * Flushes white curve(s) and additional curve into object
602 *
603 * No cleaning of colored curves - this has to be done by caller
604 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
605 *
606 */
608 static void
609 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
610 {
611 SPCurve *c;
613 if (dc->white_curves) {
614 g_assert(dc->white_item);
615 c = SPCurve::concat(dc->white_curves);
616 g_slist_free(dc->white_curves);
617 dc->white_curves = NULL;
618 if (gc) {
619 c->append(gc, FALSE);
620 }
621 } else if (gc) {
622 c = gc;
623 c->ref();
624 } else {
625 return;
626 }
628 /* Now we have to go back to item coordinates at last */
629 c->transform(( dc->white_item
630 ? sp_item_dt2i_affine(dc->white_item)
631 : to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) ));
633 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
634 SPDocument *doc = sp_desktop_document(desktop);
635 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
637 if ( c && !c->is_empty() ) {
638 /* We actually have something to write */
640 bool has_lpe = false;
641 Inkscape::XML::Node *repr;
642 if (dc->white_item) {
643 repr = SP_OBJECT_REPR(dc->white_item);
644 has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
645 } else {
646 repr = xml_doc->createElement("svg:path");
647 /* Set style */
648 sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
649 }
651 gchar *str = sp_svg_write_path( c->get_pathvector() );
652 g_assert( str != NULL );
653 if (has_lpe)
654 repr->setAttribute("inkscape:original-d", str);
655 else
656 repr->setAttribute("d", str);
657 g_free(str);
659 if (!dc->white_item) {
660 /* Attach repr */
661 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
662 dc->selection->set(repr);
663 Inkscape::GC::release(repr);
664 item->transform = from_2geom(i2i_affine(desktop->currentRoot(), desktop->currentLayer()));
665 item->updateRepr();
666 }
669 // we finished the path; now apply any waiting LPEs or freehand shapes
670 // FIXME: placing this here seems to cause issues with undo!
671 spdc_check_for_and_apply_waiting_LPE(dc, dc->selection->singleItem());
673 sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
674 _("Draw path"));
676 // When quickly drawing several subpaths with Shift, the next subpath may be finished and
677 // flushed before the selection_modified signal is fired by the previous change, which
678 // results in the tool losing all of the selected path's curve except that last subpath. To
679 // fix this, we force the selection_modified callback now, to make sure the tool's curve is
680 // in sync immediately.
681 spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
682 }
684 c->unref();
686 /* Flush pending updates */
687 sp_document_ensure_up_to_date(doc);
688 }
690 /**
691 * Returns FIRST active anchor (the activated one).
692 */
693 SPDrawAnchor *
694 spdc_test_inside(SPDrawContext *dc, NR::Point p)
695 {
696 SPDrawAnchor *active = NULL;
698 /* Test green anchor */
699 if (dc->green_anchor) {
700 active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
701 }
703 for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
704 SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
705 if ( !active && na ) {
706 active = na;
707 }
708 }
710 return active;
711 }
713 static void
714 spdc_reset_white(SPDrawContext *dc)
715 {
716 if (dc->white_item) {
717 /* We do not hold refcount */
718 dc->white_item = NULL;
719 }
720 while (dc->white_curves) {
721 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
722 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
723 }
724 while (dc->white_anchors) {
725 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
726 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
727 }
728 }
730 static void
731 spdc_free_colors(SPDrawContext *dc)
732 {
733 /* Red */
734 if (dc->red_bpath) {
735 gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
736 dc->red_bpath = NULL;
737 }
738 if (dc->red_curve) {
739 dc->red_curve = dc->red_curve->unref();
740 }
741 /* Blue */
742 if (dc->blue_bpath) {
743 gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
744 dc->blue_bpath = NULL;
745 }
746 if (dc->blue_curve) {
747 dc->blue_curve = dc->blue_curve->unref();
748 }
749 /* Green */
750 while (dc->green_bpaths) {
751 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
752 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
753 }
754 if (dc->green_curve) {
755 dc->green_curve = dc->green_curve->unref();
756 }
757 if (dc->green_anchor) {
758 dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
759 }
760 /* White */
761 if (dc->white_item) {
762 /* We do not hold refcount */
763 dc->white_item = NULL;
764 }
765 while (dc->white_curves) {
766 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
767 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
768 }
769 while (dc->white_anchors) {
770 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
771 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
772 }
773 }
776 /*
777 Local Variables:
778 mode:c++
779 c-file-style:"stroustrup"
780 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
781 indent-tabs-mode:nil
782 fill-column:99
783 End:
784 */
785 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :