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-handles.h"
30 #include "desktop-style.h"
31 #include "document.h"
32 #include "draw-anchor.h"
33 #include "macros.h"
34 #include "message-stack.h"
35 #include "pen-context.h"
36 #include "lpe-tool-context.h"
37 #include "preferences.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, Inkscape::Preferences::Entry *val);
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*/, Inkscape::Preferences::Entry */*val*/)
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 Glib::ustring 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(PATTERN_ALONG_PATH, 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;
294 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
296 if (item && SP_IS_LPE_ITEM(item)) {
297 if (prefs->getInt(tool_name(dc) + "/freehand-mode", 0) == 1) {
298 Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
299 }
301 int shape = prefs->getInt(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 10
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(PATTERN_ALONG_PATH, 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();
376 if (!strcmp(cstroke, "none")){
377 sp_repr_css_set_property (css, "fill", "black");
378 } else {
379 sp_repr_css_set_property (css, "fill", cstroke);
380 }
381 sp_repr_css_set_property (css, "stroke", "none");
382 sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
383 sp_repr_css_attr_unref(css);
384 return;
385 }
387 if (dc->waiting_LPE_type != INVALID_LPE) {
388 Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
389 dc->waiting_LPE_type = INVALID_LPE;
391 if (SP_IS_LPETOOL_CONTEXT(dc)) {
392 // since a geometric LPE was applied, we switch back to "inactive" mode
393 lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), INVALID_LPE);
394 }
395 }
396 if (SP_IS_PEN_CONTEXT(dc)) {
397 sp_pen_context_set_polyline_mode(SP_PEN_CONTEXT(dc));
398 }
399 }
400 }
402 /*
403 * Selection handlers
404 */
406 static void
407 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
408 {
409 if (dc->attach) {
410 spdc_attach_selection(dc, sel);
411 }
412 }
414 /* fixme: We have to ensure this is not delayed (Lauris) */
416 static void
417 spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
418 {
419 if (dc->attach) {
420 spdc_attach_selection(dc, sel);
421 }
422 }
424 static void
425 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
426 {
427 /* We reset white and forget white/start/end anchors */
428 spdc_reset_white(dc);
429 dc->sa = NULL;
430 dc->ea = NULL;
432 SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
434 if ( item && SP_IS_PATH(item) ) {
435 /* Create new white data */
436 /* Item */
437 dc->white_item = item;
438 /* Curve list */
439 /* We keep it in desktop coordinates to eliminate calculation errors */
440 SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
441 norm->transform(sp_item_i2d_affine(dc->white_item));
442 g_return_if_fail( norm != NULL );
443 dc->white_curves = g_slist_reverse(norm->split());
444 norm->unref();
445 /* Anchor list */
446 for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
447 SPCurve *c;
448 c = (SPCurve*)l->data;
449 g_return_if_fail( c->get_segment_count() > 0 );
450 if ( !c->is_closed() ) {
451 SPDrawAnchor *a;
452 a = sp_draw_anchor_new(dc, c, TRUE, *(c->first_point()));
453 if (a)
454 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
455 a = sp_draw_anchor_new(dc, c, FALSE, *(c->last_point()));
456 if (a)
457 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
458 }
459 }
460 /* fixme: recalculate active anchor? */
461 }
462 }
465 /**
466 * Snaps node or handle to PI/rotationsnapsperpi degree increments.
467 *
468 * \param dc draw context
469 * \param p cursor point (to be changed by snapping)
470 * \param o origin point
471 * \param state keyboard state to check if ctrl was pressed
472 */
474 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p, Geom::Point const &o,
475 guint state)
476 {
477 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
478 unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
479 /* 0 means no snapping. */
481 /* mirrored by fabs, so this corresponds to 15 degrees */
482 Geom::Point best; /* best solution */
483 double bn = NR_HUGE; /* best normal */
484 double bdot = 0;
485 Geom::Point v = Geom::Point(0, 1);
486 double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
487 double const r10 = -r01, r11 = r00;
489 Geom::Point delta = p - o;
491 for (unsigned i = 0; i < snaps; i++) {
492 double const ndot = fabs(dot(v,Geom::rot90(delta)));
493 Geom::Point t(r00*v[Geom::X] + r01*v[Geom::Y],
494 r10*v[Geom::X] + r11*v[Geom::Y]);
495 if (ndot < bn) {
496 /* I think it is better numerically to use the normal, rather than the dot product
497 * to assess solutions, but I haven't proven it. */
498 bn = ndot;
499 best = v;
500 bdot = dot(v, delta);
501 }
502 v = t;
503 }
505 if (fabs(bdot) > 0) {
506 p = o + bdot * best;
508 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
509 //After all, the user explicitly asked for angular snapping by
510 //pressing CTRL
511 /* Snap it along best vector */
512 SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
513 m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
514 Geom::Point pt2g = to_2geom(p);
515 m.constrainedSnapReturnByRef( Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE, Inkscape::Snapper::ConstraintLine(best), false);
516 p = from_2geom(pt2g);
517 }
518 }
519 }
522 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
523 {
524 SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec);
525 SnapManager &m = dt->namedview->snap_manager;
526 Inkscape::Selection *selection = sp_desktop_selection (dt);
528 // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
529 // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
531 m.setup(dt, true, selection->singleItem());
532 Geom::Point pt2g = to_2geom(p);
533 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
534 p = from_2geom(pt2g);
535 }
537 static SPCurve *
538 reverse_then_unref(SPCurve *orig)
539 {
540 SPCurve *ret = orig->create_reverse();
541 orig->unref();
542 return ret;
543 }
545 /**
546 * Concats red, blue and green.
547 * If any anchors are defined, process these, optionally removing curves from white list
548 * Invoke _flush_white to write result back to object.
549 */
550 void
551 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
552 {
553 /* Concat RBG */
554 SPCurve *c = dc->green_curve;
556 /* Green */
557 dc->green_curve = new SPCurve();
558 while (dc->green_bpaths) {
559 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
560 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
561 }
562 /* Blue */
563 c->append_continuous(dc->blue_curve, 0.0625);
564 dc->blue_curve->reset();
565 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
566 /* Red */
567 if (dc->red_curve_is_valid) {
568 c->append_continuous(dc->red_curve, 0.0625);
569 }
570 dc->red_curve->reset();
571 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
573 if (c->is_empty()) {
574 c->unref();
575 return;
576 }
578 /* Step A - test, whether we ended on green anchor */
579 if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
580 // We hit green anchor, closing Green-Blue-Red
581 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
582 c->closepath_current();
583 /* Closed path, just flush */
584 spdc_flush_white(dc, c);
585 c->unref();
586 return;
587 }
589 /* Step B - both start and end anchored to same curve */
590 if ( dc->sa && dc->ea
591 && ( dc->sa->curve == dc->ea->curve )
592 && ( ( dc->sa != dc->ea )
593 || dc->sa->curve->is_closed() ) )
594 {
595 // We hit bot start and end of single curve, closing paths
596 SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
597 if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
598 c = reverse_then_unref(c);
599 }
600 dc->sa->curve->append_continuous(c, 0.0625);
601 c->unref();
602 dc->sa->curve->closepath_current();
603 spdc_flush_white(dc, NULL);
604 return;
605 }
607 /* Step C - test start */
608 if (dc->sa) {
609 SPCurve *s = dc->sa->curve;
610 dc->white_curves = g_slist_remove(dc->white_curves, s);
611 if (dc->sa->start) {
612 s = reverse_then_unref(s);
613 }
614 s->append_continuous(c, 0.0625);
615 c->unref();
616 c = s;
617 } else /* Step D - test end */ if (dc->ea) {
618 SPCurve *e = dc->ea->curve;
619 dc->white_curves = g_slist_remove(dc->white_curves, e);
620 if (!dc->ea->start) {
621 e = reverse_then_unref(e);
622 }
623 c->append_continuous(e, 0.0625);
624 e->unref();
625 }
628 spdc_flush_white(dc, c);
630 c->unref();
631 }
633 /*
634 * Flushes white curve(s) and additional curve into object
635 *
636 * No cleaning of colored curves - this has to be done by caller
637 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
638 *
639 */
641 static void
642 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
643 {
644 SPCurve *c;
646 if (dc->white_curves) {
647 g_assert(dc->white_item);
648 c = SPCurve::concat(dc->white_curves);
649 g_slist_free(dc->white_curves);
650 dc->white_curves = NULL;
651 if (gc) {
652 c->append(gc, FALSE);
653 }
654 } else if (gc) {
655 c = gc;
656 c->ref();
657 } else {
658 return;
659 }
661 /* Now we have to go back to item coordinates at last */
662 c->transform( dc->white_item
663 ? sp_item_dt2i_affine(dc->white_item)
664 : SP_EVENT_CONTEXT_DESKTOP(dc)->dt2doc() );
666 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
667 SPDocument *doc = sp_desktop_document(desktop);
668 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
670 if ( c && !c->is_empty() ) {
671 /* We actually have something to write */
673 bool has_lpe = false;
674 Inkscape::XML::Node *repr;
675 if (dc->white_item) {
676 repr = SP_OBJECT_REPR(dc->white_item);
677 has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
678 } else {
679 repr = xml_doc->createElement("svg:path");
680 /* Set style */
681 sp_desktop_apply_style_tool(desktop, repr, tool_name(dc).data(), false);
682 }
684 gchar *str = sp_svg_write_path( c->get_pathvector() );
685 g_assert( str != NULL );
686 if (has_lpe)
687 repr->setAttribute("inkscape:original-d", str);
688 else
689 repr->setAttribute("d", str);
690 g_free(str);
692 if (!dc->white_item) {
693 /* Attach repr */
694 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
696 // we finished the path; now apply any waiting LPEs or freehand shapes
697 spdc_check_for_and_apply_waiting_LPE(dc, item);
699 dc->selection->set(repr);
700 Inkscape::GC::release(repr);
701 item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
702 item->updateRepr();
703 }
705 sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
706 _("Draw path"));
708 // When quickly drawing several subpaths with Shift, the next subpath may be finished and
709 // flushed before the selection_modified signal is fired by the previous change, which
710 // results in the tool losing all of the selected path's curve except that last subpath. To
711 // fix this, we force the selection_modified callback now, to make sure the tool's curve is
712 // in sync immediately.
713 spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
714 }
716 c->unref();
718 /* Flush pending updates */
719 sp_document_ensure_up_to_date(doc);
720 }
722 /**
723 * Returns FIRST active anchor (the activated one).
724 */
725 SPDrawAnchor *
726 spdc_test_inside(SPDrawContext *dc, Geom::Point p)
727 {
728 SPDrawAnchor *active = NULL;
730 /* Test green anchor */
731 if (dc->green_anchor) {
732 active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
733 }
735 for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
736 SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
737 if ( !active && na ) {
738 active = na;
739 }
740 }
742 return active;
743 }
745 static void
746 spdc_reset_white(SPDrawContext *dc)
747 {
748 if (dc->white_item) {
749 /* We do not hold refcount */
750 dc->white_item = NULL;
751 }
752 while (dc->white_curves) {
753 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
754 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
755 }
756 while (dc->white_anchors) {
757 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
758 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
759 }
760 }
762 static void
763 spdc_free_colors(SPDrawContext *dc)
764 {
765 /* Red */
766 if (dc->red_bpath) {
767 gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
768 dc->red_bpath = NULL;
769 }
770 if (dc->red_curve) {
771 dc->red_curve = dc->red_curve->unref();
772 }
773 /* Blue */
774 if (dc->blue_bpath) {
775 gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
776 dc->blue_bpath = NULL;
777 }
778 if (dc->blue_curve) {
779 dc->blue_curve = dc->blue_curve->unref();
780 }
781 /* Green */
782 while (dc->green_bpaths) {
783 gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
784 dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
785 }
786 if (dc->green_curve) {
787 dc->green_curve = dc->green_curve->unref();
788 }
789 if (dc->green_anchor) {
790 dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
791 }
792 /* White */
793 if (dc->white_item) {
794 /* We do not hold refcount */
795 dc->white_item = NULL;
796 }
797 while (dc->white_curves) {
798 reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
799 dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
800 }
801 while (dc->white_anchors) {
802 sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
803 dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
804 }
805 }
807 /* Create a single dot represented by a circle */
808 void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) {
809 g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil"));
810 Glib::ustring tool_path = tool;
812 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
813 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
814 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
815 repr->setAttribute("sodipodi:type", "arc");
816 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
817 Inkscape::GC::release(repr);
819 /* apply the tool's current style */
820 sp_desktop_apply_style_tool(desktop, repr, tool, false);
822 /* find out stroke width (TODO: is there an easier way??) */
823 double stroke_width = 3.0;
824 gchar const *style_str = NULL;
825 style_str = repr->attribute("style");
826 if (style_str) {
827 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
828 sp_style_merge_from_style_string(style, style_str);
829 stroke_width = style->stroke_width.computed;
830 style->stroke_width.computed = 0;
831 sp_style_unref(style);
832 }
833 /* unset stroke and set fill color to former stroke color */
834 gchar * str;
835 str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
836 repr->setAttribute("style", str);
837 g_free(str);
839 /* put the circle where the mouse click occurred and set the diameter to the
840 current stroke width, multiplied by the amount specified in the preferences */
841 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
843 Geom::Matrix const i2d (sp_item_i2d_affine (item));
844 Geom::Point pp = pt * i2d;
845 double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
846 if (event_state & GDK_MOD1_MASK) {
847 /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
848 as specified in prefs. Very simple, but it might be sufficient in practice. If not,
849 we need to devise something more sophisticated. */
850 double s = g_random_double_range(-0.5, 0.5);
851 rad *= (1 + s);
852 }
853 if (event_state & GDK_SHIFT_MASK) {
854 // double the point size
855 rad *= 2;
856 }
858 sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]);
859 sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]);
860 sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
861 sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
862 item->updateRepr();
864 sp_desktop_selection(desktop)->set(item);
866 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
867 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
868 }
870 /*
871 Local Variables:
872 mode:c++
873 c-file-style:"stroustrup"
874 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
875 indent-tabs-mode:nil
876 fill-column:99
877 End:
878 */
879 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :