5424b7a3d7289280e659e177749b48855ffbd68b
1 #define __SP_GRADIENT_CONTEXT_C__
3 /*
4 * Gradient drawing and editing tool
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 *
10 * Copyright (C) 2007 Johan Engelen
11 * Copyright (C) 2005 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "macros.h"
24 #include "document.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-handles.h"
28 #include "message-context.h"
29 #include "message-stack.h"
30 #include "pixmaps/cursor-gradient.xpm"
31 #include "pixmaps/cursor-gradient-add.xpm"
32 #include "pixmaps/cursor-gradient-delete.xpm"
33 #include "gradient-context.h"
34 #include "gradient-chemistry.h"
35 #include <glibmm/i18n.h>
36 #include "prefs-utils.h"
37 #include "gradient-drag.h"
38 #include "gradient-chemistry.h"
39 #include "xml/repr.h"
40 #include "sp-item.h"
41 #include "display/sp-ctrlline.h"
42 #include "sp-linear-gradient.h"
43 #include "sp-radial-gradient.h"
44 #include "sp-stop.h"
45 #include "svg/css-ostringstream.h"
46 #include "svg/svg-color.h"
47 #include "snap.h"
48 #include "sp-namedview.h"
49 #include "rubberband.h"
53 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
54 static void sp_gradient_context_init(SPGradientContext *gr_context);
55 static void sp_gradient_context_dispose(GObject *object);
57 static void sp_gradient_context_setup(SPEventContext *ec);
59 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
61 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
63 static SPEventContextClass *parent_class;
66 GtkType sp_gradient_context_get_type()
67 {
68 static GType type = 0;
69 if (!type) {
70 GTypeInfo info = {
71 sizeof(SPGradientContextClass),
72 NULL, NULL,
73 (GClassInitFunc) sp_gradient_context_class_init,
74 NULL, NULL,
75 sizeof(SPGradientContext),
76 4,
77 (GInstanceInitFunc) sp_gradient_context_init,
78 NULL, /* value_table */
79 };
80 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
81 }
82 return type;
83 }
85 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
86 {
87 GObjectClass *object_class = (GObjectClass *) klass;
88 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
90 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
92 object_class->dispose = sp_gradient_context_dispose;
94 event_context_class->setup = sp_gradient_context_setup;
95 event_context_class->root_handler = sp_gradient_context_root_handler;
96 }
98 static void sp_gradient_context_init(SPGradientContext *gr_context)
99 {
100 SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
102 gr_context->cursor_addnode = false;
103 event_context->cursor_shape = cursor_gradient_xpm;
104 event_context->hot_x = 4;
105 event_context->hot_y = 4;
106 event_context->xp = 0;
107 event_context->yp = 0;
108 event_context->tolerance = 6;
109 event_context->within_tolerance = false;
110 event_context->item_to_select = NULL;
111 }
113 static void sp_gradient_context_dispose(GObject *object)
114 {
115 SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
116 SPEventContext *ec = SP_EVENT_CONTEXT(object);
118 ec->enableGrDrag(false);
120 if (rc->_message_context) {
121 delete rc->_message_context;
122 }
124 rc->selcon->disconnect();
125 delete rc->selcon;
126 rc->subselcon->disconnect();
127 delete rc->subselcon;
129 G_OBJECT_CLASS(parent_class)->dispose(object);
130 }
132 const gchar *gr_handle_descr [] = {
133 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
134 N_("Linear gradient <b>end</b>"),
135 N_("Linear gradient <b>mid stop</b>"),
136 N_("Radial gradient <b>center</b>"),
137 N_("Radial gradient <b>radius</b>"),
138 N_("Radial gradient <b>radius</b>"),
139 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
140 N_("Radial gradient <b>mid stop</b>"),
141 N_("Radial gradient <b>mid stop</b>")
142 };
144 static void
145 gradient_selection_changed (Inkscape::Selection *, gpointer data)
146 {
147 SPGradientContext *rc = (SPGradientContext *) data;
149 GrDrag *drag = rc->_grdrag;
150 Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(rc)->desktop);
151 guint n_obj = g_slist_length((GSList *) selection->itemList());
153 if (!drag->isNonEmpty() || selection->isEmpty())
154 return;
155 guint n_tot = drag->numDraggers();
156 guint n_sel = drag->numSelected();
158 if (n_sel == 1) {
159 if (drag->singleSelectedDraggerNumDraggables() == 1) {
160 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
161 _("%s selected out of %d gradient handles on %d selected object(s)"), gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()], n_tot, n_obj);
162 } else {
163 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
164 _("One handle merging %d stops (drag with <b>Shift</b> to separate) selected out of %d gradient handles on %d selected object(s)"), drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
165 }
166 } else if (n_sel > 1) {
167 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
168 _("<b>%d</b> gradient handles selected out of %d on %d selected object(s)"), n_sel, n_tot, n_obj);
169 } else if (n_sel == 0) {
170 rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
171 _("<b>No</b> gradient handles selected out of %d on %d selected object(s)"), n_tot, n_obj);
172 }
173 }
175 static void
176 gradient_subselection_changed (gpointer, gpointer data)
177 {
178 gradient_selection_changed (NULL, data);
179 }
182 static void sp_gradient_context_setup(SPEventContext *ec)
183 {
184 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
186 if (((SPEventContextClass *) parent_class)->setup) {
187 ((SPEventContextClass *) parent_class)->setup(ec);
188 }
190 if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
191 ec->enableSelectionCue();
192 }
194 ec->enableGrDrag();
195 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
197 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
199 rc->selcon = new sigc::connection (selection->connectChanged( sigc::bind (sigc::ptr_fun(&gradient_selection_changed), rc)));
200 rc->subselcon = new sigc::connection (ec->desktop->connectToolSubselectionChanged(sigc::bind (sigc::ptr_fun(&gradient_subselection_changed), rc)));
201 gradient_selection_changed(selection, rc);
202 }
204 void
205 sp_gradient_context_select_next (SPEventContext *event_context)
206 {
207 GrDrag *drag = event_context->_grdrag;
208 g_assert (drag);
210 GrDragger *d = drag->select_next();
212 event_context->desktop->scroll_to_point(&(d->point), 1.0);
213 }
215 void
216 sp_gradient_context_select_prev (SPEventContext *event_context)
217 {
218 GrDrag *drag = event_context->_grdrag;
219 g_assert (drag);
221 GrDragger *d = drag->select_prev();
223 event_context->desktop->scroll_to_point(&(d->point), 1.0);
224 }
226 static bool
227 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
228 {
229 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
231 //Translate mouse point into proper coord system
232 rc->mousepoint_doc = desktop->w2d(event_p);
234 SPCtrlLine* line = SP_CTRLLINE(item);
236 NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e, 0);
237 double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
239 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
241 bool close = (dist_screen < tolerance);
243 return close;
244 }
246 std::vector<NR::Point>
247 sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
248 {
249 std::vector<NR::Point> coords;
251 // for all selected draggers
252 for (GList *i = drag->selected; i != NULL; i = i->next) {
253 GrDragger *dragger = (GrDragger *) i->data;
254 // remember the coord of the dragger to reselect it later
255 coords.push_back(dragger->point);
256 // for all draggables of dragger
257 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
258 GrDraggable *d = (GrDraggable *) j->data;
260 // find the gradient
261 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
262 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
264 // these draggable types cannot have a next draggabe to insert a stop between them
265 if (d->point_type == POINT_LG_END ||
266 d->point_type == POINT_RG_FOCUS ||
267 d->point_type == POINT_RG_R1 ||
268 d->point_type == POINT_RG_R2) {
269 continue;
270 }
272 // from draggables to stops
273 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
274 SPStop *next_stop = sp_next_stop (this_stop);
275 SPStop *last_stop = sp_last_stop (vector);
277 gint fs = d->fill_or_stroke;
278 SPItem *item = d->item;
279 gint type = d->point_type;
280 gint p_i = d->point_i;
282 // if there's a next stop,
283 if (next_stop) {
284 GrDragger *dnext = NULL;
285 // find its dragger
286 // (complex because it may have different types, and because in radial,
287 // more than one dragger may correspond to a stop, so we must distinguish)
288 if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
289 if (next_stop == last_stop)
290 dnext = drag->getDraggerFor (item, POINT_LG_END, p_i+1, fs);
291 else
292 dnext = drag->getDraggerFor (item, POINT_LG_MID, p_i+1, fs);
293 } else { // radial
294 if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
295 if (next_stop == last_stop)
296 dnext = drag->getDraggerFor (item, POINT_RG_R1, p_i+1, fs);
297 else
298 dnext = drag->getDraggerFor (item, POINT_RG_MID1, p_i+1, fs);
299 }
300 if ((type == POINT_RG_MID2) ||
301 (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
302 if (next_stop == last_stop)
303 dnext = drag->getDraggerFor (item, POINT_RG_R2, p_i+1, fs);
304 else
305 dnext = drag->getDraggerFor (item, POINT_RG_MID2, p_i+1, fs);
306 }
307 }
309 // if both adjacent draggers selected,
310 if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
312 // remember the coords of the future dragger to select it
313 coords.push_back(0.5*(dragger->point + dnext->point));
315 // do not insert a stop now, it will confuse the loop;
316 // just remember the stops
317 *these_stops = g_slist_prepend (*these_stops, this_stop);
318 *next_stops = g_slist_prepend (*next_stops, next_stop);
319 }
320 }
321 }
322 }
323 return coords;
324 }
326 static void
327 sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
328 {
329 SPDocument *doc = NULL;
330 GrDrag *drag = rc->_grdrag;
332 GSList *these_stops = NULL;
333 GSList *next_stops = NULL;
335 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
337 if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
338 // if a single stop is selected, add between that stop and the next one
339 GrDragger *dragger = (GrDragger *) drag->selected->data;
340 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
341 GrDraggable *d = (GrDraggable *) j->data;
342 SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke);
343 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
344 SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
345 SPStop *next_stop = sp_next_stop (this_stop);
346 if (this_stop && next_stop) {
347 these_stops = g_slist_prepend (these_stops, this_stop);
348 next_stops = g_slist_prepend (next_stops, next_stop);
349 }
350 }
351 }
353 // now actually create the new stops
354 GSList *i = these_stops;
355 GSList *j = next_stops;
356 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
357 SPStop *this_stop = (SPStop *) i->data;
358 SPStop *next_stop = (SPStop *) j->data;
359 gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
360 SPObject *parent = SP_OBJECT_PARENT(this_stop);
361 if (SP_IS_GRADIENT (parent)) {
362 doc = SP_OBJECT_DOCUMENT (parent);
363 sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
364 sp_gradient_ensure_vector (SP_GRADIENT (parent));
365 }
366 }
368 if (g_slist_length(these_stops) > 0 && doc) {
369 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
370 drag->updateDraggers();
371 // so that it does not automatically update draggers in idle loop, as this would deselect
372 drag->local_change = true;
373 // select all the old selected and new created draggers
374 drag->selectByCoords(coords);
375 }
377 g_slist_free (these_stops);
378 g_slist_free (next_stops);
379 }
381 double sqr(double x) {return x*x;}
383 static void
384 sp_gradient_simplify(SPGradientContext *rc, double tolerance)
385 {
386 SPDocument *doc = NULL;
387 GrDrag *drag = rc->_grdrag;
389 GSList *these_stops = NULL;
390 GSList *next_stops = NULL;
392 std::vector<NR::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
394 GSList *todel = NULL;
396 GSList *i = these_stops;
397 GSList *j = next_stops;
398 for (; i != NULL && j != NULL; i = i->next, j = j->next) {
399 SPStop *stop0 = (SPStop *) i->data;
400 SPStop *stop1 = (SPStop *) j->data;
402 gint i1 = g_slist_index(these_stops, stop1);
403 if (i1 != -1) {
404 GSList *next_next = g_slist_nth (next_stops, i1);
405 if (next_next) {
406 SPStop *stop2 = (SPStop *) next_next->data;
408 if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
409 continue;
411 guint32 const c0 = sp_stop_get_rgba32(stop0);
412 guint32 const c2 = sp_stop_get_rgba32(stop2);
413 guint32 const c1r = sp_stop_get_rgba32(stop1);
414 guint32 c1 = average_color (c0, c2,
415 (stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
417 double diff =
418 sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
419 sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
420 sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
421 sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
423 if (diff < tolerance)
424 todel = g_slist_prepend (todel, stop1);
425 }
426 }
427 }
429 for (i = todel; i != NULL; i = i->next) {
430 SPStop *stop = (SPStop*) i->data;
431 doc = SP_OBJECT_DOCUMENT (stop);
432 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
433 parent->removeChild(SP_OBJECT_REPR(stop));
434 }
436 if (g_slist_length(todel) > 0) {
437 sp_document_done (doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
438 drag->local_change = true;
439 drag->updateDraggers();
440 drag->selectByCoords(coords);
441 }
443 g_slist_free (todel);
444 g_slist_free (these_stops);
445 g_slist_free (next_stops);
446 }
449 static void
450 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 /*etime*/)
451 {
452 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
454 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
455 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
457 double tolerance = (double) ec->tolerance;
459 ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
461 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
462 _("Add gradient stop"));
464 ec->get_drag()->updateDraggers();
465 }
468 static gint
469 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
470 {
471 static bool dragging;
473 SPDesktop *desktop = event_context->desktop;
474 Inkscape::Selection *selection = sp_desktop_selection (desktop);
476 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
478 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
479 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
481 GrDrag *drag = event_context->_grdrag;
482 g_assert (drag);
484 gint ret = FALSE;
485 switch (event->type) {
486 case GDK_2BUTTON_PRESS:
487 if ( event->button.button == 1 ) {
488 bool over_line = false;
489 SPCtrlLine *line = NULL;
490 if (drag->lines) {
491 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
492 line = (SPCtrlLine*) l->data;
493 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
494 }
495 }
496 if (over_line) {
497 // we take the first item in selection, because with doubleclick, the first click
498 // always resets selection to the single object under cursor
499 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
500 } else {
501 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
502 SPItem *item = SP_ITEM(i->data);
503 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
504 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
506 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
508 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
509 sp_gradient_reset_to_userspace(priv, item);
510 }
512 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
513 _("Create default gradient"));
514 }
515 ret = TRUE;
516 }
517 break;
518 case GDK_BUTTON_PRESS:
519 if ( event->button.button == 1 && !event_context->space_panning ) {
520 NR::Point const button_w(event->button.x, event->button.y);
522 // save drag origin
523 event_context->xp = (gint) button_w[NR::X];
524 event_context->yp = (gint) button_w[NR::Y];
525 event_context->within_tolerance = true;
527 dragging = true;
529 NR::Point const button_dt = desktop->w2d(button_w);
530 if (event->button.state & GDK_SHIFT_MASK) {
531 Inkscape::Rubberband::get()->start(desktop, button_dt);
532 } else {
533 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
534 // enable Ctrl+doubleclick of exactly the selected item(s)
535 if (!(event->button.state & GDK_CONTROL_MASK))
536 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
538 /* Snap center to nearest magnetic point */
539 SnapManager const &m = desktop->namedview->snap_manager;
540 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
541 }
543 ret = TRUE;
544 }
545 break;
546 case GDK_MOTION_NOTIFY:
547 if ( dragging
548 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
549 {
550 if ( event_context->within_tolerance
551 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
552 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
553 break; // do not drag if we're within tolerance from origin
554 }
555 // Once the user has moved farther than tolerance from the original location
556 // (indicating they intend to draw, not click), then always process the
557 // motion notify coordinates as given (no snapping back to origin)
558 event_context->within_tolerance = false;
560 NR::Point const motion_w(event->motion.x,
561 event->motion.y);
562 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
564 if (Inkscape::Rubberband::get()->is_started()) {
565 Inkscape::Rubberband::get()->move(motion_dt);
566 event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
567 } else {
568 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
569 }
570 gobble_motion_events(GDK_BUTTON1_MASK);
572 ret = TRUE;
573 } else {
574 bool over_line = false;
575 if (drag->lines) {
576 for (GSList *l = drag->lines; l != NULL; l = l->next) {
577 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
578 }
579 }
581 if (rc->cursor_addnode && !over_line) {
582 event_context->cursor_shape = cursor_gradient_xpm;
583 sp_event_context_update_cursor(event_context);
584 rc->cursor_addnode = false;
585 } else if (!rc->cursor_addnode && over_line) {
586 event_context->cursor_shape = cursor_gradient_add_xpm;
587 sp_event_context_update_cursor(event_context);
588 rc->cursor_addnode = true;
589 }
590 }
591 break;
592 case GDK_BUTTON_RELEASE:
593 event_context->xp = event_context->yp = 0;
594 if ( event->button.button == 1 && !event_context->space_panning ) {
595 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
596 bool over_line = false;
597 SPCtrlLine *line = NULL;
598 if (drag->lines) {
599 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
600 line = (SPCtrlLine*) l->data;
601 over_line = sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
602 if (over_line)
603 break;
604 }
605 }
606 if (over_line && line) {
607 sp_gradient_context_add_stop_near_point(rc, line->item, rc->mousepoint_doc, 0);
608 ret = TRUE;
609 }
610 } else {
611 dragging = false;
613 // unless clicked with Ctrl (to enable Ctrl+doubleclick).
614 if (event->button.state & GDK_CONTROL_MASK) {
615 ret = TRUE;
616 break;
617 }
619 if (!event_context->within_tolerance) {
620 // we've been dragging, either do nothing (grdrag handles that),
621 // or rubberband-select if we have rubberband
622 Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
623 if (r->is_started() && !event_context->within_tolerance) {
624 // this was a rubberband drag
625 if (r->getMode() == RUBBERBAND_MODE_RECT) {
626 NR::Maybe<NR::Rect> const b = r->getRectangle();
627 drag->selectRect(*b);
628 }
629 }
631 } else if (event_context->item_to_select) {
632 // no dragging, select clicked item if any
633 if (event->button.state & GDK_SHIFT_MASK) {
634 selection->toggle(event_context->item_to_select);
635 } else {
636 selection->set(event_context->item_to_select);
637 }
638 } else {
639 // click in an empty space; do the same as Esc
640 if (drag->selected) {
641 drag->deselectAll();
642 } else {
643 selection->clear();
644 }
645 }
647 event_context->item_to_select = NULL;
648 ret = TRUE;
649 }
650 Inkscape::Rubberband::get()->stop();
651 }
652 break;
653 case GDK_KEY_PRESS:
654 switch (get_group0_keyval (&event->key)) {
655 case GDK_Alt_L:
656 case GDK_Alt_R:
657 case GDK_Control_L:
658 case GDK_Control_R:
659 case GDK_Shift_L:
660 case GDK_Shift_R:
661 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
662 case GDK_Meta_R:
663 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
664 _("<b>Ctrl</b>: snap gradient angle"),
665 _("<b>Shift</b>: draw gradient around the starting point"),
666 NULL);
667 break;
669 case GDK_x:
670 case GDK_X:
671 if (MOD__ALT_ONLY) {
672 desktop->setToolboxFocusTo ("altx-grad");
673 ret = TRUE;
674 }
675 break;
677 case GDK_A:
678 case GDK_a:
679 if (MOD__CTRL_ONLY && drag->isNonEmpty()) {
680 drag->selectAll();
681 ret = TRUE;
682 }
683 break;
685 case GDK_L:
686 case GDK_l:
687 if (MOD__CTRL_ONLY && drag->isNonEmpty() && drag->hasSelection()) {
688 sp_gradient_simplify(rc, 1e-4);
689 ret = TRUE;
690 }
691 break;
693 case GDK_Escape:
694 if (drag->selected) {
695 drag->deselectAll();
696 } else {
697 selection->clear();
698 }
699 ret = TRUE;
700 //TODO: make dragging escapable by Esc
701 break;
703 case GDK_Left: // move handle left
704 case GDK_KP_Left:
705 case GDK_KP_4:
706 if (!MOD__CTRL) { // not ctrl
707 if (MOD__ALT) { // alt
708 if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
709 else drag->selected_move_screen(-1, 0); // no shift
710 }
711 else { // no alt
712 if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
713 else drag->selected_move(-nudge, 0); // no shift
714 }
715 ret = TRUE;
716 }
717 break;
718 case GDK_Up: // move handle up
719 case GDK_KP_Up:
720 case GDK_KP_8:
721 if (!MOD__CTRL) { // not ctrl
722 if (MOD__ALT) { // alt
723 if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
724 else drag->selected_move_screen(0, 1); // no shift
725 }
726 else { // no alt
727 if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
728 else drag->selected_move(0, nudge); // no shift
729 }
730 ret = TRUE;
731 }
732 break;
733 case GDK_Right: // move handle right
734 case GDK_KP_Right:
735 case GDK_KP_6:
736 if (!MOD__CTRL) { // not ctrl
737 if (MOD__ALT) { // alt
738 if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
739 else drag->selected_move_screen(1, 0); // no shift
740 }
741 else { // no alt
742 if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
743 else drag->selected_move(nudge, 0); // no shift
744 }
745 ret = TRUE;
746 }
747 break;
748 case GDK_Down: // move handle down
749 case GDK_KP_Down:
750 case GDK_KP_2:
751 if (!MOD__CTRL) { // not ctrl
752 if (MOD__ALT) { // alt
753 if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
754 else drag->selected_move_screen(0, -1); // no shift
755 }
756 else { // no alt
757 if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
758 else drag->selected_move(0, -nudge); // no shift
759 }
760 ret = TRUE;
761 }
762 break;
763 case GDK_r:
764 case GDK_R:
765 if (MOD__SHIFT_ONLY) {
766 // First try selected dragger
767 if (drag && drag->selected) {
768 drag->selected_reverse_vector();
769 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
770 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
771 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
772 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
773 }
774 }
775 // we did an undoable action
776 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
777 _("Invert gradient"));
778 ret = TRUE;
779 }
780 break;
782 case GDK_Insert:
783 case GDK_KP_Insert:
784 // with any modifiers:
785 sp_gradient_context_add_stops_between_selected_stops (rc);
786 ret = TRUE;
787 break;
789 case GDK_Delete:
790 case GDK_KP_Delete:
791 case GDK_BackSpace:
792 if ( drag->selected ) {
793 drag->deleteSelected(MOD__CTRL_ONLY);
794 ret = TRUE;
795 }
796 break;
797 default:
798 break;
799 }
800 break;
801 case GDK_KEY_RELEASE:
802 switch (get_group0_keyval (&event->key)) {
803 case GDK_Alt_L:
804 case GDK_Alt_R:
805 case GDK_Control_L:
806 case GDK_Control_R:
807 case GDK_Shift_L:
808 case GDK_Shift_R:
809 case GDK_Meta_L: // Meta is when you press Shift+Alt
810 case GDK_Meta_R:
811 event_context->defaultMessageContext()->clear();
812 break;
813 default:
814 break;
815 }
816 break;
817 default:
818 break;
819 }
821 if (!ret) {
822 if (((SPEventContextClass *) parent_class)->root_handler) {
823 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
824 }
825 }
827 return ret;
828 }
830 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint /*state*/, guint32 etime)
831 {
832 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
833 Inkscape::Selection *selection = sp_desktop_selection(desktop);
834 SPDocument *document = sp_desktop_document(desktop);
835 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
837 if (!selection->isEmpty()) {
838 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
839 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
841 SPGradient *vector;
842 if (ec->item_to_select) {
843 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
844 } else {
845 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
846 }
848 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
849 SPCSSAttr *css = sp_repr_css_attr_new();
850 sp_repr_css_set_property(css, "fill-opacity", "1.0");
852 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
854 //FIXME: see above
855 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
857 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
859 if (type == SP_GRADIENT_TYPE_LINEAR) {
860 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
861 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
862 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
863 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
864 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
865 }
866 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
867 }
868 if (ec->_grdrag) {
869 ec->_grdrag->updateDraggers();
870 // prevent regenerating draggers by selection modified signal, which sometimes
871 // comes too late and thus destroys the knot which we will now grab:
872 ec->_grdrag->local_change = true;
873 // give the grab out-of-bounds values of xp/yp because we're already dragging
874 // and therefore are already out of tolerance
875 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
876 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
877 -1, // ignore number (though it is always 1)
878 fill_or_stroke, 99999, 99999, etime);
879 }
880 // We did an undoable action, but sp_document_done will be called by the knot when released
882 // status text; we do not track coords because this branch is run once, not all the time
883 // during drag
884 int n_objects = g_slist_length((GSList *) selection->itemList());
885 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
886 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
887 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
888 n_objects);
889 } else {
890 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
891 }
892 }
895 /*
896 Local Variables:
897 mode:c++
898 c-file-style:"stroustrup"
899 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
900 indent-tabs-mode:nil
901 fill-column:99
902 End:
903 */
904 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :