Code

Extensions. XAML export improvements.
[inkscape.git] / src / draw-context.cpp
1 /*
2  * Generic drawing context
3  *
4  * Author:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Abhishek Sharma
7  *
8  * Copyright (C) 2000 Lauris Kaplinski
9  * Copyright (C) 2000-2001 Ximian, Inc.
10  * Copyright (C) 2002 Lauris Kaplinski
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #define DRAW_VERBOSE
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20 #include <gdk/gdkkeysyms.h>
22 #include "display/canvas-bpath.h"
23 #include "xml/repr.h"
24 #include "svg/svg.h"
25 #include <glibmm/i18n.h>
26 #include "display/curve.h"
27 #include "desktop.h"
28 #include "desktop-handles.h"
29 #include "desktop-style.h"
30 #include "document.h"
31 #include "draw-anchor.h"
32 #include "macros.h"
33 #include "message-stack.h"
34 #include "pen-context.h"
35 #include "lpe-tool-context.h"
36 #include "preferences.h"
37 #include "selection.h"
38 #include "selection-chemistry.h"
39 #include "snap.h"
40 #include "sp-path.h"
41 #include "sp-namedview.h"
42 #include "live_effects/lpe-patternalongpath.h"
43 #include "style.h"
45 using Inkscape::DocumentUndo;
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(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);
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 10
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(PATTERN_ALONG_PATH, 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     }
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((dc->white_item)->i2d_affine());
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 or shift 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     SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
481     m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
483     bool snap_enabled = m.snapprefs.getSnapEnabledGlobally();
484     if (state & GDK_SHIFT_MASK) {
485         // SHIFT disables all snapping, except the angular snapping. After all, the user explicitly asked for angular
486         // snapping by pressing CTRL, otherwise we wouldn't have arrived here. But although we temporarily disable
487         // the snapping here, we must still call for a constrained snap in order to apply the constraints (i.e. round
488         // to the nearest angle increment)
489         m.snapprefs.setSnapEnabledGlobally(false);
490     }
492     Inkscape::SnappedPoint dummy = m.constrainedAngularSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE), boost::optional<Geom::Point>(), o, snaps);
493     p = dummy.getPoint();
495     if (state & GDK_SHIFT_MASK) {
496         m.snapprefs.setSnapEnabledGlobally(snap_enabled); // restore the original setting
497     }
499     m.unSetup();
503 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
505     SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec);
506     SnapManager &m = dt->namedview->snap_manager;
507     Inkscape::Selection *selection = sp_desktop_selection (dt);
509     // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
510     // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
512     m.setup(dt, true, selection->singleItem());
513     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
514     m.unSetup();
517 static SPCurve *
518 reverse_then_unref(SPCurve *orig)
520     SPCurve *ret = orig->create_reverse();
521     orig->unref();
522     return ret;
525 /**
526  * Concats red, blue and green.
527  * If any anchors are defined, process these, optionally removing curves from white list
528  * Invoke _flush_white to write result back to object.
529  */
530 void
531 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
533     /* Concat RBG */
534     SPCurve *c = dc->green_curve;
536     /* Green */
537     dc->green_curve = new SPCurve();
538     while (dc->green_bpaths) {
539         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
540         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
541     }
542     /* Blue */
543     c->append_continuous(dc->blue_curve, 0.0625);
544     dc->blue_curve->reset();
545     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
546     /* Red */
547     if (dc->red_curve_is_valid) {
548         c->append_continuous(dc->red_curve, 0.0625);
549     }
550     dc->red_curve->reset();
551     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
553     if (c->is_empty()) {
554         c->unref();
555         return;
556     }
558     /* Step A - test, whether we ended on green anchor */
559     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
560         // We hit green anchor, closing Green-Blue-Red
561         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
562         c->closepath_current();
563         /* Closed path, just flush */
564         spdc_flush_white(dc, c);
565         c->unref();
566         return;
567     }
569     /* Step B - both start and end anchored to same curve */
570     if ( dc->sa && dc->ea
571          && ( dc->sa->curve == dc->ea->curve )
572          && ( ( dc->sa != dc->ea )
573               || dc->sa->curve->is_closed() ) )
574     {
575         // We hit bot start and end of single curve, closing paths
576         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
577         if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
578             c = reverse_then_unref(c);
579         }
580         dc->sa->curve->append_continuous(c, 0.0625);
581         c->unref();
582         dc->sa->curve->closepath_current();
583         spdc_flush_white(dc, NULL);
584         return;
585     }
587     /* Step C - test start */
588     if (dc->sa) {
589         SPCurve *s = dc->sa->curve;
590         dc->white_curves = g_slist_remove(dc->white_curves, s);
591         if (dc->sa->start) {
592             s = reverse_then_unref(s);
593         }
594         s->append_continuous(c, 0.0625);
595         c->unref();
596         c = s;
597     } else /* Step D - test end */ if (dc->ea) {
598         SPCurve *e = dc->ea->curve;
599         dc->white_curves = g_slist_remove(dc->white_curves, e);
600         if (!dc->ea->start) {
601             e = reverse_then_unref(e);
602         }
603         c->append_continuous(e, 0.0625);
604         e->unref();
605     }
608     spdc_flush_white(dc, c);
610     c->unref();
613 /*
614  * Flushes white curve(s) and additional curve into object
615  *
616  * No cleaning of colored curves - this has to be done by caller
617  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
618  *
619  */
621 static void
622 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
624     SPCurve *c;
626     if (dc->white_curves) {
627         g_assert(dc->white_item);
628         c = SPCurve::concat(dc->white_curves);
629         g_slist_free(dc->white_curves);
630         dc->white_curves = NULL;
631         if (gc) {
632             c->append(gc, FALSE);
633         }
634     } else if (gc) {
635         c = gc;
636         c->ref();
637     } else {
638         return;
639     }
641     /* Now we have to go back to item coordinates at last */
642     c->transform( dc->white_item
643                             ? (dc->white_item)->dt2i_affine()
644                             : SP_EVENT_CONTEXT_DESKTOP(dc)->dt2doc() );
646     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
647     SPDocument *doc = sp_desktop_document(desktop);
648     Inkscape::XML::Document *xml_doc = doc->getReprDoc();
650     if ( c && !c->is_empty() ) {
651         /* We actually have something to write */
653         bool has_lpe = false;
654         Inkscape::XML::Node *repr;
655         if (dc->white_item) {
656             repr = SP_OBJECT_REPR(dc->white_item);
657             has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
658         } else {
659             repr = xml_doc->createElement("svg:path");
660             /* Set style */
661             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc).data(), false);
662         }
664         gchar *str = sp_svg_write_path( c->get_pathvector() );
665         g_assert( str != NULL );
666         if (has_lpe)
667             repr->setAttribute("inkscape:original-d", str);
668         else
669             repr->setAttribute("d", str);
670         g_free(str);
672         if (!dc->white_item) {
673             /* Attach repr */
674             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
676             // we finished the path; now apply any waiting LPEs or freehand shapes
677             spdc_check_for_and_apply_waiting_LPE(dc, item);
679             dc->selection->set(repr);
680             Inkscape::GC::release(repr);
681             item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
682             item->updateRepr();
683         }
685         DocumentUndo::done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
686                          _("Draw path"));
688         // When quickly drawing several subpaths with Shift, the next subpath may be finished and
689         // flushed before the selection_modified signal is fired by the previous change, which
690         // results in the tool losing all of the selected path's curve except that last subpath. To
691         // fix this, we force the selection_modified callback now, to make sure the tool's curve is
692         // in sync immediately.
693         spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
694     }
696     c->unref();
698     /* Flush pending updates */
699     doc->ensureUpToDate();
702 /**
703  * Returns FIRST active anchor (the activated one).
704  */
705 SPDrawAnchor *
706 spdc_test_inside(SPDrawContext *dc, Geom::Point p)
708     SPDrawAnchor *active = NULL;
710     /* Test green anchor */
711     if (dc->green_anchor) {
712         active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
713     }
715     for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
716         SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
717         if ( !active && na ) {
718             active = na;
719         }
720     }
722     return active;
725 static void
726 spdc_reset_white(SPDrawContext *dc)
728     if (dc->white_item) {
729         /* We do not hold refcount */
730         dc->white_item = NULL;
731     }
732     while (dc->white_curves) {
733         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
734         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
735     }
736     while (dc->white_anchors) {
737         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
738         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
739     }
742 static void
743 spdc_free_colors(SPDrawContext *dc)
745     /* Red */
746     if (dc->red_bpath) {
747         gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
748         dc->red_bpath = NULL;
749     }
750     if (dc->red_curve) {
751         dc->red_curve = dc->red_curve->unref();
752     }
753     /* Blue */
754     if (dc->blue_bpath) {
755         gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
756         dc->blue_bpath = NULL;
757     }
758     if (dc->blue_curve) {
759         dc->blue_curve = dc->blue_curve->unref();
760     }
761     /* Green */
762     while (dc->green_bpaths) {
763         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
764         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
765     }
766     if (dc->green_curve) {
767         dc->green_curve = dc->green_curve->unref();
768     }
769     if (dc->green_anchor) {
770         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
771     }
772     /* White */
773     if (dc->white_item) {
774         /* We do not hold refcount */
775         dc->white_item = NULL;
776     }
777     while (dc->white_curves) {
778         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
779         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
780     }
781     while (dc->white_anchors) {
782         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
783         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
784     }
787 /* Create a single dot represented by a circle */
788 void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) {
789     g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil"));
790     Glib::ustring tool_path = tool;
792     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
793     Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
794     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
795     repr->setAttribute("sodipodi:type", "arc");
796     SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
797     Inkscape::GC::release(repr);
799     /* apply the tool's current style */
800     sp_desktop_apply_style_tool(desktop, repr, tool, false);
802     /* find out stroke width (TODO: is there an easier way??) */
803     double stroke_width = 3.0;
804     gchar const *style_str = NULL;
805     style_str = repr->attribute("style");
806     if (style_str) {
807         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
808         sp_style_merge_from_style_string(style, style_str);
809         stroke_width = style->stroke_width.computed;
810         style->stroke_width.computed = 0;
811         sp_style_unref(style);
812     }
813     /* unset stroke and set fill color to former stroke color */
814     gchar * str;
815     str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
816     repr->setAttribute("style", str);
817     g_free(str);
819     /* put the circle where the mouse click occurred and set the diameter to the
820        current stroke width, multiplied by the amount specified in the preferences */
821     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
823     Geom::Matrix const i2d (item->i2d_affine ());
824     Geom::Point pp = pt;
825     double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
826     if (event_state & GDK_MOD1_MASK) {
827         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
828            as specified in prefs. Very simple, but it might be sufficient in practice. If not,
829            we need to devise something more sophisticated. */
830         double s = g_random_double_range(-0.5, 0.5);
831         rad *= (1 + s);
832     }
833     if (event_state & GDK_SHIFT_MASK) {
834         // double the point size
835         rad *= 2;
836     }
838     sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]);
839     sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]);
840     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
841     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
842     item->updateRepr();
843     item->set_item_transform(i2d.inverse());
845     sp_desktop_selection(desktop)->set(item);
847     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
848     DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
851 /*
852   Local Variables:
853   mode:c++
854   c-file-style:"stroustrup"
855   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
856   indent-tabs-mode:nil
857   fill-column:99
858   End:
859 */
860 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :