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 "preferences.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, Geom::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 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
207 if (prefs->getBool("/tools/gradient/selcue", true)) {
208 ec->enableSelectionCue();
209 }
211 ec->enableGrDrag();
212 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
214 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
216 rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
217 rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
218 gradient_selection_changed(selection, rc);
219 }
221 void
222 sp_gradient_context_select_next (SPEventContext *event_context)
223 {
224 GrDrag *drag = event_context->_grdrag;
225 g_assert (drag);
227 GrDragger *d = drag->select_next();
229 event_context->desktop->scroll_to_point(d->point, 1.0);
230 }
232 void
233 sp_gradient_context_select_prev (SPEventContext *event_context)
234 {
235 GrDrag *drag = event_context->_grdrag;
236 g_assert (drag);
238 GrDragger *d = drag->select_prev();
240 event_context->desktop->scroll_to_point(d->point, 1.0);
241 }
243 static bool
244 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, Geom::Point event_p)
245 {
246 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
248 //Translate mouse point into proper coord system
249 rc->mousepoint_doc = desktop->w2d(event_p);
251 SPCtrlLine* line = SP_CTRLLINE(item);
253 Geom::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e, 0);
254 double dist_screen = Geom::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
256 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
258 bool close = (dist_screen < tolerance);
260 return close;
261 }
263 std::vector<Geom::Point>
264 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
265 {
266 std::vector<Geom::Point> coords;
268 // for all selected draggers
269 for (GList *i = drag->selected; i != NULL; i = i->next) {
270 GrDragger *dragger = (GrDragger *) i->data;
271 // remember the coord of the dragger to reselect it later
272 coords.push_back(dragger->point);
273 // for all draggables of dragger
274 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
275 GrDraggable *d = (GrDraggable *) j->data;
277 // find the gradient
278 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
279 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
281 // these draggable types cannot have a next draggabe to insert a stop between them
282 if (d->point_type == POINT_LG_END ||
283 d->point_type == POINT_RG_FOCUS ||
284 d->point_type == POINT_RG_R1 ||
285 d->point_type == POINT_RG_R2) {
286 continue;
287 }
289 // from draggables to stops
290 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
291 SPStop *next_stop = sp_next_stop (this_stop);
292 SPStop *last_stop = sp_last_stop (vector);
294 gint fs = d->fill_or_stroke;
295 SPItem *item = d->item;
296 gint type = d->point_type;
297 gint p_i = d->point_i;
299 // if there's a next stop,
300 if (next_stop) {
301 GrDragger *dnext = NULL;
302 // find its dragger
303 // (complex because it may have different types, and because in radial,
304 // more than one dragger may correspond to a stop, so we must distinguish)
305 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
306 if (next_stop == last_stop)
307 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
308 else
309 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
310 } else { // radial
311 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
312 if (next_stop == last_stop)
313 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
314 else
315 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
316 }
317 if ((type == POINT_RG_MID2) ||
318 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
319 if (next_stop == last_stop)
320 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
321 else
322 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
323 }
324 }
326 // if both adjacent draggers selected,
327 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
329 // remember the coords of the future dragger to select it
330 coords.push_back(0.5*(dragger->point + dnext->point));
332 // do not insert a stop now, it will confuse the loop;
333 // just remember the stops
334 *these_stops = g_slist_prepend (*these_stops, this_stop);
335 *next_stops = g_slist_prepend (*next_stops, next_stop);
336 }
337 }
338 }
339 }
340 return coords;
341 }
343 static void
344 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
345 {
346 SPDocument *doc = NULL;
347 GrDrag *drag = rc->_grdrag;
349 GSList *these_stops = NULL;
350 GSList *next_stops = NULL;
352 std::vector<Geom::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
354 if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
355 // if a single stop is selected, add between that stop and the next one
356 GrDragger *dragger = (GrDragger *) drag->selected->data;
357 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
358 GrDraggable *d = (GrDraggable *) j->data;
359 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
360 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
361 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
362 SPStop *next_stop = sp_next_stop (this_stop);
363 if (this_stop && next_stop) {
364 these_stops = g_slist_prepend (these_stops, this_stop);
365 next_stops = g_slist_prepend (next_stops, next_stop);
366 }
367 }
368 }
370 // now actually create the new stops
371 GSList *i = these_stops;
372 GSList *j = next_stops;
373 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
374 SPStop *this_stop = (SPStop *) i->data;
375 SPStop *next_stop = (SPStop *) j->data;
376 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
377 SPObject *parent = SP_OBJECT_PARENT(this_stop);
378 if (SP_IS_GRADIENT (parent)) {
379 doc = SP_OBJECT_DOCUMENT (parent);
380 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
381 sp_gradient_ensure_vector (SP_GRADIENT (parent));
382 }
383 }
385 if (g_slist_length(these_stops) > 0 && doc) {
386 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
387 drag->updateDraggers();
388 // so that it does not automatically update draggers in idle loop, as this would deselect
389 drag->local_change = true;
390 // select all the old selected and new created draggers
391 drag->selectByCoords(coords);
392 }
394 g_slist_free (these_stops);
395 g_slist_free (next_stops);
396 }
398 double sqr(double x) {return x*x;}
400 static void
401 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
402 {
403 SPDocument *doc = NULL;
404 GrDrag *drag = rc->_grdrag;
406 GSList *these_stops = NULL;
407 GSList *next_stops = NULL;
409 std::vector<Geom::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
411 GSList *todel = NULL;
413 GSList *i = these_stops;
414 GSList *j = next_stops;
415 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
416 SPStop *stop0 = (SPStop *) i->data;
417 SPStop *stop1 = (SPStop *) j->data;
419 gint i1 = g_slist_index(these_stops, stop1);
420 if (i1 != -1) {
421 GSList *next_next = g_slist_nth (next_stops, i1);
422 if (next_next) {
423 SPStop *stop2 = (SPStop *) next_next->data;
425 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
426 continue;
428 guint32 const c0 = sp_stop_get_rgba32(stop0);
429 guint32 const c2 = sp_stop_get_rgba32(stop2);
430 guint32 const c1r = sp_stop_get_rgba32(stop1);
431 guint32 c1 = average_color (c0, c2,
432 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
434 double diff =
435 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
436 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
437 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
438 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
440 if (diff < tolerance)
441 todel = g_slist_prepend (todel, stop1);
442 }
443 }
444 }
446 for (i = todel; i != NULL; i = i->next) {
447 SPStop *stop = (SPStop*) i->data;
448 doc = SP_OBJECT_DOCUMENT (stop);
449 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
450 parent->removeChild(SP_OBJECT_REPR(stop));
451 }
453 if (g_slist_length(todel) > 0) {
454 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
455 drag->local_change = true;
456 drag->updateDraggers();
457 drag->selectByCoords(coords);
458 }
460 g_slist_free (todel);
461 g_slist_free (these_stops);
462 g_slist_free (next_stops);
463 }
466 static void
467 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
468 {
469 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
471 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
472 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
474 double tolerance = (double) ec->tolerance;
476 ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
478 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
479 _("Add gradient stop"));
481 ec->get_drag()->updateDraggers();
482 }
485 static gint
486 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
487 {
488 static bool dragging;
490 SPDesktop *desktop = event_context->desktop;
491 Inkscape::Selection *selection = sp_desktop_selection (desktop);
492 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
494 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
496 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
497 double const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
499 GrDrag *drag = event_context->_grdrag;
500 g_assert (drag);
502 gint ret = FALSE;
503 switch (event->type) {
504 case GDK_2BUTTON_PRESS:
505 if ( event->button.button == 1 ) {
506 bool over_line = false;
507 SPCtrlLine *line = NULL;
508 if (drag->lines) {
509 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
510 line = (SPCtrlLine*) l->data;
511 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
512 }
513 }
514 if (over_line) {
515 // we take the first item in selection, because with doubleclick, the first click
516 // always resets selection to the single object under cursor
517 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
518 } else {
519 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
520 SPItem *item = SP_ITEM(i->data);
521 SPGradientType new_type = (SPGradientType) prefs->getInt("/tools/gradient/newgradient", SP_GRADIENT_TYPE_LINEAR);
522 guint new_fill = prefs->getInt("/tools/gradient/newfillorstroke", 1);
524 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
526 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
527 sp_gradient_reset_to_userspace(priv, item);
528 }
530 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
531 _("Create default gradient"));
532 }
533 ret = TRUE;
534 }
535 break;
536 case GDK_BUTTON_PRESS:
537 if ( event->button.button == 1 && !event_context->space_panning ) {
538 Geom::Point button_w(event->button.x, event->button.y);
540 // save drag origin
541 event_context->xp = (gint) button_w[Geom::X];
542 event_context->yp = (gint) button_w[Geom::Y];
543 event_context->within_tolerance = true;
545 dragging = true;
547 Geom::Point button_dt = to_2geom(desktop->w2d(button_w));
548 if (event->button.state & GDK_SHIFT_MASK) {
549 Inkscape::Rubberband::get(desktop)->start(desktop, from_2geom(button_dt));
550 } else {
551 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
552 // enable Ctrl+doubleclick of exactly the selected item(s)
553 if (!(event->button.state & GDK_CONTROL_MASK))
554 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
556 /* Snap center to nearest magnetic point */
557 SnapManager &m = desktop->namedview->snap_manager;
558 m.setup(desktop);
559 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt);
560 rc->origin = from_2geom(button_dt);
561 }
563 ret = TRUE;
564 }
565 break;
566 case GDK_MOTION_NOTIFY:
567 if ( dragging
568 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
569 {
570 if ( event_context->within_tolerance
571 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
572 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
573 break; // do not drag if we're within tolerance from origin
574 }
575 // Once the user has moved farther than tolerance from the original location
576 // (indicating they intend to draw, not click), then always process the
577 // motion notify coordinates as given (no snapping back to origin)
578 event_context->within_tolerance = false;
580 Geom::Point const motion_w(event->motion.x,
581 event->motion.y);
582 Geom::Point const motion_dt = event_context->desktop->w2d(motion_w);
584 if (Inkscape::Rubberband::get(desktop)->is_started()) {
585 Inkscape::Rubberband::get(desktop)->move(motion_dt);
586 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
587 } else {
588 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
589 }
590 gobble_motion_events(GDK_BUTTON1_MASK);
592 ret = TRUE;
593 } else {
594 bool over_line = false;
595 if (drag->lines) {
596 for (GSList *l = drag->lines; l != NULL; l = l->next) {
597 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, Geom::Point(event->motion.x, event->motion.y));
598 }
599 }
601 if (rc->cursor_addnode && !over_line) {
602 event_context->cursor_shape = cursor_gradient_xpm;
603 sp_event_context_update_cursor(event_context);
604 rc->cursor_addnode = false;
605 } else if (!rc->cursor_addnode && over_line) {
606 event_context->cursor_shape = cursor_gradient_add_xpm;
607 sp_event_context_update_cursor(event_context);
608 rc->cursor_addnode = true;
609 }
610 }
611 break;
612 case GDK_BUTTON_RELEASE:
613 event_context->xp = event_context->yp = 0;
614 if ( event->button.button == 1 && !event_context->space_panning ) {
615 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
616 bool over_line = false;
617 SPCtrlLine *line = NULL;
618 if (drag->lines) {
619 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
620 line = (SPCtrlLine*) l->data;
621 over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
622 if (over_line)
623 break;
624 }
625 }
626 if (over_line && line) {
627 sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
628 ret = TRUE;
629 }
630 } else {
631 dragging = false;
633 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
634 if (event->button.state & GDK_CONTROL_MASK) {
635 ret = TRUE;
636 break;
637 }
639 if (!event_context->within_tolerance) {
640 // we've been dragging, either do nothing (grdrag handles that),
641 // or rubberband-select if we have rubberband
642 Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
643 if (r->is_started() && !event_context->within_tolerance) {
644 // this was a rubberband drag
645 if (r->getMode() == RUBBERBAND_MODE_RECT) {
646 Geom::OptRect const b = r->getRectangle();
647 drag->selectRect(*b);
648 }
649 }
651 } else if (event_context->item_to_select) {
652 // no dragging, select clicked item if any
653 if (event->button.state & GDK_SHIFT_MASK) {
654 selection->toggle(event_context->item_to_select);
655 } else {
656 selection->set(event_context->item_to_select);
657 }
658 } else {
659 // click in an empty space; do the same as Esc
660 if (drag->selected) {
661 drag->deselectAll();
662 } else {
663 selection->clear();
664 }
665 }
667 event_context->item_to_select = NULL;
668 ret = TRUE;
669 }
670 Inkscape::Rubberband::get(desktop)->stop();
671 }
672 break;
673 case GDK_KEY_PRESS:
674 switch (get_group0_keyval (&event->key)) {
675 case GDK_Alt_L:
676 case GDK_Alt_R:
677 case GDK_Control_L:
678 case GDK_Control_R:
679 case GDK_Shift_L:
680 case GDK_Shift_R:
681 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
682 case GDK_Meta_R:
683 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
684 _("<b>Ctrl</b>: snap gradient angle"),
685 _("<b>Shift</b>: draw gradient around the starting point"),
686 NULL);
687 break;
689 case GDK_x:
690 case GDK_X:
691 if (MOD__ALT_ONLY) {
692 desktop->setToolboxFocusTo ("altx-grad");
693 ret = TRUE;
694 }
695 break;
697 case GDK_A:
698 case GDK_a:
699 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
700 drag->selectAll();
701 ret = TRUE;
702 }
703 break;
705 case GDK_L:
706 case GDK_l:
707 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
708 sp_gradient_simplify(rc, 1e-4);
709 ret = TRUE;
710 }
711 break;
713 case GDK_Escape:
714 if (drag->selected) {
715 drag->deselectAll();
716 } else {
717 selection->clear();
718 }
719 ret = TRUE;
720 //TODO: make dragging escapable by Esc
721 break;
723 case GDK_Left: // move handle left
724 case GDK_KP_Left:
725 case GDK_KP_4:
726 if (!MOD__CTRL) { // not ctrl
727 gint mul = 1 + gobble_key_events(
728 get_group0_keyval(&event->key), 0); // with any mask
729 if (MOD__ALT) { // alt
730 if (MOD__SHIFT) drag->selected_move_screen(mul*-10, 0); // shift
731 else drag->selected_move_screen(mul*-1, 0); // no shift
732 }
733 else { // no alt
734 if (MOD__SHIFT) drag->selected_move(mul*-10*nudge, 0); // shift
735 else drag->selected_move(mul*-nudge, 0); // no shift
736 }
737 ret = TRUE;
738 }
739 break;
740 case GDK_Up: // move handle up
741 case GDK_KP_Up:
742 case GDK_KP_8:
743 if (!MOD__CTRL) { // not ctrl
744 gint mul = 1 + gobble_key_events(
745 get_group0_keyval(&event->key), 0); // with any mask
746 if (MOD__ALT) { // alt
747 if (MOD__SHIFT) drag->selected_move_screen(0, mul*10); // shift
748 else drag->selected_move_screen(0, mul*1); // no shift
749 }
750 else { // no alt
751 if (MOD__SHIFT) drag->selected_move(0, mul*10*nudge); // shift
752 else drag->selected_move(0, mul*nudge); // no shift
753 }
754 ret = TRUE;
755 }
756 break;
757 case GDK_Right: // move handle right
758 case GDK_KP_Right:
759 case GDK_KP_6:
760 if (!MOD__CTRL) { // not ctrl
761 gint mul = 1 + gobble_key_events(
762 get_group0_keyval(&event->key), 0); // with any mask
763 if (MOD__ALT) { // alt
764 if (MOD__SHIFT) drag->selected_move_screen(mul*10, 0); // shift
765 else drag->selected_move_screen(mul*1, 0); // no shift
766 }
767 else { // no alt
768 if (MOD__SHIFT) drag->selected_move(mul*10*nudge, 0); // shift
769 else drag->selected_move(mul*nudge, 0); // no shift
770 }
771 ret = TRUE;
772 }
773 break;
774 case GDK_Down: // move handle down
775 case GDK_KP_Down:
776 case GDK_KP_2:
777 if (!MOD__CTRL) { // not ctrl
778 gint mul = 1 + gobble_key_events(
779 get_group0_keyval(&event->key), 0); // with any mask
780 if (MOD__ALT) { // alt
781 if (MOD__SHIFT) drag->selected_move_screen(0, mul*-10); // shift
782 else drag->selected_move_screen(0, mul*-1); // no shift
783 }
784 else { // no alt
785 if (MOD__SHIFT) drag->selected_move(0, mul*-10*nudge); // shift
786 else drag->selected_move(0, mul*-nudge); // no shift
787 }
788 ret = TRUE;
789 }
790 break;
791 case GDK_r:
792 case GDK_R:
793 if (MOD__SHIFT_ONLY) {
794 // First try selected dragger
795 if (drag && drag->selected) {
796 drag->selected_reverse_vector();
797 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
798 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
799 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
800 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
801 }
802 }
803 // we did an undoable action
804 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
805 _("Invert gradient"));
806 ret = TRUE;
807 }
808 break;
810 case GDK_Insert:
811 case GDK_KP_Insert:
812 // with any modifiers:
813 sp_gradient_context_add_stops_between_selected_stops (rc);
814 ret = TRUE;
815 break;
817 case GDK_Delete:
818 case GDK_KP_Delete:
819 case GDK_BackSpace:
820 if ( drag->selected ) {
821 drag->deleteSelected(MOD__CTRL_ONLY);
822 ret = TRUE;
823 }
824 break;
825 default:
826 break;
827 }
828 break;
829 case GDK_KEY_RELEASE:
830 switch (get_group0_keyval (&event->key)) {
831 case GDK_Alt_L:
832 case GDK_Alt_R:
833 case GDK_Control_L:
834 case GDK_Control_R:
835 case GDK_Shift_L:
836 case GDK_Shift_R:
837 case GDK_Meta_L: // Meta is when you press Shift+Alt
838 case GDK_Meta_R:
839 event_context->defaultMessageContext()->clear();
840 break;
841 default:
842 break;
843 }
844 break;
845 default:
846 break;
847 }
849 if (!ret) {
850 if (((SPEventContextClass *) parent_class)->root_handler) {
851 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
852 }
853 }
855 return ret;
856 }
858 static void sp_gradient_drag(SPGradientContext &rc, Geom::Point const pt, guint /*state*/, guint32 etime)
859 {
860 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
861 Inkscape::Selection *selection = sp_desktop_selection(desktop);
862 SPDocument *document = sp_desktop_document(desktop);
863 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
865 if (!selection->isEmpty()) {
866 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
867 int type = prefs->getInt("/tools/gradient/newgradient", 1);
868 int fill_or_stroke = prefs->getInt("/tools/gradient/newfillorstroke", 1);
870 SPGradient *vector;
871 if (ec->item_to_select) {
872 // pick color from the object where drag started
873 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
874 } else {
875 // Starting from empty space:
876 // Sort items so that the topmost comes last
877 GSList *items = g_slist_copy ((GSList *) selection->itemList());
878 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
879 // take topmost
880 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
881 g_slist_free (items);
882 }
884 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
885 SPCSSAttr *css = sp_repr_css_attr_new();
886 sp_repr_css_set_property(css, "fill-opacity", "1.0");
888 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
890 //FIXME: see above
891 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
893 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
895 if (type == SP_GRADIENT_TYPE_LINEAR) {
896 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
897 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
898 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
899 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
900 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
901 }
902 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
903 }
904 if (ec->_grdrag) {
905 ec->_grdrag->updateDraggers();
906 // prevent regenerating draggers by selection modified signal, which sometimes
907 // comes too late and thus destroys the knot which we will now grab:
908 ec->_grdrag->local_change = true;
909 // give the grab out-of-bounds values of xp/yp because we're already dragging
910 // and therefore are already out of tolerance
911 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
912 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
913 -1, // ignore number (though it is always 1)
914 fill_or_stroke, 99999, 99999, etime);
915 }
916 // We did an undoable action, but sp_document_done will be called by the knot when released
918 // status text; we do not track coords because this branch is run once, not all the time
919 // during drag
920 int n_objects = g_slist_length((GSList *) selection->itemList());
921 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
922 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
923 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
924 n_objects);
925 } else {
926 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
927 }
928 }
931 /*
932 Local Variables:
933 mode:c++
934 c-file-style:"stroustrup"
935 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
936 indent-tabs-mode:nil
937 fill-column:99
938 End:
939 */
940 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :