Code

refactoring, one MakeTweak method instead of a bunch largely similar methods
[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->MakeTweak(tweak_mode_grow, theRes, 
404                                  mode == TWEAK_MODE_GROW? force : -force,
405                                  join_straight, 4.0,
406                                  true, p, NR::Point(0,0), 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->MakeTweak(tweak_mode_repel, theRes, 
410                                  mode == TWEAK_MODE_REPEL? force : -force,
411                                  join_straight, 4.0,
412                                  true, p, NR::Point(0,0), radius, &i2doc) == 0) 
413               did_this = true;
414         } else if (mode == TWEAK_MODE_PUSH) {
415             if (theShape->MakeTweak(tweak_mode_push, theRes, 
416                                  0,
417                                  join_straight, 4.0,
418                                  true, p, force*2*vector, radius, &i2doc) == 0)
419               did_this = true;
420         } else if (mode == TWEAK_MODE_ROUGHEN) {
421             if (theShape->MakeTweak(tweak_mode_roughen, theRes, 
422                                  force,
423                                  join_straight, 4.0,
424                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
425               did_this = true;
426         }
428         // the rest only makes sense if we actually changed the path
429         if (did_this) {
430             theRes->ConvertToShape(theShape, fill_positive);
432             res->Reset();
433             theRes->ConvertToForme(res);
435             double th_max = 0.6 - 0.59*sqrt(fidelity);
436             double threshold = MAX(th_max, th_max*force);
437             res->ConvertEvenLines(threshold);
438             res->Simplify(threshold);
440             if (newrepr) { // converting to path, need to replace the repr
441                 bool is_selected = selection->includes(item);
442                 if (is_selected)
443                     selection->remove(item);
445                 // It's going to resurrect, so we delete without notifying listeners.
446                 SP_OBJECT(item)->deleteObject(false);
448                 // restore id
449                 newrepr->setAttribute("id", id);
450                 // add the new repr to the parent
451                 parent->appendChild(newrepr);
452                 // move to the saved position
453                 newrepr->setPosition(pos > 0 ? pos : 0);
455                 if (is_selected)
456                     selection->add(newrepr);
457             }
459             if (res->descr_cmd.size() > 1) { 
460                 gchar *str = res->svg_dump_path();
461                 if (newrepr) {
462                     newrepr->setAttribute("d", str);
463                 } else {
464                     if (SP_IS_SHAPE(item) && SP_SHAPE(item)->path_effect_href) {
465                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
466                     } else {
467                         SP_OBJECT_REPR(item)->setAttribute("d", str);
468                     }
469                 }
470                 g_free(str);
471             } else {
472                 // TODO: if there's 0 or 1 node left, delete this path altogether
473             }
475             if (newrepr) {
476                 Inkscape::GC::release(newrepr);
477                 newrepr = NULL;
478             }
479         }
481         delete theShape;
482         delete theRes;
483         delete orig;
484         delete res;
486         if (did_this) 
487             did = true;
488     }
490     return did;
493 void 
494 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l) 
496     float rgb_g[3];
498     if (!do_h || !do_s || !do_l) {
499         float hsl_g[3];
500         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
501         float hsl_c[3];
502         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
503         if (!do_h) 
504             hsl_g[0] = hsl_c[0];
505         if (!do_s) 
506             hsl_g[1] = hsl_c[1];
507         if (!do_l) 
508             hsl_g[2] = hsl_c[2];
509         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
510     } else {
511         rgb_g[0] = SP_RGBA32_R_F(goal);
512         rgb_g[1] = SP_RGBA32_G_F(goal);
513         rgb_g[2] = SP_RGBA32_B_F(goal);
514     }
516     for (int i = 0; i < 3; i++) {
517         double d = rgb_g[i] - color[i];
518         color[i] += d * force;
519     }
522 void 
523 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l) 
525     force = force*force; // in [0..1], this makes finer adjustments easier
527     float hsl_c[3];
528     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
530     if (do_h) {
531         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
532         if (hsl_c[0] > 1)
533             hsl_c[0] -= 1;
534         if (hsl_c[0] < 0)
535             hsl_c[0] += 1;
536     }
537     if (do_s) {
538         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
539     }
540     if (do_l) {
541         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
542     }
544     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
547 void 
548 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l) 
550     if (mode == TWEAK_MODE_COLORPAINT) {
551         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
552     } else if (mode == TWEAK_MODE_COLORJITTER) {
553         tweak_colorjitter (color, force, do_h, do_s, do_l);
554     }
557 void
558 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
560     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
562     if (mode == TWEAK_MODE_COLORPAINT) {
563         double d = opacity_goal - opacity;
564         opacity += d * force;
565     } else if (mode == TWEAK_MODE_COLORJITTER) {
566         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
567     }
569     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
573 double
574 tweak_profile (double dist, double radius) 
576     if (radius == 0)
577         return 0;
578     double x = dist / radius;
579     double alpha = 1;
580     if (x >= 1) {
581         return 0;
582     } else if (x <= 0) {
583         return 1;
584     } else {
585         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
586     }
589 void
590 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke, 
591     guint32 const rgb_goal, NR::Point p_w, double radius, double force, guint mode, 
592     bool do_h, bool do_s, bool do_l, bool do_o)
594     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
596     if (!gradient || !SP_IS_GRADIENT(gradient))
597         return;
599     NR::Matrix i2d = sp_item_i2doc_affine (item);
600     NR::Point p = p_w * i2d.inverse();
601     p *= (gradient->gradientTransform).inverse();
602     // now p is in gradient's original coordinates
604     double pos = 0;
605     double r = 0;
606     if (SP_IS_LINEARGRADIENT(gradient)) {
607         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
609         NR::Point p1(lg->x1.computed, lg->y1.computed);
610         NR::Point p2(lg->x2.computed, lg->y2.computed);
611         NR::Point pdiff(p2 - p1);
612         double vl = NR::L2(pdiff);
614         // This is the matrix which moves and rotates the gradient line
615         // so it's oriented along the X axis:
616         NR::Matrix norm = NR::Matrix(NR::translate(-p1)) * NR::Matrix(NR::rotate(-atan2(pdiff[NR::Y], pdiff[NR::X])));
618         // Transform the mouse point by it to find out its projection onto the gradient line:
619         NR::Point pnorm = p * norm;
621         // Scale its X coordinate to match the length of the gradient line:
622         pos = pnorm[NR::X] / vl;
623         // Calculate radius in lenfth-of-gradient-line units
624         r = radius / vl;
626     } else if (SP_IS_RADIALGRADIENT(gradient)) {
627         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
628         NR::Point c (rg->cx.computed, rg->cy.computed);
629         pos = NR::L2(p - c) / rg->r.computed;
630         r = radius / rg->r.computed;
631     }
633     // Normalize pos to 0..1, taking into accound gradient spread:
634     double pos_e = pos;
635     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
636         if (pos > 1)
637             pos_e = 1;
638         if (pos < 0)
639             pos_e = 0;
640     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
641         if (pos > 1 || pos < 0)
642             pos_e = pos - floor(pos);
643     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
644         if (pos > 1 || pos < 0) {
645             bool odd = ((int)(floor(pos)) % 2 == 1);
646             pos_e = pos - floor(pos);
647             if (odd) 
648                 pos_e = 1 - pos_e;
649         }
650     }
652     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
654     double offset_l = 0;
655     double offset_h = 0;
656     SPObject *child_prev = NULL;
657     for (SPObject *child = sp_object_first_child(vector);
658          child != NULL; child = SP_OBJECT_NEXT(child)) {
659         if (!SP_IS_STOP(child))
660             continue;
661         SPStop *stop = SP_STOP (child);
663         offset_h = stop->offset;
665         if (child_prev) {
667             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
668                 // the summit falls in this interstop, and the radius is small,
669                 // so it only affects the ends of this interstop;
670                 // distribute the force between the two endstops so that they
671                 // get all the painting even if they are not touched by the brush
672                 tweak_color (mode, stop->specified_color.v.c, rgb_goal, 
673                                   force * (pos_e - offset_l) / (offset_h - offset_l), 
674                                   do_h, do_s, do_l);
675                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal, 
676                                   force * (offset_h - pos_e) / (offset_h - offset_l), 
677                                   do_h, do_s, do_l);
678                 SP_OBJECT(stop)->updateRepr();
679                 child_prev->updateRepr();
680                 break;
681             } else {
682                 // wide brush, may affect more than 2 stops, 
683                 // paint each stop by the force from the profile curve
684                 if (offset_l <= pos_e && offset_l > pos_e - r) { 
685                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal, 
686                                  force * tweak_profile (fabs (pos_e - offset_l), r),
687                                  do_h, do_s, do_l);
688                     child_prev->updateRepr();
689                 } 
691                 if (offset_h >= pos_e && offset_h < pos_e + r) { 
692                     tweak_color (mode, stop->specified_color.v.c, rgb_goal, 
693                                  force * tweak_profile (fabs (pos_e - offset_h), r),
694                                  do_h, do_s, do_l);
695                     SP_OBJECT(stop)->updateRepr();
696                 }
697             }
698         }
700         offset_l = offset_h;
701         child_prev = child;
702     }
705 bool
706 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point, 
707                           guint32 fill_goal, bool do_fill,
708                           guint32 stroke_goal, bool do_stroke,
709                           float opacity_goal, bool do_opacity,
710                           NR::Point p, double radius, double force, 
711                           bool do_h, bool do_s, bool do_l, bool do_o)
713     bool did = false;
715     if (SP_IS_GROUP(item)) {
716         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
717             if (SP_IS_ITEM(child)) {
718                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point, 
719                                           fill_goal, do_fill,
720                                           stroke_goal, do_stroke,
721                                           opacity_goal, do_opacity,
722                                           p, radius, force, do_h, do_s, do_l, do_o));
723                     did = true;
724             }
725         }
727     } else {
728         SPStyle *style = SP_OBJECT_STYLE(item);
729         if (!style) {
730             return false;
731         }
732         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item),
733                                                         SPItem::GEOMETRIC_BBOX);
734         if (!bbox) {
735             return false;
736         }
738         NR::Rect brush(p - NR::Point(radius, radius), p + NR::Point(radius, radius));
740         NR::Point center = bbox->midpoint();
741         double this_force;
743 // if item == item_at_point, use max force
744         if (item == item_at_point) {
745             this_force = force;
746 // else if no overlap of bbox and brush box, skip:
747         } else if (!bbox->intersects(brush)) {
748             return false;
749 //TODO:
750 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
751         //} else if (bbox->maxExtent() > 3 * radius) {
752         //}
753 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
754 // else if still smaller, then check only the object center:
755         } else {
756             this_force = force * tweak_profile (NR::L2 (p - center), radius);
757         }
759         if (this_force > 0.002) {
761             if (do_fill) {
762                 if (style->fill.isPaintserver()) {
763                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
764                     did = true;
765                 } else if (style->fill.isColor()) {
766                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
767                     item->updateRepr();
768                     did = true;
769                 }
770             }
771             if (do_stroke) {
772                 if (style->stroke.isPaintserver()) {
773                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
774                     did = true;
775                 } else if (style->stroke.isColor()) {
776                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
777                     item->updateRepr();
778                     did = true;
779                 }
780             }
781             if (do_opacity && do_o) {
782                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
783             }
784         }
785     }
787     return did;
791 bool
792 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector)
794     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
796     if (selection->isEmpty()) {
797         return false;
798     }
800     bool did = false;
801     double radius = get_dilate_radius(tc); 
802     double force = get_dilate_force(tc); 
803     if (radius == 0 || force == 0) {
804         return false;
805     }
806     double color_force = MIN(sqrt(force)/20.0, 1);
808     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
809     Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.tweak");
810     SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
811     if (tc->mode == TWEAK_MODE_COLORPAINT && !css) 
812         return false;
814     bool do_fill = false, do_stroke = false, do_opacity = false;
815     guint32 fill_goal = 0, stroke_goal = 0;
816     float opacity_goal = 1;
818     const gchar *fill_prop = sp_repr_css_property(css, "fill", NULL);
819     if (fill_prop && strcmp(fill_prop, "none")) {
820         do_fill = true;
821         fill_goal = sp_svg_read_color(fill_prop, 0);
822     }
823     const gchar *stroke_prop = sp_repr_css_property(css, "stroke", NULL);
824     if (stroke_prop && strcmp(stroke_prop, "none")) {
825         do_stroke = true;
826         stroke_goal = sp_svg_read_color(stroke_prop, 0);
827     }
828     const gchar *opacity_prop = sp_repr_css_property(css, "opacity", NULL);
829     if (opacity_prop) {
830         do_opacity = true;
831         sp_svg_number_read_f(opacity_prop, &opacity_goal);
832     }
834     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
835          items != NULL;
836          items = items->next) {
838         SPItem *item = (SPItem *) items->data;
840         if (tc->mode == TWEAK_MODE_COLORPAINT || tc->mode == TWEAK_MODE_COLORJITTER) {
841             if (do_fill || do_stroke || do_opacity) {
842                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point, 
843                                           fill_goal, do_fill,
844                                           stroke_goal, do_stroke,
845                                           opacity_goal, do_opacity,
846                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
847                 did = true;
848             }
849         } else {
850             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, force, tc->fidelity))
851                 did = true;
852         }
853     }
855     return did;
858 void
859 sp_tweak_update_area (SPTweakContext *tc)
861         double radius = get_dilate_radius(tc);
862         NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
863         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
864         sp_canvas_item_show(tc->dilate_area);
867 void
868 sp_tweak_switch_mode (SPTweakContext *tc, gint mode)
870     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
871     // need to set explicitly, because the prefs may not have changed by the previous
872     tc->mode = mode;
873     sp_tweak_update_cursor (tc, mode);
876 void
877 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode)
879    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
880    gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
881    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
882    // button has changed prefs, restore
883    prefs_set_int_attribute("tools.tweak", "mode", now_mode);
884    // changing prefs changed tc->mode, restore back :)
885    tc->mode = mode;
886    sp_tweak_update_cursor (tc, mode);
889 gint
890 sp_tweak_context_root_handler(SPEventContext *event_context,
891                                   GdkEvent *event)
893     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
894     SPDesktop *desktop = event_context->desktop;
896     gint ret = FALSE;
898     switch (event->type) {
899         case GDK_ENTER_NOTIFY:
900             sp_canvas_item_show(tc->dilate_area);
901             break;
902         case GDK_LEAVE_NOTIFY:
903             sp_canvas_item_hide(tc->dilate_area);
904             break;
905         case GDK_BUTTON_PRESS:
906             if (event->button.button == 1 && !event_context->space_panning) {
908                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
909                     return TRUE;
910                 }
912                 NR::Point const button_w(event->button.x,
913                                          event->button.y);
914                 NR::Point const button_dt(desktop->w2d(button_w));
915                 tc->last_push = desktop->dt2doc(button_dt);
917                 sp_tweak_extinput(tc, event);
919                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
920                 tc->is_drawing = true;
921                 tc->is_dilating = true;
922                 tc->has_dilated = false;
924                 ret = TRUE;
925             }
926             break;
927         case GDK_MOTION_NOTIFY:
928         {
929             NR::Point const motion_w(event->motion.x,
930                                      event->motion.y);
931             NR::Point motion_dt(desktop->w2d(motion_w));
932             NR::Point motion_doc(desktop->dt2doc(motion_dt));
933             sp_tweak_extinput(tc, event);
935             // draw the dilating cursor
936                 double radius = get_dilate_radius(tc);
937                 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
938                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
939                 sp_canvas_item_show(tc->dilate_area);
941                 guint num = 0;
942                 if (!desktop->selection->isEmpty()) {
943                     num = g_slist_length((GSList *) desktop->selection->itemList());
944                 }
945                 if (num == 0) {
946                     tc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
947                 } else {
948                     switch (tc->mode) {
949                         case TWEAK_MODE_PUSH:
950                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
951                                                       ngettext("<b>Pushing %d</b> selected object", 
952                                                       "<b>Pushing %d</b> selected objects", num), num);
953                            break;
954                         case TWEAK_MODE_SHRINK:
955                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
956                                                       ngettext("<b>Shrinking %d</b> selected object", 
957                                                       "<b>Shrinking %d</b> selected objects", num), num);
958                            break;
959                         case TWEAK_MODE_GROW:
960                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
961                                                       ngettext("<b>Growing %d</b> selected object", 
962                                                       "<b>Growing %d</b> selected objects", num), num);
963                            break;
964                         case TWEAK_MODE_ATTRACT:
965                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
966                                                       ngettext("<b>Attracting %d</b> selected object", 
967                                                       "<b>Attracting %d</b> selected objects", num), num);
968                            break;
969                         case TWEAK_MODE_REPEL:
970                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
971                                                       ngettext("<b>Repelling %d</b> selected object", 
972                                                       "<b>Repelling %d</b> selected objects", num), num);
973                            break;
974                         case TWEAK_MODE_ROUGHEN:
975                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
976                                                       ngettext("<b>Roughening %d</b> selected object", 
977                                                       "<b>Roughening %d</b> selected objects", num), num);
978                         case TWEAK_MODE_COLORPAINT:
979                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
980                                                       ngettext("<b>Painting %d</b> selected object", 
981                                                       "<b>Painting %d</b> selected objects", num), num);
982                            break;
983                         case TWEAK_MODE_COLORJITTER:
984                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
985                                                       ngettext("<b>Jittering colors in %d</b> selected object", 
986                                                       "<b>Jittering colors in %d</b> selected objects", num), num);
987                            break;
988                     }
989                 }
992             // dilating:
993             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {  
994                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push);
995                 //tc->last_push = motion_doc;
996                 tc->has_dilated = true;
997                 // it's slow, so prevent clogging up with events
998                 gobble_motion_events(GDK_BUTTON1_MASK);
999                 return TRUE;
1000             }
1002         }
1003         break;
1006     case GDK_BUTTON_RELEASE:
1007     {
1008         NR::Point const motion_w(event->button.x, event->button.y);
1009         NR::Point const motion_dt(desktop->w2d(motion_w));
1011         sp_canvas_end_forced_full_redraws(desktop->canvas);
1012         tc->is_drawing = false;
1014         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1015             if (!tc->has_dilated) {
1016                 // if we did not rub, do a light tap
1017                 tc->pressure = 0.03;
1018                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0));
1019             }
1020             tc->is_dilating = false;
1021             tc->has_dilated = false;
1022             switch (tc->mode) {
1023                 case TWEAK_MODE_PUSH:
1024                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1025                                      SP_VERB_CONTEXT_TWEAK, _("Push tweak"));
1026                     break;
1027                 case TWEAK_MODE_SHRINK:
1028                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1029                                      SP_VERB_CONTEXT_TWEAK, _("Shrink tweak"));
1030                     break;
1031                 case TWEAK_MODE_GROW:
1032                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1033                                      SP_VERB_CONTEXT_TWEAK, _("Grow tweak"));
1034                     break;
1035                 case TWEAK_MODE_ATTRACT:
1036                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1037                                      SP_VERB_CONTEXT_TWEAK, _("Attract tweak"));
1038                     break;
1039                 case TWEAK_MODE_REPEL:
1040                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1041                                      SP_VERB_CONTEXT_TWEAK, _("Repel tweak"));
1042                     break;
1043                 case TWEAK_MODE_ROUGHEN:
1044                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1045                                      SP_VERB_CONTEXT_TWEAK, _("Roughen tweak"));
1046                     break;
1047                 case TWEAK_MODE_COLORPAINT:
1048                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1049                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1050                 case TWEAK_MODE_COLORJITTER:
1051                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop), 
1052                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1053                     break;
1054             }
1055         } 
1056         break;
1057     }
1059     case GDK_KEY_PRESS:
1060         switch (get_group0_keyval (&event->key)) {
1061         case GDK_p:
1062         case GDK_P:
1063         case GDK_1:
1064             if (MOD__SHIFT_ONLY) {
1065                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH);
1066                 ret = TRUE;
1067             }
1068             break;
1069         case GDK_s:
1070         case GDK_S:
1071         case GDK_2:
1072             if (MOD__SHIFT_ONLY) {
1073                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK);
1074                 ret = TRUE;
1075             }
1076             break;
1077         case GDK_g:
1078         case GDK_G:
1079         case GDK_3:
1080             if (MOD__SHIFT_ONLY) {
1081                 sp_tweak_switch_mode(tc, TWEAK_MODE_GROW);
1082                 ret = TRUE;
1083             }
1084             break;
1085         case GDK_a:
1086         case GDK_A:
1087         case GDK_4:
1088             if (MOD__SHIFT_ONLY) {
1089                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT);
1090                 ret = TRUE;
1091             }
1092             break;
1093         case GDK_e:
1094         case GDK_E:
1095         case GDK_5:
1096             if (MOD__SHIFT_ONLY) {
1097                 sp_tweak_switch_mode(tc, TWEAK_MODE_REPEL);
1098                 ret = TRUE;
1099             }
1100             break;
1101         case GDK_r:
1102         case GDK_R:
1103         case GDK_6:
1104             if (MOD__SHIFT_ONLY) {
1105                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN);
1106                 ret = TRUE;
1107             }
1108             break;
1109         case GDK_c:
1110         case GDK_C:
1111         case GDK_7:
1112             if (MOD__SHIFT_ONLY) {
1113                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT);
1114                 ret = TRUE;
1115             }
1116             break;
1117         case GDK_j:
1118         case GDK_J:
1119         case GDK_8:
1120             if (MOD__SHIFT_ONLY) {
1121                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER);
1122                 ret = TRUE;
1123             }
1124             break;
1126         case GDK_Up:
1127         case GDK_KP_Up:
1128             if (!MOD__CTRL_ONLY) {
1129                 tc->force += 0.05;
1130                 if (tc->force > 1.0)
1131                     tc->force = 1.0;
1132                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1133                 ret = TRUE;
1134             }
1135             break;
1136         case GDK_Down:
1137         case GDK_KP_Down:
1138             if (!MOD__CTRL_ONLY) {
1139                 tc->force -= 0.05;
1140                 if (tc->force < 0.0)
1141                     tc->force = 0.0;
1142                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1143                 ret = TRUE;
1144             }
1145             break;
1146         case GDK_Right:
1147         case GDK_KP_Right:
1148             if (!MOD__CTRL_ONLY) {
1149                 tc->width += 0.01;
1150                 if (tc->width > 1.0)
1151                     tc->width = 1.0;
1152                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1153                 sp_tweak_update_area(tc);
1154                 ret = TRUE;
1155             }
1156             break;
1157         case GDK_Left:
1158         case GDK_KP_Left:
1159             if (!MOD__CTRL_ONLY) {
1160                 tc->width -= 0.01;
1161                 if (tc->width < 0.01)
1162                     tc->width = 0.01;
1163                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1164                 sp_tweak_update_area(tc);
1165                 ret = TRUE;
1166             }
1167             break;
1168         case GDK_Home:
1169         case GDK_KP_Home:
1170             tc->width = 0.01;
1171             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1172             sp_tweak_update_area(tc);
1173             ret = TRUE;
1174             break;
1175         case GDK_End:
1176         case GDK_KP_End:
1177             tc->width = 1.0;
1178             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1179             sp_tweak_update_area(tc);
1180             ret = TRUE;
1181             break;
1182         case GDK_x:
1183         case GDK_X:
1184             if (MOD__ALT_ONLY) {
1185                 desktop->setToolboxFocusTo ("altx-tweak");
1186                 ret = TRUE;
1187             }
1188             break;
1190         case GDK_Control_L:
1191         case GDK_Control_R:
1192             if (MOD__SHIFT) {
1193                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1194             } else {
1195                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1196             }
1197             break;
1198         case GDK_Shift_L:
1199         case GDK_Shift_R:
1200             if (MOD__CTRL) {
1201                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1202             }
1203             break;
1204         default:
1205             break;
1206         }
1207         break;
1209     case GDK_KEY_RELEASE:
1210         switch (get_group0_keyval(&event->key)) {
1211             case GDK_Control_L:
1212             case GDK_Control_R:
1213                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1214                 tc->_message_context->clear();
1215                 break;
1216             case GDK_Shift_L:
1217             case GDK_Shift_R:
1218                 if (MOD__CTRL) {
1219                     sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1220                 }
1221                 break;
1222             break;
1223             default:
1224                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1225                 break;
1226         }
1228     default:
1229         break;
1230     }
1232     if (!ret) {
1233         if (((SPEventContextClass *) parent_class)->root_handler) {
1234             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1235         }
1236     }
1238     return ret;
1242 /*
1243   Local Variables:
1244   mode:c++
1245   c-file-style:"stroustrup"
1246   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1247   indent-tabs-mode:nil
1248   fill-column:99
1249   End:
1250 */
1251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :