Code

reenable stop moving by keys, add select-all
[inkscape.git] / src / gradient-context.cpp
1 #define __SP_GRADIENT_CONTEXT_C__
3 /*
4  * Gradient drawing and editing tool
5  *
6  * Authors:
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 2005 Authors
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "macros.h"
24 #include "document.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-handles.h"
28 #include "message-context.h"
29 #include "message-stack.h"
30 #include "pixmaps/cursor-gradient.xpm"
31 #include "pixmaps/cursor-gradient-add.xpm"
32 #include "pixmaps/cursor-gradient-delete.xpm"
33 #include "gradient-context.h"
34 #include "gradient-chemistry.h"
35 #include <glibmm/i18n.h>
36 #include "prefs-utils.h"
37 #include "gradient-drag.h"
38 #include "gradient-chemistry.h"
39 #include "xml/repr.h"
40 #include "sp-item.h"
41 #include "display/sp-ctrlline.h"
42 #include "sp-linear-gradient.h"
43 #include "sp-radial-gradient.h"
44 #include "sp-stop.h"
45 #include "svg/css-ostringstream.h"
46 #include "svg/svg-color.h"
47 #include "snap.h"
48 #include "sp-namedview.h"
52 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
53 static void sp_gradient_context_init(SPGradientContext *gr_context);
54 static void sp_gradient_context_dispose(GObject *object);
56 static void sp_gradient_context_setup(SPEventContext *ec);
58 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
60 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
62 static SPEventContextClass *parent_class;
65 GtkType sp_gradient_context_get_type()
66 {
67     static GType type = 0;
68     if (!type) {
69         GTypeInfo info = {
70             sizeof(SPGradientContextClass),
71             NULL, NULL,
72             (GClassInitFunc) sp_gradient_context_class_init,
73             NULL, NULL,
74             sizeof(SPGradientContext),
75             4,
76             (GInstanceInitFunc) sp_gradient_context_init,
77             NULL,    /* value_table */
78         };
79         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
80     }
81     return type;
82 }
84 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
85 {
86     GObjectClass *object_class = (GObjectClass *) klass;
87     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
91     object_class->dispose = sp_gradient_context_dispose;
93     event_context_class->setup = sp_gradient_context_setup;
94     event_context_class->root_handler  = sp_gradient_context_root_handler;
95 }
97 static void sp_gradient_context_init(SPGradientContext *gr_context)
98 {
99     SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
101     gr_context->cursor_addnode = false;
102     event_context->cursor_shape = cursor_gradient_xpm;
103     event_context->hot_x = 4;
104     event_context->hot_y = 4;
105     event_context->xp = 0;
106     event_context->yp = 0;
107     event_context->tolerance = 6;
108     event_context->within_tolerance = false;
109     event_context->item_to_select = NULL;
112 static void sp_gradient_context_dispose(GObject *object)
114     SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
115     SPEventContext *ec = SP_EVENT_CONTEXT(object);
117     ec->enableGrDrag(false);
119     if (rc->_message_context) {
120         delete rc->_message_context;
121     }
123     G_OBJECT_CLASS(parent_class)->dispose(object);
126 static void sp_gradient_context_setup(SPEventContext *ec)
128     SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
130     if (((SPEventContextClass *) parent_class)->setup) {
131         ((SPEventContextClass *) parent_class)->setup(ec);
132     }
134     if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
135         ec->enableSelectionCue();
136     }
138     ec->enableGrDrag();
140     rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
143 void
144 sp_gradient_context_select_next (SPEventContext *event_context)
146     GrDrag *drag = event_context->_grdrag;
147     g_assert (drag);
149     drag->select_next();
152 void
153 sp_gradient_context_select_prev (SPEventContext *event_context)
155     GrDrag *drag = event_context->_grdrag;
156     g_assert (drag);
158     drag->select_prev();
161 // FIXME: make global function in libnr or somewhere.
162 static NR::Point
163 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end)
165     double length = NR::L2(end - begin);
166     NR::Point be = (end - begin) / length;
167     double r = NR::dot(p - begin, be);
169     if (r < 0.0) return begin;
170     if (r > length) return end;
172     return (begin + r * be);
175 static bool
176 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
178     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
180     //Translate mouse point into proper coord system
181     rc->mousepoint_doc = desktop->w2d(event_p);
183     SPCtrlLine* line = SP_CTRLLINE(item);
185     NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e);
186     double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
188     double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
190     bool close = (dist_screen < tolerance);
192     return close;
195 // Fixme : must be able to put this in a general file.
196 static guint32
197 average_color (guint32 c1, guint32 c2, gdouble p = 0.5)
199         guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
200         guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
201         guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
202         guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
204         return SP_RGBA32_U_COMPOSE (r, g, b, a);
207 static double
208 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
210     double length = NR::L2(end - begin);
211     NR::Point be = (end - begin) / length;
212     double r = NR::dot(p - begin, be);
214     if (r < 0.0) return 0.0;
215     if (r > length) return 1.0;
217     return (r / length);
220 static void
221 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item,  NR::Point mouse_p, guint32 etime)
223     // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
225     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
226     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
228     double tolerance = (double) ec->tolerance;
230     gfloat offset; // type of SPStop.offset = gfloat
231     SPGradient *gradient;
232     bool fill_or_stroke = true;
233     bool r1_knot = false;
235     bool addknot = false;
236     do {
237         gradient = sp_item_gradient (item, fill_or_stroke);
238         if (SP_IS_LINEARGRADIENT(gradient)) {
239             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
240             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
242             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
243             double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
244             if ( dist_screen < tolerance ) {
245                 // add the knot
246                 offset = get_offset_between_points(nearest, begin, end);
247                 addknot = true;
248                 break; // break out of the while loop: add only one knot
249             }
250         } else if (SP_IS_RADIALGRADIENT(gradient)) {
251             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
252             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
253             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
254             double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
255             if ( dist_screen < tolerance ) {
256                 offset = get_offset_between_points(nearest, begin, end);
257                 addknot = true;
258                 r1_knot = true;
259                 break; // break out of the while loop: add only one knot
260             }
262             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
263             nearest = snap_vector_midpoint (mouse_p, begin, end);
264             dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
265             if ( dist_screen < tolerance ) {
266                 offset = get_offset_between_points(nearest, begin, end);
267                 addknot = true;
268                 r1_knot = false;
269                 break; // break out of the while loop: add only one knot
270             }
271         }
272         fill_or_stroke = !fill_or_stroke;
273     } while (!fill_or_stroke && !addknot) ;
275     if (addknot) {
276         SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
277         SPStop* prev_stop = sp_first_stop(vector);
278         SPStop* next_stop = sp_next_stop(prev_stop);
279         while ( (next_stop) && (next_stop->offset < offset) ) {
280             prev_stop = next_stop;
281             next_stop = sp_next_stop(next_stop);
282         }
283         if (!next_stop) {
284             // logical error: the endstop should have offset 1 and should always be more than this offset here
285             return;
286         }
288         Inkscape::XML::Node *new_stop_repr = NULL;
289         new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
290         SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
292         SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
293         newstop->offset = offset;
294         sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
295         guint32 const c1 = sp_stop_get_rgba32(prev_stop);
296         guint32 const c2 = sp_stop_get_rgba32(next_stop);
297         guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
298         Inkscape::CSSOStringStream os;
299         gchar c[64];
300         sp_svg_write_color (c, sizeof(c), cnew);
301         gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
302         os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
303         SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
306         Inkscape::GC::release(new_stop_repr);
307         sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT,
308                   _("Add gradient stop"));
310         ec->_grdrag->updateDraggers();
311         sp_gradient_ensure_vector (gradient);
313         if (vector->has_stops) {
314             int i = 0;
315             for ( SPObject *ochild = sp_object_first_child (SP_OBJECT(vector)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) {
316                 if (SP_IS_STOP (ochild)) {
317                     if ( SP_STOP(ochild) == newstop ) {
318                         break;
319                     } else {
320                         i++;
321                     }
322                 }
323             }
325             gradient = sp_item_gradient (item, fill_or_stroke);
326             GrPointType pointtype = POINT_G_INVALID;
327             if (SP_IS_LINEARGRADIENT(gradient)) {
328                 pointtype = POINT_LG_MID;
329             } else if (SP_IS_RADIALGRADIENT(gradient)) {
330                 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
331             }
332             GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
333             if (dragger && (etime == 0) ) {
334                 ec->_grdrag->setSelected (dragger);
335             } else {
336                 ec->_grdrag->grabKnot (item,
337                                    pointtype,
338                                    i,
339                                    fill_or_stroke, 99999, 99999, etime);
340             }
341             ec->_grdrag->local_change = true;
343         }
344     }
348 static gint
349 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
351     static bool dragging;
353     SPDesktop *desktop = event_context->desktop;
354     Inkscape::Selection *selection = sp_desktop_selection (desktop);
356     SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
358     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
359     double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
361     GrDrag *drag = event_context->_grdrag;
362     g_assert (drag);
364     gint ret = FALSE;
365     switch (event->type) {
366     case GDK_2BUTTON_PRESS:
367         if ( event->button.button == 1 ) {
368             bool over_line = false;
369             SPCtrlLine *line = NULL;
370             if (drag->lines) {
371                 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
372                     line = (SPCtrlLine*) l->data;
373                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
374                 }
375             }
376             if (over_line) {
377                 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
378             } else {
379                 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
380                     SPItem *item = SP_ITEM(i->data);
381                     SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
382                     guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
384                     SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop,                                                                                   SP_OBJECT (item), new_fill);
386                     SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
387                     sp_gradient_reset_to_userspace(priv, item);
388                 }
390                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
391                                   _("Create default gradient"));
392             }
393             ret = TRUE;
394         }
395         break;
396     case GDK_BUTTON_PRESS:
397         if ( event->button.button == 1 && !event_context->space_panning ) {
398             NR::Point const button_w(event->button.x, event->button.y);
400             // save drag origin
401             event_context->xp = (gint) button_w[NR::X];
402             event_context->yp = (gint) button_w[NR::Y];
403             event_context->within_tolerance = true;
405             // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
406             // enable Ctrl+doubleclick of exactly the selected item(s)
407             if (!(event->button.state & GDK_CONTROL_MASK))
408                 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
410             dragging = true;
411             /* Position center */
412             NR::Point const button_dt = desktop->w2d(button_w);
413             /* Snap center to nearest magnetic point */
414             
415             SnapManager const &m = desktop->namedview->snap_manager;
416             rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
418             ret = TRUE;
419         }
420         break;
421     case GDK_MOTION_NOTIFY:
422         if ( dragging
423              && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
424         {
425             if ( event_context->within_tolerance
426                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
427                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
428                 break; // do not drag if we're within tolerance from origin
429             }
430             // Once the user has moved farther than tolerance from the original location
431             // (indicating they intend to draw, not click), then always process the
432             // motion notify coordinates as given (no snapping back to origin)
433             event_context->within_tolerance = false;
435             NR::Point const motion_w(event->motion.x,
436                                      event->motion.y);
437             NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
439             sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
441             ret = TRUE;
442         } else {
443             bool over_line = false;
444             if (drag->lines) {
445                 for (GSList *l = drag->lines; l != NULL; l = l->next) {
446                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
447                 }
448             }
450             if (rc->cursor_addnode && !over_line) {
451                 event_context->cursor_shape = cursor_gradient_xpm;
452                 sp_event_context_update_cursor(event_context);
453                 rc->cursor_addnode = false;
454             } else if (!rc->cursor_addnode && over_line) {
455                 event_context->cursor_shape = cursor_gradient_add_xpm;
456                 sp_event_context_update_cursor(event_context);
457                 rc->cursor_addnode = true;
458             }
459         }
460         break;
461     case GDK_BUTTON_RELEASE:
462         event_context->xp = event_context->yp = 0;
463         if ( event->button.button == 1 && !event_context->space_panning ) {
464             if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
465                 bool over_line = false;
466                 SPCtrlLine *line = NULL;
467                 if (drag->lines) {
468                     for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
469                         line = (SPCtrlLine*) l->data;
470                         over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
471                     }
472                 }
473                 if (over_line) {
474                     sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
475                     ret = TRUE;
476                 }
477             } else {
478                 dragging = false;
480                 // unless clicked with Ctrl (to enable Ctrl+doubleclick).  (don't what this is for (johan))
481                 if (event->button.state & GDK_CONTROL_MASK) {
482                     ret = TRUE;
483                     break;
484                 }
486                 if (!event_context->within_tolerance) {
487                     // we've been dragging, do nothing (grdrag handles that)
488                 } else if (event_context->item_to_select) {
489                     // no dragging, select clicked item if any
490                     if (event->button.state & GDK_SHIFT_MASK) {
491                         selection->toggle(event_context->item_to_select);
492                     } else {
493                         selection->set(event_context->item_to_select);
494                     }
495                 } else {
496                     // click in an empty space; do the same as Esc
497                     if (drag->selected) {
498                         drag->deselectAll();
499                     } else {
500                         selection->clear();
501                     }
502                 }
504                 event_context->item_to_select = NULL;
505                 ret = TRUE;
506             }
507         }
508         break;
509     case GDK_KEY_PRESS:
510         switch (get_group0_keyval (&event->key)) {
511         case GDK_Alt_L:
512         case GDK_Alt_R:
513         case GDK_Control_L:
514         case GDK_Control_R:
515         case GDK_Shift_L:
516         case GDK_Shift_R:
517         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
518         case GDK_Meta_R:
519             sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
520                                         _("<b>Ctrl</b>: snap gradient angle"),
521                                         _("<b>Shift</b>: draw gradient around the starting point"),
522                                         NULL);
523             break;
525         case GDK_x:
526         case GDK_X:
527             if (MOD__ALT_ONLY) {
528                 desktop->setToolboxFocusTo ("altx-grad");
529                 ret = TRUE;
530             }
531             break;
533         case GDK_A:
534         case GDK_a:
535             if (MOD__CTRL_ONLY) {
536                 drag->selectAll();
537             }
538             ret = TRUE;
539             break;
541         case GDK_Escape:
542             if (drag->selected) {
543                 drag->deselectAll();
544             } else {
545                 selection->clear();
546             }
547             ret = TRUE;
548             //TODO: make dragging escapable by Esc
549             break;
551         case GDK_Left: // move handle left
552         case GDK_KP_Left:
553         case GDK_KP_4:
554             if (!MOD__CTRL) { // not ctrl
555                 if (MOD__ALT) { // alt
556                     if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
557                     else drag->selected_move_screen(-1, 0); // no shift
558                 }
559                 else { // no alt
560                     if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
561                     else drag->selected_move(-nudge, 0); // no shift
562                 }
563                 ret = TRUE;
564             }
565             break;
566         case GDK_Up: // move handle up
567         case GDK_KP_Up:
568         case GDK_KP_8:
569             if (!MOD__CTRL) { // not ctrl
570                 if (MOD__ALT) { // alt
571                     if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
572                     else drag->selected_move_screen(0, 1); // no shift
573                 }
574                 else { // no alt
575                     if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
576                     else drag->selected_move(0, nudge); // no shift
577                 }
578                 ret = TRUE;
579             }
580             break;
581         case GDK_Right: // move handle right
582         case GDK_KP_Right:
583         case GDK_KP_6:
584             if (!MOD__CTRL) { // not ctrl
585                 if (MOD__ALT) { // alt
586                     if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
587                     else drag->selected_move_screen(1, 0); // no shift
588                 }
589                 else { // no alt
590                     if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
591                     else drag->selected_move(nudge, 0); // no shift
592                 }
593                 ret = TRUE;
594             }
595             break;
596         case GDK_Down: // move handle down
597         case GDK_KP_Down:
598         case GDK_KP_2:
599             if (!MOD__CTRL) { // not ctrl
600                 if (MOD__ALT) { // alt
601                     if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
602                     else drag->selected_move_screen(0, -1); // no shift
603                 }
604                 else { // no alt
605                     if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
606                     else drag->selected_move(0, -nudge); // no shift
607                 }
608                 ret = TRUE;
609             }
610             break;
611         case GDK_r:
612         case GDK_R:
613             if (MOD__SHIFT_ONLY) {
614                 // First try selected dragger
615                 if (drag && drag->selected) {
616                     drag->selected_reverse_vector();
617                 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
618                     for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
619                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
620                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
621                     }
622                 }
623                 // we did an undoable action
624                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
625                                   _("Invert gradient"));
626                 ret = TRUE;
627             }
628             break;
629 /*
630         case GDK_Insert:
631         case GDK_KP_Insert:
632             // with any modifiers:
633             // insert mid-stops between selected stops in gradient, or between all stops if none or only one selected
634             ret = TRUE;
635             break;
636 */
637         case GDK_Delete:
638         case GDK_KP_Delete:
639         case GDK_BackSpace:
640             if ( drag->selected ) {
641                 drag->deleteSelected(MOD__CTRL_ONLY);
642                 ret = TRUE;
643             }
644             break;
645         default:
646             break;
647         }
648         break;
649     case GDK_KEY_RELEASE:
650         switch (get_group0_keyval (&event->key)) {
651         case GDK_Alt_L:
652         case GDK_Alt_R:
653         case GDK_Control_L:
654         case GDK_Control_R:
655         case GDK_Shift_L:
656         case GDK_Shift_R:
657         case GDK_Meta_L:  // Meta is when you press Shift+Alt
658         case GDK_Meta_R:
659             event_context->defaultMessageContext()->clear();
660             break;
661         default:
662             break;
663         }
664         break;
665     default:
666         break;
667     }
669     if (!ret) {
670         if (((SPEventContextClass *) parent_class)->root_handler) {
671             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
672         }
673     }
675     return ret;
678 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
680     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
681     Inkscape::Selection *selection = sp_desktop_selection(desktop);
682     SPDocument *document = sp_desktop_document(desktop);
683     SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
685     if (!selection->isEmpty()) {
686         int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
687         int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
689         SPGradient *vector;
690         if (ec->item_to_select) {
691             vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
692         } else {
693             vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
694         }
696         // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
697         SPCSSAttr *css = sp_repr_css_attr_new();
698         sp_repr_css_set_property(css, "fill-opacity", "1.0");
700         for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
702             //FIXME: see above
703             sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
705             sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
707             if (type == SP_GRADIENT_TYPE_LINEAR) {
708                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
709                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
710             } else if (type == SP_GRADIENT_TYPE_RADIAL) {
711                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
712                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
713             }
714             SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
715         }
716         if (ec->_grdrag) {
717             ec->_grdrag->updateDraggers();
718             // prevent regenerating draggers by selection modified signal, which sometimes
719             // comes too late and thus destroys the knot which we will now grab:
720             ec->_grdrag->local_change = true;
721             // give the grab out-of-bounds values of xp/yp because we're already dragging
722             // and therefore are already out of tolerance
723             ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
724                                    type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
725                                    0, //point_i
726                                    fill_or_stroke, 99999, 99999, etime);
727         }
728         // We did an undoable action, but sp_document_done will be called by the knot when released
730         // status text; we do not track coords because this branch is run once, not all the time
731         // during drag
732         int n_objects = g_slist_length((GSList *) selection->itemList());
733         rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
734                                   ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
735                                            "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
736                                   n_objects);
737     } else {
738         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
739     }
743 /*
744   Local Variables:
745   mode:c++
746   c-file-style:"stroustrup"
747   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
748   indent-tabs-mode:nil
749   fill-column:99
750   End:
751 */
752 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :