Code

fix invalid access warning
[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);
168     if (r < 0.0) return begin;
169     if (r > length) return end;
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);
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);
213     if (r < 0.0) return 0.0;
214     if (r > length) return 1.0;
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
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;
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);
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         }
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));
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());
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             }
324             gradient = sp_item_gradient (item, fill_or_stroke);
325             GrPointType pointtype = POINT_G_INVALID;
326             if (SP_IS_LINEARGRADIENT(gradient)) {
327                 pointtype = POINT_LG_MID;
328             } else if (SP_IS_RADIALGRADIENT(gradient)) {
329                 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
330             }
331             GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
332             if (dragger && (etime == 0) ) {
333                 ec->_grdrag->setSelected (dragger);
334             } else {
335                 ec->_grdrag->grabKnot (item,
336                                    pointtype,
337                                    i,
338                                    fill_or_stroke, 99999, 99999, etime);
339             }
340             ec->_grdrag->local_change = true;
342         }
343     }
347 static gint
348 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
350     static bool dragging;
352     SPDesktop *desktop = event_context->desktop;
353     Inkscape::Selection *selection = sp_desktop_selection (desktop);
355     SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
357     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
358     double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
360     GrDrag *drag = event_context->_grdrag;
361     g_assert (drag);
363     gint ret = FALSE;
364     switch (event->type) {
365     case GDK_2BUTTON_PRESS:
366         if ( event->button.button == 1 ) {
367             bool over_line = false;
368             SPCtrlLine *line = NULL;
369             if (drag->lines) {
370                 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
371                     line = (SPCtrlLine*) l->data;
372                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
373                 }
374             }
375             if (over_line) {
376                 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
377             } else {
378                 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
379                     SPItem *item = SP_ITEM(i->data);
380                     SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
381                     guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
383                     SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop,                                                                                   SP_OBJECT (item), new_fill);
385                     SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
386                     sp_gradient_reset_to_userspace(priv, item);
387                 }
389                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
390                                   _("Create default gradient"));
391             }
392             ret = TRUE;
393         }
394         break;
395     case GDK_BUTTON_PRESS:
396         if ( event->button.button == 1 ) {
397             NR::Point const button_w(event->button.x, event->button.y);
399             // save drag origin
400             event_context->xp = (gint) button_w[NR::X];
401             event_context->yp = (gint) button_w[NR::Y];
402             event_context->within_tolerance = true;
404             // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
405             // enable Ctrl+doubleclick of exactly the selected item(s)
406             if (!(event->button.state & GDK_CONTROL_MASK))
407                 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
409             dragging = true;
410             /* Position center */
411             NR::Point const button_dt = desktop->w2d(button_w);
412             /* Snap center to nearest magnetic point */
414             rc->origin = button_dt;
416             ret = TRUE;
417         }
418         break;
419     case GDK_MOTION_NOTIFY:
420         if ( dragging
421              && ( event->motion.state & GDK_BUTTON1_MASK ) )
422         {
423             if ( event_context->within_tolerance
424                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
425                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
426                 break; // do not drag if we're within tolerance from origin
427             }
428             // Once the user has moved farther than tolerance from the original location
429             // (indicating they intend to draw, not click), then always process the
430             // motion notify coordinates as given (no snapping back to origin)
431             event_context->within_tolerance = false;
433             NR::Point const motion_w(event->motion.x,
434                                      event->motion.y);
435             NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
437             sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
439             ret = TRUE;
440         } else {
441             bool over_line = false;
442             if (drag->lines) {
443                 for (GSList *l = drag->lines; l != NULL; l = l->next) {
444                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
445                 }
446             }
448             if (rc->cursor_addnode && !over_line) {
449                 event_context->cursor_shape = cursor_gradient_xpm;
450                 sp_event_context_update_cursor(event_context);
451                 rc->cursor_addnode = false;
452             } else if (!rc->cursor_addnode && over_line) {
453                 event_context->cursor_shape = cursor_gradient_add_xpm;
454                 sp_event_context_update_cursor(event_context);
455                 rc->cursor_addnode = true;
456             }
457         }
458         break;
459     case GDK_BUTTON_RELEASE:
460         event_context->xp = event_context->yp = 0;
461         if ( event->button.button == 1 ) {
462             if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
463                 bool over_line = false;
464                 SPCtrlLine *line = NULL;
465                 if (drag->lines) {
466                     for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
467                         line = (SPCtrlLine*) l->data;
468                         over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
469                     }
470                 }
471                 if (over_line) {
472                     sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
473                     ret = TRUE;
474                 }
475             } else {
476                 dragging = false;
478                 // unless clicked with Ctrl (to enable Ctrl+doubleclick).  (don't what this is for (johan))
479                 if (event->button.state & GDK_CONTROL_MASK) {
480                     ret = TRUE;
481                     break;
482                 }
484                 if (!event_context->within_tolerance) {
485                     // we've been dragging, do nothing (grdrag handles that)
486                 } else if (event_context->item_to_select) {
487                     // no dragging, select clicked item if any
488                     if (event->button.state & GDK_SHIFT_MASK) {
489                         selection->toggle(event_context->item_to_select);
490                     } else {
491                         selection->set(event_context->item_to_select);
492                     }
493                 } else {
494                     // click in an empty space; do the same as Esc
495                     if (drag->selected) {
496                         drag->deselectAll();
497                     } else {
498                         selection->clear();
499                     }
500                 }
502                 event_context->item_to_select = NULL;
503                 ret = TRUE;
504             }
505         }
506         break;
507     case GDK_KEY_PRESS:
508         switch (get_group0_keyval (&event->key)) {
509         case GDK_Alt_L:
510         case GDK_Alt_R:
511         case GDK_Control_L:
512         case GDK_Control_R:
513         case GDK_Shift_L:
514         case GDK_Shift_R:
515         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
516         case GDK_Meta_R:
517             sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
518                                         _("<b>Ctrl</b>: snap gradient angle"),
519                                         _("<b>Shift</b>: draw gradient around the starting point"),
520                                         NULL);
521             break;
523         case GDK_x:
524         case GDK_X:
525             if (MOD__ALT_ONLY) {
526                 desktop->setToolboxFocusTo ("altx-grad");
527                 ret = TRUE;
528             }
529             break;
531         case GDK_Escape:
532             if (drag->selected) {
533                 drag->deselectAll();
534             } else {
535                 selection->clear();
536             }
537             ret = TRUE;
538             //TODO: make dragging escapable by Esc
539             break;
541         case GDK_Left: // move handle left
542         case GDK_KP_Left:
543         case GDK_KP_4:
544             if (!MOD__CTRL) { // not ctrl
545                 if (MOD__ALT) { // alt
546                     if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
547                     else drag->selected_move_screen(-1, 0); // no shift
548                 }
549                 else { // no alt
550                     if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
551                     else drag->selected_move(-nudge, 0); // no shift
552                 }
553                 ret = TRUE;
554             }
555             break;
556         case GDK_Up: // move handle up
557         case GDK_KP_Up:
558         case GDK_KP_8:
559             if (!MOD__CTRL) { // not ctrl
560                 if (MOD__ALT) { // alt
561                     if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
562                     else drag->selected_move_screen(0, 1); // no shift
563                 }
564                 else { // no alt
565                     if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
566                     else drag->selected_move(0, nudge); // no shift
567                 }
568                 ret = TRUE;
569             }
570             break;
571         case GDK_Right: // move handle right
572         case GDK_KP_Right:
573         case GDK_KP_6:
574             if (!MOD__CTRL) { // not ctrl
575                 if (MOD__ALT) { // alt
576                     if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
577                     else drag->selected_move_screen(1, 0); // no shift
578                 }
579                 else { // no alt
580                     if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
581                     else drag->selected_move(nudge, 0); // no shift
582                 }
583                 ret = TRUE;
584             }
585             break;
586         case GDK_Down: // move handle down
587         case GDK_KP_Down:
588         case GDK_KP_2:
589             if (!MOD__CTRL) { // not ctrl
590                 if (MOD__ALT) { // alt
591                     if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
592                     else drag->selected_move_screen(0, -1); // no shift
593                 }
594                 else { // no alt
595                     if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
596                     else drag->selected_move(0, -nudge); // no shift
597                 }
598                 ret = TRUE;
599             }
600             break;
601         case GDK_r:
602         case GDK_R:
603             if (MOD__SHIFT_ONLY) {
604                 // First try selected dragger
605                 if (drag && drag->selected) {
606                     drag->selected_reverse_vector();
607                 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
608                     for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
609                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
610                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
611                     }
612                 }
613                 // we did an undoable action
614                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
615                                   _("Invert gradient"));
616                 ret = TRUE;
617             }
618             break;
619 /*
620         case GDK_Insert:
621         case GDK_KP_Insert:
622             // with any modifiers
623             sp_node_selected_add_node();
624             ret = TRUE;
625             break;
626 */
627         case GDK_Delete:
628         case GDK_KP_Delete:
629         case GDK_BackSpace:
630             if ( drag->selected ) {
631                 drag->deleteSelected(MOD__CTRL_ONLY);
632                 ret = TRUE;
633             }
634             break;
635         default:
636             break;
637         }
638         break;
639     case GDK_KEY_RELEASE:
640         switch (get_group0_keyval (&event->key)) {
641         case GDK_Alt_L:
642         case GDK_Alt_R:
643         case GDK_Control_L:
644         case GDK_Control_R:
645         case GDK_Shift_L:
646         case GDK_Shift_R:
647         case GDK_Meta_L:  // Meta is when you press Shift+Alt
648         case GDK_Meta_R:
649             event_context->defaultMessageContext()->clear();
650             break;
651         default:
652             break;
653         }
654         break;
655     default:
656         break;
657     }
659     if (!ret) {
660         if (((SPEventContextClass *) parent_class)->root_handler) {
661             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
662         }
663     }
665     return ret;
668 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
670     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
671     Inkscape::Selection *selection = sp_desktop_selection(desktop);
672     SPDocument *document = sp_desktop_document(desktop);
673     SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
675     if (!selection->isEmpty()) {
676         int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
677         int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
679         SPGradient *vector;
680         if (ec->item_to_select) {
681             vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
682         } else {
683             vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
684         }
686         // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
687         SPCSSAttr *css = sp_repr_css_attr_new();
688         sp_repr_css_set_property(css, "fill-opacity", "1.0");
690         for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
692             //FIXME: see above
693             sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
695             sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
697             if (type == SP_GRADIENT_TYPE_LINEAR) {
698                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
699                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
700             } else if (type == SP_GRADIENT_TYPE_RADIAL) {
701                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
702                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
703             }
704             SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
705         }
706         if (ec->_grdrag) {
707             ec->_grdrag->updateDraggers();
708             // prevent regenerating draggers by selection modified signal, which sometimes
709             // comes too late and thus destroys the knot which we will now grab:
710             ec->_grdrag->local_change = true;
711             // give the grab out-of-bounds values of xp/yp because we're already dragging
712             // and therefore are already out of tolerance
713             ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
714                                    type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
715                                    0, //point_i
716                                    fill_or_stroke, 99999, 99999, etime);
717         }
718         // We did an undoable action, but sp_document_done will be called by the knot when released
720         // status text; we do not track coords because this branch is run once, not all the time
721         // during drag
722         int n_objects = g_slist_length((GSList *) selection->itemList());
723         rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
724                                   ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
725                                            "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
726                                   n_objects);
727     } else {
728         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
729     }
733 /*
734   Local Variables:
735   mode:c++
736   c-file-style:"stroustrup"
737   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
738   indent-tabs-mode:nil
739   fill-column:99
740   End:
741 */
742 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :