Code

Remove ca.new form ALL_LINGUAS.
[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"
51 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
52 static void sp_gradient_context_init(SPGradientContext *gr_context);
53 static void sp_gradient_context_dispose(GObject *object);
55 static void sp_gradient_context_setup(SPEventContext *ec);
57 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
59 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
61 static SPEventContextClass *parent_class;
64 GtkType sp_gradient_context_get_type()
65 {
66     static GType type = 0;
67     if (!type) {
68         GTypeInfo info = {
69             sizeof(SPGradientContextClass),
70             NULL, NULL,
71             (GClassInitFunc) sp_gradient_context_class_init,
72             NULL, NULL,
73             sizeof(SPGradientContext),
74             4,
75             (GInstanceInitFunc) sp_gradient_context_init,
76             NULL,    /* value_table */
77         };
78         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
79     }
80     return type;
81 }
83 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
84 {
85     GObjectClass *object_class = (GObjectClass *) klass;
86     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
90     object_class->dispose = sp_gradient_context_dispose;
92     event_context_class->setup = sp_gradient_context_setup;
93     event_context_class->root_handler  = sp_gradient_context_root_handler;
94 }
96 static void sp_gradient_context_init(SPGradientContext *gr_context)
97 {
98     SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
100     gr_context->cursor_addnode = false;
101     event_context->cursor_shape = cursor_gradient_xpm;
102     event_context->hot_x = 4;
103     event_context->hot_y = 4;
104     event_context->xp = 0;
105     event_context->yp = 0;
106     event_context->tolerance = 6;
107     event_context->within_tolerance = false;
108     event_context->item_to_select = NULL;
111 static void sp_gradient_context_dispose(GObject *object)
113     SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
114     SPEventContext *ec = SP_EVENT_CONTEXT(object);
116     ec->enableGrDrag(false);
118     if (rc->_message_context) {
119         delete rc->_message_context;
120     }
122     G_OBJECT_CLASS(parent_class)->dispose(object);
125 static void sp_gradient_context_setup(SPEventContext *ec)
127     SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
129     if (((SPEventContextClass *) parent_class)->setup) {
130         ((SPEventContextClass *) parent_class)->setup(ec);
131     }
133     if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
134         ec->enableSelectionCue();
135     }
137     ec->enableGrDrag();
139     rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
142 void 
143 sp_gradient_context_select_next (SPEventContext *event_context)
145     GrDrag *drag = event_context->_grdrag;
146     g_assert (drag);
148     drag->select_next();
151 void 
152 sp_gradient_context_select_prev (SPEventContext *event_context)
154     GrDrag *drag = event_context->_grdrag;
155     g_assert (drag);
157     drag->select_prev();
160 // FIXME: make global function in libnr or somewhere.
161 static NR::Point
162 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end)
164     double length = NR::L2(end - begin);
165     NR::Point be = (end - begin) / length;
166     double r = NR::dot(p - begin, be);
167         
168     if (r < 0.0) return begin;
169     if (r > length) return end;    
170     
171     return (begin + r * be);
174 static bool
175 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
177     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
179     //Translate mouse point into proper coord system
180     rc->mousepoint_doc = desktop->w2d(event_p);
182     SPCtrlLine* line = SP_CTRLLINE(item);
183    
184     NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e);
185     double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
187     double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
189     bool close = (dist_screen < tolerance);
191     return close;
194 // Fixme : must be able to put this in a general file.
195 static guint32
196 average_color (guint32 c1, guint32 c2, gdouble p = 0.5)
198         guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
199         guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
200         guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
201         guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
203         return SP_RGBA32_U_COMPOSE (r, g, b, a);
206 static double
207 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
209     double length = NR::L2(end - begin);
210     NR::Point be = (end - begin) / length;
211     double r = NR::dot(p - begin, be);
212         
213     if (r < 0.0) return 0.0;
214     if (r > length) return 1.0;    
215     
216     return (r / length);
219 static void
220 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item,  NR::Point mouse_p, guint32 etime)
222     // item is the selected item. mouse_p the location in doc coordinates of where to add the stop    
223     
224     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
225     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
227     double tolerance = (double) ec->tolerance;
229     gfloat offset; // type of SPStop.offset = gfloat
230     SPGradient *gradient; 
231     bool fill_or_stroke = true; 
232     bool r1_knot = false;
233     
234     bool addknot = false;
235     do {
236         gradient = sp_item_gradient (item, fill_or_stroke);
237         if (SP_IS_LINEARGRADIENT(gradient)) {
238             NR::Point begin   = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
239             NR::Point end     = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
240                 
241             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
242             double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
243             if ( dist_screen < tolerance ) {
244                 // add the knot 
245                 offset = get_offset_between_points(nearest, begin, end); 
246                 addknot = true;              
247                 break; // break out of the while loop: add only one knot
248             }
249         } else if (SP_IS_RADIALGRADIENT(gradient)) {
250             NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
251             NR::Point end   = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
252             NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
253             double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
254             if ( dist_screen < tolerance ) {
255                 offset = get_offset_between_points(nearest, begin, end); 
256                 addknot = true;
257                 r1_knot = true;              
258                 break; // break out of the while loop: add only one knot
259             }
261             end    = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
262             nearest = snap_vector_midpoint (mouse_p, begin, end);
263             dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
264             if ( dist_screen < tolerance ) {
265                 offset = get_offset_between_points(nearest, begin, end); 
266                 addknot = true;
267                 r1_knot = false;              
268                 break; // break out of the while loop: add only one knot
269             }
270         }     
271         fill_or_stroke = !fill_or_stroke;
272     } while (!fill_or_stroke && !addknot) ;
274     if (addknot) {
275         SPGradient *vector = sp_gradient_get_vector (gradient, false);
276         SPStop* prev_stop = sp_first_stop(vector);
277         SPStop* next_stop = sp_next_stop(prev_stop);
278         while ( (next_stop) && (next_stop->offset < offset) ) {
279             prev_stop = next_stop;
280             next_stop = sp_next_stop(next_stop);
281         }
282         if (!next_stop) {
283             // logical error: the endstop should have offset 1 and should always be more than this offset here
284             return;
285         }
286                     
287         Inkscape::XML::Node *new_stop_repr = NULL;
288         new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate();
289         SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
290     
291         SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
292         newstop->offset = offset;
293         sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
294         guint32 const c1 = sp_stop_get_rgba32(prev_stop);
295         guint32 const c2 = sp_stop_get_rgba32(next_stop);
296         guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
297         Inkscape::CSSOStringStream os;
298         gchar c[64];
299         sp_svg_write_color (c, 64, cnew);
300         gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
301         os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
302         SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
304     
305         Inkscape::GC::release(new_stop_repr);
306         sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT,
307                   _("Add gradient stop"));
309         ec->_grdrag->updateDraggers();
310                 sp_gradient_ensure_vector (gradient);
312         if (vector->has_stops) {
313             int i = 0;
314             for ( SPObject *ochild = sp_object_first_child (SP_OBJECT(vector)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) {
315                 if (SP_IS_STOP (ochild)) {
316                     if ( SP_STOP(ochild) == newstop ) {
317                         break;
318                     } else {
319                         i++;
320                     }
321                 }
322             }
323             GrDragger *dragger = NULL;
324             gradient = sp_item_gradient (item, fill_or_stroke);
325             GrPointType pointtype;
326             if (SP_IS_LINEARGRADIENT(gradient)) {
327                 dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, POINT_LG_MID, i, fill_or_stroke);                                         
328                 pointtype = POINT_LG_MID;
329             } else if (SP_IS_RADIALGRADIENT(gradient)) {                
330                 dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, r1_knot ? POINT_RG_MID1 : POINT_RG_MID2, i, fill_or_stroke);                                              
331                 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
332             }
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;
342             
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);
383     
384                     SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop,                                                                                   SP_OBJECT (item), new_fill);
385     
386                     SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
387                     sp_gradient_reset_to_userspace(priv, item);
388                 }
389     
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 ) {
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 */
415             rc->origin = button_dt;
417             ret = TRUE;
418         }
419         break;
420     case GDK_MOTION_NOTIFY:
421         if ( dragging
422              && ( event->motion.state & GDK_BUTTON1_MASK ) )
423         {
424             if ( event_context->within_tolerance
425                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
426                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
427                 break; // do not drag if we're within tolerance from origin
428             }
429             // Once the user has moved farther than tolerance from the original location
430             // (indicating they intend to draw, not click), then always process the
431             // motion notify coordinates as given (no snapping back to origin)
432             event_context->within_tolerance = false;
434             NR::Point const motion_w(event->motion.x,
435                                      event->motion.y);
436             NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
438             sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
440             ret = TRUE;
441         } else {
442             bool over_line = false;
443             if (drag->lines) {
444                 for (GSList *l = drag->lines; l != NULL; l = l->next) {
445                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
446                 }
447             }
449             if (rc->cursor_addnode && !over_line) {
450                 event_context->cursor_shape = cursor_gradient_xpm;
451                 sp_event_context_update_cursor(event_context);
452                 rc->cursor_addnode = false;
453             } else if (!rc->cursor_addnode && over_line) {
454                 event_context->cursor_shape = cursor_gradient_add_xpm;
455                 sp_event_context_update_cursor(event_context);
456                 rc->cursor_addnode = true;
457             }
458         }
459         break;
460     case GDK_BUTTON_RELEASE:
461         event_context->xp = event_context->yp = 0;
462         if ( event->button.button == 1 ) {
463             if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
464                 bool over_line = false;
465                 SPCtrlLine *line = NULL;
466                 if (drag->lines) {
467                     for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
468                         line = (SPCtrlLine*) l->data;                        
469                         over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
470                     }
471                 }
472                 if (over_line) {
473                     sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);   
474                     ret = TRUE;
475                 }
476             } else {    
477                 dragging = false;
478     
479                 // unless clicked with Ctrl (to enable Ctrl+doubleclick).  (don't what this is for (johan))
480                 if (event->button.state & GDK_CONTROL_MASK) {
481                     ret = TRUE;
482                     break;
483                 }
484     
485                 if (!event_context->within_tolerance) {
486                     // we've been dragging, do nothing (grdrag handles that)
487                 } else if (event_context->item_to_select) {
488                     // no dragging, select clicked item if any
489                     if (event->button.state & GDK_SHIFT_MASK) {
490                         selection->toggle(event_context->item_to_select);
491                     } else {
492                         selection->set(event_context->item_to_select);
493                     }
494                 } else {
495                     // click in an empty space; do the same as Esc
496                     if (drag->selected) {
497                         drag->deselectAll();
498                     } else {
499                         selection->clear();
500                     }
501                 }
502     
503                 event_context->item_to_select = NULL;
504                 ret = TRUE;
505             }
506         }
507         break;
508     case GDK_KEY_PRESS:
509         switch (get_group0_keyval (&event->key)) {
510         case GDK_Alt_L:
511         case GDK_Alt_R:
512         case GDK_Control_L:
513         case GDK_Control_R:
514         case GDK_Shift_L:
515         case GDK_Shift_R:
516         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
517         case GDK_Meta_R:
518             sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
519                                         _("<b>Ctrl</b>: snap gradient angle"),
520                                         _("<b>Shift</b>: draw gradient around the starting point"),
521                                         NULL);
522             break;
524         case GDK_x:
525         case GDK_X:
526             if (MOD__ALT_ONLY) {
527                 desktop->setToolboxFocusTo ("altx-grad");
528                 ret = TRUE;
529             }
530             break;
532         case GDK_Escape:
533             if (drag->selected) {
534                 drag->deselectAll();
535             } else {
536                 selection->clear();
537             }
538             ret = TRUE;
539             //TODO: make dragging escapable by Esc
540             break;
542         case GDK_Left: // move handle left
543         case GDK_KP_Left:
544         case GDK_KP_4:
545             if (!MOD__CTRL) { // not ctrl
546                 if (MOD__ALT) { // alt
547                     if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
548                     else drag->selected_move_screen(-1, 0); // no shift
549                 }
550                 else { // no alt
551                     if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
552                     else drag->selected_move(-nudge, 0); // no shift
553                 }
554                 ret = TRUE;
555             }
556             break;
557         case GDK_Up: // move handle up
558         case GDK_KP_Up:
559         case GDK_KP_8:
560             if (!MOD__CTRL) { // not ctrl
561                 if (MOD__ALT) { // alt
562                     if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
563                     else drag->selected_move_screen(0, 1); // no shift
564                 }
565                 else { // no alt
566                     if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
567                     else drag->selected_move(0, nudge); // no shift
568                 }
569                 ret = TRUE;
570             }
571             break;
572         case GDK_Right: // move handle right
573         case GDK_KP_Right:
574         case GDK_KP_6:
575             if (!MOD__CTRL) { // not ctrl
576                 if (MOD__ALT) { // alt
577                     if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
578                     else drag->selected_move_screen(1, 0); // no shift
579                 }
580                 else { // no alt
581                     if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
582                     else drag->selected_move(nudge, 0); // no shift
583                 }
584                 ret = TRUE;
585             }
586             break;
587         case GDK_Down: // move handle down
588         case GDK_KP_Down:
589         case GDK_KP_2:
590             if (!MOD__CTRL) { // not ctrl
591                 if (MOD__ALT) { // alt
592                     if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
593                     else drag->selected_move_screen(0, -1); // no shift
594                 }
595                 else { // no alt
596                     if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
597                     else drag->selected_move(0, -nudge); // no shift
598                 }
599                 ret = TRUE;
600             }
601             break;
602         case GDK_r:
603         case GDK_R:
604             if (MOD__SHIFT_ONLY) {
605                 // First try selected dragger
606                 if (drag && drag->selected) {
607                     drag->selected_reverse_vector();
608                 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
609                     for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
610                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
611                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
612                     }
613                 }
614                 // we did an undoable action
615                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
616                                   _("Invert gradient"));
617                 ret = TRUE;
618             }
619             break;
620 /*                
621         case GDK_Insert:
622         case GDK_KP_Insert:
623             // with any modifiers
624             sp_node_selected_add_node();
625             ret = TRUE;
626             break;
627 */            
628         case GDK_Delete:
629         case GDK_KP_Delete:
630         case GDK_BackSpace:
631             if ( drag->selected ) {
632                 drag->deleteSelected(MOD__CTRL_ONLY);
633                 ret = TRUE;            
634             }
635             break;
636         default:
637             break;
638         }
639         break;
640     case GDK_KEY_RELEASE:
641         switch (get_group0_keyval (&event->key)) {
642         case GDK_Alt_L:
643         case GDK_Alt_R:
644         case GDK_Control_L:
645         case GDK_Control_R:
646         case GDK_Shift_L:
647         case GDK_Shift_R:
648         case GDK_Meta_L:  // Meta is when you press Shift+Alt
649         case GDK_Meta_R:
650             event_context->defaultMessageContext()->clear();
651             break;
652         default:
653             break;
654         }
655         break;
656     default:
657         break;
658     }
660     if (!ret) {
661         if (((SPEventContextClass *) parent_class)->root_handler) {
662             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
663         }
664     }
665     
666     return ret;
669 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
671     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
672     Inkscape::Selection *selection = sp_desktop_selection(desktop);
673     SPDocument *document = sp_desktop_document(desktop);
674     SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
676     if (!selection->isEmpty()) {
677         int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
678         int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
680         SPGradient *vector;
681         if (ec->item_to_select) {
682             vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
683         } else {
684             vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
685         }
687         // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
688         SPCSSAttr *css = sp_repr_css_attr_new();
689         sp_repr_css_set_property(css, "fill-opacity", "1.0");
691         for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
693             //FIXME: see above
694             sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
696             sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
698             if (type == SP_GRADIENT_TYPE_LINEAR) {
699                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
700                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
701             } else if (type == SP_GRADIENT_TYPE_RADIAL) {
702                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
703                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
704             }
705             SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
706         }
707         if (ec->_grdrag) {
708             ec->_grdrag->updateDraggers();
709             // prevent regenerating draggers by selection modified signal, which sometimes
710             // comes too late and thus destroys the knot which we will now grab:
711             ec->_grdrag->local_change = true;
712             // give the grab out-of-bounds values of xp/yp because we're already dragging
713             // and therefore are already out of tolerance
714             ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
715                                    type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
716                                    0, //point_i
717                                    fill_or_stroke, 99999, 99999, etime);
718         }
719         // We did an undoable action, but sp_document_done will be called by the knot when released
721         // status text; we do not track coords because this branch is run once, not all the time
722         // during drag
723         int n_objects = g_slist_length((GSList *) selection->itemList());
724         rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
725                                   ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
726                                            "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
727                                   n_objects);
728     } else {
729         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
730     }
734 /*
735   Local Variables:
736   mode:c++
737   c-file-style:"stroustrup"
738   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
739   indent-tabs-mode:nil
740   fill-column:99
741   End:
742 */
743 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :