93cf792141544419e5cffc128b3669b75e65c4eb
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 drag->select_next();
210 }
212 void
213 sp_gradient_context_select_prev (SPEventContext *event_context)
214 {
215 GrDrag *drag = event_context->_grdrag;
216 g_assert (drag);
218 drag->select_prev();
219 }
221 // FIXME: make global function in libnr or somewhere.
222 static NR::Point
223 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end)
224 {
225 double length = NR::L2(end - begin);
226 NR::Point be = (end - begin) / length;
227 double r = NR::dot(p - begin, be);
229 if (r < 0.0) return begin;
230 if (r > length) return end;
232 return (begin + r * be);
233 }
235 static bool
236 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
237 {
238 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
240 //Translate mouse point into proper coord system
241 rc->mousepoint_doc = desktop->w2d(event_p);
243 SPCtrlLine* line = SP_CTRLLINE(item);
245 NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e);
246 double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
248 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
250 bool close = (dist_screen < tolerance);
252 return close;
253 }
255 static double
256 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
257 {
258 double length = NR::L2(end - begin);
259 NR::Point be = (end - begin) / length;
260 double r = NR::dot(p - begin, be);
262 if (r < 0.0) return 0.0;
263 if (r > length) return 1.0;
265 return (r / length);
266 }
268 std::vector<NR::Point>
269 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
270 {
271 std::vector<NR::Point> coords;
273 // for all selected draggers
274 for (GList *i = drag->selected; i != NULL; i = i->next) {
275 GrDragger *dragger = (GrDragger *) i->data;
276 // remember the coord of the dragger to reselect it later
277 coords.push_back(dragger->point);
278 // for all draggables of dragger
279 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
280 GrDraggable *d = (GrDraggable *) j->data;
282 // find the gradient
283 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
284 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
286 // these draggable types cannot have a next draggabe to insert a stop between them
287 if (d->point_type == POINT_LG_END ||
288 d->point_type == POINT_RG_FOCUS ||
289 d->point_type == POINT_RG_R1 ||
290 d->point_type == POINT_RG_R2) {
291 continue;
292 }
294 // from draggables to stops
295 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
296 SPStop *next_stop = sp_next_stop (this_stop);
297 SPStop *last_stop = sp_last_stop (vector);
299 gint fs = d->fill_or_stroke;
300 SPItem *item = d->item;
301 gint type = d->point_type;
302 gint p_i = d->point_i;
304 // if there's a next stop,
305 if (next_stop) {
306 GrDragger *dnext = NULL;
307 // find its dragger
308 // (complex because it may have different types, and because in radial,
309 // more than one dragger may correspond to a stop, so we must distinguish)
310 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
311 if (next_stop == last_stop)
312 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
313 else
314 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
315 } else { // radial
316 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
317 if (next_stop == last_stop)
318 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
319 else
320 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
321 }
322 if ((type == POINT_RG_MID2) ||
323 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
324 if (next_stop == last_stop)
325 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
326 else
327 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
328 }
329 }
331 // remember the coords of the future dragger to select it
332 coords.push_back(0.5*(dragger->point + dnext->point));
334 // if both adjacent draggers selected,
335 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
336 // do not insert a stop now, it will confuse the loop;
337 // just remember the stops
338 *these_stops = g_slist_prepend (*these_stops, this_stop);
339 *next_stops = g_slist_prepend (*next_stops, next_stop);
340 }
341 }
342 }
343 }
344 return coords;
345 }
347 static void
348 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
349 {
350 SPDocument *doc = NULL;
351 GrDrag *drag = rc->_grdrag;
353 GSList *these_stops = NULL;
354 GSList *next_stops = NULL;
356 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
358 // now actually create the new stops
359 GSList *i = these_stops;
360 GSList *j = next_stops;
361 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
362 SPStop *this_stop = (SPStop *) i->data;
363 SPStop *next_stop = (SPStop *) j->data;
364 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
365 SPObject *parent = SP_OBJECT_PARENT(this_stop);
366 if (SP_IS_GRADIENT (parent)) {
367 doc = SP_OBJECT_DOCUMENT (parent);
368 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
369 sp_gradient_ensure_vector (SP_GRADIENT (parent));
370 }
371 }
373 if (g_slist_length(these_stops) > 0 && doc) {
374 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
375 drag->updateDraggers();
376 // so that it does not automatically update draggers in idle loop, as this would deselect
377 drag->local_change = true;
378 // select all the old selected and new created draggers
379 drag->selectByCoords(coords);
380 }
382 g_slist_free (these_stops);
383 g_slist_free (next_stops);
384 }
386 double sqr(double x) {return x*x;}
388 static void
389 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
390 {
391 SPDocument *doc = NULL;
392 GrDrag *drag = rc->_grdrag;
394 GSList *these_stops = NULL;
395 GSList *next_stops = NULL;
397 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
399 GSList *todel = NULL;
401 GSList *i = these_stops;
402 GSList *j = next_stops;
403 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
404 SPStop *stop0 = (SPStop *) i->data;
405 SPStop *stop1 = (SPStop *) j->data;
407 gint i1 = g_slist_index(these_stops, stop1);
408 if (i1 != -1) {
409 GSList *next_next = g_slist_nth (next_stops, i1);
410 if (next_next) {
411 SPStop *stop2 = (SPStop *) next_next->data;
413 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
414 continue;
416 guint32 const c0 = sp_stop_get_rgba32(stop0);
417 guint32 const c2 = sp_stop_get_rgba32(stop2);
418 guint32 const c1r = sp_stop_get_rgba32(stop1);
419 guint32 c1 = average_color (c0, c2,
420 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
422 double diff =
423 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
424 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
425 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
426 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
428 if (diff < tolerance)
429 todel = g_slist_prepend (todel, stop1);
430 }
431 }
432 }
434 for (i = todel; i != NULL; i = i->next) {
435 SPStop *stop = (SPStop*) i->data;
436 doc = SP_OBJECT_DOCUMENT (stop);
437 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
438 parent->removeChild(SP_OBJECT_REPR(stop));
439 }
441 if (g_slist_length(todel) > 0) {
442 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
443 drag->local_change = true;
444 drag->updateDraggers();
445 drag->selectByCoords(coords);
446 }
448 g_slist_free (todel);
449 g_slist_free (these_stops);
450 g_slist_free (next_stops);
451 }
455 static void
456 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 etime)
457 {
458 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
460 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
461 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
463 double tolerance = (double) ec->tolerance;
465 gfloat offset; // type of SPStop.offset = gfloat
466 SPGradient *gradient;
467 bool fill_or_stroke = true;
468 bool r1_knot = false;
470 bool addknot = false;
471 do {
472 gradient = sp_item_gradient (item, fill_or_stroke);
473 if (SP_IS_LINEARGRADIENT(gradient)) {
474 NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
475 NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
477 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
478 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
479 if ( dist_screen < tolerance ) {
480 // add the knot
481 offset = get_offset_between_points(nearest, begin, end);
482 addknot = true;
483 break; // break out of the while loop: add only one knot
484 }
485 } else if (SP_IS_RADIALGRADIENT(gradient)) {
486 NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
487 NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
488 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
489 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
490 if ( dist_screen < tolerance ) {
491 offset = get_offset_between_points(nearest, begin, end);
492 addknot = true;
493 r1_knot = true;
494 break; // break out of the while loop: add only one knot
495 }
497 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
498 nearest = snap_vector_midpoint (mouse_p, begin, end);
499 dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
500 if ( dist_screen < tolerance ) {
501 offset = get_offset_between_points(nearest, begin, end);
502 addknot = true;
503 r1_knot = false;
504 break; // break out of the while loop: add only one knot
505 }
506 }
507 fill_or_stroke = !fill_or_stroke;
508 } while (!fill_or_stroke && !addknot) ;
510 if (addknot) {
511 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
512 SPStop* prev_stop = sp_first_stop(vector);
513 SPStop* next_stop = sp_next_stop(prev_stop);
514 while ( (next_stop) && (next_stop->offset < offset) ) {
515 prev_stop = next_stop;
516 next_stop = sp_next_stop(next_stop);
517 }
518 if (!next_stop) {
519 // logical error: the endstop should have offset 1 and should always be more than this offset here
520 return;
521 }
524 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
526 sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT,
527 _("Add gradient stop"));
529 ec->_grdrag->updateDraggers();
530 sp_gradient_ensure_vector (gradient);
532 if (vector->has_stops) {
533 guint i = sp_number_of_stops_before_stop(vector, newstop);
535 gradient = sp_item_gradient (item, fill_or_stroke);
536 GrPointType pointtype = POINT_G_INVALID;
537 if (SP_IS_LINEARGRADIENT(gradient)) {
538 pointtype = POINT_LG_MID;
539 } else if (SP_IS_RADIALGRADIENT(gradient)) {
540 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
541 }
542 GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
543 if (dragger && (etime == 0) ) {
544 ec->_grdrag->setSelected (dragger);
545 } else {
546 ec->_grdrag->grabKnot (item,
547 pointtype,
548 i,
549 fill_or_stroke, 99999, 99999, etime);
550 }
551 ec->_grdrag->local_change = true;
553 }
554 }
555 }
558 static gint
559 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
560 {
561 static bool dragging;
563 SPDesktop *desktop = event_context->desktop;
564 Inkscape::Selection *selection = sp_desktop_selection (desktop);
566 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
568 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
569 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
571 GrDrag *drag = event_context->_grdrag;
572 g_assert (drag);
574 gint ret = FALSE;
575 switch (event->type) {
576 case GDK_2BUTTON_PRESS:
577 if ( event->button.button == 1 ) {
578 bool over_line = false;
579 SPCtrlLine *line = NULL;
580 if (drag->lines) {
581 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
582 line = (SPCtrlLine*) l->data;
583 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
584 }
585 }
586 if (over_line) {
587 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
588 } else {
589 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
590 SPItem *item = SP_ITEM(i->data);
591 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
592 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
594 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
596 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
597 sp_gradient_reset_to_userspace(priv, item);
598 }
600 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
601 _("Create default gradient"));
602 }
603 ret = TRUE;
604 }
605 break;
606 case GDK_BUTTON_PRESS:
607 if ( event->button.button == 1 && !event_context->space_panning ) {
608 NR::Point const button_w(event->button.x, event->button.y);
610 // save drag origin
611 event_context->xp = (gint) button_w[NR::X];
612 event_context->yp = (gint) button_w[NR::Y];
613 event_context->within_tolerance = true;
615 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
616 // enable Ctrl+doubleclick of exactly the selected item(s)
617 if (!(event->button.state & GDK_CONTROL_MASK))
618 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
620 dragging = true;
621 /* Position center */
622 NR::Point const button_dt = desktop->w2d(button_w);
623 /* Snap center to nearest magnetic point */
625 SnapManager const &m = desktop->namedview->snap_manager;
626 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
628 ret = TRUE;
629 }
630 break;
631 case GDK_MOTION_NOTIFY:
632 if ( dragging
633 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
634 {
635 if ( event_context->within_tolerance
636 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
637 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
638 break; // do not drag if we're within tolerance from origin
639 }
640 // Once the user has moved farther than tolerance from the original location
641 // (indicating they intend to draw, not click), then always process the
642 // motion notify coordinates as given (no snapping back to origin)
643 event_context->within_tolerance = false;
645 NR::Point const motion_w(event->motion.x,
646 event->motion.y);
647 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
649 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
651 ret = TRUE;
652 } else {
653 bool over_line = false;
654 if (drag->lines) {
655 for (GSList *l = drag->lines; l != NULL; l = l->next) {
656 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
657 }
658 }
660 if (rc->cursor_addnode && !over_line) {
661 event_context->cursor_shape = cursor_gradient_xpm;
662 sp_event_context_update_cursor(event_context);
663 rc->cursor_addnode = false;
664 } else if (!rc->cursor_addnode && over_line) {
665 event_context->cursor_shape = cursor_gradient_add_xpm;
666 sp_event_context_update_cursor(event_context);
667 rc->cursor_addnode = true;
668 }
669 }
670 break;
671 case GDK_BUTTON_RELEASE:
672 event_context->xp = event_context->yp = 0;
673 if ( event->button.button == 1 && !event_context->space_panning ) {
674 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
675 bool over_line = false;
676 SPCtrlLine *line = NULL;
677 if (drag->lines) {
678 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
679 line = (SPCtrlLine*) l->data;
680 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
681 }
682 }
683 if (over_line) {
684 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
685 ret = TRUE;
686 }
687 } else {
688 dragging = false;
690 // unless clicked with Ctrl (to enable Ctrl+doubleclick). (don't what this is for (johan))
691 if (event->button.state & GDK_CONTROL_MASK) {
692 ret = TRUE;
693 break;
694 }
696 if (!event_context->within_tolerance) {
697 // we've been dragging, do nothing (grdrag handles that)
698 } else if (event_context->item_to_select) {
699 // no dragging, select clicked item if any
700 if (event->button.state & GDK_SHIFT_MASK) {
701 selection->toggle(event_context->item_to_select);
702 } else {
703 selection->set(event_context->item_to_select);
704 }
705 } else {
706 // click in an empty space; do the same as Esc
707 if (drag->selected) {
708 drag->deselectAll();
709 } else {
710 selection->clear();
711 }
712 }
714 event_context->item_to_select = NULL;
715 ret = TRUE;
716 }
717 }
718 break;
719 case GDK_KEY_PRESS:
720 switch (get_group0_keyval (&event->key)) {
721 case GDK_Alt_L:
722 case GDK_Alt_R:
723 case GDK_Control_L:
724 case GDK_Control_R:
725 case GDK_Shift_L:
726 case GDK_Shift_R:
727 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
728 case GDK_Meta_R:
729 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
730 _("<b>Ctrl</b>: snap gradient angle"),
731 _("<b>Shift</b>: draw gradient around the starting point"),
732 NULL);
733 break;
735 case GDK_x:
736 case GDK_X:
737 if (MOD__ALT_ONLY) {
738 desktop->setToolboxFocusTo ("altx-grad");
739 ret = TRUE;
740 }
741 break;
743 case GDK_A:
744 case GDK_a:
745 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
746 drag->selectAll();
747 ret = TRUE;
748 }
749 break;
751 case GDK_L:
752 case GDK_l:
753 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
754 sp_gradient_simplify(rc, 1e-4);
755 ret = TRUE;
756 }
757 break;
759 case GDK_Escape:
760 if (drag->selected) {
761 drag->deselectAll();
762 } else {
763 selection->clear();
764 }
765 ret = TRUE;
766 //TODO: make dragging escapable by Esc
767 break;
769 case GDK_Left: // move handle left
770 case GDK_KP_Left:
771 case GDK_KP_4:
772 if (!MOD__CTRL) { // not ctrl
773 if (MOD__ALT) { // alt
774 if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
775 else drag->selected_move_screen(-1, 0); // no shift
776 }
777 else { // no alt
778 if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
779 else drag->selected_move(-nudge, 0); // no shift
780 }
781 ret = TRUE;
782 }
783 break;
784 case GDK_Up: // move handle up
785 case GDK_KP_Up:
786 case GDK_KP_8:
787 if (!MOD__CTRL) { // not ctrl
788 if (MOD__ALT) { // alt
789 if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
790 else drag->selected_move_screen(0, 1); // no shift
791 }
792 else { // no alt
793 if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
794 else drag->selected_move(0, nudge); // no shift
795 }
796 ret = TRUE;
797 }
798 break;
799 case GDK_Right: // move handle right
800 case GDK_KP_Right:
801 case GDK_KP_6:
802 if (!MOD__CTRL) { // not ctrl
803 if (MOD__ALT) { // alt
804 if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
805 else drag->selected_move_screen(1, 0); // no shift
806 }
807 else { // no alt
808 if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
809 else drag->selected_move(nudge, 0); // no shift
810 }
811 ret = TRUE;
812 }
813 break;
814 case GDK_Down: // move handle down
815 case GDK_KP_Down:
816 case GDK_KP_2:
817 if (!MOD__CTRL) { // not ctrl
818 if (MOD__ALT) { // alt
819 if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
820 else drag->selected_move_screen(0, -1); // no shift
821 }
822 else { // no alt
823 if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
824 else drag->selected_move(0, -nudge); // no shift
825 }
826 ret = TRUE;
827 }
828 break;
829 case GDK_r:
830 case GDK_R:
831 if (MOD__SHIFT_ONLY) {
832 // First try selected dragger
833 if (drag && drag->selected) {
834 drag->selected_reverse_vector();
835 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
836 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
837 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
838 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
839 }
840 }
841 // we did an undoable action
842 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
843 _("Invert gradient"));
844 ret = TRUE;
845 }
846 break;
848 case GDK_Insert:
849 case GDK_KP_Insert:
850 // with any modifiers:
851 sp_gradient_context_add_stops_between_selected_stops (rc);
852 ret = TRUE;
853 break;
855 case GDK_Delete:
856 case GDK_KP_Delete:
857 case GDK_BackSpace:
858 if ( drag->selected ) {
859 drag->deleteSelected(MOD__CTRL_ONLY);
860 ret = TRUE;
861 }
862 break;
863 default:
864 break;
865 }
866 break;
867 case GDK_KEY_RELEASE:
868 switch (get_group0_keyval (&event->key)) {
869 case GDK_Alt_L:
870 case GDK_Alt_R:
871 case GDK_Control_L:
872 case GDK_Control_R:
873 case GDK_Shift_L:
874 case GDK_Shift_R:
875 case GDK_Meta_L: // Meta is when you press Shift+Alt
876 case GDK_Meta_R:
877 event_context->defaultMessageContext()->clear();
878 break;
879 default:
880 break;
881 }
882 break;
883 default:
884 break;
885 }
887 if (!ret) {
888 if (((SPEventContextClass *) parent_class)->root_handler) {
889 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
890 }
891 }
893 return ret;
894 }
896 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
897 {
898 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
899 Inkscape::Selection *selection = sp_desktop_selection(desktop);
900 SPDocument *document = sp_desktop_document(desktop);
901 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
903 if (!selection->isEmpty()) {
904 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
905 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
907 SPGradient *vector;
908 if (ec->item_to_select) {
909 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
910 } else {
911 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
912 }
914 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
915 SPCSSAttr *css = sp_repr_css_attr_new();
916 sp_repr_css_set_property(css, "fill-opacity", "1.0");
918 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
920 //FIXME: see above
921 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
923 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
925 if (type == SP_GRADIENT_TYPE_LINEAR) {
926 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
927 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
928 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
929 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
930 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
931 }
932 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
933 }
934 if (ec->_grdrag) {
935 ec->_grdrag->updateDraggers();
936 // prevent regenerating draggers by selection modified signal, which sometimes
937 // comes too late and thus destroys the knot which we will now grab:
938 ec->_grdrag->local_change = true;
939 // give the grab out-of-bounds values of xp/yp because we're already dragging
940 // and therefore are already out of tolerance
941 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
942 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
943 -1, // ignore number (though it is always 1)
944 fill_or_stroke, 99999, 99999, etime);
945 }
946 // We did an undoable action, but sp_document_done will be called by the knot when released
948 // status text; we do not track coords because this branch is run once, not all the time
949 // during drag
950 int n_objects = g_slist_length((GSList *) selection->itemList());
951 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
952 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
953 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
954 n_objects);
955 } else {
956 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
957 }
958 }
961 /*
962 Local Variables:
963 mode:c++
964 c-file-style:"stroustrup"
965 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
966 indent-tabs-mode:nil
967 fill-column:99
968 End:
969 */
970 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :