Code

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