Code

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