Code

ca68b3f6df0fe66aa0b9beed444c584ee6a8c752
[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-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"
45 #include "util/mathfns.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(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(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 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));
481     if (snaps > 0) { // 0 means no snapping
482         // p is at an arbitrary angle. Now we should snap this angle to specific increments.
483         // For this we'll calculate the closest two angles, one at each side of the current angle
484         Geom::Line y_axis(Geom::Point(0, 0), Geom::Point(0, 1));
485         Geom::Line p_line(o, p);
486         double angle = Geom::angle_between(y_axis, p_line);
487         double angle_incr = M_PI / snaps;
488         double angle_ceil = round_to_upper_multiple_plus(angle, angle_incr);
489         double angle_floor = round_to_lower_multiple_plus(angle, angle_incr);
490         // We have to angles now. The constrained snapper will try each of them and return the closest
491         // But first we should setup the snapper
493         SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
494         m.setup(SP_EVENT_CONTEXT_DESKTOP(ec));
495         bool snap_enabled = m.snapprefs.getSnapEnabledGlobally();
496         if (state & GDK_SHIFT_MASK) {
497             // SHIFT disables all snapping, except the angular snapping. After all, the user explicitly asked for angular
498             // snapping by pressing CTRL, otherwise we wouldn't have arrived here. But although we temporarily disable
499             // the snapping here, we must still call for a constrained snap in order to apply the constraints (i.e. round
500             // to the nearest angle increment)
501             m.snapprefs.setSnapEnabledGlobally(false);
502         }
504         // Now do the snapping...
505         std::vector<Inkscape::Snapper::SnapConstraint> constraints;
506         constraints.push_back(Inkscape::Snapper::SnapConstraint(Geom::Line(o, angle_ceil - M_PI/2)));
507         constraints.push_back(Inkscape::Snapper::SnapConstraint(Geom::Line(o, angle_floor - M_PI/2)));
509         Inkscape::SnappedPoint sp = m.multipleConstrainedSnaps(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE), constraints);
510         p = sp.getPoint();
512         m.unSetup();
513         if (state & GDK_SHIFT_MASK) {
514             m.snapprefs.setSnapEnabledGlobally(snap_enabled); // restore the original setting
515         }
516     }
520 void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/)
522     SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec);
523     SnapManager &m = dt->namedview->snap_manager;
524     Inkscape::Selection *selection = sp_desktop_selection (dt);
526     // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping)
527     // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment
529     m.setup(dt, true, selection->singleItem());
530     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
531     m.unSetup();
534 static SPCurve *
535 reverse_then_unref(SPCurve *orig)
537     SPCurve *ret = orig->create_reverse();
538     orig->unref();
539     return ret;
542 /**
543  * Concats red, blue and green.
544  * If any anchors are defined, process these, optionally removing curves from white list
545  * Invoke _flush_white to write result back to object.
546  */
547 void
548 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
550     /* Concat RBG */
551     SPCurve *c = dc->green_curve;
553     /* Green */
554     dc->green_curve = new SPCurve();
555     while (dc->green_bpaths) {
556         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
557         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
558     }
559     /* Blue */
560     c->append_continuous(dc->blue_curve, 0.0625);
561     dc->blue_curve->reset();
562     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
563     /* Red */
564     if (dc->red_curve_is_valid) {
565         c->append_continuous(dc->red_curve, 0.0625);
566     }
567     dc->red_curve->reset();
568     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
570     if (c->is_empty()) {
571         c->unref();
572         return;
573     }
575     /* Step A - test, whether we ended on green anchor */
576     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
577         // We hit green anchor, closing Green-Blue-Red
578         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
579         c->closepath_current();
580         /* Closed path, just flush */
581         spdc_flush_white(dc, c);
582         c->unref();
583         return;
584     }
586     /* Step B - both start and end anchored to same curve */
587     if ( dc->sa && dc->ea
588          && ( dc->sa->curve == dc->ea->curve )
589          && ( ( dc->sa != dc->ea )
590               || dc->sa->curve->is_closed() ) )
591     {
592         // We hit bot start and end of single curve, closing paths
593         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
594         if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
595             c = reverse_then_unref(c);
596         }
597         dc->sa->curve->append_continuous(c, 0.0625);
598         c->unref();
599         dc->sa->curve->closepath_current();
600         spdc_flush_white(dc, NULL);
601         return;
602     }
604     /* Step C - test start */
605     if (dc->sa) {
606         SPCurve *s = dc->sa->curve;
607         dc->white_curves = g_slist_remove(dc->white_curves, s);
608         if (dc->sa->start) {
609             s = reverse_then_unref(s);
610         }
611         s->append_continuous(c, 0.0625);
612         c->unref();
613         c = s;
614     } else /* Step D - test end */ if (dc->ea) {
615         SPCurve *e = dc->ea->curve;
616         dc->white_curves = g_slist_remove(dc->white_curves, e);
617         if (!dc->ea->start) {
618             e = reverse_then_unref(e);
619         }
620         c->append_continuous(e, 0.0625);
621         e->unref();
622     }
625     spdc_flush_white(dc, c);
627     c->unref();
630 /*
631  * Flushes white curve(s) and additional curve into object
632  *
633  * No cleaning of colored curves - this has to be done by caller
634  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
635  *
636  */
638 static void
639 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
641     SPCurve *c;
643     if (dc->white_curves) {
644         g_assert(dc->white_item);
645         c = SPCurve::concat(dc->white_curves);
646         g_slist_free(dc->white_curves);
647         dc->white_curves = NULL;
648         if (gc) {
649             c->append(gc, FALSE);
650         }
651     } else if (gc) {
652         c = gc;
653         c->ref();
654     } else {
655         return;
656     }
658     /* Now we have to go back to item coordinates at last */
659     c->transform( dc->white_item
660                             ? sp_item_dt2i_affine(dc->white_item)
661                             : SP_EVENT_CONTEXT_DESKTOP(dc)->dt2doc() );
663     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
664     SPDocument *doc = sp_desktop_document(desktop);
665     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
667     if ( c && !c->is_empty() ) {
668         /* We actually have something to write */
670         bool has_lpe = false;
671         Inkscape::XML::Node *repr;
672         if (dc->white_item) {
673             repr = SP_OBJECT_REPR(dc->white_item);
674             has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
675         } else {
676             repr = xml_doc->createElement("svg:path");
677             /* Set style */
678             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc).data(), false);
679         }
681         gchar *str = sp_svg_write_path( c->get_pathvector() );
682         g_assert( str != NULL );
683         if (has_lpe)
684             repr->setAttribute("inkscape:original-d", str);
685         else
686             repr->setAttribute("d", str);
687         g_free(str);
689         if (!dc->white_item) {
690             /* Attach repr */
691             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
693             // we finished the path; now apply any waiting LPEs or freehand shapes
694             spdc_check_for_and_apply_waiting_LPE(dc, item);
696             dc->selection->set(repr);
697             Inkscape::GC::release(repr);
698             item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
699             item->updateRepr();
700         }
702         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL,
703                          _("Draw path"));
705         // When quickly drawing several subpaths with Shift, the next subpath may be finished and
706         // flushed before the selection_modified signal is fired by the previous change, which
707         // results in the tool losing all of the selected path's curve except that last subpath. To
708         // fix this, we force the selection_modified callback now, to make sure the tool's curve is
709         // in sync immediately.
710         spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
711     }
713     c->unref();
715     /* Flush pending updates */
716     sp_document_ensure_up_to_date(doc);
719 /**
720  * Returns FIRST active anchor (the activated one).
721  */
722 SPDrawAnchor *
723 spdc_test_inside(SPDrawContext *dc, Geom::Point p)
725     SPDrawAnchor *active = NULL;
727     /* Test green anchor */
728     if (dc->green_anchor) {
729         active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
730     }
732     for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
733         SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
734         if ( !active && na ) {
735             active = na;
736         }
737     }
739     return active;
742 static void
743 spdc_reset_white(SPDrawContext *dc)
745     if (dc->white_item) {
746         /* We do not hold refcount */
747         dc->white_item = NULL;
748     }
749     while (dc->white_curves) {
750         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
751         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
752     }
753     while (dc->white_anchors) {
754         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
755         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
756     }
759 static void
760 spdc_free_colors(SPDrawContext *dc)
762     /* Red */
763     if (dc->red_bpath) {
764         gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
765         dc->red_bpath = NULL;
766     }
767     if (dc->red_curve) {
768         dc->red_curve = dc->red_curve->unref();
769     }
770     /* Blue */
771     if (dc->blue_bpath) {
772         gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
773         dc->blue_bpath = NULL;
774     }
775     if (dc->blue_curve) {
776         dc->blue_curve = dc->blue_curve->unref();
777     }
778     /* Green */
779     while (dc->green_bpaths) {
780         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
781         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
782     }
783     if (dc->green_curve) {
784         dc->green_curve = dc->green_curve->unref();
785     }
786     if (dc->green_anchor) {
787         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
788     }
789     /* White */
790     if (dc->white_item) {
791         /* We do not hold refcount */
792         dc->white_item = NULL;
793     }
794     while (dc->white_curves) {
795         reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
796         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
797     }
798     while (dc->white_anchors) {
799         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
800         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
801     }
804 /* Create a single dot represented by a circle */
805 void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) {
806     g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil"));
807     Glib::ustring tool_path = tool;
809     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
810     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
811     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
812     repr->setAttribute("sodipodi:type", "arc");
813     SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
814     Inkscape::GC::release(repr);
816     /* apply the tool's current style */
817     sp_desktop_apply_style_tool(desktop, repr, tool, false);
819     /* find out stroke width (TODO: is there an easier way??) */
820     double stroke_width = 3.0;
821     gchar const *style_str = NULL;
822     style_str = repr->attribute("style");
823     if (style_str) {
824         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
825         sp_style_merge_from_style_string(style, style_str);
826         stroke_width = style->stroke_width.computed;
827         style->stroke_width.computed = 0;
828         sp_style_unref(style);
829     }
830     /* unset stroke and set fill color to former stroke color */
831     gchar * str;
832     str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
833     repr->setAttribute("style", str);
834     g_free(str);
836     /* put the circle where the mouse click occurred and set the diameter to the
837        current stroke width, multiplied by the amount specified in the preferences */
838     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
840     Geom::Matrix const i2d (sp_item_i2d_affine (item));
841     Geom::Point pp = pt * i2d.inverse();
842     double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0);
843     if (event_state & GDK_MOD1_MASK) {
844         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
845            as specified in prefs. Very simple, but it might be sufficient in practice. If not,
846            we need to devise something more sophisticated. */
847         double s = g_random_double_range(-0.5, 0.5);
848         rad *= (1 + s);
849     }
850     if (event_state & GDK_SHIFT_MASK) {
851         // double the point size
852         rad *= 2;
853     }
855     sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]);
856     sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]);
857     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
858     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
859     item->updateRepr();
861     sp_desktop_selection(desktop)->set(item);
863     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
864     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
867 /*
868   Local Variables:
869   mode:c++
870   c-file-style:"stroustrup"
871   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
872   indent-tabs-mode:nil
873   fill-column:99
874   End:
875 */
876 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :