Code

SPDocument->Document
[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            event_context->cursor_shape = cursor_tweak_move_xpm;
212            break;
213        case TWEAK_MODE_MOVE_IN_OUT:
214            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);
215            event_context->cursor_shape = cursor_tweak_move_xpm;
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            event_context->cursor_shape = cursor_tweak_move_xpm;
220            break;
221        case TWEAK_MODE_SCALE:
222            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>scale down</b>; with Shift to <b>scale up</b>."), sel_message);
223            event_context->cursor_shape = cursor_tweak_move_xpm;
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            event_context->cursor_shape = cursor_tweak_move_xpm;
228            break;
229        case TWEAK_MODE_MORELESS:
230            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>duplicate</b>; with Shift, <b>delete</b>."), sel_message);
231            event_context->cursor_shape = cursor_tweak_move_xpm;
232            break;
233        case TWEAK_MODE_PUSH:
234            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>push paths</b>."), sel_message);
235            event_context->cursor_shape = cursor_push_xpm;
236            break;
237        case TWEAK_MODE_SHRINK_GROW:
238            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>inset paths</b>; with Shift to <b>outset</b>."), sel_message);
239            if (with_shift) {
240                event_context->cursor_shape = cursor_thicken_xpm;
241            } else {
242                event_context->cursor_shape = cursor_thin_xpm;
243            }
244            break;
245        case TWEAK_MODE_ATTRACT_REPEL:
246            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>attract paths</b>; with Shift to <b>repel</b>."), sel_message);
247            if (with_shift) {
248                event_context->cursor_shape = cursor_repel_xpm;
249            } else {
250                event_context->cursor_shape = cursor_attract_xpm;
251            }
252            break;
253        case TWEAK_MODE_ROUGHEN:
254            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>roughen paths</b>."), sel_message);
255            event_context->cursor_shape = cursor_roughen_xpm;
256            break;
257        case TWEAK_MODE_COLORPAINT:
258            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>paint objects</b> with color."), sel_message);
259            event_context->cursor_shape = cursor_color_xpm;
260            break;
261        case TWEAK_MODE_COLORJITTER:
262            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>randomize colors</b>."), sel_message);
263            event_context->cursor_shape = cursor_color_xpm;
264            break;
265        case TWEAK_MODE_BLUR:
266            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>increase blur</b>; with Shift to <b>decrease</b>."), sel_message);
267            event_context->cursor_shape = cursor_color_xpm;
268            break;
269    }
270    sp_event_context_update_cursor(event_context);
271    g_free(sel_message);
274 static bool
275 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
277     if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
278         // we cannot store properties with uris
279         css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
280         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
281         prefs->setStyle("/tools/tweak/style", const_cast<SPCSSAttr*>(css));
282         return true;
283     }
284     return false;
288 static void
289 sp_tweak_context_setup(SPEventContext *ec)
291     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
293     if (((SPEventContextClass *) parent_class)->setup)
294         ((SPEventContextClass *) parent_class)->setup(ec);
296     {
297         /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
298         SPCurve *c = new SPCurve();
299         const double C1 = 0.552;
300         c->moveto(-1,0);
301         c->curveto(-1, C1, -C1, 1, 0, 1 );
302         c->curveto(C1, 1, 1, C1, 1, 0 );
303         c->curveto(1, -C1, C1, -1, 0, -1 );
304         c->curveto(-C1, -1, -1, -C1, -1, 0 );
305         c->closepath();
306         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
307         c->unref();
308         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
309         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
310         sp_canvas_item_hide(tc->dilate_area);
311     }
313     tc->is_drawing = false;
315     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
317     sp_event_context_read(ec, "width");
318     sp_event_context_read(ec, "mode");
319     sp_event_context_read(ec, "fidelity");
320     sp_event_context_read(ec, "force");
321     sp_event_context_read(ec, "usepressure");
322     sp_event_context_read(ec, "doh");
323     sp_event_context_read(ec, "dol");
324     sp_event_context_read(ec, "dos");
325     sp_event_context_read(ec, "doo");
327     tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
328         sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
329     );
330     
331     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
332     if (prefs->getBool("/tools/tweak/selcue")) {
333         ec->enableSelectionCue();
334     }
336     if (prefs->getBool("/tools/tweak/gradientdrag")) {
337         ec->enableGrDrag();
338     }
341 static void
342 sp_tweak_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
344     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
345     Glib::ustring path = val->getEntryName();
347     if (path == "width") {
348         tc->width = CLAMP(val->getDouble(0.1), -1000.0, 1000.0);
349     } else if (path == "mode") {
350         tc->mode = val->getInt();
351         sp_tweak_update_cursor(tc, false);
352     } else if (path == "fidelity") {
353         tc->fidelity = CLAMP(val->getDouble(), 0.0, 1.0);
354     } else if (path == "force") {
355         tc->force = CLAMP(val->getDouble(1.0), 0, 1.0);
356     } else if (path == "usepressure") {
357         tc->usepressure = val->getBool();
358     } else if (path == "doh") {
359         tc->do_h = val->getBool();
360     } else if (path == "dos") {
361         tc->do_s = val->getBool();
362     } else if (path == "dol") {
363         tc->do_l = val->getBool();
364     } else if (path == "doo") {
365         tc->do_o = val->getBool();
366     }
369 static void
370 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
372     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
373         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
374     else
375         tc->pressure = TC_DEFAULT_PRESSURE;
378 double
379 get_dilate_radius (SPTweakContext *tc)
381     // 10 times the pen width:
382     return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
385 double
386 get_path_force (SPTweakContext *tc)
388     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
389         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
390     if (force > 3) {
391         force += 4 * (force - 3);
392     }
393     return force * tc->force;
396 double
397 get_move_force (SPTweakContext *tc)
399     double force = (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE);
400     return force * tc->force;
403 bool
404 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)
406     bool did = false;
408     if (SP_IS_BOX3D(item) && !is_transform_mode(mode) && !is_color_mode(mode)) {
409         // convert 3D boxes to ordinary groups before tweaking their shapes
410         item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
411         selection->add(item);
412     }
414     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
415         GSList *items = g_slist_prepend (NULL, item);
416         GSList *selected = NULL;
417         GSList *to_select = NULL;
418         Document *doc = SP_OBJECT_DOCUMENT(item);
419         sp_item_list_to_curves (items, &selected, &to_select);
420         g_slist_free (items);
421         SPObject* newObj = doc->getObjectByRepr((Inkscape::XML::Node *) to_select->data);
422         g_slist_free (to_select);
423         item = (SPItem *) newObj;
424         selection->add(item);
425     }
427     if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item)) {
428         GSList *children = NULL;
429         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
430             if (SP_IS_ITEM(child)) {
431                 children = g_slist_prepend(children, child);
432             }
433         }
435         for (GSList *i = children; i; i = i->next) {
436             SPItem *child = SP_ITEM(i->data);
437             if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity, reverse))
438                 did = true;
439         }
441         g_slist_free(children);
443     } else {
444         if (mode == TWEAK_MODE_MOVE) {
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) * vector;
452                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
453                     did = true;
454                 }
455             }
457         } else if (mode == TWEAK_MODE_MOVE_IN_OUT) {
459             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
460             if (a) {
461                 double x = Geom::L2(a->midpoint() - p)/radius;
462                 if (a->contains(p)) x = 0;
463                 if (x < 1) {
464                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * 
465                         (reverse? (a->midpoint() - p) : (p - a->midpoint()));
466                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
467                     did = true;
468                 }
469             }
471         } else if (mode == TWEAK_MODE_MOVE_JITTER) {
473             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
474             if (a) {
475                 double dp = g_random_double_range(0, M_PI*2);
476                 double dr = g_random_double_range(0, radius);
477                 double x = Geom::L2(a->midpoint() - p)/radius;
478                 if (a->contains(p)) x = 0;
479                 if (x < 1) {
480                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * Geom::Point(cos(dp)*dr, sin(dp)*dr);
481                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
482                     did = true;
483                 }
484             }
486         } else if (mode == TWEAK_MODE_SCALE) {
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 scale = 1 + (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1);
494                     sp_item_scale_rel(item, Geom::Scale(scale, scale));
495                     did = true;
496                 }
497             }
499         } else if (mode == TWEAK_MODE_ROTATE) {
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 angle = (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1) * M_PI;
507                     sp_item_rotate_rel(item, Geom::Rotate(angle));
508                     did = true;
509                 }
510             }
512         } else if (mode == TWEAK_MODE_MORELESS) {
514             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
515             if (a) {
516                 double x = Geom::L2(a->midpoint() - p)/radius;
517                 if (a->contains(p)) x = 0;
518                 if (x < 1) {
519                     double prob = force * 0.5 * (cos(M_PI * x) + 1);
520                     double chance = g_random_double_range(0, 1);
521                     if (chance <= prob) {
522                         if (reverse) { // delete
523                             sp_object_ref(SP_OBJECT(item), NULL);
524                             SP_OBJECT(item)->deleteObject(true, true);
525                             sp_object_unref(SP_OBJECT(item), NULL);
526                         } else { // duplicate
527                             Document *doc = SP_OBJECT_DOCUMENT(item);
528                             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
529                             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
530                             SPObject *old_obj = doc->getObjectByRepr(old_repr);
531                             Inkscape::XML::Node *parent = old_repr->parent();
532                             Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
533                             parent->appendChild(copy);
534                             SPObject *new_obj = doc->getObjectByRepr(copy);
535                             if (selection->includes(old_obj)) {
536                                 selection->add(new_obj);
537                             }
538                             Inkscape::GC::release(copy);
539                         }
540                         did = true;
541                     }
542                 }
543             }
545         } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item)) {
547         Inkscape::XML::Node *newrepr = NULL;
548         gint pos = 0;
549         Inkscape::XML::Node *parent = NULL;
550         char const *id = NULL;
551         if (!SP_IS_PATH(item)) {
552             newrepr = sp_selected_item_to_curved_repr(item, 0);
553             if (!newrepr)
554                 return false;
556             // remember the position of the item
557             pos = SP_OBJECT_REPR(item)->position();
558             // remember parent
559             parent = SP_OBJECT_REPR(item)->parent();
560             // remember id
561             id = SP_OBJECT_REPR(item)->attribute("id");
562         }
565         // skip those paths whose bboxes are entirely out of reach with our radius
566         Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item));
567         if (bbox) {
568             bbox->expandBy(radius);
569             if (!bbox->contains(p)) {
570                 return false;
571             }
572         }
574         Path *orig = Path_for_item(item, false);
575         if (orig == NULL) {
576             return false;
577         }
579         Path *res = new Path;
580         res->SetBackData(false);
582         Shape *theShape = new Shape;
583         Shape *theRes = new Shape;
584         Geom::Matrix i2doc(sp_item_i2doc_affine(item));
586         orig->ConvertWithBackData((0.08 - (0.07 * fidelity)) / i2doc.descrim()); // default 0.059
587         orig->Fill(theShape, 0);
589         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
590         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
591         if (val && strcmp(val, "nonzero") == 0)
592         {
593             theRes->ConvertToShape(theShape, fill_nonZero);
594         }
595         else if (val && strcmp(val, "evenodd") == 0)
596         {
597             theRes->ConvertToShape(theShape, fill_oddEven);
598         }
599         else
600         {
601             theRes->ConvertToShape(theShape, fill_nonZero);
602         }
604         if (Geom::L2(vector) != 0)
605             vector = 1/Geom::L2(vector) * vector;
607         bool did_this = false;
608         if (mode == TWEAK_MODE_SHRINK_GROW) {
609             if (theShape->MakeTweak(tweak_mode_grow, theRes,
610                                  reverse? force : -force,
611                                  join_straight, 4.0,
612                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
613               did_this = true;
614         } else if (mode == TWEAK_MODE_ATTRACT_REPEL) {
615             if (theShape->MakeTweak(tweak_mode_repel, theRes,
616                                  reverse? force : -force,
617                                  join_straight, 4.0,
618                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0)
619               did_this = true;
620         } else if (mode == TWEAK_MODE_PUSH) {
621             if (theShape->MakeTweak(tweak_mode_push, theRes,
622                                  1.0,
623                                  join_straight, 4.0,
624                                  true, p, force*2*vector, radius, &i2doc) == 0)
625               did_this = true;
626         } else if (mode == TWEAK_MODE_ROUGHEN) {
627             if (theShape->MakeTweak(tweak_mode_roughen, theRes,
628                                  force,
629                                  join_straight, 4.0,
630                                  true, p, Geom::Point(0,0), radius, &i2doc) == 0)
631               did_this = true;
632         }
634         // the rest only makes sense if we actually changed the path
635         if (did_this) {
636             theRes->ConvertToShape(theShape, fill_positive);
638             res->Reset();
639             theRes->ConvertToForme(res);
641             double th_max = (0.6 - 0.59*sqrt(fidelity)) / i2doc.descrim();
642             double threshold = MAX(th_max, th_max*force);
643             res->ConvertEvenLines(threshold);
644             res->Simplify(threshold / (selection->desktop()->current_zoom()));
646             if (newrepr) { // converting to path, need to replace the repr
647                 bool is_selected = selection->includes(item);
648                 if (is_selected)
649                     selection->remove(item);
651                 // It's going to resurrect, so we delete without notifying listeners.
652                 SP_OBJECT(item)->deleteObject(false);
654                 // restore id
655                 newrepr->setAttribute("id", id);
656                 // add the new repr to the parent
657                 parent->appendChild(newrepr);
658                 // move to the saved position
659                 newrepr->setPosition(pos > 0 ? pos : 0);
661                 if (is_selected)
662                     selection->add(newrepr);
663             }
665             if (res->descr_cmd.size() > 1) {
666                 gchar *str = res->svg_dump_path();
667                 if (newrepr) {
668                     newrepr->setAttribute("d", str);
669                 } else {
670                     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
671                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
672                     } else {
673                         SP_OBJECT_REPR(item)->setAttribute("d", str);
674                     }
675                 }
676                 g_free(str);
677             } else {
678                 // TODO: if there's 0 or 1 node left, delete this path altogether
679             }
681             if (newrepr) {
682                 Inkscape::GC::release(newrepr);
683                 newrepr = NULL;
684             }
685         }
687         delete theShape;
688         delete theRes;
689         delete orig;
690         delete res;
692         if (did_this)
693             did = true;
694     }
696     }
698     return did;
701 void
702 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
704     float rgb_g[3];
706     if (!do_h || !do_s || !do_l) {
707         float hsl_g[3];
708         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
709         float hsl_c[3];
710         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
711         if (!do_h)
712             hsl_g[0] = hsl_c[0];
713         if (!do_s)
714             hsl_g[1] = hsl_c[1];
715         if (!do_l)
716             hsl_g[2] = hsl_c[2];
717         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
718     } else {
719         rgb_g[0] = SP_RGBA32_R_F(goal);
720         rgb_g[1] = SP_RGBA32_G_F(goal);
721         rgb_g[2] = SP_RGBA32_B_F(goal);
722     }
724     for (int i = 0; i < 3; i++) {
725         double d = rgb_g[i] - color[i];
726         color[i] += d * force;
727     }
730 void
731 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
733     float hsl_c[3];
734     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
736     if (do_h) {
737         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
738         if (hsl_c[0] > 1)
739             hsl_c[0] -= 1;
740         if (hsl_c[0] < 0)
741             hsl_c[0] += 1;
742     }
743     if (do_s) {
744         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
745     }
746     if (do_l) {
747         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
748     }
750     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
753 void
754 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
756     if (mode == TWEAK_MODE_COLORPAINT) {
757         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
758     } else if (mode == TWEAK_MODE_COLORJITTER) {
759         tweak_colorjitter (color, force, do_h, do_s, do_l);
760     }
763 void
764 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
766     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
768     if (mode == TWEAK_MODE_COLORPAINT) {
769         double d = opacity_goal - opacity;
770         opacity += d * force;
771     } else if (mode == TWEAK_MODE_COLORJITTER) {
772         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
773     }
775     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
779 double
780 tweak_profile (double dist, double radius)
782     if (radius == 0)
783         return 0;
784     double x = dist / radius;
785     double alpha = 1;
786     if (x >= 1) {
787         return 0;
788     } else if (x <= 0) {
789         return 1;
790     } else {
791         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
792     }
795 void
796 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
797                           guint32 const rgb_goal, Geom::Point p_w, double radius, double force, guint mode,
798                           bool do_h, bool do_s, bool do_l, bool /*do_o*/)
800     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
802     if (!gradient || !SP_IS_GRADIENT(gradient))
803         return;
805     Geom::Matrix i2d (sp_item_i2doc_affine (item));
806     Geom::Point p = p_w * i2d.inverse();
807     p *= (gradient->gradientTransform).inverse();
808     // now p is in gradient's original coordinates
810     double pos = 0;
811     double r = 0;
812     if (SP_IS_LINEARGRADIENT(gradient)) {
813         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
815         Geom::Point p1(lg->x1.computed, lg->y1.computed);
816         Geom::Point p2(lg->x2.computed, lg->y2.computed);
817         Geom::Point pdiff(p2 - p1);
818         double vl = Geom::L2(pdiff);
820         // This is the matrix which moves and rotates the gradient line
821         // so it's oriented along the X axis:
822         Geom::Matrix norm = Geom::Matrix(Geom::Translate(-p1)) * Geom::Matrix(Geom::Rotate(-atan2(pdiff[Geom::Y], pdiff[Geom::X])));
824         // Transform the mouse point by it to find out its projection onto the gradient line:
825         Geom::Point pnorm = p * norm;
827         // Scale its X coordinate to match the length of the gradient line:
828         pos = pnorm[Geom::X] / vl;
829         // Calculate radius in lenfth-of-gradient-line units
830         r = radius / vl;
832     } else if (SP_IS_RADIALGRADIENT(gradient)) {
833         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
834         Geom::Point c (rg->cx.computed, rg->cy.computed);
835         pos = Geom::L2(p - c) / rg->r.computed;
836         r = radius / rg->r.computed;
837     }
839     // Normalize pos to 0..1, taking into accound gradient spread:
840     double pos_e = pos;
841     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
842         if (pos > 1)
843             pos_e = 1;
844         if (pos < 0)
845             pos_e = 0;
846     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
847         if (pos > 1 || pos < 0)
848             pos_e = pos - floor(pos);
849     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
850         if (pos > 1 || pos < 0) {
851             bool odd = ((int)(floor(pos)) % 2 == 1);
852             pos_e = pos - floor(pos);
853             if (odd)
854                 pos_e = 1 - pos_e;
855         }
856     }
858     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
860     double offset_l = 0;
861     double offset_h = 0;
862     SPObject *child_prev = NULL;
863     for (SPObject *child = sp_object_first_child(vector);
864          child != NULL; child = SP_OBJECT_NEXT(child)) {
865         if (!SP_IS_STOP(child))
866             continue;
867         SPStop *stop = SP_STOP (child);
869         offset_h = stop->offset;
871         if (child_prev) {
873             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
874                 // the summit falls in this interstop, and the radius is small,
875                 // so it only affects the ends of this interstop;
876                 // distribute the force between the two endstops so that they
877                 // get all the painting even if they are not touched by the brush
878                 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
879                                   force * (pos_e - offset_l) / (offset_h - offset_l),
880                                   do_h, do_s, do_l);
881                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
882                                   force * (offset_h - pos_e) / (offset_h - offset_l),
883                                   do_h, do_s, do_l);
884                 SP_OBJECT(stop)->updateRepr();
885                 child_prev->updateRepr();
886                 break;
887             } else {
888                 // wide brush, may affect more than 2 stops,
889                 // paint each stop by the force from the profile curve
890                 if (offset_l <= pos_e && offset_l > pos_e - r) {
891                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
892                                  force * tweak_profile (fabs (pos_e - offset_l), r),
893                                  do_h, do_s, do_l);
894                     child_prev->updateRepr();
895                 }
897                 if (offset_h >= pos_e && offset_h < pos_e + r) {
898                     tweak_color (mode, stop->specified_color.v.c, rgb_goal,
899                                  force * tweak_profile (fabs (pos_e - offset_h), r),
900                                  do_h, do_s, do_l);
901                     SP_OBJECT(stop)->updateRepr();
902                 }
903             }
904         }
906         offset_l = offset_h;
907         child_prev = child;
908     }
911 bool
912 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
913                           guint32 fill_goal, bool do_fill,
914                           guint32 stroke_goal, bool do_stroke,
915                           float opacity_goal, bool do_opacity,
916                           bool do_blur, bool reverse,
917                           Geom::Point p, double radius, double force,
918                           bool do_h, bool do_s, bool do_l, bool do_o)
920     bool did = false;
922     if (SP_IS_GROUP(item)) {
923         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
924             if (SP_IS_ITEM(child)) {
925                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
926                                           fill_goal, do_fill,
927                                           stroke_goal, do_stroke,
928                                           opacity_goal, do_opacity,
929                                           do_blur, reverse,
930                                           p, radius, force, do_h, do_s, do_l, do_o))
931                     did = true;
932             }
933         }
935     } else {
936         SPStyle *style = SP_OBJECT_STYLE(item);
937         if (!style) {
938             return false;
939         }
940         Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item),
941                                                         SPItem::GEOMETRIC_BBOX);
942         if (!bbox) {
943             return false;
944         }
946         Geom::Rect brush(p - Geom::Point(radius, radius), p + Geom::Point(radius, radius));
948         Geom::Point center = bbox->midpoint();
949         double this_force;
951 // if item == item_at_point, use max force
952         if (item == item_at_point) {
953             this_force = force;
954 // else if no overlap of bbox and brush box, skip:
955         } else if (!bbox->intersects(brush)) {
956             return false;
957 //TODO:
958 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
959         //} else if (bbox->maxExtent() > 3 * radius) {
960         //}
961 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
962 // else if still smaller, then check only the object center:
963         } else {
964             this_force = force * tweak_profile (Geom::L2 (p - center), radius);
965         }
967         if (this_force > 0.002) {
969             if (do_blur) {
970                 Geom::OptRect bbox = item->getBounds(sp_item_i2doc_affine(item),
971                                                         SPItem::GEOMETRIC_BBOX);
972                 if (!bbox) {
973                     return did;
974                 }
976                 double blur_now = 0;
977                 Geom::Matrix i2d = sp_item_i2d_affine (item);
978                 if (style->filter.set && style->getFilter()) {
979                     //cycle through filter primitives
980                     SPObject *primitive_obj = style->getFilter()->children;
981                     while (primitive_obj) {
982                         if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
983                             SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
984                             //if primitive is gaussianblur
985                             if(SP_IS_GAUSSIANBLUR(primitive)) {
986                                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
987                                 float num = spblur->stdDeviation.getNumber();
988                                 blur_now += num * i2d.descrim(); // sum all blurs in the filter
989                             }
990                         }
991                         primitive_obj = primitive_obj->next;
992                     }
993                 }
994                 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y];
995                 blur_now = blur_now / perimeter;
997                 double blur_new;
998                 if (reverse) 
999                     blur_new = blur_now - 0.06 * force;
1000                 else
1001                     blur_new = blur_now + 0.06 * force;
1002                 if (blur_new < 0.0005 && blur_new < blur_now) {
1003                     blur_new = 0;
1004                 }
1006                 if (blur_new == 0) {
1007                     remove_filter(item, false);
1008                 } else {
1009                     double radius = blur_new * perimeter;
1010                     SPFilter *filter = modify_filter_gaussian_blur_from_item(SP_OBJECT_DOCUMENT(item), item, radius);
1012                     sp_style_set_property_url(item, "filter", filter, false);
1013                 }
1014                 return true; // do not do colors, blur is a separate mode
1015             }
1017             if (do_fill) {
1018                 if (style->fill.isPaintserver()) {
1019                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1020                     did = true;
1021                 } else if (style->fill.isColor()) {
1022                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
1023                     item->updateRepr();
1024                     did = true;
1025                 }
1026             }
1027             if (do_stroke) {
1028                 if (style->stroke.isPaintserver()) {
1029                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1030                     did = true;
1031                 } else if (style->stroke.isColor()) {
1032                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
1033                     item->updateRepr();
1034                     did = true;
1035                 }
1036             }
1037             if (do_opacity && do_o) {
1038                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
1039             }
1040         }
1041     }
1043     return did;
1047 bool
1048 sp_tweak_dilate (SPTweakContext *tc, Geom::Point event_p, Geom::Point p, Geom::Point vector, bool reverse)
1050     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
1051     SPDesktop *desktop = SP_EVENT_CONTEXT(tc)->desktop;
1053     if (selection->isEmpty()) {
1054         return false;
1055     }
1057     bool did = false;
1058     double radius = get_dilate_radius(tc);
1060     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
1062     bool do_fill = false, do_stroke = false, do_opacity = false;
1063     guint32 fill_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", true, &do_fill);
1064     guint32 stroke_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", false, &do_stroke);
1065     double opacity_goal = sp_desktop_get_master_opacity_tool(desktop, "/tools/tweak", &do_opacity);
1066     if (reverse) {
1067 #if 0
1068         // HSL inversion 
1069         float hsv[3];
1070         float rgb[3];
1071         sp_color_rgb_to_hsv_floatv (hsv, 
1072                                     SP_RGBA32_R_F(fill_goal),
1073                                     SP_RGBA32_G_F(fill_goal),
1074                                     SP_RGBA32_B_F(fill_goal));
1075         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1076         fill_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1077         sp_color_rgb_to_hsv_floatv (hsv, 
1078                                     SP_RGBA32_R_F(stroke_goal),
1079                                     SP_RGBA32_G_F(stroke_goal),
1080                                     SP_RGBA32_B_F(stroke_goal));
1081         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1082         stroke_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1083 #else
1084         // RGB inversion 
1085         fill_goal = SP_RGBA32_U_COMPOSE(
1086             (255 - SP_RGBA32_R_U(fill_goal)),
1087             (255 - SP_RGBA32_G_U(fill_goal)),
1088             (255 - SP_RGBA32_B_U(fill_goal)),
1089             (255 - SP_RGBA32_A_U(fill_goal)));
1090         stroke_goal = SP_RGBA32_U_COMPOSE(
1091             (255 - SP_RGBA32_R_U(stroke_goal)),
1092             (255 - SP_RGBA32_G_U(stroke_goal)),
1093             (255 - SP_RGBA32_B_U(stroke_goal)),
1094             (255 - SP_RGBA32_A_U(stroke_goal)));
1095 #endif
1096         opacity_goal = 1 - opacity_goal;
1097     }
1099     double path_force = get_path_force(tc);
1100     if (radius == 0 || path_force == 0) {
1101         return false;
1102     }
1103     double move_force = get_move_force(tc);
1104     double color_force = MIN(sqrt(path_force)/20.0, 1);
1106     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1107          items != NULL;
1108          items = items->next) {
1110         SPItem *item = (SPItem *) items->data;
1112         if (is_color_mode (tc->mode)) {
1113             if (do_fill || do_stroke || do_opacity) {
1114                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
1115                                           fill_goal, do_fill,
1116                                           stroke_goal, do_stroke,
1117                                           opacity_goal, do_opacity,
1118                                           tc->mode == TWEAK_MODE_BLUR, reverse,
1119                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
1120                 did = true;
1121             }
1122         } else if (is_transform_mode(tc->mode)) {
1123             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, move_force, tc->fidelity, reverse))
1124                 did = true;
1125         } else {
1126             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, path_force, tc->fidelity, reverse))
1127                 did = true;
1128         }
1129     }
1131     return did;
1134 void
1135 sp_tweak_update_area (SPTweakContext *tc)
1137         double radius = get_dilate_radius(tc);
1138         Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
1139         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1140         sp_canvas_item_show(tc->dilate_area);
1143 void
1144 sp_tweak_switch_mode (SPTweakContext *tc, gint mode, bool with_shift)
1146     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1147     // need to set explicitly, because the prefs may not have changed by the previous
1148     tc->mode = mode;
1149     sp_tweak_update_cursor (tc, with_shift);
1152 void
1153 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode, bool with_shift)
1155     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1156    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
1157    gint now_mode = prefs->getInt("/tools/tweak/mode", 0);
1158    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1159    // button has changed prefs, restore
1160    prefs->setInt("/tools/tweak/mode", now_mode);
1161    // changing prefs changed tc->mode, restore back :)
1162    tc->mode = mode;
1163    sp_tweak_update_cursor (tc, with_shift);
1166 gint
1167 sp_tweak_context_root_handler(SPEventContext *event_context,
1168                                   GdkEvent *event)
1170     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
1171     SPDesktop *desktop = event_context->desktop;
1173     gint ret = FALSE;
1175     switch (event->type) {
1176         case GDK_ENTER_NOTIFY:
1177             sp_canvas_item_show(tc->dilate_area);
1178             break;
1179         case GDK_LEAVE_NOTIFY:
1180             sp_canvas_item_hide(tc->dilate_area);
1181             break;
1182         case GDK_BUTTON_PRESS:
1183             if (event->button.button == 1 && !event_context->space_panning) {
1185                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
1186                     return TRUE;
1187                 }
1189                 Geom::Point const button_w(event->button.x,
1190                                          event->button.y);
1191                 Geom::Point const button_dt(desktop->w2d(button_w));
1192                 tc->last_push = desktop->dt2doc(button_dt);
1194                 sp_tweak_extinput(tc, event);
1196                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
1197                 tc->is_drawing = true;
1198                 tc->is_dilating = true;
1199                 tc->has_dilated = false;
1201                 ret = TRUE;
1202             }
1203             break;
1204         case GDK_MOTION_NOTIFY:
1205         {
1206             Geom::Point const motion_w(event->motion.x,
1207                                      event->motion.y);
1208             Geom::Point motion_dt(desktop->w2d(motion_w));
1209             Geom::Point motion_doc(desktop->dt2doc(motion_dt));
1210             sp_tweak_extinput(tc, event);
1212             // draw the dilating cursor
1213                 double radius = get_dilate_radius(tc);
1214                 Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(desktop->w2d(motion_w)));
1215                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1216                 sp_canvas_item_show(tc->dilate_area);
1218                 guint num = 0;
1219                 if (!desktop->selection->isEmpty()) {
1220                     num = g_slist_length((GSList *) desktop->selection->itemList());
1221                 }
1222                 if (num == 0) {
1223                     tc->_message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
1224                 }
1226             // dilating:
1227             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1228                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
1229                 //tc->last_push = motion_doc;
1230                 tc->has_dilated = true;
1231                 // it's slow, so prevent clogging up with events
1232                 gobble_motion_events(GDK_BUTTON1_MASK);
1233                 return TRUE;
1234             }
1236         }
1237         break;
1240     case GDK_BUTTON_RELEASE:
1241     {
1242         Geom::Point const motion_w(event->button.x, event->button.y);
1243         Geom::Point const motion_dt(desktop->w2d(motion_w));
1245         sp_canvas_end_forced_full_redraws(desktop->canvas);
1246         tc->is_drawing = false;
1248         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1249             if (!tc->has_dilated) {
1250                 // if we did not rub, do a light tap
1251                 tc->pressure = 0.03;
1252                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), Geom::Point(0,0), MOD__SHIFT);
1253             }
1254             tc->is_dilating = false;
1255             tc->has_dilated = false;
1256             switch (tc->mode) {
1257                 case TWEAK_MODE_MOVE:
1258                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1259                                      SP_VERB_CONTEXT_TWEAK, _("Move tweak"));
1260                     break;
1261                 case TWEAK_MODE_MOVE_IN_OUT:
1262                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1263                                      SP_VERB_CONTEXT_TWEAK, _("Move in/out tweak"));
1264                     break;
1265                 case TWEAK_MODE_MOVE_JITTER:
1266                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1267                                      SP_VERB_CONTEXT_TWEAK, _("Move jitter tweak"));
1268                     break;
1269                 case TWEAK_MODE_SCALE:
1270                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1271                                      SP_VERB_CONTEXT_TWEAK, _("Scale tweak"));
1272                     break;
1273                 case TWEAK_MODE_ROTATE:
1274                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1275                                      SP_VERB_CONTEXT_TWEAK, _("Rotate tweak"));
1276                     break;
1277                 case TWEAK_MODE_MORELESS:
1278                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1279                                      SP_VERB_CONTEXT_TWEAK, _("Duplicate/delete tweak"));
1280                     break;
1281                 case TWEAK_MODE_PUSH:
1282                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1283                                      SP_VERB_CONTEXT_TWEAK, _("Push path tweak"));
1284                     break;
1285                 case TWEAK_MODE_SHRINK_GROW:
1286                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1287                                      SP_VERB_CONTEXT_TWEAK, _("Shrink/grow path tweak"));
1288                     break;
1289                 case TWEAK_MODE_ATTRACT_REPEL:
1290                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1291                                      SP_VERB_CONTEXT_TWEAK, _("Attract/repel path tweak"));
1292                     break;
1293                 case TWEAK_MODE_ROUGHEN:
1294                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1295                                      SP_VERB_CONTEXT_TWEAK, _("Roughen path tweak"));
1296                     break;
1297                 case TWEAK_MODE_COLORPAINT:
1298                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1299                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1300                     break;
1301                 case TWEAK_MODE_COLORJITTER:
1302                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1303                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1304                     break;
1305                 case TWEAK_MODE_BLUR:
1306                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1307                                      SP_VERB_CONTEXT_TWEAK, _("Blur tweak"));
1308                     break;
1309             }
1310         }
1311         break;
1312     }
1314     case GDK_KEY_PRESS:
1315         switch (get_group0_keyval (&event->key)) {
1316         case GDK_m:
1317         case GDK_M:
1318         case GDK_0:
1319             if (MOD__SHIFT_ONLY) {
1320                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE, MOD__SHIFT);
1321                 ret = TRUE;
1322             }
1323             break;
1324         case GDK_i:
1325         case GDK_I:
1326         case GDK_1:
1327             if (MOD__SHIFT_ONLY) {
1328                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_IN_OUT, MOD__SHIFT);
1329                 ret = TRUE;
1330             }
1331             break;
1332         case GDK_z:
1333         case GDK_Z:
1334         case GDK_2:
1335             if (MOD__SHIFT_ONLY) {
1336                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_JITTER, MOD__SHIFT);
1337                 ret = TRUE;
1338             }
1339             break;
1340         case GDK_less:
1341         case GDK_comma:
1342         case GDK_greater:
1343         case GDK_period:
1344         case GDK_3:
1345             if (MOD__SHIFT_ONLY) {
1346                 sp_tweak_switch_mode(tc, TWEAK_MODE_SCALE, MOD__SHIFT);
1347                 ret = TRUE;
1348             }
1349             break;
1350         case GDK_bracketright:
1351         case GDK_bracketleft:
1352         case GDK_4:
1353             if (MOD__SHIFT_ONLY) {
1354                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROTATE, MOD__SHIFT);
1355                 ret = TRUE;
1356             }
1357             break;
1358         case GDK_d:
1359         case GDK_D:
1360         case GDK_5:
1361             if (MOD__SHIFT_ONLY) {
1362                 sp_tweak_switch_mode(tc, TWEAK_MODE_MORELESS, MOD__SHIFT);
1363                 ret = TRUE;
1364             }
1365             break;
1366         case GDK_p:
1367         case GDK_P:
1368         case GDK_6:
1369             if (MOD__SHIFT_ONLY) {
1370                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH, MOD__SHIFT);
1371                 ret = TRUE;
1372             }
1373             break;
1374         case GDK_s:
1375         case GDK_S:
1376         case GDK_7:
1377             if (MOD__SHIFT_ONLY) {
1378                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1379                 ret = TRUE;
1380             }
1381             break;
1382         case GDK_a:
1383         case GDK_A:
1384         case GDK_8:
1385             if (MOD__SHIFT_ONLY) {
1386                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT_REPEL, MOD__SHIFT);
1387                 ret = TRUE;
1388             }
1389             break;
1390         case GDK_r:
1391         case GDK_R:
1392         case GDK_9:
1393             if (MOD__SHIFT_ONLY) {
1394                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN, MOD__SHIFT);
1395                 ret = TRUE;
1396             }
1397             break;
1398         case GDK_c:
1399         case GDK_C:
1400             if (MOD__SHIFT_ONLY) {
1401                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT, MOD__SHIFT);
1402                 ret = TRUE;
1403             }
1404             break;
1405         case GDK_j:
1406         case GDK_J:
1407             if (MOD__SHIFT_ONLY) {
1408                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER, MOD__SHIFT);
1409                 ret = TRUE;
1410             }
1411             break;
1412         case GDK_b:
1413         case GDK_B:
1414             if (MOD__SHIFT_ONLY) {
1415                 sp_tweak_switch_mode(tc, TWEAK_MODE_BLUR, MOD__SHIFT);
1416                 ret = TRUE;
1417             }
1418             break;
1420         case GDK_Up:
1421         case GDK_KP_Up:
1422             if (!MOD__CTRL_ONLY) {
1423                 tc->force += 0.05;
1424                 if (tc->force > 1.0)
1425                     tc->force = 1.0;
1426                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1427                 ret = TRUE;
1428             }
1429             break;
1430         case GDK_Down:
1431         case GDK_KP_Down:
1432             if (!MOD__CTRL_ONLY) {
1433                 tc->force -= 0.05;
1434                 if (tc->force < 0.0)
1435                     tc->force = 0.0;
1436                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1437                 ret = TRUE;
1438             }
1439             break;
1440         case GDK_Right:
1441         case GDK_KP_Right:
1442             if (!MOD__CTRL_ONLY) {
1443                 tc->width += 0.01;
1444                 if (tc->width > 1.0)
1445                     tc->width = 1.0;
1446                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1447                 sp_tweak_update_area(tc);
1448                 ret = TRUE;
1449             }
1450             break;
1451         case GDK_Left:
1452         case GDK_KP_Left:
1453             if (!MOD__CTRL_ONLY) {
1454                 tc->width -= 0.01;
1455                 if (tc->width < 0.01)
1456                     tc->width = 0.01;
1457                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1458                 sp_tweak_update_area(tc);
1459                 ret = TRUE;
1460             }
1461             break;
1462         case GDK_Home:
1463         case GDK_KP_Home:
1464             tc->width = 0.01;
1465             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1466             sp_tweak_update_area(tc);
1467             ret = TRUE;
1468             break;
1469         case GDK_End:
1470         case GDK_KP_End:
1471             tc->width = 1.0;
1472             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1473             sp_tweak_update_area(tc);
1474             ret = TRUE;
1475             break;
1476         case GDK_x:
1477         case GDK_X:
1478             if (MOD__ALT_ONLY) {
1479                 desktop->setToolboxFocusTo ("altx-tweak");
1480                 ret = TRUE;
1481             }
1482             break;
1484         case GDK_Shift_L:
1485         case GDK_Shift_R:
1486             sp_tweak_update_cursor(tc, true);
1487             break;
1489         case GDK_Control_L:
1490         case GDK_Control_R:
1491             sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1492             break;
1493         default:
1494             break;
1495         }
1496         break;
1498     case GDK_KEY_RELEASE: {
1499         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1500         switch (get_group0_keyval(&event->key)) {
1501             case GDK_Shift_L:
1502             case GDK_Shift_R:
1503                 sp_tweak_update_cursor(tc, false);
1504                 break;
1505             case GDK_Control_L:
1506             case GDK_Control_R:
1507                 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1508                 tc->_message_context->clear();
1509                 break;
1510             default:
1511                 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1512                 break;
1513         }
1514     }
1516     default:
1517         break;
1518     }
1520     if (!ret) {
1521         if (((SPEventContextClass *) parent_class)->root_handler) {
1522             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1523         }
1524     }
1526     return ret;
1530 /*
1531   Local Variables:
1532   mode:c++
1533   c-file-style:"stroustrup"
1534   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1535   indent-tabs-mode:nil
1536   fill-column:99
1537   End:
1538 */
1539 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :