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 "pixmaps/cursor-gradient-delete.xpm"
33 #include "gradient-context.h"
34 #include "gradient-chemistry.h"
35 #include <glibmm/i18n.h>
36 #include "prefs-utils.h"
37 #include "gradient-drag.h"
38 #include "gradient-chemistry.h"
39 #include "xml/repr.h"
40 #include "sp-item.h"
41 #include "display/sp-ctrlline.h"
42 #include "sp-linear-gradient.h"
43 #include "sp-radial-gradient.h"
44 #include "sp-stop.h"
45 #include "svg/css-ostringstream.h"
46 #include "svg/svg-color.h"
47 #include "snap.h"
48 #include "sp-namedview.h"
49 #include "rubberband.h"
53 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
54 static void sp_gradient_context_init(SPGradientContext *gr_context);
55 static void sp_gradient_context_dispose(GObject *object);
57 static void sp_gradient_context_setup(SPEventContext *ec);
59 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
61 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
63 static SPEventContextClass *parent_class;
66 GtkType sp_gradient_context_get_type()
67 {
68 static GType type = 0;
69 if (!type) {
70 GTypeInfo info = {
71 sizeof(SPGradientContextClass),
72 NULL, NULL,
73 (GClassInitFunc) sp_gradient_context_class_init,
74 NULL, NULL,
75 sizeof(SPGradientContext),
76 4,
77 (GInstanceInitFunc) sp_gradient_context_init,
78 NULL, /* value_table */
79 };
80 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
81 }
82 return type;
83 }
85 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
86 {
87 GObjectClass *object_class = (GObjectClass *) klass;
88 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
90 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
92 object_class->dispose = sp_gradient_context_dispose;
94 event_context_class->setup = sp_gradient_context_setup;
95 event_context_class->root_handler = sp_gradient_context_root_handler;
96 }
98 static void sp_gradient_context_init(SPGradientContext *gr_context)
99 {
100 SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
102 gr_context->cursor_addnode = false;
103 event_context->cursor_shape = cursor_gradient_xpm;
104 event_context->hot_x = 4;
105 event_context->hot_y = 4;
106 event_context->xp = 0;
107 event_context->yp = 0;
108 event_context->tolerance = 6;
109 event_context->within_tolerance = false;
110 event_context->item_to_select = NULL;
111 }
113 static void sp_gradient_context_dispose(GObject *object)
114 {
115 SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
116 SPEventContext *ec = SP_EVENT_CONTEXT(object);
118 ec->enableGrDrag(false);
120 if (rc->_message_context) {
121 delete rc->_message_context;
122 }
124 rc->selcon->disconnect();
125 delete rc->selcon;
126 rc->subselcon->disconnect();
127 delete rc->subselcon;
129 G_OBJECT_CLASS(parent_class)->dispose(object);
130 }
132 const gchar *gr_handle_descr [] = {
133 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
134 N_("Linear gradient <b>end</b>"),
135 N_("Linear gradient <b>mid stop</b>"),
136 N_("Radial gradient <b>center</b>"),
137 N_("Radial gradient <b>radius</b>"),
138 N_("Radial gradient <b>radius</b>"),
139 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
140 N_("Radial gradient <b>mid stop</b>"),
141 N_("Radial gradient <b>mid stop</b>")
142 };
144 static void
145 gradient_selection_changed (Inkscape::Selection *, gpointer data)
146 {
147 SPGradientContext *rc = (SPGradientContext *) data;
149 GrDrag *drag = rc->_grdrag;
150 Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(rc)->desktop);
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 if (n_sel == 1) {
159 if (drag->singleSelectedDraggerNumDraggables() == 1) {
160 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
161 _("%s selected out of %d gradient handles on %d selected object(s)"), gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()], n_tot, n_obj);
162 } else {
163 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
164 _("One handle merging %d stops (drag with <b>Shift</b> to separate) selected out of %d gradient handles on %d selected object(s)"), drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
165 }
166 } else if (n_sel > 1) {
167 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
168 _("<b>%d</b> gradient handles selected out of %d on %d selected object(s)"), n_sel, n_tot, n_obj);
169 } else if (n_sel == 0) {
170 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
171 _("<b>No</b> gradient handles selected out of %d on %d selected object(s)"), n_tot, n_obj);
172 }
173 }
175 static void
176 gradient_subselection_changed (gpointer, gpointer data)
177 {
178 gradient_selection_changed (NULL, data);
179 }
182 static void sp_gradient_context_setup(SPEventContext *ec)
183 {
184 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
186 if (((SPEventContextClass *) parent_class)->setup) {
187 ((SPEventContextClass *) parent_class)->setup(ec);
188 }
190 if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
191 ec->enableSelectionCue();
192 }
194 ec->enableGrDrag();
195 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
197 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
199 rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
200 rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
201 gradient_selection_changed(selection, rc);
202 }
204 void
205 sp_gradient_context_select_next (SPEventContext *event_context)
206 {
207 GrDrag *drag = event_context->_grdrag;
208 g_assert (drag);
210 GrDragger *d = drag->select_next();
212 event_context->desktop->scroll_to_point(&(d->point), 1.0);
213 }
215 void
216 sp_gradient_context_select_prev (SPEventContext *event_context)
217 {
218 GrDrag *drag = event_context->_grdrag;
219 g_assert (drag);
221 GrDragger *d = drag->select_prev();
223 event_context->desktop->scroll_to_point(&(d->point), 1.0);
224 }
226 // FIXME: make global function in libnr or somewhere.
227 static NR::Point
228 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end)
229 {
230 double length = NR::L2(end - begin);
231 NR::Point be = (end - begin) / length;
232 double r = NR::dot(p - begin, be);
234 if (r < 0.0) return begin;
235 if (r > length) return end;
237 return (begin + r * be);
238 }
240 static bool
241 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
242 {
243 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
245 //Translate mouse point into proper coord system
246 rc->mousepoint_doc = desktop->w2d(event_p);
248 SPCtrlLine* line = SP_CTRLLINE(item);
250 NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e);
251 double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
253 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
255 bool close = (dist_screen < tolerance);
257 return close;
258 }
260 static double
261 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
262 {
263 double length = NR::L2(end - begin);
264 NR::Point be = (end - begin) / length;
265 double r = NR::dot(p - begin, be);
267 if (r < 0.0) return 0.0;
268 if (r > length) return 1.0;
270 return (r / length);
271 }
273 std::vector<NR::Point>
274 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
275 {
276 std::vector<NR::Point> coords;
278 // for all selected draggers
279 for (GList *i = drag->selected; i != NULL; i = i->next) {
280 GrDragger *dragger = (GrDragger *) i->data;
281 // remember the coord of the dragger to reselect it later
282 coords.push_back(dragger->point);
283 // for all draggables of dragger
284 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
285 GrDraggable *d = (GrDraggable *) j->data;
287 // find the gradient
288 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
289 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
291 // these draggable types cannot have a next draggabe to insert a stop between them
292 if (d->point_type == POINT_LG_END ||
293 d->point_type == POINT_RG_FOCUS ||
294 d->point_type == POINT_RG_R1 ||
295 d->point_type == POINT_RG_R2) {
296 continue;
297 }
299 // from draggables to stops
300 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
301 SPStop *next_stop = sp_next_stop (this_stop);
302 SPStop *last_stop = sp_last_stop (vector);
304 gint fs = d->fill_or_stroke;
305 SPItem *item = d->item;
306 gint type = d->point_type;
307 gint p_i = d->point_i;
309 // if there's a next stop,
310 if (next_stop) {
311 GrDragger *dnext = NULL;
312 // find its dragger
313 // (complex because it may have different types, and because in radial,
314 // more than one dragger may correspond to a stop, so we must distinguish)
315 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
316 if (next_stop == last_stop)
317 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
318 else
319 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
320 } else { // radial
321 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
322 if (next_stop == last_stop)
323 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
324 else
325 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
326 }
327 if ((type == POINT_RG_MID2) ||
328 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
329 if (next_stop == last_stop)
330 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
331 else
332 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
333 }
334 }
336 // remember the coords of the future dragger to select it
337 coords.push_back(0.5*(dragger->point + dnext->point));
339 // if both adjacent draggers selected,
340 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
341 // do not insert a stop now, it will confuse the loop;
342 // just remember the stops
343 *these_stops = g_slist_prepend (*these_stops, this_stop);
344 *next_stops = g_slist_prepend (*next_stops, next_stop);
345 }
346 }
347 }
348 }
349 return coords;
350 }
352 static void
353 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
354 {
355 SPDocument *doc = NULL;
356 GrDrag *drag = rc->_grdrag;
358 GSList *these_stops = NULL;
359 GSList *next_stops = NULL;
361 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
363 if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
364 // if a single stop is selected, add between that stop and the next one
365 GrDragger *dragger = (GrDragger *) drag->selected->data;
366 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
367 GrDraggable *d = (GrDraggable *) j->data;
368 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
369 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
370 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
371 SPStop *next_stop = sp_next_stop (this_stop);
372 if (this_stop && next_stop) {
373 these_stops = g_slist_prepend (these_stops, this_stop);
374 next_stops = g_slist_prepend (next_stops, next_stop);
375 }
376 }
377 }
379 // now actually create the new stops
380 GSList *i = these_stops;
381 GSList *j = next_stops;
382 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
383 SPStop *this_stop = (SPStop *) i->data;
384 SPStop *next_stop = (SPStop *) j->data;
385 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
386 SPObject *parent = SP_OBJECT_PARENT(this_stop);
387 if (SP_IS_GRADIENT (parent)) {
388 doc = SP_OBJECT_DOCUMENT (parent);
389 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
390 sp_gradient_ensure_vector (SP_GRADIENT (parent));
391 }
392 }
394 if (g_slist_length(these_stops) > 0 && doc) {
395 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
396 drag->updateDraggers();
397 // so that it does not automatically update draggers in idle loop, as this would deselect
398 drag->local_change = true;
399 // select all the old selected and new created draggers
400 drag->selectByCoords(coords);
401 }
403 g_slist_free (these_stops);
404 g_slist_free (next_stops);
405 }
407 double sqr(double x) {return x*x;}
409 static void
410 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
411 {
412 SPDocument *doc = NULL;
413 GrDrag *drag = rc->_grdrag;
415 GSList *these_stops = NULL;
416 GSList *next_stops = NULL;
418 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
420 GSList *todel = NULL;
422 GSList *i = these_stops;
423 GSList *j = next_stops;
424 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
425 SPStop *stop0 = (SPStop *) i->data;
426 SPStop *stop1 = (SPStop *) j->data;
428 gint i1 = g_slist_index(these_stops, stop1);
429 if (i1 != -1) {
430 GSList *next_next = g_slist_nth (next_stops, i1);
431 if (next_next) {
432 SPStop *stop2 = (SPStop *) next_next->data;
434 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
435 continue;
437 guint32 const c0 = sp_stop_get_rgba32(stop0);
438 guint32 const c2 = sp_stop_get_rgba32(stop2);
439 guint32 const c1r = sp_stop_get_rgba32(stop1);
440 guint32 c1 = average_color (c0, c2,
441 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
443 double diff =
444 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
445 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
446 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
447 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
449 if (diff < tolerance)
450 todel = g_slist_prepend (todel, stop1);
451 }
452 }
453 }
455 for (i = todel; i != NULL; i = i->next) {
456 SPStop *stop = (SPStop*) i->data;
457 doc = SP_OBJECT_DOCUMENT (stop);
458 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
459 parent->removeChild(SP_OBJECT_REPR(stop));
460 }
462 if (g_slist_length(todel) > 0) {
463 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
464 drag->local_change = true;
465 drag->updateDraggers();
466 drag->selectByCoords(coords);
467 }
469 g_slist_free (todel);
470 g_slist_free (these_stops);
471 g_slist_free (next_stops);
472 }
476 static void
477 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 etime)
478 {
479 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
481 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
482 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
484 double tolerance = (double) ec->tolerance;
486 gfloat offset; // type of SPStop.offset = gfloat
487 SPGradient *gradient;
488 bool fill_or_stroke = true;
489 bool r1_knot = false;
491 bool addknot = false;
492 do {
493 gradient = sp_item_gradient (item, fill_or_stroke);
494 if (SP_IS_LINEARGRADIENT(gradient)) {
495 NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
496 NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
498 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
499 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
500 if ( dist_screen < tolerance ) {
501 // add the knot
502 offset = get_offset_between_points(nearest, begin, end);
503 addknot = true;
504 break; // break out of the while loop: add only one knot
505 }
506 } else if (SP_IS_RADIALGRADIENT(gradient)) {
507 NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
508 NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
509 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
510 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
511 if ( dist_screen < tolerance ) {
512 offset = get_offset_between_points(nearest, begin, end);
513 addknot = true;
514 r1_knot = true;
515 break; // break out of the while loop: add only one knot
516 }
518 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
519 nearest = snap_vector_midpoint (mouse_p, begin, end);
520 dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
521 if ( dist_screen < tolerance ) {
522 offset = get_offset_between_points(nearest, begin, end);
523 addknot = true;
524 r1_knot = false;
525 break; // break out of the while loop: add only one knot
526 }
527 }
528 fill_or_stroke = !fill_or_stroke;
529 } while (!fill_or_stroke && !addknot) ;
531 if (addknot) {
532 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
533 SPStop* prev_stop = sp_first_stop(vector);
534 SPStop* next_stop = sp_next_stop(prev_stop);
535 while ( (next_stop) && (next_stop->offset < offset) ) {
536 prev_stop = next_stop;
537 next_stop = sp_next_stop(next_stop);
538 }
539 if (!next_stop) {
540 // logical error: the endstop should have offset 1 and should always be more than this offset here
541 return;
542 }
545 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
547 sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT,
548 _("Add gradient stop"));
550 ec->_grdrag->updateDraggers();
551 sp_gradient_ensure_vector (gradient);
553 if (vector->has_stops) {
554 guint i = sp_number_of_stops_before_stop(vector, newstop);
556 gradient = sp_item_gradient (item, fill_or_stroke);
557 GrPointType pointtype = POINT_G_INVALID;
558 if (SP_IS_LINEARGRADIENT(gradient)) {
559 pointtype = POINT_LG_MID;
560 } else if (SP_IS_RADIALGRADIENT(gradient)) {
561 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
562 }
563 GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
564 if (dragger && (etime == 0) ) {
565 ec->_grdrag->setSelected (dragger);
566 } else {
567 ec->_grdrag->grabKnot (item,
568 pointtype,
569 i,
570 fill_or_stroke, 99999, 99999, etime);
571 }
572 ec->_grdrag->local_change = true;
574 }
575 }
576 }
579 static gint
580 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
581 {
582 static bool dragging;
584 SPDesktop *desktop = event_context->desktop;
585 Inkscape::Selection *selection = sp_desktop_selection (desktop);
587 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
589 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
590 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
592 GrDrag *drag = event_context->_grdrag;
593 g_assert (drag);
595 gint ret = FALSE;
596 switch (event->type) {
597 case GDK_2BUTTON_PRESS:
598 if ( event->button.button == 1 ) {
599 bool over_line = false;
600 SPCtrlLine *line = NULL;
601 if (drag->lines) {
602 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
603 line = (SPCtrlLine*) l->data;
604 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
605 }
606 }
607 if (over_line) {
608 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
609 } else {
610 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
611 SPItem *item = SP_ITEM(i->data);
612 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
613 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
615 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
617 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
618 sp_gradient_reset_to_userspace(priv, item);
619 }
621 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
622 _("Create default gradient"));
623 }
624 ret = TRUE;
625 }
626 break;
627 case GDK_BUTTON_PRESS:
628 if ( event->button.button == 1 && !event_context->space_panning ) {
629 NR::Point const button_w(event->button.x, event->button.y);
631 // save drag origin
632 event_context->xp = (gint) button_w[NR::X];
633 event_context->yp = (gint) button_w[NR::Y];
634 event_context->within_tolerance = true;
636 dragging = true;
638 NR::Point const button_dt = desktop->w2d(button_w);
639 if (event->button.state & GDK_SHIFT_MASK) {
640 Inkscape::Rubberband::get()->start(desktop, button_dt);
641 } else {
642 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
643 // enable Ctrl+doubleclick of exactly the selected item(s)
644 if (!(event->button.state & GDK_CONTROL_MASK))
645 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
647 /* Snap center to nearest magnetic point */
648 SnapManager const &m = desktop->namedview->snap_manager;
649 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
650 }
652 ret = TRUE;
653 }
654 break;
655 case GDK_MOTION_NOTIFY:
656 if ( dragging
657 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
658 {
659 if ( event_context->within_tolerance
660 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
661 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
662 break; // do not drag if we're within tolerance from origin
663 }
664 // Once the user has moved farther than tolerance from the original location
665 // (indicating they intend to draw, not click), then always process the
666 // motion notify coordinates as given (no snapping back to origin)
667 event_context->within_tolerance = false;
669 NR::Point const motion_w(event->motion.x,
670 event->motion.y);
671 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
673 if (Inkscape::Rubberband::get()->is_started()) {
674 Inkscape::Rubberband::get()->move(motion_dt);
675 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
676 } else {
677 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
678 }
679 gobble_motion_events(GDK_BUTTON1_MASK);
681 ret = TRUE;
682 } else {
683 bool over_line = false;
684 if (drag->lines) {
685 for (GSList *l = drag->lines; l != NULL; l = l->next) {
686 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
687 }
688 }
690 if (rc->cursor_addnode && !over_line) {
691 event_context->cursor_shape = cursor_gradient_xpm;
692 sp_event_context_update_cursor(event_context);
693 rc->cursor_addnode = false;
694 } else if (!rc->cursor_addnode && over_line) {
695 event_context->cursor_shape = cursor_gradient_add_xpm;
696 sp_event_context_update_cursor(event_context);
697 rc->cursor_addnode = true;
698 }
699 }
700 break;
701 case GDK_BUTTON_RELEASE:
702 event_context->xp = event_context->yp = 0;
703 if ( event->button.button == 1 && !event_context->space_panning ) {
704 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
705 bool over_line = false;
706 SPCtrlLine *line = NULL;
707 if (drag->lines) {
708 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
709 line = (SPCtrlLine*) l->data;
710 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
711 }
712 }
713 if (over_line) {
714 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
715 ret = TRUE;
716 }
717 } else {
718 dragging = false;
720 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
721 if (event->button.state & GDK_CONTROL_MASK) {
722 ret = TRUE;
723 break;
724 }
726 if (!event_context->within_tolerance) {
727 // we've been dragging, either do nothing (grdrag handles that),
728 // or rubberband-select if we have rubberband
729 Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
730 if (r->is_started() && !event_context->within_tolerance) {
731 // this was a rubberband drag
732 if (r->getMode() == RUBBERBAND_MODE_RECT) {
733 NR::Maybe<NR::Rect> const b = r->getRectangle();
734 drag->selectRect(*b);
735 }
736 }
738 } else if (event_context->item_to_select) {
739 // no dragging, select clicked item if any
740 if (event->button.state & GDK_SHIFT_MASK) {
741 selection->toggle(event_context->item_to_select);
742 } else {
743 selection->set(event_context->item_to_select);
744 }
745 } else {
746 // click in an empty space; do the same as Esc
747 if (drag->selected) {
748 drag->deselectAll();
749 } else {
750 selection->clear();
751 }
752 }
754 event_context->item_to_select = NULL;
755 ret = TRUE;
756 }
757 Inkscape::Rubberband::get()->stop();
758 }
759 break;
760 case GDK_KEY_PRESS:
761 switch (get_group0_keyval (&event->key)) {
762 case GDK_Alt_L:
763 case GDK_Alt_R:
764 case GDK_Control_L:
765 case GDK_Control_R:
766 case GDK_Shift_L:
767 case GDK_Shift_R:
768 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
769 case GDK_Meta_R:
770 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
771 _("<b>Ctrl</b>: snap gradient angle"),
772 _("<b>Shift</b>: draw gradient around the starting point"),
773 NULL);
774 break;
776 case GDK_x:
777 case GDK_X:
778 if (MOD__ALT_ONLY) {
779 desktop->setToolboxFocusTo ("altx-grad");
780 ret = TRUE;
781 }
782 break;
784 case GDK_A:
785 case GDK_a:
786 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
787 drag->selectAll();
788 ret = TRUE;
789 }
790 break;
792 case GDK_L:
793 case GDK_l:
794 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
795 sp_gradient_simplify(rc, 1e-4);
796 ret = TRUE;
797 }
798 break;
800 case GDK_Escape:
801 if (drag->selected) {
802 drag->deselectAll();
803 } else {
804 selection->clear();
805 }
806 ret = TRUE;
807 //TODO: make dragging escapable by Esc
808 break;
810 case GDK_Left: // move handle left
811 case GDK_KP_Left:
812 case GDK_KP_4:
813 if (!MOD__CTRL) { // not ctrl
814 if (MOD__ALT) { // alt
815 if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
816 else drag->selected_move_screen(-1, 0); // no shift
817 }
818 else { // no alt
819 if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
820 else drag->selected_move(-nudge, 0); // no shift
821 }
822 ret = TRUE;
823 }
824 break;
825 case GDK_Up: // move handle up
826 case GDK_KP_Up:
827 case GDK_KP_8:
828 if (!MOD__CTRL) { // not ctrl
829 if (MOD__ALT) { // alt
830 if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
831 else drag->selected_move_screen(0, 1); // no shift
832 }
833 else { // no alt
834 if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
835 else drag->selected_move(0, nudge); // no shift
836 }
837 ret = TRUE;
838 }
839 break;
840 case GDK_Right: // move handle right
841 case GDK_KP_Right:
842 case GDK_KP_6:
843 if (!MOD__CTRL) { // not ctrl
844 if (MOD__ALT) { // alt
845 if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
846 else drag->selected_move_screen(1, 0); // no shift
847 }
848 else { // no alt
849 if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
850 else drag->selected_move(nudge, 0); // no shift
851 }
852 ret = TRUE;
853 }
854 break;
855 case GDK_Down: // move handle down
856 case GDK_KP_Down:
857 case GDK_KP_2:
858 if (!MOD__CTRL) { // not ctrl
859 if (MOD__ALT) { // alt
860 if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
861 else drag->selected_move_screen(0, -1); // no shift
862 }
863 else { // no alt
864 if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
865 else drag->selected_move(0, -nudge); // no shift
866 }
867 ret = TRUE;
868 }
869 break;
870 case GDK_r:
871 case GDK_R:
872 if (MOD__SHIFT_ONLY) {
873 // First try selected dragger
874 if (drag && drag->selected) {
875 drag->selected_reverse_vector();
876 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
877 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
878 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
879 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
880 }
881 }
882 // we did an undoable action
883 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
884 _("Invert gradient"));
885 ret = TRUE;
886 }
887 break;
889 case GDK_Insert:
890 case GDK_KP_Insert:
891 // with any modifiers:
892 sp_gradient_context_add_stops_between_selected_stops (rc);
893 ret = TRUE;
894 break;
896 case GDK_Delete:
897 case GDK_KP_Delete:
898 case GDK_BackSpace:
899 if ( drag->selected ) {
900 drag->deleteSelected(MOD__CTRL_ONLY);
901 ret = TRUE;
902 }
903 break;
904 default:
905 break;
906 }
907 break;
908 case GDK_KEY_RELEASE:
909 switch (get_group0_keyval (&event->key)) {
910 case GDK_Alt_L:
911 case GDK_Alt_R:
912 case GDK_Control_L:
913 case GDK_Control_R:
914 case GDK_Shift_L:
915 case GDK_Shift_R:
916 case GDK_Meta_L: // Meta is when you press Shift+Alt
917 case GDK_Meta_R:
918 event_context->defaultMessageContext()->clear();
919 break;
920 default:
921 break;
922 }
923 break;
924 default:
925 break;
926 }
928 if (!ret) {
929 if (((SPEventContextClass *) parent_class)->root_handler) {
930 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
931 }
932 }
934 return ret;
935 }
937 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
938 {
939 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
940 Inkscape::Selection *selection = sp_desktop_selection(desktop);
941 SPDocument *document = sp_desktop_document(desktop);
942 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
944 if (!selection->isEmpty()) {
945 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
946 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
948 SPGradient *vector;
949 if (ec->item_to_select) {
950 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
951 } else {
952 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
953 }
955 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
956 SPCSSAttr *css = sp_repr_css_attr_new();
957 sp_repr_css_set_property(css, "fill-opacity", "1.0");
959 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
961 //FIXME: see above
962 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
964 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
966 if (type == SP_GRADIENT_TYPE_LINEAR) {
967 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
968 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
969 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
970 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
971 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
972 }
973 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
974 }
975 if (ec->_grdrag) {
976 ec->_grdrag->updateDraggers();
977 // prevent regenerating draggers by selection modified signal, which sometimes
978 // comes too late and thus destroys the knot which we will now grab:
979 ec->_grdrag->local_change = true;
980 // give the grab out-of-bounds values of xp/yp because we're already dragging
981 // and therefore are already out of tolerance
982 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
983 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
984 -1, // ignore number (though it is always 1)
985 fill_or_stroke, 99999, 99999, etime);
986 }
987 // We did an undoable action, but sp_document_done will be called by the knot when released
989 // status text; we do not track coords because this branch is run once, not all the time
990 // during drag
991 int n_objects = g_slist_length((GSList *) selection->itemList());
992 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
993 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
994 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
995 n_objects);
996 } else {
997 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
998 }
999 }
1002 /*
1003 Local Variables:
1004 mode:c++
1005 c-file-style:"stroustrup"
1006 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1007 indent-tabs-mode:nil
1008 fill-column:99
1009 End:
1010 */
1011 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :