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 *
8 * Copyright (C) 2007 Johan Engelen
9 * Copyright (C) 2005 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
19 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "document.h"
23 #include "selection.h"
24 #include "desktop.h"
25 #include "desktop-handles.h"
26 #include "message-context.h"
27 #include "message-stack.h"
28 #include "pixmaps/cursor-gradient.xpm"
29 #include "pixmaps/cursor-gradient-add.xpm"
30 #include "gradient-context.h"
31 #include "gradient-chemistry.h"
32 #include <glibmm/i18n.h>
33 #include "preferences.h"
34 #include "gradient-drag.h"
35 #include "gradient-chemistry.h"
36 #include "xml/repr.h"
37 #include "sp-item.h"
38 #include "display/sp-ctrlline.h"
39 #include "sp-linear-gradient.h"
40 #include "sp-radial-gradient.h"
41 #include "sp-stop.h"
42 #include "svg/css-ostringstream.h"
43 #include "svg/svg-color.h"
44 #include "snap.h"
45 #include "sp-namedview.h"
46 #include "rubberband.h"
50 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
51 static void sp_gradient_context_init(SPGradientContext *gr_context);
52 static void sp_gradient_context_dispose(GObject *object);
54 static void sp_gradient_context_setup(SPEventContext *ec);
56 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
58 static void sp_gradient_drag(SPGradientContext &rc, Geom::Point const pt, guint state, guint32 etime);
60 static SPEventContextClass *parent_class;
63 GtkType sp_gradient_context_get_type()
64 {
65 static GType type = 0;
66 if (!type) {
67 GTypeInfo info = {
68 sizeof(SPGradientContextClass),
69 NULL, NULL,
70 (GClassInitFunc) sp_gradient_context_class_init,
71 NULL, NULL,
72 sizeof(SPGradientContext),
73 4,
74 (GInstanceInitFunc) sp_gradient_context_init,
75 NULL, /* value_table */
76 };
77 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
78 }
79 return type;
80 }
82 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
83 {
84 GObjectClass *object_class = (GObjectClass *) klass;
85 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
87 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
89 object_class->dispose = sp_gradient_context_dispose;
91 event_context_class->setup = sp_gradient_context_setup;
92 event_context_class->root_handler = sp_gradient_context_root_handler;
93 }
95 static void sp_gradient_context_init(SPGradientContext *gr_context)
96 {
97 SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
99 gr_context->cursor_addnode = false;
100 event_context->cursor_shape = cursor_gradient_xpm;
101 event_context->hot_x = 4;
102 event_context->hot_y = 4;
103 event_context->xp = 0;
104 event_context->yp = 0;
105 event_context->tolerance = 6;
106 event_context->within_tolerance = false;
107 event_context->item_to_select = NULL;
108 }
110 static void sp_gradient_context_dispose(GObject *object)
111 {
112 SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
113 SPEventContext *ec = SP_EVENT_CONTEXT(object);
115 ec->enableGrDrag(false);
117 if (rc->_message_context) {
118 delete rc->_message_context;
119 }
121 rc->selcon->disconnect();
122 delete rc->selcon;
123 rc->subselcon->disconnect();
124 delete rc->subselcon;
126 G_OBJECT_CLASS(parent_class)->dispose(object);
127 }
129 const gchar *gr_handle_descr [] = {
130 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
131 N_("Linear gradient <b>end</b>"),
132 N_("Linear gradient <b>mid stop</b>"),
133 N_("Radial gradient <b>center</b>"),
134 N_("Radial gradient <b>radius</b>"),
135 N_("Radial gradient <b>radius</b>"),
136 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
137 N_("Radial gradient <b>mid stop</b>"),
138 N_("Radial gradient <b>mid stop</b>")
139 };
141 static void
142 gradient_selection_changed (Inkscape::Selection *, gpointer data)
143 {
144 SPGradientContext *rc = (SPGradientContext *) data;
146 GrDrag *drag = rc->_grdrag;
147 Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(rc)->desktop);
148 if (selection == NULL) {
149 return;
150 }
151 guint n_obj = g_slist_length((GSList *) selection->itemList());
153 if (!drag->isNonEmpty() || selection->isEmpty())
154 return;
155 guint n_tot = drag->numDraggers();
156 guint n_sel = drag->numSelected();
158 //The use of ngettext in the following code is intentional even if the English singular form would never be used
159 if (n_sel == 1) {
160 if (drag->singleSelectedDraggerNumDraggables() == 1) {
161 gchar * message = g_strconcat(
162 //TRANSLATORS: %s will be substituted with the point name (see previous messages); This is part of a compound message
163 _("%s selected"),
164 //TRANSLATORS: Mind the space in front. This is part of a compound message
165 ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
166 ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
167 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
168 message,_(gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()]), n_tot, n_obj);
169 } else {
170 gchar * message = g_strconcat(
171 //TRANSLATORS: This is a part of a compound message (out of two more indicating: grandint handle count & object count)
172 ngettext("One handle merging %d stop (drag with <b>Shift</b> to separate) selected",
173 "One handle merging %d stops (drag with <b>Shift</b> to separate) selected",drag->singleSelectedDraggerNumDraggables()),
174 ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
175 ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
176 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message,drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
177 }
178 } else if (n_sel > 1) {
179 //TRANSLATORS: The plural refers to number of selected gradient handles. This is part of a compound message (part two indicates selected object count)
180 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),
181 //TRANSLATORS: Mind the space in front. (Refers to gradient handles selected). This is part of a compound message
182 ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
183 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message, n_sel, n_tot, n_obj);
184 } else if (n_sel == 0) {
185 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
186 //TRANSLATORS: The plural refers to number of selected objects
187 ngettext("<b>No</b> gradient handles selected out of %d on %d selected object",
188 "<b>No</b> gradient handles selected out of %d on %d selected objects",n_obj), n_tot, n_obj);
189 }
190 }
192 static void
193 gradient_subselection_changed (gpointer, gpointer data)
194 {
195 gradient_selection_changed (NULL, data);
196 }
199 static void sp_gradient_context_setup(SPEventContext *ec)
200 {
201 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
203 if (((SPEventContextClass *) parent_class)->setup) {
204 ((SPEventContextClass *) parent_class)->setup(ec);
205 }
207 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
208 if (prefs->getBool("/tools/gradient/selcue", true)) {
209 ec->enableSelectionCue();
210 }
212 ec->enableGrDrag();
213 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
215 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
217 rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
218 rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
219 gradient_selection_changed(selection, rc);
220 }
222 void
223 sp_gradient_context_select_next (SPEventContext *event_context)
224 {
225 GrDrag *drag = event_context->_grdrag;
226 g_assert (drag);
228 GrDragger *d = drag->select_next();
230 event_context->desktop->scroll_to_point(d->point, 1.0);
231 }
233 void
234 sp_gradient_context_select_prev (SPEventContext *event_context)
235 {
236 GrDrag *drag = event_context->_grdrag;
237 g_assert (drag);
239 GrDragger *d = drag->select_prev();
241 event_context->desktop->scroll_to_point(d->point, 1.0);
242 }
244 static bool
245 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, Geom::Point event_p)
246 {
247 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
249 //Translate mouse point into proper coord system
250 rc->mousepoint_doc = desktop->w2d(event_p);
252 SPCtrlLine* line = SP_CTRLLINE(item);
254 Geom::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e, 0);
255 double dist_screen = Geom::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
257 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
259 bool close = (dist_screen < tolerance);
261 return close;
262 }
264 std::vector<Geom::Point>
265 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
266 {
267 std::vector<Geom::Point> coords;
269 // for all selected draggers
270 for (GList *i = drag->selected; i != NULL; i = i->next) {
271 GrDragger *dragger = (GrDragger *) i->data;
272 // remember the coord of the dragger to reselect it later
273 coords.push_back(dragger->point);
274 // for all draggables of dragger
275 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
276 GrDraggable *d = (GrDraggable *) j->data;
278 // find the gradient
279 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
280 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
282 // these draggable types cannot have a next draggabe to insert a stop between them
283 if (d->point_type == POINT_LG_END ||
284 d->point_type == POINT_RG_FOCUS ||
285 d->point_type == POINT_RG_R1 ||
286 d->point_type == POINT_RG_R2) {
287 continue;
288 }
290 // from draggables to stops
291 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
292 SPStop *next_stop = this_stop->getNextStop();
293 SPStop *last_stop = sp_last_stop (vector);
295 gint fs = d->fill_or_stroke;
296 SPItem *item = d->item;
297 gint type = d->point_type;
298 gint p_i = d->point_i;
300 // if there's a next stop,
301 if (next_stop) {
302 GrDragger *dnext = NULL;
303 // find its dragger
304 // (complex because it may have different types, and because in radial,
305 // more than one dragger may correspond to a stop, so we must distinguish)
306 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
307 if (next_stop == last_stop)
308 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
309 else
310 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
311 } else { // radial
312 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
313 if (next_stop == last_stop)
314 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
315 else
316 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
317 }
318 if ((type == POINT_RG_MID2) ||
319 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
320 if (next_stop == last_stop)
321 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
322 else
323 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
324 }
325 }
327 // if both adjacent draggers selected,
328 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
330 // remember the coords of the future dragger to select it
331 coords.push_back(0.5*(dragger->point + dnext->point));
333 // do not insert a stop now, it will confuse the loop;
334 // just remember the stops
335 *these_stops = g_slist_prepend (*these_stops, this_stop);
336 *next_stops = g_slist_prepend (*next_stops, next_stop);
337 }
338 }
339 }
340 }
341 return coords;
342 }
344 static void
345 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
346 {
347 SPDocument *doc = NULL;
348 GrDrag *drag = rc->_grdrag;
350 GSList *these_stops = NULL;
351 GSList *next_stops = NULL;
353 std::vector<Geom::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
355 if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
356 // if a single stop is selected, add between that stop and the next one
357 GrDragger *dragger = (GrDragger *) drag->selected->data;
358 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
359 GrDraggable *d = (GrDraggable *) j->data;
360 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
361 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
362 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
363 SPStop *next_stop = this_stop->getNextStop();
364 if (this_stop && next_stop) {
365 these_stops = g_slist_prepend (these_stops, this_stop);
366 next_stops = g_slist_prepend (next_stops, next_stop);
367 }
368 }
369 }
371 // now actually create the new stops
372 GSList *i = these_stops;
373 GSList *j = next_stops;
374 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
375 SPStop *this_stop = (SPStop *) i->data;
376 SPStop *next_stop = (SPStop *) j->data;
377 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
378 SPObject *parent = SP_OBJECT_PARENT(this_stop);
379 if (SP_IS_GRADIENT (parent)) {
380 doc = SP_OBJECT_DOCUMENT (parent);
381 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
382 SP_GRADIENT(parent)->ensureVector();
383 }
384 }
386 if (g_slist_length(these_stops) > 0 && doc) {
387 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
388 drag->updateDraggers();
389 // so that it does not automatically update draggers in idle loop, as this would deselect
390 drag->local_change = true;
391 // select all the old selected and new created draggers
392 drag->selectByCoords(coords);
393 }
395 g_slist_free (these_stops);
396 g_slist_free (next_stops);
397 }
399 double sqr(double x) {return x*x;}
401 static void
402 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
403 {
404 SPDocument *doc = NULL;
405 GrDrag *drag = rc->_grdrag;
407 GSList *these_stops = NULL;
408 GSList *next_stops = NULL;
410 std::vector<Geom::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
412 GSList *todel = NULL;
414 GSList *i = these_stops;
415 GSList *j = next_stops;
416 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
417 SPStop *stop0 = (SPStop *) i->data;
418 SPStop *stop1 = (SPStop *) j->data;
420 gint i1 = g_slist_index(these_stops, stop1);
421 if (i1 != -1) {
422 GSList *next_next = g_slist_nth (next_stops, i1);
423 if (next_next) {
424 SPStop *stop2 = (SPStop *) next_next->data;
426 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
427 continue;
429 guint32 const c0 = sp_stop_get_rgba32(stop0);
430 guint32 const c2 = sp_stop_get_rgba32(stop2);
431 guint32 const c1r = sp_stop_get_rgba32(stop1);
432 guint32 c1 = average_color (c0, c2,
433 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
435 double diff =
436 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
437 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
438 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
439 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
441 if (diff < tolerance)
442 todel = g_slist_prepend (todel, stop1);
443 }
444 }
445 }
447 for (i = todel; i != NULL; i = i->next) {
448 SPStop *stop = (SPStop*) i->data;
449 doc = SP_OBJECT_DOCUMENT (stop);
450 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
451 parent->removeChild(SP_OBJECT_REPR(stop));
452 }
454 if (g_slist_length(todel) > 0) {
455 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
456 drag->local_change = true;
457 drag->updateDraggers();
458 drag->selectByCoords(coords);
459 }
461 g_slist_free (todel);
462 g_slist_free (these_stops);
463 g_slist_free (next_stops);
464 }
467 static void
468 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
469 {
470 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
472 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
473 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
475 double tolerance = (double) ec->tolerance;
477 ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
479 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
480 _("Add gradient stop"));
482 ec->get_drag()->updateDraggers();
483 }
486 static gint
487 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
488 {
489 static bool dragging;
491 SPDesktop *desktop = event_context->desktop;
492 Inkscape::Selection *selection = sp_desktop_selection (desktop);
493 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
495 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
497 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
498 double const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
500 GrDrag *drag = event_context->_grdrag;
501 g_assert (drag);
503 gint ret = FALSE;
504 switch (event->type) {
505 case GDK_2BUTTON_PRESS:
506 if ( event->button.button == 1 ) {
507 bool over_line = false;
508 SPCtrlLine *line = NULL;
509 if (drag->lines) {
510 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
511 line = (SPCtrlLine*) l->data;
512 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
513 }
514 }
515 if (over_line) {
516 // we take the first item in selection, because with doubleclick, the first click
517 // always resets selection to the single object under cursor
518 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
519 } else {
520 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
521 SPItem *item = SP_ITEM(i->data);
522 SPGradientType new_type = (SPGradientType) prefs->getInt("/tools/gradient/newgradient", SP_GRADIENT_TYPE_LINEAR);
523 guint new_fill = prefs->getInt("/tools/gradient/newfillorstroke", 1);
525 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
527 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
528 sp_gradient_reset_to_userspace(priv, item);
529 }
531 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
532 _("Create default gradient"));
533 }
534 ret = TRUE;
535 }
536 break;
537 case GDK_BUTTON_PRESS:
538 if ( event->button.button == 1 && !event_context->space_panning ) {
539 Geom::Point button_w(event->button.x, event->button.y);
541 // save drag origin
542 event_context->xp = (gint) button_w[Geom::X];
543 event_context->yp = (gint) button_w[Geom::Y];
544 event_context->within_tolerance = true;
546 dragging = true;
548 Geom::Point button_dt = to_2geom(desktop->w2d(button_w));
549 if (event->button.state & GDK_SHIFT_MASK) {
550 Inkscape::Rubberband::get(desktop)->start(desktop, from_2geom(button_dt));
551 } else {
552 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
553 // enable Ctrl+doubleclick of exactly the selected item(s)
554 if (!(event->button.state & GDK_CONTROL_MASK))
555 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
557 SnapManager &m = desktop->namedview->snap_manager;
558 m.setup(desktop);
559 m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
560 m.unSetup();
561 rc->origin = from_2geom(button_dt);
562 }
564 ret = TRUE;
565 }
566 break;
567 case GDK_MOTION_NOTIFY:
568 if ( dragging
569 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
570 {
571 if ( event_context->within_tolerance
572 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
573 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
574 break; // do not drag if we're within tolerance from origin
575 }
576 // Once the user has moved farther than tolerance from the original location
577 // (indicating they intend to draw, not click), then always process the
578 // motion notify coordinates as given (no snapping back to origin)
579 event_context->within_tolerance = false;
581 Geom::Point const motion_w(event->motion.x,
582 event->motion.y);
583 Geom::Point const motion_dt = event_context->desktop->w2d(motion_w);
585 if (Inkscape::Rubberband::get(desktop)->is_started()) {
586 Inkscape::Rubberband::get(desktop)->move(motion_dt);
587 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
588 } else {
589 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
590 }
591 gobble_motion_events(GDK_BUTTON1_MASK);
593 ret = TRUE;
594 } else {
595 if (!drag->mouseOver()) {
596 SnapManager &m = desktop->namedview->snap_manager;
597 m.setup(desktop);
599 Geom::Point const motion_w(event->motion.x, event->motion.y);
600 Geom::Point const motion_dt = event_context->desktop->w2d(motion_w);
602 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
603 m.unSetup();
604 }
606 bool over_line = false;
607 if (drag->lines) {
608 for (GSList *l = drag->lines; l != NULL; l = l->next) {
609 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, Geom::Point(event->motion.x, event->motion.y));
610 }
611 }
613 if (rc->cursor_addnode && !over_line) {
614 event_context->cursor_shape = cursor_gradient_xpm;
615 sp_event_context_update_cursor(event_context);
616 rc->cursor_addnode = false;
617 } else if (!rc->cursor_addnode && over_line) {
618 event_context->cursor_shape = cursor_gradient_add_xpm;
619 sp_event_context_update_cursor(event_context);
620 rc->cursor_addnode = true;
621 }
622 }
623 break;
624 case GDK_BUTTON_RELEASE:
625 event_context->xp = event_context->yp = 0;
626 if ( event->button.button == 1 && !event_context->space_panning ) {
627 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
628 bool over_line = false;
629 SPCtrlLine *line = NULL;
630 if (drag->lines) {
631 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
632 line = (SPCtrlLine*) l->data;
633 over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
634 if (over_line)
635 break;
636 }
637 }
638 if (over_line && line) {
639 sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
640 ret = TRUE;
641 }
642 } else {
643 dragging = false;
645 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
646 if (event->button.state & GDK_CONTROL_MASK) {
647 ret = TRUE;
648 break;
649 }
651 if (!event_context->within_tolerance) {
652 // we've been dragging, either do nothing (grdrag handles that),
653 // or rubberband-select if we have rubberband
654 Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
655 if (r->is_started() && !event_context->within_tolerance) {
656 // this was a rubberband drag
657 if (r->getMode() == RUBBERBAND_MODE_RECT) {
658 Geom::OptRect const b = r->getRectangle();
659 drag->selectRect(*b);
660 }
661 }
663 } else if (event_context->item_to_select) {
664 // no dragging, select clicked item if any
665 if (event->button.state & GDK_SHIFT_MASK) {
666 selection->toggle(event_context->item_to_select);
667 } else {
668 selection->set(event_context->item_to_select);
669 }
670 } else {
671 // click in an empty space; do the same as Esc
672 if (drag->selected) {
673 drag->deselectAll();
674 } else {
675 selection->clear();
676 }
677 }
679 event_context->item_to_select = NULL;
680 ret = TRUE;
681 }
682 Inkscape::Rubberband::get(desktop)->stop();
683 }
684 break;
685 case GDK_KEY_PRESS:
686 switch (get_group0_keyval (&event->key)) {
687 case GDK_Alt_L:
688 case GDK_Alt_R:
689 case GDK_Control_L:
690 case GDK_Control_R:
691 case GDK_Shift_L:
692 case GDK_Shift_R:
693 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
694 case GDK_Meta_R:
695 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
696 _("<b>Ctrl</b>: snap gradient angle"),
697 _("<b>Shift</b>: draw gradient around the starting point"),
698 NULL);
699 break;
701 case GDK_x:
702 case GDK_X:
703 if (MOD__ALT_ONLY) {
704 desktop->setToolboxFocusTo ("altx-grad");
705 ret = TRUE;
706 }
707 break;
709 case GDK_A:
710 case GDK_a:
711 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
712 drag->selectAll();
713 ret = TRUE;
714 }
715 break;
717 case GDK_L:
718 case GDK_l:
719 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
720 sp_gradient_simplify(rc, 1e-4);
721 ret = TRUE;
722 }
723 break;
725 case GDK_Escape:
726 if (drag->selected) {
727 drag->deselectAll();
728 } else {
729 selection->clear();
730 }
731 ret = TRUE;
732 //TODO: make dragging escapable by Esc
733 break;
735 case GDK_Left: // move handle left
736 case GDK_KP_Left:
737 case GDK_KP_4:
738 if (!MOD__CTRL) { // not ctrl
739 gint mul = 1 + gobble_key_events(
740 get_group0_keyval(&event->key), 0); // with any mask
741 if (MOD__ALT) { // alt
742 if (MOD__SHIFT) drag->selected_move_screen(mul*-10, 0); // shift
743 else drag->selected_move_screen(mul*-1, 0); // no shift
744 }
745 else { // no alt
746 if (MOD__SHIFT) drag->selected_move(mul*-10*nudge, 0); // shift
747 else drag->selected_move(mul*-nudge, 0); // no shift
748 }
749 ret = TRUE;
750 }
751 break;
752 case GDK_Up: // move handle up
753 case GDK_KP_Up:
754 case GDK_KP_8:
755 if (!MOD__CTRL) { // not ctrl
756 gint mul = 1 + gobble_key_events(
757 get_group0_keyval(&event->key), 0); // with any mask
758 if (MOD__ALT) { // alt
759 if (MOD__SHIFT) drag->selected_move_screen(0, mul*10); // shift
760 else drag->selected_move_screen(0, mul*1); // no shift
761 }
762 else { // no alt
763 if (MOD__SHIFT) drag->selected_move(0, mul*10*nudge); // shift
764 else drag->selected_move(0, mul*nudge); // no shift
765 }
766 ret = TRUE;
767 }
768 break;
769 case GDK_Right: // move handle right
770 case GDK_KP_Right:
771 case GDK_KP_6:
772 if (!MOD__CTRL) { // not ctrl
773 gint mul = 1 + gobble_key_events(
774 get_group0_keyval(&event->key), 0); // with any mask
775 if (MOD__ALT) { // alt
776 if (MOD__SHIFT) drag->selected_move_screen(mul*10, 0); // shift
777 else drag->selected_move_screen(mul*1, 0); // no shift
778 }
779 else { // no alt
780 if (MOD__SHIFT) drag->selected_move(mul*10*nudge, 0); // shift
781 else drag->selected_move(mul*nudge, 0); // no shift
782 }
783 ret = TRUE;
784 }
785 break;
786 case GDK_Down: // move handle down
787 case GDK_KP_Down:
788 case GDK_KP_2:
789 if (!MOD__CTRL) { // not ctrl
790 gint mul = 1 + gobble_key_events(
791 get_group0_keyval(&event->key), 0); // with any mask
792 if (MOD__ALT) { // alt
793 if (MOD__SHIFT) drag->selected_move_screen(0, mul*-10); // shift
794 else drag->selected_move_screen(0, mul*-1); // no shift
795 }
796 else { // no alt
797 if (MOD__SHIFT) drag->selected_move(0, mul*-10*nudge); // shift
798 else drag->selected_move(0, mul*-nudge); // no shift
799 }
800 ret = TRUE;
801 }
802 break;
803 case GDK_r:
804 case GDK_R:
805 if (MOD__SHIFT_ONLY) {
806 // First try selected dragger
807 if (drag && drag->selected) {
808 drag->selected_reverse_vector();
809 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
810 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
811 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
812 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
813 }
814 }
815 // we did an undoable action
816 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
817 _("Invert gradient"));
818 ret = TRUE;
819 }
820 break;
822 case GDK_Insert:
823 case GDK_KP_Insert:
824 // with any modifiers:
825 sp_gradient_context_add_stops_between_selected_stops (rc);
826 ret = TRUE;
827 break;
829 case GDK_Delete:
830 case GDK_KP_Delete:
831 case GDK_BackSpace:
832 if ( drag->selected ) {
833 drag->deleteSelected(MOD__CTRL_ONLY);
834 ret = TRUE;
835 }
836 break;
837 default:
838 break;
839 }
840 break;
841 case GDK_KEY_RELEASE:
842 switch (get_group0_keyval (&event->key)) {
843 case GDK_Alt_L:
844 case GDK_Alt_R:
845 case GDK_Control_L:
846 case GDK_Control_R:
847 case GDK_Shift_L:
848 case GDK_Shift_R:
849 case GDK_Meta_L: // Meta is when you press Shift+Alt
850 case GDK_Meta_R:
851 event_context->defaultMessageContext()->clear();
852 break;
853 default:
854 break;
855 }
856 break;
857 default:
858 break;
859 }
861 if (!ret) {
862 if (((SPEventContextClass *) parent_class)->root_handler) {
863 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
864 }
865 }
867 return ret;
868 }
870 static void sp_gradient_drag(SPGradientContext &rc, Geom::Point const pt, guint /*state*/, guint32 etime)
871 {
872 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
873 Inkscape::Selection *selection = sp_desktop_selection(desktop);
874 SPDocument *document = sp_desktop_document(desktop);
875 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
877 if (!selection->isEmpty()) {
878 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
879 int type = prefs->getInt("/tools/gradient/newgradient", 1);
880 int fill_or_stroke = prefs->getInt("/tools/gradient/newfillorstroke", 1);
882 SPGradient *vector;
883 if (ec->item_to_select) {
884 // pick color from the object where drag started
885 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
886 } else {
887 // Starting from empty space:
888 // Sort items so that the topmost comes last
889 GSList *items = g_slist_copy ((GSList *) selection->itemList());
890 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
891 // take topmost
892 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
893 g_slist_free (items);
894 }
896 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
897 SPCSSAttr *css = sp_repr_css_attr_new();
898 sp_repr_css_set_property(css, "fill-opacity", "1.0");
900 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
902 //FIXME: see above
903 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
905 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
907 if (type == SP_GRADIENT_TYPE_LINEAR) {
908 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
909 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
910 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
911 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
912 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
913 }
914 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
915 }
916 if (ec->_grdrag) {
917 ec->_grdrag->updateDraggers();
918 // prevent regenerating draggers by selection modified signal, which sometimes
919 // comes too late and thus destroys the knot which we will now grab:
920 ec->_grdrag->local_change = true;
921 // give the grab out-of-bounds values of xp/yp because we're already dragging
922 // and therefore are already out of tolerance
923 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
924 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
925 -1, // ignore number (though it is always 1)
926 fill_or_stroke, 99999, 99999, etime);
927 }
928 // We did an undoable action, but sp_document_done will be called by the knot when released
930 // status text; we do not track coords because this branch is run once, not all the time
931 // during drag
932 int n_objects = g_slist_length((GSList *) selection->itemList());
933 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
934 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
935 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
936 n_objects);
937 } else {
938 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
939 }
940 }
943 /*
944 Local Variables:
945 mode:c++
946 c-file-style:"stroustrup"
947 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
948 indent-tabs-mode:nil
949 fill-column:99
950 End:
951 */
952 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :