Code

fix parameter ranges, copyedit
[inkscape.git] / src / draw-context.cpp
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;
110 static void
111 sp_draw_context_init(SPDrawContext *dc)
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();
144 static void
145 sp_draw_context_dispose(GObject *object)
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);
166 static void
167 sp_draw_context_setup(SPEventContext *ec)
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);
208 static void
209 sp_draw_context_finish(SPEventContext *ec)
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);
227 static void
228 sp_draw_context_set(SPEventContext */*ec*/, Inkscape::Preferences::Entry */*val*/)
232 gint
233 sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
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;
266 static Glib::ustring const
267 tool_name(SPDrawContext *dc)
269     return ( SP_IS_PEN_CONTEXT(dc)
270              ? "/tools/freehand/pen"
271              : "/tools/freehand/pencil" );
274 static void
275 spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
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);
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)
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), Inkscape::LivePathEffect::INVALID_LPE);
395             }
396         }
397         if (SP_IS_PEN_CONTEXT(dc)) {
398             sp_pen_context_set_polyline_mode(SP_PEN_CONTEXT(dc));
399         }
400     }
403 /*
404  * Selection handlers
405  */
407 static void
408 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
410     if (dc->attach) {
411         spdc_attach_selection(dc, sel);
412     }
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)
420     if (dc->attach) {
421         spdc_attach_selection(dc, sel);
422     }
425 static void
426 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
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     }
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)
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 explicitely 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::Snapper::ConstraintLine(best));
517             p = from_2geom(pt2g);
518         }
519     }
523 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
525     SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
526     m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
527     Geom::Point pt2g = to_2geom(p);
528     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
529     p = from_2geom(pt2g);
532 static SPCurve *
533 reverse_then_unref(SPCurve *orig)
535     SPCurve *ret = orig->create_reverse();
536     orig->unref();
537     return ret;
540 /**
541  * Concats red, blue and green.
542  * If any anchors are defined, process these, optionally removing curves from white list
543  * Invoke _flush_white to write result back to object.
544  */
545 void
546 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
548     /* Concat RBG */
549     SPCurve *c = dc->green_curve;
551     /* Green */
552     dc->green_curve = new SPCurve();
553     while (dc->green_bpaths) {
554         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
555         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
556     }
557     /* Blue */
558     c->append_continuous(dc->blue_curve, 0.0625);
559     dc->blue_curve->reset();
560     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
561     /* Red */
562     if (dc->red_curve_is_valid) {
563         c->append_continuous(dc->red_curve, 0.0625);
564     }
565     dc->red_curve->reset();
566     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
568     if (c->is_empty()) {
569         c->unref();
570         return;
571     }
573     /* Step A - test, whether we ended on green anchor */
574     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
575         // We hit green anchor, closing Green-Blue-Red
576         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
577         c->closepath_current();
578         /* Closed path, just flush */
579         spdc_flush_white(dc, c);
580         c->unref();
581         return;
582     }
584     /* Step B - both start and end anchored to same curve */
585     if ( dc->sa && dc->ea
586          && ( dc->sa->curve == dc->ea->curve )
587          && ( ( dc->sa != dc->ea )
588               || dc->sa->curve->is_closed() ) )
589     {
590         // We hit bot start and end of single curve, closing paths
591         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
592         if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
593             c = reverse_then_unref(c);
594         }
595         dc->sa->curve->append_continuous(c, 0.0625);
596         c->unref();
597         dc->sa->curve->closepath_current();
598         spdc_flush_white(dc, NULL);
599         return;
600     }
602     /* Step C - test start */
603     if (dc->sa) {
604         SPCurve *s = dc->sa->curve;
605         dc->white_curves = g_slist_remove(dc->white_curves, s);
606         if (dc->sa->start) {
607             s = reverse_then_unref(s);
608         }
609         s->append_continuous(c, 0.0625);
610         c->unref();
611         c = s;
612     } else /* Step D - test end */ if (dc->ea) {
613         SPCurve *e = dc->ea->curve;
614         dc->white_curves = g_slist_remove(dc->white_curves, e);
615         if (!dc->ea->start) {
616             e = reverse_then_unref(e);
617         }
618         c->append_continuous(e, 0.0625);
619         e->unref();
620     }
623     spdc_flush_white(dc, c);
625     c->unref();
628 /*
629  * Flushes white curve(s) and additional curve into object
630  *
631  * No cleaning of colored curves - this has to be done by caller
632  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
633  *
634  */
636 static void
637 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
639     SPCurve *c;
641     if (dc->white_curves) {
642         g_assert(dc->white_item);
643         c = SPCurve::concat(dc->white_curves);
644         g_slist_free(dc->white_curves);
645         dc->white_curves = NULL;
646         if (gc) {
647             c->append(gc, FALSE);
648         }
649     } else if (gc) {
650         c = gc;
651         c->ref();
652     } else {
653         return;
654     }
656     /* Now we have to go back to item coordinates at last */
657     c->transform( dc->white_item
658                             ? sp_item_dt2i_affine(dc->white_item)
659                             : to_2geom(sp_desktop_dt2doc_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) );
661     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
662     SPDocument *doc = sp_desktop_document(desktop);
663     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
665     if ( c && !c->is_empty() ) {
666         /* We actually have something to write */
668         bool has_lpe = false;
669         Inkscape::XML::Node *repr;
670         if (dc->white_item) {
671             repr = SP_OBJECT_REPR(dc->white_item);
672             has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
673         } else {
674             repr = xml_doc->createElement("svg:path");
675             /* Set style */
676             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc).data(), false);
677         }
679         gchar *str = sp_svg_write_path( c->get_pathvector() );
680         g_assert( str != NULL );
681         if (has_lpe)
682             repr->setAttribute("inkscape:original-d", str);
683         else
684             repr->setAttribute("d", str);
685         g_free(str);
687         if (!dc->white_item) {
688             /* Attach repr */
689             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
691             // we finished the path; now apply any waiting LPEs or freehand shapes
692             spdc_check_for_and_apply_waiting_LPE(dc, item);
694             dc->selection->set(repr);
695             Inkscape::GC::release(repr);
696             item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
697             item->updateRepr();
698         }
700         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
701                          _("Draw path"));
703         // When quickly drawing several subpaths with Shift, the next subpath may be finished and
704         // flushed before the selection_modified signal is fired by the previous change, which
705         // results in the tool losing all of the selected path's curve except that last subpath. To
706         // fix this, we force the selection_modified callback now, to make sure the tool's curve is
707         // in sync immediately.
708         spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
709     }
711     c->unref();
713     /* Flush pending updates */
714     sp_document_ensure_up_to_date(doc);
717 /**
718  * Returns FIRST active anchor (the activated one).
719  */
720 SPDrawAnchor *
721 spdc_test_inside(SPDrawContext *dc, Geom::Point p)
723     SPDrawAnchor *active = NULL;
725     /* Test green anchor */
726     if (dc->green_anchor) {
727         active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
728     }
730     for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
731         SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
732         if ( !active && na ) {
733             active = na;
734         }
735     }
737     return active;
740 static void
741 spdc_reset_white(SPDrawContext *dc)
743     if (dc->white_item) {
744         /* We do not hold refcount */
745         dc->white_item = NULL;
746     }
747     while (dc->white_curves) {
748         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
749         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
750     }
751     while (dc->white_anchors) {
752         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
753         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
754     }
757 static void
758 spdc_free_colors(SPDrawContext *dc)
760     /* Red */
761     if (dc->red_bpath) {
762         gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
763         dc->red_bpath = NULL;
764     }
765     if (dc->red_curve) {
766         dc->red_curve = dc->red_curve->unref();
767     }
768     /* Blue */
769     if (dc->blue_bpath) {
770         gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
771         dc->blue_bpath = NULL;
772     }
773     if (dc->blue_curve) {
774         dc->blue_curve = dc->blue_curve->unref();
775     }
776     /* Green */
777     while (dc->green_bpaths) {
778         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
779         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
780     }
781     if (dc->green_curve) {
782         dc->green_curve = dc->green_curve->unref();
783     }
784     if (dc->green_anchor) {
785         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
786     }
787     /* White */
788     if (dc->white_item) {
789         /* We do not hold refcount */
790         dc->white_item = NULL;
791     }
792     while (dc->white_curves) {
793         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
794         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
795     }
796     while (dc->white_anchors) {
797         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
798         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
799     }
802 /* Create a single dot represented by a circle */
803 void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) {
804     g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil"));
805     Glib::ustring tool_path = tool;
807     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
808     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
809     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
810     repr->setAttribute("sodipodi:type", "arc");
811     SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
812     Inkscape::GC::release(repr);
814     /* apply the tool's current style */
815     sp_desktop_apply_style_tool(desktop, repr, tool, false);
817     /* find out stroke width (TODO: is there an easier way??) */
818     double stroke_width = 3.0;
819     gchar const *style_str = NULL;
820     style_str = repr->attribute("style");
821     if (style_str) {
822         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
823         sp_style_merge_from_style_string(style, style_str);
824         stroke_width = style->stroke_width.computed;
825         style->stroke_width.computed = 0;
826         sp_style_unref(style);
827     }
828     /* unset stroke and set fill color to former stroke color */
829     gchar * str;
830     str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
831     repr->setAttribute("style", str);
832     g_free(str);
834     /* put the circle where the mouse click occurred and set the diameter to the
835        current stroke width, multiplied by the amount specified in the preferences */
836     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
838     Geom::Matrix const i2d (sp_item_i2d_affine (item));
839     Geom::Point pp = pt * i2d;
840     double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
841     if (event_state & GDK_MOD1_MASK) {
842         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
843            as specified in prefs. Very simple, but it might be sufficient in practice. If not,
844            we need to devise something more sophisticated. */
845         double s = g_random_double_range(-0.5, 0.5);
846         rad *= (1 + s);
847     }
848     if (event_state & GDK_SHIFT_MASK) {
849         // double the point size
850         rad *= 2;
851     }
853     sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]);
854     sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]);
855     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
856     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
857     item->updateRepr();
859     sp_desktop_selection(desktop)->set(item);
861     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
862     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
865 /*
866   Local Variables:
867   mode:c++
868   c-file-style:"stroustrup"
869   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
870   indent-tabs-mode:nil
871   fill-column:99
872   End:
873 */
874 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :