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 const &m = desktop->namedview->snap_manager;
556 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
557 }
559 ret = TRUE;
560 }
561 break;
562 case GDK_MOTION_NOTIFY:
563 if ( dragging
564 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
565 {
566 if ( event_context->within_tolerance
567 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
568 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
569 break; // do not drag if we're within tolerance from origin
570 }
571 // Once the user has moved farther than tolerance from the original location
572 // (indicating they intend to draw, not click), then always process the
573 // motion notify coordinates as given (no snapping back to origin)
574 event_context->within_tolerance = false;
576 NR::Point const motion_w(event->motion.x,
577 event->motion.y);
578 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
580 if (Inkscape::Rubberband::get()->is_started()) {
581 Inkscape::Rubberband::get()->move(motion_dt);
582 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
583 } else {
584 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
585 }
586 gobble_motion_events(GDK_BUTTON1_MASK);
588 ret = TRUE;
589 } else {
590 bool over_line = false;
591 if (drag->lines) {
592 for (GSList *l = drag->lines; l != NULL; l = l->next) {
593 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
594 }
595 }
597 if (rc->cursor_addnode && !over_line) {
598 event_context->cursor_shape = cursor_gradient_xpm;
599 sp_event_context_update_cursor(event_context);
600 rc->cursor_addnode = false;
601 } else if (!rc->cursor_addnode && over_line) {
602 event_context->cursor_shape = cursor_gradient_add_xpm;
603 sp_event_context_update_cursor(event_context);
604 rc->cursor_addnode = true;
605 }
606 }
607 break;
608 case GDK_BUTTON_RELEASE:
609 event_context->xp = event_context->yp = 0;
610 if ( event->button.button == 1 && !event_context->space_panning ) {
611 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
612 bool over_line = false;
613 SPCtrlLine *line = NULL;
614 if (drag->lines) {
615 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
616 line = (SPCtrlLine*) l->data;
617 over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
618 if (over_line)
619 break;
620 }
621 }
622 if (over_line && line) {
623 sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
624 ret = TRUE;
625 }
626 } else {
627 dragging = false;
629 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
630 if (event->button.state & GDK_CONTROL_MASK) {
631 ret = TRUE;
632 break;
633 }
635 if (!event_context->within_tolerance) {
636 // we've been dragging, either do nothing (grdrag handles that),
637 // or rubberband-select if we have rubberband
638 Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
639 if (r->is_started() && !event_context->within_tolerance) {
640 // this was a rubberband drag
641 if (r->getMode() == RUBBERBAND_MODE_RECT) {
642 NR::Maybe<NR::Rect> const b = r->getRectangle();
643 drag->selectRect(*b);
644 }
645 }
647 } else if (event_context->item_to_select) {
648 // no dragging, select clicked item if any
649 if (event->button.state & GDK_SHIFT_MASK) {
650 selection->toggle(event_context->item_to_select);
651 } else {
652 selection->set(event_context->item_to_select);
653 }
654 } else {
655 // click in an empty space; do the same as Esc
656 if (drag->selected) {
657 drag->deselectAll();
658 } else {
659 selection->clear();
660 }
661 }
663 event_context->item_to_select = NULL;
664 ret = TRUE;
665 }
666 Inkscape::Rubberband::get()->stop();
667 }
668 break;
669 case GDK_KEY_PRESS:
670 switch (get_group0_keyval (&event->key)) {
671 case GDK_Alt_L:
672 case GDK_Alt_R:
673 case GDK_Control_L:
674 case GDK_Control_R:
675 case GDK_Shift_L:
676 case GDK_Shift_R:
677 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
678 case GDK_Meta_R:
679 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
680 _("<b>Ctrl</b>: snap gradient angle"),
681 _("<b>Shift</b>: draw gradient around the starting point"),
682 NULL);
683 break;
685 case GDK_x:
686 case GDK_X:
687 if (MOD__ALT_ONLY) {
688 desktop->setToolboxFocusTo ("altx-grad");
689 ret = TRUE;
690 }
691 break;
693 case GDK_A:
694 case GDK_a:
695 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
696 drag->selectAll();
697 ret = TRUE;
698 }
699 break;
701 case GDK_L:
702 case GDK_l:
703 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
704 sp_gradient_simplify(rc, 1e-4);
705 ret = TRUE;
706 }
707 break;
709 case GDK_Escape:
710 if (drag->selected) {
711 drag->deselectAll();
712 } else {
713 selection->clear();
714 }
715 ret = TRUE;
716 //TODO: make dragging escapable by Esc
717 break;
719 case GDK_Left: // move handle left
720 case GDK_KP_Left:
721 case GDK_KP_4:
722 if (!MOD__CTRL) { // not ctrl
723 gint mul = 1 + gobble_key_events(
724 get_group0_keyval(&event->key), 0); // with any mask
725 if (MOD__ALT) { // alt
726 if (MOD__SHIFT) drag->selected_move_screen(mul*-10, 0); // shift
727 else drag->selected_move_screen(mul*-1, 0); // no shift
728 }
729 else { // no alt
730 if (MOD__SHIFT) drag->selected_move(mul*-10*nudge, 0); // shift
731 else drag->selected_move(mul*-nudge, 0); // no shift
732 }
733 ret = TRUE;
734 }
735 break;
736 case GDK_Up: // move handle up
737 case GDK_KP_Up:
738 case GDK_KP_8:
739 if (!MOD__CTRL) { // not ctrl
740 gint mul = 1 + gobble_key_events(
741 get_group0_keyval(&event->key), 0); // with any mask
742 if (MOD__ALT) { // alt
743 if (MOD__SHIFT) drag->selected_move_screen(0, mul*10); // shift
744 else drag->selected_move_screen(0, mul*1); // no shift
745 }
746 else { // no alt
747 if (MOD__SHIFT) drag->selected_move(0, mul*10*nudge); // shift
748 else drag->selected_move(0, mul*nudge); // no shift
749 }
750 ret = TRUE;
751 }
752 break;
753 case GDK_Right: // move handle right
754 case GDK_KP_Right:
755 case GDK_KP_6:
756 if (!MOD__CTRL) { // not ctrl
757 gint mul = 1 + gobble_key_events(
758 get_group0_keyval(&event->key), 0); // with any mask
759 if (MOD__ALT) { // alt
760 if (MOD__SHIFT) drag->selected_move_screen(mul*10, 0); // shift
761 else drag->selected_move_screen(mul*1, 0); // no shift
762 }
763 else { // no alt
764 if (MOD__SHIFT) drag->selected_move(mul*10*nudge, 0); // shift
765 else drag->selected_move(mul*nudge, 0); // no shift
766 }
767 ret = TRUE;
768 }
769 break;
770 case GDK_Down: // move handle down
771 case GDK_KP_Down:
772 case GDK_KP_2:
773 if (!MOD__CTRL) { // not ctrl
774 gint mul = 1 + gobble_key_events(
775 get_group0_keyval(&event->key), 0); // with any mask
776 if (MOD__ALT) { // alt
777 if (MOD__SHIFT) drag->selected_move_screen(0, mul*-10); // shift
778 else drag->selected_move_screen(0, mul*-1); // no shift
779 }
780 else { // no alt
781 if (MOD__SHIFT) drag->selected_move(0, mul*-10*nudge); // shift
782 else drag->selected_move(0, mul*-nudge); // no shift
783 }
784 ret = TRUE;
785 }
786 break;
787 case GDK_r:
788 case GDK_R:
789 if (MOD__SHIFT_ONLY) {
790 // First try selected dragger
791 if (drag && drag->selected) {
792 drag->selected_reverse_vector();
793 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
794 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
795 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
796 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
797 }
798 }
799 // we did an undoable action
800 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
801 _("Invert gradient"));
802 ret = TRUE;
803 }
804 break;
806 case GDK_Insert:
807 case GDK_KP_Insert:
808 // with any modifiers:
809 sp_gradient_context_add_stops_between_selected_stops (rc);
810 ret = TRUE;
811 break;
813 case GDK_Delete:
814 case GDK_KP_Delete:
815 case GDK_BackSpace:
816 if ( drag->selected ) {
817 drag->deleteSelected(MOD__CTRL_ONLY);
818 ret = TRUE;
819 }
820 break;
821 default:
822 break;
823 }
824 break;
825 case GDK_KEY_RELEASE:
826 switch (get_group0_keyval (&event->key)) {
827 case GDK_Alt_L:
828 case GDK_Alt_R:
829 case GDK_Control_L:
830 case GDK_Control_R:
831 case GDK_Shift_L:
832 case GDK_Shift_R:
833 case GDK_Meta_L: // Meta is when you press Shift+Alt
834 case GDK_Meta_R:
835 event_context->defaultMessageContext()->clear();
836 break;
837 default:
838 break;
839 }
840 break;
841 default:
842 break;
843 }
845 if (!ret) {
846 if (((SPEventContextClass *) parent_class)->root_handler) {
847 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
848 }
849 }
851 return ret;
852 }
854 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint /*state*/, guint32 etime)
855 {
856 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
857 Inkscape::Selection *selection = sp_desktop_selection(desktop);
858 SPDocument *document = sp_desktop_document(desktop);
859 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
861 if (!selection->isEmpty()) {
862 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
863 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
865 SPGradient *vector;
866 if (ec->item_to_select) {
867 // pick color from the object where drag started
868 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
869 } else {
870 // Starting from empty space:
871 // Sort items so that the topmost comes last
872 GSList *items = g_slist_copy ((GSList *) selection->itemList());
873 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
874 // take topmost
875 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
876 g_slist_free (items);
877 }
879 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
880 SPCSSAttr *css = sp_repr_css_attr_new();
881 sp_repr_css_set_property(css, "fill-opacity", "1.0");
883 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
885 //FIXME: see above
886 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
888 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
890 if (type == SP_GRADIENT_TYPE_LINEAR) {
891 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
892 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
893 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
894 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
895 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
896 }
897 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
898 }
899 if (ec->_grdrag) {
900 ec->_grdrag->updateDraggers();
901 // prevent regenerating draggers by selection modified signal, which sometimes
902 // comes too late and thus destroys the knot which we will now grab:
903 ec->_grdrag->local_change = true;
904 // give the grab out-of-bounds values of xp/yp because we're already dragging
905 // and therefore are already out of tolerance
906 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
907 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
908 -1, // ignore number (though it is always 1)
909 fill_or_stroke, 99999, 99999, etime);
910 }
911 // We did an undoable action, but sp_document_done will be called by the knot when released
913 // status text; we do not track coords because this branch is run once, not all the time
914 // during drag
915 int n_objects = g_slist_length((GSList *) selection->itemList());
916 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
917 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
918 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
919 n_objects);
920 } else {
921 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
922 }
923 }
926 /*
927 Local Variables:
928 mode:c++
929 c-file-style:"stroustrup"
930 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
931 indent-tabs-mode:nil
932 fill-column:99
933 End:
934 */
935 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :