e8a280c408d7dec8285a4ea8f5bdb88e9800d151
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"
52 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
53 static void sp_gradient_context_init(SPGradientContext *gr_context);
54 static void sp_gradient_context_dispose(GObject *object);
56 static void sp_gradient_context_setup(SPEventContext *ec);
58 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
60 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
62 static SPEventContextClass *parent_class;
65 GtkType sp_gradient_context_get_type()
66 {
67 static GType type = 0;
68 if (!type) {
69 GTypeInfo info = {
70 sizeof(SPGradientContextClass),
71 NULL, NULL,
72 (GClassInitFunc) sp_gradient_context_class_init,
73 NULL, NULL,
74 sizeof(SPGradientContext),
75 4,
76 (GInstanceInitFunc) sp_gradient_context_init,
77 NULL, /* value_table */
78 };
79 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
80 }
81 return type;
82 }
84 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
85 {
86 GObjectClass *object_class = (GObjectClass *) klass;
87 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
91 object_class->dispose = sp_gradient_context_dispose;
93 event_context_class->setup = sp_gradient_context_setup;
94 event_context_class->root_handler = sp_gradient_context_root_handler;
95 }
97 static void sp_gradient_context_init(SPGradientContext *gr_context)
98 {
99 SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
101 gr_context->cursor_addnode = false;
102 event_context->cursor_shape = cursor_gradient_xpm;
103 event_context->hot_x = 4;
104 event_context->hot_y = 4;
105 event_context->xp = 0;
106 event_context->yp = 0;
107 event_context->tolerance = 6;
108 event_context->within_tolerance = false;
109 event_context->item_to_select = NULL;
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 guint n_obj = g_slist_length((GSList *) selection->itemList());
152 if (!drag->isNonEmpty() || selection->isEmpty())
153 return;
154 guint n_tot = drag->numDraggers();
155 guint n_sel = drag->numSelected();
157 if (n_sel == 1) {
158 if (drag->singleSelectedDraggerNumDraggables() == 1) {
159 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
160 _("%s selected out of %d gradient handles on %d selected object(s)"), gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()], n_tot, n_obj);
161 } else {
162 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
163 _("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);
164 }
165 } else if (n_sel > 1) {
166 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
167 _("<b>%d</b> gradient handles selected out of %d on %d selected object(s)"), n_sel, n_tot, n_obj);
168 } else if (n_sel == 0) {
169 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
170 _("<b>No</b> gradient handles selected out of %d on %d selected object(s)"), n_tot, n_obj);
171 }
172 }
174 static void
175 gradient_subselection_changed (gpointer, gpointer data)
176 {
177 gradient_selection_changed (NULL, data);
178 }
181 static void sp_gradient_context_setup(SPEventContext *ec)
182 {
183 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
185 if (((SPEventContextClass *) parent_class)->setup) {
186 ((SPEventContextClass *) parent_class)->setup(ec);
187 }
189 if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
190 ec->enableSelectionCue();
191 }
193 ec->enableGrDrag();
194 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
196 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
198 rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
199 rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
200 gradient_selection_changed(selection, rc);
201 }
203 void
204 sp_gradient_context_select_next (SPEventContext *event_context)
205 {
206 GrDrag *drag = event_context->_grdrag;
207 g_assert (drag);
209 GrDragger *d = drag->select_next();
211 event_context->desktop->scroll_to_point(&(d->point), 1.0);
212 }
214 void
215 sp_gradient_context_select_prev (SPEventContext *event_context)
216 {
217 GrDrag *drag = event_context->_grdrag;
218 g_assert (drag);
220 GrDragger *d = drag->select_prev();
222 event_context->desktop->scroll_to_point(&(d->point), 1.0);
223 }
225 // FIXME: make global function in libnr or somewhere.
226 static NR::Point
227 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end)
228 {
229 double length = NR::L2(end - begin);
230 NR::Point be = (end - begin) / length;
231 double r = NR::dot(p - begin, be);
233 if (r < 0.0) return begin;
234 if (r > length) return end;
236 return (begin + r * be);
237 }
239 static bool
240 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
241 {
242 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
244 //Translate mouse point into proper coord system
245 rc->mousepoint_doc = desktop->w2d(event_p);
247 SPCtrlLine* line = SP_CTRLLINE(item);
249 NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e);
250 double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
252 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
254 bool close = (dist_screen < tolerance);
256 return close;
257 }
259 static double
260 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
261 {
262 double length = NR::L2(end - begin);
263 NR::Point be = (end - begin) / length;
264 double r = NR::dot(p - begin, be);
266 if (r < 0.0) return 0.0;
267 if (r > length) return 1.0;
269 return (r / length);
270 }
272 std::vector<NR::Point>
273 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
274 {
275 std::vector<NR::Point> coords;
277 // for all selected draggers
278 for (GList *i = drag->selected; i != NULL; i = i->next) {
279 GrDragger *dragger = (GrDragger *) i->data;
280 // remember the coord of the dragger to reselect it later
281 coords.push_back(dragger->point);
282 // for all draggables of dragger
283 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
284 GrDraggable *d = (GrDraggable *) j->data;
286 // find the gradient
287 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
288 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
290 // these draggable types cannot have a next draggabe to insert a stop between them
291 if (d->point_type == POINT_LG_END ||
292 d->point_type == POINT_RG_FOCUS ||
293 d->point_type == POINT_RG_R1 ||
294 d->point_type == POINT_RG_R2) {
295 continue;
296 }
298 // from draggables to stops
299 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
300 SPStop *next_stop = sp_next_stop (this_stop);
301 SPStop *last_stop = sp_last_stop (vector);
303 gint fs = d->fill_or_stroke;
304 SPItem *item = d->item;
305 gint type = d->point_type;
306 gint p_i = d->point_i;
308 // if there's a next stop,
309 if (next_stop) {
310 GrDragger *dnext = NULL;
311 // find its dragger
312 // (complex because it may have different types, and because in radial,
313 // more than one dragger may correspond to a stop, so we must distinguish)
314 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
315 if (next_stop == last_stop)
316 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
317 else
318 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
319 } else { // radial
320 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
321 if (next_stop == last_stop)
322 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
323 else
324 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
325 }
326 if ((type == POINT_RG_MID2) ||
327 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
328 if (next_stop == last_stop)
329 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
330 else
331 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
332 }
333 }
335 // remember the coords of the future dragger to select it
336 coords.push_back(0.5*(dragger->point + dnext->point));
338 // if both adjacent draggers selected,
339 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
340 // do not insert a stop now, it will confuse the loop;
341 // just remember the stops
342 *these_stops = g_slist_prepend (*these_stops, this_stop);
343 *next_stops = g_slist_prepend (*next_stops, next_stop);
344 }
345 }
346 }
347 }
348 return coords;
349 }
351 static void
352 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
353 {
354 SPDocument *doc = NULL;
355 GrDrag *drag = rc->_grdrag;
357 GSList *these_stops = NULL;
358 GSList *next_stops = NULL;
360 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
362 if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
363 // if a single stop is selected, add between that stop and the next one
364 GrDragger *dragger = (GrDragger *) drag->selected->data;
365 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
366 GrDraggable *d = (GrDraggable *) j->data;
367 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
368 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
369 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
370 SPStop *next_stop = sp_next_stop (this_stop);
371 if (this_stop && next_stop) {
372 these_stops = g_slist_prepend (these_stops, this_stop);
373 next_stops = g_slist_prepend (next_stops, next_stop);
374 }
375 }
376 }
378 // now actually create the new stops
379 GSList *i = these_stops;
380 GSList *j = next_stops;
381 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
382 SPStop *this_stop = (SPStop *) i->data;
383 SPStop *next_stop = (SPStop *) j->data;
384 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
385 SPObject *parent = SP_OBJECT_PARENT(this_stop);
386 if (SP_IS_GRADIENT (parent)) {
387 doc = SP_OBJECT_DOCUMENT (parent);
388 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
389 sp_gradient_ensure_vector (SP_GRADIENT (parent));
390 }
391 }
393 if (g_slist_length(these_stops) > 0 && doc) {
394 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
395 drag->updateDraggers();
396 // so that it does not automatically update draggers in idle loop, as this would deselect
397 drag->local_change = true;
398 // select all the old selected and new created draggers
399 drag->selectByCoords(coords);
400 }
402 g_slist_free (these_stops);
403 g_slist_free (next_stops);
404 }
406 double sqr(double x) {return x*x;}
408 static void
409 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
410 {
411 SPDocument *doc = NULL;
412 GrDrag *drag = rc->_grdrag;
414 GSList *these_stops = NULL;
415 GSList *next_stops = NULL;
417 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
419 GSList *todel = NULL;
421 GSList *i = these_stops;
422 GSList *j = next_stops;
423 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
424 SPStop *stop0 = (SPStop *) i->data;
425 SPStop *stop1 = (SPStop *) j->data;
427 gint i1 = g_slist_index(these_stops, stop1);
428 if (i1 != -1) {
429 GSList *next_next = g_slist_nth (next_stops, i1);
430 if (next_next) {
431 SPStop *stop2 = (SPStop *) next_next->data;
433 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
434 continue;
436 guint32 const c0 = sp_stop_get_rgba32(stop0);
437 guint32 const c2 = sp_stop_get_rgba32(stop2);
438 guint32 const c1r = sp_stop_get_rgba32(stop1);
439 guint32 c1 = average_color (c0, c2,
440 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
442 double diff =
443 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
444 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
445 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
446 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
448 if (diff < tolerance)
449 todel = g_slist_prepend (todel, stop1);
450 }
451 }
452 }
454 for (i = todel; i != NULL; i = i->next) {
455 SPStop *stop = (SPStop*) i->data;
456 doc = SP_OBJECT_DOCUMENT (stop);
457 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
458 parent->removeChild(SP_OBJECT_REPR(stop));
459 }
461 if (g_slist_length(todel) > 0) {
462 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
463 drag->local_change = true;
464 drag->updateDraggers();
465 drag->selectByCoords(coords);
466 }
468 g_slist_free (todel);
469 g_slist_free (these_stops);
470 g_slist_free (next_stops);
471 }
475 static void
476 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 etime)
477 {
478 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
480 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
481 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
483 double tolerance = (double) ec->tolerance;
485 gfloat offset; // type of SPStop.offset = gfloat
486 SPGradient *gradient;
487 bool fill_or_stroke = true;
488 bool r1_knot = false;
490 bool addknot = false;
491 do {
492 gradient = sp_item_gradient (item, fill_or_stroke);
493 if (SP_IS_LINEARGRADIENT(gradient)) {
494 NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
495 NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
497 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
498 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
499 if ( dist_screen < tolerance ) {
500 // add the knot
501 offset = get_offset_between_points(nearest, begin, end);
502 addknot = true;
503 break; // break out of the while loop: add only one knot
504 }
505 } else if (SP_IS_RADIALGRADIENT(gradient)) {
506 NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
507 NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
508 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
509 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
510 if ( dist_screen < tolerance ) {
511 offset = get_offset_between_points(nearest, begin, end);
512 addknot = true;
513 r1_knot = true;
514 break; // break out of the while loop: add only one knot
515 }
517 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
518 nearest = snap_vector_midpoint (mouse_p, begin, end);
519 dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
520 if ( dist_screen < tolerance ) {
521 offset = get_offset_between_points(nearest, begin, end);
522 addknot = true;
523 r1_knot = false;
524 break; // break out of the while loop: add only one knot
525 }
526 }
527 fill_or_stroke = !fill_or_stroke;
528 } while (!fill_or_stroke && !addknot) ;
530 if (addknot) {
531 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
532 SPStop* prev_stop = sp_first_stop(vector);
533 SPStop* next_stop = sp_next_stop(prev_stop);
534 while ( (next_stop) && (next_stop->offset < offset) ) {
535 prev_stop = next_stop;
536 next_stop = sp_next_stop(next_stop);
537 }
538 if (!next_stop) {
539 // logical error: the endstop should have offset 1 and should always be more than this offset here
540 return;
541 }
544 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
546 sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT,
547 _("Add gradient stop"));
549 ec->_grdrag->updateDraggers();
550 sp_gradient_ensure_vector (gradient);
552 if (vector->has_stops) {
553 guint i = sp_number_of_stops_before_stop(vector, newstop);
555 gradient = sp_item_gradient (item, fill_or_stroke);
556 GrPointType pointtype = POINT_G_INVALID;
557 if (SP_IS_LINEARGRADIENT(gradient)) {
558 pointtype = POINT_LG_MID;
559 } else if (SP_IS_RADIALGRADIENT(gradient)) {
560 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
561 }
562 GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
563 if (dragger && (etime == 0) ) {
564 ec->_grdrag->setSelected (dragger);
565 } else {
566 ec->_grdrag->grabKnot (item,
567 pointtype,
568 i,
569 fill_or_stroke, 99999, 99999, etime);
570 }
571 ec->_grdrag->local_change = true;
573 }
574 }
575 }
578 static gint
579 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
580 {
581 static bool dragging;
583 SPDesktop *desktop = event_context->desktop;
584 Inkscape::Selection *selection = sp_desktop_selection (desktop);
586 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
588 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
589 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
591 GrDrag *drag = event_context->_grdrag;
592 g_assert (drag);
594 gint ret = FALSE;
595 switch (event->type) {
596 case GDK_2BUTTON_PRESS:
597 if ( event->button.button == 1 ) {
598 bool over_line = false;
599 SPCtrlLine *line = NULL;
600 if (drag->lines) {
601 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
602 line = (SPCtrlLine*) l->data;
603 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
604 }
605 }
606 if (over_line) {
607 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
608 } else {
609 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
610 SPItem *item = SP_ITEM(i->data);
611 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
612 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
614 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
616 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
617 sp_gradient_reset_to_userspace(priv, item);
618 }
620 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
621 _("Create default gradient"));
622 }
623 ret = TRUE;
624 }
625 break;
626 case GDK_BUTTON_PRESS:
627 if ( event->button.button == 1 && !event_context->space_panning ) {
628 NR::Point const button_w(event->button.x, event->button.y);
630 // save drag origin
631 event_context->xp = (gint) button_w[NR::X];
632 event_context->yp = (gint) button_w[NR::Y];
633 event_context->within_tolerance = true;
635 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
636 // enable Ctrl+doubleclick of exactly the selected item(s)
637 if (!(event->button.state & GDK_CONTROL_MASK))
638 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
640 dragging = true;
641 /* Position center */
642 NR::Point const button_dt = desktop->w2d(button_w);
643 /* Snap center to nearest magnetic point */
645 SnapManager const &m = desktop->namedview->snap_manager;
646 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
648 ret = TRUE;
649 }
650 break;
651 case GDK_MOTION_NOTIFY:
652 if ( dragging
653 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
654 {
655 if ( event_context->within_tolerance
656 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
657 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
658 break; // do not drag if we're within tolerance from origin
659 }
660 // Once the user has moved farther than tolerance from the original location
661 // (indicating they intend to draw, not click), then always process the
662 // motion notify coordinates as given (no snapping back to origin)
663 event_context->within_tolerance = false;
665 NR::Point const motion_w(event->motion.x,
666 event->motion.y);
667 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
669 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
671 ret = TRUE;
672 } else {
673 bool over_line = false;
674 if (drag->lines) {
675 for (GSList *l = drag->lines; l != NULL; l = l->next) {
676 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
677 }
678 }
680 if (rc->cursor_addnode && !over_line) {
681 event_context->cursor_shape = cursor_gradient_xpm;
682 sp_event_context_update_cursor(event_context);
683 rc->cursor_addnode = false;
684 } else if (!rc->cursor_addnode && over_line) {
685 event_context->cursor_shape = cursor_gradient_add_xpm;
686 sp_event_context_update_cursor(event_context);
687 rc->cursor_addnode = true;
688 }
689 }
690 break;
691 case GDK_BUTTON_RELEASE:
692 event_context->xp = event_context->yp = 0;
693 if ( event->button.button == 1 && !event_context->space_panning ) {
694 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
695 bool over_line = false;
696 SPCtrlLine *line = NULL;
697 if (drag->lines) {
698 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
699 line = (SPCtrlLine*) l->data;
700 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
701 }
702 }
703 if (over_line) {
704 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
705 ret = TRUE;
706 }
707 } else {
708 dragging = false;
710 // unless clicked with Ctrl (to enable Ctrl+doubleclick). (don't what this is for (johan))
711 if (event->button.state & GDK_CONTROL_MASK) {
712 ret = TRUE;
713 break;
714 }
716 if (!event_context->within_tolerance) {
717 // we've been dragging, do nothing (grdrag handles that)
718 } else if (event_context->item_to_select) {
719 // no dragging, select clicked item if any
720 if (event->button.state & GDK_SHIFT_MASK) {
721 selection->toggle(event_context->item_to_select);
722 } else {
723 selection->set(event_context->item_to_select);
724 }
725 } else {
726 // click in an empty space; do the same as Esc
727 if (drag->selected) {
728 drag->deselectAll();
729 } else {
730 selection->clear();
731 }
732 }
734 event_context->item_to_select = NULL;
735 ret = TRUE;
736 }
737 }
738 break;
739 case GDK_KEY_PRESS:
740 switch (get_group0_keyval (&event->key)) {
741 case GDK_Alt_L:
742 case GDK_Alt_R:
743 case GDK_Control_L:
744 case GDK_Control_R:
745 case GDK_Shift_L:
746 case GDK_Shift_R:
747 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
748 case GDK_Meta_R:
749 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
750 _("<b>Ctrl</b>: snap gradient angle"),
751 _("<b>Shift</b>: draw gradient around the starting point"),
752 NULL);
753 break;
755 case GDK_x:
756 case GDK_X:
757 if (MOD__ALT_ONLY) {
758 desktop->setToolboxFocusTo ("altx-grad");
759 ret = TRUE;
760 }
761 break;
763 case GDK_A:
764 case GDK_a:
765 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
766 drag->selectAll();
767 ret = TRUE;
768 }
769 break;
771 case GDK_L:
772 case GDK_l:
773 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
774 sp_gradient_simplify(rc, 1e-4);
775 ret = TRUE;
776 }
777 break;
779 case GDK_Escape:
780 if (drag->selected) {
781 drag->deselectAll();
782 } else {
783 selection->clear();
784 }
785 ret = TRUE;
786 //TODO: make dragging escapable by Esc
787 break;
789 case GDK_Left: // move handle left
790 case GDK_KP_Left:
791 case GDK_KP_4:
792 if (!MOD__CTRL) { // not ctrl
793 if (MOD__ALT) { // alt
794 if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
795 else drag->selected_move_screen(-1, 0); // no shift
796 }
797 else { // no alt
798 if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
799 else drag->selected_move(-nudge, 0); // no shift
800 }
801 ret = TRUE;
802 }
803 break;
804 case GDK_Up: // move handle up
805 case GDK_KP_Up:
806 case GDK_KP_8:
807 if (!MOD__CTRL) { // not ctrl
808 if (MOD__ALT) { // alt
809 if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
810 else drag->selected_move_screen(0, 1); // no shift
811 }
812 else { // no alt
813 if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
814 else drag->selected_move(0, nudge); // no shift
815 }
816 ret = TRUE;
817 }
818 break;
819 case GDK_Right: // move handle right
820 case GDK_KP_Right:
821 case GDK_KP_6:
822 if (!MOD__CTRL) { // not ctrl
823 if (MOD__ALT) { // alt
824 if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
825 else drag->selected_move_screen(1, 0); // no shift
826 }
827 else { // no alt
828 if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
829 else drag->selected_move(nudge, 0); // no shift
830 }
831 ret = TRUE;
832 }
833 break;
834 case GDK_Down: // move handle down
835 case GDK_KP_Down:
836 case GDK_KP_2:
837 if (!MOD__CTRL) { // not ctrl
838 if (MOD__ALT) { // alt
839 if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
840 else drag->selected_move_screen(0, -1); // no shift
841 }
842 else { // no alt
843 if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
844 else drag->selected_move(0, -nudge); // no shift
845 }
846 ret = TRUE;
847 }
848 break;
849 case GDK_r:
850 case GDK_R:
851 if (MOD__SHIFT_ONLY) {
852 // First try selected dragger
853 if (drag && drag->selected) {
854 drag->selected_reverse_vector();
855 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
856 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
857 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
858 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
859 }
860 }
861 // we did an undoable action
862 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
863 _("Invert gradient"));
864 ret = TRUE;
865 }
866 break;
868 case GDK_Insert:
869 case GDK_KP_Insert:
870 // with any modifiers:
871 sp_gradient_context_add_stops_between_selected_stops (rc);
872 ret = TRUE;
873 break;
875 case GDK_Delete:
876 case GDK_KP_Delete:
877 case GDK_BackSpace:
878 if ( drag->selected ) {
879 drag->deleteSelected(MOD__CTRL_ONLY);
880 ret = TRUE;
881 }
882 break;
883 default:
884 break;
885 }
886 break;
887 case GDK_KEY_RELEASE:
888 switch (get_group0_keyval (&event->key)) {
889 case GDK_Alt_L:
890 case GDK_Alt_R:
891 case GDK_Control_L:
892 case GDK_Control_R:
893 case GDK_Shift_L:
894 case GDK_Shift_R:
895 case GDK_Meta_L: // Meta is when you press Shift+Alt
896 case GDK_Meta_R:
897 event_context->defaultMessageContext()->clear();
898 break;
899 default:
900 break;
901 }
902 break;
903 default:
904 break;
905 }
907 if (!ret) {
908 if (((SPEventContextClass *) parent_class)->root_handler) {
909 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
910 }
911 }
913 return ret;
914 }
916 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
917 {
918 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
919 Inkscape::Selection *selection = sp_desktop_selection(desktop);
920 SPDocument *document = sp_desktop_document(desktop);
921 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
923 if (!selection->isEmpty()) {
924 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
925 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
927 SPGradient *vector;
928 if (ec->item_to_select) {
929 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
930 } else {
931 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
932 }
934 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
935 SPCSSAttr *css = sp_repr_css_attr_new();
936 sp_repr_css_set_property(css, "fill-opacity", "1.0");
938 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
940 //FIXME: see above
941 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
943 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
945 if (type == SP_GRADIENT_TYPE_LINEAR) {
946 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
947 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
948 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
949 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
950 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
951 }
952 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
953 }
954 if (ec->_grdrag) {
955 ec->_grdrag->updateDraggers();
956 // prevent regenerating draggers by selection modified signal, which sometimes
957 // comes too late and thus destroys the knot which we will now grab:
958 ec->_grdrag->local_change = true;
959 // give the grab out-of-bounds values of xp/yp because we're already dragging
960 // and therefore are already out of tolerance
961 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
962 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
963 -1, // ignore number (though it is always 1)
964 fill_or_stroke, 99999, 99999, etime);
965 }
966 // We did an undoable action, but sp_document_done will be called by the knot when released
968 // status text; we do not track coords because this branch is run once, not all the time
969 // during drag
970 int n_objects = g_slist_length((GSList *) selection->itemList());
971 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
972 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
973 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
974 n_objects);
975 } else {
976 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
977 }
978 }
981 /*
982 Local Variables:
983 mode:c++
984 c-file-style:"stroustrup"
985 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
986 indent-tabs-mode:nil
987 fill-column:99
988 End:
989 */
990 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :