Code

fix warnings and delete gradient pixmap that will not be used
[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 "gradient-context.h"
33 #include "gradient-chemistry.h"
34 #include <glibmm/i18n.h>
35 #include "prefs-utils.h"
36 #include "gradient-drag.h"
37 #include "gradient-chemistry.h"
38 #include "xml/repr.h"
39 #include "sp-item.h"
40 #include "display/sp-ctrlline.h"
41 #include "sp-linear-gradient.h"
42 #include "sp-radial-gradient.h"
43 #include "sp-stop.h"
44 #include "svg/css-ostringstream.h"
45 #include "svg/svg-color.h"
46 #include "snap.h"
47 #include "sp-namedview.h"
48 #include "rubberband.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     rc->selcon->disconnect();
124     delete rc->selcon;
125     rc->subselcon->disconnect();
126     delete rc->subselcon;
128     G_OBJECT_CLASS(parent_class)->dispose(object);
131 const gchar *gr_handle_descr [] = {
132     N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
133     N_("Linear gradient <b>end</b>"),
134     N_("Linear gradient <b>mid stop</b>"),
135     N_("Radial gradient <b>center</b>"),
136     N_("Radial gradient <b>radius</b>"),
137     N_("Radial gradient <b>radius</b>"),
138     N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
139     N_("Radial gradient <b>mid stop</b>"),
140     N_("Radial gradient <b>mid stop</b>")
141 };
143 static void 
144 gradient_selection_changed (Inkscape::Selection *, gpointer data)
146     SPGradientContext *rc = (SPGradientContext *) data;
148     GrDrag *drag = rc->_grdrag;
149     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(rc)->desktop);
150     guint n_obj = g_slist_length((GSList *) selection->itemList());
152     if (!drag->isNonEmpty() || selection->isEmpty())
153         return;
154     guint n_tot = drag->numDraggers();
155     guint n_sel = drag->numSelected();
157     if (n_sel == 1) {
158         if (drag->singleSelectedDraggerNumDraggables() == 1) {
159             rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
160                     _("%s selected out of %d gradient handles on %d selected object(s)"), gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()], n_tot, n_obj);
161         } else {
162             rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
163                     _("One handle merging %d stops (drag with <b>Shift</b> to separate) selected out of %d gradient handles on %d selected object(s)"), drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
164         }
165     } else if (n_sel > 1) {
166         rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
167                                    _("<b>%d</b> gradient handles selected out of %d on %d selected object(s)"), n_sel, n_tot, n_obj);
168     } else if (n_sel == 0) {
169         rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
170                                    _("<b>No</b> gradient handles selected out of %d on %d selected object(s)"), n_tot, n_obj);
171     }
174 static void
175 gradient_subselection_changed (gpointer, gpointer data)
177     gradient_selection_changed (NULL, data);
181 static void sp_gradient_context_setup(SPEventContext *ec)
183     SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
185     if (((SPEventContextClass *) parent_class)->setup) {
186         ((SPEventContextClass *) parent_class)->setup(ec);
187     }
189     if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
190         ec->enableSelectionCue();
191     }
193     ec->enableGrDrag();
194     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
196     rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
198     rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
199     rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
200     gradient_selection_changed(selection, rc);
203 void
204 sp_gradient_context_select_next (SPEventContext *event_context)
206     GrDrag *drag = event_context->_grdrag;
207     g_assert (drag);
209     GrDragger *d = drag->select_next();
211     event_context->desktop->scroll_to_point(&(d->point), 1.0);
214 void
215 sp_gradient_context_select_prev (SPEventContext *event_context)
217     GrDrag *drag = event_context->_grdrag;
218     g_assert (drag);
220     GrDragger *d = drag->select_prev();
222     event_context->desktop->scroll_to_point(&(d->point), 1.0);
225 static bool
226 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
228     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
230     //Translate mouse point into proper coord system
231     rc->mousepoint_doc = desktop->w2d(event_p);
233     SPCtrlLine* line = SP_CTRLLINE(item);
235     NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e, 0);
236     double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
238     double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
240     bool close = (dist_screen < tolerance);
242     return close;
245 std::vector<NR::Point>
246 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
248     std::vector<NR::Point> coords;
250     // for all selected draggers
251     for (GList *i = drag->selected; i != NULL; i = i->next) {
252         GrDragger *dragger = (GrDragger *) i->data;
253         // remember the coord of the dragger to reselect it later
254         coords.push_back(dragger->point);
255         // for all draggables of dragger
256         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) { 
257             GrDraggable *d = (GrDraggable *) j->data;
259             // find the gradient
260             SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
261             SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
263             // these draggable types cannot have a next draggabe to insert a stop between them
264             if (d->point_type == POINT_LG_END || 
265                 d->point_type == POINT_RG_FOCUS || 
266                 d->point_type == POINT_RG_R1 || 
267                 d->point_type == POINT_RG_R2) {
268                 continue;
269             }
271             // from draggables to stops
272             SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
273             SPStop *next_stop = sp_next_stop (this_stop);
274             SPStop *last_stop = sp_last_stop (vector);
276             gint fs = d->fill_or_stroke;
277             SPItem *item = d->item;
278             gint type = d->point_type;
279             gint p_i = d->point_i;
281             // if there's a next stop,
282             if (next_stop) {
283                 GrDragger *dnext = NULL;
284                 // find its dragger 
285                 // (complex because it may have different types, and because in radial,
286                 // more than one dragger may correspond to a stop, so we must distinguish)
287                 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
288                     if (next_stop == last_stop)
289                         dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
290                     else
291                         dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
292                 } else { // radial
293                     if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
294                         if (next_stop == last_stop)
295                             dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
296                         else 
297                             dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
298                     } 
299                     if ((type == POINT_RG_MID2) || 
300                         (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
301                         if (next_stop == last_stop)
302                             dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
303                         else 
304                             dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
305                     }
306                 }
308                 // if both adjacent draggers selected,
309                 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
311                     // remember the coords of the future dragger to select it
312                     coords.push_back(0.5*(dragger->point + dnext->point));
314                     // do not insert a stop now, it will confuse the loop;
315                     // just remember the stops
316                     *these_stops = g_slist_prepend (*these_stops, this_stop);
317                     *next_stops = g_slist_prepend (*next_stops, next_stop);
318                 }
319             }
320         }
321     }
322     return coords;
325 static void
326 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
328     SPDocument *doc = NULL;
329     GrDrag *drag = rc->_grdrag;
331     GSList *these_stops = NULL;
332     GSList *next_stops = NULL;
334     std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
336     if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
337         // if a single stop is selected, add between that stop and the next one
338         GrDragger *dragger = (GrDragger *) drag->selected->data;
339         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) { 
340             GrDraggable *d = (GrDraggable *) j->data;
341             SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
342             SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
343             SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
344             SPStop *next_stop = sp_next_stop (this_stop);
345             if (this_stop && next_stop) {
346                 these_stops = g_slist_prepend (these_stops, this_stop);
347                 next_stops = g_slist_prepend (next_stops, next_stop);
348             }
349         }
350     }
352     // now actually create the new stops
353     GSList *i = these_stops;
354     GSList *j = next_stops;
355     for (; i != NULL && j != NULL; i = i->next, j = j->next) {
356         SPStop *this_stop = (SPStop *) i->data;
357         SPStop *next_stop = (SPStop *) j->data;
358         gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
359         SPObject *parent = SP_OBJECT_PARENT(this_stop);
360         if (SP_IS_GRADIENT (parent)) {
361             doc = SP_OBJECT_DOCUMENT (parent);
362             sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
363             sp_gradient_ensure_vector (SP_GRADIENT (parent));
364         }
365     }
367     if (g_slist_length(these_stops) > 0 && doc) {
368         sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
369         drag->updateDraggers();
370         // so that it does not automatically update draggers in idle loop, as this would deselect
371         drag->local_change = true;
372         // select all the old selected and new created draggers
373         drag->selectByCoords(coords);
374     }
376     g_slist_free (these_stops);
377     g_slist_free (next_stops);
380 double sqr(double x) {return x*x;}
382 static void
383 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
385     SPDocument *doc = NULL;
386     GrDrag *drag = rc->_grdrag;
388     GSList *these_stops = NULL;
389     GSList *next_stops = NULL;
391     std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
393     GSList *todel = NULL;
395     GSList *i = these_stops;
396     GSList *j = next_stops;
397     for (; i != NULL && j != NULL; i = i->next, j = j->next) {
398         SPStop *stop0 = (SPStop *) i->data;
399         SPStop *stop1 = (SPStop *) j->data;
401         gint i1 = g_slist_index(these_stops, stop1);
402         if (i1 != -1) {
403             GSList *next_next = g_slist_nth (next_stops, i1);
404             if (next_next) {
405                 SPStop *stop2 = (SPStop *) next_next->data;
407                 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
408                     continue;
410                 guint32 const c0 = sp_stop_get_rgba32(stop0);
411                 guint32 const c2 = sp_stop_get_rgba32(stop2);
412                 guint32 const c1r = sp_stop_get_rgba32(stop1);
413                 guint32 c1 = average_color (c0, c2, 
414                        (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
416                 double diff = 
417                     sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
418                     sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
419                     sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
420                     sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
422                 if (diff < tolerance)
423                     todel = g_slist_prepend (todel, stop1);
424             }
425         }
426     }
428     for (i = todel; i != NULL; i = i->next) {
429         SPStop *stop = (SPStop*) i->data;
430         doc = SP_OBJECT_DOCUMENT (stop);
431         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
432         parent->removeChild(SP_OBJECT_REPR(stop));
433     }
435     if (g_slist_length(todel) > 0) {
436         sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
437         drag->local_change = true;
438         drag->updateDraggers();
439         drag->selectByCoords(coords);
440     }
442     g_slist_free (todel);
443     g_slist_free (these_stops);
444     g_slist_free (next_stops);
448 static void
449 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item,  NR::Point mouse_p, guint32 /*etime*/)
451     // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
453     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
454     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
456     double tolerance = (double) ec->tolerance;
458     ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
460     sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
461                       _("Add gradient stop"));
463     ec->get_drag()->updateDraggers();
467 static gint
468 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
470     static bool dragging;
472     SPDesktop *desktop = event_context->desktop;
473     Inkscape::Selection *selection = sp_desktop_selection (desktop);
475     SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
477     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
478     double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
480     GrDrag *drag = event_context->_grdrag;
481     g_assert (drag);
483     gint ret = FALSE;
484     switch (event->type) {
485     case GDK_2BUTTON_PRESS:
486         if ( event->button.button == 1 ) {
487             bool over_line = false;
488             SPCtrlLine *line = NULL;
489             if (drag->lines) {
490                 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
491                     line = (SPCtrlLine*) l->data;
492                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
493                 }
494             }
495             if (over_line) {
496                 // we take the first item in selection, because with doubleclick, the first click
497                 // always resets selection to the single object under cursor
498                 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
499             } else {
500                 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
501                     SPItem *item = SP_ITEM(i->data);
502                     SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
503                     guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
505                     SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop,                                                                                   SP_OBJECT (item), new_fill);
507                     SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
508                     sp_gradient_reset_to_userspace(priv, item);
509                 }
511                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
512                                   _("Create default gradient"));
513             }
514             ret = TRUE;
515         }
516         break;
517     case GDK_BUTTON_PRESS:
518         if ( event->button.button == 1 && !event_context->space_panning ) {
519             NR::Point const button_w(event->button.x, event->button.y);
521             // save drag origin
522             event_context->xp = (gint) button_w[NR::X];
523             event_context->yp = (gint) button_w[NR::Y];
524             event_context->within_tolerance = true;
526             dragging = true;
528             NR::Point const button_dt = desktop->w2d(button_w);
529             if (event->button.state & GDK_SHIFT_MASK) {
530                 Inkscape::Rubberband::get()->start(desktop, button_dt);
531             } else {
532                 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
533                 // enable Ctrl+doubleclick of exactly the selected item(s)
534                 if (!(event->button.state & GDK_CONTROL_MASK))
535                     event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
537                 /* Snap center to nearest magnetic point */
538                 SnapManager const &m = desktop->namedview->snap_manager;
539                 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
540             }
542             ret = TRUE;
543         }
544         break;
545     case GDK_MOTION_NOTIFY:
546         if ( dragging
547              && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
548         {
549             if ( event_context->within_tolerance
550                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
551                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
552                 break; // do not drag if we're within tolerance from origin
553             }
554             // Once the user has moved farther than tolerance from the original location
555             // (indicating they intend to draw, not click), then always process the
556             // motion notify coordinates as given (no snapping back to origin)
557             event_context->within_tolerance = false;
559             NR::Point const motion_w(event->motion.x,
560                                      event->motion.y);
561             NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
563             if (Inkscape::Rubberband::get()->is_started()) {
564                 Inkscape::Rubberband::get()->move(motion_dt);
565                 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
566             } else {
567                 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
568             }
569             gobble_motion_events(GDK_BUTTON1_MASK);
571             ret = TRUE;
572         } else {
573             bool over_line = false;
574             if (drag->lines) {
575                 for (GSList *l = drag->lines; l != NULL; l = l->next) {
576                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
577                 }
578             }
580             if (rc->cursor_addnode && !over_line) {
581                 event_context->cursor_shape = cursor_gradient_xpm;
582                 sp_event_context_update_cursor(event_context);
583                 rc->cursor_addnode = false;
584             } else if (!rc->cursor_addnode && over_line) {
585                 event_context->cursor_shape = cursor_gradient_add_xpm;
586                 sp_event_context_update_cursor(event_context);
587                 rc->cursor_addnode = true;
588             }
589         }
590         break;
591     case GDK_BUTTON_RELEASE:
592         event_context->xp = event_context->yp = 0;
593         if ( event->button.button == 1 && !event_context->space_panning ) {
594             if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
595                 bool over_line = false;
596                 SPCtrlLine *line = NULL;
597                 if (drag->lines) {
598                     for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
599                         line = (SPCtrlLine*) l->data;
600                         over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
601                         if (over_line)
602                             break;
603                     }
604                 }
605                 if (over_line && line) {
606                     sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
607                     ret = TRUE;
608                 }
609             } else {
610                 dragging = false;
612                 // unless clicked with Ctrl (to enable Ctrl+doubleclick).  
613                 if (event->button.state & GDK_CONTROL_MASK) {
614                     ret = TRUE;
615                     break;
616                 }
618                 if (!event_context->within_tolerance) {
619                     // we've been dragging, either do nothing (grdrag handles that),
620                     // or rubberband-select if we have rubberband
621                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
622                     if (r->is_started() && !event_context->within_tolerance) {
623                         // this was a rubberband drag
624                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
625                             NR::Maybe<NR::Rect> const b = r->getRectangle();
626                             drag->selectRect(*b);
627                         }
628                     }
630                 } else if (event_context->item_to_select) {
631                     // no dragging, select clicked item if any
632                     if (event->button.state & GDK_SHIFT_MASK) {
633                         selection->toggle(event_context->item_to_select);
634                     } else {
635                         selection->set(event_context->item_to_select);
636                     }
637                 } else {
638                     // click in an empty space; do the same as Esc
639                     if (drag->selected) {
640                         drag->deselectAll();
641                     } else {
642                         selection->clear();
643                     }
644                 }
646                 event_context->item_to_select = NULL;
647                 ret = TRUE;
648             }
649             Inkscape::Rubberband::get()->stop(); 
650         }
651         break;
652     case GDK_KEY_PRESS:
653         switch (get_group0_keyval (&event->key)) {
654         case GDK_Alt_L:
655         case GDK_Alt_R:
656         case GDK_Control_L:
657         case GDK_Control_R:
658         case GDK_Shift_L:
659         case GDK_Shift_R:
660         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
661         case GDK_Meta_R:
662             sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
663                                         _("<b>Ctrl</b>: snap gradient angle"),
664                                         _("<b>Shift</b>: draw gradient around the starting point"),
665                                         NULL);
666             break;
668         case GDK_x:
669         case GDK_X:
670             if (MOD__ALT_ONLY) {
671                 desktop->setToolboxFocusTo ("altx-grad");
672                 ret = TRUE;
673             }
674             break;
676         case GDK_A:
677         case GDK_a:
678             if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
679                 drag->selectAll();
680                 ret = TRUE;
681             }
682             break;
684         case GDK_L:
685         case GDK_l:
686             if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
687                 sp_gradient_simplify(rc, 1e-4);
688                 ret = TRUE;
689             }
690             break;
692         case GDK_Escape:
693             if (drag->selected) {
694                 drag->deselectAll();
695             } else {
696                 selection->clear();
697             }
698             ret = TRUE;
699             //TODO: make dragging escapable by Esc
700             break;
702         case GDK_Left: // move handle left
703         case GDK_KP_Left:
704         case GDK_KP_4:
705             if (!MOD__CTRL) { // not ctrl
706                 if (MOD__ALT) { // alt
707                     if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
708                     else drag->selected_move_screen(-1, 0); // no shift
709                 }
710                 else { // no alt
711                     if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
712                     else drag->selected_move(-nudge, 0); // no shift
713                 }
714                 ret = TRUE;
715             }
716             break;
717         case GDK_Up: // move handle up
718         case GDK_KP_Up:
719         case GDK_KP_8:
720             if (!MOD__CTRL) { // not ctrl
721                 if (MOD__ALT) { // alt
722                     if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
723                     else drag->selected_move_screen(0, 1); // no shift
724                 }
725                 else { // no alt
726                     if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
727                     else drag->selected_move(0, nudge); // no shift
728                 }
729                 ret = TRUE;
730             }
731             break;
732         case GDK_Right: // move handle right
733         case GDK_KP_Right:
734         case GDK_KP_6:
735             if (!MOD__CTRL) { // not ctrl
736                 if (MOD__ALT) { // alt
737                     if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
738                     else drag->selected_move_screen(1, 0); // no shift
739                 }
740                 else { // no alt
741                     if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
742                     else drag->selected_move(nudge, 0); // no shift
743                 }
744                 ret = TRUE;
745             }
746             break;
747         case GDK_Down: // move handle down
748         case GDK_KP_Down:
749         case GDK_KP_2:
750             if (!MOD__CTRL) { // not ctrl
751                 if (MOD__ALT) { // alt
752                     if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
753                     else drag->selected_move_screen(0, -1); // no shift
754                 }
755                 else { // no alt
756                     if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
757                     else drag->selected_move(0, -nudge); // no shift
758                 }
759                 ret = TRUE;
760             }
761             break;
762         case GDK_r:
763         case GDK_R:
764             if (MOD__SHIFT_ONLY) {
765                 // First try selected dragger
766                 if (drag && drag->selected) {
767                     drag->selected_reverse_vector();
768                 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
769                     for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
770                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
771                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
772                     }
773                 }
774                 // we did an undoable action
775                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
776                                   _("Invert gradient"));
777                 ret = TRUE;
778             }
779             break;
781         case GDK_Insert:
782         case GDK_KP_Insert:
783             // with any modifiers:
784             sp_gradient_context_add_stops_between_selected_stops (rc);
785             ret = TRUE;
786             break;
788         case GDK_Delete:
789         case GDK_KP_Delete:
790         case GDK_BackSpace:
791             if ( drag->selected ) {
792                 drag->deleteSelected(MOD__CTRL_ONLY);
793                 ret = TRUE;
794             }
795             break;
796         default:
797             break;
798         }
799         break;
800     case GDK_KEY_RELEASE:
801         switch (get_group0_keyval (&event->key)) {
802         case GDK_Alt_L:
803         case GDK_Alt_R:
804         case GDK_Control_L:
805         case GDK_Control_R:
806         case GDK_Shift_L:
807         case GDK_Shift_R:
808         case GDK_Meta_L:  // Meta is when you press Shift+Alt
809         case GDK_Meta_R:
810             event_context->defaultMessageContext()->clear();
811             break;
812         default:
813             break;
814         }
815         break;
816     default:
817         break;
818     }
820     if (!ret) {
821         if (((SPEventContextClass *) parent_class)->root_handler) {
822             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
823         }
824     }
826     return ret;
829 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint /*state*/, guint32 etime)
831     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
832     Inkscape::Selection *selection = sp_desktop_selection(desktop);
833     SPDocument *document = sp_desktop_document(desktop);
834     SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
836     if (!selection->isEmpty()) {
837         int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
838         int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
840         SPGradient *vector;
841         if (ec->item_to_select) {
842             vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
843         } else {
844             vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
845         }
847         // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
848         SPCSSAttr *css = sp_repr_css_attr_new();
849         sp_repr_css_set_property(css, "fill-opacity", "1.0");
851         for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
853             //FIXME: see above
854             sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
856             sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
858             if (type == SP_GRADIENT_TYPE_LINEAR) {
859                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
860                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
861             } else if (type == SP_GRADIENT_TYPE_RADIAL) {
862                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
863                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
864             }
865             SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
866         }
867         if (ec->_grdrag) {
868             ec->_grdrag->updateDraggers();
869             // prevent regenerating draggers by selection modified signal, which sometimes
870             // comes too late and thus destroys the knot which we will now grab:
871             ec->_grdrag->local_change = true;
872             // give the grab out-of-bounds values of xp/yp because we're already dragging
873             // and therefore are already out of tolerance
874             ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
875                                    type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
876                                    -1, // ignore number (though it is always 1)
877                                    fill_or_stroke, 99999, 99999, etime);
878         }
879         // We did an undoable action, but sp_document_done will be called by the knot when released
881         // status text; we do not track coords because this branch is run once, not all the time
882         // during drag
883         int n_objects = g_slist_length((GSList *) selection->itemList());
884         rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
885                                   ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
886                                            "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
887                                   n_objects);
888     } else {
889         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
890     }
894 /*
895   Local Variables:
896   mode:c++
897   c-file-style:"stroustrup"
898   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
899   indent-tabs-mode:nil
900   fill-column:99
901   End:
902 */
903 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :