1 /*
2 * On-canvas gradient dragging
3 *
4 * Authors:
5 * bulia byak <buliabyak@users.sf.net>
6 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
7 * Jon A. Cruz <jon@joncruz.org>
8 *
9 * Copyright (C) 2007 Johan Engelen
10 * Copyright (C) 2005,2010 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
19 #include <glibmm/i18n.h>
20 #include <cstring>
21 #include <string>
23 #include "desktop-handles.h"
24 #include "selection.h"
25 #include "desktop.h"
26 #include "desktop-style.h"
27 #include "document.h"
28 #include "display/sp-ctrlline.h"
29 #include "display/sp-canvas-util.h"
30 #include "xml/repr.h"
31 #include "svg/css-ostringstream.h"
32 #include "svg/svg.h"
33 #include "libnr/nr-point-fns.h"
34 #include "preferences.h"
35 #include "sp-item.h"
36 #include "style.h"
37 #include "knot.h"
38 #include "sp-linear-gradient.h"
39 #include "sp-radial-gradient.h"
40 #include "gradient-chemistry.h"
41 #include "gradient-drag.h"
42 #include "sp-stop.h"
43 #include "snap.h"
44 #include "sp-namedview.h"
45 #include "selection-chemistry.h"
47 #define GR_KNOT_COLOR_NORMAL 0xffffff00
48 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
49 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
51 #define GR_LINE_COLOR_FILL 0x0000ff7f
52 #define GR_LINE_COLOR_STROKE 0x9999007f
54 // screen pixels between knots when they snap:
55 #define SNAP_DIST 5
57 // absolute distance between gradient points for them to become a single dragger when the drag is created:
58 #define MERGE_DIST 0.1
60 // knot shapes corresponding to GrPointType enum
61 SPKnotShapeType gr_knot_shapes [] = {
62 SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
63 SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END
64 SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
65 SP_KNOT_SHAPE_SQUARE, // POINT_RG_CENTER
66 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R1
67 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R2
68 SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
69 SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
70 SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
71 };
73 const gchar *gr_knot_descr [] = {
74 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
75 N_("Linear gradient <b>end</b>"),
76 N_("Linear gradient <b>mid stop</b>"),
77 N_("Radial gradient <b>center</b>"),
78 N_("Radial gradient <b>radius</b>"),
79 N_("Radial gradient <b>radius</b>"),
80 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
81 N_("Radial gradient <b>mid stop</b>"),
82 N_("Radial gradient <b>mid stop</b>")
83 };
85 static void
86 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
87 {
88 GrDrag *drag = (GrDrag *) data;
89 drag->updateDraggers ();
90 drag->updateLines ();
91 drag->updateLevels ();
92 }
94 static void
95 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
96 {
97 GrDrag *drag = (GrDrag *) data;
98 if (drag->local_change) {
99 drag->local_change = false;
100 } else {
101 drag->updateDraggers ();
102 }
103 drag->updateLines ();
104 drag->updateLevels ();
105 }
107 /**
108 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
109 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
110 any.
111 */
112 int
113 gr_drag_style_query (SPStyle *style, int property, gpointer data)
114 {
115 GrDrag *drag = (GrDrag *) data;
117 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
118 return QUERY_STYLE_NOTHING;
119 }
121 if (!drag->selected) {
122 return QUERY_STYLE_NOTHING;
123 } else {
124 int ret = QUERY_STYLE_NOTHING;
126 float cf[4];
127 cf[0] = cf[1] = cf[2] = cf[3] = 0;
129 int count = 0;
131 for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
132 GrDragger *d = (GrDragger *) i->data;
133 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
134 GrDraggable *draggable = (GrDraggable *) j->data;
136 if (ret == QUERY_STYLE_NOTHING) {
137 ret = QUERY_STYLE_SINGLE;
138 } else if (ret == QUERY_STYLE_SINGLE) {
139 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
140 }
142 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
143 cf[0] += SP_RGBA32_R_F (c);
144 cf[1] += SP_RGBA32_G_F (c);
145 cf[2] += SP_RGBA32_B_F (c);
146 cf[3] += SP_RGBA32_A_F (c);
148 count ++;
149 }
150 }
152 if (count) {
153 cf[0] /= count;
154 cf[1] /= count;
155 cf[2] /= count;
156 cf[3] /= count;
158 // set both fill and stroke with our stop-color and stop-opacity
159 style->fill.clear();
160 style->fill.setColor( cf[0], cf[1], cf[2] );
161 style->fill.set = TRUE;
162 style->stroke.clear();
163 style->stroke.setColor( cf[0], cf[1], cf[2] );
164 style->stroke.set = TRUE;
166 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
167 style->fill_opacity.set = TRUE;
168 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
169 style->stroke_opacity.set = TRUE;
171 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
172 style->opacity.set = TRUE;
173 }
175 return ret;
176 }
177 }
179 Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull )
180 {
181 Glib::ustring colorStr;
182 if ( str ) {
183 isNull = false;
184 colorStr = str;
185 Glib::ustring::size_type pos = colorStr.find("url(#");
186 if ( pos != Glib::ustring::npos ) {
187 Glib::ustring targetName = colorStr.substr(pos + 5, colorStr.length() - 6);
188 const GSList *gradients = sp_document_get_resource_list(desktop->doc(), "gradient");
189 for (const GSList *item = gradients; item; item = item->next) {
190 SPGradient* grad = SP_GRADIENT(item->data);
191 if ( targetName == grad->getId() ) {
192 SPGradient *vect = grad->getVector();
193 SPStop *firstStop = (vect) ? vect->getFirstStop() : grad->getFirstStop();
194 if (firstStop) {
195 Glib::ustring stopColorStr;
196 if (firstStop->currentColor) {
197 stopColorStr = sp_object_get_style_property(firstStop, "color", NULL);
198 } else {
199 stopColorStr = firstStop->specified_color.toString();
200 }
201 if ( !stopColorStr.empty() ) {
202 colorStr = stopColorStr;
203 }
204 }
205 break;
206 }
207 }
208 }
209 } else {
210 isNull = true;
211 }
213 return colorStr;
214 }
216 bool GrDrag::styleSet( const SPCSSAttr *css )
217 {
218 if (!selected) {
219 return false;
220 }
222 SPCSSAttr *stop = sp_repr_css_attr_new();
224 // See if the css contains interesting properties, and if so, translate them into the format
225 // acceptable for gradient stops
227 // any of color properties, in order of increasing priority:
228 if (css->attribute("flood-color")) {
229 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
230 }
232 if (css->attribute("lighting-color")) {
233 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
234 }
236 if (css->attribute("color")) {
237 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
238 }
240 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) {
241 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
242 }
244 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) {
245 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
246 }
248 if (css->attribute("stop-color")) {
249 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
250 }
252 // Make sure the style is allowed for gradient stops.
253 if ( !sp_repr_css_property_is_unset( stop, "stop-color") ) {
254 bool stopIsNull = false;
255 Glib::ustring tmp = makeStopSafeColor( sp_repr_css_property( stop, "stop-color", "" ), stopIsNull );
256 if ( !stopIsNull && !tmp.empty() ) {
257 sp_repr_css_set_property( stop, "stop-color", tmp.c_str() );
258 }
259 }
262 if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
263 sp_repr_css_set_property(stop, "stop-opacity", css->attribute("stop-opacity"));
264 } else { // multiply all opacity properties:
265 gdouble accumulated = 1.0;
266 accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
267 accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
268 accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
269 accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
271 Inkscape::CSSOStringStream os;
272 os << accumulated;
273 sp_repr_css_set_property(stop, "stop-opacity", os.str().c_str());
275 if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
276 (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none"))) {
277 sp_repr_css_set_property(stop, "stop-opacity", "0"); // if a single fill/stroke property is set to none, don't change color, set opacity to 0
278 }
279 }
281 if (!stop->attributeList()) { // nothing for us here, pass it on
282 sp_repr_css_attr_unref(stop);
283 return false;
284 }
286 for (GList const* sel = selected; sel != NULL; sel = sel->next) { // for all selected draggers
287 GrDragger* dragger = reinterpret_cast<GrDragger*>(sel->data);
288 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
289 GrDraggable *draggable = reinterpret_cast<GrDraggable *>(i->data);
291 local_change = true;
292 sp_item_gradient_stop_set_style(draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
293 }
294 }
296 //sp_repr_css_print(stop);
297 sp_repr_css_attr_unref(stop);
298 return true;
299 }
301 guint32 GrDrag::getColor()
302 {
303 if (!selected) return 0;
305 float cf[4];
306 cf[0] = cf[1] = cf[2] = cf[3] = 0;
308 int count = 0;
310 for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
311 GrDragger *d = (GrDragger *) i->data;
312 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
313 GrDraggable *draggable = (GrDraggable *) j->data;
315 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
316 cf[0] += SP_RGBA32_R_F (c);
317 cf[1] += SP_RGBA32_G_F (c);
318 cf[2] += SP_RGBA32_B_F (c);
319 cf[3] += SP_RGBA32_A_F (c);
321 count ++;
322 }
323 }
325 if (count) {
326 cf[0] /= count;
327 cf[1] /= count;
328 cf[2] /= count;
329 cf[3] /= count;
330 }
332 return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
333 }
335 SPStop *
336 GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance)
337 {
338 gfloat offset; // type of SPStop.offset = gfloat
339 SPGradient *gradient;
340 bool fill_or_stroke = true;
341 bool r1_knot = false;
343 bool addknot = false;
344 do {
345 gradient = sp_item_gradient (item, fill_or_stroke);
346 if (SP_IS_LINEARGRADIENT(gradient)) {
347 Geom::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
348 Geom::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
350 Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
351 double dist_screen = Geom::L2 (mouse_p - nearest);
352 if ( dist_screen < tolerance ) {
353 // add the knot
354 offset = get_offset_between_points(nearest, begin, end);
355 addknot = true;
356 break; // break out of the while loop: add only one knot
357 }
358 } else if (SP_IS_RADIALGRADIENT(gradient)) {
359 Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
360 Geom::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
361 Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
362 double dist_screen = Geom::L2 (mouse_p - nearest);
363 if ( dist_screen < tolerance ) {
364 offset = get_offset_between_points(nearest, begin, end);
365 addknot = true;
366 r1_knot = true;
367 break; // break out of the while loop: add only one knot
368 }
370 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
371 nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
372 dist_screen = Geom::L2 (mouse_p - nearest);
373 if ( dist_screen < tolerance ) {
374 offset = get_offset_between_points(nearest, begin, end);
375 addknot = true;
376 r1_knot = false;
377 break; // break out of the while loop: add only one knot
378 }
379 }
380 fill_or_stroke = !fill_or_stroke;
381 } while (!fill_or_stroke && !addknot) ;
383 if (addknot) {
384 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
385 SPStop* prev_stop = vector->getFirstStop();
386 SPStop* next_stop = prev_stop->getNextStop();
387 guint i = 1;
388 while ( (next_stop) && (next_stop->offset < offset) ) {
389 prev_stop = next_stop;
390 next_stop = next_stop->getNextStop();
391 i++;
392 }
393 if (!next_stop) {
394 // logical error: the endstop should have offset 1 and should always be more than this offset here
395 return NULL;
396 }
399 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
400 gradient->ensureVector();
401 updateDraggers();
403 return newstop;
404 }
406 return NULL;
407 }
410 bool
411 GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p)
412 {
413 // Note: not sure if a null pointer can come in for the style, but handle that just in case
414 bool stopIsNull = false;
415 Glib::ustring toUse = makeStopSafeColor( c, stopIsNull );
417 // first, see if we can drop onto one of the existing draggers
418 for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
419 GrDragger *d = (GrDragger *) i->data;
421 if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) {
422 SPCSSAttr *stop = sp_repr_css_attr_new ();
423 sp_repr_css_set_property( stop, "stop-color", stopIsNull ? 0 : toUse.c_str() );
424 sp_repr_css_set_property( stop, "stop-opacity", "1" );
425 for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
426 GrDraggable *draggable = (GrDraggable *) j->data;
427 local_change = true;
428 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
429 }
430 sp_repr_css_attr_unref(stop);
431 return true;
432 }
433 }
435 // now see if we're over line and create a new stop
436 bool over_line = false;
437 SPCtrlLine *line = NULL;
438 if (lines) {
439 for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
440 line = (SPCtrlLine*) l->data;
441 Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
442 double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom();
443 if (line->item && dist_screen < 5) {
444 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
445 if (stop) {
446 SPCSSAttr *css = sp_repr_css_attr_new ();
447 sp_repr_css_set_property( css, "stop-color", stopIsNull ? 0 : toUse.c_str() );
448 sp_repr_css_set_property( css, "stop-opacity", "1" );
449 sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
450 return true;
451 }
452 }
453 }
454 }
456 return false;
457 }
460 GrDrag::GrDrag(SPDesktop *desktop) :
461 selected(0),
462 keep_selection(false),
463 local_change(false),
464 desktop(desktop),
465 hor_levels(),
466 vert_levels(),
467 draggers(0),
468 lines(0),
469 selection(sp_desktop_selection(desktop)),
470 sel_changed_connection(),
471 sel_modified_connection(),
472 style_set_connection(),
473 style_query_connection()
474 {
475 sel_changed_connection = selection->connectChanged(
476 sigc::bind(
477 sigc::ptr_fun(&gr_drag_sel_changed),
478 (gpointer)this )
480 );
481 sel_modified_connection = selection->connectModified(
482 sigc::bind(
483 sigc::ptr_fun(&gr_drag_sel_modified),
484 (gpointer)this )
485 );
487 style_set_connection = desktop->connectSetStyle( sigc::mem_fun(*this, &GrDrag::styleSet) );
489 style_query_connection = desktop->connectQueryStyle(
490 sigc::bind(
491 sigc::ptr_fun(&gr_drag_style_query),
492 (gpointer)this )
493 );
495 updateDraggers();
496 updateLines();
497 updateLevels();
499 if (desktop->gr_item) {
500 setSelected(getDraggerFor(desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
501 }
502 }
504 GrDrag::~GrDrag()
505 {
506 this->sel_changed_connection.disconnect();
507 this->sel_modified_connection.disconnect();
508 this->style_set_connection.disconnect();
509 this->style_query_connection.disconnect();
511 if (this->selected) {
512 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
513 desktop->gr_item = draggable->item;
514 desktop->gr_point_type = draggable->point_type;
515 desktop->gr_point_i = draggable->point_i;
516 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
517 } else {
518 desktop->gr_item = NULL;
519 desktop->gr_point_type = 0;
520 desktop->gr_point_i = 0;
521 desktop->gr_fill_or_stroke = true;
522 }
524 deselect_all();
525 for (GList *l = this->draggers; l != NULL; l = l->next) {
526 delete ((GrDragger *) l->data);
527 }
528 g_list_free (this->draggers);
529 this->draggers = NULL;
530 this->selected = NULL;
532 for (GSList *l = this->lines; l != NULL; l = l->next) {
533 gtk_object_destroy( GTK_OBJECT (l->data));
534 }
535 g_slist_free (this->lines);
536 this->lines = NULL;
537 }
539 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
540 {
541 this->item = item;
542 this->point_type = point_type;
543 this->point_i = point_i;
544 this->fill_or_stroke = fill_or_stroke;
546 g_object_ref (G_OBJECT (this->item));
547 }
549 GrDraggable::~GrDraggable ()
550 {
551 g_object_unref (G_OBJECT (this->item));
552 }
555 SPObject *GrDraggable::getServer()
556 {
557 if (!item) {
558 return NULL;
559 }
561 SPObject *server = NULL;
562 if (fill_or_stroke) {
563 server = item->style->getFillPaintServer();
564 }else {
565 server = item->style->getStrokePaintServer();
566 }
568 return server;
569 }
571 static void
572 gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data)
573 {
574 GrDragger *dragger = (GrDragger *) data;
575 GrDrag *drag = dragger->parent;
577 Geom::Point p = ppointer;
579 SPDesktop *desktop = dragger->parent->desktop;
580 SnapManager &m = desktop->namedview->snap_manager;
581 double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom();
583 if (state & GDK_SHIFT_MASK) {
584 // with Shift; unsnap if we carry more than one draggable
585 if (dragger->draggables && dragger->draggables->next) {
586 // create a new dragger
587 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
588 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
589 // relink to it all but the first draggable in the list
590 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
591 GrDraggable *draggable = (GrDraggable *) i->data;
592 dr_new->addDraggable (draggable);
593 }
594 dr_new->updateKnotShape();
595 g_slist_free (dragger->draggables->next);
596 dragger->draggables->next = NULL;
597 dragger->updateKnotShape();
598 dragger->updateTip();
599 }
600 } else if (!(state & GDK_CONTROL_MASK)) {
601 // without Shift or Ctrl; see if we need to snap to another dragger
602 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
603 GrDragger *d_new = (GrDragger *) di->data;
604 if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) {
606 // Merge draggers:
607 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
608 GrDraggable *draggable = (GrDraggable *) i->data;
609 // copy draggable to d_new:
610 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
611 d_new->addDraggable (da_new);
612 }
614 // unlink and delete this dragger
615 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
616 delete dragger;
618 // update the new merged dragger
619 d_new->fireDraggables(true, false, true);
620 d_new->parent->updateLines();
621 d_new->parent->setSelected (d_new);
622 d_new->updateKnotShape ();
623 d_new->updateTip ();
624 d_new->updateDependencies(true);
625 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
626 _("Merge gradient handles"));
627 return;
628 }
629 }
630 }
632 if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) {
633 m.setup(desktop);
634 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
635 m.unSetup();
636 if (s.getSnapped()) {
637 p = s.getPoint();
638 sp_knot_moveto (knot, p);
639 }
640 } else if (state & GDK_CONTROL_MASK) {
641 SnappedConstraints sc;
642 Inkscape::SnapCandidatePoint scp = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
643 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
644 unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
645 /* 0 means no snapping. */
647 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
648 GrDraggable *draggable = (GrDraggable *) i->data;
650 Geom::Point dr_snap(Geom::infinity(), Geom::infinity());
652 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
653 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
654 GrDragger *d_new = (GrDragger *) di->data;
655 if (d_new == dragger)
656 continue;
657 if (d_new->isA (draggable->item,
658 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
659 draggable->fill_or_stroke)) {
660 // found the other end of the linear gradient;
661 if (state & GDK_SHIFT_MASK) {
662 // moving linear around center
663 Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point));
664 dr_snap = center;
665 } else {
666 // moving linear around the other end
667 dr_snap = d_new->point;
668 }
669 }
670 }
671 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
672 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
673 GrDragger *d_new = (GrDragger *) di->data;
674 if (d_new == dragger)
675 continue;
676 if (d_new->isA (draggable->item,
677 POINT_RG_CENTER,
678 draggable->fill_or_stroke)) {
679 // found the center of the radial gradient;
680 dr_snap = d_new->point;
681 }
682 }
683 } else if (draggable->point_type == POINT_RG_CENTER) {
684 // radial center snaps to hor/vert relative to its original position
685 dr_snap = dragger->point_original;
686 }
688 // dr_snap contains the origin of the gradient, whereas p will be the new endpoint which we will try to snap now
689 Inkscape::SnappedPoint sp;
690 if (dr_snap.isFinite()) {
691 m.setup(desktop);
692 if (state & GDK_MOD1_MASK) {
693 // with Alt, snap to the original angle and its perpendiculars
694 sp = m.constrainedAngularSnap(scp, dragger->point_original, dr_snap, 2);
695 } else {
696 // with Ctrl, snap to M_PI/snaps
697 sp = m.constrainedAngularSnap(scp, boost::optional<Geom::Point>(), dr_snap, snaps);
698 }
699 m.unSetup();
700 sc.points.push_back(sp);
701 }
702 }
704 m.setup(desktop, false); // turn of the snap indicator temporarily
705 Inkscape::SnappedPoint bsp = m.findBestSnap(scp, sc, true);
706 m.unSetup();
707 if (!bsp.getSnapped()) {
708 // If we didn't truly snap to an object or to a grid, then we will still have to look for the
709 // closest projection onto one of the constraints. findBestSnap() will not do this for us
710 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sc.points.begin(); i != sc.points.end(); i++) {
711 if (i == sc.points.begin() || (Geom::L2((*i).getPoint() - p) < Geom::L2(bsp.getPoint() - p))) {
712 bsp.setPoint((*i).getPoint());
713 bsp.setTarget(Inkscape::SNAPTARGET_CONSTRAINED_ANGLE);
714 }
715 }
716 }
717 //p = sc.points.front().getPoint();
718 p = bsp.getPoint();
719 sp_knot_moveto (knot, p);
720 }
722 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
723 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
725 if (drag->keep_selection) {
726 Geom::Point diff = p - dragger->point;
727 drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial);
728 } else {
729 dragger->point = p;
730 dragger->fireDraggables (false, scale_radial);
731 dragger->updateDependencies(false);
732 }
734 }
737 static void
738 gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving)
739 {
741 GrDrag *drag = dragger->parent;
742 // a midpoint dragger can (logically) only contain one GrDraggable
743 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
745 // get begin and end points between which dragging is allowed:
746 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
747 *moving = g_slist_append(*moving, dragger);
749 guint lowest_i = draggable->point_i;
750 guint highest_i = draggable->point_i;
751 GrDragger *lowest_dragger = dragger;
752 GrDragger *highest_dragger = dragger;
753 if (dragger->isSelected()) {
754 GrDragger* d_add;
755 while ( true )
756 {
757 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
758 if ( d_add && g_list_find(drag->selected, d_add) ) {
759 lowest_i = lowest_i - 1;
760 *moving = g_slist_prepend(*moving, d_add);
761 lowest_dragger = d_add;
762 } else {
763 break;
764 }
765 }
767 while ( true )
768 {
769 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
770 if ( d_add && g_list_find(drag->selected, d_add) ) {
771 highest_i = highest_i + 1;
772 *moving = g_slist_append(*moving, d_add);
773 highest_dragger = d_add;
774 } else {
775 break;
776 }
777 }
778 }
780 if ( SP_IS_LINEARGRADIENT(server) ) {
781 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
782 GrDragger *d_temp;
783 if (lowest_i == 1) {
784 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
785 } else {
786 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
787 }
788 if (d_temp)
789 *begin = d_temp->point;
791 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
792 if (d_temp == NULL) {
793 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
794 }
795 if (d_temp)
796 *end = d_temp->point;
797 } else if ( SP_IS_RADIALGRADIENT(server) ) {
798 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
799 GrDragger *d_temp;
800 if (lowest_i == 1) {
801 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
802 } else {
803 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
804 }
805 if (d_temp)
806 *begin = d_temp->point;
808 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
809 if (d_temp == NULL) {
810 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
811 }
812 if (d_temp)
813 *end = d_temp->point;
814 }
816 *low_lim = dragger->point - (lowest_dragger->point - *begin);
817 *high_lim = dragger->point - (highest_dragger->point - *end);
818 }
822 /**
823 Called when a midpoint knot is dragged.
824 */
825 static void
826 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state, gpointer data)
827 {
828 GrDragger *dragger = (GrDragger *) data;
829 GrDrag *drag = dragger->parent;
830 // a midpoint dragger can (logically) only contain one GrDraggable
831 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
833 // FIXME: take from prefs
834 double snap_fraction = 0.1;
836 Geom::Point p = ppointer;
837 Geom::Point begin(0,0), end(0,0);
838 Geom::Point low_lim(0,0), high_lim(0,0);
840 SPObject *server = draggable->getServer();
842 GSList *moving = NULL;
843 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
845 if (state & GDK_CONTROL_MASK) {
846 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
847 } else {
848 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
849 if (!(state & GDK_SHIFT_MASK)) {
850 Inkscape::Snapper::SnapConstraint cl(low_lim, high_lim - low_lim);
851 SPDesktop *desktop = dragger->parent->desktop;
852 SnapManager &m = desktop->namedview->snap_manager;
853 m.setup(desktop);
854 m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl);
855 m.unSetup();
856 }
857 }
858 Geom::Point displacement = p - dragger->point;
860 for (GSList const* i = moving; i != NULL; i = i->next) {
861 GrDragger *drg = (GrDragger*) i->data;
862 SPKnot *drgknot = drg->knot;
863 Geom::Point this_move = displacement;
864 if (state & GDK_MOD1_MASK) {
865 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
866 double alpha = 1.0;
867 if (Geom::L2(drg->point - dragger->point) + Geom::L2(drg->point - begin) - 1e-3 > Geom::L2(dragger->point - begin)) { // drg is on the end side from dragger
868 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point);
869 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
870 } else { // drg is on the begin side from dragger
871 double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point);
872 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
873 }
874 }
875 drg->point += this_move;
876 sp_knot_moveto (drgknot, drg->point);
877 drg->fireDraggables (false);
878 drg->updateDependencies(false);
879 }
881 g_slist_free(moving);
883 drag->keep_selection = dragger->isSelected();
884 }
888 static void
889 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
890 {
891 GrDragger *dragger = (GrDragger *) data;
893 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
894 }
896 /**
897 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
898 */
899 static void
900 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
901 {
902 GrDragger *dragger = (GrDragger *) data;
904 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
906 dragger->point_original = dragger->point = knot->pos;
908 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
909 dragger->fireDraggables (true, true);
910 } else {
911 dragger->fireDraggables (true);
912 }
914 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
915 GrDragger *d = (GrDragger *) i->data;
916 if (d == dragger)
917 continue;
918 d->fireDraggables (true);
919 }
921 // make this dragger selected
922 if (!dragger->parent->keep_selection) {
923 dragger->parent->setSelected (dragger);
924 }
925 dragger->parent->keep_selection = false;
927 dragger->updateDependencies(true);
929 // we did an undoable action
930 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
931 _("Move gradient handle"));
932 }
934 /**
935 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
936 state of the keyboard keys
937 */
938 static void
939 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
940 {
941 GrDragger *dragger = (GrDragger *) data;
942 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
943 if (!draggable) return;
945 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
946 // delete this knot from vector
947 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
948 gradient = gradient->getVector();
949 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
950 SPStop *stop = NULL;
951 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
952 case POINT_LG_BEGIN:
953 case POINT_RG_CENTER:
954 stop = gradient->getFirstStop();
955 {
956 SPStop *next = stop->getNextStop();
957 if (next) {
958 next->offset = 0;
959 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
960 }
961 }
962 break;
963 case POINT_LG_END:
964 case POINT_RG_R1:
965 case POINT_RG_R2:
966 stop = sp_last_stop(gradient);
967 {
968 SPStop *prev = stop->getPrevStop();
969 if (prev) {
970 prev->offset = 1;
971 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
972 }
973 }
974 break;
975 case POINT_LG_MID:
976 case POINT_RG_MID1:
977 case POINT_RG_MID2:
978 stop = sp_get_stop_i(gradient, draggable->point_i);
979 break;
980 }
982 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
983 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
984 _("Delete gradient stop"));
985 }
986 } else {
987 // select the dragger
988 dragger->point_original = dragger->point;
990 if ( state & GDK_SHIFT_MASK ) {
991 dragger->parent->setSelected (dragger, true, false);
992 } else {
993 dragger->parent->setSelected (dragger);
994 }
995 }
996 }
998 /**
999 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
1000 */
1001 static void
1002 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
1003 {
1004 GrDragger *dragger = (GrDragger *) data;
1006 dragger->point_original = dragger->point;
1008 if (dragger->draggables == NULL)
1009 return;
1011 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1012 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1013 }
1015 /**
1016 Act upon all draggables of the dragger, setting them to the dragger's point
1017 */
1018 void
1019 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1020 {
1021 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1022 GrDraggable *draggable = (GrDraggable *) i->data;
1024 // set local_change flag so that selection_changed callback does not regenerate draggers
1025 this->parent->local_change = true;
1027 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1028 // to the center, unless it's the first update upon merge when we must snap it to the point
1029 if (merging_focus ||
1030 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1031 {
1032 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1033 }
1034 }
1035 }
1037 /**
1038 Checks if the dragger has a draggable with this point_type
1039 */
1040 bool
1041 GrDragger::isA (gint point_type)
1042 {
1043 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1044 GrDraggable *draggable = (GrDraggable *) i->data;
1045 if (draggable->point_type == point_type) {
1046 return true;
1047 }
1048 }
1049 return false;
1050 }
1052 /**
1053 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1054 */
1055 bool
1056 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1057 {
1058 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1059 GrDraggable *draggable = (GrDraggable *) i->data;
1060 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1061 return true;
1062 }
1063 }
1064 return false;
1065 }
1067 /**
1068 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1069 */
1070 bool
1071 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1072 {
1073 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1074 GrDraggable *draggable = (GrDraggable *) i->data;
1075 if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1076 return true;
1077 }
1078 }
1079 return false;
1080 }
1082 bool
1083 GrDraggable::mayMerge (GrDraggable *da2)
1084 {
1085 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1086 // we must not merge the points of the same gradient!
1087 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1088 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1089 // except that we can snap center and focus together
1090 return false;
1091 }
1092 }
1093 // disable merging of midpoints.
1094 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1095 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1096 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1097 return false;
1099 return true;
1100 }
1102 bool
1103 GrDragger::mayMerge (GrDragger *other)
1104 {
1105 if (this == other)
1106 return false;
1108 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1109 GrDraggable *da1 = (GrDraggable *) i->data;
1110 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1111 GrDraggable *da2 = (GrDraggable *) j->data;
1112 if (!da1->mayMerge(da2))
1113 return false;
1114 }
1115 }
1116 return true;
1117 }
1119 bool
1120 GrDragger::mayMerge (GrDraggable *da2)
1121 {
1122 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1123 GrDraggable *da1 = (GrDraggable *) i->data;
1124 if (!da1->mayMerge(da2))
1125 return false;
1126 }
1127 return true;
1128 }
1130 /**
1131 Updates the statusbar tip of the dragger knot, based on its draggables
1132 */
1133 void
1134 GrDragger::updateTip ()
1135 {
1136 if (this->knot && this->knot->tip) {
1137 g_free (this->knot->tip);
1138 this->knot->tip = NULL;
1139 }
1141 if (g_slist_length (this->draggables) == 1) {
1142 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1143 char *item_desc = sp_item_description(draggable->item);
1144 switch (draggable->point_type) {
1145 case POINT_LG_MID:
1146 case POINT_RG_MID1:
1147 case POINT_RG_MID2:
1148 this->knot->tip = g_strdup_printf (_("%s %d for: %s%s; drag with <b>Ctrl</b> to snap offset; click with <b>Ctrl+Alt</b> to delete stop"),
1149 _(gr_knot_descr[draggable->point_type]),
1150 draggable->point_i,
1151 item_desc,
1152 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1153 break;
1155 default:
1156 this->knot->tip = g_strdup_printf (_("%s for: %s%s; drag with <b>Ctrl</b> to snap angle, with <b>Ctrl+Alt</b> to preserve angle, with <b>Ctrl+Shift</b> to scale around center"),
1157 _(gr_knot_descr[draggable->point_type]),
1158 item_desc,
1159 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1160 break;
1161 }
1162 g_free(item_desc);
1163 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1164 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1165 } else {
1166 int length = g_slist_length (this->draggables);
1167 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1168 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1169 length),
1170 length);
1171 }
1172 }
1174 /**
1175 Adds a draggable to the dragger
1176 */
1177 void
1178 GrDragger::updateKnotShape ()
1179 {
1180 if (!draggables)
1181 return;
1182 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1183 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1184 }
1186 /**
1187 Adds a draggable to the dragger
1188 */
1189 void
1190 GrDragger::addDraggable (GrDraggable *draggable)
1191 {
1192 this->draggables = g_slist_prepend (this->draggables, draggable);
1194 this->updateTip();
1195 }
1198 /**
1199 Moves this dragger to the point of the given draggable, acting upon all other draggables
1200 */
1201 void
1202 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1203 {
1204 GrDraggable *dr_first = (GrDraggable *) this->draggables->data;
1205 if (!dr_first) return;
1207 this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke);
1208 this->point_original = this->point;
1210 sp_knot_moveto (this->knot, this->point);
1212 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1213 GrDraggable *da = (GrDraggable *) i->data;
1214 if ( (da->item == item) &&
1215 (point_type == -1 || da->point_type == point_type) &&
1216 (point_i == -1 || da->point_i == point_i) &&
1217 (da->fill_or_stroke == fill_or_stroke) ) {
1218 continue;
1219 }
1220 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1221 }
1222 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1223 }
1226 /**
1227 Moves all midstop draggables that depend on this one
1228 */
1229 void
1230 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1231 SPObject *server = draggable->getServer();
1232 if (!server)
1233 return;
1234 guint num = SP_GRADIENT(server)->vector.stops.size();
1235 if (num <= 2) return;
1237 if ( SP_IS_LINEARGRADIENT(server) ) {
1238 for ( guint i = 1; i < num - 1; i++ ) {
1239 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1240 }
1241 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1242 for ( guint i = 1; i < num - 1; i++ ) {
1243 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1244 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1245 }
1246 }
1247 }
1250 /**
1251 Moves all draggables that depend on this one
1252 */
1253 void
1254 GrDragger::updateDependencies (bool write_repr)
1255 {
1256 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1257 GrDraggable *draggable = (GrDraggable *) i->data;
1258 switch (draggable->point_type) {
1259 case POINT_LG_BEGIN:
1260 {
1261 // the end point is dependent only when dragging with ctrl+shift
1262 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1264 this->updateMidstopDependencies (draggable, write_repr);
1265 }
1266 break;
1267 case POINT_LG_END:
1268 {
1269 // the begin point is dependent only when dragging with ctrl+shift
1270 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1272 this->updateMidstopDependencies (draggable, write_repr);
1273 }
1274 break;
1275 case POINT_LG_MID:
1276 // no other nodes depend on mid points.
1277 break;
1278 case POINT_RG_R2:
1279 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1280 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1281 this->updateMidstopDependencies (draggable, write_repr);
1282 break;
1283 case POINT_RG_R1:
1284 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1285 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1286 this->updateMidstopDependencies (draggable, write_repr);
1287 break;
1288 case POINT_RG_CENTER:
1289 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1290 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1291 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1292 this->updateMidstopDependencies (draggable, write_repr);
1293 break;
1294 case POINT_RG_FOCUS:
1295 // nothing can depend on that
1296 break;
1297 case POINT_RG_MID1:
1298 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1299 break;
1300 case POINT_RG_MID2:
1301 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1302 break;
1303 default:
1304 break;
1305 }
1306 }
1307 }
1311 GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable)
1312 : point(p),
1313 point_original(p)
1314 {
1315 this->draggables = NULL;
1317 this->parent = parent;
1319 // create the knot
1320 this->knot = sp_knot_new (parent->desktop, NULL);
1321 this->knot->setMode(SP_KNOT_MODE_XOR);
1322 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1323 this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f);
1324 sp_knot_update_ctrl(this->knot);
1326 // move knot to the given point
1327 sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL);
1328 sp_knot_show (this->knot);
1330 // connect knot's signals
1331 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1332 // luckily, midstops never snap to other nodes so are never unsnapped...
1333 && ( (draggable->point_type == POINT_LG_MID)
1334 || (draggable->point_type == POINT_RG_MID1)
1335 || (draggable->point_type == POINT_RG_MID2) ) )
1336 {
1337 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1338 } else {
1339 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1340 }
1341 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1342 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1343 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1344 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1346 // add the initial draggable
1347 if (draggable)
1348 this->addDraggable (draggable);
1349 updateKnotShape();
1350 }
1352 GrDragger::~GrDragger ()
1353 {
1354 // unselect if it was selected
1355 this->parent->setDeselected(this);
1357 // disconnect signals
1358 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1359 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1360 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1361 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1362 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1364 /* unref should call destroy */
1365 g_object_unref (G_OBJECT (this->knot));
1367 // delete all draggables
1368 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1369 delete ((GrDraggable *) i->data);
1370 }
1371 g_slist_free (this->draggables);
1372 this->draggables = NULL;
1373 }
1375 /**
1376 Select the dragger which has the given draggable.
1377 */
1378 GrDragger *
1379 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1380 {
1381 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1382 GrDragger *dragger = (GrDragger *) i->data;
1383 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1384 GrDraggable *da2 = (GrDraggable *) j->data;
1385 if ( (da2->item == item) &&
1386 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1387 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1388 (da2->fill_or_stroke == fill_or_stroke)) {
1389 return (dragger);
1390 }
1391 }
1392 }
1393 return NULL;
1394 }
1397 void
1398 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1399 {
1400 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1401 if (d && d != this) {
1402 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1403 }
1404 }
1407 /**
1408 Draw this dragger as selected
1409 */
1410 void
1411 GrDragger::select()
1412 {
1413 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1414 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1415 }
1417 /**
1418 Draw this dragger as normal (deselected)
1419 */
1420 void
1421 GrDragger::deselect()
1422 {
1423 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1424 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1425 }
1427 bool
1428 GrDragger::isSelected()
1429 {
1430 return g_list_find (parent->selected, this);
1431 }
1433 /**
1434 \brief Deselect all stops/draggers (private)
1435 */
1436 void
1437 GrDrag::deselect_all()
1438 {
1439 while (selected) {
1440 ( (GrDragger*) selected->data)->deselect();
1441 selected = g_list_remove(selected, selected->data);
1442 }
1443 }
1445 /**
1446 \brief Deselect all stops/draggers (public; emits signal)
1447 */
1448 void
1449 GrDrag::deselectAll()
1450 {
1451 deselect_all();
1452 this->desktop->emitToolSubselectionChanged(NULL);
1453 }
1455 /**
1456 \brief Select all stops/draggers
1457 */
1458 void
1459 GrDrag::selectAll()
1460 {
1461 for (GList *l = this->draggers; l != NULL; l = l->next) {
1462 GrDragger *d = ((GrDragger *) l->data);
1463 setSelected (d, true, true);
1464 }
1465 }
1467 /**
1468 \brief Select all stops/draggers that match the coords
1469 */
1470 void
1471 GrDrag::selectByCoords(std::vector<Geom::Point> coords)
1472 {
1473 for (GList *l = this->draggers; l != NULL; l = l->next) {
1474 GrDragger *d = ((GrDragger *) l->data);
1475 for (guint k = 0; k < coords.size(); k++) {
1476 if (Geom::L2 (d->point - coords[k]) < 1e-4) {
1477 setSelected (d, true, true);
1478 }
1479 }
1480 }
1481 }
1484 /**
1485 \brief Select all stops/draggers that fall within the rect
1486 */
1487 void
1488 GrDrag::selectRect(Geom::Rect const &r)
1489 {
1490 for (GList *l = this->draggers; l != NULL; l = l->next) {
1491 GrDragger *d = ((GrDragger *) l->data);
1492 if (r.contains(d->point)) {
1493 setSelected (d, true, true);
1494 }
1495 }
1496 }
1498 /**
1499 \brief Select a dragger
1500 \param dragger The dragger to select
1501 \param add_to_selection If true, add to selection, otherwise deselect others
1502 \param override If true, always select this node, otherwise toggle selected status
1503 */
1504 void
1505 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1506 {
1507 GrDragger *seldragger = NULL;
1509 if (add_to_selection) {
1510 if (!dragger) return;
1511 if (override) {
1512 if (!g_list_find(selected, dragger)) {
1513 selected = g_list_prepend(selected, dragger);
1514 }
1515 dragger->select();
1516 seldragger = dragger;
1517 } else { // toggle
1518 if (g_list_find(selected, dragger)) {
1519 selected = g_list_remove(selected, dragger);
1520 dragger->deselect();
1521 if (selected) {
1522 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1523 }
1524 } else {
1525 selected = g_list_prepend(selected, dragger);
1526 dragger->select();
1527 seldragger = dragger;
1528 }
1529 }
1530 } else {
1531 deselect_all();
1532 if (dragger) {
1533 selected = g_list_prepend(selected, dragger);
1534 dragger->select();
1535 seldragger = dragger;
1536 }
1537 }
1538 if (seldragger) {
1539 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1540 }
1541 }
1543 /**
1544 \brief Deselect a dragger
1545 \param dragger The dragger to deselect
1546 */
1547 void
1548 GrDrag::setDeselected (GrDragger *dragger)
1549 {
1550 if (g_list_find(selected, dragger)) {
1551 selected = g_list_remove(selected, dragger);
1552 dragger->deselect();
1553 }
1554 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1555 }
1559 /**
1560 Create a line from p1 to p2 and add it to the lines list
1561 */
1562 void
1563 GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba)
1564 {
1565 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1566 SP_TYPE_CTRLLINE, NULL);
1567 sp_canvas_item_move_to_z(line, 0);
1568 SP_CTRLLINE(line)->item = item;
1569 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1570 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1571 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1572 sp_canvas_item_show (line);
1573 this->lines = g_slist_append (this->lines, line);
1574 }
1576 /**
1577 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1578 new dragger and add it to draggers list
1579 */
1580 void
1581 GrDrag::addDragger (GrDraggable *draggable)
1582 {
1583 Geom::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1585 for (GList *i = this->draggers; i != NULL; i = i->next) {
1586 GrDragger *dragger = (GrDragger *) i->data;
1587 if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) {
1588 // distance is small, merge this draggable into dragger, no need to create new dragger
1589 dragger->addDraggable (draggable);
1590 dragger->updateKnotShape();
1591 return;
1592 }
1593 }
1595 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1596 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1597 this->draggers = g_list_append (this->draggers, new_dragger);
1598 }
1600 /**
1601 Add draggers for the radial gradient rg on item
1602 */
1603 void
1604 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1605 {
1606 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1607 guint num = rg->vector.stops.size();
1608 if (num > 2) {
1609 for ( guint i = 1; i < num - 1; i++ ) {
1610 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1611 }
1612 }
1613 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1614 if (num > 2) {
1615 for ( guint i = 1; i < num - 1; i++ ) {
1616 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1617 }
1618 }
1619 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1620 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1621 }
1623 /**
1624 Add draggers for the linear gradient lg on item
1625 */
1626 void
1627 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1628 {
1629 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1630 guint num = lg->vector.stops.size();
1631 if (num > 2) {
1632 for ( guint i = 1; i < num - 1; i++ ) {
1633 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1634 }
1635 }
1636 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1637 }
1639 /**
1640 Artificially grab the knot of this dragger; used by the gradient context
1641 */
1642 void
1643 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1644 {
1645 if (dragger) {
1646 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1647 }
1648 }
1650 /**
1651 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1652 */
1653 void
1654 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1655 {
1656 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1657 if (dragger) {
1658 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1659 }
1660 }
1662 /**
1663 Regenerates the draggers list from the current selection; is called when selection is changed or
1664 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1665 */
1666 void GrDrag::updateDraggers ()
1667 {
1668 while (selected) {
1669 selected = g_list_remove(selected, selected->data);
1670 }
1671 // delete old draggers
1672 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1673 delete static_cast<GrDragger *>(i->data);
1674 }
1675 g_list_free(this->draggers);
1676 this->draggers = NULL;
1678 g_return_if_fail(this->selection != NULL);
1680 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1681 SPItem *item = SP_ITEM(i->data);
1682 SPStyle *style = item->style;
1684 if (style && (style->fill.isPaintserver())) {
1685 SPPaintServer *server = style->getFillPaintServer();
1686 if ( server && server->isSolid() ) {
1687 // Suppress "gradientness" of solid paint
1688 } else if ( SP_IS_LINEARGRADIENT(server) ) {
1689 addDraggersLinear( SP_LINEARGRADIENT(server), item, true );
1690 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1691 addDraggersRadial( SP_RADIALGRADIENT(server), item, true );
1692 }
1693 }
1695 if (style && (style->stroke.isPaintserver())) {
1696 SPPaintServer *server = style->getStrokePaintServer();
1697 if ( server && server->isSolid() ) {
1698 // Suppress "gradientness" of solid paint
1699 } else if ( SP_IS_LINEARGRADIENT(server) ) {
1700 addDraggersLinear( SP_LINEARGRADIENT(server), item, false );
1701 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1702 addDraggersRadial( SP_RADIALGRADIENT(server), item, false );
1703 }
1704 }
1705 }
1706 }
1709 /**
1710 * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it
1711 */
1713 bool
1714 GrDrag::mouseOver()
1715 {
1716 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1717 GrDragger *d = (GrDragger *) i->data;
1718 if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) {
1719 return true;
1720 }
1721 }
1722 return false;
1723 }
1724 /**
1725 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1726 lines are always in sync with the actual gradient
1727 */
1728 void
1729 GrDrag::updateLines ()
1730 {
1731 // delete old lines
1732 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1733 gtk_object_destroy( GTK_OBJECT (i->data));
1734 }
1735 g_slist_free (this->lines);
1736 this->lines = NULL;
1738 g_return_if_fail (this->selection != NULL);
1740 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1742 SPItem *item = SP_ITEM(i->data);
1744 SPStyle *style = SP_OBJECT_STYLE (item);
1746 if (style && (style->fill.isPaintserver())) {
1747 SPPaintServer *server = item->style->getFillPaintServer();
1748 if ( server && server->isSolid() ) {
1749 // Suppress "gradientness" of solid paint
1750 } else if ( SP_IS_LINEARGRADIENT(server) ) {
1751 this->addLine (item, sp_item_gradient_get_coords (item, POINT_LG_BEGIN, 0, true), sp_item_gradient_get_coords (item, POINT_LG_END, 0, true), GR_LINE_COLOR_FILL);
1752 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1753 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1754 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1755 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1756 }
1757 }
1759 if (style && (style->stroke.isPaintserver())) {
1760 SPPaintServer *server = item->style->getStrokePaintServer();
1761 if ( server && server->isSolid() ) {
1762 // Suppress "gradientness" of solid paint
1763 } else if ( SP_IS_LINEARGRADIENT(server) ) {
1764 this->addLine (item, sp_item_gradient_get_coords (item, POINT_LG_BEGIN, 0, false), sp_item_gradient_get_coords (item, POINT_LG_END, 0, false), GR_LINE_COLOR_STROKE);
1765 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1766 Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1767 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1768 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1769 }
1770 }
1771 }
1772 }
1774 /**
1775 Regenerates the levels list from the current selection
1776 */
1777 void
1778 GrDrag::updateLevels ()
1779 {
1780 hor_levels.clear();
1781 vert_levels.clear();
1783 g_return_if_fail (this->selection != NULL);
1785 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1786 SPItem *item = SP_ITEM(i->data);
1787 Geom::OptRect rect = sp_item_bbox_desktop (item);
1788 if (rect) {
1789 // Remember the edges of the bbox and the center axis
1790 hor_levels.push_back(rect->min()[Geom::Y]);
1791 hor_levels.push_back(rect->max()[Geom::Y]);
1792 hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y]));
1793 vert_levels.push_back(rect->min()[Geom::X]);
1794 vert_levels.push_back(rect->max()[Geom::X]);
1795 vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X]));
1796 }
1797 }
1798 }
1800 void
1801 GrDrag::selected_reverse_vector ()
1802 {
1803 if (selected == NULL)
1804 return;
1806 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1807 GrDraggable *draggable = (GrDraggable *) i->data;
1809 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1810 }
1811 }
1813 void
1814 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1815 {
1816 selected_move (x, y, false, scale_radial);
1817 }
1819 void
1820 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1821 {
1822 if (selected == NULL)
1823 return;
1825 bool did = false;
1827 for (GList *i = selected; i != NULL; i = i->next) {
1828 GrDragger *d = (GrDragger *) i->data;
1830 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1831 // if this is an endpoint,
1833 // Moving an rg center moves its focus and radii as well.
1834 // therefore, if this is a focus or radius and if selection
1835 // contains the center as well, do not move this one
1836 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1837 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1838 bool skip_radius_with_center = false;
1839 for (GList *di = selected; di != NULL; di = di->next) {
1840 GrDragger *d_new = (GrDragger *) di->data;
1841 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1842 POINT_RG_CENTER,
1843 0,
1844 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1845 // FIXME: here we take into account only the first draggable!
1846 skip_radius_with_center = true;
1847 }
1848 }
1849 if (skip_radius_with_center)
1850 continue;
1851 }
1853 did = true;
1854 d->point += Geom::Point (x, y);
1855 d->point_original = d->point;
1856 sp_knot_moveto (d->knot, d->point);
1858 d->fireDraggables (write_repr, scale_radial);
1860 d->updateDependencies(write_repr);
1861 }
1862 }
1864 if (write_repr && did) {
1865 // we did an undoable action
1866 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1867 _("Move gradient handle(s)"));
1868 return;
1869 }
1871 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1873 GrDragger *dragger = (GrDragger *) selected->data;
1874 // a midpoint dragger can (logically) only contain one GrDraggable
1875 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1877 Geom::Point begin(0,0), end(0,0);
1878 Geom::Point low_lim(0,0), high_lim(0,0);
1880 SPObject *server = draggable->getServer();
1881 GSList *moving = NULL;
1882 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1884 Geom::Point p(x, y);
1885 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1886 Geom::Point displacement = p - dragger->point;
1888 for (GSList const* i = moving; i != NULL; i = i->next) {
1889 GrDragger *drg = (GrDragger*) i->data;
1890 SPKnot *drgknot = drg->knot;
1891 drg->point += displacement;
1892 sp_knot_moveto (drgknot, drg->point);
1893 drg->fireDraggables (true);
1894 drg->updateDependencies(true);
1895 did = true;
1896 }
1898 g_slist_free(moving);
1900 if (write_repr && did) {
1901 // we did an undoable action
1902 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1903 _("Move gradient mid stop(s)"));
1904 }
1905 }
1906 }
1908 void
1909 GrDrag::selected_move_screen (double x, double y)
1910 {
1911 gdouble zoom = desktop->current_zoom();
1912 gdouble zx = x / zoom;
1913 gdouble zy = y / zoom;
1915 selected_move (zx, zy);
1916 }
1918 /**
1919 Select the knot next to the last selected one and deselect all other selected.
1920 */
1921 GrDragger *
1922 GrDrag::select_next ()
1923 {
1924 GrDragger *d = NULL;
1925 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1926 if (draggers)
1927 d = (GrDragger *) draggers->data;
1928 } else {
1929 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1930 }
1931 if (d)
1932 setSelected (d);
1933 return d;
1934 }
1936 /**
1937 Select the knot previous from the last selected one and deselect all other selected.
1938 */
1939 GrDragger *
1940 GrDrag::select_prev ()
1941 {
1942 GrDragger *d = NULL;
1943 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1944 if (draggers)
1945 d = (GrDragger *) g_list_last (draggers)->data;
1946 } else {
1947 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1948 }
1949 if (d)
1950 setSelected (d);
1951 return d;
1952 }
1955 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1956 void
1957 GrDrag::deleteSelected (bool just_one)
1958 {
1959 if (!selected) return;
1961 SPDocument *document = false;
1963 struct StructStopInfo {
1964 SPStop * spstop;
1965 GrDraggable * draggable;
1966 SPGradient * gradient;
1967 SPGradient * vector;
1968 };
1970 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1971 GSList *endstoplist = NULL; // list of stops that must be deleted
1972 while (selected) {
1973 GrDragger *dragger = (GrDragger*) selected->data;
1974 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1975 GrDraggable *draggable = (GrDraggable*) drgble->data;
1976 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1977 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1979 switch (draggable->point_type) {
1980 case POINT_LG_MID:
1981 case POINT_RG_MID1:
1982 case POINT_RG_MID2:
1983 {
1984 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1985 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1986 bool present = false;
1987 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1988 if ( (SPStop*)l->data == stop ) {
1989 present = true;
1990 break; // no need to search further.
1991 }
1992 }
1993 if (!present)
1994 midstoplist = g_slist_append(midstoplist, stop);
1995 }
1996 break;
1997 case POINT_LG_BEGIN:
1998 case POINT_LG_END:
1999 case POINT_RG_CENTER:
2000 case POINT_RG_R1:
2001 case POINT_RG_R2:
2002 {
2003 SPStop *stop = NULL;
2004 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
2005 stop = vector->getFirstStop();
2006 } else {
2007 stop = sp_last_stop(vector);
2008 }
2009 if (stop) {
2010 StructStopInfo *stopinfo = new StructStopInfo;
2011 stopinfo->spstop = stop;
2012 stopinfo->draggable = draggable;
2013 stopinfo->gradient = gradient;
2014 stopinfo->vector = vector;
2015 // check if already present in list. (e.g. when both R1 and R2 were selected)
2016 bool present = false;
2017 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
2018 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
2019 present = true;
2020 break; // no need to search further.
2021 }
2022 }
2023 if (!present)
2024 endstoplist = g_slist_append(endstoplist, stopinfo);
2025 }
2026 }
2027 break;
2028 default:
2029 break;
2030 }
2031 }
2032 selected = g_list_remove(selected, dragger);
2033 if ( just_one ) break; // iterate once if just_one is set.
2034 }
2035 while (midstoplist) {
2036 SPStop *stop = (SPStop*) midstoplist->data;
2037 document = SP_OBJECT_DOCUMENT (stop);
2038 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2039 parent->removeChild(SP_OBJECT_REPR(stop));
2040 midstoplist = g_slist_remove(midstoplist, stop);
2041 }
2042 while (endstoplist) {
2043 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
2044 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2046 // 2 is the minimum, cannot delete more than that without deleting the whole vector
2047 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2048 // manually count the children, don't know if there already exists a function for this...
2049 int len = 0;
2050 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2051 child != NULL ;
2052 child = SP_OBJECT_NEXT(child) )
2053 {
2054 if ( SP_IS_STOP(child) ) len ++;
2055 }
2056 if (len > 2)
2057 {
2058 switch (stopinfo->draggable->point_type) {
2059 case POINT_LG_BEGIN:
2060 {
2061 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2063 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2064 Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed);
2065 Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed);
2066 SPStop *stop = stopinfo->vector->getFirstStop();
2067 gdouble offset = stop->offset;
2068 Geom::Point newbegin = oldbegin + offset * (end - oldbegin);
2069 lg->x1.computed = newbegin[Geom::X];
2070 lg->y1.computed = newbegin[Geom::Y];
2072 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2073 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2074 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2075 stop->offset = 0;
2076 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2078 // iterate through midstops to set new offset values such that they won't move on canvas.
2079 SPStop *laststop = sp_last_stop(stopinfo->vector);
2080 stop = stop->getNextStop();
2081 while ( stop != laststop ) {
2082 stop->offset = (stop->offset - offset)/(1 - offset);
2083 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2084 stop = stop->getNextStop();
2085 }
2086 }
2087 break;
2088 case POINT_LG_END:
2089 {
2090 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2092 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2093 Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed);
2094 Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed);
2095 SPStop *laststop = sp_last_stop(stopinfo->vector);
2096 gdouble offset = laststop->offset;
2097 Geom::Point newend = begin + offset * (oldend - begin);
2098 lg->x2.computed = newend[Geom::X];
2099 lg->y2.computed = newend[Geom::Y];
2101 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2102 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2103 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2104 laststop->offset = 1;
2105 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2107 // iterate through midstops to set new offset values such that they won't move on canvas.
2108 SPStop *stop = stopinfo->vector->getFirstStop();
2109 stop = stop->getNextStop();
2110 while ( stop != laststop ) {
2111 stop->offset = stop->offset / offset;
2112 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2113 stop = stop->getNextStop();
2114 }
2115 }
2116 break;
2117 case POINT_RG_CENTER:
2118 {
2119 SPStop *newfirst = stopinfo->spstop->getNextStop();
2120 if (newfirst) {
2121 newfirst->offset = 0;
2122 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2123 }
2124 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2125 }
2126 break;
2127 case POINT_RG_R1:
2128 case POINT_RG_R2:
2129 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2131 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2132 double oldradius = rg->r.computed;
2133 SPStop *laststop = sp_last_stop(stopinfo->vector);
2134 gdouble offset = laststop->offset;
2135 double newradius = offset * oldradius;
2136 rg->r.computed = newradius;
2138 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2139 sp_repr_set_svg_double(repr, "r", rg->r.computed);
2140 laststop->offset = 1;
2141 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2143 // iterate through midstops to set new offset values such that they won't move on canvas.
2144 SPStop *stop = stopinfo->vector->getFirstStop();
2145 stop = stop->getNextStop();
2146 while ( stop != laststop ) {
2147 stop->offset = stop->offset / offset;
2148 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2149 stop = stop->getNextStop();
2150 }
2151 break;
2152 }
2153 }
2154 else
2155 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
2156 SPCSSAttr *css = sp_repr_css_attr_new ();
2158 // stopinfo->spstop is the selected stop
2159 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2160 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2161 unselectedrepr = unselectedrepr->next();
2162 }
2164 if (unselectedrepr == NULL) {
2165 if (stopinfo->draggable->fill_or_stroke) {
2166 sp_repr_css_unset_property (css, "fill");
2167 } else {
2168 sp_repr_css_unset_property (css, "stroke");
2169 }
2170 } else {
2171 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2172 if (stopinfo->draggable->fill_or_stroke) {
2173 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2174 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2175 } else {
2176 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2177 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2178 }
2179 sp_repr_css_attr_unref (stopcss);
2180 }
2182 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2183 sp_repr_css_attr_unref (css);
2184 }
2186 endstoplist = g_slist_remove(endstoplist, stopinfo);
2187 delete stopinfo;
2188 }
2190 if (document) {
2191 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2192 }
2193 }
2196 /*
2197 Local Variables:
2198 mode:c++
2199 c-file-style:"stroustrup"
2200 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2201 indent-tabs-mode:nil
2202 fill-column:99
2203 End:
2204 */
2205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :