Code

preserve path effect
[inkscape.git] / src / tweak-context.cpp
1 #define __SP_TWEAK_CONTEXT_C__
3 /*
4  * tweaking paths without node editing
5  *
6  * Authors:
7  *   bulia byak 
8  *
9  * Copyright (C) 2007 authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "config.h"
16 #include <gtk/gtk.h>
17 #include <gdk/gdkkeysyms.h>
18 #include <glibmm/i18n.h>
20 #include <numeric>
22 #include "svg/svg.h"
23 #include "display/canvas-bpath.h"
24 #include "display/bezier-utils.h"
26 #include <glib/gmem.h>
27 #include "macros.h"
28 #include "document.h"
29 #include "selection.h"
30 #include "desktop.h"
31 #include "desktop-events.h"
32 #include "desktop-handles.h"
33 #include "desktop-affine.h"
34 #include "desktop-style.h"
35 #include "message-context.h"
36 #include "pixmaps/cursor-thin.xpm"
37 #include "pixmaps/cursor-thicken.xpm"
38 #include "pixmaps/cursor-attract.xpm"
39 #include "pixmaps/cursor-repel.xpm"
40 #include "pixmaps/cursor-push.xpm"
41 #include "pixmaps/cursor-roughen.xpm"
42 #include "pixmaps/cursor-color.xpm"
43 #include "libnr/n-art-bpath.h"
44 #include "libnr/nr-path.h"
45 #include "libnr/nr-maybe.h"
46 #include "libnr/nr-matrix-ops.h"
47 #include "libnr/nr-scale-translate-ops.h"
48 #include "xml/repr.h"
49 #include "context-fns.h"
50 #include "sp-item.h"
51 #include "inkscape.h"
52 #include "color.h"
53 #include "svg/svg-color.h"
54 #include "splivarot.h"
55 #include "sp-item-group.h"
56 #include "sp-shape.h"
57 #include "sp-path.h"
58 #include "path-chemistry.h"
59 #include "sp-gradient.h"
60 #include "sp-stop.h"
61 #include "sp-stop-fns.h"
62 #include "sp-gradient-reference.h"
63 #include "sp-linear-gradient.h"
64 #include "sp-radial-gradient.h"
65 #include "gradient-chemistry.h"
66 #include "sp-text.h"
67 #include "sp-flowtext.h"
68 #include "display/canvas-bpath.h"
69 #include "display/canvas-arena.h"
70 #include "livarot/Shape.h"
71 #include "isnan.h"
72 #include "prefs-utils.h"
73 #include "style.h"
75 #include "tweak-context.h"
77 #define DDC_RED_RGBA 0xff0000ff
79 #define DYNA_MIN_WIDTH 1.0e-6
81 // FIXME: move it to some shared file to be reused by both calligraphy and dropper
82 #define C1 0.552
83 static NArtBpath const hatch_area_circle[] = {
84     { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
85     { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
86     { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
87     { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
88     { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
89     { NR_END, 0, 0, 0, 0, 0, 0 }
90 };
91 #undef C1
94 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
95 static void sp_tweak_context_init(SPTweakContext *ddc);
96 static void sp_tweak_context_dispose(GObject *object);
98 static void sp_tweak_context_setup(SPEventContext *ec);
99 static void sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
100 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
102 static SPEventContextClass *parent_class;
104 GtkType
105 sp_tweak_context_get_type(void)
107     static GType type = 0;
108     if (!type) {
109         GTypeInfo info = {
110             sizeof(SPTweakContextClass),
111             NULL, NULL,
112             (GClassInitFunc) sp_tweak_context_class_init,
113             NULL, NULL,
114             sizeof(SPTweakContext),
115             4,
116             (GInstanceInitFunc) sp_tweak_context_init,
117             NULL,   /* value_table */
118         };
119         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
120     }
121     return type;
124 static void
125 sp_tweak_context_class_init(SPTweakContextClass *klass)
127     GObjectClass *object_class = (GObjectClass *) klass;
128     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
130     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
132     object_class->dispose = sp_tweak_context_dispose;
134     event_context_class->setup = sp_tweak_context_setup;
135     event_context_class->set = sp_tweak_context_set;
136     event_context_class->root_handler = sp_tweak_context_root_handler;
139 static void
140 sp_tweak_context_init(SPTweakContext *tc)
142     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
144     event_context->cursor_shape = cursor_push_xpm;
145     event_context->hot_x = 4;
146     event_context->hot_y = 4;
148     /* attributes */
149     tc->dragging = FALSE;
151     tc->width = 0.2;
152     tc->force = 0.2;
153     tc->pressure = TC_DEFAULT_PRESSURE;
155     tc->is_dilating = false;
156     tc->has_dilated = false;
158     tc->do_h = true;
159     tc->do_s = true;
160     tc->do_l = true;
161     tc->do_o = false;
163     new (&tc->style_set_connection) sigc::connection();
166 static void
167 sp_tweak_context_dispose(GObject *object)
169     SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
171     tc->style_set_connection.disconnect();
172     tc->style_set_connection.~connection();
174     if (tc->dilate_area) {
175         gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
176         tc->dilate_area = NULL;
177     }
179     if (tc->_message_context) {
180         delete tc->_message_context;
181     }
183     G_OBJECT_CLASS(parent_class)->dispose(object);
186 void
187 sp_tweak_update_cursor (SPTweakContext *tc, gint mode)
189     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
190    switch (mode) {
191        case TWEAK_MODE_PUSH:
192            event_context->cursor_shape = cursor_push_xpm;
193            break;
194        case TWEAK_MODE_SHRINK:
195            event_context->cursor_shape = cursor_thin_xpm;
196            break;
197        case TWEAK_MODE_GROW:
198            event_context->cursor_shape = cursor_thicken_xpm;
199            break;
200        case TWEAK_MODE_ATTRACT:
201            event_context->cursor_shape = cursor_attract_xpm;
202            break;
203        case TWEAK_MODE_REPEL:
204            event_context->cursor_shape = cursor_repel_xpm;
205            break;
206        case TWEAK_MODE_ROUGHEN:
207            event_context->cursor_shape = cursor_roughen_xpm;
208            break;
209        case TWEAK_MODE_COLORPAINT:
210        case TWEAK_MODE_COLORJITTER:
211            event_context->cursor_shape = cursor_color_xpm;
212            break;
213    }
214    sp_event_context_update_cursor(event_context);
217 static bool
218 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
220     if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
221         // we cannot store properties with uris
222         css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
224         sp_repr_css_change (inkscape_get_repr (INKSCAPE, "tools.tweak"), (SPCSSAttr *) css, "style");
226         return true;
227     }
228     return false;
232 static void
233 sp_tweak_context_setup(SPEventContext *ec)
235     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
237     if (((SPEventContextClass *) parent_class)->setup)
238         ((SPEventContextClass *) parent_class)->setup(ec);
240     {
241         SPCurve *c = sp_curve_new_from_foreign_bpath(hatch_area_circle);
242         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
243         sp_curve_unref(c);
244         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
245         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
246         sp_canvas_item_hide(tc->dilate_area);
247     }
249     sp_event_context_read(ec, "width");
250     sp_event_context_read(ec, "mode");
251     sp_event_context_read(ec, "fidelity");
252     sp_event_context_read(ec, "force");
253     sp_event_context_read(ec, "usepressure");
254     sp_event_context_read(ec, "doh");
255     sp_event_context_read(ec, "dol");
256     sp_event_context_read(ec, "dos");
257     sp_event_context_read(ec, "doo");
259     tc->is_drawing = false;
261     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
263     tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
264         sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
265     );
268 static void
269 sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
271     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
273     if (!strcmp(key, "width")) {
274         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
275         tc->width = CLAMP(dval, -1000.0, 1000.0);
276     } else if (!strcmp(key, "mode")) {
277         gint64 const dval = ( val ? g_ascii_strtoll (val, NULL, 10) : 0 );
278         tc->mode = dval;
279         sp_tweak_update_cursor(tc, tc->mode);
280     } else if (!strcmp(key, "fidelity")) {
281         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
282         tc->fidelity = CLAMP(dval, 0.0, 1.0);
283     } else if (!strcmp(key, "force")) {
284         double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
285         tc->force = CLAMP(dval, 0, 1.0);
286     } else if (!strcmp(key, "usepressure")) {
287         tc->usepressure = (val && strcmp(val, "0"));
288     } else if (!strcmp(key, "doh")) {
289         tc->do_h = (val && strcmp(val, "0"));
290     } else if (!strcmp(key, "dos")) {
291         tc->do_s = (val && strcmp(val, "0"));
292     } else if (!strcmp(key, "dol")) {
293         tc->do_l = (val && strcmp(val, "0"));
294     } else if (!strcmp(key, "doo")) {
295         tc->do_o = (val && strcmp(val, "0"));
296     } 
299 static void
300 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
302     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
303         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
304     else
305         tc->pressure = TC_DEFAULT_PRESSURE;
308 double
309 get_dilate_radius (SPTweakContext *tc)
311     // 10 times the pen width:
312     return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom(); 
315 double
316 get_dilate_force (SPTweakContext *tc)
318     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
319         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom()); 
320     if (force > 3) {
321         force += 4 * (force - 3);
322     }
323     return force * tc->force;
326 bool
327 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, NR::Point p, NR::Point vector, gint mode, double radius, double force, double fidelity)
329     bool did = false;
331     if (SP_IS_GROUP(item)) {
332         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
333             if (SP_IS_ITEM(child)) {
334                 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity))
335                     did = true;
336             }
337         }
339     } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
341         Inkscape::XML::Node *newrepr = NULL;
342         gint pos = 0;
343         Inkscape::XML::Node *parent = NULL;
344         char const *id = NULL;
345         if (!SP_IS_PATH(item)) {
346             newrepr = sp_selected_item_to_curved_repr(item, 0);
347             if (!newrepr)
348                 return false;
350             // remember the position of the item
351             pos = SP_OBJECT_REPR(item)->position();
352             // remember parent
353             parent = SP_OBJECT_REPR(item)->parent();
354             // remember id
355             id = SP_OBJECT_REPR(item)->attribute("id");
356         }
359         // skip those paths whose bboxes are entirely out of reach with our radius
360         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item));
361         if (bbox) {
362             bbox->growBy(radius);
363             if (!bbox->contains(p)) {
364                 return false;
365             }
366         }
368         Path *orig = Path_for_item(item, false);
369         if (orig == NULL) {
370             return false;
371         }
373         Path *res = new Path;
374         res->SetBackData(false);
376         Shape *theShape = new Shape;
377         Shape *theRes = new Shape;
379         orig->ConvertWithBackData(0.08 - (0.07 * fidelity)); // default 0.059
380         orig->Fill(theShape, 0);
382         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
383         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
384         if (val && strcmp(val, "nonzero") == 0)
385         {
386             theRes->ConvertToShape(theShape, fill_nonZero);
387         }
388         else if (val && strcmp(val, "evenodd") == 0)
389         {
390             theRes->ConvertToShape(theShape, fill_oddEven);
391         }
392         else
393         {
394             theRes->ConvertToShape(theShape, fill_nonZero);
395         }
397         if (NR::L2(vector) != 0)
398             vector = 1/NR::L2(vector) * vector;
400         bool did_this = false;
401         NR::Matrix i2doc(sp_item_i2doc_affine(item));
402         if (mode == TWEAK_MODE_SHRINK || mode == TWEAK_MODE_GROW) {
403             if (theShape->MakeOffset(theRes, 
404                                  mode == TWEAK_MODE_GROW? force : -force,
405                                  join_straight, 4.0,
406                                  true, p[NR::X], p[NR::Y], radius, &i2doc) == 0) // 0 means the shape was actually changed
407               did_this = true;
408         } else if (mode == TWEAK_MODE_ATTRACT || mode == TWEAK_MODE_REPEL) {
409             if (theShape->MakeRepel(theRes, 
410                                  mode == TWEAK_MODE_REPEL? force : -force,
411                                  join_straight, 4.0,
412                                  true, p[NR::X], p[NR::Y], radius, &i2doc) == 0) 
413               did_this = true;
414         } else if (mode == TWEAK_MODE_PUSH) {
415             if (theShape->MakePush(theRes, 
416                                  join_straight, 4.0,
417                                  true, p, force*2*vector, radius, &i2doc) == 0)
418               did_this = true;
419         } else if (mode == TWEAK_MODE_ROUGHEN) {
420             if (theShape->MakeJitter(theRes, 
421                                  join_straight, 4.0,
422                                  true, p, force, radius, &i2doc) == 0)
423               did_this = true;
424         }
426         // the rest only makes sense if we actually changed the path
427         if (did_this) {
428             theRes->ConvertToShape(theShape, fill_positive);
430             res->Reset();
431             theRes->ConvertToForme(res);
433             double th_max = 0.6 - 0.59*sqrt(fidelity);
434             double threshold = MAX(th_max, th_max*force);
435             res->ConvertEvenLines(threshold);
436             res->Simplify(threshold);
438             if (newrepr) { // converting to path, need to replace the repr
439                 bool is_selected = selection->includes(item);
440                 if (is_selected)
441                     selection->remove(item);
443                 // It's going to resurrect, so we delete without notifying listeners.
444                 SP_OBJECT(item)->deleteObject(false);
446                 // restore id
447                 newrepr->setAttribute("id", id);
448                 // add the new repr to the parent
449                 parent->appendChild(newrepr);
450                 // move to the saved position
451                 newrepr->setPosition(pos > 0 ? pos : 0);
453                 if (is_selected)
454                     selection->add(newrepr);
455             }
457             if (res->descr_cmd.size() > 1) { 
458                 gchar *str = res->svg_dump_path();
459                 if (newrepr) {
460                     newrepr->setAttribute("d", str);
461                 } else {
462                     if (SP_IS_SHAPE(item) && SP_SHAPE(item)->path_effect_href) {
463                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
464                     } else {
465                         SP_OBJECT_REPR(item)->setAttribute("d", str);
466                     }
467                 }
468                 g_free(str);
469             } else {
470                 // TODO: if there's 0 or 1 node left, delete this path altogether
471             }
473             if (newrepr) {
474                 Inkscape::GC::release(newrepr);
475                 newrepr = NULL;
476             }
477         }
479         delete theShape;
480         delete theRes;
481         delete orig;
482         delete res;
484         if (did_this) 
485             did = true;
486     }
488     return did;
491 void 
492 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l) 
494     float rgb_g[3];
496     if (!do_h || !do_s || !do_l) {
497         float hsl_g[3];
498         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
499         float hsl_c[3];
500         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
501         if (!do_h) 
502             hsl_g[0] = hsl_c[0];
503         if (!do_s) 
504             hsl_g[1] = hsl_c[1];
505         if (!do_l) 
506             hsl_g[2] = hsl_c[2];
507         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
508     } else {
509         rgb_g[0] = SP_RGBA32_R_F(goal);
510         rgb_g[1] = SP_RGBA32_G_F(goal);
511         rgb_g[2] = SP_RGBA32_B_F(goal);
512     }
514     for (int i = 0; i < 3; i++) {
515         double d = rgb_g[i] - color[i];
516         color[i] += d * force;
517     }
520 void 
521 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l) 
523     force = force*force; // in [0..1], this makes finer adjustments easier
525     float hsl_c[3];
526     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
528     if (do_h) {
529         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
530         if (hsl_c[0] > 1)
531             hsl_c[0] -= 1;
532         if (hsl_c[0] < 0)
533             hsl_c[0] += 1;
534     }
535     if (do_s) {
536         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
537     }
538     if (do_l) {
539         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
540     }
542     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
545 void 
546 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l) 
548     if (mode == TWEAK_MODE_COLORPAINT) {
549         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
550     } else if (mode == TWEAK_MODE_COLORJITTER) {
551         tweak_colorjitter (color, force, do_h, do_s, do_l);
552     }
555 void
556 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
558     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
560     if (mode == TWEAK_MODE_COLORPAINT) {
561         double d = opacity_goal - opacity;
562         opacity += d * force;
563     } else if (mode == TWEAK_MODE_COLORJITTER) {
564         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
565     }
567     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
571 double
572 tweak_profile (double dist, double radius) 
574     if (radius == 0)
575         return 0;
576     double x = dist / radius;
577     double alpha = 1;
578     if (x >= 1) {
579         return 0;
580     } else if (x <= 0) {
581         return 1;
582     } else {
583         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
584     }
587 void
588 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke, 
589     guint32 const rgb_goal, NR::Point p_w, double radius, double force, guint mode, 
590     bool do_h, bool do_s, bool do_l, bool do_o)
592     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
594     if (!gradient || !SP_IS_GRADIENT(gradient))
595         return;
597     NR::Matrix i2d = sp_item_i2doc_affine (item);
598     NR::Point p = p_w * i2d.inverse();
599     p *= (gradient->gradientTransform).inverse();
600     // now p is in gradient's original coordinates
602     double pos = 0;
603     double r = 0;
604     if (SP_IS_LINEARGRADIENT(gradient)) {
605         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
607         NR::Point p1(lg->x1.computed, lg->y1.computed);
608         NR::Point p2(lg->x2.computed, lg->y2.computed);
609         NR::Point pdiff(p2 - p1);
610         double vl = NR::L2(pdiff);
612         // This is the matrix which moves and rotates the gradient line
613         // so it's oriented along the X axis:
614         NR::Matrix norm = NR::Matrix(NR::translate(-p1)) * NR::Matrix(NR::rotate(-atan2(pdiff[NR::Y], pdiff[NR::X])));
616         // Transform the mouse point by it to find out its projection onto the gradient line:
617         NR::Point pnorm = p * norm;
619         // Scale its X coordinate to match the length of the gradient line:
620         pos = pnorm[NR::X] / vl;
621         // Calculate radius in lenfth-of-gradient-line units
622         r = radius / vl;
624     } else if (SP_IS_RADIALGRADIENT(gradient)) {
625         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
626         NR::Point c (rg->cx.computed, rg->cy.computed);
627         pos = NR::L2(p - c) / rg->r.computed;
628         r = radius / rg->r.computed;
629     }
631     // Normalize pos to 0..1, taking into accound gradient spread:
632     double pos_e = pos;
633     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
634         if (pos > 1)
635             pos_e = 1;
636         if (pos < 0)
637             pos_e = 0;
638     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
639         if (pos > 1 || pos < 0)
640             pos_e = pos - floor(pos);
641     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
642         if (pos > 1 || pos < 0) {
643             bool odd = ((int)(floor(pos)) % 2 == 1);
644             pos_e = pos - floor(pos);
645             if (odd) 
646                 pos_e = 1 - pos_e;
647         }
648     }
650     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
652     double offset_l = 0;
653     double offset_h = 0;
654     SPObject *child_prev = NULL;
655     for (SPObject *child = sp_object_first_child(vector);
656          child != NULL; child = SP_OBJECT_NEXT(child)) {
657         if (!SP_IS_STOP(child))
658             continue;
659         SPStop *stop = SP_STOP (child);
661         offset_h = stop->offset;
663         if (child_prev) {
665             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
666                 // the summit falls in this interstop, and the radius is small,
667                 // so it only affects the ends of this interstop;
668                 // distribute the force between the two endstops so that they
669                 // get all the painting even if they are not touched by the brush
670                 tweak_color (mode, stop->specified_color.v.c, rgb_goal, 
671                                   force * (pos_e - offset_l) / (offset_h - offset_l), 
672                                   do_h, do_s, do_l);
673                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal, 
674                                   force * (offset_h - pos_e) / (offset_h - offset_l), 
675                                   do_h, do_s, do_l);
676                 SP_OBJECT(stop)->updateRepr();
677                 child_prev->updateRepr();
678                 break;
679             } else {
680                 // wide brush, may affect more than 2 stops, 
681                 // paint each stop by the force from the profile curve
682                 if (offset_l <= pos_e && offset_l > pos_e - r) { 
683                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal, 
684                                  force * tweak_profile (fabs (pos_e - offset_l), r),
685                                  do_h, do_s, do_l);
686                     child_prev->updateRepr();
687                 } 
689                 if (offset_h >= pos_e && offset_h < pos_e + r) { 
690                     tweak_color (mode, stop->specified_color.v.c, rgb_goal, 
691                                  force * tweak_profile (fabs (pos_e - offset_h), r),
692                                  do_h, do_s, do_l);
693                     SP_OBJECT(stop)->updateRepr();
694                 }
695             }
696         }
698         offset_l = offset_h;
699         child_prev = child;
700     }
703 bool
704 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point, 
705                           guint32 fill_goal, bool do_fill,
706                           guint32 stroke_goal, bool do_stroke,
707                           float opacity_goal, bool do_opacity,
708                           NR::Point p, double radius, double force, 
709                           bool do_h, bool do_s, bool do_l, bool do_o)
711     bool did = false;
713     if (SP_IS_GROUP(item)) {
714         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
715             if (SP_IS_ITEM(child)) {
716                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point, 
717                                           fill_goal, do_fill,
718                                           stroke_goal, do_stroke,
719                                           opacity_goal, do_opacity,
720                                           p, radius, force, do_h, do_s, do_l, do_o));
721                     did = true;
722             }
723         }
725     } else {
726         SPStyle *style = SP_OBJECT_STYLE(item);
727         if (!style) {
728             return false;
729         }
730         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item),
731                                                         SPItem::GEOMETRIC_BBOX);
732         if (!bbox) {
733             return false;
734         }
736         NR::Rect brush(p - NR::Point(radius, radius), p + NR::Point(radius, radius));
738         NR::Point center = bbox->midpoint();
739         double this_force;
741 // if item == item_at_point, use max force
742         if (item == item_at_point) {
743             this_force = force;
744 // else if no overlap of bbox and brush box, skip:
745         } else if (!bbox->intersects(brush)) {
746             return false;
747 //TODO:
748 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
749         //} else if (bbox->maxExtent() > 3 * radius) {
750         //}
751 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
752 // else if still smaller, then check only the object center:
753         } else {
754             this_force = force * tweak_profile (NR::L2 (p - center), radius);
755         }
757         if (this_force > 0.002) {
759             if (do_fill) {
760                 if (style->fill.isPaintserver()) {
761                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
762                     did = true;
763                 } else if (style->fill.isColor()) {
764                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
765                     item->updateRepr();
766                     did = true;
767                 }
768             }
769             if (do_stroke) {
770                 if (style->stroke.isPaintserver()) {
771                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
772                     did = true;
773                 } else if (style->stroke.isColor()) {
774                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
775                     item->updateRepr();
776                     did = true;
777                 }
778             }
779             if (do_opacity && do_o) {
780                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
781             }
782         }
783     }
785     return did;
789 bool
790 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector)
792     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
794     if (selection->isEmpty()) {
795         return false;
796     }
798     bool did = false;
799     double radius = get_dilate_radius(tc); 
800     double force = get_dilate_force(tc); 
801     if (radius == 0 || force == 0) {
802         return false;
803     }
804     double color_force = MIN(sqrt(force)/20.0, 1);
806     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
807     Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.tweak");
808     SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
809     if (tc->mode == TWEAK_MODE_COLORPAINT && !css) 
810         return false;
812     bool do_fill = false, do_stroke = false, do_opacity = false;
813     guint32 fill_goal = 0, stroke_goal = 0;
814     float opacity_goal = 1;
816     const gchar *fill_prop = sp_repr_css_property(css, "fill", NULL);
817     if (fill_prop && strcmp(fill_prop, "none")) {
818         do_fill = true;
819         fill_goal = sp_svg_read_color(fill_prop, 0);
820     }
821     const gchar *stroke_prop = sp_repr_css_property(css, "stroke", NULL);
822     if (stroke_prop && strcmp(stroke_prop, "none")) {
823         do_stroke = true;
824         stroke_goal = sp_svg_read_color(stroke_prop, 0);
825     }
826     const gchar *opacity_prop = sp_repr_css_property(css, "opacity", NULL);
827     if (opacity_prop) {
828         do_opacity = true;
829         sp_svg_number_read_f(opacity_prop, &opacity_goal);
830     }
832     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
833          items != NULL;
834          items = items->next) {
836         SPItem *item = (SPItem *) items->data;
838         if (tc->mode == TWEAK_MODE_COLORPAINT || tc->mode == TWEAK_MODE_COLORJITTER) {
839             if (do_fill || do_stroke || do_opacity) {
840                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point, 
841                                           fill_goal, do_fill,
842                                           stroke_goal, do_stroke,
843                                           opacity_goal, do_opacity,
844                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
845                 did = true;
846             }
847         } else {
848             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, force, tc->fidelity))
849                 did = true;
850         }
851     }
853     return did;
856 void
857 sp_tweak_update_area (SPTweakContext *tc)
859         double radius = get_dilate_radius(tc);
860         NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
861         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
862         sp_canvas_item_show(tc->dilate_area);
865 void
866 sp_tweak_switch_mode (SPTweakContext *tc, gint mode)
868     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
869     // need to set explicitly, because the prefs may not have changed by the previous
870     tc->mode = mode;
871     sp_tweak_update_cursor (tc, mode);
874 void
875 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode)
877    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
878    gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
879    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
880    // button has changed prefs, restore
881    prefs_set_int_attribute("tools.tweak", "mode", now_mode);
882    // changing prefs changed tc->mode, restore back :)
883    tc->mode = mode;
884    sp_tweak_update_cursor (tc, mode);
887 gint
888 sp_tweak_context_root_handler(SPEventContext *event_context,
889                                   GdkEvent *event)
891     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
892     SPDesktop *desktop = event_context->desktop;
894     gint ret = FALSE;
896     switch (event->type) {
897         case GDK_ENTER_NOTIFY:
898             sp_canvas_item_show(tc->dilate_area);
899             break;
900         case GDK_LEAVE_NOTIFY:
901             sp_canvas_item_hide(tc->dilate_area);
902             break;
903         case GDK_BUTTON_PRESS:
904             if (event->button.button == 1 && !event_context->space_panning) {
906                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
907                     return TRUE;
908                 }
910                 NR::Point const button_w(event->button.x,
911                                          event->button.y);
912                 NR::Point const button_dt(desktop->w2d(button_w));
913                 tc->last_push = desktop->dt2doc(button_dt);
915                 sp_tweak_extinput(tc, event);
917                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
918                 tc->is_drawing = true;
919                 tc->is_dilating = true;
920                 tc->has_dilated = false;
922                 ret = TRUE;
923             }
924             break;
925         case GDK_MOTION_NOTIFY:
926         {
927             NR::Point const motion_w(event->motion.x,
928                                      event->motion.y);
929             NR::Point motion_dt(desktop->w2d(motion_w));
930             NR::Point motion_doc(desktop->dt2doc(motion_dt));
931             sp_tweak_extinput(tc, event);
933             // draw the dilating cursor
934                 double radius = get_dilate_radius(tc);
935                 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
936                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
937                 sp_canvas_item_show(tc->dilate_area);
939                 guint num = 0;
940                 if (!desktop->selection->isEmpty()) {
941                     num = g_slist_length((GSList *) desktop->selection->itemList());
942                 }
943                 if (num == 0) {
944                     tc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
945                 } else {
946                     switch (tc->mode) {
947                         case TWEAK_MODE_PUSH:
948                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
949                                                       ngettext("<b>Pushing %d</b> selected object", 
950                                                       "<b>Pushing %d</b> selected objects", num), num);
951                            break;
952                         case TWEAK_MODE_SHRINK:
953                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
954                                                       ngettext("<b>Shrinking %d</b> selected object", 
955                                                       "<b>Shrinking %d</b> selected objects", num), num);
956                            break;
957                         case TWEAK_MODE_GROW:
958                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
959                                                       ngettext("<b>Growing %d</b> selected object", 
960                                                       "<b>Growing %d</b> selected objects", num), num);
961                            break;
962                         case TWEAK_MODE_ATTRACT:
963                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
964                                                       ngettext("<b>Attracting %d</b> selected object", 
965                                                       "<b>Attracting %d</b> selected objects", num), num);
966                            break;
967                         case TWEAK_MODE_REPEL:
968                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
969                                                       ngettext("<b>Repelling %d</b> selected object", 
970                                                       "<b>Repelling %d</b> selected objects", num), num);
971                            break;
972                         case TWEAK_MODE_ROUGHEN:
973                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
974                                                       ngettext("<b>Roughening %d</b> selected object", 
975                                                       "<b>Roughening %d</b> selected objects", num), num);
976                         case TWEAK_MODE_COLORPAINT:
977                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
978                                                       ngettext("<b>Painting %d</b> selected object", 
979                                                       "<b>Painting %d</b> selected objects", num), num);
980                            break;
981                         case TWEAK_MODE_COLORJITTER:
982                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
983                                                       ngettext("<b>Jittering colors in %d</b> selected object", 
984                                                       "<b>Jittering colors in %d</b> selected objects", num), num);
985                            break;
986                     }
987                 }
990             // dilating:
991             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {  
992                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push);
993                 //tc->last_push = motion_doc;
994                 tc->has_dilated = true;
995                 // it's slow, so prevent clogging up with events
996                 gobble_motion_events(GDK_BUTTON1_MASK);
997                 return TRUE;
998             }
1000         }
1001         break;
1004     case GDK_BUTTON_RELEASE:
1005     {
1006         NR::Point const motion_w(event->button.x, event->button.y);
1007         NR::Point const motion_dt(desktop->w2d(motion_w));
1009         sp_canvas_end_forced_full_redraws(desktop->canvas);
1010         tc->is_drawing = false;
1012         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1013             if (!tc->has_dilated) {
1014                 // if we did not rub, do a light tap
1015                 tc->pressure = 0.03;
1016                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0));
1017             }
1018             tc->is_dilating = false;
1019             tc->has_dilated = false;
1020             switch (tc->mode) {
1021                 case TWEAK_MODE_PUSH:
1022                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1023                                      SP_VERB_CONTEXT_TWEAK, _("Push tweak"));
1024                     break;
1025                 case TWEAK_MODE_SHRINK:
1026                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1027                                      SP_VERB_CONTEXT_TWEAK, _("Shrink tweak"));
1028                     break;
1029                 case TWEAK_MODE_GROW:
1030                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1031                                      SP_VERB_CONTEXT_TWEAK, _("Grow tweak"));
1032                     break;
1033                 case TWEAK_MODE_ATTRACT:
1034                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1035                                      SP_VERB_CONTEXT_TWEAK, _("Attract tweak"));
1036                     break;
1037                 case TWEAK_MODE_REPEL:
1038                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1039                                      SP_VERB_CONTEXT_TWEAK, _("Repel tweak"));
1040                     break;
1041                 case TWEAK_MODE_ROUGHEN:
1042                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1043                                      SP_VERB_CONTEXT_TWEAK, _("Roughen tweak"));
1044                     break;
1045                 case TWEAK_MODE_COLORPAINT:
1046                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1047                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1048                 case TWEAK_MODE_COLORJITTER:
1049                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1050                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1051                     break;
1052             }
1053         } 
1054         break;
1055     }
1057     case GDK_KEY_PRESS:
1058         switch (get_group0_keyval (&event->key)) {
1059         case GDK_p:
1060         case GDK_P:
1061         case GDK_1:
1062             if (MOD__SHIFT_ONLY) {
1063                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH);
1064                 ret = TRUE;
1065             }
1066             break;
1067         case GDK_s:
1068         case GDK_S:
1069         case GDK_2:
1070             if (MOD__SHIFT_ONLY) {
1071                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK);
1072                 ret = TRUE;
1073             }
1074             break;
1075         case GDK_g:
1076         case GDK_G:
1077         case GDK_3:
1078             if (MOD__SHIFT_ONLY) {
1079                 sp_tweak_switch_mode(tc, TWEAK_MODE_GROW);
1080                 ret = TRUE;
1081             }
1082             break;
1083         case GDK_a:
1084         case GDK_A:
1085         case GDK_4:
1086             if (MOD__SHIFT_ONLY) {
1087                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT);
1088                 ret = TRUE;
1089             }
1090             break;
1091         case GDK_e:
1092         case GDK_E:
1093         case GDK_5:
1094             if (MOD__SHIFT_ONLY) {
1095                 sp_tweak_switch_mode(tc, TWEAK_MODE_REPEL);
1096                 ret = TRUE;
1097             }
1098             break;
1099         case GDK_r:
1100         case GDK_R:
1101         case GDK_6:
1102             if (MOD__SHIFT_ONLY) {
1103                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN);
1104                 ret = TRUE;
1105             }
1106             break;
1107         case GDK_c:
1108         case GDK_C:
1109         case GDK_7:
1110             if (MOD__SHIFT_ONLY) {
1111                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT);
1112                 ret = TRUE;
1113             }
1114             break;
1115         case GDK_j:
1116         case GDK_J:
1117         case GDK_8:
1118             if (MOD__SHIFT_ONLY) {
1119                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER);
1120                 ret = TRUE;
1121             }
1122             break;
1124         case GDK_Up:
1125         case GDK_KP_Up:
1126             if (!MOD__CTRL_ONLY) {
1127                 tc->force += 0.05;
1128                 if (tc->force > 1.0)
1129                     tc->force = 1.0;
1130                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1131                 ret = TRUE;
1132             }
1133             break;
1134         case GDK_Down:
1135         case GDK_KP_Down:
1136             if (!MOD__CTRL_ONLY) {
1137                 tc->force -= 0.05;
1138                 if (tc->force < 0.0)
1139                     tc->force = 0.0;
1140                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1141                 ret = TRUE;
1142             }
1143             break;
1144         case GDK_Right:
1145         case GDK_KP_Right:
1146             if (!MOD__CTRL_ONLY) {
1147                 tc->width += 0.01;
1148                 if (tc->width > 1.0)
1149                     tc->width = 1.0;
1150                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1151                 sp_tweak_update_area(tc);
1152                 ret = TRUE;
1153             }
1154             break;
1155         case GDK_Left:
1156         case GDK_KP_Left:
1157             if (!MOD__CTRL_ONLY) {
1158                 tc->width -= 0.01;
1159                 if (tc->width < 0.01)
1160                     tc->width = 0.01;
1161                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1162                 sp_tweak_update_area(tc);
1163                 ret = TRUE;
1164             }
1165             break;
1166         case GDK_Home:
1167         case GDK_KP_Home:
1168             tc->width = 0.01;
1169             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1170             sp_tweak_update_area(tc);
1171             ret = TRUE;
1172             break;
1173         case GDK_End:
1174         case GDK_KP_End:
1175             tc->width = 1.0;
1176             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1177             sp_tweak_update_area(tc);
1178             ret = TRUE;
1179             break;
1180         case GDK_x:
1181         case GDK_X:
1182             if (MOD__ALT_ONLY) {
1183                 desktop->setToolboxFocusTo ("altx-tweak");
1184                 ret = TRUE;
1185             }
1186             break;
1188         case GDK_Control_L:
1189         case GDK_Control_R:
1190             if (MOD__SHIFT) {
1191                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1192             } else {
1193                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1194             }
1195             break;
1196         case GDK_Shift_L:
1197         case GDK_Shift_R:
1198             if (MOD__CTRL) {
1199                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1200             }
1201             break;
1202         default:
1203             break;
1204         }
1205         break;
1207     case GDK_KEY_RELEASE:
1208         switch (get_group0_keyval(&event->key)) {
1209             case GDK_Control_L:
1210             case GDK_Control_R:
1211                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1212                 tc->_message_context->clear();
1213                 break;
1214             case GDK_Shift_L:
1215             case GDK_Shift_R:
1216                 if (MOD__CTRL) {
1217                     sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1218                 }
1219                 break;
1220             break;
1221             default:
1222                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1223                 break;
1224         }
1226     default:
1227         break;
1228     }
1230     if (!ret) {
1231         if (((SPEventContextClass *) parent_class)->root_handler) {
1232             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1233         }
1234     }
1236     return ret;
1240 /*
1241   Local Variables:
1242   mode:c++
1243   c-file-style:"stroustrup"
1244   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1245   indent-tabs-mode:nil
1246   fill-column:99
1247   End:
1248 */
1249 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :