Code

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