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 "lpe-tool-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->red_bpath = NULL;
121 dc->red_curve = NULL;
123 dc->blue_bpath = NULL;
124 dc->blue_curve = NULL;
126 dc->green_bpaths = NULL;
127 dc->green_curve = NULL;
128 dc->green_anchor = NULL;
129 dc->green_closed = false;
131 dc->white_item = NULL;
132 dc->white_curves = NULL;
133 dc->white_anchors = NULL;
135 dc->sa = NULL;
136 dc->ea = NULL;
138 dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
140 new (&dc->sel_changed_connection) sigc::connection();
141 new (&dc->sel_modified_connection) sigc::connection();
142 }
144 static void
145 sp_draw_context_dispose(GObject *object)
146 {
147 SPDrawContext *dc = SP_DRAW_CONTEXT(object);
149 dc->sel_changed_connection.~connection();
150 dc->sel_modified_connection.~connection();
152 if (dc->grab) {
153 sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
154 dc->grab = NULL;
155 }
157 if (dc->selection) {
158 dc->selection = NULL;
159 }
161 spdc_free_colors(dc);
163 G_OBJECT_CLASS(draw_parent_class)->dispose(object);
164 }
166 static void
167 sp_draw_context_setup(SPEventContext *ec)
168 {
169 SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
170 SPDesktop *dt = ec->desktop;
172 if (((SPEventContextClass *) draw_parent_class)->setup) {
173 ((SPEventContextClass *) draw_parent_class)->setup(ec);
174 }
176 dc->selection = sp_desktop_selection(dt);
178 /* Connect signals to track selection changes */
179 dc->sel_changed_connection = dc->selection->connectChanged(
180 sigc::bind(sigc::ptr_fun(&spdc_selection_changed), dc)
181 );
182 dc->sel_modified_connection = dc->selection->connectModified(
183 sigc::bind(sigc::ptr_fun(&spdc_selection_modified), dc)
184 );
186 /* Create red bpath */
187 dc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
188 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->red_bpath), dc->red_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
189 /* Create red curve */
190 dc->red_curve = new SPCurve();
192 /* Create blue bpath */
193 dc->blue_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
194 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->blue_bpath), dc->blue_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
195 /* Create blue curve */
196 dc->blue_curve = new SPCurve();
198 /* Create green curve */
199 dc->green_curve = new SPCurve();
200 /* No green anchor by default */
201 dc->green_anchor = NULL;
202 dc->green_closed = FALSE;
204 dc->attach = TRUE;
205 spdc_attach_selection(dc, dc->selection);
206 }
208 static void
209 sp_draw_context_finish(SPEventContext *ec)
210 {
211 SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
213 dc->sel_changed_connection.disconnect();
214 dc->sel_modified_connection.disconnect();
216 if (dc->grab) {
217 sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
218 }
220 if (dc->selection) {
221 dc->selection = NULL;
222 }
224 spdc_free_colors(dc);
225 }
227 static void
228 sp_draw_context_set(SPEventContext */*ec*/, const gchar */*key*/, const gchar */*value*/)
229 {
230 }
232 gint
233 sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
234 {
235 gint ret = FALSE;
237 switch (event->type) {
238 case GDK_KEY_PRESS:
239 switch (get_group0_keyval (&event->key)) {
240 case GDK_Up:
241 case GDK_Down:
242 case GDK_KP_Up:
243 case GDK_KP_Down:
244 // prevent the zoom field from activation
245 if (!MOD__CTRL_ONLY) {
246 ret = TRUE;
247 }
248 break;
249 default:
250 break;
251 }
252 break;
253 default:
254 break;
255 }
257 if (!ret) {
258 if (((SPEventContextClass *) draw_parent_class)->root_handler) {
259 ret = ((SPEventContextClass *) draw_parent_class)->root_handler(ec, event);
260 }
261 }
263 return ret;
264 }
266 static char const *
267 tool_name(SPDrawContext *dc)
268 {
269 return ( SP_IS_PEN_CONTEXT(dc)
270 ? "tools.freehand.pen"
271 : "tools.freehand.pencil" );
272 }
274 static void
275 spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
276 {
277 using namespace Inkscape::LivePathEffect;
279 // TODO: Don't paste path if nothing is on the clipboard
281 Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
282 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
283 gchar *svgd = sp_svg_write_path(c->get_pathvector());
284 static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
285 }
287 /*
288 * If we have an item and a waiting LPE, apply the effect to the item
289 * (spiro spline mode is treated separately)
290 */
291 void
292 spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
293 {
294 using namespace Inkscape::LivePathEffect;
296 if (item && SP_IS_LPE_ITEM(item)) {
297 if (prefs_get_int_attribute(tool_name(dc), "freehand-mode", 0) == 1) {
298 Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
299 }
301 int shape = prefs_get_int_attribute(tool_name(dc), "shape", 0);
302 bool shape_applied = false;
303 SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
304 const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
306 #define SHAPE_LENGTH 100
307 #define SHAPE_HEIGHT 10
309 switch (shape) {
310 case 0:
311 // don't apply any shape
312 break;
313 case 1:
314 {
315 // "triangle in"
316 // TODO: this is only for illustration (we create a "decrescendo"-shaped path
317 // manually; eventually we should read the path from a separate file)
318 SPCurve *c = new SPCurve();
319 c->moveto(0,0);
320 c->lineto(0, SHAPE_HEIGHT);
321 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT/2);
322 c->closepath();
323 spdc_paste_curve_as_freehand_shape(c, dc, item);
324 c->unref();
326 shape_applied = true;
327 break;
328 }
329 case 2:
330 {
331 // "triangle out"
332 SPCurve *c = new SPCurve();
333 c->moveto(0, SHAPE_HEIGHT/2);
334 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT);
335 c->lineto(SHAPE_LENGTH, 0);
336 c->closepath();
337 spdc_paste_curve_as_freehand_shape(c, dc, item);
338 c->unref();
340 shape_applied = true;
341 break;
342 }
343 case 3:
344 {
345 // "ellipse"
346 SPCurve *c = new SPCurve();
347 const double C1 = 0.552;
348 c->moveto(0, SHAPE_HEIGHT/2);
349 c->curveto(0, (1 - C1) * SHAPE_HEIGHT/2, (1 - C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH/2, 0);
350 c->curveto((1 + C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH, (1 - C1) * SHAPE_HEIGHT/2, SHAPE_LENGTH, SHAPE_HEIGHT/2);
351 c->curveto(SHAPE_LENGTH, (1 + C1) * SHAPE_HEIGHT/2, (1 + C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, SHAPE_LENGTH/2, SHAPE_HEIGHT);
352 c->curveto((1 - C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, 0, (1 + C1) * SHAPE_HEIGHT/2, 0, SHAPE_HEIGHT/2);
353 c->closepath();
354 spdc_paste_curve_as_freehand_shape(c, dc, item);
355 c->unref();
356 shape_applied = true;
357 break;
358 }
359 case 4:
360 {
361 // take shape from clipboard; TODO: catch the case where clipboard is empty
362 Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
363 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
364 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
366 shape_applied = true;
367 break;
368 }
369 default:
370 break;
371 }
372 if (shape_applied) {
373 // apply original stroke color as fill and unset stroke; then return
374 SPCSSAttr *css = sp_repr_css_attr_new();
375 sp_repr_css_set_property (css, "fill", cstroke);
376 sp_repr_css_set_property (css, "stroke", "none");
377 sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
378 sp_repr_css_attr_unref(css);
379 return;
380 }
382 if (dc->waiting_LPE_type != INVALID_LPE) {
383 Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
384 dc->waiting_LPE_type = INVALID_LPE;
386 if (SP_IS_LPETOOL_CONTEXT(dc)) {
387 // since a geometric LPE was applied, we switch back to "inactive" mode
388 lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), Inkscape::LivePathEffect::INVALID_LPE);
389 }
390 }
391 if (SP_IS_PEN_CONTEXT(dc)) {
392 sp_pen_context_set_polyline_mode(SP_PEN_CONTEXT(dc));
393 }
394 }
395 }
397 /*
398 * Selection handlers
399 */
401 static void
402 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
403 {
404 if (dc->attach) {
405 spdc_attach_selection(dc, sel);
406 }
407 }
409 /* fixme: We have to ensure this is not delayed (Lauris) */
411 static void
412 spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
413 {
414 if (dc->attach) {
415 spdc_attach_selection(dc, sel);
416 }
417 }
419 static void
420 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
421 {
422 /* We reset white and forget white/start/end anchors */
423 spdc_reset_white(dc);
424 dc->sa = NULL;
425 dc->ea = NULL;
427 SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
429 if ( item && SP_IS_PATH(item) ) {
430 /* Create new white data */
431 /* Item */
432 dc->white_item = item;
433 /* Curve list */
434 /* We keep it in desktop coordinates to eliminate calculation errors */
435 SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
436 norm->transform(sp_item_i2d_affine(dc->white_item));
437 g_return_if_fail( norm != NULL );
438 dc->white_curves = g_slist_reverse(norm->split());
439 norm->unref();
440 /* Anchor list */
441 for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
442 SPCurve *c;
443 c = (SPCurve*)l->data;
444 g_return_if_fail( c->get_segment_count() > 0 );
445 if ( !c->is_closed() ) {
446 SPDrawAnchor *a;
447 a = sp_draw_anchor_new(dc, c, TRUE, *(c->first_point()));
448 if (a)
449 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
450 a = sp_draw_anchor_new(dc, c, FALSE, *(c->last_point()));
451 if (a)
452 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
453 }
454 }
455 /* fixme: recalculate active anchor? */
456 }
457 }
460 /**
461 * Snaps node or handle to PI/rotationsnapsperpi degree increments.
462 *
463 * \param dc draw context
464 * \param p cursor point (to be changed by snapping)
465 * \param o origin point
466 * \param state keyboard state to check if ctrl was pressed
467 */
469 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p, Geom::Point const &o,
470 guint state)
471 {
472 unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
473 /* 0 means no snapping. */
475 /* mirrored by fabs, so this corresponds to 15 degrees */
476 Geom::Point best; /* best solution */
477 double bn = NR_HUGE; /* best normal */
478 double bdot = 0;
479 Geom::Point v = Geom::Point(0, 1);
480 double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
481 double const r10 = -r01, r11 = r00;
483 Geom::Point delta = p - o;
485 for (unsigned i = 0; i < snaps; i++) {
486 double const ndot = fabs(dot(v,Geom::rot90(delta)));
487 Geom::Point t(r00*v[NR::X] + r01*v[NR::Y],
488 r10*v[NR::X] + r11*v[NR::Y]);
489 if (ndot < bn) {
490 /* I think it is better numerically to use the normal, rather than the dot product
491 * to assess solutions, but I haven't proven it. */
492 bn = ndot;
493 best = v;
494 bdot = dot(v, delta);
495 }
496 v = t;
497 }
499 if (fabs(bdot) > 0) {
500 p = o + bdot * best;
502 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
503 //After all, the user explicitely asked for angular snapping by
504 //pressing CTRL
505 /* Snap it along best vector */
506 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
507 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
508 Geom::Point pt2g = to_2geom(p);
509 m.constrainedSnapReturnByRef( Inkscape::Snapper::SNAPPOINT_NODE, pt2g, Inkscape::Snapper::ConstraintLine(best));
510 p = from_2geom(pt2g);
511 }
512 }
513 }
516 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
517 {
518 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
519 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
520 Geom::Point pt2g = to_2geom(p);
521 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
522 p = from_2geom(pt2g);
523 }
525 static SPCurve *
526 reverse_then_unref(SPCurve *orig)
527 {
528 SPCurve *ret = orig->create_reverse();
529 orig->unref();
530 return ret;
531 }
533 /**
534 * Concats red, blue and green.
535 * If any anchors are defined, process these, optionally removing curves from white list
536 * Invoke _flush_white to write result back to object.
537 */
538 void
539 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
540 {
541 /* Concat RBG */
542 SPCurve *c = dc->green_curve;
544 /* Green */
545 dc->green_curve = new SPCurve();
546 while (dc->green_bpaths) {
547 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
548 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
549 }
550 /* Blue */
551 c->append_continuous(dc->blue_curve, 0.0625);
552 dc->blue_curve->reset();
553 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
554 /* Red */
555 if (dc->red_curve_is_valid) {
556 c->append_continuous(dc->red_curve, 0.0625);
557 }
558 dc->red_curve->reset();
559 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
561 if (c->is_empty()) {
562 c->unref();
563 return;
564 }
566 /* Step A - test, whether we ended on green anchor */
567 if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
568 // We hit green anchor, closing Green-Blue-Red
569 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
570 c->closepath_current();
571 /* Closed path, just flush */
572 spdc_flush_white(dc, c);
573 c->unref();
574 return;
575 }
577 /* Step B - both start and end anchored to same curve */
578 if ( dc->sa && dc->ea
579 && ( dc->sa->curve == dc->ea->curve )
580 && ( ( dc->sa != dc->ea )
581 || dc->sa->curve->is_closed() ) )
582 {
583 // We hit bot start and end of single curve, closing paths
584 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
585 if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
586 c = reverse_then_unref(c);
587 }
588 dc->sa->curve->append_continuous(c, 0.0625);
589 c->unref();
590 dc->sa->curve->closepath_current();
591 spdc_flush_white(dc, NULL);
592 return;
593 }
595 /* Step C - test start */
596 if (dc->sa) {
597 SPCurve *s = dc->sa->curve;
598 dc->white_curves = g_slist_remove(dc->white_curves, s);
599 if (dc->sa->start) {
600 s = reverse_then_unref(s);
601 }
602 s->append_continuous(c, 0.0625);
603 c->unref();
604 c = s;
605 } else /* Step D - test end */ if (dc->ea) {
606 SPCurve *e = dc->ea->curve;
607 dc->white_curves = g_slist_remove(dc->white_curves, e);
608 if (!dc->ea->start) {
609 e = reverse_then_unref(e);
610 }
611 c->append_continuous(e, 0.0625);
612 e->unref();
613 }
616 spdc_flush_white(dc, c);
618 c->unref();
619 }
621 /*
622 * Flushes white curve(s) and additional curve into object
623 *
624 * No cleaning of colored curves - this has to be done by caller
625 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
626 *
627 */
629 static void
630 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
631 {
632 SPCurve *c;
634 if (dc->white_curves) {
635 g_assert(dc->white_item);
636 c = SPCurve::concat(dc->white_curves);
637 g_slist_free(dc->white_curves);
638 dc->white_curves = NULL;
639 if (gc) {
640 c->append(gc, FALSE);
641 }
642 } else if (gc) {
643 c = gc;
644 c->ref();
645 } else {
646 return;
647 }
649 /* Now we have to go back to item coordinates at last */
650 c->transform( dc->white_item
651 ? sp_item_dt2i_affine(dc->white_item)
652 : to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) );
654 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
655 SPDocument *doc = sp_desktop_document(desktop);
656 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
658 if ( c && !c->is_empty() ) {
659 /* We actually have something to write */
661 bool has_lpe = false;
662 Inkscape::XML::Node *repr;
663 if (dc->white_item) {
664 repr = SP_OBJECT_REPR(dc->white_item);
665 has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
666 } else {
667 repr = xml_doc->createElement("svg:path");
668 /* Set style */
669 sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
670 }
672 gchar *str = sp_svg_write_path( c->get_pathvector() );
673 g_assert( str != NULL );
674 if (has_lpe)
675 repr->setAttribute("inkscape:original-d", str);
676 else
677 repr->setAttribute("d", str);
678 g_free(str);
680 if (!dc->white_item) {
681 /* Attach repr */
682 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
684 // we finished the path; now apply any waiting LPEs or freehand shapes
685 spdc_check_for_and_apply_waiting_LPE(dc, item);
687 dc->selection->set(repr);
688 Inkscape::GC::release(repr);
689 item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
690 item->updateRepr();
691 }
693 sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
694 _("Draw path"));
696 // When quickly drawing several subpaths with Shift, the next subpath may be finished and
697 // flushed before the selection_modified signal is fired by the previous change, which
698 // results in the tool losing all of the selected path's curve except that last subpath. To
699 // fix this, we force the selection_modified callback now, to make sure the tool's curve is
700 // in sync immediately.
701 spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
702 }
704 c->unref();
706 /* Flush pending updates */
707 sp_document_ensure_up_to_date(doc);
708 }
710 /**
711 * Returns FIRST active anchor (the activated one).
712 */
713 SPDrawAnchor *
714 spdc_test_inside(SPDrawContext *dc, Geom::Point p)
715 {
716 SPDrawAnchor *active = NULL;
718 /* Test green anchor */
719 if (dc->green_anchor) {
720 active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
721 }
723 for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
724 SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
725 if ( !active && na ) {
726 active = na;
727 }
728 }
730 return active;
731 }
733 static void
734 spdc_reset_white(SPDrawContext *dc)
735 {
736 if (dc->white_item) {
737 /* We do not hold refcount */
738 dc->white_item = NULL;
739 }
740 while (dc->white_curves) {
741 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
742 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
743 }
744 while (dc->white_anchors) {
745 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
746 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
747 }
748 }
750 static void
751 spdc_free_colors(SPDrawContext *dc)
752 {
753 /* Red */
754 if (dc->red_bpath) {
755 gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
756 dc->red_bpath = NULL;
757 }
758 if (dc->red_curve) {
759 dc->red_curve = dc->red_curve->unref();
760 }
761 /* Blue */
762 if (dc->blue_bpath) {
763 gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
764 dc->blue_bpath = NULL;
765 }
766 if (dc->blue_curve) {
767 dc->blue_curve = dc->blue_curve->unref();
768 }
769 /* Green */
770 while (dc->green_bpaths) {
771 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
772 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
773 }
774 if (dc->green_curve) {
775 dc->green_curve = dc->green_curve->unref();
776 }
777 if (dc->green_anchor) {
778 dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
779 }
780 /* White */
781 if (dc->white_item) {
782 /* We do not hold refcount */
783 dc->white_item = NULL;
784 }
785 while (dc->white_curves) {
786 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
787 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
788 }
789 while (dc->white_anchors) {
790 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
791 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
792 }
793 }
795 /* Create a single dot represented by a circle */
796 void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) {
797 g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
799 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
800 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
801 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
802 repr->setAttribute("sodipodi:type", "arc");
803 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
804 Inkscape::GC::release(repr);
806 /* apply the tool's current style */
807 sp_desktop_apply_style_tool(desktop, repr, tool, false);
809 /* find out stroke width (TODO: is there an easier way??) */
810 double stroke_width = 3.0;
811 gchar const *style_str = NULL;
812 style_str = repr->attribute("style");
813 if (style_str) {
814 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
815 sp_style_merge_from_style_string(style, style_str);
816 stroke_width = style->stroke_width.computed;
817 style->stroke_width.computed = 0;
818 sp_style_unref(style);
819 }
820 /* unset stroke and set fill color to former stroke color */
821 gchar * str;
822 str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
823 repr->setAttribute("style", str);
824 g_free(str);
826 /* put the circle where the mouse click occurred and set the diameter to the
827 current stroke width, multiplied by the amount specified in the preferences */
828 Geom::Matrix const i2d (sp_item_i2d_affine (item));
829 Geom::Point pp = pt * i2d;
830 double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
831 if (event_state & GDK_MOD1_MASK) {
832 /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
833 as specified in prefs. Very simple, but it might be sufficient in practice. If not,
834 we need to devise something more sophisticated. */
835 double s = g_random_double_range(-0.5, 0.5);
836 rad *= (1 + s);
837 }
838 if (event_state & GDK_SHIFT_MASK) {
839 // double the point size
840 rad *= 2;
841 }
843 sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
844 sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
845 sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
846 sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
847 item->updateRepr();
849 sp_desktop_selection(desktop)->set(item);
851 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
852 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
853 }
855 /*
856 Local Variables:
857 mode:c++
858 c-file-style:"stroustrup"
859 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
860 indent-tabs-mode:nil
861 fill-column:99
862 End:
863 */
864 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :