Code

7d797915e7779a30b80db4ecbd9421fdd75f1288
[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         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
424             if (SP_IS_ITEM(child)) {
425                 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity, reverse))
426                     did = true;
427             }
428         }
430     } else {
431         if (mode == TWEAK_MODE_MOVE) {
433             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
434             if (a) {
435                 double x = Geom::L2(a->midpoint() - p)/radius;
436                 if (a->contains(p)) x = 0;
437                 if (x < 1) {
438                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * vector;
439                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
440                     did = true;
441                 }
442             }
444         } else if (mode == TWEAK_MODE_MOVE_IN_OUT) {
446             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
447             if (a) {
448                 double x = Geom::L2(a->midpoint() - p)/radius;
449                 if (a->contains(p)) x = 0;
450                 if (x < 1) {
451                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * 
452                         (reverse? (a->midpoint() - p) : (p - a->midpoint()));
453                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
454                     did = true;
455                 }
456             }
458         } else if (mode == TWEAK_MODE_MOVE_JITTER) {
460             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
461             if (a) {
462                 double dp = g_random_double_range(0, M_PI*2);
463                 double dr = g_random_double_range(0, radius);
464                 double x = Geom::L2(a->midpoint() - p)/radius;
465                 if (a->contains(p)) x = 0;
466                 if (x < 1) {
467                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * Geom::Point(cos(dp)*dr, sin(dp)*dr);
468                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
469                     did = true;
470                 }
471             }
473         } else if (mode == TWEAK_MODE_SCALE) {
475             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
476             if (a) {
477                 double x = Geom::L2(a->midpoint() - p)/radius;
478                 if (a->contains(p)) x = 0;
479                 if (x < 1) {
480                     double scale = 1 + (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1);
481                     sp_item_scale_rel(item, Geom::Scale(scale, scale));
482                     did = true;
483                 }
484             }
486         } else if (mode == TWEAK_MODE_ROTATE) {
488             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
489             if (a) {
490                 double x = Geom::L2(a->midpoint() - p)/radius;
491                 if (a->contains(p)) x = 0;
492                 if (x < 1) {
493                     double angle = (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1) * M_PI;
494                     sp_item_rotate_rel(item, Geom::Rotate(angle));
495                     did = true;
496                 }
497             }
499         } else if (mode == TWEAK_MODE_MORELESS) {
501             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
502             if (a) {
503                 double x = Geom::L2(a->midpoint() - p)/radius;
504                 if (a->contains(p)) x = 0;
505                 if (x < 1) {
506                     double prob = force * 0.5 * (cos(M_PI * x) + 1);
507                     double chance = g_random_double_range(0, 1);
508                     if (chance <= prob) {
509                         if (reverse) { // delete
510                             sp_object_ref(SP_OBJECT(item), NULL);
511                             SP_OBJECT(item)->deleteObject(true, true);
512                             sp_object_unref(SP_OBJECT(item), NULL);
513                         } else { // duplicate
514                             SPDocument *doc = SP_OBJECT_DOCUMENT(item);
515                             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
516                             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
517                             SPObject *old_obj = doc->getObjectByRepr(old_repr);
518                             Inkscape::XML::Node *parent = old_repr->parent();
519                             Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
520                             parent->appendChild(copy);
521                             SPObject *new_obj = doc->getObjectByRepr(copy);
522                             if (selection->includes(old_obj)) {
523                                 selection->add(new_obj);
524                             }
525                             Inkscape::GC::release(copy);
526                         }
527                     }
528                 }
529             }
531         } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item)) {
533         Inkscape::XML::Node *newrepr = NULL;
534         gint pos = 0;
535         Inkscape::XML::Node *parent = NULL;
536         char const *id = NULL;
537         if (!SP_IS_PATH(item)) {
538             newrepr = sp_selected_item_to_curved_repr(item, 0);
539             if (!newrepr)
540                 return false;
542             // remember the position of the item
543             pos = SP_OBJECT_REPR(item)->position();
544             // remember parent
545             parent = SP_OBJECT_REPR(item)->parent();
546             // remember id
547             id = SP_OBJECT_REPR(item)->attribute("id");
548         }
551         // skip those paths whose bboxes are entirely out of reach with our radius
552         Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item));
553         if (bbox) {
554             bbox->expandBy(radius);
555             if (!bbox->contains(p)) {
556                 return false;
557             }
558         }
560         Path *orig = Path_for_item(item, false);
561         if (orig == NULL) {
562             return false;
563         }
565         Path *res = new Path;
566         res->SetBackData(false);
568         Shape *theShape = new Shape;
569         Shape *theRes = new Shape;
570         Geom::Matrix i2doc(sp_item_i2doc_affine(item));
572         orig->ConvertWithBackData((0.08 - (0.07 * fidelity)) / i2doc.descrim()); // default 0.059
573         orig->Fill(theShape, 0);
575         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
576         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
577         if (val && strcmp(val, "nonzero") == 0)
578         {
579             theRes->ConvertToShape(theShape, fill_nonZero);
580         }
581         else if (val && strcmp(val, "evenodd") == 0)
582         {
583             theRes->ConvertToShape(theShape, fill_oddEven);
584         }
585         else
586         {
587             theRes->ConvertToShape(theShape, fill_nonZero);
588         }
590         if (Geom::L2(vector) != 0)
591             vector = 1/Geom::L2(vector) * vector;
593         bool did_this = false;
594         if (mode == TWEAK_MODE_SHRINK_GROW) {
595             if (theShape->MakeTweak(tweak_mode_grow, theRes,
596                                  reverse? force : -force,
597                                  join_straight, 4.0,
598                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
599               did_this = true;
600         } else if (mode == TWEAK_MODE_ATTRACT_REPEL) {
601             if (theShape->MakeTweak(tweak_mode_repel, theRes,
602                                  reverse? force : -force,
603                                  join_straight, 4.0,
604                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0)
605               did_this = true;
606         } else if (mode == TWEAK_MODE_PUSH) {
607             if (theShape->MakeTweak(tweak_mode_push, theRes,
608                                  1.0,
609                                  join_straight, 4.0,
610                                  true, p, force*2*vector, radius, &i2doc) == 0)
611               did_this = true;
612         } else if (mode == TWEAK_MODE_ROUGHEN) {
613             if (theShape->MakeTweak(tweak_mode_roughen, theRes,
614                                  force,
615                                  join_straight, 4.0,
616                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0)
617               did_this = true;
618         }
620         // the rest only makes sense if we actually changed the path
621         if (did_this) {
622             theRes->ConvertToShape(theShape, fill_positive);
624             res->Reset();
625             theRes->ConvertToForme(res);
627             double th_max = (0.6 - 0.59*sqrt(fidelity)) / i2doc.descrim();
628             double threshold = MAX(th_max, th_max*force);
629             res->ConvertEvenLines(threshold);
630             res->Simplify(threshold / (selection->desktop()->current_zoom()));
632             if (newrepr) { // converting to path, need to replace the repr
633                 bool is_selected = selection->includes(item);
634                 if (is_selected)
635                     selection->remove(item);
637                 // It's going to resurrect, so we delete without notifying listeners.
638                 SP_OBJECT(item)->deleteObject(false);
640                 // restore id
641                 newrepr->setAttribute("id", id);
642                 // add the new repr to the parent
643                 parent->appendChild(newrepr);
644                 // move to the saved position
645                 newrepr->setPosition(pos > 0 ? pos : 0);
647                 if (is_selected)
648                     selection->add(newrepr);
649             }
651             if (res->descr_cmd.size() > 1) {
652                 gchar *str = res->svg_dump_path();
653                 if (newrepr) {
654                     newrepr->setAttribute("d", str);
655                 } else {
656                     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
657                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
658                     } else {
659                         SP_OBJECT_REPR(item)->setAttribute("d", str);
660                     }
661                 }
662                 g_free(str);
663             } else {
664                 // TODO: if there's 0 or 1 node left, delete this path altogether
665             }
667             if (newrepr) {
668                 Inkscape::GC::release(newrepr);
669                 newrepr = NULL;
670             }
671         }
673         delete theShape;
674         delete theRes;
675         delete orig;
676         delete res;
678         if (did_this)
679             did = true;
680     }
682     }
684     return did;
687 void
688 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
690     float rgb_g[3];
692     if (!do_h || !do_s || !do_l) {
693         float hsl_g[3];
694         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
695         float hsl_c[3];
696         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
697         if (!do_h)
698             hsl_g[0] = hsl_c[0];
699         if (!do_s)
700             hsl_g[1] = hsl_c[1];
701         if (!do_l)
702             hsl_g[2] = hsl_c[2];
703         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
704     } else {
705         rgb_g[0] = SP_RGBA32_R_F(goal);
706         rgb_g[1] = SP_RGBA32_G_F(goal);
707         rgb_g[2] = SP_RGBA32_B_F(goal);
708     }
710     for (int i = 0; i < 3; i++) {
711         double d = rgb_g[i] - color[i];
712         color[i] += d * force;
713     }
716 void
717 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
719     float hsl_c[3];
720     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
722     if (do_h) {
723         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
724         if (hsl_c[0] > 1)
725             hsl_c[0] -= 1;
726         if (hsl_c[0] < 0)
727             hsl_c[0] += 1;
728     }
729     if (do_s) {
730         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
731     }
732     if (do_l) {
733         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
734     }
736     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
739 void
740 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
742     if (mode == TWEAK_MODE_COLORPAINT) {
743         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
744     } else if (mode == TWEAK_MODE_COLORJITTER) {
745         tweak_colorjitter (color, force, do_h, do_s, do_l);
746     }
749 void
750 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
752     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
754     if (mode == TWEAK_MODE_COLORPAINT) {
755         double d = opacity_goal - opacity;
756         opacity += d * force;
757     } else if (mode == TWEAK_MODE_COLORJITTER) {
758         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
759     }
761     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
765 double
766 tweak_profile (double dist, double radius)
768     if (radius == 0)
769         return 0;
770     double x = dist / radius;
771     double alpha = 1;
772     if (x >= 1) {
773         return 0;
774     } else if (x <= 0) {
775         return 1;
776     } else {
777         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
778     }
781 void
782 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
783                           guint32 const rgb_goal, Geom::Point p_w, double radius, double force, guint mode,
784                           bool do_h, bool do_s, bool do_l, bool /*do_o*/)
786     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
788     if (!gradient || !SP_IS_GRADIENT(gradient))
789         return;
791     Geom::Matrix i2d (sp_item_i2doc_affine (item));
792     Geom::Point p = p_w * i2d.inverse();
793     p *= (gradient->gradientTransform).inverse();
794     // now p is in gradient's original coordinates
796     double pos = 0;
797     double r = 0;
798     if (SP_IS_LINEARGRADIENT(gradient)) {
799         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
801         Geom::Point p1(lg->x1.computed, lg->y1.computed);
802         Geom::Point p2(lg->x2.computed, lg->y2.computed);
803         Geom::Point pdiff(p2 - p1);
804         double vl = Geom::L2(pdiff);
806         // This is the matrix which moves and rotates the gradient line
807         // so it's oriented along the X axis:
808         Geom::Matrix norm = Geom::Matrix(Geom::Translate(-p1)) * Geom::Matrix(Geom::Rotate(-atan2(pdiff[Geom::Y], pdiff[Geom::X])));
810         // Transform the mouse point by it to find out its projection onto the gradient line:
811         Geom::Point pnorm = p * norm;
813         // Scale its X coordinate to match the length of the gradient line:
814         pos = pnorm[Geom::X] / vl;
815         // Calculate radius in lenfth-of-gradient-line units
816         r = radius / vl;
818     } else if (SP_IS_RADIALGRADIENT(gradient)) {
819         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
820         Geom::Point c (rg->cx.computed, rg->cy.computed);
821         pos = Geom::L2(p - c) / rg->r.computed;
822         r = radius / rg->r.computed;
823     }
825     // Normalize pos to 0..1, taking into accound gradient spread:
826     double pos_e = pos;
827     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
828         if (pos > 1)
829             pos_e = 1;
830         if (pos < 0)
831             pos_e = 0;
832     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
833         if (pos > 1 || pos < 0)
834             pos_e = pos - floor(pos);
835     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
836         if (pos > 1 || pos < 0) {
837             bool odd = ((int)(floor(pos)) % 2 == 1);
838             pos_e = pos - floor(pos);
839             if (odd)
840                 pos_e = 1 - pos_e;
841         }
842     }
844     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
846     double offset_l = 0;
847     double offset_h = 0;
848     SPObject *child_prev = NULL;
849     for (SPObject *child = sp_object_first_child(vector);
850          child != NULL; child = SP_OBJECT_NEXT(child)) {
851         if (!SP_IS_STOP(child))
852             continue;
853         SPStop *stop = SP_STOP (child);
855         offset_h = stop->offset;
857         if (child_prev) {
859             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
860                 // the summit falls in this interstop, and the radius is small,
861                 // so it only affects the ends of this interstop;
862                 // distribute the force between the two endstops so that they
863                 // get all the painting even if they are not touched by the brush
864                 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
865                                   force * (pos_e - offset_l) / (offset_h - offset_l),
866                                   do_h, do_s, do_l);
867                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
868                                   force * (offset_h - pos_e) / (offset_h - offset_l),
869                                   do_h, do_s, do_l);
870                 SP_OBJECT(stop)->updateRepr();
871                 child_prev->updateRepr();
872                 break;
873             } else {
874                 // wide brush, may affect more than 2 stops,
875                 // paint each stop by the force from the profile curve
876                 if (offset_l <= pos_e && offset_l > pos_e - r) {
877                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
878                                  force * tweak_profile (fabs (pos_e - offset_l), r),
879                                  do_h, do_s, do_l);
880                     child_prev->updateRepr();
881                 }
883                 if (offset_h >= pos_e && offset_h < pos_e + r) {
884                     tweak_color (mode, stop->specified_color.v.c, rgb_goal,
885                                  force * tweak_profile (fabs (pos_e - offset_h), r),
886                                  do_h, do_s, do_l);
887                     SP_OBJECT(stop)->updateRepr();
888                 }
889             }
890         }
892         offset_l = offset_h;
893         child_prev = child;
894     }
897 bool
898 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
899                           guint32 fill_goal, bool do_fill,
900                           guint32 stroke_goal, bool do_stroke,
901                           float opacity_goal, bool do_opacity,
902                           bool do_blur, bool reverse,
903                           Geom::Point p, double radius, double force,
904                           bool do_h, bool do_s, bool do_l, bool do_o)
906     bool did = false;
908     if (SP_IS_GROUP(item)) {
909         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
910             if (SP_IS_ITEM(child)) {
911                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
912                                           fill_goal, do_fill,
913                                           stroke_goal, do_stroke,
914                                           opacity_goal, do_opacity,
915                                           do_blur, reverse,
916                                           p, radius, force, do_h, do_s, do_l, do_o))
917                     did = true;
918             }
919         }
921     } else {
922         SPStyle *style = SP_OBJECT_STYLE(item);
923         if (!style) {
924             return false;
925         }
926         Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item),
927                                                         SPItem::GEOMETRIC_BBOX);
928         if (!bbox) {
929             return false;
930         }
932         Geom::Rect brush(p - Geom::Point(radius, radius), p + Geom::Point(radius, radius));
934         Geom::Point center = bbox->midpoint();
935         double this_force;
937 // if item == item_at_point, use max force
938         if (item == item_at_point) {
939             this_force = force;
940 // else if no overlap of bbox and brush box, skip:
941         } else if (!bbox->intersects(brush)) {
942             return false;
943 //TODO:
944 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
945         //} else if (bbox->maxExtent() > 3 * radius) {
946         //}
947 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
948 // else if still smaller, then check only the object center:
949         } else {
950             this_force = force * tweak_profile (Geom::L2 (p - center), radius);
951         }
953         if (this_force > 0.002) {
955             if (do_blur) {
956                 Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item),
957                                                         SPItem::GEOMETRIC_BBOX);
958                 if (!bbox) {
959                     return did;
960                 }
962                 double blur_now = 0;
963                 Geom::Matrix i2d = sp_item_i2d_affine (item);
964                 if (style->filter.set && style->getFilter()) {
965                     //cycle through filter primitives
966                     SPObject *primitive_obj = style->getFilter()->children;
967                     while (primitive_obj) {
968                         if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
969                             SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
970                             //if primitive is gaussianblur
971                             if(SP_IS_GAUSSIANBLUR(primitive)) {
972                                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
973                                 float num = spblur->stdDeviation.getNumber();
974                                 blur_now += num * i2d.descrim(); // sum all blurs in the filter
975                             }
976                         }
977                         primitive_obj = primitive_obj->next;
978                     }
979                 }
980                 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y];
981                 blur_now = blur_now / perimeter;
983                 double blur_new;
984                 if (reverse) 
985                     blur_new = blur_now - 0.06 * force;
986                 else
987                     blur_new = blur_now + 0.06 * force;
988                 if (blur_new < 0.0005 && blur_new < blur_now) {
989                     blur_new = 0;
990                 }
992                 if (blur_new == 0) {
993                     remove_filter(item, false);
994                 } else {
995                     double radius = blur_new * perimeter;
996                     SPFilter *filter = modify_filter_gaussian_blur_from_item(SP_OBJECT_DOCUMENT(item), item, radius);
998                     sp_style_set_property_url(item, "filter", filter, false);
999                 }
1000                 return true; // do not do colors, blur is a separate mode
1001             }
1003             if (do_fill) {
1004                 if (style->fill.isPaintserver()) {
1005                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1006                     did = true;
1007                 } else if (style->fill.isColor()) {
1008                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
1009                     item->updateRepr();
1010                     did = true;
1011                 }
1012             }
1013             if (do_stroke) {
1014                 if (style->stroke.isPaintserver()) {
1015                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1016                     did = true;
1017                 } else if (style->stroke.isColor()) {
1018                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
1019                     item->updateRepr();
1020                     did = true;
1021                 }
1022             }
1023             if (do_opacity && do_o) {
1024                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
1025             }
1026         }
1027     }
1029     return did;
1033 bool
1034 sp_tweak_dilate (SPTweakContext *tc, Geom::Point event_p, Geom::Point p, Geom::Point vector, bool reverse)
1036     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
1037     SPDesktop *desktop = SP_EVENT_CONTEXT(tc)->desktop;
1039     if (selection->isEmpty()) {
1040         return false;
1041     }
1043     bool did = false;
1044     double radius = get_dilate_radius(tc);
1046     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
1048     bool do_fill = false, do_stroke = false, do_opacity = false;
1049     guint32 fill_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", true, &do_fill);
1050     guint32 stroke_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", false, &do_stroke);
1051     double opacity_goal = sp_desktop_get_master_opacity_tool(desktop, "/tools/tweak", &do_opacity);
1052     if (reverse) {
1053 #if 0
1054         // HSL inversion 
1055         float hsv[3];
1056         float rgb[3];
1057         sp_color_rgb_to_hsv_floatv (hsv, 
1058                                     SP_RGBA32_R_F(fill_goal),
1059                                     SP_RGBA32_G_F(fill_goal),
1060                                     SP_RGBA32_B_F(fill_goal));
1061         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1062         fill_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1063         sp_color_rgb_to_hsv_floatv (hsv, 
1064                                     SP_RGBA32_R_F(stroke_goal),
1065                                     SP_RGBA32_G_F(stroke_goal),
1066                                     SP_RGBA32_B_F(stroke_goal));
1067         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1068         stroke_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1069 #else
1070         // RGB inversion 
1071         fill_goal = SP_RGBA32_U_COMPOSE(
1072             (255 - SP_RGBA32_R_U(fill_goal)),
1073             (255 - SP_RGBA32_G_U(fill_goal)),
1074             (255 - SP_RGBA32_B_U(fill_goal)),
1075             (255 - SP_RGBA32_A_U(fill_goal)));
1076         stroke_goal = SP_RGBA32_U_COMPOSE(
1077             (255 - SP_RGBA32_R_U(stroke_goal)),
1078             (255 - SP_RGBA32_G_U(stroke_goal)),
1079             (255 - SP_RGBA32_B_U(stroke_goal)),
1080             (255 - SP_RGBA32_A_U(stroke_goal)));
1081 #endif
1082         opacity_goal = 1 - opacity_goal;
1083     }
1085     double path_force = get_path_force(tc);
1086     if (radius == 0 || path_force == 0) {
1087         return false;
1088     }
1089     double move_force = get_move_force(tc);
1090     double color_force = MIN(sqrt(path_force)/20.0, 1);
1092     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1093          items != NULL;
1094          items = items->next) {
1096         SPItem *item = (SPItem *) items->data;
1098         if (is_color_mode (tc->mode)) {
1099             if (do_fill || do_stroke || do_opacity) {
1100                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
1101                                           fill_goal, do_fill,
1102                                           stroke_goal, do_stroke,
1103                                           opacity_goal, do_opacity,
1104                                           tc->mode == TWEAK_MODE_BLUR, reverse,
1105                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
1106                 did = true;
1107             }
1108         } else if (is_transform_mode(tc->mode)) {
1109             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, move_force, tc->fidelity, reverse))
1110                 did = true;
1111         } else {
1112             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, path_force, tc->fidelity, reverse))
1113                 did = true;
1114         }
1115     }
1117     return did;
1120 void
1121 sp_tweak_update_area (SPTweakContext *tc)
1123         double radius = get_dilate_radius(tc);
1124         Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
1125         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1126         sp_canvas_item_show(tc->dilate_area);
1129 void
1130 sp_tweak_switch_mode (SPTweakContext *tc, gint mode, bool with_shift)
1132     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1133     // need to set explicitly, because the prefs may not have changed by the previous
1134     tc->mode = mode;
1135     sp_tweak_update_cursor (tc, with_shift);
1138 void
1139 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode, bool with_shift)
1141     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1142    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
1143    gint now_mode = prefs->getInt("/tools/tweak/mode", 0);
1144    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1145    // button has changed prefs, restore
1146    prefs->setInt("/tools/tweak/mode", now_mode);
1147    // changing prefs changed tc->mode, restore back :)
1148    tc->mode = mode;
1149    sp_tweak_update_cursor (tc, with_shift);
1152 gint
1153 sp_tweak_context_root_handler(SPEventContext *event_context,
1154                                   GdkEvent *event)
1156     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
1157     SPDesktop *desktop = event_context->desktop;
1159     gint ret = FALSE;
1161     switch (event->type) {
1162         case GDK_ENTER_NOTIFY:
1163             sp_canvas_item_show(tc->dilate_area);
1164             break;
1165         case GDK_LEAVE_NOTIFY:
1166             sp_canvas_item_hide(tc->dilate_area);
1167             break;
1168         case GDK_BUTTON_PRESS:
1169             if (event->button.button == 1 && !event_context->space_panning) {
1171                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
1172                     return TRUE;
1173                 }
1175                 Geom::Point const button_w(event->button.x,
1176                                          event->button.y);
1177                 Geom::Point const button_dt(desktop->w2d(button_w));
1178                 tc->last_push = desktop->dt2doc(button_dt);
1180                 sp_tweak_extinput(tc, event);
1182                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
1183                 tc->is_drawing = true;
1184                 tc->is_dilating = true;
1185                 tc->has_dilated = false;
1187                 ret = TRUE;
1188             }
1189             break;
1190         case GDK_MOTION_NOTIFY:
1191         {
1192             Geom::Point const motion_w(event->motion.x,
1193                                      event->motion.y);
1194             Geom::Point motion_dt(desktop->w2d(motion_w));
1195             Geom::Point motion_doc(desktop->dt2doc(motion_dt));
1196             sp_tweak_extinput(tc, event);
1198             // draw the dilating cursor
1199                 double radius = get_dilate_radius(tc);
1200                 Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(desktop->w2d(motion_w)));
1201                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1202                 sp_canvas_item_show(tc->dilate_area);
1204                 guint num = 0;
1205                 if (!desktop->selection->isEmpty()) {
1206                     num = g_slist_length((GSList *) desktop->selection->itemList());
1207                 }
1208                 if (num == 0) {
1209                     tc->_message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
1210                 }
1212             // dilating:
1213             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1214                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
1215                 //tc->last_push = motion_doc;
1216                 tc->has_dilated = true;
1217                 // it's slow, so prevent clogging up with events
1218                 gobble_motion_events(GDK_BUTTON1_MASK);
1219                 return TRUE;
1220             }
1222         }
1223         break;
1226     case GDK_BUTTON_RELEASE:
1227     {
1228         Geom::Point const motion_w(event->button.x, event->button.y);
1229         Geom::Point const motion_dt(desktop->w2d(motion_w));
1231         sp_canvas_end_forced_full_redraws(desktop->canvas);
1232         tc->is_drawing = false;
1234         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1235             if (!tc->has_dilated) {
1236                 // if we did not rub, do a light tap
1237                 tc->pressure = 0.03;
1238                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), Geom::Point(0,0), MOD__SHIFT);
1239             }
1240             tc->is_dilating = false;
1241             tc->has_dilated = false;
1242             switch (tc->mode) {
1243                 case TWEAK_MODE_MOVE:
1244                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1245                                      SP_VERB_CONTEXT_TWEAK, _("Move tweak"));
1246                     break;
1247                 case TWEAK_MODE_MOVE_IN_OUT:
1248                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1249                                      SP_VERB_CONTEXT_TWEAK, _("Move in/out tweak"));
1250                     break;
1251                 case TWEAK_MODE_MOVE_JITTER:
1252                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1253                                      SP_VERB_CONTEXT_TWEAK, _("Move jitter tweak"));
1254                     break;
1255                 case TWEAK_MODE_SCALE:
1256                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1257                                      SP_VERB_CONTEXT_TWEAK, _("Scale tweak"));
1258                     break;
1259                 case TWEAK_MODE_ROTATE:
1260                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1261                                      SP_VERB_CONTEXT_TWEAK, _("Rotate tweak"));
1262                     break;
1263                 case TWEAK_MODE_MORELESS:
1264                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1265                                      SP_VERB_CONTEXT_TWEAK, _("Duplicate/delete tweak"));
1266                     break;
1267                 case TWEAK_MODE_PUSH:
1268                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1269                                      SP_VERB_CONTEXT_TWEAK, _("Push path tweak"));
1270                     break;
1271                 case TWEAK_MODE_SHRINK_GROW:
1272                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1273                                      SP_VERB_CONTEXT_TWEAK, _("Shrink/grow path tweak"));
1274                     break;
1275                 case TWEAK_MODE_ATTRACT_REPEL:
1276                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1277                                      SP_VERB_CONTEXT_TWEAK, _("Attract/repel path tweak"));
1278                     break;
1279                 case TWEAK_MODE_ROUGHEN:
1280                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1281                                      SP_VERB_CONTEXT_TWEAK, _("Roughen path tweak"));
1282                     break;
1283                 case TWEAK_MODE_COLORPAINT:
1284                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1285                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1286                     break;
1287                 case TWEAK_MODE_COLORJITTER:
1288                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1289                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1290                     break;
1291                 case TWEAK_MODE_BLUR:
1292                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1293                                      SP_VERB_CONTEXT_TWEAK, _("Blur tweak"));
1294                     break;
1295             }
1296         }
1297         break;
1298     }
1300     case GDK_KEY_PRESS:
1301         switch (get_group0_keyval (&event->key)) {
1302         case GDK_m:
1303         case GDK_M:
1304         case GDK_0:
1305             if (MOD__SHIFT_ONLY) {
1306                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE, MOD__SHIFT);
1307                 ret = TRUE;
1308             }
1309             break;
1310         case GDK_i:
1311         case GDK_I:
1312         case GDK_1:
1313             if (MOD__SHIFT_ONLY) {
1314                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_IN_OUT, MOD__SHIFT);
1315                 ret = TRUE;
1316             }
1317             break;
1318         case GDK_z:
1319         case GDK_Z:
1320         case GDK_2:
1321             if (MOD__SHIFT_ONLY) {
1322                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_JITTER, MOD__SHIFT);
1323                 ret = TRUE;
1324             }
1325             break;
1326         case GDK_less:
1327         case GDK_comma:
1328         case GDK_greater:
1329         case GDK_period:
1330         case GDK_3:
1331             if (MOD__SHIFT_ONLY) {
1332                 sp_tweak_switch_mode(tc, TWEAK_MODE_SCALE, MOD__SHIFT);
1333                 ret = TRUE;
1334             }
1335             break;
1336         case GDK_bracketright:
1337         case GDK_bracketleft:
1338         case GDK_4:
1339             if (MOD__SHIFT_ONLY) {
1340                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROTATE, MOD__SHIFT);
1341                 ret = TRUE;
1342             }
1343             break;
1344         case GDK_d:
1345         case GDK_D:
1346         case GDK_5:
1347             if (MOD__SHIFT_ONLY) {
1348                 sp_tweak_switch_mode(tc, TWEAK_MODE_MORELESS, MOD__SHIFT);
1349                 ret = TRUE;
1350             }
1351             break;
1352         case GDK_p:
1353         case GDK_P:
1354         case GDK_6:
1355             if (MOD__SHIFT_ONLY) {
1356                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH, MOD__SHIFT);
1357                 ret = TRUE;
1358             }
1359             break;
1360         case GDK_s:
1361         case GDK_S:
1362         case GDK_7:
1363             if (MOD__SHIFT_ONLY) {
1364                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1365                 ret = TRUE;
1366             }
1367             break;
1368         case GDK_a:
1369         case GDK_A:
1370         case GDK_8:
1371             if (MOD__SHIFT_ONLY) {
1372                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT_REPEL, MOD__SHIFT);
1373                 ret = TRUE;
1374             }
1375             break;
1376         case GDK_r:
1377         case GDK_R:
1378         case GDK_9:
1379             if (MOD__SHIFT_ONLY) {
1380                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN, MOD__SHIFT);
1381                 ret = TRUE;
1382             }
1383             break;
1384         case GDK_c:
1385         case GDK_C:
1386             if (MOD__SHIFT_ONLY) {
1387                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT, MOD__SHIFT);
1388                 ret = TRUE;
1389             }
1390             break;
1391         case GDK_j:
1392         case GDK_J:
1393             if (MOD__SHIFT_ONLY) {
1394                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER, MOD__SHIFT);
1395                 ret = TRUE;
1396             }
1397             break;
1398         case GDK_b:
1399         case GDK_B:
1400             if (MOD__SHIFT_ONLY) {
1401                 sp_tweak_switch_mode(tc, TWEAK_MODE_BLUR, MOD__SHIFT);
1402                 ret = TRUE;
1403             }
1404             break;
1406         case GDK_Up:
1407         case GDK_KP_Up:
1408             if (!MOD__CTRL_ONLY) {
1409                 tc->force += 0.05;
1410                 if (tc->force > 1.0)
1411                     tc->force = 1.0;
1412                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1413                 ret = TRUE;
1414             }
1415             break;
1416         case GDK_Down:
1417         case GDK_KP_Down:
1418             if (!MOD__CTRL_ONLY) {
1419                 tc->force -= 0.05;
1420                 if (tc->force < 0.0)
1421                     tc->force = 0.0;
1422                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1423                 ret = TRUE;
1424             }
1425             break;
1426         case GDK_Right:
1427         case GDK_KP_Right:
1428             if (!MOD__CTRL_ONLY) {
1429                 tc->width += 0.01;
1430                 if (tc->width > 1.0)
1431                     tc->width = 1.0;
1432                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1433                 sp_tweak_update_area(tc);
1434                 ret = TRUE;
1435             }
1436             break;
1437         case GDK_Left:
1438         case GDK_KP_Left:
1439             if (!MOD__CTRL_ONLY) {
1440                 tc->width -= 0.01;
1441                 if (tc->width < 0.01)
1442                     tc->width = 0.01;
1443                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1444                 sp_tweak_update_area(tc);
1445                 ret = TRUE;
1446             }
1447             break;
1448         case GDK_Home:
1449         case GDK_KP_Home:
1450             tc->width = 0.01;
1451             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1452             sp_tweak_update_area(tc);
1453             ret = TRUE;
1454             break;
1455         case GDK_End:
1456         case GDK_KP_End:
1457             tc->width = 1.0;
1458             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1459             sp_tweak_update_area(tc);
1460             ret = TRUE;
1461             break;
1462         case GDK_x:
1463         case GDK_X:
1464             if (MOD__ALT_ONLY) {
1465                 desktop->setToolboxFocusTo ("altx-tweak");
1466                 ret = TRUE;
1467             }
1468             break;
1470         case GDK_Shift_L:
1471         case GDK_Shift_R:
1472             sp_tweak_update_cursor(tc, true);
1473             break;
1475         case GDK_Control_L:
1476         case GDK_Control_R:
1477             sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1478             break;
1479         default:
1480             break;
1481         }
1482         break;
1484     case GDK_KEY_RELEASE: {
1485         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1486         switch (get_group0_keyval(&event->key)) {
1487             case GDK_Shift_L:
1488             case GDK_Shift_R:
1489                 sp_tweak_update_cursor(tc, false);
1490                 break;
1491             case GDK_Control_L:
1492             case GDK_Control_R:
1493                 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1494                 tc->_message_context->clear();
1495                 break;
1496             default:
1497                 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1498                 break;
1499         }
1500     }
1502     default:
1503         break;
1504     }
1506     if (!ret) {
1507         if (((SPEventContextClass *) parent_class)->root_handler) {
1508             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1509         }
1510     }
1512     return ret;
1516 /*
1517   Local Variables:
1518   mode:c++
1519   c-file-style:"stroustrup"
1520   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1521   indent-tabs-mode:nil
1522   fill-column:99
1523   End:
1524 */
1525 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :