Code

a34a61f53cc05292f381283338a49b3a2585d584
[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"
25 #include <glib/gmem.h>
26 #include "macros.h"
27 #include "document.h"
28 #include "selection.h"
29 #include "desktop.h"
30 #include "desktop-events.h"
31 #include "desktop-handles.h"
32 #include "desktop-style.h"
33 #include "message-context.h"
34 #include "pixmaps/cursor-tweak-move.xpm"
35 #include "pixmaps/cursor-thin.xpm"
36 #include "pixmaps/cursor-thicken.xpm"
37 #include "pixmaps/cursor-attract.xpm"
38 #include "pixmaps/cursor-repel.xpm"
39 #include "pixmaps/cursor-push.xpm"
40 #include "pixmaps/cursor-roughen.xpm"
41 #include "pixmaps/cursor-color.xpm"
42 #include <boost/optional.hpp>
43 #include "libnr/nr-matrix-ops.h"
44 #include "libnr/nr-scale-translate-ops.h"
45 #include "xml/repr.h"
46 #include "context-fns.h"
47 #include "sp-item.h"
48 #include "inkscape.h"
49 #include "color.h"
50 #include "svg/svg-color.h"
51 #include "splivarot.h"
52 #include "sp-item-group.h"
53 #include "sp-shape.h"
54 #include "sp-path.h"
55 #include "path-chemistry.h"
56 #include "sp-gradient.h"
57 #include "sp-stop.h"
58 #include "sp-gradient-reference.h"
59 #include "sp-linear-gradient.h"
60 #include "sp-radial-gradient.h"
61 #include "gradient-chemistry.h"
62 #include "sp-text.h"
63 #include "sp-flowtext.h"
64 #include "display/canvas-bpath.h"
65 #include "display/canvas-arena.h"
66 #include "display/curve.h"
67 #include "livarot/Shape.h"
68 #include <2geom/isnan.h>
69 #include <2geom/transforms.h>
70 #include "preferences.h"
71 #include "style.h"
72 #include "box3d.h"
73 #include "sp-item-transform.h"
74 #include "filter-chemistry.h"
75 #include "sp-gaussian-blur-fns.h"
76 #include "sp-gaussian-blur.h"
78 #include "tweak-context.h"
80 #define DDC_RED_RGBA 0xff0000ff
82 #define DYNA_MIN_WIDTH 1.0e-6
84 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
85 static void sp_tweak_context_init(SPTweakContext *ddc);
86 static void sp_tweak_context_dispose(GObject *object);
88 static void sp_tweak_context_setup(SPEventContext *ec);
89 static void sp_tweak_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
90 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
92 static SPEventContextClass *parent_class;
94 GtkType
95 sp_tweak_context_get_type(void)
96 {
97     static GType type = 0;
98     if (!type) {
99         GTypeInfo info = {
100             sizeof(SPTweakContextClass),
101             NULL, NULL,
102             (GClassInitFunc) sp_tweak_context_class_init,
103             NULL, NULL,
104             sizeof(SPTweakContext),
105             4,
106             (GInstanceInitFunc) sp_tweak_context_init,
107             NULL,   /* value_table */
108         };
109         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
110     }
111     return type;
114 static void
115 sp_tweak_context_class_init(SPTweakContextClass *klass)
117     GObjectClass *object_class = (GObjectClass *) klass;
118     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
120     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
122     object_class->dispose = sp_tweak_context_dispose;
124     event_context_class->setup = sp_tweak_context_setup;
125     event_context_class->set = sp_tweak_context_set;
126     event_context_class->root_handler = sp_tweak_context_root_handler;
129 static void
130 sp_tweak_context_init(SPTweakContext *tc)
132     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
134     event_context->cursor_shape = cursor_push_xpm;
135     event_context->hot_x = 4;
136     event_context->hot_y = 4;
138     /* attributes */
139     tc->dragging = FALSE;
141     tc->width = 0.2;
142     tc->force = 0.2;
143     tc->pressure = TC_DEFAULT_PRESSURE;
145     tc->is_dilating = false;
146     tc->has_dilated = false;
148     tc->do_h = true;
149     tc->do_s = true;
150     tc->do_l = true;
151     tc->do_o = false;
153     new (&tc->style_set_connection) sigc::connection();
156 static void
157 sp_tweak_context_dispose(GObject *object)
159     SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
161     tc->style_set_connection.disconnect();
162     tc->style_set_connection.~connection();
164     if (tc->dilate_area) {
165         gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
166         tc->dilate_area = NULL;
167     }
169     if (tc->_message_context) {
170         delete tc->_message_context;
171     }
173     G_OBJECT_CLASS(parent_class)->dispose(object);
176 bool is_transform_mode (gint mode)
178     return (mode == TWEAK_MODE_MOVE || 
179             mode == TWEAK_MODE_MOVE_IN_OUT || 
180             mode == TWEAK_MODE_MOVE_JITTER || 
181             mode == TWEAK_MODE_SCALE || 
182             mode == TWEAK_MODE_ROTATE || 
183             mode == TWEAK_MODE_MORELESS);
186 bool is_color_mode (gint mode)
188     return (mode == TWEAK_MODE_COLORPAINT || mode == TWEAK_MODE_COLORJITTER || mode == TWEAK_MODE_BLUR);
191 void
192 sp_tweak_update_cursor (SPTweakContext *tc, bool with_shift)
194    SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
195    SPDesktop *desktop = event_context->desktop;
197                 guint num = 0;
198                 gchar *sel_message = NULL;
199                 if (!desktop->selection->isEmpty()) {
200                     num = g_slist_length((GSList *) desktop->selection->itemList());
201                     sel_message = g_strdup_printf(ngettext("<b>%i</b> object selected","<b>%i</b> objects selected",num), num);
202                 } else {
203                     sel_message = g_strdup_printf(_("<b>Nothing</b> selected"));
204                 }
207    switch (tc->mode) {
208        case TWEAK_MODE_MOVE:
209            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>move</b>."), sel_message);
210            event_context->cursor_shape = cursor_tweak_move_xpm;
211            break;
212        case TWEAK_MODE_MOVE_IN_OUT:
213            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>move in</b>; with Shift to <b>move out</b>."), sel_message);
214            event_context->cursor_shape = cursor_tweak_move_xpm;
215            break;
216        case TWEAK_MODE_MOVE_JITTER:
217            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>move randomly</b>."), sel_message);
218            event_context->cursor_shape = cursor_tweak_move_xpm;
219            break;
220        case TWEAK_MODE_SCALE:
221            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>scale down</b>; with Shift to <b>scale up</b>."), sel_message);
222            event_context->cursor_shape = cursor_tweak_move_xpm;
223            break;
224        case TWEAK_MODE_ROTATE:
225            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>rotate clockwise</b>; with Shift, <b>counterclockwise</b>."), sel_message);
226            event_context->cursor_shape = cursor_tweak_move_xpm;
227            break;
228        case TWEAK_MODE_MORELESS:
229            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>duplicate</b>; with Shift, <b>delete</b>."), sel_message);
230            event_context->cursor_shape = cursor_tweak_move_xpm;
231            break;
232        case TWEAK_MODE_PUSH:
233            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>push paths</b>."), sel_message);
234            event_context->cursor_shape = cursor_push_xpm;
235            break;
236        case TWEAK_MODE_SHRINK_GROW:
237            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>inset paths</b>; with Shift to <b>outset</b>."), sel_message);
238            if (with_shift) {
239                event_context->cursor_shape = cursor_thicken_xpm;
240            } else {
241                event_context->cursor_shape = cursor_thin_xpm;
242            }
243            break;
244        case TWEAK_MODE_ATTRACT_REPEL:
245            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>attract paths</b>; with Shift to <b>repel</b>."), sel_message);
246            if (with_shift) {
247                event_context->cursor_shape = cursor_repel_xpm;
248            } else {
249                event_context->cursor_shape = cursor_attract_xpm;
250            }
251            break;
252        case TWEAK_MODE_ROUGHEN:
253            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>roughen paths</b>."), sel_message);
254            event_context->cursor_shape = cursor_roughen_xpm;
255            break;
256        case TWEAK_MODE_COLORPAINT:
257            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>paint objects</b> with color."), sel_message);
258            event_context->cursor_shape = cursor_color_xpm;
259            break;
260        case TWEAK_MODE_COLORJITTER:
261            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>randomize colors</b>."), sel_message);
262            event_context->cursor_shape = cursor_color_xpm;
263            break;
264        case TWEAK_MODE_BLUR:
265            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>increase blur</b>; with Shift to <b>decrease</b>."), sel_message);
266            event_context->cursor_shape = cursor_color_xpm;
267            break;
268    }
269    sp_event_context_update_cursor(event_context);
270    g_free(sel_message);
273 static bool
274 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
276     if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
277         // we cannot store properties with uris
278         css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
279         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
280         prefs->setStyle("/tools/tweak/style", const_cast<SPCSSAttr*>(css));
281         return true;
282     }
283     return false;
287 static void
288 sp_tweak_context_setup(SPEventContext *ec)
290     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
292     if (((SPEventContextClass *) parent_class)->setup)
293         ((SPEventContextClass *) parent_class)->setup(ec);
295     {
296         /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
297         SPCurve *c = new SPCurve();
298         const double C1 = 0.552;
299         c->moveto(-1,0);
300         c->curveto(-1, C1, -C1, 1, 0, 1 );
301         c->curveto(C1, 1, 1, C1, 1, 0 );
302         c->curveto(1, -C1, C1, -1, 0, -1 );
303         c->curveto(-C1, -1, -1, -C1, -1, 0 );
304         c->closepath();
305         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
306         c->unref();
307         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
308         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
309         sp_canvas_item_hide(tc->dilate_area);
310     }
312     tc->is_drawing = false;
314     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
316     sp_event_context_read(ec, "width");
317     sp_event_context_read(ec, "mode");
318     sp_event_context_read(ec, "fidelity");
319     sp_event_context_read(ec, "force");
320     sp_event_context_read(ec, "usepressure");
321     sp_event_context_read(ec, "doh");
322     sp_event_context_read(ec, "dol");
323     sp_event_context_read(ec, "dos");
324     sp_event_context_read(ec, "doo");
326     tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
327         sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
328     );
329     
330     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
331     if (prefs->getBool("/tools/tweak/selcue")) {
332         ec->enableSelectionCue();
333     }
335     if (prefs->getBool("/tools/tweak/gradientdrag")) {
336         ec->enableGrDrag();
337     }
340 static void
341 sp_tweak_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
343     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
344     Glib::ustring path = val->getEntryName();
346     if (path == "width") {
347         tc->width = CLAMP(val->getDouble(0.1), -1000.0, 1000.0);
348     } else if (path == "mode") {
349         tc->mode = val->getInt();
350         sp_tweak_update_cursor(tc, false);
351     } else if (path == "fidelity") {
352         tc->fidelity = CLAMP(val->getDouble(), 0.0, 1.0);
353     } else if (path == "force") {
354         tc->force = CLAMP(val->getDouble(1.0), 0, 1.0);
355     } else if (path == "usepressure") {
356         tc->usepressure = val->getBool();
357     } else if (path == "doh") {
358         tc->do_h = val->getBool();
359     } else if (path == "dos") {
360         tc->do_s = val->getBool();
361     } else if (path == "dol") {
362         tc->do_l = val->getBool();
363     } else if (path == "doo") {
364         tc->do_o = val->getBool();
365     }
368 static void
369 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
371     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
372         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
373     else
374         tc->pressure = TC_DEFAULT_PRESSURE;
377 double
378 get_dilate_radius (SPTweakContext *tc)
380     // 10 times the pen width:
381     return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
384 double
385 get_path_force (SPTweakContext *tc)
387     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
388         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
389     if (force > 3) {
390         force += 4 * (force - 3);
391     }
392     return force * tc->force;
395 double
396 get_move_force (SPTweakContext *tc)
398     double force = (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE);
399     return force * tc->force;
402 bool
403 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, Geom::Point p, Geom::Point vector, gint mode, double radius, double force, double fidelity, bool reverse)
405     bool did = false;
407     if (SP_IS_BOX3D(item) && !is_transform_mode(mode) && !is_color_mode(mode)) {
408         // convert 3D boxes to ordinary groups before tweaking their shapes
409         item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
410         selection->add(item);
411     }
413     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
414         GSList *items = g_slist_prepend (NULL, item);
415         GSList *selected = NULL;
416         GSList *to_select = NULL;
417         SPDocument *doc = SP_OBJECT_DOCUMENT(item);
418         sp_item_list_to_curves (items, &selected, &to_select);
419         g_slist_free (items);
420         SPObject* newObj = doc->getObjectByRepr((Inkscape::XML::Node *) to_select->data);
421         g_slist_free (to_select);
422         item = (SPItem *) newObj;
423         selection->add(item);
424     }
426     if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item)) {
427         GSList *children = NULL;
428         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
429             if (SP_IS_ITEM(child)) {
430                 children = g_slist_prepend(children, child);
431             }
432         }
434         for (GSList *i = children; i; i = i->next) {
435             SPItem *child = SP_ITEM(i->data);
436             if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity, reverse))
437                 did = true;
438         }
440         g_slist_free(children);
442     } else {
443         if (mode == TWEAK_MODE_MOVE) {
445             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
446             if (a) {
447                 double x = Geom::L2(a->midpoint() - p)/radius;
448                 if (a->contains(p)) x = 0;
449                 if (x < 1) {
450                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * vector;
451                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], move[Geom::Y]));
452                     did = true;
453                 }
454             }
456         } else if (mode == TWEAK_MODE_MOVE_IN_OUT) {
458             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
459             if (a) {
460                 double x = Geom::L2(a->midpoint() - p)/radius;
461                 if (a->contains(p)) x = 0;
462                 if (x < 1) {
463                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * 
464                         (reverse? (a->midpoint() - p) : (p - a->midpoint()));
465                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], move[Geom::Y]));
466                     did = true;
467                 }
468             }
470         } else if (mode == TWEAK_MODE_MOVE_JITTER) {
472             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
473             if (a) {
474                 double dp = g_random_double_range(0, M_PI*2);
475                 double dr = g_random_double_range(0, radius);
476                 double x = Geom::L2(a->midpoint() - p)/radius;
477                 if (a->contains(p)) x = 0;
478                 if (x < 1) {
479                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * Geom::Point(cos(dp)*dr, sin(dp)*dr);
480                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], move[Geom::Y]));
481                     did = true;
482                 }
483             }
485         } else if (mode == TWEAK_MODE_SCALE) {
487             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
488             if (a) {
489                 double x = Geom::L2(a->midpoint() - p)/radius;
490                 if (a->contains(p)) x = 0;
491                 if (x < 1) {
492                     double scale = 1 + (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1);
493                     sp_item_scale_rel(item, Geom::Scale(scale, scale));
494                     did = true;
495                 }
496             }
498         } else if (mode == TWEAK_MODE_ROTATE) {
500             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
501             if (a) {
502                 double x = Geom::L2(a->midpoint() - p)/radius;
503                 if (a->contains(p)) x = 0;
504                 if (x < 1) {
505                     double angle = (reverse? -force : force) * 0.05 * (cos(M_PI * x) + 1) * M_PI;
506                     sp_item_rotate_rel(item, Geom::Rotate(angle));
507                     did = true;
508                 }
509             }
511         } else if (mode == TWEAK_MODE_MORELESS) {
513             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
514             if (a) {
515                 double x = Geom::L2(a->midpoint() - p)/radius;
516                 if (a->contains(p)) x = 0;
517                 if (x < 1) {
518                     double prob = force * 0.5 * (cos(M_PI * x) + 1);
519                     double chance = g_random_double_range(0, 1);
520                     if (chance <= prob) {
521                         if (reverse) { // delete
522                             sp_object_ref(SP_OBJECT(item), NULL);
523                             SP_OBJECT(item)->deleteObject(true, true);
524                             sp_object_unref(SP_OBJECT(item), NULL);
525                         } else { // duplicate
526                             SPDocument *doc = SP_OBJECT_DOCUMENT(item);
527                             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
528                             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
529                             SPObject *old_obj = doc->getObjectByRepr(old_repr);
530                             Inkscape::XML::Node *parent = old_repr->parent();
531                             Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
532                             parent->appendChild(copy);
533                             SPObject *new_obj = doc->getObjectByRepr(copy);
534                             if (selection->includes(old_obj)) {
535                                 selection->add(new_obj);
536                             }
537                             Inkscape::GC::release(copy);
538                         }
539                         did = true;
540                     }
541                 }
542             }
544         } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item)) {
546         Inkscape::XML::Node *newrepr = NULL;
547         gint pos = 0;
548         Inkscape::XML::Node *parent = NULL;
549         char const *id = NULL;
550         if (!SP_IS_PATH(item)) {
551             newrepr = sp_selected_item_to_curved_repr(item, 0);
552             if (!newrepr)
553                 return false;
555             // remember the position of the item
556             pos = SP_OBJECT_REPR(item)->position();
557             // remember parent
558             parent = SP_OBJECT_REPR(item)->parent();
559             // remember id
560             id = SP_OBJECT_REPR(item)->attribute("id");
561         }
564         // skip those paths whose bboxes are entirely out of reach with our radius
565         Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item));
566         if (bbox) {
567             bbox->expandBy(radius);
568             if (!bbox->contains(p)) {
569                 return false;
570             }
571         }
573         Path *orig = Path_for_item(item, false);
574         if (orig == NULL) {
575             return false;
576         }
578         Path *res = new Path;
579         res->SetBackData(false);
581         Shape *theShape = new Shape;
582         Shape *theRes = new Shape;
583         Geom::Matrix i2doc(sp_item_i2doc_affine(item));
585         orig->ConvertWithBackData((0.08 - (0.07 * fidelity)) / i2doc.descrim()); // default 0.059
586         orig->Fill(theShape, 0);
588         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
589         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
590         if (val && strcmp(val, "nonzero") == 0)
591         {
592             theRes->ConvertToShape(theShape, fill_nonZero);
593         }
594         else if (val && strcmp(val, "evenodd") == 0)
595         {
596             theRes->ConvertToShape(theShape, fill_oddEven);
597         }
598         else
599         {
600             theRes->ConvertToShape(theShape, fill_nonZero);
601         }
603         if (Geom::L2(vector) != 0)
604             vector = 1/Geom::L2(vector) * vector;
606         bool did_this = false;
607         if (mode == TWEAK_MODE_SHRINK_GROW) {
608             if (theShape->MakeTweak(tweak_mode_grow, theRes,
609                                  reverse? force : -force,
610                                  join_straight, 4.0,
611                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
612               did_this = true;
613         } else if (mode == TWEAK_MODE_ATTRACT_REPEL) {
614             if (theShape->MakeTweak(tweak_mode_repel, theRes,
615                                  reverse? force : -force,
616                                  join_straight, 4.0,
617                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0)
618               did_this = true;
619         } else if (mode == TWEAK_MODE_PUSH) {
620             if (theShape->MakeTweak(tweak_mode_push, theRes,
621                                  1.0,
622                                  join_straight, 4.0,
623                                  true, p, force*2*vector, radius, &i2doc) == 0)
624               did_this = true;
625         } else if (mode == TWEAK_MODE_ROUGHEN) {
626             if (theShape->MakeTweak(tweak_mode_roughen, theRes,
627                                  force,
628                                  join_straight, 4.0,
629                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0)
630               did_this = true;
631         }
633         // the rest only makes sense if we actually changed the path
634         if (did_this) {
635             theRes->ConvertToShape(theShape, fill_positive);
637             res->Reset();
638             theRes->ConvertToForme(res);
640             double th_max = (0.6 - 0.59*sqrt(fidelity)) / i2doc.descrim();
641             double threshold = MAX(th_max, th_max*force);
642             res->ConvertEvenLines(threshold);
643             res->Simplify(threshold / (selection->desktop()->current_zoom()));
645             if (newrepr) { // converting to path, need to replace the repr
646                 bool is_selected = selection->includes(item);
647                 if (is_selected)
648                     selection->remove(item);
650                 // It's going to resurrect, so we delete without notifying listeners.
651                 SP_OBJECT(item)->deleteObject(false);
653                 // restore id
654                 newrepr->setAttribute("id", id);
655                 // add the new repr to the parent
656                 parent->appendChild(newrepr);
657                 // move to the saved position
658                 newrepr->setPosition(pos > 0 ? pos : 0);
660                 if (is_selected)
661                     selection->add(newrepr);
662             }
664             if (res->descr_cmd.size() > 1) {
665                 gchar *str = res->svg_dump_path();
666                 if (newrepr) {
667                     newrepr->setAttribute("d", str);
668                 } else {
669                     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
670                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
671                     } else {
672                         SP_OBJECT_REPR(item)->setAttribute("d", str);
673                     }
674                 }
675                 g_free(str);
676             } else {
677                 // TODO: if there's 0 or 1 node left, delete this path altogether
678             }
680             if (newrepr) {
681                 Inkscape::GC::release(newrepr);
682                 newrepr = NULL;
683             }
684         }
686         delete theShape;
687         delete theRes;
688         delete orig;
689         delete res;
691         if (did_this)
692             did = true;
693     }
695     }
697     return did;
700 void
701 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
703     float rgb_g[3];
705     if (!do_h || !do_s || !do_l) {
706         float hsl_g[3];
707         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
708         float hsl_c[3];
709         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
710         if (!do_h)
711             hsl_g[0] = hsl_c[0];
712         if (!do_s)
713             hsl_g[1] = hsl_c[1];
714         if (!do_l)
715             hsl_g[2] = hsl_c[2];
716         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
717     } else {
718         rgb_g[0] = SP_RGBA32_R_F(goal);
719         rgb_g[1] = SP_RGBA32_G_F(goal);
720         rgb_g[2] = SP_RGBA32_B_F(goal);
721     }
723     for (int i = 0; i < 3; i++) {
724         double d = rgb_g[i] - color[i];
725         color[i] += d * force;
726     }
729 void
730 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
732     float hsl_c[3];
733     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
735     if (do_h) {
736         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
737         if (hsl_c[0] > 1)
738             hsl_c[0] -= 1;
739         if (hsl_c[0] < 0)
740             hsl_c[0] += 1;
741     }
742     if (do_s) {
743         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
744     }
745     if (do_l) {
746         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
747     }
749     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
752 void
753 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
755     if (mode == TWEAK_MODE_COLORPAINT) {
756         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
757     } else if (mode == TWEAK_MODE_COLORJITTER) {
758         tweak_colorjitter (color, force, do_h, do_s, do_l);
759     }
762 void
763 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
765     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
767     if (mode == TWEAK_MODE_COLORPAINT) {
768         double d = opacity_goal - opacity;
769         opacity += d * force;
770     } else if (mode == TWEAK_MODE_COLORJITTER) {
771         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
772     }
774     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
778 double
779 tweak_profile (double dist, double radius)
781     if (radius == 0)
782         return 0;
783     double x = dist / radius;
784     double alpha = 1;
785     if (x >= 1) {
786         return 0;
787     } else if (x <= 0) {
788         return 1;
789     } else {
790         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
791     }
794 void
795 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
796                           guint32 const rgb_goal, Geom::Point p_w, double radius, double force, guint mode,
797                           bool do_h, bool do_s, bool do_l, bool /*do_o*/)
799     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
801     if (!gradient || !SP_IS_GRADIENT(gradient))
802         return;
804     Geom::Matrix i2d (sp_item_i2doc_affine (item));
805     Geom::Point p = p_w * i2d.inverse();
806     p *= (gradient->gradientTransform).inverse();
807     // now p is in gradient's original coordinates
809     double pos = 0;
810     double r = 0;
811     if (SP_IS_LINEARGRADIENT(gradient)) {
812         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
814         Geom::Point p1(lg->x1.computed, lg->y1.computed);
815         Geom::Point p2(lg->x2.computed, lg->y2.computed);
816         Geom::Point pdiff(p2 - p1);
817         double vl = Geom::L2(pdiff);
819         // This is the matrix which moves and rotates the gradient line
820         // so it's oriented along the X axis:
821         Geom::Matrix norm = Geom::Matrix(Geom::Translate(-p1)) * Geom::Matrix(Geom::Rotate(-atan2(pdiff[Geom::Y], pdiff[Geom::X])));
823         // Transform the mouse point by it to find out its projection onto the gradient line:
824         Geom::Point pnorm = p * norm;
826         // Scale its X coordinate to match the length of the gradient line:
827         pos = pnorm[Geom::X] / vl;
828         // Calculate radius in lenfth-of-gradient-line units
829         r = radius / vl;
831     } else if (SP_IS_RADIALGRADIENT(gradient)) {
832         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
833         Geom::Point c (rg->cx.computed, rg->cy.computed);
834         pos = Geom::L2(p - c) / rg->r.computed;
835         r = radius / rg->r.computed;
836     }
838     // Normalize pos to 0..1, taking into accound gradient spread:
839     double pos_e = pos;
840     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
841         if (pos > 1)
842             pos_e = 1;
843         if (pos < 0)
844             pos_e = 0;
845     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
846         if (pos > 1 || pos < 0)
847             pos_e = pos - floor(pos);
848     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
849         if (pos > 1 || pos < 0) {
850             bool odd = ((int)(floor(pos)) % 2 == 1);
851             pos_e = pos - floor(pos);
852             if (odd)
853                 pos_e = 1 - pos_e;
854         }
855     }
857     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
859     double offset_l = 0;
860     double offset_h = 0;
861     SPObject *child_prev = NULL;
862     for (SPObject *child = sp_object_first_child(vector);
863          child != NULL; child = SP_OBJECT_NEXT(child)) {
864         if (!SP_IS_STOP(child))
865             continue;
866         SPStop *stop = SP_STOP (child);
868         offset_h = stop->offset;
870         if (child_prev) {
872             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
873                 // the summit falls in this interstop, and the radius is small,
874                 // so it only affects the ends of this interstop;
875                 // distribute the force between the two endstops so that they
876                 // get all the painting even if they are not touched by the brush
877                 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
878                                   force * (pos_e - offset_l) / (offset_h - offset_l),
879                                   do_h, do_s, do_l);
880                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
881                                   force * (offset_h - pos_e) / (offset_h - offset_l),
882                                   do_h, do_s, do_l);
883                 SP_OBJECT(stop)->updateRepr();
884                 child_prev->updateRepr();
885                 break;
886             } else {
887                 // wide brush, may affect more than 2 stops,
888                 // paint each stop by the force from the profile curve
889                 if (offset_l <= pos_e && offset_l > pos_e - r) {
890                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
891                                  force * tweak_profile (fabs (pos_e - offset_l), r),
892                                  do_h, do_s, do_l);
893                     child_prev->updateRepr();
894                 }
896                 if (offset_h >= pos_e && offset_h < pos_e + r) {
897                     tweak_color (mode, stop->specified_color.v.c, rgb_goal,
898                                  force * tweak_profile (fabs (pos_e - offset_h), r),
899                                  do_h, do_s, do_l);
900                     SP_OBJECT(stop)->updateRepr();
901                 }
902             }
903         }
905         offset_l = offset_h;
906         child_prev = child;
907     }
910 bool
911 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
912                           guint32 fill_goal, bool do_fill,
913                           guint32 stroke_goal, bool do_stroke,
914                           float opacity_goal, bool do_opacity,
915                           bool do_blur, bool reverse,
916                           Geom::Point p, double radius, double force,
917                           bool do_h, bool do_s, bool do_l, bool do_o)
919     bool did = false;
921     if (SP_IS_GROUP(item)) {
922         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
923             if (SP_IS_ITEM(child)) {
924                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
925                                           fill_goal, do_fill,
926                                           stroke_goal, do_stroke,
927                                           opacity_goal, do_opacity,
928                                           do_blur, reverse,
929                                           p, radius, force, do_h, do_s, do_l, do_o))
930                     did = true;
931             }
932         }
934     } else {
935         SPStyle *style = SP_OBJECT_STYLE(item);
936         if (!style) {
937             return false;
938         }
939         Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item),
940                                                         SPItem::GEOMETRIC_BBOX);
941         if (!bbox) {
942             return false;
943         }
945         Geom::Rect brush(p - Geom::Point(radius, radius), p + Geom::Point(radius, radius));
947         Geom::Point center = bbox->midpoint();
948         double this_force;
950 // if item == item_at_point, use max force
951         if (item == item_at_point) {
952             this_force = force;
953 // else if no overlap of bbox and brush box, skip:
954         } else if (!bbox->intersects(brush)) {
955             return false;
956 //TODO:
957 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
958         //} else if (bbox->maxExtent() > 3 * radius) {
959         //}
960 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
961 // else if still smaller, then check only the object center:
962         } else {
963             this_force = force * tweak_profile (Geom::L2 (p - center), radius);
964         }
966         if (this_force > 0.002) {
968             if (do_blur) {
969                 Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item),
970                                                         SPItem::GEOMETRIC_BBOX);
971                 if (!bbox) {
972                     return did;
973                 }
975                 double blur_now = 0;
976                 Geom::Matrix i2d = sp_item_i2d_affine (item);
977                 if (style->filter.set && style->getFilter()) {
978                     //cycle through filter primitives
979                     SPObject *primitive_obj = style->getFilter()->children;
980                     while (primitive_obj) {
981                         if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
982                             SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
983                             //if primitive is gaussianblur
984                             if(SP_IS_GAUSSIANBLUR(primitive)) {
985                                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
986                                 float num = spblur->stdDeviation.getNumber();
987                                 blur_now += num * i2d.descrim(); // sum all blurs in the filter
988                             }
989                         }
990                         primitive_obj = primitive_obj->next;
991                     }
992                 }
993                 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y];
994                 blur_now = blur_now / perimeter;
996                 double blur_new;
997                 if (reverse) 
998                     blur_new = blur_now - 0.06 * force;
999                 else
1000                     blur_new = blur_now + 0.06 * force;
1001                 if (blur_new < 0.0005 && blur_new < blur_now) {
1002                     blur_new = 0;
1003                 }
1005                 if (blur_new == 0) {
1006                     remove_filter(item, false);
1007                 } else {
1008                     double radius = blur_new * perimeter;
1009                     SPFilter *filter = modify_filter_gaussian_blur_from_item(SP_OBJECT_DOCUMENT(item), item, radius);
1011                     sp_style_set_property_url(item, "filter", filter, false);
1012                 }
1013                 return true; // do not do colors, blur is a separate mode
1014             }
1016             if (do_fill) {
1017                 if (style->fill.isPaintserver()) {
1018                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1019                     did = true;
1020                 } else if (style->fill.isColor()) {
1021                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
1022                     item->updateRepr();
1023                     did = true;
1024                 }
1025             }
1026             if (do_stroke) {
1027                 if (style->stroke.isPaintserver()) {
1028                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1029                     did = true;
1030                 } else if (style->stroke.isColor()) {
1031                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
1032                     item->updateRepr();
1033                     did = true;
1034                 }
1035             }
1036             if (do_opacity && do_o) {
1037                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
1038             }
1039         }
1040     }
1042     return did;
1046 bool
1047 sp_tweak_dilate (SPTweakContext *tc, Geom::Point event_p, Geom::Point p, Geom::Point vector, bool reverse)
1049     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
1050     SPDesktop *desktop = SP_EVENT_CONTEXT(tc)->desktop;
1052     if (selection->isEmpty()) {
1053         return false;
1054     }
1056     bool did = false;
1057     double radius = get_dilate_radius(tc);
1059     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
1061     bool do_fill = false, do_stroke = false, do_opacity = false;
1062     guint32 fill_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", true, &do_fill);
1063     guint32 stroke_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", false, &do_stroke);
1064     double opacity_goal = sp_desktop_get_master_opacity_tool(desktop, "/tools/tweak", &do_opacity);
1065     if (reverse) {
1066 #if 0
1067         // HSL inversion 
1068         float hsv[3];
1069         float rgb[3];
1070         sp_color_rgb_to_hsv_floatv (hsv, 
1071                                     SP_RGBA32_R_F(fill_goal),
1072                                     SP_RGBA32_G_F(fill_goal),
1073                                     SP_RGBA32_B_F(fill_goal));
1074         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1075         fill_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1076         sp_color_rgb_to_hsv_floatv (hsv, 
1077                                     SP_RGBA32_R_F(stroke_goal),
1078                                     SP_RGBA32_G_F(stroke_goal),
1079                                     SP_RGBA32_B_F(stroke_goal));
1080         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1081         stroke_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1082 #else
1083         // RGB inversion 
1084         fill_goal = SP_RGBA32_U_COMPOSE(
1085             (255 - SP_RGBA32_R_U(fill_goal)),
1086             (255 - SP_RGBA32_G_U(fill_goal)),
1087             (255 - SP_RGBA32_B_U(fill_goal)),
1088             (255 - SP_RGBA32_A_U(fill_goal)));
1089         stroke_goal = SP_RGBA32_U_COMPOSE(
1090             (255 - SP_RGBA32_R_U(stroke_goal)),
1091             (255 - SP_RGBA32_G_U(stroke_goal)),
1092             (255 - SP_RGBA32_B_U(stroke_goal)),
1093             (255 - SP_RGBA32_A_U(stroke_goal)));
1094 #endif
1095         opacity_goal = 1 - opacity_goal;
1096     }
1098     double path_force = get_path_force(tc);
1099     if (radius == 0 || path_force == 0) {
1100         return false;
1101     }
1102     double move_force = get_move_force(tc);
1103     double color_force = MIN(sqrt(path_force)/20.0, 1);
1105     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1106          items != NULL;
1107          items = items->next) {
1109         SPItem *item = (SPItem *) items->data;
1111         if (is_color_mode (tc->mode)) {
1112             if (do_fill || do_stroke || do_opacity) {
1113                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
1114                                           fill_goal, do_fill,
1115                                           stroke_goal, do_stroke,
1116                                           opacity_goal, do_opacity,
1117                                           tc->mode == TWEAK_MODE_BLUR, reverse,
1118                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
1119                 did = true;
1120             }
1121         } else if (is_transform_mode(tc->mode)) {
1122             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, move_force, tc->fidelity, reverse))
1123                 did = true;
1124         } else {
1125             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, path_force, tc->fidelity, reverse))
1126                 did = true;
1127         }
1128     }
1130     return did;
1133 void
1134 sp_tweak_update_area (SPTweakContext *tc)
1136         double radius = get_dilate_radius(tc);
1137         Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
1138         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1139         sp_canvas_item_show(tc->dilate_area);
1142 void
1143 sp_tweak_switch_mode (SPTweakContext *tc, gint mode, bool with_shift)
1145     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1146     // need to set explicitly, because the prefs may not have changed by the previous
1147     tc->mode = mode;
1148     sp_tweak_update_cursor (tc, with_shift);
1151 void
1152 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode, bool with_shift)
1154     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1155    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
1156    gint now_mode = prefs->getInt("/tools/tweak/mode", 0);
1157    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1158    // button has changed prefs, restore
1159    prefs->setInt("/tools/tweak/mode", now_mode);
1160    // changing prefs changed tc->mode, restore back :)
1161    tc->mode = mode;
1162    sp_tweak_update_cursor (tc, with_shift);
1165 gint
1166 sp_tweak_context_root_handler(SPEventContext *event_context,
1167                                   GdkEvent *event)
1169     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
1170     SPDesktop *desktop = event_context->desktop;
1172     gint ret = FALSE;
1174     switch (event->type) {
1175         case GDK_ENTER_NOTIFY:
1176             sp_canvas_item_show(tc->dilate_area);
1177             break;
1178         case GDK_LEAVE_NOTIFY:
1179             sp_canvas_item_hide(tc->dilate_area);
1180             break;
1181         case GDK_BUTTON_PRESS:
1182             if (event->button.button == 1 && !event_context->space_panning) {
1184                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
1185                     return TRUE;
1186                 }
1188                 Geom::Point const button_w(event->button.x,
1189                                          event->button.y);
1190                 Geom::Point const button_dt(desktop->w2d(button_w));
1191                 tc->last_push = desktop->dt2doc(button_dt);
1193                 sp_tweak_extinput(tc, event);
1195                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
1196                 tc->is_drawing = true;
1197                 tc->is_dilating = true;
1198                 tc->has_dilated = false;
1200                 ret = TRUE;
1201             }
1202             break;
1203         case GDK_MOTION_NOTIFY:
1204         {
1205             Geom::Point const motion_w(event->motion.x,
1206                                      event->motion.y);
1207             Geom::Point motion_dt(desktop->w2d(motion_w));
1208             Geom::Point motion_doc(desktop->dt2doc(motion_dt));
1209             sp_tweak_extinput(tc, event);
1211             // draw the dilating cursor
1212                 double radius = get_dilate_radius(tc);
1213                 Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(desktop->w2d(motion_w)));
1214                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1215                 sp_canvas_item_show(tc->dilate_area);
1217                 guint num = 0;
1218                 if (!desktop->selection->isEmpty()) {
1219                     num = g_slist_length((GSList *) desktop->selection->itemList());
1220                 }
1221                 if (num == 0) {
1222                     tc->_message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
1223                 }
1225             // dilating:
1226             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1227                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
1228                 //tc->last_push = motion_doc;
1229                 tc->has_dilated = true;
1230                 // it's slow, so prevent clogging up with events
1231                 gobble_motion_events(GDK_BUTTON1_MASK);
1232                 return TRUE;
1233             }
1235         }
1236         break;
1239     case GDK_BUTTON_RELEASE:
1240     {
1241         Geom::Point const motion_w(event->button.x, event->button.y);
1242         Geom::Point const motion_dt(desktop->w2d(motion_w));
1244         sp_canvas_end_forced_full_redraws(desktop->canvas);
1245         tc->is_drawing = false;
1247         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1248             if (!tc->has_dilated) {
1249                 // if we did not rub, do a light tap
1250                 tc->pressure = 0.03;
1251                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), Geom::Point(0,0), MOD__SHIFT);
1252             }
1253             tc->is_dilating = false;
1254             tc->has_dilated = false;
1255             switch (tc->mode) {
1256                 case TWEAK_MODE_MOVE:
1257                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1258                                      SP_VERB_CONTEXT_TWEAK, _("Move tweak"));
1259                     break;
1260                 case TWEAK_MODE_MOVE_IN_OUT:
1261                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1262                                      SP_VERB_CONTEXT_TWEAK, _("Move in/out tweak"));
1263                     break;
1264                 case TWEAK_MODE_MOVE_JITTER:
1265                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1266                                      SP_VERB_CONTEXT_TWEAK, _("Move jitter tweak"));
1267                     break;
1268                 case TWEAK_MODE_SCALE:
1269                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1270                                      SP_VERB_CONTEXT_TWEAK, _("Scale tweak"));
1271                     break;
1272                 case TWEAK_MODE_ROTATE:
1273                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1274                                      SP_VERB_CONTEXT_TWEAK, _("Rotate tweak"));
1275                     break;
1276                 case TWEAK_MODE_MORELESS:
1277                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1278                                      SP_VERB_CONTEXT_TWEAK, _("Duplicate/delete tweak"));
1279                     break;
1280                 case TWEAK_MODE_PUSH:
1281                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1282                                      SP_VERB_CONTEXT_TWEAK, _("Push path tweak"));
1283                     break;
1284                 case TWEAK_MODE_SHRINK_GROW:
1285                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1286                                      SP_VERB_CONTEXT_TWEAK, _("Shrink/grow path tweak"));
1287                     break;
1288                 case TWEAK_MODE_ATTRACT_REPEL:
1289                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1290                                      SP_VERB_CONTEXT_TWEAK, _("Attract/repel path tweak"));
1291                     break;
1292                 case TWEAK_MODE_ROUGHEN:
1293                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1294                                      SP_VERB_CONTEXT_TWEAK, _("Roughen path tweak"));
1295                     break;
1296                 case TWEAK_MODE_COLORPAINT:
1297                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1298                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1299                     break;
1300                 case TWEAK_MODE_COLORJITTER:
1301                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1302                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1303                     break;
1304                 case TWEAK_MODE_BLUR:
1305                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1306                                      SP_VERB_CONTEXT_TWEAK, _("Blur tweak"));
1307                     break;
1308             }
1309         }
1310         break;
1311     }
1313     case GDK_KEY_PRESS:
1314         switch (get_group0_keyval (&event->key)) {
1315         case GDK_m:
1316         case GDK_M:
1317         case GDK_0:
1318             if (MOD__SHIFT_ONLY) {
1319                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE, MOD__SHIFT);
1320                 ret = TRUE;
1321             }
1322             break;
1323         case GDK_i:
1324         case GDK_I:
1325         case GDK_1:
1326             if (MOD__SHIFT_ONLY) {
1327                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_IN_OUT, MOD__SHIFT);
1328                 ret = TRUE;
1329             }
1330             break;
1331         case GDK_z:
1332         case GDK_Z:
1333         case GDK_2:
1334             if (MOD__SHIFT_ONLY) {
1335                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_JITTER, MOD__SHIFT);
1336                 ret = TRUE;
1337             }
1338             break;
1339         case GDK_less:
1340         case GDK_comma:
1341         case GDK_greater:
1342         case GDK_period:
1343         case GDK_3:
1344             if (MOD__SHIFT_ONLY) {
1345                 sp_tweak_switch_mode(tc, TWEAK_MODE_SCALE, MOD__SHIFT);
1346                 ret = TRUE;
1347             }
1348             break;
1349         case GDK_bracketright:
1350         case GDK_bracketleft:
1351         case GDK_4:
1352             if (MOD__SHIFT_ONLY) {
1353                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROTATE, MOD__SHIFT);
1354                 ret = TRUE;
1355             }
1356             break;
1357         case GDK_d:
1358         case GDK_D:
1359         case GDK_5:
1360             if (MOD__SHIFT_ONLY) {
1361                 sp_tweak_switch_mode(tc, TWEAK_MODE_MORELESS, MOD__SHIFT);
1362                 ret = TRUE;
1363             }
1364             break;
1365         case GDK_p:
1366         case GDK_P:
1367         case GDK_6:
1368             if (MOD__SHIFT_ONLY) {
1369                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH, MOD__SHIFT);
1370                 ret = TRUE;
1371             }
1372             break;
1373         case GDK_s:
1374         case GDK_S:
1375         case GDK_7:
1376             if (MOD__SHIFT_ONLY) {
1377                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1378                 ret = TRUE;
1379             }
1380             break;
1381         case GDK_a:
1382         case GDK_A:
1383         case GDK_8:
1384             if (MOD__SHIFT_ONLY) {
1385                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT_REPEL, MOD__SHIFT);
1386                 ret = TRUE;
1387             }
1388             break;
1389         case GDK_r:
1390         case GDK_R:
1391         case GDK_9:
1392             if (MOD__SHIFT_ONLY) {
1393                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN, MOD__SHIFT);
1394                 ret = TRUE;
1395             }
1396             break;
1397         case GDK_c:
1398         case GDK_C:
1399             if (MOD__SHIFT_ONLY) {
1400                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT, MOD__SHIFT);
1401                 ret = TRUE;
1402             }
1403             break;
1404         case GDK_j:
1405         case GDK_J:
1406             if (MOD__SHIFT_ONLY) {
1407                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER, MOD__SHIFT);
1408                 ret = TRUE;
1409             }
1410             break;
1411         case GDK_b:
1412         case GDK_B:
1413             if (MOD__SHIFT_ONLY) {
1414                 sp_tweak_switch_mode(tc, TWEAK_MODE_BLUR, MOD__SHIFT);
1415                 ret = TRUE;
1416             }
1417             break;
1419         case GDK_Up:
1420         case GDK_KP_Up:
1421             if (!MOD__CTRL_ONLY) {
1422                 tc->force += 0.05;
1423                 if (tc->force > 1.0)
1424                     tc->force = 1.0;
1425                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1426                 ret = TRUE;
1427             }
1428             break;
1429         case GDK_Down:
1430         case GDK_KP_Down:
1431             if (!MOD__CTRL_ONLY) {
1432                 tc->force -= 0.05;
1433                 if (tc->force < 0.0)
1434                     tc->force = 0.0;
1435                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1436                 ret = TRUE;
1437             }
1438             break;
1439         case GDK_Right:
1440         case GDK_KP_Right:
1441             if (!MOD__CTRL_ONLY) {
1442                 tc->width += 0.01;
1443                 if (tc->width > 1.0)
1444                     tc->width = 1.0;
1445                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1446                 sp_tweak_update_area(tc);
1447                 ret = TRUE;
1448             }
1449             break;
1450         case GDK_Left:
1451         case GDK_KP_Left:
1452             if (!MOD__CTRL_ONLY) {
1453                 tc->width -= 0.01;
1454                 if (tc->width < 0.01)
1455                     tc->width = 0.01;
1456                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1457                 sp_tweak_update_area(tc);
1458                 ret = TRUE;
1459             }
1460             break;
1461         case GDK_Home:
1462         case GDK_KP_Home:
1463             tc->width = 0.01;
1464             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1465             sp_tweak_update_area(tc);
1466             ret = TRUE;
1467             break;
1468         case GDK_End:
1469         case GDK_KP_End:
1470             tc->width = 1.0;
1471             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1472             sp_tweak_update_area(tc);
1473             ret = TRUE;
1474             break;
1475         case GDK_x:
1476         case GDK_X:
1477             if (MOD__ALT_ONLY) {
1478                 desktop->setToolboxFocusTo ("altx-tweak");
1479                 ret = TRUE;
1480             }
1481             break;
1483         case GDK_Shift_L:
1484         case GDK_Shift_R:
1485             sp_tweak_update_cursor(tc, true);
1486             break;
1488         case GDK_Control_L:
1489         case GDK_Control_R:
1490             sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1491             break;
1492         default:
1493             break;
1494         }
1495         break;
1497     case GDK_KEY_RELEASE: {
1498         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1499         switch (get_group0_keyval(&event->key)) {
1500             case GDK_Shift_L:
1501             case GDK_Shift_R:
1502                 sp_tweak_update_cursor(tc, false);
1503                 break;
1504             case GDK_Control_L:
1505             case GDK_Control_R:
1506                 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1507                 tc->_message_context->clear();
1508                 break;
1509             default:
1510                 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1511                 break;
1512         }
1513     }
1515     default:
1516         break;
1517     }
1519     if (!ret) {
1520         if (((SPEventContextClass *) parent_class)->root_handler) {
1521             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1522         }
1523     }
1525     return ret;
1529 /*
1530   Local Variables:
1531   mode:c++
1532   c-file-style:"stroustrup"
1533   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1534   indent-tabs-mode:nil
1535   fill-column:99
1536   End:
1537 */
1538 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :