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 "gradient-context.h"
33 #include "gradient-chemistry.h"
34 #include <glibmm/i18n.h>
35 #include "prefs-utils.h"
36 #include "gradient-drag.h"
37 #include "gradient-chemistry.h"
38 #include "xml/repr.h"
39 #include "sp-item.h"
40 #include "display/sp-ctrlline.h"
41 #include "sp-linear-gradient.h"
42 #include "sp-radial-gradient.h"
43 #include "sp-stop.h"
44 #include "svg/css-ostringstream.h"
45 #include "svg/svg-color.h"
46 #include "snap.h"
47 #include "sp-namedview.h"
48 #include "rubberband.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 //The use of ngettext in the following code is intentional even if the English singular form would never be used
158 if (n_sel == 1) {
159 if (drag->singleSelectedDraggerNumDraggables() == 1) {
160 gchar * message = g_strconcat(
161 //TRANSLATORS: %s will be substituted with the point name (see previous messages); This is part of a compound message
162 _("%s selected"),
163 //TRANSLATORS: Mind the space in front. This is part of a compound message
164 ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
165 ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
166 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
167 message,_(gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()]), n_tot, n_obj);
168 } else {
169 gchar * message = g_strconcat(
170 //TRANSLATORS: This is a part of a compound message (out of two more indicating: grandint handle count & object count)
171 ngettext("One handle merging %d stop (drag with <b>Shift</b> to separate) selected",
172 "One handle merging %d stops (drag with <b>Shift</b> to separate) selected",drag->singleSelectedDraggerNumDraggables()),
173 ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
174 ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
175 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message,drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
176 }
177 } else if (n_sel > 1) {
178 //TRANSLATORS: The plural refers to number of selected gradient handles. This is part of a compound message (part two indicates selected object count)
179 gchar * message = g_strconcat(ngettext("<b>%d</b> gradient handle selected out of %d","<b>%d</b> gradient handles selected out of %d",n_sel),
180 //TRANSLATORS: Mind the space in front. (Refers to gradient handles selected). This is part of a compound message
181 ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
182 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message, n_sel, n_tot, n_obj);
183 } else if (n_sel == 0) {
184 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
185 //TRANSLATORS: The plural refers to number of selected objects
186 ngettext("<b>No</b> gradient handles selected out of %d on %d selected object",
187 "<b>No</b> gradient handles selected out of %d on %d selected objects",n_obj), n_tot, n_obj);
188 }
189 }
191 static void
192 gradient_subselection_changed (gpointer, gpointer data)
193 {
194 gradient_selection_changed (NULL, data);
195 }
198 static void sp_gradient_context_setup(SPEventContext *ec)
199 {
200 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
202 if (((SPEventContextClass *) parent_class)->setup) {
203 ((SPEventContextClass *) parent_class)->setup(ec);
204 }
206 if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
207 ec->enableSelectionCue();
208 }
210 ec->enableGrDrag();
211 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
213 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
215 rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
216 rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
217 gradient_selection_changed(selection, rc);
218 }
220 void
221 sp_gradient_context_select_next (SPEventContext *event_context)
222 {
223 GrDrag *drag = event_context->_grdrag;
224 g_assert (drag);
226 GrDragger *d = drag->select_next();
228 event_context->desktop->scroll_to_point(&(d->point), 1.0);
229 }
231 void
232 sp_gradient_context_select_prev (SPEventContext *event_context)
233 {
234 GrDrag *drag = event_context->_grdrag;
235 g_assert (drag);
237 GrDragger *d = drag->select_prev();
239 event_context->desktop->scroll_to_point(&(d->point), 1.0);
240 }
242 static bool
243 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
244 {
245 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
247 //Translate mouse point into proper coord system
248 rc->mousepoint_doc = desktop->w2d(event_p);
250 SPCtrlLine* line = SP_CTRLLINE(item);
252 NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e, 0);
253 double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
255 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
257 bool close = (dist_screen < tolerance);
259 return close;
260 }
262 std::vector<NR::Point>
263 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
264 {
265 std::vector<NR::Point> coords;
267 // for all selected draggers
268 for (GList *i = drag->selected; i != NULL; i = i->next) {
269 GrDragger *dragger = (GrDragger *) i->data;
270 // remember the coord of the dragger to reselect it later
271 coords.push_back(dragger->point);
272 // for all draggables of dragger
273 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
274 GrDraggable *d = (GrDraggable *) j->data;
276 // find the gradient
277 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
278 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
280 // these draggable types cannot have a next draggabe to insert a stop between them
281 if (d->point_type == POINT_LG_END ||
282 d->point_type == POINT_RG_FOCUS ||
283 d->point_type == POINT_RG_R1 ||
284 d->point_type == POINT_RG_R2) {
285 continue;
286 }
288 // from draggables to stops
289 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
290 SPStop *next_stop = sp_next_stop (this_stop);
291 SPStop *last_stop = sp_last_stop (vector);
293 gint fs = d->fill_or_stroke;
294 SPItem *item = d->item;
295 gint type = d->point_type;
296 gint p_i = d->point_i;
298 // if there's a next stop,
299 if (next_stop) {
300 GrDragger *dnext = NULL;
301 // find its dragger
302 // (complex because it may have different types, and because in radial,
303 // more than one dragger may correspond to a stop, so we must distinguish)
304 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
305 if (next_stop == last_stop)
306 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
307 else
308 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
309 } else { // radial
310 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
311 if (next_stop == last_stop)
312 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
313 else
314 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
315 }
316 if ((type == POINT_RG_MID2) ||
317 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
318 if (next_stop == last_stop)
319 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
320 else
321 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
322 }
323 }
325 // if both adjacent draggers selected,
326 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
328 // remember the coords of the future dragger to select it
329 coords.push_back(0.5*(dragger->point + dnext->point));
331 // do not insert a stop now, it will confuse the loop;
332 // just remember the stops
333 *these_stops = g_slist_prepend (*these_stops, this_stop);
334 *next_stops = g_slist_prepend (*next_stops, next_stop);
335 }
336 }
337 }
338 }
339 return coords;
340 }
342 static void
343 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
344 {
345 SPDocument *doc = NULL;
346 GrDrag *drag = rc->_grdrag;
348 GSList *these_stops = NULL;
349 GSList *next_stops = NULL;
351 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
353 if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
354 // if a single stop is selected, add between that stop and the next one
355 GrDragger *dragger = (GrDragger *) drag->selected->data;
356 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
357 GrDraggable *d = (GrDraggable *) j->data;
358 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
359 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
360 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
361 SPStop *next_stop = sp_next_stop (this_stop);
362 if (this_stop && next_stop) {
363 these_stops = g_slist_prepend (these_stops, this_stop);
364 next_stops = g_slist_prepend (next_stops, next_stop);
365 }
366 }
367 }
369 // now actually create the new stops
370 GSList *i = these_stops;
371 GSList *j = next_stops;
372 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
373 SPStop *this_stop = (SPStop *) i->data;
374 SPStop *next_stop = (SPStop *) j->data;
375 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
376 SPObject *parent = SP_OBJECT_PARENT(this_stop);
377 if (SP_IS_GRADIENT (parent)) {
378 doc = SP_OBJECT_DOCUMENT (parent);
379 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
380 sp_gradient_ensure_vector (SP_GRADIENT (parent));
381 }
382 }
384 if (g_slist_length(these_stops) > 0 && doc) {
385 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
386 drag->updateDraggers();
387 // so that it does not automatically update draggers in idle loop, as this would deselect
388 drag->local_change = true;
389 // select all the old selected and new created draggers
390 drag->selectByCoords(coords);
391 }
393 g_slist_free (these_stops);
394 g_slist_free (next_stops);
395 }
397 double sqr(double x) {return x*x;}
399 static void
400 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
401 {
402 SPDocument *doc = NULL;
403 GrDrag *drag = rc->_grdrag;
405 GSList *these_stops = NULL;
406 GSList *next_stops = NULL;
408 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
410 GSList *todel = NULL;
412 GSList *i = these_stops;
413 GSList *j = next_stops;
414 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
415 SPStop *stop0 = (SPStop *) i->data;
416 SPStop *stop1 = (SPStop *) j->data;
418 gint i1 = g_slist_index(these_stops, stop1);
419 if (i1 != -1) {
420 GSList *next_next = g_slist_nth (next_stops, i1);
421 if (next_next) {
422 SPStop *stop2 = (SPStop *) next_next->data;
424 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
425 continue;
427 guint32 const c0 = sp_stop_get_rgba32(stop0);
428 guint32 const c2 = sp_stop_get_rgba32(stop2);
429 guint32 const c1r = sp_stop_get_rgba32(stop1);
430 guint32 c1 = average_color (c0, c2,
431 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
433 double diff =
434 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
435 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
436 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
437 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
439 if (diff < tolerance)
440 todel = g_slist_prepend (todel, stop1);
441 }
442 }
443 }
445 for (i = todel; i != NULL; i = i->next) {
446 SPStop *stop = (SPStop*) i->data;
447 doc = SP_OBJECT_DOCUMENT (stop);
448 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
449 parent->removeChild(SP_OBJECT_REPR(stop));
450 }
452 if (g_slist_length(todel) > 0) {
453 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
454 drag->local_change = true;
455 drag->updateDraggers();
456 drag->selectByCoords(coords);
457 }
459 g_slist_free (todel);
460 g_slist_free (these_stops);
461 g_slist_free (next_stops);
462 }
465 static void
466 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 /*etime*/)
467 {
468 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
470 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
471 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
473 double tolerance = (double) ec->tolerance;
475 ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
477 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
478 _("Add gradient stop"));
480 ec->get_drag()->updateDraggers();
481 }
484 static gint
485 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
486 {
487 static bool dragging;
489 SPDesktop *desktop = event_context->desktop;
490 Inkscape::Selection *selection = sp_desktop_selection (desktop);
492 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
494 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
495 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
497 GrDrag *drag = event_context->_grdrag;
498 g_assert (drag);
500 gint ret = FALSE;
501 switch (event->type) {
502 case GDK_2BUTTON_PRESS:
503 if ( event->button.button == 1 ) {
504 bool over_line = false;
505 SPCtrlLine *line = NULL;
506 if (drag->lines) {
507 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
508 line = (SPCtrlLine*) l->data;
509 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
510 }
511 }
512 if (over_line) {
513 // we take the first item in selection, because with doubleclick, the first click
514 // always resets selection to the single object under cursor
515 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
516 } else {
517 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
518 SPItem *item = SP_ITEM(i->data);
519 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
520 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
522 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
524 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
525 sp_gradient_reset_to_userspace(priv, item);
526 }
528 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
529 _("Create default gradient"));
530 }
531 ret = TRUE;
532 }
533 break;
534 case GDK_BUTTON_PRESS:
535 if ( event->button.button == 1 && !event_context->space_panning ) {
536 NR::Point const button_w(event->button.x, event->button.y);
538 // save drag origin
539 event_context->xp = (gint) button_w[NR::X];
540 event_context->yp = (gint) button_w[NR::Y];
541 event_context->within_tolerance = true;
543 dragging = true;
545 NR::Point const button_dt = desktop->w2d(button_w);
546 if (event->button.state & GDK_SHIFT_MASK) {
547 Inkscape::Rubberband::get()->start(desktop, button_dt);
548 } else {
549 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
550 // enable Ctrl+doubleclick of exactly the selected item(s)
551 if (!(event->button.state & GDK_CONTROL_MASK))
552 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
554 /* Snap center to nearest magnetic point */
555 SnapManager &m = desktop->namedview->snap_manager;
556 m.setup(desktop);
557 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt).getPoint();
558 }
560 ret = TRUE;
561 }
562 break;
563 case GDK_MOTION_NOTIFY:
564 if ( dragging
565 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
566 {
567 if ( event_context->within_tolerance
568 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
569 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
570 break; // do not drag if we're within tolerance from origin
571 }
572 // Once the user has moved farther than tolerance from the original location
573 // (indicating they intend to draw, not click), then always process the
574 // motion notify coordinates as given (no snapping back to origin)
575 event_context->within_tolerance = false;
577 NR::Point const motion_w(event->motion.x,
578 event->motion.y);
579 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
581 if (Inkscape::Rubberband::get()->is_started()) {
582 Inkscape::Rubberband::get()->move(motion_dt);
583 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
584 } else {
585 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
586 }
587 gobble_motion_events(GDK_BUTTON1_MASK);
589 ret = TRUE;
590 } else {
591 bool over_line = false;
592 if (drag->lines) {
593 for (GSList *l = drag->lines; l != NULL; l = l->next) {
594 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
595 }
596 }
598 if (rc->cursor_addnode && !over_line) {
599 event_context->cursor_shape = cursor_gradient_xpm;
600 sp_event_context_update_cursor(event_context);
601 rc->cursor_addnode = false;
602 } else if (!rc->cursor_addnode && over_line) {
603 event_context->cursor_shape = cursor_gradient_add_xpm;
604 sp_event_context_update_cursor(event_context);
605 rc->cursor_addnode = true;
606 }
607 }
608 break;
609 case GDK_BUTTON_RELEASE:
610 event_context->xp = event_context->yp = 0;
611 if ( event->button.button == 1 && !event_context->space_panning ) {
612 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
613 bool over_line = false;
614 SPCtrlLine *line = NULL;
615 if (drag->lines) {
616 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
617 line = (SPCtrlLine*) l->data;
618 over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
619 if (over_line)
620 break;
621 }
622 }
623 if (over_line && line) {
624 sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
625 ret = TRUE;
626 }
627 } else {
628 dragging = false;
630 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
631 if (event->button.state & GDK_CONTROL_MASK) {
632 ret = TRUE;
633 break;
634 }
636 if (!event_context->within_tolerance) {
637 // we've been dragging, either do nothing (grdrag handles that),
638 // or rubberband-select if we have rubberband
639 Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
640 if (r->is_started() && !event_context->within_tolerance) {
641 // this was a rubberband drag
642 if (r->getMode() == RUBBERBAND_MODE_RECT) {
643 NR::Maybe<NR::Rect> const b = r->getRectangle();
644 drag->selectRect(*b);
645 }
646 }
648 } else if (event_context->item_to_select) {
649 // no dragging, select clicked item if any
650 if (event->button.state & GDK_SHIFT_MASK) {
651 selection->toggle(event_context->item_to_select);
652 } else {
653 selection->set(event_context->item_to_select);
654 }
655 } else {
656 // click in an empty space; do the same as Esc
657 if (drag->selected) {
658 drag->deselectAll();
659 } else {
660 selection->clear();
661 }
662 }
664 event_context->item_to_select = NULL;
665 ret = TRUE;
666 }
667 Inkscape::Rubberband::get()->stop();
668 }
669 break;
670 case GDK_KEY_PRESS:
671 switch (get_group0_keyval (&event->key)) {
672 case GDK_Alt_L:
673 case GDK_Alt_R:
674 case GDK_Control_L:
675 case GDK_Control_R:
676 case GDK_Shift_L:
677 case GDK_Shift_R:
678 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
679 case GDK_Meta_R:
680 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
681 _("<b>Ctrl</b>: snap gradient angle"),
682 _("<b>Shift</b>: draw gradient around the starting point"),
683 NULL);
684 break;
686 case GDK_x:
687 case GDK_X:
688 if (MOD__ALT_ONLY) {
689 desktop->setToolboxFocusTo ("altx-grad");
690 ret = TRUE;
691 }
692 break;
694 case GDK_A:
695 case GDK_a:
696 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
697 drag->selectAll();
698 ret = TRUE;
699 }
700 break;
702 case GDK_L:
703 case GDK_l:
704 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
705 sp_gradient_simplify(rc, 1e-4);
706 ret = TRUE;
707 }
708 break;
710 case GDK_Escape:
711 if (drag->selected) {
712 drag->deselectAll();
713 } else {
714 selection->clear();
715 }
716 ret = TRUE;
717 //TODO: make dragging escapable by Esc
718 break;
720 case GDK_Left: // move handle left
721 case GDK_KP_Left:
722 case GDK_KP_4:
723 if (!MOD__CTRL) { // not ctrl
724 gint mul = 1 + gobble_key_events(
725 get_group0_keyval(&event->key), 0); // with any mask
726 if (MOD__ALT) { // alt
727 if (MOD__SHIFT) drag->selected_move_screen(mul*-10, 0); // shift
728 else drag->selected_move_screen(mul*-1, 0); // no shift
729 }
730 else { // no alt
731 if (MOD__SHIFT) drag->selected_move(mul*-10*nudge, 0); // shift
732 else drag->selected_move(mul*-nudge, 0); // no shift
733 }
734 ret = TRUE;
735 }
736 break;
737 case GDK_Up: // move handle up
738 case GDK_KP_Up:
739 case GDK_KP_8:
740 if (!MOD__CTRL) { // not ctrl
741 gint mul = 1 + gobble_key_events(
742 get_group0_keyval(&event->key), 0); // with any mask
743 if (MOD__ALT) { // alt
744 if (MOD__SHIFT) drag->selected_move_screen(0, mul*10); // shift
745 else drag->selected_move_screen(0, mul*1); // no shift
746 }
747 else { // no alt
748 if (MOD__SHIFT) drag->selected_move(0, mul*10*nudge); // shift
749 else drag->selected_move(0, mul*nudge); // no shift
750 }
751 ret = TRUE;
752 }
753 break;
754 case GDK_Right: // move handle right
755 case GDK_KP_Right:
756 case GDK_KP_6:
757 if (!MOD__CTRL) { // not ctrl
758 gint mul = 1 + gobble_key_events(
759 get_group0_keyval(&event->key), 0); // with any mask
760 if (MOD__ALT) { // alt
761 if (MOD__SHIFT) drag->selected_move_screen(mul*10, 0); // shift
762 else drag->selected_move_screen(mul*1, 0); // no shift
763 }
764 else { // no alt
765 if (MOD__SHIFT) drag->selected_move(mul*10*nudge, 0); // shift
766 else drag->selected_move(mul*nudge, 0); // no shift
767 }
768 ret = TRUE;
769 }
770 break;
771 case GDK_Down: // move handle down
772 case GDK_KP_Down:
773 case GDK_KP_2:
774 if (!MOD__CTRL) { // not ctrl
775 gint mul = 1 + gobble_key_events(
776 get_group0_keyval(&event->key), 0); // with any mask
777 if (MOD__ALT) { // alt
778 if (MOD__SHIFT) drag->selected_move_screen(0, mul*-10); // shift
779 else drag->selected_move_screen(0, mul*-1); // no shift
780 }
781 else { // no alt
782 if (MOD__SHIFT) drag->selected_move(0, mul*-10*nudge); // shift
783 else drag->selected_move(0, mul*-nudge); // no shift
784 }
785 ret = TRUE;
786 }
787 break;
788 case GDK_r:
789 case GDK_R:
790 if (MOD__SHIFT_ONLY) {
791 // First try selected dragger
792 if (drag && drag->selected) {
793 drag->selected_reverse_vector();
794 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
795 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
796 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
797 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
798 }
799 }
800 // we did an undoable action
801 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
802 _("Invert gradient"));
803 ret = TRUE;
804 }
805 break;
807 case GDK_Insert:
808 case GDK_KP_Insert:
809 // with any modifiers:
810 sp_gradient_context_add_stops_between_selected_stops (rc);
811 ret = TRUE;
812 break;
814 case GDK_Delete:
815 case GDK_KP_Delete:
816 case GDK_BackSpace:
817 if ( drag->selected ) {
818 drag->deleteSelected(MOD__CTRL_ONLY);
819 ret = TRUE;
820 }
821 break;
822 default:
823 break;
824 }
825 break;
826 case GDK_KEY_RELEASE:
827 switch (get_group0_keyval (&event->key)) {
828 case GDK_Alt_L:
829 case GDK_Alt_R:
830 case GDK_Control_L:
831 case GDK_Control_R:
832 case GDK_Shift_L:
833 case GDK_Shift_R:
834 case GDK_Meta_L: // Meta is when you press Shift+Alt
835 case GDK_Meta_R:
836 event_context->defaultMessageContext()->clear();
837 break;
838 default:
839 break;
840 }
841 break;
842 default:
843 break;
844 }
846 if (!ret) {
847 if (((SPEventContextClass *) parent_class)->root_handler) {
848 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
849 }
850 }
852 return ret;
853 }
855 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint /*state*/, guint32 etime)
856 {
857 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
858 Inkscape::Selection *selection = sp_desktop_selection(desktop);
859 SPDocument *document = sp_desktop_document(desktop);
860 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
862 if (!selection->isEmpty()) {
863 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
864 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
866 SPGradient *vector;
867 if (ec->item_to_select) {
868 // pick color from the object where drag started
869 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
870 } else {
871 // Starting from empty space:
872 // Sort items so that the topmost comes last
873 GSList *items = g_slist_copy ((GSList *) selection->itemList());
874 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
875 // take topmost
876 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
877 g_slist_free (items);
878 }
880 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
881 SPCSSAttr *css = sp_repr_css_attr_new();
882 sp_repr_css_set_property(css, "fill-opacity", "1.0");
884 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
886 //FIXME: see above
887 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
889 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
891 if (type == SP_GRADIENT_TYPE_LINEAR) {
892 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
893 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
894 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
895 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
896 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
897 }
898 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
899 }
900 if (ec->_grdrag) {
901 ec->_grdrag->updateDraggers();
902 // prevent regenerating draggers by selection modified signal, which sometimes
903 // comes too late and thus destroys the knot which we will now grab:
904 ec->_grdrag->local_change = true;
905 // give the grab out-of-bounds values of xp/yp because we're already dragging
906 // and therefore are already out of tolerance
907 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
908 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
909 -1, // ignore number (though it is always 1)
910 fill_or_stroke, 99999, 99999, etime);
911 }
912 // We did an undoable action, but sp_document_done will be called by the knot when released
914 // status text; we do not track coords because this branch is run once, not all the time
915 // during drag
916 int n_objects = g_slist_length((GSList *) selection->itemList());
917 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
918 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
919 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
920 n_objects);
921 } else {
922 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
923 }
924 }
927 /*
928 Local Variables:
929 mode:c++
930 c-file-style:"stroustrup"
931 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
932 indent-tabs-mode:nil
933 fill-column:99
934 End:
935 */
936 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :