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