Code

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