Code

NR::Maybe => boost::optional
[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     //The use of ngettext in the following code is intentional even if the English singular form would never be used
158     if (n_sel == 1) {
159         if (drag->singleSelectedDraggerNumDraggables() == 1) {
160                 gchar * message = g_strconcat(
161                         //TRANSLATORS: %s will be substituted with the point name (see previous messages); This is part of a compound message
162                         _("%s selected"),
163                         //TRANSLATORS: Mind the space in front. This is part of a compound message
164                         ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
165                         ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
166                 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
167                         message,_(gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()]), n_tot, n_obj);
168         } else {
169                 gchar * message = g_strconcat(
170                         //TRANSLATORS: This is a part of a compound message (out of two more indicating: grandint handle count & object count)
171                         ngettext("One handle merging %d stop (drag with <b>Shift</b> to separate) selected",
172                                 "One handle merging %d stops (drag with <b>Shift</b> to separate) selected",drag->singleSelectedDraggerNumDraggables()),
173                         ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
174                         ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
175                 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message,drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
176         }
177     } else if (n_sel > 1) {
178         //TRANSLATORS: The plural refers to number of selected gradient handles. This is part of a compound message (part two indicates selected object count)
179         gchar * message = g_strconcat(ngettext("<b>%d</b> gradient handle selected out of %d","<b>%d</b> gradient handles selected out of %d",n_sel),
180                                       //TRANSLATORS: Mind the space in front. (Refers to gradient handles selected). This is part of a compound message
181                                       ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
182         rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message, n_sel, n_tot, n_obj);
183     } else if (n_sel == 0) {
184         rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
185                 //TRANSLATORS: The plural refers to number of selected objects
186                 ngettext("<b>No</b> gradient handles selected out of %d on %d selected object",
187                          "<b>No</b> gradient handles selected out of %d on %d selected objects",n_obj), n_tot, n_obj);
188     }
191 static void
192 gradient_subselection_changed (gpointer, gpointer data)
194     gradient_selection_changed (NULL, data);
198 static void sp_gradient_context_setup(SPEventContext *ec)
200     SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
202     if (((SPEventContextClass *) parent_class)->setup) {
203         ((SPEventContextClass *) parent_class)->setup(ec);
204     }
206     if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
207         ec->enableSelectionCue();
208     }
210     ec->enableGrDrag();
211     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
213     rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
215     rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
216     rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
217     gradient_selection_changed(selection, rc);
220 void
221 sp_gradient_context_select_next (SPEventContext *event_context)
223     GrDrag *drag = event_context->_grdrag;
224     g_assert (drag);
226     GrDragger *d = drag->select_next();
228     event_context->desktop->scroll_to_point(&(d->point), 1.0);
231 void
232 sp_gradient_context_select_prev (SPEventContext *event_context)
234     GrDrag *drag = event_context->_grdrag;
235     g_assert (drag);
237     GrDragger *d = drag->select_prev();
239     event_context->desktop->scroll_to_point(&(d->point), 1.0);
242 static bool
243 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
245     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
247     //Translate mouse point into proper coord system
248     rc->mousepoint_doc = desktop->w2d(event_p);
250     SPCtrlLine* line = SP_CTRLLINE(item);
252     NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e, 0);
253     double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
255     double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
257     bool close = (dist_screen < tolerance);
259     return close;
262 std::vector<NR::Point>
263 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
265     std::vector<NR::Point> coords;
267     // for all selected draggers
268     for (GList *i = drag->selected; i != NULL; i = i->next) {
269         GrDragger *dragger = (GrDragger *) i->data;
270         // remember the coord of the dragger to reselect it later
271         coords.push_back(dragger->point);
272         // for all draggables of dragger
273         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) { 
274             GrDraggable *d = (GrDraggable *) j->data;
276             // find the gradient
277             SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
278             SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
280             // these draggable types cannot have a next draggabe to insert a stop between them
281             if (d->point_type == POINT_LG_END || 
282                 d->point_type == POINT_RG_FOCUS || 
283                 d->point_type == POINT_RG_R1 || 
284                 d->point_type == POINT_RG_R2) {
285                 continue;
286             }
288             // from draggables to stops
289             SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
290             SPStop *next_stop = sp_next_stop (this_stop);
291             SPStop *last_stop = sp_last_stop (vector);
293             gint fs = d->fill_or_stroke;
294             SPItem *item = d->item;
295             gint type = d->point_type;
296             gint p_i = d->point_i;
298             // if there's a next stop,
299             if (next_stop) {
300                 GrDragger *dnext = NULL;
301                 // find its dragger 
302                 // (complex because it may have different types, and because in radial,
303                 // more than one dragger may correspond to a stop, so we must distinguish)
304                 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
305                     if (next_stop == last_stop)
306                         dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
307                     else
308                         dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
309                 } else { // radial
310                     if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
311                         if (next_stop == last_stop)
312                             dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
313                         else 
314                             dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
315                     } 
316                     if ((type == POINT_RG_MID2) || 
317                         (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
318                         if (next_stop == last_stop)
319                             dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
320                         else 
321                             dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
322                     }
323                 }
325                 // if both adjacent draggers selected,
326                 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
328                     // remember the coords of the future dragger to select it
329                     coords.push_back(0.5*(dragger->point + dnext->point));
331                     // do not insert a stop now, it will confuse the loop;
332                     // just remember the stops
333                     *these_stops = g_slist_prepend (*these_stops, this_stop);
334                     *next_stops = g_slist_prepend (*next_stops, next_stop);
335                 }
336             }
337         }
338     }
339     return coords;
342 static void
343 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
345     SPDocument *doc = NULL;
346     GrDrag *drag = rc->_grdrag;
348     GSList *these_stops = NULL;
349     GSList *next_stops = NULL;
351     std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
353     if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
354         // if a single stop is selected, add between that stop and the next one
355         GrDragger *dragger = (GrDragger *) drag->selected->data;
356         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) { 
357             GrDraggable *d = (GrDraggable *) j->data;
358             SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
359             SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
360             SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
361             SPStop *next_stop = sp_next_stop (this_stop);
362             if (this_stop && next_stop) {
363                 these_stops = g_slist_prepend (these_stops, this_stop);
364                 next_stops = g_slist_prepend (next_stops, next_stop);
365             }
366         }
367     }
369     // now actually create the new stops
370     GSList *i = these_stops;
371     GSList *j = next_stops;
372     for (; i != NULL && j != NULL; i = i->next, j = j->next) {
373         SPStop *this_stop = (SPStop *) i->data;
374         SPStop *next_stop = (SPStop *) j->data;
375         gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
376         SPObject *parent = SP_OBJECT_PARENT(this_stop);
377         if (SP_IS_GRADIENT (parent)) {
378             doc = SP_OBJECT_DOCUMENT (parent);
379             sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
380             sp_gradient_ensure_vector (SP_GRADIENT (parent));
381         }
382     }
384     if (g_slist_length(these_stops) > 0 && doc) {
385         sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
386         drag->updateDraggers();
387         // so that it does not automatically update draggers in idle loop, as this would deselect
388         drag->local_change = true;
389         // select all the old selected and new created draggers
390         drag->selectByCoords(coords);
391     }
393     g_slist_free (these_stops);
394     g_slist_free (next_stops);
397 double sqr(double x) {return x*x;}
399 static void
400 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
402     SPDocument *doc = NULL;
403     GrDrag *drag = rc->_grdrag;
405     GSList *these_stops = NULL;
406     GSList *next_stops = NULL;
408     std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
410     GSList *todel = NULL;
412     GSList *i = these_stops;
413     GSList *j = next_stops;
414     for (; i != NULL && j != NULL; i = i->next, j = j->next) {
415         SPStop *stop0 = (SPStop *) i->data;
416         SPStop *stop1 = (SPStop *) j->data;
418         gint i1 = g_slist_index(these_stops, stop1);
419         if (i1 != -1) {
420             GSList *next_next = g_slist_nth (next_stops, i1);
421             if (next_next) {
422                 SPStop *stop2 = (SPStop *) next_next->data;
424                 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
425                     continue;
427                 guint32 const c0 = sp_stop_get_rgba32(stop0);
428                 guint32 const c2 = sp_stop_get_rgba32(stop2);
429                 guint32 const c1r = sp_stop_get_rgba32(stop1);
430                 guint32 c1 = average_color (c0, c2, 
431                        (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
433                 double diff = 
434                     sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
435                     sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
436                     sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
437                     sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
439                 if (diff < tolerance)
440                     todel = g_slist_prepend (todel, stop1);
441             }
442         }
443     }
445     for (i = todel; i != NULL; i = i->next) {
446         SPStop *stop = (SPStop*) i->data;
447         doc = SP_OBJECT_DOCUMENT (stop);
448         Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
449         parent->removeChild(SP_OBJECT_REPR(stop));
450     }
452     if (g_slist_length(todel) > 0) {
453         sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
454         drag->local_change = true;
455         drag->updateDraggers();
456         drag->selectByCoords(coords);
457     }
459     g_slist_free (todel);
460     g_slist_free (these_stops);
461     g_slist_free (next_stops);
465 static void
466 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item,  NR::Point mouse_p, guint32 /*etime*/)
468     // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
470     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
471     SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
473     double tolerance = (double) ec->tolerance;
475     ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
477     sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
478                       _("Add gradient stop"));
480     ec->get_drag()->updateDraggers();
484 static gint
485 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
487     static bool dragging;
489     SPDesktop *desktop = event_context->desktop;
490     Inkscape::Selection *selection = sp_desktop_selection (desktop);
492     SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
494     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
495     double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
497     GrDrag *drag = event_context->_grdrag;
498     g_assert (drag);
500     gint ret = FALSE;
501     switch (event->type) {
502     case GDK_2BUTTON_PRESS:
503         if ( event->button.button == 1 ) {
504             bool over_line = false;
505             SPCtrlLine *line = NULL;
506             if (drag->lines) {
507                 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
508                     line = (SPCtrlLine*) l->data;
509                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
510                 }
511             }
512             if (over_line) {
513                 // we take the first item in selection, because with doubleclick, the first click
514                 // always resets selection to the single object under cursor
515                 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
516             } else {
517                 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
518                     SPItem *item = SP_ITEM(i->data);
519                     SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
520                     guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
522                     SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop,                                                                                   SP_OBJECT (item), new_fill);
524                     SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
525                     sp_gradient_reset_to_userspace(priv, item);
526                 }
528                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
529                                   _("Create default gradient"));
530             }
531             ret = TRUE;
532         }
533         break;
534     case GDK_BUTTON_PRESS:
535         if ( event->button.button == 1 && !event_context->space_panning ) {
536             NR::Point button_w(event->button.x, event->button.y);
538             // save drag origin
539             event_context->xp = (gint) button_w[NR::X];
540             event_context->yp = (gint) button_w[NR::Y];
541             event_context->within_tolerance = true;
543             dragging = true;
545             NR::Point button_dt = desktop->w2d(button_w);
546             if (event->button.state & GDK_SHIFT_MASK) {
547                 Inkscape::Rubberband::get()->start(desktop, button_dt);
548             } else {
549                 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
550                 // enable Ctrl+doubleclick of exactly the selected item(s)
551                 if (!(event->button.state & GDK_CONTROL_MASK))
552                     event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
554                 /* Snap center to nearest magnetic point */
555                 SnapManager &m = desktop->namedview->snap_manager;
556                 m.setup(desktop);
557                 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, button_dt);
558                 rc->origin = button_dt;
559             }
561             ret = TRUE;
562         }
563         break;
564     case GDK_MOTION_NOTIFY:
565         if ( dragging
566              && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
567         {
568             if ( event_context->within_tolerance
569                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
570                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
571                 break; // do not drag if we're within tolerance from origin
572             }
573             // Once the user has moved farther than tolerance from the original location
574             // (indicating they intend to draw, not click), then always process the
575             // motion notify coordinates as given (no snapping back to origin)
576             event_context->within_tolerance = false;
578             NR::Point const motion_w(event->motion.x,
579                                      event->motion.y);
580             NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
582             if (Inkscape::Rubberband::get()->is_started()) {
583                 Inkscape::Rubberband::get()->move(motion_dt);
584                 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
585             } else {
586                 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
587             }
588             gobble_motion_events(GDK_BUTTON1_MASK);
590             ret = TRUE;
591         } else {
592             bool over_line = false;
593             if (drag->lines) {
594                 for (GSList *l = drag->lines; l != NULL; l = l->next) {
595                     over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
596                 }
597             }
599             if (rc->cursor_addnode && !over_line) {
600                 event_context->cursor_shape = cursor_gradient_xpm;
601                 sp_event_context_update_cursor(event_context);
602                 rc->cursor_addnode = false;
603             } else if (!rc->cursor_addnode && over_line) {
604                 event_context->cursor_shape = cursor_gradient_add_xpm;
605                 sp_event_context_update_cursor(event_context);
606                 rc->cursor_addnode = true;
607             }
608         }
609         break;
610     case GDK_BUTTON_RELEASE:
611         event_context->xp = event_context->yp = 0;
612         if ( event->button.button == 1 && !event_context->space_panning ) {
613             if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
614                 bool over_line = false;
615                 SPCtrlLine *line = NULL;
616                 if (drag->lines) {
617                     for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
618                         line = (SPCtrlLine*) l->data;
619                         over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
620                         if (over_line)
621                             break;
622                     }
623                 }
624                 if (over_line && line) {
625                     sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
626                     ret = TRUE;
627                 }
628             } else {
629                 dragging = false;
631                 // unless clicked with Ctrl (to enable Ctrl+doubleclick).  
632                 if (event->button.state & GDK_CONTROL_MASK) {
633                     ret = TRUE;
634                     break;
635                 }
637                 if (!event_context->within_tolerance) {
638                     // we've been dragging, either do nothing (grdrag handles that),
639                     // or rubberband-select if we have rubberband
640                     Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
641                     if (r->is_started() && !event_context->within_tolerance) {
642                         // this was a rubberband drag
643                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
644                             boost::optional<NR::Rect> const b = r->getRectangle();
645                             drag->selectRect(*b);
646                         }
647                     }
649                 } else if (event_context->item_to_select) {
650                     // no dragging, select clicked item if any
651                     if (event->button.state & GDK_SHIFT_MASK) {
652                         selection->toggle(event_context->item_to_select);
653                     } else {
654                         selection->set(event_context->item_to_select);
655                     }
656                 } else {
657                     // click in an empty space; do the same as Esc
658                     if (drag->selected) {
659                         drag->deselectAll();
660                     } else {
661                         selection->clear();
662                     }
663                 }
665                 event_context->item_to_select = NULL;
666                 ret = TRUE;
667             }
668             Inkscape::Rubberband::get()->stop(); 
669         }
670         break;
671     case GDK_KEY_PRESS:
672         switch (get_group0_keyval (&event->key)) {
673         case GDK_Alt_L:
674         case GDK_Alt_R:
675         case GDK_Control_L:
676         case GDK_Control_R:
677         case GDK_Shift_L:
678         case GDK_Shift_R:
679         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
680         case GDK_Meta_R:
681             sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
682                                         _("<b>Ctrl</b>: snap gradient angle"),
683                                         _("<b>Shift</b>: draw gradient around the starting point"),
684                                         NULL);
685             break;
687         case GDK_x:
688         case GDK_X:
689             if (MOD__ALT_ONLY) {
690                 desktop->setToolboxFocusTo ("altx-grad");
691                 ret = TRUE;
692             }
693             break;
695         case GDK_A:
696         case GDK_a:
697             if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
698                 drag->selectAll();
699                 ret = TRUE;
700             }
701             break;
703         case GDK_L:
704         case GDK_l:
705             if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
706                 sp_gradient_simplify(rc, 1e-4);
707                 ret = TRUE;
708             }
709             break;
711         case GDK_Escape:
712             if (drag->selected) {
713                 drag->deselectAll();
714             } else {
715                 selection->clear();
716             }
717             ret = TRUE;
718             //TODO: make dragging escapable by Esc
719             break;
721         case GDK_Left: // move handle left
722         case GDK_KP_Left:
723         case GDK_KP_4:
724             if (!MOD__CTRL) { // not ctrl
725                 gint mul = 1 + gobble_key_events(
726                     get_group0_keyval(&event->key), 0); // with any mask
727                 if (MOD__ALT) { // alt
728                     if (MOD__SHIFT) drag->selected_move_screen(mul*-10, 0); // shift
729                     else drag->selected_move_screen(mul*-1, 0); // no shift
730                 }
731                 else { // no alt
732                     if (MOD__SHIFT) drag->selected_move(mul*-10*nudge, 0); // shift
733                     else drag->selected_move(mul*-nudge, 0); // no shift
734                 }
735                 ret = TRUE;
736             }
737             break;
738         case GDK_Up: // move handle up
739         case GDK_KP_Up:
740         case GDK_KP_8:
741             if (!MOD__CTRL) { // not ctrl
742                 gint mul = 1 + gobble_key_events(
743                     get_group0_keyval(&event->key), 0); // with any mask
744                 if (MOD__ALT) { // alt
745                     if (MOD__SHIFT) drag->selected_move_screen(0, mul*10); // shift
746                     else drag->selected_move_screen(0, mul*1); // no shift
747                 }
748                 else { // no alt
749                     if (MOD__SHIFT) drag->selected_move(0, mul*10*nudge); // shift
750                     else drag->selected_move(0, mul*nudge); // no shift
751                 }
752                 ret = TRUE;
753             }
754             break;
755         case GDK_Right: // move handle right
756         case GDK_KP_Right:
757         case GDK_KP_6:
758             if (!MOD__CTRL) { // not ctrl
759                 gint mul = 1 + gobble_key_events(
760                     get_group0_keyval(&event->key), 0); // with any mask
761                 if (MOD__ALT) { // alt
762                     if (MOD__SHIFT) drag->selected_move_screen(mul*10, 0); // shift
763                     else drag->selected_move_screen(mul*1, 0); // no shift
764                 }
765                 else { // no alt
766                     if (MOD__SHIFT) drag->selected_move(mul*10*nudge, 0); // shift
767                     else drag->selected_move(mul*nudge, 0); // no shift
768                 }
769                 ret = TRUE;
770             }
771             break;
772         case GDK_Down: // move handle down
773         case GDK_KP_Down:
774         case GDK_KP_2:
775             if (!MOD__CTRL) { // not ctrl
776                 gint mul = 1 + gobble_key_events(
777                     get_group0_keyval(&event->key), 0); // with any mask
778                 if (MOD__ALT) { // alt
779                     if (MOD__SHIFT) drag->selected_move_screen(0, mul*-10); // shift
780                     else drag->selected_move_screen(0, mul*-1); // no shift
781                 }
782                 else { // no alt
783                     if (MOD__SHIFT) drag->selected_move(0, mul*-10*nudge); // shift
784                     else drag->selected_move(0, mul*-nudge); // no shift
785                 }
786                 ret = TRUE;
787             }
788             break;
789         case GDK_r:
790         case GDK_R:
791             if (MOD__SHIFT_ONLY) {
792                 // First try selected dragger
793                 if (drag && drag->selected) {
794                     drag->selected_reverse_vector();
795                 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
796                     for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
797                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
798                         sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
799                     }
800                 }
801                 // we did an undoable action
802                 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
803                                   _("Invert gradient"));
804                 ret = TRUE;
805             }
806             break;
808         case GDK_Insert:
809         case GDK_KP_Insert:
810             // with any modifiers:
811             sp_gradient_context_add_stops_between_selected_stops (rc);
812             ret = TRUE;
813             break;
815         case GDK_Delete:
816         case GDK_KP_Delete:
817         case GDK_BackSpace:
818             if ( drag->selected ) {
819                 drag->deleteSelected(MOD__CTRL_ONLY);
820                 ret = TRUE;
821             }
822             break;
823         default:
824             break;
825         }
826         break;
827     case GDK_KEY_RELEASE:
828         switch (get_group0_keyval (&event->key)) {
829         case GDK_Alt_L:
830         case GDK_Alt_R:
831         case GDK_Control_L:
832         case GDK_Control_R:
833         case GDK_Shift_L:
834         case GDK_Shift_R:
835         case GDK_Meta_L:  // Meta is when you press Shift+Alt
836         case GDK_Meta_R:
837             event_context->defaultMessageContext()->clear();
838             break;
839         default:
840             break;
841         }
842         break;
843     default:
844         break;
845     }
847     if (!ret) {
848         if (((SPEventContextClass *) parent_class)->root_handler) {
849             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
850         }
851     }
853     return ret;
856 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint /*state*/, guint32 etime)
858     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
859     Inkscape::Selection *selection = sp_desktop_selection(desktop);
860     SPDocument *document = sp_desktop_document(desktop);
861     SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
863     if (!selection->isEmpty()) {
864         int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
865         int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
867         SPGradient *vector;
868         if (ec->item_to_select) {
869             // pick color from the object where drag started
870             vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
871         } else {
872             // Starting from empty space:
873             // Sort items so that the topmost comes last
874             GSList *items = g_slist_copy ((GSList *) selection->itemList());
875             items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
876             // take topmost
877             vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
878             g_slist_free (items);
879         }
881         // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
882         SPCSSAttr *css = sp_repr_css_attr_new();
883         sp_repr_css_set_property(css, "fill-opacity", "1.0");
885         for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
887             //FIXME: see above
888             sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
890             sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
892             if (type == SP_GRADIENT_TYPE_LINEAR) {
893                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
894                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
895             } else if (type == SP_GRADIENT_TYPE_RADIAL) {
896                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
897                 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
898             }
899             SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
900         }
901         if (ec->_grdrag) {
902             ec->_grdrag->updateDraggers();
903             // prevent regenerating draggers by selection modified signal, which sometimes
904             // comes too late and thus destroys the knot which we will now grab:
905             ec->_grdrag->local_change = true;
906             // give the grab out-of-bounds values of xp/yp because we're already dragging
907             // and therefore are already out of tolerance
908             ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
909                                    type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
910                                    -1, // ignore number (though it is always 1)
911                                    fill_or_stroke, 99999, 99999, etime);
912         }
913         // We did an undoable action, but sp_document_done will be called by the knot when released
915         // status text; we do not track coords because this branch is run once, not all the time
916         // during drag
917         int n_objects = g_slist_length((GSList *) selection->itemList());
918         rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
919                                   ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
920                                            "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
921                                   n_objects);
922     } else {
923         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
924     }
928 /*
929   Local Variables:
930   mode:c++
931   c-file-style:"stroustrup"
932   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
933   indent-tabs-mode:nil
934   fill-column:99
935   End:
936 */
937 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :