Code

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