Code

transform modes for tweak tool
[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 "prefs-utils.h"
73 #include "style.h"
74 #include "box3d.h"
75 #include "sp-item-transform.h"
77 #include "tweak-context.h"
79 #define DDC_RED_RGBA 0xff0000ff
81 #define DYNA_MIN_WIDTH 1.0e-6
83 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
84 static void sp_tweak_context_init(SPTweakContext *ddc);
85 static void sp_tweak_context_dispose(GObject *object);
87 static void sp_tweak_context_setup(SPEventContext *ec);
88 static void sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
89 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
91 static SPEventContextClass *parent_class;
93 GtkType
94 sp_tweak_context_get_type(void)
95 {
96     static GType type = 0;
97     if (!type) {
98         GTypeInfo info = {
99             sizeof(SPTweakContextClass),
100             NULL, NULL,
101             (GClassInitFunc) sp_tweak_context_class_init,
102             NULL, NULL,
103             sizeof(SPTweakContext),
104             4,
105             (GInstanceInitFunc) sp_tweak_context_init,
106             NULL,   /* value_table */
107         };
108         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
109     }
110     return type;
113 static void
114 sp_tweak_context_class_init(SPTweakContextClass *klass)
116     GObjectClass *object_class = (GObjectClass *) klass;
117     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
119     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
121     object_class->dispose = sp_tweak_context_dispose;
123     event_context_class->setup = sp_tweak_context_setup;
124     event_context_class->set = sp_tweak_context_set;
125     event_context_class->root_handler = sp_tweak_context_root_handler;
128 static void
129 sp_tweak_context_init(SPTweakContext *tc)
131     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
133     event_context->cursor_shape = cursor_push_xpm;
134     event_context->hot_x = 4;
135     event_context->hot_y = 4;
137     /* attributes */
138     tc->dragging = FALSE;
140     tc->width = 0.2;
141     tc->force = 0.2;
142     tc->pressure = TC_DEFAULT_PRESSURE;
144     tc->is_dilating = false;
145     tc->has_dilated = false;
147     tc->do_h = true;
148     tc->do_s = true;
149     tc->do_l = true;
150     tc->do_o = false;
152     new (&tc->style_set_connection) sigc::connection();
155 static void
156 sp_tweak_context_dispose(GObject *object)
158     SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
160     tc->style_set_connection.disconnect();
161     tc->style_set_connection.~connection();
163     if (tc->dilate_area) {
164         gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
165         tc->dilate_area = NULL;
166     }
168     if (tc->_message_context) {
169         delete tc->_message_context;
170     }
172     G_OBJECT_CLASS(parent_class)->dispose(object);
175 bool is_transform_mode (gint mode)
177     return (mode == TWEAK_MODE_MOVE || 
178             mode == TWEAK_MODE_MOVE_IN_OUT || 
179             mode == TWEAK_MODE_MOVE_JITTER || 
180             mode == TWEAK_MODE_SCALE || 
181             mode == TWEAK_MODE_ROTATE || 
182             mode == TWEAK_MODE_MORELESS);
185 bool is_color_mode (gint mode)
187     return (mode == TWEAK_MODE_COLORPAINT || mode == TWEAK_MODE_COLORJITTER);
190 void
191 sp_tweak_update_cursor (SPTweakContext *tc, bool with_shift)
193    SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
194    SPDesktop *desktop = event_context->desktop;
196                 guint num = 0;
197                 gchar *sel_message = NULL;
198                 if (!desktop->selection->isEmpty()) {
199                     num = g_slist_length((GSList *) desktop->selection->itemList());
200                     sel_message = g_strdup_printf(_("<b>%i</b> objects selected"), num);
201                 } else {
202                     sel_message = g_strdup_printf(_("<b>Nothing</b> selected"));
203                 }
206    switch (tc->mode) {
207        case TWEAK_MODE_MOVE:
208            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>move</b>."), sel_message);
209                            break;
210            event_context->cursor_shape = cursor_tweak_move_xpm;
211            break;
212        case TWEAK_MODE_MOVE_IN_OUT:
213            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);
214            break;
215        case TWEAK_MODE_MOVE_JITTER:
216            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>move randomly</b>."), sel_message);
217            break;
218        case TWEAK_MODE_SCALE:
219            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);
220            break;
221        case TWEAK_MODE_ROTATE:
222            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>rotate clockwise</b>; with Shift, <b>counterclockwise</b>."), sel_message);
223            break;
224        case TWEAK_MODE_MORELESS:
225            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>duplicate</b>; with Shift, <b>delete</b>."), sel_message);
226            break;
227        case TWEAK_MODE_PUSH:
228            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>push paths</b>."), sel_message);
229            event_context->cursor_shape = cursor_push_xpm;
230            break;
231        case TWEAK_MODE_SHRINK_GROW:
232            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>inset paths</b>; with Shift to <b>outset</b>."), sel_message);
233            if (with_shift) {
234                event_context->cursor_shape = cursor_thicken_xpm;
235            } else {
236                event_context->cursor_shape = cursor_thin_xpm;
237            }
238            break;
239        case TWEAK_MODE_ATTRACT_REPEL:
240            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>attract paths</b>; with Shift to <b>repel</b>."), sel_message);
241            if (with_shift) {
242                event_context->cursor_shape = cursor_repel_xpm;
243            } else {
244                event_context->cursor_shape = cursor_attract_xpm;
245            }
246            break;
247        case TWEAK_MODE_ROUGHEN:
248            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>roughen paths</b>."), sel_message);
249            event_context->cursor_shape = cursor_roughen_xpm;
250            break;
251        case TWEAK_MODE_COLORPAINT:
252            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>paint objects</b> with color."), sel_message);
253            break;
254        case TWEAK_MODE_COLORJITTER:
255            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>randomize colors</b>."), sel_message);
256            event_context->cursor_shape = cursor_color_xpm;
257            break;
258    }
259    sp_event_context_update_cursor(event_context);
260    g_free(sel_message);
263 static bool
264 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
266     if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
267         // we cannot store properties with uris
268         css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
270         sp_repr_css_change (inkscape_get_repr (INKSCAPE, "tools.tweak"), (SPCSSAttr *) css, "style");
272         return true;
273     }
274     return false;
278 static void
279 sp_tweak_context_setup(SPEventContext *ec)
281     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
283     if (((SPEventContextClass *) parent_class)->setup)
284         ((SPEventContextClass *) parent_class)->setup(ec);
286     {
287         /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
288         SPCurve *c = new SPCurve();
289         const double C1 = 0.552;
290         c->moveto(-1,0);
291         c->curveto(-1, C1, -C1, 1, 0, 1 );
292         c->curveto(C1, 1, 1, C1, 1, 0 );
293         c->curveto(1, -C1, C1, -1, 0, -1 );
294         c->curveto(-C1, -1, -1, -C1, -1, 0 );
295         c->closepath();
296         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
297         c->unref();
298         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
299         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
300         sp_canvas_item_hide(tc->dilate_area);
301     }
303     tc->is_drawing = false;
305     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
307     sp_event_context_read(ec, "width");
308     sp_event_context_read(ec, "mode");
309     sp_event_context_read(ec, "fidelity");
310     sp_event_context_read(ec, "force");
311     sp_event_context_read(ec, "usepressure");
312     sp_event_context_read(ec, "doh");
313     sp_event_context_read(ec, "dol");
314     sp_event_context_read(ec, "dos");
315     sp_event_context_read(ec, "doo");
317     tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
318         sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
319     );
321     if (prefs_get_int_attribute("tools.tweak", "selcue", 0) != 0) {
322         ec->enableSelectionCue();
323     }
325     if (prefs_get_int_attribute("tools.tweak", "gradientdrag", 0) != 0) {
326         ec->enableGrDrag();
327     }
330 static void
331 sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
333     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
335     if (!strcmp(key, "width")) {
336         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
337         tc->width = CLAMP(dval, -1000.0, 1000.0);
338     } else if (!strcmp(key, "mode")) {
339         gint64 const dval = ( val ? g_ascii_strtoll (val, NULL, 10) : 0 );
340         tc->mode = dval;
341         sp_tweak_update_cursor(tc, false);
342     } else if (!strcmp(key, "fidelity")) {
343         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
344         tc->fidelity = CLAMP(dval, 0.0, 1.0);
345     } else if (!strcmp(key, "force")) {
346         double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
347         tc->force = CLAMP(dval, 0, 1.0);
348     } else if (!strcmp(key, "usepressure")) {
349         tc->usepressure = (val && strcmp(val, "0"));
350     } else if (!strcmp(key, "doh")) {
351         tc->do_h = (val && strcmp(val, "0"));
352     } else if (!strcmp(key, "dos")) {
353         tc->do_s = (val && strcmp(val, "0"));
354     } else if (!strcmp(key, "dol")) {
355         tc->do_l = (val && strcmp(val, "0"));
356     } else if (!strcmp(key, "doo")) {
357         tc->do_o = (val && strcmp(val, "0"));
358     }
361 static void
362 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
364     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
365         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
366     else
367         tc->pressure = TC_DEFAULT_PRESSURE;
370 double
371 get_dilate_radius (SPTweakContext *tc)
373     // 10 times the pen width:
374     return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
377 double
378 get_path_force (SPTweakContext *tc)
380     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
381         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
382     if (force > 3) {
383         force += 4 * (force - 3);
384     }
385     return force * tc->force;
388 double
389 get_move_force (SPTweakContext *tc)
391     double force = (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE);
392     return force * tc->force;
395 bool
396 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, NR::Point p, NR::Point vector, gint mode, double radius, double force, double fidelity, bool reverse)
398     bool did = false;
400     if (SP_IS_BOX3D(item) && !is_transform_mode(mode) && !is_color_mode(mode)) {
401         // convert 3D boxes to ordinary groups before tweaking their shapes
402         item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
403         selection->add(item);
404     }
406     if (SP_IS_GROUP(item)) {
407         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
408             if (SP_IS_ITEM(child)) {
409                 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity, reverse))
410                     did = true;
411             }
412         }
414     } else {
415         if (mode == TWEAK_MODE_MOVE) {
417             boost::optional<NR::Rect> a = item->getBounds(sp_item_i2doc_affine(item));
418             if (a) {
419                 double x = NR::L2(a->midpoint() - p)/radius;
420                 if (a->contains(p)) x = 0;
421                 if (x < 1) {
422                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * vector;
423                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
424                     did = true;
425                 }
426             }
428         } else if (mode == TWEAK_MODE_MOVE_IN_OUT) {
430             boost::optional<NR::Rect> a = item->getBounds(sp_item_i2doc_affine(item));
431             if (a) {
432                 double x = NR::L2(a->midpoint() - p)/radius;
433                 if (a->contains(p)) x = 0;
434                 if (x < 1) {
435                     Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * 
436                         (reverse? (a->midpoint() - p) : (p - a->midpoint()));
437                     sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
438                     did = true;
439                 }
440             }
442         } else if (mode == TWEAK_MODE_MOVE_JITTER) {
444             boost::optional<NR::Rect> a = item->getBounds(sp_item_i2doc_affine(item));
445             if (a) {
446                 double dp = g_random_double_range(0, M_PI*2);
447                 double dr = g_random_double_range(0, radius);
448                 double x = NR::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) * NR::Point(cos(dp)*dr, sin(dp)*dr);
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_SCALE) {
459             boost::optional<NR::Rect> a = item->getBounds(sp_item_i2doc_affine(item));
460             if (a) {
461                 double x = NR::L2(a->midpoint() - p)/radius;
462                 if (a->contains(p)) x = 0;
463                 if (x < 1) {
464                     double scale = 1 + (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1);
465                     sp_item_scale_rel(item, Geom::Scale(scale, scale));
466                     did = true;
467                 }
468             }
470         } else if (mode == TWEAK_MODE_ROTATE) {
472             boost::optional<NR::Rect> a = item->getBounds(sp_item_i2doc_affine(item));
473             if (a) {
474                 double x = NR::L2(a->midpoint() - p)/radius;
475                 if (a->contains(p)) x = 0;
476                 if (x < 1) {
477                     double angle = (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1) * M_PI;
478                     sp_item_rotate_rel(item, Geom::Rotate(angle));
479                     did = true;
480                 }
481             }
483         } else if (mode == TWEAK_MODE_MORELESS) {
485             boost::optional<NR::Rect> a = item->getBounds(sp_item_i2doc_affine(item));
486             if (a) {
487                 double x = NR::L2(a->midpoint() - p)/radius;
488                 if (a->contains(p)) x = 0;
489                 if (x < 1) {
490                     double prob = force * 0.5 * (cos(M_PI * x) + 1);
491                     double chance = g_random_double_range(0, 1);
492                     if (chance <= prob) {
493                         if (reverse) { // delete
494                             sp_object_ref(SP_OBJECT(item), NULL);
495                             SP_OBJECT(item)->deleteObject(true, true);
496                             sp_object_unref(SP_OBJECT(item), NULL);
497                         } else { // duplicate
498                             SPDocument *doc = SP_OBJECT_DOCUMENT(item);
499                             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
500                             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
501                             Inkscape::XML::Node *parent = old_repr->parent();
502                             Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
503                             parent->appendChild(copy);
504                             Inkscape::GC::release(copy);
505                         }
506                     }
507                 }
508             }
510         } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
512         Inkscape::XML::Node *newrepr = NULL;
513         gint pos = 0;
514         Inkscape::XML::Node *parent = NULL;
515         char const *id = NULL;
516         if (!SP_IS_PATH(item)) {
517             newrepr = sp_selected_item_to_curved_repr(item, 0);
518             if (!newrepr)
519                 return false;
521             // remember the position of the item
522             pos = SP_OBJECT_REPR(item)->position();
523             // remember parent
524             parent = SP_OBJECT_REPR(item)->parent();
525             // remember id
526             id = SP_OBJECT_REPR(item)->attribute("id");
527         }
530         // skip those paths whose bboxes are entirely out of reach with our radius
531         boost::optional<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item));
532         if (bbox) {
533             bbox->growBy(radius);
534             if (!bbox->contains(p)) {
535                 return false;
536             }
537         }
539         Path *orig = Path_for_item(item, false);
540         if (orig == NULL) {
541             return false;
542         }
544         Path *res = new Path;
545         res->SetBackData(false);
547         Shape *theShape = new Shape;
548         Shape *theRes = new Shape;
549         Geom::Matrix i2doc(sp_item_i2doc_affine(item));
551         orig->ConvertWithBackData((0.08 - (0.07 * fidelity)) / i2doc.descrim()); // default 0.059
552         orig->Fill(theShape, 0);
554         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
555         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
556         if (val && strcmp(val, "nonzero") == 0)
557         {
558             theRes->ConvertToShape(theShape, fill_nonZero);
559         }
560         else if (val && strcmp(val, "evenodd") == 0)
561         {
562             theRes->ConvertToShape(theShape, fill_oddEven);
563         }
564         else
565         {
566             theRes->ConvertToShape(theShape, fill_nonZero);
567         }
569         if (NR::L2(vector) != 0)
570             vector = 1/NR::L2(vector) * vector;
572         bool did_this = false;
573         if (mode == TWEAK_MODE_SHRINK_GROW) {
574             if (theShape->MakeTweak(tweak_mode_grow, theRes,
575                                  reverse? force : -force,
576                                  join_straight, 4.0,
577                                  true, p, NR::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
578               did_this = true;
579         } else if (mode == TWEAK_MODE_ATTRACT_REPEL) {
580             if (theShape->MakeTweak(tweak_mode_repel, theRes,
581                                  reverse? force : -force,
582                                  join_straight, 4.0,
583                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
584               did_this = true;
585         } else if (mode == TWEAK_MODE_PUSH) {
586             if (theShape->MakeTweak(tweak_mode_push, theRes,
587                                  1.0,
588                                  join_straight, 4.0,
589                                  true, p, force*2*vector, radius, &i2doc) == 0)
590               did_this = true;
591         } else if (mode == TWEAK_MODE_ROUGHEN) {
592             if (theShape->MakeTweak(tweak_mode_roughen, theRes,
593                                  force,
594                                  join_straight, 4.0,
595                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
596               did_this = true;
597         }
599         // the rest only makes sense if we actually changed the path
600         if (did_this) {
601             theRes->ConvertToShape(theShape, fill_positive);
603             res->Reset();
604             theRes->ConvertToForme(res);
606             double th_max = (0.6 - 0.59*sqrt(fidelity)) / i2doc.descrim();
607             double threshold = MAX(th_max, th_max*force);
608             res->ConvertEvenLines(threshold);
609             res->Simplify(threshold / (selection->desktop()->current_zoom()));
611             if (newrepr) { // converting to path, need to replace the repr
612                 bool is_selected = selection->includes(item);
613                 if (is_selected)
614                     selection->remove(item);
616                 // It's going to resurrect, so we delete without notifying listeners.
617                 SP_OBJECT(item)->deleteObject(false);
619                 // restore id
620                 newrepr->setAttribute("id", id);
621                 // add the new repr to the parent
622                 parent->appendChild(newrepr);
623                 // move to the saved position
624                 newrepr->setPosition(pos > 0 ? pos : 0);
626                 if (is_selected)
627                     selection->add(newrepr);
628             }
630             if (res->descr_cmd.size() > 1) {
631                 gchar *str = res->svg_dump_path();
632                 if (newrepr) {
633                     newrepr->setAttribute("d", str);
634                 } else {
635                     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
636                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
637                     } else {
638                         SP_OBJECT_REPR(item)->setAttribute("d", str);
639                     }
640                 }
641                 g_free(str);
642             } else {
643                 // TODO: if there's 0 or 1 node left, delete this path altogether
644             }
646             if (newrepr) {
647                 Inkscape::GC::release(newrepr);
648                 newrepr = NULL;
649             }
650         }
652         delete theShape;
653         delete theRes;
654         delete orig;
655         delete res;
657         if (did_this)
658             did = true;
659     }
661     }
663     return did;
666 void
667 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
669     float rgb_g[3];
671     if (!do_h || !do_s || !do_l) {
672         float hsl_g[3];
673         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
674         float hsl_c[3];
675         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
676         if (!do_h)
677             hsl_g[0] = hsl_c[0];
678         if (!do_s)
679             hsl_g[1] = hsl_c[1];
680         if (!do_l)
681             hsl_g[2] = hsl_c[2];
682         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
683     } else {
684         rgb_g[0] = SP_RGBA32_R_F(goal);
685         rgb_g[1] = SP_RGBA32_G_F(goal);
686         rgb_g[2] = SP_RGBA32_B_F(goal);
687     }
689     for (int i = 0; i < 3; i++) {
690         double d = rgb_g[i] - color[i];
691         color[i] += d * force;
692     }
695 void
696 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
698     float hsl_c[3];
699     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
701     if (do_h) {
702         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
703         if (hsl_c[0] > 1)
704             hsl_c[0] -= 1;
705         if (hsl_c[0] < 0)
706             hsl_c[0] += 1;
707     }
708     if (do_s) {
709         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
710     }
711     if (do_l) {
712         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
713     }
715     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
718 void
719 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
721     if (mode == TWEAK_MODE_COLORPAINT) {
722         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
723     } else if (mode == TWEAK_MODE_COLORJITTER) {
724         tweak_colorjitter (color, force, do_h, do_s, do_l);
725     }
728 void
729 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
731     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
733     if (mode == TWEAK_MODE_COLORPAINT) {
734         double d = opacity_goal - opacity;
735         opacity += d * force;
736     } else if (mode == TWEAK_MODE_COLORJITTER) {
737         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
738     }
740     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
744 double
745 tweak_profile (double dist, double radius)
747     if (radius == 0)
748         return 0;
749     double x = dist / radius;
750     double alpha = 1;
751     if (x >= 1) {
752         return 0;
753     } else if (x <= 0) {
754         return 1;
755     } else {
756         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
757     }
760 void
761 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
762                           guint32 const rgb_goal, NR::Point p_w, double radius, double force, guint mode,
763                           bool do_h, bool do_s, bool do_l, bool /*do_o*/)
765     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
767     if (!gradient || !SP_IS_GRADIENT(gradient))
768         return;
770     NR::Matrix i2d (sp_item_i2doc_affine (item));
771     NR::Point p = p_w * i2d.inverse();
772     p *= (gradient->gradientTransform).inverse();
773     // now p is in gradient's original coordinates
775     double pos = 0;
776     double r = 0;
777     if (SP_IS_LINEARGRADIENT(gradient)) {
778         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
780         NR::Point p1(lg->x1.computed, lg->y1.computed);
781         NR::Point p2(lg->x2.computed, lg->y2.computed);
782         NR::Point pdiff(p2 - p1);
783         double vl = NR::L2(pdiff);
785         // This is the matrix which moves and rotates the gradient line
786         // so it's oriented along the X axis:
787         NR::Matrix norm = NR::Matrix(NR::translate(-p1)) * NR::Matrix(NR::rotate(-atan2(pdiff[NR::Y], pdiff[NR::X])));
789         // Transform the mouse point by it to find out its projection onto the gradient line:
790         NR::Point pnorm = p * norm;
792         // Scale its X coordinate to match the length of the gradient line:
793         pos = pnorm[NR::X] / vl;
794         // Calculate radius in lenfth-of-gradient-line units
795         r = radius / vl;
797     } else if (SP_IS_RADIALGRADIENT(gradient)) {
798         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
799         NR::Point c (rg->cx.computed, rg->cy.computed);
800         pos = NR::L2(p - c) / rg->r.computed;
801         r = radius / rg->r.computed;
802     }
804     // Normalize pos to 0..1, taking into accound gradient spread:
805     double pos_e = pos;
806     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
807         if (pos > 1)
808             pos_e = 1;
809         if (pos < 0)
810             pos_e = 0;
811     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
812         if (pos > 1 || pos < 0)
813             pos_e = pos - floor(pos);
814     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
815         if (pos > 1 || pos < 0) {
816             bool odd = ((int)(floor(pos)) % 2 == 1);
817             pos_e = pos - floor(pos);
818             if (odd)
819                 pos_e = 1 - pos_e;
820         }
821     }
823     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
825     double offset_l = 0;
826     double offset_h = 0;
827     SPObject *child_prev = NULL;
828     for (SPObject *child = sp_object_first_child(vector);
829          child != NULL; child = SP_OBJECT_NEXT(child)) {
830         if (!SP_IS_STOP(child))
831             continue;
832         SPStop *stop = SP_STOP (child);
834         offset_h = stop->offset;
836         if (child_prev) {
838             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
839                 // the summit falls in this interstop, and the radius is small,
840                 // so it only affects the ends of this interstop;
841                 // distribute the force between the two endstops so that they
842                 // get all the painting even if they are not touched by the brush
843                 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
844                                   force * (pos_e - offset_l) / (offset_h - offset_l),
845                                   do_h, do_s, do_l);
846                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
847                                   force * (offset_h - pos_e) / (offset_h - offset_l),
848                                   do_h, do_s, do_l);
849                 SP_OBJECT(stop)->updateRepr();
850                 child_prev->updateRepr();
851                 break;
852             } else {
853                 // wide brush, may affect more than 2 stops,
854                 // paint each stop by the force from the profile curve
855                 if (offset_l <= pos_e && offset_l > pos_e - r) {
856                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
857                                  force * tweak_profile (fabs (pos_e - offset_l), r),
858                                  do_h, do_s, do_l);
859                     child_prev->updateRepr();
860                 }
862                 if (offset_h >= pos_e && offset_h < pos_e + r) {
863                     tweak_color (mode, stop->specified_color.v.c, rgb_goal,
864                                  force * tweak_profile (fabs (pos_e - offset_h), r),
865                                  do_h, do_s, do_l);
866                     SP_OBJECT(stop)->updateRepr();
867                 }
868             }
869         }
871         offset_l = offset_h;
872         child_prev = child;
873     }
876 bool
877 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
878                           guint32 fill_goal, bool do_fill,
879                           guint32 stroke_goal, bool do_stroke,
880                           float opacity_goal, bool do_opacity,
881                           NR::Point p, double radius, double force,
882                           bool do_h, bool do_s, bool do_l, bool do_o)
884     bool did = false;
886     if (SP_IS_GROUP(item)) {
887         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
888             if (SP_IS_ITEM(child)) {
889                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
890                                           fill_goal, do_fill,
891                                           stroke_goal, do_stroke,
892                                           opacity_goal, do_opacity,
893                                           p, radius, force, do_h, do_s, do_l, do_o))
894                     did = true;
895             }
896         }
898     } else {
899         SPStyle *style = SP_OBJECT_STYLE(item);
900         if (!style) {
901             return false;
902         }
903         boost::optional<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item),
904                                                         SPItem::GEOMETRIC_BBOX);
905         if (!bbox) {
906             return false;
907         }
909         NR::Rect brush(p - NR::Point(radius, radius), p + NR::Point(radius, radius));
911         NR::Point center = bbox->midpoint();
912         double this_force;
914 // if item == item_at_point, use max force
915         if (item == item_at_point) {
916             this_force = force;
917 // else if no overlap of bbox and brush box, skip:
918         } else if (!bbox->intersects(brush)) {
919             return false;
920 //TODO:
921 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
922         //} else if (bbox->maxExtent() > 3 * radius) {
923         //}
924 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
925 // else if still smaller, then check only the object center:
926         } else {
927             this_force = force * tweak_profile (NR::L2 (p - center), radius);
928         }
930         if (this_force > 0.002) {
932             if (do_fill) {
933                 if (style->fill.isPaintserver()) {
934                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
935                     did = true;
936                 } else if (style->fill.isColor()) {
937                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
938                     item->updateRepr();
939                     did = true;
940                 }
941             }
942             if (do_stroke) {
943                 if (style->stroke.isPaintserver()) {
944                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
945                     did = true;
946                 } else if (style->stroke.isColor()) {
947                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
948                     item->updateRepr();
949                     did = true;
950                 }
951             }
952             if (do_opacity && do_o) {
953                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
954             }
955         }
956     }
958     return did;
962 bool
963 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector, bool reverse)
965     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
966     SPDesktop *desktop = SP_EVENT_CONTEXT(tc)->desktop;
968     if (selection->isEmpty()) {
969         return false;
970     }
972     bool did = false;
973     double radius = get_dilate_radius(tc);
975     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
977     bool do_fill = false, do_stroke = false, do_opacity = false;
978     guint32 fill_goal = sp_desktop_get_color_tool(desktop, "tools.tweak", true, &do_fill);
979     guint32 stroke_goal = sp_desktop_get_color_tool(desktop, "tools.tweak", false, &do_stroke);
980     double opacity_goal = sp_desktop_get_master_opacity_tool(desktop, "tools.tweak", &do_opacity);
982     double path_force = get_path_force(tc);
983     if (radius == 0 || path_force == 0) {
984         return false;
985     }
986     double move_force = get_move_force(tc);
987     double color_force = MIN(sqrt(path_force)/20.0, 1);
989     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
990          items != NULL;
991          items = items->next) {
993         SPItem *item = (SPItem *) items->data;
995         if (is_color_mode (tc->mode)) {
996             if (do_fill || do_stroke || do_opacity) {
997                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
998                                           fill_goal, do_fill,
999                                           stroke_goal, do_stroke,
1000                                           opacity_goal, do_opacity,
1001                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
1002                 did = true;
1003             }
1004         } else if (is_transform_mode(tc->mode)) {
1005             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, move_force, tc->fidelity, reverse))
1006                 did = true;
1007         } else {
1008             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, path_force, tc->fidelity, reverse))
1009                 did = true;
1010         }
1011     }
1013     return did;
1016 void
1017 sp_tweak_update_area (SPTweakContext *tc)
1019         double radius = get_dilate_radius(tc);
1020         NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
1021         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1022         sp_canvas_item_show(tc->dilate_area);
1025 void
1026 sp_tweak_switch_mode (SPTweakContext *tc, gint mode, bool with_shift)
1028     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1029     // need to set explicitly, because the prefs may not have changed by the previous
1030     tc->mode = mode;
1031     sp_tweak_update_cursor (tc, with_shift);
1034 void
1035 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode, bool with_shift)
1037    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
1038    gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
1039    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1040    // button has changed prefs, restore
1041    prefs_set_int_attribute("tools.tweak", "mode", now_mode);
1042    // changing prefs changed tc->mode, restore back :)
1043    tc->mode = mode;
1044    sp_tweak_update_cursor (tc, with_shift);
1047 gint
1048 sp_tweak_context_root_handler(SPEventContext *event_context,
1049                                   GdkEvent *event)
1051     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
1052     SPDesktop *desktop = event_context->desktop;
1054     gint ret = FALSE;
1056     switch (event->type) {
1057         case GDK_ENTER_NOTIFY:
1058             sp_canvas_item_show(tc->dilate_area);
1059             break;
1060         case GDK_LEAVE_NOTIFY:
1061             sp_canvas_item_hide(tc->dilate_area);
1062             break;
1063         case GDK_BUTTON_PRESS:
1064             if (event->button.button == 1 && !event_context->space_panning) {
1066                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
1067                     return TRUE;
1068                 }
1070                 NR::Point const button_w(event->button.x,
1071                                          event->button.y);
1072                 NR::Point const button_dt(desktop->w2d(button_w));
1073                 tc->last_push = desktop->dt2doc(button_dt);
1075                 sp_tweak_extinput(tc, event);
1077                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
1078                 tc->is_drawing = true;
1079                 tc->is_dilating = true;
1080                 tc->has_dilated = false;
1082                 ret = TRUE;
1083             }
1084             break;
1085         case GDK_MOTION_NOTIFY:
1086         {
1087             NR::Point const motion_w(event->motion.x,
1088                                      event->motion.y);
1089             NR::Point motion_dt(desktop->w2d(motion_w));
1090             NR::Point motion_doc(desktop->dt2doc(motion_dt));
1091             sp_tweak_extinput(tc, event);
1093             // draw the dilating cursor
1094                 double radius = get_dilate_radius(tc);
1095                 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
1096                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1097                 sp_canvas_item_show(tc->dilate_area);
1099                 guint num = 0;
1100                 if (!desktop->selection->isEmpty()) {
1101                     num = g_slist_length((GSList *) desktop->selection->itemList());
1102                 }
1103                 if (num == 0) {
1104                     tc->_message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
1105                 }
1107             // dilating:
1108             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1109                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
1110                 //tc->last_push = motion_doc;
1111                 tc->has_dilated = true;
1112                 // it's slow, so prevent clogging up with events
1113                 gobble_motion_events(GDK_BUTTON1_MASK);
1114                 return TRUE;
1115             }
1117         }
1118         break;
1121     case GDK_BUTTON_RELEASE:
1122     {
1123         NR::Point const motion_w(event->button.x, event->button.y);
1124         NR::Point const motion_dt(desktop->w2d(motion_w));
1126         sp_canvas_end_forced_full_redraws(desktop->canvas);
1127         tc->is_drawing = false;
1129         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1130             if (!tc->has_dilated) {
1131                 // if we did not rub, do a light tap
1132                 tc->pressure = 0.03;
1133                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0), MOD__SHIFT);
1134             }
1135             tc->is_dilating = false;
1136             tc->has_dilated = false;
1137             switch (tc->mode) {
1138                 case TWEAK_MODE_MOVE:
1139                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1140                                      SP_VERB_CONTEXT_TWEAK, _("Move tweak"));
1141                     break;
1142                 case TWEAK_MODE_MOVE_IN_OUT:
1143                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1144                                      SP_VERB_CONTEXT_TWEAK, _("Move in/out tweak"));
1145                     break;
1146                 case TWEAK_MODE_MOVE_JITTER:
1147                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1148                                      SP_VERB_CONTEXT_TWEAK, _("Move jitter tweak"));
1149                     break;
1150                 case TWEAK_MODE_SCALE:
1151                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1152                                      SP_VERB_CONTEXT_TWEAK, _("Scale tweak"));
1153                     break;
1154                 case TWEAK_MODE_ROTATE:
1155                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1156                                      SP_VERB_CONTEXT_TWEAK, _("Rotate tweak"));
1157                     break;
1158                 case TWEAK_MODE_MORELESS:
1159                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1160                                      SP_VERB_CONTEXT_TWEAK, _("Duplicate/delete tweak"));
1161                     break;
1162                 case TWEAK_MODE_PUSH:
1163                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1164                                      SP_VERB_CONTEXT_TWEAK, _("Push path tweak"));
1165                     break;
1166                 case TWEAK_MODE_SHRINK_GROW:
1167                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1168                                      SP_VERB_CONTEXT_TWEAK, _("Shrink/grow path tweak"));
1169                     break;
1170                 case TWEAK_MODE_ATTRACT_REPEL:
1171                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1172                                      SP_VERB_CONTEXT_TWEAK, _("Attract/repel path tweak"));
1173                     break;
1174                 case TWEAK_MODE_ROUGHEN:
1175                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1176                                      SP_VERB_CONTEXT_TWEAK, _("Roughen path tweak"));
1177                     break;
1178                 case TWEAK_MODE_COLORPAINT:
1179                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1180                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1181                     break;
1182                 case TWEAK_MODE_COLORJITTER:
1183                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1184                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1185                     break;
1186             }
1187         }
1188         break;
1189     }
1191     case GDK_KEY_PRESS:
1192         switch (get_group0_keyval (&event->key)) {
1193         case GDK_m:
1194         case GDK_M:
1195         case GDK_0:
1196             if (MOD__SHIFT_ONLY) {
1197                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE, MOD__SHIFT);
1198                 ret = TRUE;
1199             }
1200             break;
1201         case GDK_i:
1202         case GDK_I:
1203         case GDK_1:
1204             if (MOD__SHIFT_ONLY) {
1205                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_IN_OUT, MOD__SHIFT);
1206                 ret = TRUE;
1207             }
1208             break;
1209         case GDK_z:
1210         case GDK_Z:
1211         case GDK_2:
1212             if (MOD__SHIFT_ONLY) {
1213                 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_JITTER, MOD__SHIFT);
1214                 ret = TRUE;
1215             }
1216             break;
1217         case GDK_less:
1218         case GDK_comma:
1219         case GDK_greater:
1220         case GDK_period:
1221         case GDK_3:
1222             if (MOD__SHIFT_ONLY) {
1223                 sp_tweak_switch_mode(tc, TWEAK_MODE_SCALE, MOD__SHIFT);
1224                 ret = TRUE;
1225             }
1226             break;
1227         case GDK_bracketright:
1228         case GDK_bracketleft:
1229         case GDK_4:
1230             if (MOD__SHIFT_ONLY) {
1231                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROTATE, MOD__SHIFT);
1232                 ret = TRUE;
1233             }
1234             break;
1235         case GDK_d:
1236         case GDK_D:
1237         case GDK_5:
1238             if (MOD__SHIFT_ONLY) {
1239                 sp_tweak_switch_mode(tc, TWEAK_MODE_MORELESS, MOD__SHIFT);
1240                 ret = TRUE;
1241             }
1242             break;
1243         case GDK_p:
1244         case GDK_P:
1245         case GDK_6:
1246             if (MOD__SHIFT_ONLY) {
1247                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH, MOD__SHIFT);
1248                 ret = TRUE;
1249             }
1250             break;
1251         case GDK_s:
1252         case GDK_S:
1253         case GDK_7:
1254             if (MOD__SHIFT_ONLY) {
1255                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1256                 ret = TRUE;
1257             }
1258             break;
1259         case GDK_a:
1260         case GDK_A:
1261         case GDK_8:
1262             if (MOD__SHIFT_ONLY) {
1263                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT_REPEL, MOD__SHIFT);
1264                 ret = TRUE;
1265             }
1266             break;
1267         case GDK_r:
1268         case GDK_R:
1269         case GDK_9:
1270             if (MOD__SHIFT_ONLY) {
1271                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN, MOD__SHIFT);
1272                 ret = TRUE;
1273             }
1274             break;
1275         case GDK_c:
1276         case GDK_C:
1277             if (MOD__SHIFT_ONLY) {
1278                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT, MOD__SHIFT);
1279                 ret = TRUE;
1280             }
1281             break;
1282         case GDK_j:
1283         case GDK_J:
1284             if (MOD__SHIFT_ONLY) {
1285                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER, MOD__SHIFT);
1286                 ret = TRUE;
1287             }
1288             break;
1290         case GDK_Up:
1291         case GDK_KP_Up:
1292             if (!MOD__CTRL_ONLY) {
1293                 tc->force += 0.05;
1294                 if (tc->force > 1.0)
1295                     tc->force = 1.0;
1296                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1297                 ret = TRUE;
1298             }
1299             break;
1300         case GDK_Down:
1301         case GDK_KP_Down:
1302             if (!MOD__CTRL_ONLY) {
1303                 tc->force -= 0.05;
1304                 if (tc->force < 0.0)
1305                     tc->force = 0.0;
1306                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1307                 ret = TRUE;
1308             }
1309             break;
1310         case GDK_Right:
1311         case GDK_KP_Right:
1312             if (!MOD__CTRL_ONLY) {
1313                 tc->width += 0.01;
1314                 if (tc->width > 1.0)
1315                     tc->width = 1.0;
1316                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1317                 sp_tweak_update_area(tc);
1318                 ret = TRUE;
1319             }
1320             break;
1321         case GDK_Left:
1322         case GDK_KP_Left:
1323             if (!MOD__CTRL_ONLY) {
1324                 tc->width -= 0.01;
1325                 if (tc->width < 0.01)
1326                     tc->width = 0.01;
1327                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1328                 sp_tweak_update_area(tc);
1329                 ret = TRUE;
1330             }
1331             break;
1332         case GDK_Home:
1333         case GDK_KP_Home:
1334             tc->width = 0.01;
1335             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1336             sp_tweak_update_area(tc);
1337             ret = TRUE;
1338             break;
1339         case GDK_End:
1340         case GDK_KP_End:
1341             tc->width = 1.0;
1342             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1343             sp_tweak_update_area(tc);
1344             ret = TRUE;
1345             break;
1346         case GDK_x:
1347         case GDK_X:
1348             if (MOD__ALT_ONLY) {
1349                 desktop->setToolboxFocusTo ("altx-tweak");
1350                 ret = TRUE;
1351             }
1352             break;
1354         case GDK_Shift_L:
1355         case GDK_Shift_R:
1356             sp_tweak_update_cursor(tc, true);
1357             break;
1359         case GDK_Control_L:
1360         case GDK_Control_R:
1361             sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1362             break;
1363         default:
1364             break;
1365         }
1366         break;
1368     case GDK_KEY_RELEASE:
1369         switch (get_group0_keyval(&event->key)) {
1370             case GDK_Shift_L:
1371             case GDK_Shift_R:
1372                 sp_tweak_update_cursor(tc, false);
1373                 break;
1374             case GDK_Control_L:
1375             case GDK_Control_R:
1376                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0), MOD__SHIFT);
1377                 tc->_message_context->clear();
1378                 break;
1379             default:
1380                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0), MOD__SHIFT);
1381                 break;
1382         }
1384     default:
1385         break;
1386     }
1388     if (!ret) {
1389         if (((SPEventContextClass *) parent_class)->root_handler) {
1390             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1391         }
1392     }
1394     return ret;
1398 /*
1399   Local Variables:
1400   mode:c++
1401   c-file-style:"stroustrup"
1402   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1403   indent-tabs-mode:nil
1404   fill-column:99
1405   End:
1406 */
1407 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :