1 #define __GRADIENT_DRAG_C__
3 /*
4 * On-canvas gradient dragging
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
20 #include <glibmm/i18n.h>
21 #include <cstring>
22 #include <string>
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-style.h"
28 #include "document.h"
29 #include "display/sp-ctrlline.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 "prefs-utils.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"
46 #include "display/snap-indicator.h"
49 #define GR_KNOT_COLOR_NORMAL 0xffffff00
50 #define GR_KNOT_COLOR_MOUSEOVER 0xff000000
51 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
53 #define GR_LINE_COLOR_FILL 0x0000ff7f
54 #define GR_LINE_COLOR_STROKE 0x9999007f
56 // screen pixels between knots when they snap:
57 #define SNAP_DIST 5
59 // absolute distance between gradient points for them to become a single dragger when the drag is created:
60 #define MERGE_DIST 0.1
62 // knot shapes corresponding to GrPointType enum
63 SPKnotShapeType gr_knot_shapes [] = {
64 SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
65 SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END
66 SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
67 SP_KNOT_SHAPE_SQUARE, // POINT_RG_CENTER
68 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R1
69 SP_KNOT_SHAPE_CIRCLE, // POINT_RG_R2
70 SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
71 SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
72 SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
73 };
75 const gchar *gr_knot_descr [] = {
76 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
77 N_("Linear gradient <b>end</b>"),
78 N_("Linear gradient <b>mid stop</b>"),
79 N_("Radial gradient <b>center</b>"),
80 N_("Radial gradient <b>radius</b>"),
81 N_("Radial gradient <b>radius</b>"),
82 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
83 N_("Radial gradient <b>mid stop</b>"),
84 N_("Radial gradient <b>mid stop</b>")
85 };
87 static void
88 gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data)
89 {
90 GrDrag *drag = (GrDrag *) data;
91 drag->updateDraggers ();
92 drag->updateLines ();
93 drag->updateLevels ();
94 }
96 static void
97 gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data)
98 {
99 GrDrag *drag = (GrDrag *) data;
100 if (drag->local_change) {
101 drag->local_change = false;
102 } else {
103 drag->updateDraggers ();
104 }
105 drag->updateLines ();
106 drag->updateLevels ();
107 }
109 /**
110 When a _query_style_signal is received, check that \a property requests fill/stroke/opacity (otherwise
111 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
112 any.
113 */
114 int
115 gr_drag_style_query (SPStyle *style, int property, gpointer data)
116 {
117 GrDrag *drag = (GrDrag *) data;
119 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE && property != QUERY_STYLE_PROPERTY_MASTEROPACITY) {
120 return QUERY_STYLE_NOTHING;
121 }
123 if (!drag->selected) {
124 return QUERY_STYLE_NOTHING;
125 } else {
126 int ret = QUERY_STYLE_NOTHING;
128 float cf[4];
129 cf[0] = cf[1] = cf[2] = cf[3] = 0;
131 int count = 0;
133 for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers
134 GrDragger *d = (GrDragger *) i->data;
135 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
136 GrDraggable *draggable = (GrDraggable *) j->data;
138 if (ret == QUERY_STYLE_NOTHING) {
139 ret = QUERY_STYLE_SINGLE;
140 } else if (ret == QUERY_STYLE_SINGLE) {
141 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
142 }
144 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
145 cf[0] += SP_RGBA32_R_F (c);
146 cf[1] += SP_RGBA32_G_F (c);
147 cf[2] += SP_RGBA32_B_F (c);
148 cf[3] += SP_RGBA32_A_F (c);
150 count ++;
151 }
152 }
154 if (count) {
155 cf[0] /= count;
156 cf[1] /= count;
157 cf[2] /= count;
158 cf[3] /= count;
160 // set both fill and stroke with our stop-color and stop-opacity
161 style->fill.clear();
162 style->fill.setColor( cf[0], cf[1], cf[2] );
163 style->fill.set = TRUE;
164 style->stroke.clear();
165 style->stroke.setColor( cf[0], cf[1], cf[2] );
166 style->stroke.set = TRUE;
168 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
169 style->fill_opacity.set = TRUE;
170 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
171 style->stroke_opacity.set = TRUE;
173 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
174 style->opacity.set = TRUE;
175 }
177 return ret;
178 }
179 }
181 bool
182 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
183 {
184 GrDrag *drag = (GrDrag *) data;
186 if (!drag->selected)
187 return false;
189 SPCSSAttr *stop = sp_repr_css_attr_new ();
191 // See if the css contains interesting properties, and if so, translate them into the format
192 // acceptable for gradient stops
194 // any of color properties, in order of increasing priority:
195 if (css->attribute("flood-color"))
196 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
198 if (css->attribute("lighting-color"))
199 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
201 if (css->attribute("color"))
202 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
204 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
205 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
207 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
208 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
210 if (css->attribute("stop-color"))
211 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
214 if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
215 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
216 } else { // multiply all opacity properties:
217 gdouble accumulated = 1.0;
218 accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
219 accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
220 accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
221 accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
223 Inkscape::CSSOStringStream os;
224 os << accumulated;
225 sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
227 if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) ||
228 (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none")))
229 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
230 }
232 if (!stop->attributeList()) { // nothing for us here, pass it on
233 sp_repr_css_attr_unref(stop);
234 return false;
235 }
237 for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
238 GrDragger* dragger = (GrDragger*) sel->data;
239 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
240 GrDraggable *draggable = (GrDraggable *) i->data;
242 drag->local_change = true;
243 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
244 }
245 }
247 //sp_repr_css_print(stop);
248 sp_repr_css_attr_unref(stop);
249 return true;
250 }
252 bool
253 GrDrag::copy()
254 {
255 if (!selected)
256 return false;
258 float cf[4];
259 cf[0] = cf[1] = cf[2] = cf[3] = 0;
261 int count = 0;
263 for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers
264 GrDragger *d = (GrDragger *) i->data;
265 for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
266 GrDraggable *draggable = (GrDraggable *) j->data;
268 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
269 cf[0] += SP_RGBA32_R_F (c);
270 cf[1] += SP_RGBA32_G_F (c);
271 cf[2] += SP_RGBA32_B_F (c);
272 cf[3] += SP_RGBA32_A_F (c);
274 count ++;
275 }
276 }
278 if (count) {
279 cf[0] /= count;
280 cf[1] /= count;
281 cf[2] /= count;
282 cf[3] /= count;
283 }
285 guint32 const c32 = SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]);
286 gchar c[64];
288 SPCSSAttr *css = sp_repr_css_attr_new ();
289 g_snprintf(c, 64, "#%06x", c32 >> 8);
290 sp_repr_css_set_property (css, "fill", c);
291 Inkscape::CSSOStringStream os;
292 os << cf[3];
293 sp_repr_css_set_property (css, "opacity", os.str().c_str());
294 sp_set_style_clipboard (css);
296 g_snprintf(c, 64, "%06x%02x", c32 >> 8, c32 & 0x000000ff);
297 Glib::ustring text;
298 text += c;
299 if (!text.empty())
300 {
301 Glib::RefPtr<Gtk::Clipboard> refClipboard =
302 Gtk::Clipboard::get();
303 refClipboard->set_text(text);
304 }
306 return true;
307 }
309 SPStop *
310 GrDrag::addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance)
311 {
312 gfloat offset; // type of SPStop.offset = gfloat
313 SPGradient *gradient;
314 bool fill_or_stroke = true;
315 bool r1_knot = false;
317 bool addknot = false;
318 do {
319 gradient = sp_item_gradient (item, fill_or_stroke);
320 if (SP_IS_LINEARGRADIENT(gradient)) {
321 NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
322 NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
324 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
325 double dist_screen = NR::L2 (mouse_p - nearest);
326 if ( dist_screen < tolerance ) {
327 // add the knot
328 offset = get_offset_between_points(nearest, begin, end);
329 addknot = true;
330 break; // break out of the while loop: add only one knot
331 }
332 } else if (SP_IS_RADIALGRADIENT(gradient)) {
333 NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
334 NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
335 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
336 double dist_screen = NR::L2 (mouse_p - nearest);
337 if ( dist_screen < tolerance ) {
338 offset = get_offset_between_points(nearest, begin, end);
339 addknot = true;
340 r1_knot = true;
341 break; // break out of the while loop: add only one knot
342 }
344 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
345 nearest = snap_vector_midpoint (mouse_p, begin, end, 0);
346 dist_screen = NR::L2 (mouse_p - nearest);
347 if ( dist_screen < tolerance ) {
348 offset = get_offset_between_points(nearest, begin, end);
349 addknot = true;
350 r1_knot = false;
351 break; // break out of the while loop: add only one knot
352 }
353 }
354 fill_or_stroke = !fill_or_stroke;
355 } while (!fill_or_stroke && !addknot) ;
357 if (addknot) {
358 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
359 SPStop* prev_stop = sp_first_stop(vector);
360 SPStop* next_stop = sp_next_stop(prev_stop);
361 guint i = 1;
362 while ( (next_stop) && (next_stop->offset < offset) ) {
363 prev_stop = next_stop;
364 next_stop = sp_next_stop(next_stop);
365 i++;
366 }
367 if (!next_stop) {
368 // logical error: the endstop should have offset 1 and should always be more than this offset here
369 return NULL;
370 }
373 SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset);
374 sp_gradient_ensure_vector (gradient);
375 updateDraggers();
377 return newstop;
378 }
380 return NULL;
381 }
384 bool
385 GrDrag::dropColor(SPItem */*item*/, gchar *c, NR::Point p)
386 {
387 // first, see if we can drop onto one of the existing draggers
388 for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger
389 GrDragger *d = (GrDragger *) i->data;
391 if (NR::L2(p - d->point)*desktop->current_zoom() < 5) {
392 SPCSSAttr *stop = sp_repr_css_attr_new ();
393 sp_repr_css_set_property (stop, "stop-color", c);
394 sp_repr_css_set_property (stop, "stop-opacity", "1");
395 for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger
396 GrDraggable *draggable = (GrDraggable *) j->data;
397 local_change = true;
398 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
399 }
400 sp_repr_css_attr_unref(stop);
401 return true;
402 }
403 }
405 // now see if we're over line and create a new stop
406 bool over_line = false;
407 SPCtrlLine *line = NULL;
408 if (lines) {
409 for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) {
410 line = (SPCtrlLine*) l->data;
411 NR::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0);
412 double dist_screen = NR::L2 (p - nearest) * desktop->current_zoom();
413 if (line->item && dist_screen < 5) {
414 SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom());
415 if (stop) {
416 SPCSSAttr *css = sp_repr_css_attr_new ();
417 sp_repr_css_set_property (css, "stop-color", c);
418 sp_repr_css_set_property (css, "stop-opacity", "1");
419 sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style");
420 return true;
421 }
422 }
423 }
424 }
426 return false;
427 }
430 GrDrag::GrDrag(SPDesktop *desktop) {
432 this->desktop = desktop;
434 this->selection = sp_desktop_selection(desktop);
436 this->draggers = NULL;
437 this->lines = NULL;
438 this->selected = NULL;
440 this->hor_levels.clear();
441 this->vert_levels.clear();
443 this->local_change = false;
445 this->sel_changed_connection = this->selection->connectChanged(
446 sigc::bind (
447 sigc::ptr_fun(&gr_drag_sel_changed),
448 (gpointer)this )
450 );
451 this->sel_modified_connection = this->selection->connectModified(
452 sigc::bind(
453 sigc::ptr_fun(&gr_drag_sel_modified),
454 (gpointer)this )
455 );
457 this->style_set_connection = this->desktop->connectSetStyle(
458 sigc::bind(
459 sigc::ptr_fun(&gr_drag_style_set),
460 (gpointer)this )
461 );
463 this->style_query_connection = this->desktop->connectQueryStyle(
464 sigc::bind(
465 sigc::ptr_fun(&gr_drag_style_query),
466 (gpointer)this )
467 );
469 this->updateDraggers ();
470 this->updateLines ();
471 this->updateLevels ();
473 if (desktop->gr_item) {
474 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
475 }
476 }
478 GrDrag::~GrDrag()
479 {
480 this->sel_changed_connection.disconnect();
481 this->sel_modified_connection.disconnect();
482 this->style_set_connection.disconnect();
483 this->style_query_connection.disconnect();
485 if (this->selected) {
486 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
487 desktop->gr_item = draggable->item;
488 desktop->gr_point_type = draggable->point_type;
489 desktop->gr_point_i = draggable->point_i;
490 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
491 } else {
492 desktop->gr_item = NULL;
493 desktop->gr_point_type = 0;
494 desktop->gr_point_i = 0;
495 desktop->gr_fill_or_stroke = true;
496 }
498 deselect_all();
499 for (GList *l = this->draggers; l != NULL; l = l->next) {
500 delete ((GrDragger *) l->data);
501 }
502 g_list_free (this->draggers);
503 this->draggers = NULL;
504 this->selected = NULL;
506 for (GSList *l = this->lines; l != NULL; l = l->next) {
507 gtk_object_destroy( GTK_OBJECT (l->data));
508 }
509 g_slist_free (this->lines);
510 this->lines = NULL;
511 }
513 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
514 {
515 this->item = item;
516 this->point_type = point_type;
517 this->point_i = point_i;
518 this->fill_or_stroke = fill_or_stroke;
520 g_object_ref (G_OBJECT (this->item));
521 }
523 GrDraggable::~GrDraggable ()
524 {
525 g_object_unref (G_OBJECT (this->item));
526 }
529 SPObject *
530 GrDraggable::getServer ()
531 {
532 if (!item)
533 return NULL;
535 SPObject *server = NULL;
536 if (fill_or_stroke)
537 server = SP_OBJECT_STYLE_FILL_SERVER (item);
538 else
539 server = SP_OBJECT_STYLE_STROKE_SERVER (item);
541 return server;
542 }
544 static void
545 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
546 {
547 GrDragger *dragger = (GrDragger *) data;
548 GrDrag *drag = dragger->parent;
550 NR::Point p = *ppointer;
552 // FIXME: take from prefs
553 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
555 dragger->parent->desktop->snapindicator->remove_snappoint();
557 if (state & GDK_SHIFT_MASK) {
558 // with Shift; unsnap if we carry more than one draggable
559 if (dragger->draggables && dragger->draggables->next) {
560 // create a new dragger
561 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
562 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
563 // relink to it all but the first draggable in the list
564 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
565 GrDraggable *draggable = (GrDraggable *) i->data;
566 dr_new->addDraggable (draggable);
567 }
568 dr_new->updateKnotShape();
569 g_slist_free (dragger->draggables->next);
570 dragger->draggables->next = NULL;
571 dragger->updateKnotShape();
572 dragger->updateTip();
573 }
574 } else if (!(state & GDK_CONTROL_MASK)) {
575 // without Shift or Ctrl; see if we need to snap to another dragger
576 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
577 GrDragger *d_new = (GrDragger *) di->data;
578 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
580 // Merge draggers:
581 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
582 GrDraggable *draggable = (GrDraggable *) i->data;
583 // copy draggable to d_new:
584 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
585 d_new->addDraggable (da_new);
586 }
588 // unlink and delete this dragger
589 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
590 delete dragger;
592 // update the new merged dragger
593 d_new->fireDraggables(true, false, true);
594 d_new->parent->updateLines();
595 d_new->parent->setSelected (d_new);
596 d_new->updateKnotShape ();
597 d_new->updateTip ();
598 d_new->updateDependencies(true);
599 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
600 _("Merge gradient handles"));
601 return;
602 }
603 }
604 }
606 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
607 // Try snapping to the grid or guides
608 SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
609 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
610 if (s.getDistance() < 1e6) {
611 p = s.getPoint();
612 sp_knot_moveto (knot, &p);
613 dragger->parent->desktop->snapindicator->set_new_snappoint(p.to_2geom());
614 } else {
615 bool was_snapped = false;
616 Geom::Point snapped_to;
617 // No snapping so far, let's see if we need to snap to any of the levels
618 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
619 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
620 p[NR::Y] = dragger->parent->hor_levels[i];
621 snapped_to = p.to_2geom();
622 was_snapped = true;
623 sp_knot_moveto (knot, &p);
624 }
625 }
626 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
627 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
628 p[NR::X] = dragger->parent->vert_levels[i];
629 snapped_to = p.to_2geom();
630 was_snapped = true;
631 sp_knot_moveto (knot, &p);
632 }
633 }
634 if (was_snapped) {
635 dragger->parent->desktop->snapindicator->set_new_snappoint(snapped_to);
636 }
637 }
638 }
640 if (state & GDK_CONTROL_MASK) {
641 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
642 /* 0 means no snapping. */
644 // This list will store snap vectors from all draggables of dragger
645 GSList *snap_vectors = NULL;
647 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
648 GrDraggable *draggable = (GrDraggable *) i->data;
650 NR::Point *dr_snap = NULL;
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 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
664 dr_snap = ¢er;
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 NR::Point *snap_vector = NULL;
689 if (dr_snap) {
690 if (state & GDK_MOD1_MASK) {
691 // with Alt, snap to the original angle and its perpendiculars
692 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
693 } else {
694 // with Ctrl, snap to M_PI/snaps
695 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
696 }
697 }
698 if (snap_vector) {
699 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
700 }
701 }
703 // Move by the smallest of snap vectors:
704 NR::Point move(9999, 9999);
705 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
706 NR::Point *snap_vector = (NR::Point *) i->data;
707 if (NR::L2(*snap_vector) < NR::L2(move))
708 move = *snap_vector;
709 }
710 if (move[NR::X] < 9999) {
711 p += move;
712 sp_knot_moveto (knot, &p);
713 }
715 g_slist_free(snap_vectors);
716 }
718 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
719 bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK);
721 if (drag->keep_selection) {
722 NR::Point diff = p - dragger->point;
723 drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial);
724 } else {
725 dragger->point = p;
726 dragger->fireDraggables (false, scale_radial);
727 dragger->updateDependencies(false);
728 }
730 }
733 static void
734 gr_midpoint_limits(GrDragger *dragger, SPObject *server, NR::Point *begin, NR::Point *end, NR::Point *low_lim, NR::Point *high_lim, GSList **moving)
735 {
737 GrDrag *drag = dragger->parent;
738 // a midpoint dragger can (logically) only contain one GrDraggable
739 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
741 // get begin and end points between which dragging is allowed:
742 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
743 *moving = g_slist_append(*moving, dragger);
745 guint lowest_i = draggable->point_i;
746 guint highest_i = draggable->point_i;
747 GrDragger *lowest_dragger = dragger;
748 GrDragger *highest_dragger = dragger;
749 if (dragger->isSelected()) {
750 GrDragger* d_add;
751 while ( true )
752 {
753 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
754 if ( d_add && g_list_find(drag->selected, d_add) ) {
755 lowest_i = lowest_i - 1;
756 *moving = g_slist_prepend(*moving, d_add);
757 lowest_dragger = d_add;
758 } else {
759 break;
760 }
761 }
763 while ( true )
764 {
765 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
766 if ( d_add && g_list_find(drag->selected, d_add) ) {
767 highest_i = highest_i + 1;
768 *moving = g_slist_append(*moving, d_add);
769 highest_dragger = d_add;
770 } else {
771 break;
772 }
773 }
774 }
776 if ( SP_IS_LINEARGRADIENT(server) ) {
777 guint num = SP_LINEARGRADIENT(server)->vector.stops.size();
778 GrDragger *d_temp;
779 if (lowest_i == 1) {
780 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
781 } else {
782 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
783 }
784 if (d_temp)
785 *begin = d_temp->point;
787 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
788 if (d_temp == NULL) {
789 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, num-1, draggable->fill_or_stroke);
790 }
791 if (d_temp)
792 *end = d_temp->point;
793 } else if ( SP_IS_RADIALGRADIENT(server) ) {
794 guint num = SP_RADIALGRADIENT(server)->vector.stops.size();
795 GrDragger *d_temp;
796 if (lowest_i == 1) {
797 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
798 } else {
799 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
800 }
801 if (d_temp)
802 *begin = d_temp->point;
804 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
805 if (d_temp == NULL) {
806 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, num-1, draggable->fill_or_stroke);
807 }
808 if (d_temp)
809 *end = d_temp->point;
810 }
812 *low_lim = dragger->point - (lowest_dragger->point - *begin);
813 *high_lim = dragger->point - (highest_dragger->point - *end);
814 }
818 /**
819 Called when a midpoint knot is dragged.
820 */
821 static void
822 gr_knot_moved_midpoint_handler(SPKnot */*knot*/, NR::Point const *ppointer, guint state, gpointer data)
823 {
824 GrDragger *dragger = (GrDragger *) data;
825 GrDrag *drag = dragger->parent;
826 // a midpoint dragger can (logically) only contain one GrDraggable
827 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
829 // FIXME: take from prefs
830 double snap_fraction = 0.1;
832 NR::Point p = *ppointer;
833 NR::Point begin(0,0), end(0,0);
834 NR::Point low_lim(0,0), high_lim(0,0);
836 SPObject *server = draggable->getServer();
838 GSList *moving = NULL;
839 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
841 if (state & GDK_CONTROL_MASK) {
842 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
843 } else {
844 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
845 }
846 NR::Point displacement = p - dragger->point;
848 for (GSList const* i = moving; i != NULL; i = i->next) {
849 GrDragger *drg = (GrDragger*) i->data;
850 SPKnot *drgknot = drg->knot;
851 NR::Point this_move = displacement;
852 if (state & GDK_MOD1_MASK) {
853 // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place
854 double alpha = 1.0;
855 if (NR::L2(drg->point - dragger->point) + NR::L2(drg->point - begin) - 1e-3 > NR::L2(dragger->point - begin)) { // drg is on the end side from dragger
856 double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point);
857 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
858 } else { // drg is on the begin side from dragger
859 double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point);
860 this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move;
861 }
862 }
863 drg->point += this_move;
864 sp_knot_moveto (drgknot, & drg->point);
865 drg->fireDraggables (false);
866 drg->updateDependencies(false);
867 }
869 g_slist_free(moving);
871 drag->keep_selection = dragger->isSelected();
872 }
876 static void
877 gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data)
878 {
879 GrDragger *dragger = (GrDragger *) data;
881 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
882 }
884 /**
885 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
886 */
887 static void
888 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
889 {
890 GrDragger *dragger = (GrDragger *) data;
892 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
894 dragger->point_original = dragger->point = knot->pos;
896 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
897 dragger->fireDraggables (true, true);
898 } else {
899 dragger->fireDraggables (true);
900 }
902 for (GList *i = dragger->parent->selected; i != NULL; i = i->next) {
903 GrDragger *d = (GrDragger *) i->data;
904 if (d == dragger)
905 continue;
906 d->fireDraggables (true);
907 }
909 // make this dragger selected
910 if (!dragger->parent->keep_selection) {
911 dragger->parent->setSelected (dragger);
912 }
913 dragger->parent->keep_selection = false;
915 dragger->updateDependencies(true);
917 // we did an undoable action
918 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
919 _("Move gradient handle"));
920 }
922 /**
923 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
924 state of the keyboard keys
925 */
926 static void
927 gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data)
928 {
929 GrDragger *dragger = (GrDragger *) data;
930 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
931 if (!draggable) return;
933 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
934 // delete this knot from vector
935 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
936 gradient = sp_gradient_get_vector (gradient, false);
937 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
938 SPStop *stop = NULL;
939 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
940 case POINT_LG_BEGIN:
941 case POINT_RG_CENTER:
942 stop = sp_first_stop(gradient);
943 {
944 SPStop *next = sp_next_stop (stop);
945 if (next) {
946 next->offset = 0;
947 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
948 }
949 }
950 break;
951 case POINT_LG_END:
952 case POINT_RG_R1:
953 case POINT_RG_R2:
954 stop = sp_last_stop(gradient);
955 {
956 SPStop *prev = sp_prev_stop (stop, gradient);
957 if (prev) {
958 prev->offset = 1;
959 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
960 }
961 }
962 break;
963 case POINT_LG_MID:
964 case POINT_RG_MID1:
965 case POINT_RG_MID2:
966 stop = sp_get_stop_i(gradient, draggable->point_i);
967 break;
968 }
970 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
971 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
972 _("Delete gradient stop"));
973 }
974 } else {
975 // select the dragger
976 dragger->point_original = dragger->point;
978 if ( state & GDK_SHIFT_MASK ) {
979 dragger->parent->setSelected (dragger, true, false);
980 } else {
981 dragger->parent->setSelected (dragger);
982 }
983 }
984 }
986 /**
987 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
988 */
989 static void
990 gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data)
991 {
992 GrDragger *dragger = (GrDragger *) data;
994 dragger->point_original = dragger->point;
996 if (dragger->draggables == NULL)
997 return;
999 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1000 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1001 }
1003 /**
1004 Act upon all draggables of the dragger, setting them to the dragger's point
1005 */
1006 void
1007 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
1008 {
1009 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1010 GrDraggable *draggable = (GrDraggable *) i->data;
1012 // set local_change flag so that selection_changed callback does not regenerate draggers
1013 this->parent->local_change = true;
1015 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
1016 // to the center, unless it's the first update upon merge when we must snap it to the point
1017 if (merging_focus ||
1018 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
1019 {
1020 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
1021 }
1022 }
1023 }
1025 /**
1026 Checks if the dragger has a draggable with this point_type
1027 */
1028 bool
1029 GrDragger::isA (gint point_type)
1030 {
1031 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1032 GrDraggable *draggable = (GrDraggable *) i->data;
1033 if (draggable->point_type == point_type) {
1034 return true;
1035 }
1036 }
1037 return false;
1038 }
1040 /**
1041 Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke
1042 */
1043 bool
1044 GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1045 {
1046 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1047 GrDraggable *draggable = (GrDraggable *) i->data;
1048 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1049 return true;
1050 }
1051 }
1052 return false;
1053 }
1055 /**
1056 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
1057 */
1058 bool
1059 GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke)
1060 {
1061 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1062 GrDraggable *draggable = (GrDraggable *) i->data;
1063 if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
1064 return true;
1065 }
1066 }
1067 return false;
1068 }
1070 bool
1071 GrDraggable::mayMerge (GrDraggable *da2)
1072 {
1073 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
1074 // we must not merge the points of the same gradient!
1075 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
1076 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
1077 // except that we can snap center and focus together
1078 return false;
1079 }
1080 }
1081 // disable merging of midpoints.
1082 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
1083 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
1084 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
1085 return false;
1087 return true;
1088 }
1090 bool
1091 GrDragger::mayMerge (GrDragger *other)
1092 {
1093 if (this == other)
1094 return false;
1096 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1097 GrDraggable *da1 = (GrDraggable *) i->data;
1098 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
1099 GrDraggable *da2 = (GrDraggable *) j->data;
1100 if (!da1->mayMerge(da2))
1101 return false;
1102 }
1103 }
1104 return true;
1105 }
1107 bool
1108 GrDragger::mayMerge (GrDraggable *da2)
1109 {
1110 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
1111 GrDraggable *da1 = (GrDraggable *) i->data;
1112 if (!da1->mayMerge(da2))
1113 return false;
1114 }
1115 return true;
1116 }
1118 /**
1119 Updates the statusbar tip of the dragger knot, based on its draggables
1120 */
1121 void
1122 GrDragger::updateTip ()
1123 {
1124 if (this->knot && this->knot->tip) {
1125 g_free (this->knot->tip);
1126 this->knot->tip = NULL;
1127 }
1129 if (g_slist_length (this->draggables) == 1) {
1130 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
1131 char *item_desc = sp_item_description(draggable->item);
1132 switch (draggable->point_type) {
1133 case POINT_LG_MID:
1134 case POINT_RG_MID1:
1135 case POINT_RG_MID2:
1136 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"),
1137 _(gr_knot_descr[draggable->point_type]),
1138 draggable->point_i,
1139 item_desc,
1140 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1141 break;
1143 default:
1144 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"),
1145 _(gr_knot_descr[draggable->point_type]),
1146 item_desc,
1147 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
1148 break;
1149 }
1150 g_free(item_desc);
1151 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
1152 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
1153 } else {
1154 int length = g_slist_length (this->draggables);
1155 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
1156 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
1157 length),
1158 length);
1159 }
1160 }
1162 /**
1163 Adds a draggable to the dragger
1164 */
1165 void
1166 GrDragger::updateKnotShape ()
1167 {
1168 if (!draggables)
1169 return;
1170 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
1171 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
1172 }
1174 /**
1175 Adds a draggable to the dragger
1176 */
1177 void
1178 GrDragger::addDraggable (GrDraggable *draggable)
1179 {
1180 this->draggables = g_slist_prepend (this->draggables, draggable);
1182 this->updateTip();
1183 }
1186 /**
1187 Moves this dragger to the point of the given draggable, acting upon all other draggables
1188 */
1189 void
1190 GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1191 {
1192 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
1193 this->point_original = this->point;
1195 sp_knot_moveto (this->knot, &(this->point));
1197 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1198 GrDraggable *da = (GrDraggable *) i->data;
1199 if ( (da->item == item) &&
1200 (point_type == -1 || da->point_type == point_type) &&
1201 (point_i == -1 || da->point_i == point_i) &&
1202 (da->fill_or_stroke == fill_or_stroke) ) {
1203 continue;
1204 }
1205 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
1206 }
1207 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
1208 }
1211 /**
1212 Moves all midstop draggables that depend on this one
1213 */
1214 void
1215 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
1216 SPObject *server = draggable->getServer();
1217 if (!server)
1218 return;
1219 guint num = SP_GRADIENT(server)->vector.stops.size();
1220 if (num <= 2) return;
1222 if ( SP_IS_LINEARGRADIENT(server) ) {
1223 for ( guint i = 1; i < num - 1; i++ ) {
1224 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
1225 }
1226 } else if ( SP_IS_RADIALGRADIENT(server) ) {
1227 for ( guint i = 1; i < num - 1; i++ ) {
1228 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
1229 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1230 }
1231 }
1232 }
1235 /**
1236 Moves all draggables that depend on this one
1237 */
1238 void
1239 GrDragger::updateDependencies (bool write_repr)
1240 {
1241 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1242 GrDraggable *draggable = (GrDraggable *) i->data;
1243 switch (draggable->point_type) {
1244 case POINT_LG_BEGIN:
1245 {
1246 // the end point is dependent only when dragging with ctrl+shift
1247 this->moveOtherToDraggable (draggable->item, POINT_LG_END, -1, draggable->fill_or_stroke, write_repr);
1249 this->updateMidstopDependencies (draggable, write_repr);
1250 }
1251 break;
1252 case POINT_LG_END:
1253 {
1254 // the begin point is dependent only when dragging with ctrl+shift
1255 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1257 this->updateMidstopDependencies (draggable, write_repr);
1258 }
1259 break;
1260 case POINT_LG_MID:
1261 // no other nodes depend on mid points.
1262 break;
1263 case POINT_RG_R2:
1264 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1265 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1266 this->updateMidstopDependencies (draggable, write_repr);
1267 break;
1268 case POINT_RG_R1:
1269 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1270 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1271 this->updateMidstopDependencies (draggable, write_repr);
1272 break;
1273 case POINT_RG_CENTER:
1274 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, -1, draggable->fill_or_stroke, write_repr);
1275 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, -1, draggable->fill_or_stroke, write_repr);
1276 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, -1, draggable->fill_or_stroke, write_repr);
1277 this->updateMidstopDependencies (draggable, write_repr);
1278 break;
1279 case POINT_RG_FOCUS:
1280 // nothing can depend on that
1281 break;
1282 case POINT_RG_MID1:
1283 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1284 break;
1285 case POINT_RG_MID2:
1286 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1287 break;
1288 default:
1289 break;
1290 }
1291 }
1292 }
1296 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1297 {
1298 this->draggables = NULL;
1300 this->parent = parent;
1302 this->point = p;
1303 this->point_original = p;
1305 // create the knot
1306 this->knot = sp_knot_new (parent->desktop, NULL);
1307 this->knot->setMode(SP_KNOT_MODE_XOR);
1308 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER);
1309 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1310 sp_knot_update_ctrl(this->knot);
1312 // move knot to the given point
1313 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1314 sp_knot_show (this->knot);
1316 // connect knot's signals
1317 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1318 // luckily, midstops never snap to other nodes so are never unsnapped...
1319 && ( (draggable->point_type == POINT_LG_MID)
1320 || (draggable->point_type == POINT_RG_MID1)
1321 || (draggable->point_type == POINT_RG_MID2) ) )
1322 {
1323 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1324 } else {
1325 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1326 }
1327 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1328 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1329 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1330 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1332 // add the initial draggable
1333 if (draggable)
1334 this->addDraggable (draggable);
1335 updateKnotShape();
1336 }
1338 GrDragger::~GrDragger ()
1339 {
1340 // unselect if it was selected
1341 this->parent->setDeselected(this);
1343 // disconnect signals
1344 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1345 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1346 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1347 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1348 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1350 /* unref should call destroy */
1351 g_object_unref (G_OBJECT (this->knot));
1353 // delete all draggables
1354 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1355 delete ((GrDraggable *) i->data);
1356 }
1357 g_slist_free (this->draggables);
1358 this->draggables = NULL;
1359 }
1361 /**
1362 Select the dragger which has the given draggable.
1363 */
1364 GrDragger *
1365 GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke)
1366 {
1367 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1368 GrDragger *dragger = (GrDragger *) i->data;
1369 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1370 GrDraggable *da2 = (GrDraggable *) j->data;
1371 if ( (da2->item == item) &&
1372 (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter
1373 (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter
1374 (da2->fill_or_stroke == fill_or_stroke)) {
1375 return (dragger);
1376 }
1377 }
1378 }
1379 return NULL;
1380 }
1383 void
1384 GrDragger::moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr)
1385 {
1386 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1387 if (d && d != this) {
1388 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1389 }
1390 }
1393 /**
1394 Draw this dragger as selected
1395 */
1396 void
1397 GrDragger::select()
1398 {
1399 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1400 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1401 }
1403 /**
1404 Draw this dragger as normal (deselected)
1405 */
1406 void
1407 GrDragger::deselect()
1408 {
1409 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1410 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1411 }
1413 bool
1414 GrDragger::isSelected()
1415 {
1416 return g_list_find (parent->selected, this);
1417 }
1419 /**
1420 \brief Deselect all stops/draggers (private)
1421 */
1422 void
1423 GrDrag::deselect_all()
1424 {
1425 while (selected) {
1426 ( (GrDragger*) selected->data)->deselect();
1427 selected = g_list_remove(selected, selected->data);
1428 }
1429 }
1431 /**
1432 \brief Deselect all stops/draggers (public; emits signal)
1433 */
1434 void
1435 GrDrag::deselectAll()
1436 {
1437 deselect_all();
1438 this->desktop->emitToolSubselectionChanged(NULL);
1439 }
1441 /**
1442 \brief Select all stops/draggers
1443 */
1444 void
1445 GrDrag::selectAll()
1446 {
1447 for (GList *l = this->draggers; l != NULL; l = l->next) {
1448 GrDragger *d = ((GrDragger *) l->data);
1449 setSelected (d, true, true);
1450 }
1451 }
1453 /**
1454 \brief Select all stops/draggers that match the coords
1455 */
1456 void
1457 GrDrag::selectByCoords(std::vector<NR::Point> coords)
1458 {
1459 for (GList *l = this->draggers; l != NULL; l = l->next) {
1460 GrDragger *d = ((GrDragger *) l->data);
1461 for (guint k = 0; k < coords.size(); k++) {
1462 if (NR::L2 (d->point - coords[k]) < 1e-4) {
1463 setSelected (d, true, true);
1464 }
1465 }
1466 }
1467 }
1470 /**
1471 \brief Select all stops/draggers that fall within the rect
1472 */
1473 void
1474 GrDrag::selectRect(NR::Rect const &r)
1475 {
1476 for (GList *l = this->draggers; l != NULL; l = l->next) {
1477 GrDragger *d = ((GrDragger *) l->data);
1478 if (r.contains(d->point)) {
1479 setSelected (d, true, true);
1480 }
1481 }
1482 }
1484 /**
1485 \brief Select a dragger
1486 \param dragger The dragger to select
1487 \param add_to_selection If true, add to selection, otherwise deselect others
1488 \param override If true, always select this node, otherwise toggle selected status
1489 */
1490 void
1491 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1492 {
1493 GrDragger *seldragger = NULL;
1495 if (add_to_selection) {
1496 if (!dragger) return;
1497 if (override) {
1498 if (!g_list_find(selected, dragger)) {
1499 selected = g_list_prepend(selected, dragger);
1500 }
1501 dragger->select();
1502 seldragger = dragger;
1503 } else { // toggle
1504 if (g_list_find(selected, dragger)) {
1505 selected = g_list_remove(selected, dragger);
1506 dragger->deselect();
1507 if (selected) {
1508 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1509 }
1510 } else {
1511 selected = g_list_prepend(selected, dragger);
1512 dragger->select();
1513 seldragger = dragger;
1514 }
1515 }
1516 } else {
1517 deselect_all();
1518 if (dragger) {
1519 selected = g_list_prepend(selected, dragger);
1520 dragger->select();
1521 seldragger = dragger;
1522 }
1523 }
1524 if (seldragger) {
1525 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1526 }
1527 }
1529 /**
1530 \brief Deselect a dragger
1531 \param dragger The dragger to deselect
1532 */
1533 void
1534 GrDrag::setDeselected (GrDragger *dragger)
1535 {
1536 if (g_list_find(selected, dragger)) {
1537 selected = g_list_remove(selected, dragger);
1538 dragger->deselect();
1539 }
1540 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1541 }
1545 /**
1546 Create a line from p1 to p2 and add it to the lines list
1547 */
1548 void
1549 GrDrag::addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba)
1550 {
1551 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1552 SP_TYPE_CTRLLINE, NULL);
1553 SP_CTRLLINE(line)->item = item;
1554 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1555 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1556 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1557 sp_canvas_item_show (line);
1558 this->lines = g_slist_append (this->lines, line);
1559 }
1561 /**
1562 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1563 new dragger and add it to draggers list
1564 */
1565 void
1566 GrDrag::addDragger (GrDraggable *draggable)
1567 {
1568 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1570 for (GList *i = this->draggers; i != NULL; i = i->next) {
1571 GrDragger *dragger = (GrDragger *) i->data;
1572 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1573 // distance is small, merge this draggable into dragger, no need to create new dragger
1574 dragger->addDraggable (draggable);
1575 dragger->updateKnotShape();
1576 return;
1577 }
1578 }
1580 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1581 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1582 this->draggers = g_list_append (this->draggers, new_dragger);
1583 }
1585 /**
1586 Add draggers for the radial gradient rg on item
1587 */
1588 void
1589 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1590 {
1591 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1592 guint num = rg->vector.stops.size();
1593 if (num > 2) {
1594 for ( guint i = 1; i < num - 1; i++ ) {
1595 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1596 }
1597 }
1598 addDragger (new GrDraggable (item, POINT_RG_R1, num-1, fill_or_stroke));
1599 if (num > 2) {
1600 for ( guint i = 1; i < num - 1; i++ ) {
1601 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1602 }
1603 }
1604 addDragger (new GrDraggable (item, POINT_RG_R2, num-1, fill_or_stroke));
1605 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1606 }
1608 /**
1609 Add draggers for the linear gradient lg on item
1610 */
1611 void
1612 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1613 {
1614 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1615 guint num = lg->vector.stops.size();
1616 if (num > 2) {
1617 for ( guint i = 1; i < num - 1; i++ ) {
1618 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1619 }
1620 }
1621 addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke));
1622 }
1624 /**
1625 Artificially grab the knot of this dragger; used by the gradient context
1626 */
1627 void
1628 GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime)
1629 {
1630 if (dragger) {
1631 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1632 }
1633 }
1635 /**
1636 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1637 */
1638 void
1639 GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1640 {
1641 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1642 if (dragger) {
1643 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1644 }
1645 }
1647 /**
1648 Regenerates the draggers list from the current selection; is called when selection is changed or
1649 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1650 */
1651 void
1652 GrDrag::updateDraggers ()
1653 {
1654 while (selected) {
1655 selected = g_list_remove(selected, selected->data);
1656 }
1657 // delete old draggers
1658 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1659 delete ((GrDragger *) i->data);
1660 }
1661 g_list_free (this->draggers);
1662 this->draggers = NULL;
1664 g_return_if_fail (this->selection != NULL);
1666 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1668 SPItem *item = SP_ITEM(i->data);
1669 SPStyle *style = SP_OBJECT_STYLE (item);
1671 if (style && (style->fill.isPaintserver())) {
1672 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1673 if (SP_IS_LINEARGRADIENT (server)) {
1674 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1675 } else if (SP_IS_RADIALGRADIENT (server)) {
1676 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1677 }
1678 }
1680 if (style && (style->stroke.isPaintserver())) {
1681 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1682 if (SP_IS_LINEARGRADIENT (server)) {
1683 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1684 } else if (SP_IS_RADIALGRADIENT (server)) {
1685 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1686 }
1687 }
1688 }
1689 }
1691 /**
1692 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1693 lines are always in sync with the actual gradient
1694 */
1695 void
1696 GrDrag::updateLines ()
1697 {
1698 // delete old lines
1699 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1700 gtk_object_destroy( GTK_OBJECT (i->data));
1701 }
1702 g_slist_free (this->lines);
1703 this->lines = NULL;
1705 g_return_if_fail (this->selection != NULL);
1707 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1709 SPItem *item = SP_ITEM(i->data);
1711 SPStyle *style = SP_OBJECT_STYLE (item);
1713 if (style && (style->fill.isPaintserver())) {
1714 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1715 if (SP_IS_LINEARGRADIENT (server)) {
1716 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);
1717 } else if (SP_IS_RADIALGRADIENT (server)) {
1718 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1719 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1720 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1721 }
1722 }
1724 if (style && (style->stroke.isPaintserver())) {
1725 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1726 if (SP_IS_LINEARGRADIENT (server)) {
1727 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);
1728 } else if (SP_IS_RADIALGRADIENT (server)) {
1729 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1730 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1731 this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1732 }
1733 }
1734 }
1735 }
1737 /**
1738 Regenerates the levels list from the current selection
1739 */
1740 void
1741 GrDrag::updateLevels ()
1742 {
1743 hor_levels.clear();
1744 vert_levels.clear();
1746 g_return_if_fail (this->selection != NULL);
1748 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1749 SPItem *item = SP_ITEM(i->data);
1750 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1751 if (rect) {
1752 // Remember the edges of the bbox and the center axis
1753 hor_levels.push_back(rect->min()[NR::Y]);
1754 hor_levels.push_back(rect->max()[NR::Y]);
1755 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1756 vert_levels.push_back(rect->min()[NR::X]);
1757 vert_levels.push_back(rect->max()[NR::X]);
1758 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1759 }
1760 }
1761 }
1763 void
1764 GrDrag::selected_reverse_vector ()
1765 {
1766 if (selected == NULL)
1767 return;
1769 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1770 GrDraggable *draggable = (GrDraggable *) i->data;
1772 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1773 }
1774 }
1776 void
1777 GrDrag::selected_move_nowrite (double x, double y, bool scale_radial)
1778 {
1779 selected_move (x, y, false, scale_radial);
1780 }
1782 void
1783 GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial)
1784 {
1785 if (selected == NULL)
1786 return;
1788 bool did = false;
1790 for (GList *i = selected; i != NULL; i = i->next) {
1791 GrDragger *d = (GrDragger *) i->data;
1793 if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) {
1794 // if this is an endpoint,
1796 // Moving an rg center moves its focus and radii as well.
1797 // therefore, if this is a focus or radius and if selection
1798 // contains the center as well, do not move this one
1799 if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) ||
1800 (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) {
1801 bool skip_radius_with_center = false;
1802 for (GList *di = selected; di != NULL; di = di->next) {
1803 GrDragger *d_new = (GrDragger *) di->data;
1804 if (d_new->isA (((GrDraggable *) d->draggables->data)->item,
1805 POINT_RG_CENTER,
1806 0,
1807 ((GrDraggable *) d->draggables->data)->fill_or_stroke)) {
1808 // FIXME: here we take into account only the first draggable!
1809 skip_radius_with_center = true;
1810 }
1811 }
1812 if (skip_radius_with_center)
1813 continue;
1814 }
1816 did = true;
1817 d->point += NR::Point (x, y);
1818 d->point_original = d->point;
1819 sp_knot_moveto (d->knot, &(d->point));
1821 d->fireDraggables (write_repr, scale_radial);
1823 d->updateDependencies(write_repr);
1824 }
1825 }
1827 if (write_repr && did) {
1828 // we did an undoable action
1829 sp_document_maybe_done (sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT,
1830 _("Move gradient handle(s)"));
1831 return;
1832 }
1834 if (!did) { // none of the end draggers are selected, so let's try to move the mids
1836 GrDragger *dragger = (GrDragger *) selected->data;
1837 // a midpoint dragger can (logically) only contain one GrDraggable
1838 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
1840 NR::Point begin(0,0), end(0,0);
1841 NR::Point low_lim(0,0), high_lim(0,0);
1843 SPObject *server = draggable->getServer();
1844 GSList *moving = NULL;
1845 gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving);
1847 NR::Point p(x, y);
1848 p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0);
1849 NR::Point displacement = p - dragger->point;
1851 for (GSList const* i = moving; i != NULL; i = i->next) {
1852 GrDragger *drg = (GrDragger*) i->data;
1853 SPKnot *drgknot = drg->knot;
1854 drg->point += displacement;
1855 sp_knot_moveto (drgknot, & drg->point);
1856 drg->fireDraggables (true);
1857 drg->updateDependencies(true);
1858 did = true;
1859 }
1861 g_slist_free(moving);
1863 if (write_repr && did) {
1864 // we did an undoable action
1865 sp_document_maybe_done (sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT,
1866 _("Move gradient mid stop(s)"));
1867 }
1868 }
1869 }
1871 void
1872 GrDrag::selected_move_screen (double x, double y)
1873 {
1874 gdouble zoom = desktop->current_zoom();
1875 gdouble zx = x / zoom;
1876 gdouble zy = y / zoom;
1878 selected_move (zx, zy);
1879 }
1881 /**
1882 Select the knot next to the last selected one and deselect all other selected.
1883 */
1884 GrDragger *
1885 GrDrag::select_next ()
1886 {
1887 GrDragger *d = NULL;
1888 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1889 if (draggers)
1890 d = (GrDragger *) draggers->data;
1891 } else {
1892 d = (GrDragger *) g_list_find(draggers, selected->data)->next->data;
1893 }
1894 if (d)
1895 setSelected (d);
1896 return d;
1897 }
1899 /**
1900 Select the knot previous from the last selected one and deselect all other selected.
1901 */
1902 GrDragger *
1903 GrDrag::select_prev ()
1904 {
1905 GrDragger *d = NULL;
1906 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1907 if (draggers)
1908 d = (GrDragger *) g_list_last (draggers)->data;
1909 } else {
1910 d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data;
1911 }
1912 if (d)
1913 setSelected (d);
1914 return d;
1915 }
1918 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1919 void
1920 GrDrag::deleteSelected (bool just_one)
1921 {
1922 if (!selected) return;
1924 SPDocument *document = false;
1926 struct StructStopInfo {
1927 SPStop * spstop;
1928 GrDraggable * draggable;
1929 SPGradient * gradient;
1930 SPGradient * vector;
1931 };
1933 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1934 GSList *endstoplist = NULL; // list of stops that must be deleted
1935 while (selected) {
1936 GrDragger *dragger = (GrDragger*) selected->data;
1937 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1938 GrDraggable *draggable = (GrDraggable*) drgble->data;
1939 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1940 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1942 switch (draggable->point_type) {
1943 case POINT_LG_MID:
1944 case POINT_RG_MID1:
1945 case POINT_RG_MID2:
1946 {
1947 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1948 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1949 bool present = false;
1950 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1951 if ( (SPStop*)l->data == stop ) {
1952 present = true;
1953 break; // no need to search further.
1954 }
1955 }
1956 if (!present)
1957 midstoplist = g_slist_append(midstoplist, stop);
1958 }
1959 break;
1960 case POINT_LG_BEGIN:
1961 case POINT_LG_END:
1962 case POINT_RG_CENTER:
1963 case POINT_RG_R1:
1964 case POINT_RG_R2:
1965 {
1966 SPStop *stop = NULL;
1967 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1968 stop = sp_first_stop(vector);
1969 } else {
1970 stop = sp_last_stop(vector);
1971 }
1972 if (stop) {
1973 StructStopInfo *stopinfo = new StructStopInfo;
1974 stopinfo->spstop = stop;
1975 stopinfo->draggable = draggable;
1976 stopinfo->gradient = gradient;
1977 stopinfo->vector = vector;
1978 // check if already present in list. (e.g. when both R1 and R2 were selected)
1979 bool present = false;
1980 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1981 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1982 present = true;
1983 break; // no need to search further.
1984 }
1985 }
1986 if (!present)
1987 endstoplist = g_slist_append(endstoplist, stopinfo);
1988 }
1989 }
1990 break;
1991 default:
1992 break;
1993 }
1994 }
1995 selected = g_list_remove(selected, dragger);
1996 if ( just_one ) break; // iterate once if just_one is set.
1997 }
1998 while (midstoplist) {
1999 SPStop *stop = (SPStop*) midstoplist->data;
2000 document = SP_OBJECT_DOCUMENT (stop);
2001 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
2002 parent->removeChild(SP_OBJECT_REPR(stop));
2003 midstoplist = g_slist_remove(midstoplist, stop);
2004 }
2005 while (endstoplist) {
2006 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
2007 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
2009 // 2 is the minimum, cannot delete more than that without deleting the whole vector
2010 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
2011 // manually count the children, don't know if there already exists a function for this...
2012 int len = 0;
2013 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
2014 child != NULL ;
2015 child = SP_OBJECT_NEXT(child) )
2016 {
2017 if ( SP_IS_STOP(child) ) len ++;
2018 }
2019 if (len > 2)
2020 {
2021 switch (stopinfo->draggable->point_type) {
2022 case POINT_LG_BEGIN:
2023 {
2024 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2026 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2027 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
2028 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
2029 SPStop *stop = sp_first_stop(stopinfo->vector);
2030 gdouble offset = stop->offset;
2031 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
2032 lg->x1.computed = newbegin[NR::X];
2033 lg->y1.computed = newbegin[NR::Y];
2035 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2036 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
2037 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
2038 stop->offset = 0;
2039 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
2041 // iterate through midstops to set new offset values such that they won't move on canvas.
2042 SPStop *laststop = sp_last_stop(stopinfo->vector);
2043 stop = sp_next_stop(stop);
2044 while ( stop != laststop ) {
2045 stop->offset = (stop->offset - offset)/(1 - offset);
2046 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2047 stop = sp_next_stop(stop);
2048 }
2049 }
2050 break;
2051 case POINT_LG_END:
2052 {
2053 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2055 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
2056 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
2057 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
2058 SPStop *laststop = sp_last_stop(stopinfo->vector);
2059 gdouble offset = laststop->offset;
2060 NR::Point newend = begin + offset * (oldend - begin);
2061 lg->x2.computed = newend[NR::X];
2062 lg->y2.computed = newend[NR::Y];
2064 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
2065 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
2066 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
2067 laststop->offset = 1;
2068 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2070 // iterate through midstops to set new offset values such that they won't move on canvas.
2071 SPStop *stop = sp_first_stop(stopinfo->vector);
2072 stop = sp_next_stop(stop);
2073 while ( stop != laststop ) {
2074 stop->offset = stop->offset / offset;
2075 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2076 stop = sp_next_stop(stop);
2077 }
2078 }
2079 break;
2080 case POINT_RG_CENTER:
2081 {
2082 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
2083 if (newfirst) {
2084 newfirst->offset = 0;
2085 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
2086 }
2087 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2088 }
2089 break;
2090 case POINT_RG_R1:
2091 case POINT_RG_R2:
2092 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
2094 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
2095 double oldradius = rg->r.computed;
2096 SPStop *laststop = sp_last_stop(stopinfo->vector);
2097 gdouble offset = laststop->offset;
2098 double newradius = offset * oldradius;
2099 rg->r.computed = newradius;
2101 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
2102 sp_repr_set_svg_double(repr, "r", rg->r.computed);
2103 laststop->offset = 1;
2104 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
2106 // iterate through midstops to set new offset values such that they won't move on canvas.
2107 SPStop *stop = sp_first_stop(stopinfo->vector);
2108 stop = sp_next_stop(stop);
2109 while ( stop != laststop ) {
2110 stop->offset = stop->offset / offset;
2111 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
2112 stop = sp_next_stop(stop);
2113 }
2114 break;
2115 }
2116 }
2117 else
2118 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
2119 SPCSSAttr *css = sp_repr_css_attr_new ();
2121 // stopinfo->spstop is the selected stop
2122 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
2123 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
2124 unselectedrepr = unselectedrepr->next();
2125 }
2127 if (unselectedrepr == NULL) {
2128 if (stopinfo->draggable->fill_or_stroke) {
2129 sp_repr_css_unset_property (css, "fill");
2130 } else {
2131 sp_repr_css_unset_property (css, "stroke");
2132 }
2133 } else {
2134 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
2135 if (stopinfo->draggable->fill_or_stroke) {
2136 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2137 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2138 } else {
2139 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
2140 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
2141 }
2142 sp_repr_css_attr_unref (stopcss);
2143 }
2145 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
2146 sp_repr_css_attr_unref (css);
2147 }
2149 endstoplist = g_slist_remove(endstoplist, stopinfo);
2150 delete stopinfo;
2151 }
2153 if (document) {
2154 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
2155 }
2156 }
2159 /*
2160 Local Variables:
2161 mode:c++
2162 c-file-style:"stroustrup"
2163 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2164 indent-tabs-mode:nil
2165 fill-column:99
2166 End:
2167 */
2168 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :