1 /*
2 * Gradient drawing and editing tool
3 *
4 * Authors:
5 * bulia byak <buliabyak@users.sf.net>
6 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
7 * Abhishek Sharma
8 *
9 * Copyright (C) 2007 Johan Engelen
10 * Copyright (C) 2005 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
20 #include <gdk/gdkkeysyms.h>
22 #include "macros.h"
23 #include "document.h"
24 #include "selection.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "message-context.h"
28 #include "message-stack.h"
29 #include "pixmaps/cursor-gradient.xpm"
30 #include "pixmaps/cursor-gradient-add.xpm"
31 #include "gradient-context.h"
32 #include "gradient-chemistry.h"
33 #include <glibmm/i18n.h>
34 #include "preferences.h"
35 #include "gradient-drag.h"
36 #include "gradient-chemistry.h"
37 #include "xml/repr.h"
38 #include "sp-item.h"
39 #include "display/sp-ctrlline.h"
40 #include "sp-linear-gradient.h"
41 #include "sp-radial-gradient.h"
42 #include "sp-stop.h"
43 #include "svg/css-ostringstream.h"
44 #include "svg/svg-color.h"
45 #include "snap.h"
46 #include "sp-namedview.h"
47 #include "rubberband.h"
49 using Inkscape::DocumentUndo;
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;
110 }
112 static void sp_gradient_context_dispose(GObject *object)
113 {
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);
129 }
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)
145 {
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 }
192 }
194 static void
195 gradient_subselection_changed (gpointer, gpointer data)
196 {
197 gradient_selection_changed (NULL, data);
198 }
201 static void sp_gradient_context_setup(SPEventContext *ec)
202 {
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);
222 }
224 void
225 sp_gradient_context_select_next (SPEventContext *event_context)
226 {
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);
233 }
235 void
236 sp_gradient_context_select_prev (SPEventContext *event_context)
237 {
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);
244 }
246 static bool
247 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, Geom::Point event_p)
248 {
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;
264 }
266 std::vector<Geom::Point>
267 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
268 {
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;
344 }
346 static void
347 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
348 {
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 DocumentUndo::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);
399 }
401 double sqr(double x) {return x*x;}
403 static void
404 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
405 {
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 DocumentUndo::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);
466 }
469 static void
470 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
471 {
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 DocumentUndo::done(sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
482 _("Add gradient stop"));
484 ec->get_drag()->updateDraggers();
485 }
488 static gint
489 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
490 {
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 DocumentUndo::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 m.unSetup();
563 rc->origin = from_2geom(button_dt);
564 }
566 ret = TRUE;
567 }
568 break;
569 case GDK_MOTION_NOTIFY:
570 if ( dragging
571 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
572 {
573 if ( event_context->within_tolerance
574 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
575 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
576 break; // do not drag if we're within tolerance from origin
577 }
578 // Once the user has moved farther than tolerance from the original location
579 // (indicating they intend to draw, not click), then always process the
580 // motion notify coordinates as given (no snapping back to origin)
581 event_context->within_tolerance = false;
583 Geom::Point const motion_w(event->motion.x,
584 event->motion.y);
585 Geom::Point const motion_dt = event_context->desktop->w2d(motion_w);
587 if (Inkscape::Rubberband::get(desktop)->is_started()) {
588 Inkscape::Rubberband::get(desktop)->move(motion_dt);
589 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
590 } else {
591 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
592 }
593 gobble_motion_events(GDK_BUTTON1_MASK);
595 ret = TRUE;
596 } else {
597 if (!drag->mouseOver()) {
598 SnapManager &m = desktop->namedview->snap_manager;
599 m.setup(desktop);
601 Geom::Point const motion_w(event->motion.x, event->motion.y);
602 Geom::Point const motion_dt = event_context->desktop->w2d(motion_w);
604 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
605 m.unSetup();
606 }
608 bool over_line = false;
609 if (drag->lines) {
610 for (GSList *l = drag->lines; l != NULL; l = l->next) {
611 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, Geom::Point(event->motion.x, event->motion.y));
612 }
613 }
615 if (rc->cursor_addnode && !over_line) {
616 event_context->cursor_shape = cursor_gradient_xpm;
617 sp_event_context_update_cursor(event_context);
618 rc->cursor_addnode = false;
619 } else if (!rc->cursor_addnode && over_line) {
620 event_context->cursor_shape = cursor_gradient_add_xpm;
621 sp_event_context_update_cursor(event_context);
622 rc->cursor_addnode = true;
623 }
624 }
625 break;
626 case GDK_BUTTON_RELEASE:
627 event_context->xp = event_context->yp = 0;
628 if ( event->button.button == 1 && !event_context->space_panning ) {
629 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
630 bool over_line = false;
631 SPCtrlLine *line = NULL;
632 if (drag->lines) {
633 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
634 line = (SPCtrlLine*) l->data;
635 over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
636 if (over_line)
637 break;
638 }
639 }
640 if (over_line && line) {
641 sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
642 ret = TRUE;
643 }
644 } else {
645 dragging = false;
647 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
648 if (event->button.state & GDK_CONTROL_MASK) {
649 ret = TRUE;
650 break;
651 }
653 if (!event_context->within_tolerance) {
654 // we've been dragging, either do nothing (grdrag handles that),
655 // or rubberband-select if we have rubberband
656 Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
657 if (r->is_started() && !event_context->within_tolerance) {
658 // this was a rubberband drag
659 if (r->getMode() == RUBBERBAND_MODE_RECT) {
660 Geom::OptRect const b = r->getRectangle();
661 drag->selectRect(*b);
662 }
663 }
665 } else if (event_context->item_to_select) {
666 // no dragging, select clicked item if any
667 if (event->button.state & GDK_SHIFT_MASK) {
668 selection->toggle(event_context->item_to_select);
669 } else {
670 selection->set(event_context->item_to_select);
671 }
672 } else {
673 // click in an empty space; do the same as Esc
674 if (drag->selected) {
675 drag->deselectAll();
676 } else {
677 selection->clear();
678 }
679 }
681 event_context->item_to_select = NULL;
682 ret = TRUE;
683 }
684 Inkscape::Rubberband::get(desktop)->stop();
685 }
686 break;
687 case GDK_KEY_PRESS:
688 switch (get_group0_keyval (&event->key)) {
689 case GDK_Alt_L:
690 case GDK_Alt_R:
691 case GDK_Control_L:
692 case GDK_Control_R:
693 case GDK_Shift_L:
694 case GDK_Shift_R:
695 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
696 case GDK_Meta_R:
697 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
698 _("<b>Ctrl</b>: snap gradient angle"),
699 _("<b>Shift</b>: draw gradient around the starting point"),
700 NULL);
701 break;
703 case GDK_x:
704 case GDK_X:
705 if (MOD__ALT_ONLY) {
706 desktop->setToolboxFocusTo ("altx-grad");
707 ret = TRUE;
708 }
709 break;
711 case GDK_A:
712 case GDK_a:
713 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
714 drag->selectAll();
715 ret = TRUE;
716 }
717 break;
719 case GDK_L:
720 case GDK_l:
721 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
722 sp_gradient_simplify(rc, 1e-4);
723 ret = TRUE;
724 }
725 break;
727 case GDK_Escape:
728 if (drag->selected) {
729 drag->deselectAll();
730 } else {
731 selection->clear();
732 }
733 ret = TRUE;
734 //TODO: make dragging escapable by Esc
735 break;
737 case GDK_Left: // move handle left
738 case GDK_KP_Left:
739 case GDK_KP_4:
740 if (!MOD__CTRL) { // not ctrl
741 gint mul = 1 + gobble_key_events(
742 get_group0_keyval(&event->key), 0); // with any mask
743 if (MOD__ALT) { // alt
744 if (MOD__SHIFT) drag->selected_move_screen(mul*-10, 0); // shift
745 else drag->selected_move_screen(mul*-1, 0); // no shift
746 }
747 else { // no alt
748 if (MOD__SHIFT) drag->selected_move(mul*-10*nudge, 0); // shift
749 else drag->selected_move(mul*-nudge, 0); // no shift
750 }
751 ret = TRUE;
752 }
753 break;
754 case GDK_Up: // move handle up
755 case GDK_KP_Up:
756 case GDK_KP_8:
757 if (!MOD__CTRL) { // not ctrl
758 gint mul = 1 + gobble_key_events(
759 get_group0_keyval(&event->key), 0); // with any mask
760 if (MOD__ALT) { // alt
761 if (MOD__SHIFT) drag->selected_move_screen(0, mul*10); // shift
762 else drag->selected_move_screen(0, mul*1); // no shift
763 }
764 else { // no alt
765 if (MOD__SHIFT) drag->selected_move(0, mul*10*nudge); // shift
766 else drag->selected_move(0, mul*nudge); // no shift
767 }
768 ret = TRUE;
769 }
770 break;
771 case GDK_Right: // move handle right
772 case GDK_KP_Right:
773 case GDK_KP_6:
774 if (!MOD__CTRL) { // not ctrl
775 gint mul = 1 + gobble_key_events(
776 get_group0_keyval(&event->key), 0); // with any mask
777 if (MOD__ALT) { // alt
778 if (MOD__SHIFT) drag->selected_move_screen(mul*10, 0); // shift
779 else drag->selected_move_screen(mul*1, 0); // no shift
780 }
781 else { // no alt
782 if (MOD__SHIFT) drag->selected_move(mul*10*nudge, 0); // shift
783 else drag->selected_move(mul*nudge, 0); // no shift
784 }
785 ret = TRUE;
786 }
787 break;
788 case GDK_Down: // move handle down
789 case GDK_KP_Down:
790 case GDK_KP_2:
791 if (!MOD__CTRL) { // not ctrl
792 gint mul = 1 + gobble_key_events(
793 get_group0_keyval(&event->key), 0); // with any mask
794 if (MOD__ALT) { // alt
795 if (MOD__SHIFT) drag->selected_move_screen(0, mul*-10); // shift
796 else drag->selected_move_screen(0, mul*-1); // no shift
797 }
798 else { // no alt
799 if (MOD__SHIFT) drag->selected_move(0, mul*-10*nudge); // shift
800 else drag->selected_move(0, mul*-nudge); // no shift
801 }
802 ret = TRUE;
803 }
804 break;
805 case GDK_r:
806 case GDK_R:
807 if (MOD__SHIFT_ONLY) {
808 // First try selected dragger
809 if (drag && drag->selected) {
810 drag->selected_reverse_vector();
811 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
812 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
813 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
814 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
815 }
816 }
817 // we did an undoable action
818 DocumentUndo::done(sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
819 _("Invert gradient"));
820 ret = TRUE;
821 }
822 break;
824 case GDK_Insert:
825 case GDK_KP_Insert:
826 // with any modifiers:
827 sp_gradient_context_add_stops_between_selected_stops (rc);
828 ret = TRUE;
829 break;
831 case GDK_Delete:
832 case GDK_KP_Delete:
833 case GDK_BackSpace:
834 if ( drag->selected ) {
835 drag->deleteSelected(MOD__CTRL_ONLY);
836 ret = TRUE;
837 }
838 break;
839 default:
840 break;
841 }
842 break;
843 case GDK_KEY_RELEASE:
844 switch (get_group0_keyval (&event->key)) {
845 case GDK_Alt_L:
846 case GDK_Alt_R:
847 case GDK_Control_L:
848 case GDK_Control_R:
849 case GDK_Shift_L:
850 case GDK_Shift_R:
851 case GDK_Meta_L: // Meta is when you press Shift+Alt
852 case GDK_Meta_R:
853 event_context->defaultMessageContext()->clear();
854 break;
855 default:
856 break;
857 }
858 break;
859 default:
860 break;
861 }
863 if (!ret) {
864 if (((SPEventContextClass *) parent_class)->root_handler) {
865 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
866 }
867 }
869 return ret;
870 }
872 static void sp_gradient_drag(SPGradientContext &rc, Geom::Point const pt, guint /*state*/, guint32 etime)
873 {
874 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
875 Inkscape::Selection *selection = sp_desktop_selection(desktop);
876 SPDocument *document = sp_desktop_document(desktop);
877 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
879 if (!selection->isEmpty()) {
880 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
881 int type = prefs->getInt("/tools/gradient/newgradient", 1);
882 int fill_or_stroke = prefs->getInt("/tools/gradient/newfillorstroke", 1);
884 SPGradient *vector;
885 if (ec->item_to_select) {
886 // pick color from the object where drag started
887 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
888 } else {
889 // Starting from empty space:
890 // Sort items so that the topmost comes last
891 GSList *items = g_slist_copy ((GSList *) selection->itemList());
892 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
893 // take topmost
894 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
895 g_slist_free (items);
896 }
898 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
899 SPCSSAttr *css = sp_repr_css_attr_new();
900 sp_repr_css_set_property(css, "fill-opacity", "1.0");
902 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
904 //FIXME: see above
905 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
907 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
909 if (type == SP_GRADIENT_TYPE_LINEAR) {
910 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
911 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
912 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
913 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
914 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
915 }
916 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
917 }
918 if (ec->_grdrag) {
919 ec->_grdrag->updateDraggers();
920 // prevent regenerating draggers by selection modified signal, which sometimes
921 // comes too late and thus destroys the knot which we will now grab:
922 ec->_grdrag->local_change = true;
923 // give the grab out-of-bounds values of xp/yp because we're already dragging
924 // and therefore are already out of tolerance
925 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
926 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
927 -1, // ignore number (though it is always 1)
928 fill_or_stroke, 99999, 99999, etime);
929 }
930 // We did an undoable action, but SPDocumentUndo::done will be called by the knot when released
932 // status text; we do not track coords because this branch is run once, not all the time
933 // during drag
934 int n_objects = g_slist_length((GSList *) selection->itemList());
935 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
936 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
937 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
938 n_objects);
939 } else {
940 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
941 }
942 }
945 /*
946 Local Variables:
947 mode:c++
948 c-file-style:"stroustrup"
949 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
950 indent-tabs-mode:nil
951 fill-column:99
952 End:
953 */
954 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :