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 "preferences.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, Inkscape::Preferences::Entry *val);
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*/, Inkscape::Preferences::Entry */*val*/)
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 Glib::ustring 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(PATTERN_ALONG_PATH, 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;
295 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
297 if (item && SP_IS_LPE_ITEM(item)) {
298 if (prefs->getInt(tool_name(dc) + "/freehand-mode", 0) == 1) {
299 Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
300 }
302 int shape = prefs->getInt(tool_name(dc) + "/shape", 0);
303 bool shape_applied = false;
304 SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
305 const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
307 #define SHAPE_LENGTH 100
308 #define SHAPE_HEIGHT 10
310 switch (shape) {
311 case 0:
312 // don't apply any shape
313 break;
314 case 1:
315 {
316 // "triangle in"
317 // TODO: this is only for illustration (we create a "decrescendo"-shaped path
318 // manually; eventually we should read the path from a separate file)
319 SPCurve *c = new SPCurve();
320 c->moveto(0,0);
321 c->lineto(0, SHAPE_HEIGHT);
322 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT/2);
323 c->closepath();
324 spdc_paste_curve_as_freehand_shape(c, dc, item);
325 c->unref();
327 shape_applied = true;
328 break;
329 }
330 case 2:
331 {
332 // "triangle out"
333 SPCurve *c = new SPCurve();
334 c->moveto(0, SHAPE_HEIGHT/2);
335 c->lineto(SHAPE_LENGTH, SHAPE_HEIGHT);
336 c->lineto(SHAPE_LENGTH, 0);
337 c->closepath();
338 spdc_paste_curve_as_freehand_shape(c, dc, item);
339 c->unref();
341 shape_applied = true;
342 break;
343 }
344 case 3:
345 {
346 // "ellipse"
347 SPCurve *c = new SPCurve();
348 const double C1 = 0.552;
349 c->moveto(0, SHAPE_HEIGHT/2);
350 c->curveto(0, (1 - C1) * SHAPE_HEIGHT/2, (1 - C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH/2, 0);
351 c->curveto((1 + C1) * SHAPE_LENGTH/2, 0, SHAPE_LENGTH, (1 - C1) * SHAPE_HEIGHT/2, SHAPE_LENGTH, SHAPE_HEIGHT/2);
352 c->curveto(SHAPE_LENGTH, (1 + C1) * SHAPE_HEIGHT/2, (1 + C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, SHAPE_LENGTH/2, SHAPE_HEIGHT);
353 c->curveto((1 - C1) * SHAPE_LENGTH/2, SHAPE_HEIGHT, 0, (1 + C1) * SHAPE_HEIGHT/2, 0, SHAPE_HEIGHT/2);
354 c->closepath();
355 spdc_paste_curve_as_freehand_shape(c, dc, item);
356 c->unref();
357 shape_applied = true;
358 break;
359 }
360 case 4:
361 {
362 // take shape from clipboard; TODO: catch the case where clipboard is empty
363 Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
364 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
365 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
367 shape_applied = true;
368 break;
369 }
370 default:
371 break;
372 }
373 if (shape_applied) {
374 // apply original stroke color as fill and unset stroke; then return
375 SPCSSAttr *css = sp_repr_css_attr_new();
377 if (!strcmp(cstroke, "none")){
378 sp_repr_css_set_property (css, "fill", "black");
379 } else {
380 sp_repr_css_set_property (css, "fill", cstroke);
381 }
382 sp_repr_css_set_property (css, "stroke", "none");
383 sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
384 sp_repr_css_attr_unref(css);
385 return;
386 }
388 if (dc->waiting_LPE_type != INVALID_LPE) {
389 Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
390 dc->waiting_LPE_type = INVALID_LPE;
392 if (SP_IS_LPETOOL_CONTEXT(dc)) {
393 // since a geometric LPE was applied, we switch back to "inactive" mode
394 lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), INVALID_LPE);
395 }
396 }
397 if (SP_IS_PEN_CONTEXT(dc)) {
398 sp_pen_context_set_polyline_mode(SP_PEN_CONTEXT(dc));
399 }
400 }
401 }
403 /*
404 * Selection handlers
405 */
407 static void
408 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
409 {
410 if (dc->attach) {
411 spdc_attach_selection(dc, sel);
412 }
413 }
415 /* fixme: We have to ensure this is not delayed (Lauris) */
417 static void
418 spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
419 {
420 if (dc->attach) {
421 spdc_attach_selection(dc, sel);
422 }
423 }
425 static void
426 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
427 {
428 /* We reset white and forget white/start/end anchors */
429 spdc_reset_white(dc);
430 dc->sa = NULL;
431 dc->ea = NULL;
433 SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
435 if ( item && SP_IS_PATH(item) ) {
436 /* Create new white data */
437 /* Item */
438 dc->white_item = item;
439 /* Curve list */
440 /* We keep it in desktop coordinates to eliminate calculation errors */
441 SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
442 norm->transform(sp_item_i2d_affine(dc->white_item));
443 g_return_if_fail( norm != NULL );
444 dc->white_curves = g_slist_reverse(norm->split());
445 norm->unref();
446 /* Anchor list */
447 for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
448 SPCurve *c;
449 c = (SPCurve*)l->data;
450 g_return_if_fail( c->get_segment_count() > 0 );
451 if ( !c->is_closed() ) {
452 SPDrawAnchor *a;
453 a = sp_draw_anchor_new(dc, c, TRUE, *(c->first_point()));
454 if (a)
455 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
456 a = sp_draw_anchor_new(dc, c, FALSE, *(c->last_point()));
457 if (a)
458 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
459 }
460 }
461 /* fixme: recalculate active anchor? */
462 }
463 }
466 /**
467 * Snaps node or handle to PI/rotationsnapsperpi degree increments.
468 *
469 * \param dc draw context
470 * \param p cursor point (to be changed by snapping)
471 * \param o origin point
472 * \param state keyboard state to check if ctrl was pressed
473 */
475 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p, Geom::Point const &o,
476 guint state)
477 {
478 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
479 unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
480 /* 0 means no snapping. */
482 /* mirrored by fabs, so this corresponds to 15 degrees */
483 Geom::Point best; /* best solution */
484 double bn = NR_HUGE; /* best normal */
485 double bdot = 0;
486 Geom::Point v = Geom::Point(0, 1);
487 double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
488 double const r10 = -r01, r11 = r00;
490 Geom::Point delta = p - o;
492 for (unsigned i = 0; i < snaps; i++) {
493 double const ndot = fabs(dot(v,Geom::rot90(delta)));
494 Geom::Point t(r00*v[Geom::X] + r01*v[Geom::Y],
495 r10*v[Geom::X] + r11*v[Geom::Y]);
496 if (ndot < bn) {
497 /* I think it is better numerically to use the normal, rather than the dot product
498 * to assess solutions, but I haven't proven it. */
499 bn = ndot;
500 best = v;
501 bdot = dot(v, delta);
502 }
503 v = t;
504 }
506 if (fabs(bdot) > 0) {
507 p = o + bdot * best;
509 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
510 //After all, the user explicitly asked for angular snapping by
511 //pressing CTRL
512 /* Snap it along best vector */
513 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
514 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
515 Geom::Point pt2g = to_2geom(p);
516 m.constrainedSnapReturnByRef( Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE, Inkscape::Snapper::ConstraintLine(best));
517 p = from_2geom(pt2g);
518 }
519 }
520 }
523 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
524 {
525 SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec);
526 SnapManager &m = dt->namedview->snap_manager;
527 Inkscape::Selection *selection = sp_desktop_selection (dt);
529 // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
530 // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
532 m.setup(dt, true, selection->singleItem());
533 Geom::Point pt2g = to_2geom(p);
534 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
535 p = from_2geom(pt2g);
536 }
538 static SPCurve *
539 reverse_then_unref(SPCurve *orig)
540 {
541 SPCurve *ret = orig->create_reverse();
542 orig->unref();
543 return ret;
544 }
546 /**
547 * Concats red, blue and green.
548 * If any anchors are defined, process these, optionally removing curves from white list
549 * Invoke _flush_white to write result back to object.
550 */
551 void
552 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
553 {
554 /* Concat RBG */
555 SPCurve *c = dc->green_curve;
557 /* Green */
558 dc->green_curve = new SPCurve();
559 while (dc->green_bpaths) {
560 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
561 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
562 }
563 /* Blue */
564 c->append_continuous(dc->blue_curve, 0.0625);
565 dc->blue_curve->reset();
566 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
567 /* Red */
568 if (dc->red_curve_is_valid) {
569 c->append_continuous(dc->red_curve, 0.0625);
570 }
571 dc->red_curve->reset();
572 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
574 if (c->is_empty()) {
575 c->unref();
576 return;
577 }
579 /* Step A - test, whether we ended on green anchor */
580 if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
581 // We hit green anchor, closing Green-Blue-Red
582 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
583 c->closepath_current();
584 /* Closed path, just flush */
585 spdc_flush_white(dc, c);
586 c->unref();
587 return;
588 }
590 /* Step B - both start and end anchored to same curve */
591 if ( dc->sa && dc->ea
592 && ( dc->sa->curve == dc->ea->curve )
593 && ( ( dc->sa != dc->ea )
594 || dc->sa->curve->is_closed() ) )
595 {
596 // We hit bot start and end of single curve, closing paths
597 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
598 if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
599 c = reverse_then_unref(c);
600 }
601 dc->sa->curve->append_continuous(c, 0.0625);
602 c->unref();
603 dc->sa->curve->closepath_current();
604 spdc_flush_white(dc, NULL);
605 return;
606 }
608 /* Step C - test start */
609 if (dc->sa) {
610 SPCurve *s = dc->sa->curve;
611 dc->white_curves = g_slist_remove(dc->white_curves, s);
612 if (dc->sa->start) {
613 s = reverse_then_unref(s);
614 }
615 s->append_continuous(c, 0.0625);
616 c->unref();
617 c = s;
618 } else /* Step D - test end */ if (dc->ea) {
619 SPCurve *e = dc->ea->curve;
620 dc->white_curves = g_slist_remove(dc->white_curves, e);
621 if (!dc->ea->start) {
622 e = reverse_then_unref(e);
623 }
624 c->append_continuous(e, 0.0625);
625 e->unref();
626 }
629 spdc_flush_white(dc, c);
631 c->unref();
632 }
634 /*
635 * Flushes white curve(s) and additional curve into object
636 *
637 * No cleaning of colored curves - this has to be done by caller
638 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
639 *
640 */
642 static void
643 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
644 {
645 SPCurve *c;
647 if (dc->white_curves) {
648 g_assert(dc->white_item);
649 c = SPCurve::concat(dc->white_curves);
650 g_slist_free(dc->white_curves);
651 dc->white_curves = NULL;
652 if (gc) {
653 c->append(gc, FALSE);
654 }
655 } else if (gc) {
656 c = gc;
657 c->ref();
658 } else {
659 return;
660 }
662 /* Now we have to go back to item coordinates at last */
663 c->transform( dc->white_item
664 ? sp_item_dt2i_affine(dc->white_item)
665 : to_2geom(sp_desktop_dt2doc_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) );
667 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
668 SPDocument *doc = sp_desktop_document(desktop);
669 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
671 if ( c && !c->is_empty() ) {
672 /* We actually have something to write */
674 bool has_lpe = false;
675 Inkscape::XML::Node *repr;
676 if (dc->white_item) {
677 repr = SP_OBJECT_REPR(dc->white_item);
678 has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
679 } else {
680 repr = xml_doc->createElement("svg:path");
681 /* Set style */
682 sp_desktop_apply_style_tool(desktop, repr, tool_name(dc).data(), false);
683 }
685 gchar *str = sp_svg_write_path( c->get_pathvector() );
686 g_assert( str != NULL );
687 if (has_lpe)
688 repr->setAttribute("inkscape:original-d", str);
689 else
690 repr->setAttribute("d", str);
691 g_free(str);
693 if (!dc->white_item) {
694 /* Attach repr */
695 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
697 // we finished the path; now apply any waiting LPEs or freehand shapes
698 spdc_check_for_and_apply_waiting_LPE(dc, item);
700 dc->selection->set(repr);
701 Inkscape::GC::release(repr);
702 item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
703 item->updateRepr();
704 }
706 sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
707 _("Draw path"));
709 // When quickly drawing several subpaths with Shift, the next subpath may be finished and
710 // flushed before the selection_modified signal is fired by the previous change, which
711 // results in the tool losing all of the selected path's curve except that last subpath. To
712 // fix this, we force the selection_modified callback now, to make sure the tool's curve is
713 // in sync immediately.
714 spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
715 }
717 c->unref();
719 /* Flush pending updates */
720 sp_document_ensure_up_to_date(doc);
721 }
723 /**
724 * Returns FIRST active anchor (the activated one).
725 */
726 SPDrawAnchor *
727 spdc_test_inside(SPDrawContext *dc, Geom::Point p)
728 {
729 SPDrawAnchor *active = NULL;
731 /* Test green anchor */
732 if (dc->green_anchor) {
733 active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
734 }
736 for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
737 SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
738 if ( !active && na ) {
739 active = na;
740 }
741 }
743 return active;
744 }
746 static void
747 spdc_reset_white(SPDrawContext *dc)
748 {
749 if (dc->white_item) {
750 /* We do not hold refcount */
751 dc->white_item = NULL;
752 }
753 while (dc->white_curves) {
754 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
755 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
756 }
757 while (dc->white_anchors) {
758 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
759 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
760 }
761 }
763 static void
764 spdc_free_colors(SPDrawContext *dc)
765 {
766 /* Red */
767 if (dc->red_bpath) {
768 gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
769 dc->red_bpath = NULL;
770 }
771 if (dc->red_curve) {
772 dc->red_curve = dc->red_curve->unref();
773 }
774 /* Blue */
775 if (dc->blue_bpath) {
776 gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
777 dc->blue_bpath = NULL;
778 }
779 if (dc->blue_curve) {
780 dc->blue_curve = dc->blue_curve->unref();
781 }
782 /* Green */
783 while (dc->green_bpaths) {
784 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
785 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
786 }
787 if (dc->green_curve) {
788 dc->green_curve = dc->green_curve->unref();
789 }
790 if (dc->green_anchor) {
791 dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
792 }
793 /* White */
794 if (dc->white_item) {
795 /* We do not hold refcount */
796 dc->white_item = NULL;
797 }
798 while (dc->white_curves) {
799 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
800 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
801 }
802 while (dc->white_anchors) {
803 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
804 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
805 }
806 }
808 /* Create a single dot represented by a circle */
809 void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) {
810 g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil"));
811 Glib::ustring tool_path = tool;
813 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
814 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
815 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
816 repr->setAttribute("sodipodi:type", "arc");
817 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
818 Inkscape::GC::release(repr);
820 /* apply the tool's current style */
821 sp_desktop_apply_style_tool(desktop, repr, tool, false);
823 /* find out stroke width (TODO: is there an easier way??) */
824 double stroke_width = 3.0;
825 gchar const *style_str = NULL;
826 style_str = repr->attribute("style");
827 if (style_str) {
828 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
829 sp_style_merge_from_style_string(style, style_str);
830 stroke_width = style->stroke_width.computed;
831 style->stroke_width.computed = 0;
832 sp_style_unref(style);
833 }
834 /* unset stroke and set fill color to former stroke color */
835 gchar * str;
836 str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
837 repr->setAttribute("style", str);
838 g_free(str);
840 /* put the circle where the mouse click occurred and set the diameter to the
841 current stroke width, multiplied by the amount specified in the preferences */
842 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
844 Geom::Matrix const i2d (sp_item_i2d_affine (item));
845 Geom::Point pp = pt * i2d;
846 double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
847 if (event_state & GDK_MOD1_MASK) {
848 /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
849 as specified in prefs. Very simple, but it might be sufficient in practice. If not,
850 we need to devise something more sophisticated. */
851 double s = g_random_double_range(-0.5, 0.5);
852 rad *= (1 + s);
853 }
854 if (event_state & GDK_SHIFT_MASK) {
855 // double the point size
856 rad *= 2;
857 }
859 sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]);
860 sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]);
861 sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
862 sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
863 item->updateRepr();
865 sp_desktop_selection(desktop)->set(item);
867 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
868 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
869 }
871 /*
872 Local Variables:
873 mode:c++
874 c-file-style:"stroustrup"
875 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
876 indent-tabs-mode:nil
877 fill-column:99
878 End:
879 */
880 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :