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>
22 #include "desktop-handles.h"
23 #include "selection.h"
24 #include "desktop.h"
25 #include "desktop-style.h"
26 #include "document.h"
27 #include "display/sp-ctrlline.h"
29 #include "xml/repr.h"
30 #include "svg/css-ostringstream.h"
32 #include "svg/svg.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"
44 #include "snap.h"
45 #include "sp-namedview.h"
48 #define GR_KNOT_COLOR_NORMAL 0xffffff00
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>midstop</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_("Linear gradient <b>midstop</b>"),
82 N_("Linear gradient <b>midstop</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 (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger
132 GrDraggable *draggable = (GrDraggable *) i->data;
134 if (ret == QUERY_STYLE_NOTHING) {
135 ret = QUERY_STYLE_SINGLE;
136 } else if (ret == QUERY_STYLE_SINGLE) {
137 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
138 }
140 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
141 cf[0] += SP_RGBA32_R_F (c);
142 cf[1] += SP_RGBA32_G_F (c);
143 cf[2] += SP_RGBA32_B_F (c);
144 cf[3] += SP_RGBA32_A_F (c);
146 count ++;
147 }
149 if (count) {
150 cf[0] /= count;
151 cf[1] /= count;
152 cf[2] /= count;
153 cf[3] /= count;
155 // set both fill and stroke with our stop-color and stop-opacity
156 style->fill.clear();
157 style->fill.setColor( cf[0], cf[1], cf[2] );
158 style->fill.set = TRUE;
159 style->stroke.clear();
160 style->stroke.setColor( cf[0], cf[1], cf[2] );
161 style->stroke.set = TRUE;
163 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
164 style->fill_opacity.set = TRUE;
165 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (1.0);
166 style->stroke_opacity.set = TRUE;
168 style->opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
169 style->opacity.set = TRUE;
170 }
172 return ret;
173 }
174 }
176 bool
177 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
178 {
179 GrDrag *drag = (GrDrag *) data;
181 if (!drag->selected)
182 return false;
184 SPCSSAttr *stop = sp_repr_css_attr_new ();
186 // See if the css contains interesting properties, and if so, translate them into the format
187 // acceptable for gradient stops
189 // any of color properties, in order of increasing priority:
190 if (css->attribute("flood-color"))
191 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
193 if (css->attribute("lighting-color"))
194 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
196 if (css->attribute("color"))
197 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
199 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
200 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
202 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
203 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
205 if (css->attribute("stop-color"))
206 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
209 if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority
210 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
211 } else { // multiply all opacity properties:
212 gdouble accumulated = 1.0;
213 accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0);
214 accumulated *= sp_svg_read_percentage(css->attribute("opacity"), 1.0);
215 accumulated *= sp_svg_read_percentage(css->attribute("stroke-opacity"), 1.0);
216 accumulated *= sp_svg_read_percentage(css->attribute("fill-opacity"), 1.0);
218 Inkscape::CSSOStringStream os;
219 os << accumulated;
220 sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str());
222 if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
223 (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
224 sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
225 }
227 if (!stop->attributeList()) { // nothing for us here, pass it on
228 sp_repr_css_attr_unref(stop);
229 return false;
230 }
232 for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
233 GrDragger* dragger = (GrDragger*) sel->data;
234 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
235 GrDraggable *draggable = (GrDraggable *) i->data;
237 drag->local_change = true;
238 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
239 }
240 }
242 //sp_repr_css_print(stop);
243 sp_repr_css_attr_unref(stop);
244 return true;
245 }
247 GrDrag::GrDrag(SPDesktop *desktop) {
249 this->desktop = desktop;
251 this->selection = sp_desktop_selection(desktop);
253 this->draggers = NULL;
254 this->lines = NULL;
255 this->selected = NULL;
257 this->hor_levels.clear();
258 this->vert_levels.clear();
260 this->local_change = false;
262 this->sel_changed_connection = this->selection->connectChanged(
263 sigc::bind (
264 sigc::ptr_fun(&gr_drag_sel_changed),
265 (gpointer)this )
267 );
268 this->sel_modified_connection = this->selection->connectModified(
269 sigc::bind(
270 sigc::ptr_fun(&gr_drag_sel_modified),
271 (gpointer)this )
272 );
274 this->style_set_connection = this->desktop->connectSetStyle(
275 sigc::bind(
276 sigc::ptr_fun(&gr_drag_style_set),
277 (gpointer)this )
278 );
280 this->style_query_connection = this->desktop->connectQueryStyle(
281 sigc::bind(
282 sigc::ptr_fun(&gr_drag_style_query),
283 (gpointer)this )
284 );
286 this->updateDraggers ();
287 this->updateLines ();
288 this->updateLevels ();
290 if (desktop->gr_item) {
291 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
292 }
293 }
295 GrDrag::~GrDrag()
296 {
297 this->sel_changed_connection.disconnect();
298 this->sel_modified_connection.disconnect();
299 this->style_set_connection.disconnect();
300 this->style_query_connection.disconnect();
302 if (this->selected) {
303 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
304 desktop->gr_item = draggable->item;
305 desktop->gr_point_type = draggable->point_type;
306 desktop->gr_point_i = draggable->point_i;
307 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
308 } else {
309 desktop->gr_item = NULL;
310 desktop->gr_point_type = 0;
311 desktop->gr_point_i = 0;
312 desktop->gr_fill_or_stroke = true;
313 }
315 deselect_all();
316 for (GList *l = this->draggers; l != NULL; l = l->next) {
317 delete ((GrDragger *) l->data);
318 }
319 g_list_free (this->draggers);
320 this->draggers = NULL;
321 this->selected = NULL;
323 for (GSList *l = this->lines; l != NULL; l = l->next) {
324 gtk_object_destroy( GTK_OBJECT (l->data));
325 }
326 g_slist_free (this->lines);
327 this->lines = NULL;
328 }
330 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
331 {
332 this->item = item;
333 this->point_type = point_type;
334 this->point_i = point_i;
335 this->fill_or_stroke = fill_or_stroke;
337 g_object_ref (G_OBJECT (this->item));
338 }
340 GrDraggable::~GrDraggable ()
341 {
342 g_object_unref (G_OBJECT (this->item));
343 }
345 // FIXME: make global function in libnr or somewhere.
346 static NR::Point *
347 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
348 {
349 double r = NR::L2 (p - o);
350 if (r < 1e-3)
351 return NULL;
352 double angle = NR::atan2 (p - o);
353 // snap angle to snaps increments, starting from initial:
354 double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
355 // calculate the new position and subtract p to get the vector:
356 return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
357 }
359 // FIXME: make global function in libnr or somewhere.
360 static NR::Point
361 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
362 {
363 double length = NR::L2(end - begin);
364 NR::Point be = (end - begin) / length;
365 double r = NR::dot(p - begin, be);
367 if (r < 0.0) return begin;
368 if (r > length) return end;
370 double snapdist = length * snap;
371 double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
373 return (begin + r_snapped * be);
374 }
376 static void
377 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
378 {
379 GrDragger *dragger = (GrDragger *) data;
380 GrDrag *drag = dragger->parent;
382 NR::Point p = *ppointer;
384 // FIXME: take from prefs
385 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
387 if (state & GDK_SHIFT_MASK) {
388 // with Shift; unsnap if we carry more than one draggable
389 if (dragger->draggables && dragger->draggables->next) {
390 // create a new dragger
391 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
392 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
393 // relink to it all but the first draggable in the list
394 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
395 GrDraggable *draggable = (GrDraggable *) i->data;
396 dr_new->addDraggable (draggable);
397 }
398 dr_new->updateKnotShape();
399 g_slist_free (dragger->draggables->next);
400 dragger->draggables->next = NULL;
401 dragger->updateKnotShape();
402 dragger->updateTip();
403 }
404 } else if (!(state & GDK_CONTROL_MASK)) {
405 // without Shift or Ctrl; see if we need to snap to another dragger
406 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
407 GrDragger *d_new = (GrDragger *) di->data;
408 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
410 // Merge draggers:
411 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
412 GrDraggable *draggable = (GrDraggable *) i->data;
413 // copy draggable to d_new:
414 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
415 d_new->addDraggable (da_new);
416 }
418 // unlink and delete this dragger
419 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
420 delete dragger;
422 // update the new merged dragger
423 d_new->fireDraggables(true, false, true);
424 d_new->parent->updateLines();
425 d_new->parent->setSelected (d_new);
426 d_new->updateKnotShape ();
427 d_new->updateTip ();
428 d_new->updateDependencies(true);
429 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
430 _("Merge gradient handles"));
431 return;
432 }
433 }
434 }
436 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
437 // Try snapping to the grid or guides
438 SnapManager const &m = dragger->parent->desktop->namedview->snap_manager;
439 Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL);
440 if (s.getDistance() < 1e6) {
441 p = s.getPoint();
442 sp_knot_moveto (knot, &p);
443 } else {
444 // No snapping so far, let's see if we need to snap to any of the levels
445 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
446 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
447 p[NR::Y] = dragger->parent->hor_levels[i];
448 sp_knot_moveto (knot, &p);
449 }
450 }
451 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
452 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
453 p[NR::X] = dragger->parent->vert_levels[i];
454 sp_knot_moveto (knot, &p);
455 }
456 }
457 }
458 }
460 if (state & GDK_CONTROL_MASK) {
461 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
462 /* 0 means no snapping. */
464 // This list will store snap vectors from all draggables of dragger
465 GSList *snap_vectors = NULL;
467 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
468 GrDraggable *draggable = (GrDraggable *) i->data;
470 NR::Point *dr_snap = NULL;
472 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
473 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
474 GrDragger *d_new = (GrDragger *) di->data;
475 if (d_new == dragger)
476 continue;
477 if (d_new->isA (draggable->item,
478 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
479 draggable->point_i,
480 draggable->fill_or_stroke)) {
481 // found the other end of the linear gradient;
482 if (state & GDK_SHIFT_MASK) {
483 // moving linear around center
484 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
485 dr_snap = ¢er;
486 } else {
487 // moving linear around the other end
488 dr_snap = &d_new->point;
489 }
490 }
491 }
492 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
493 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
494 GrDragger *d_new = (GrDragger *) di->data;
495 if (d_new == dragger)
496 continue;
497 if (d_new->isA (draggable->item,
498 POINT_RG_CENTER,
499 draggable->point_i,
500 draggable->fill_or_stroke)) {
501 // found the center of the radial gradient;
502 dr_snap = &(d_new->point);
503 }
504 }
505 } else if (draggable->point_type == POINT_RG_CENTER) {
506 // radial center snaps to hor/vert relative to its original position
507 dr_snap = &(dragger->point_original);
508 }
510 NR::Point *snap_vector = NULL;
511 if (dr_snap) {
512 if (state & GDK_MOD1_MASK) {
513 // with Alt, snap to the original angle and its perpendiculars
514 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
515 } else {
516 // with Ctrl, snap to M_PI/snaps
517 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
518 }
519 }
520 if (snap_vector) {
521 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
522 }
523 }
525 // Move by the smallest of snap vectors:
526 NR::Point move(9999, 9999);
527 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
528 NR::Point *snap_vector = (NR::Point *) i->data;
529 if (NR::L2(*snap_vector) < NR::L2(move))
530 move = *snap_vector;
531 }
532 if (move[NR::X] < 9999) {
533 p += move;
534 sp_knot_moveto (knot, &p);
535 }
537 g_slist_free(snap_vectors);
538 }
540 dragger->point = p;
542 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
543 dragger->fireDraggables (false, true);
544 } else {
545 dragger->fireDraggables (false);
546 }
548 dragger->updateDependencies(false);
550 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
551 }
554 /**
555 Called when a midpoint knot is dragged.
556 */
557 static void
558 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
559 {
560 GrDragger *dragger = (GrDragger *) data;
561 GrDrag *drag = dragger->parent;
562 // a midpoint dragger can (logically) only contain one GrDraggable
563 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
565 // FIXME: take from prefs
566 double snap_fraction = 0.1;
568 NR::Point p = *ppointer;
569 NR::Point begin(0,0), end(0,0);
571 SPObject *server;
572 if (draggable->fill_or_stroke)
573 server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
574 else
575 server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
578 // get begin and end points between which dragging is allowed:
579 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
580 GSList *moving = NULL;
581 moving = g_slist_append(moving, dragger);
583 guint lowest_i = draggable->point_i;
584 guint highest_i = draggable->point_i;
585 GrDragger *lowest_dragger = dragger;
586 GrDragger *highest_dragger = dragger;
587 bool is_selected = g_list_find(drag->selected, dragger);
588 if (is_selected) {
589 GrDragger* d_add;
590 while ( true )
591 {
592 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
593 if ( d_add && g_list_find(drag->selected, d_add) ) {
594 lowest_i = lowest_i - 1;
595 moving = g_slist_prepend(moving, d_add);
596 lowest_dragger = d_add;
597 } else {
598 break;
599 }
600 }
602 while ( true )
603 {
604 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
605 if ( d_add && g_list_find(drag->selected, d_add) ) {
606 highest_i = highest_i + 1;
607 moving = g_slist_append(moving, d_add);
608 highest_dragger = d_add;
609 } else {
610 break;
611 }
612 }
613 }
615 if ( SP_IS_LINEARGRADIENT(server) ) {
616 GrDragger *d_temp;
617 if (lowest_i == 1) {
618 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
619 } else {
620 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
621 }
622 if (d_temp) begin = d_temp->point;
624 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
625 if (d_temp == NULL) {
626 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke);
627 }
628 if (d_temp) end = d_temp->point;
629 } else if ( SP_IS_RADIALGRADIENT(server) ) {
630 GrDragger *d_temp;
631 if (lowest_i == 1) {
632 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
633 } else {
634 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
635 }
636 if (d_temp) begin = d_temp->point;
638 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
639 if (d_temp == NULL) {
640 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, 0, draggable->fill_or_stroke);
641 }
642 if (d_temp) end = d_temp->point;
643 }
645 NR::Point low_lim = dragger->point - (lowest_dragger->point - begin);
646 NR::Point high_lim = dragger->point - (highest_dragger->point - end);
648 if (state & GDK_CONTROL_MASK) {
649 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
650 } else {
651 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
652 }
653 NR::Point displacement = p - dragger->point;
655 for (GSList const* i = moving; i != NULL; i = i->next) {
656 GrDragger *drg = (GrDragger*) i->data;
657 SPKnot *drgknot = drg->knot;
658 drg->point += displacement;
659 sp_knot_moveto (drgknot, & drg->point);
660 drg->fireDraggables (false);
661 drg->updateDependencies(false);
662 }
664 drag->keep_selection = is_selected;
665 }
669 static void
670 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
671 {
672 GrDragger *dragger = (GrDragger *) data;
674 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
675 }
677 /**
678 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
679 */
680 static void
681 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
682 {
683 GrDragger *dragger = (GrDragger *) data;
685 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
687 dragger->point_original = dragger->point = knot->pos;
689 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
690 dragger->fireDraggables (true, true);
691 } else {
692 dragger->fireDraggables (true);
693 }
695 // make this dragger selected
696 if (!dragger->parent->keep_selection) {
697 dragger->parent->setSelected (dragger);
698 }
699 dragger->parent->keep_selection = false;
701 dragger->updateDependencies(true);
703 // we did an undoable action
704 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
705 _("Move gradient handle"));
706 }
708 /**
709 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
710 state of the keyboard keys
711 */
712 static void
713 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
714 {
715 GrDragger *dragger = (GrDragger *) data;
716 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
717 if (!draggable) return;
719 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
720 // delete this knot from vector
721 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
722 gradient = sp_gradient_get_vector (gradient, false);
723 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
724 SPStop *stop = NULL;
725 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
726 case POINT_LG_BEGIN:
727 case POINT_RG_CENTER:
728 stop = sp_first_stop(gradient);
729 {
730 SPStop *next = sp_next_stop (stop);
731 if (next) {
732 next->offset = 0;
733 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
734 }
735 }
736 break;
737 case POINT_LG_END:
738 case POINT_RG_R1:
739 case POINT_RG_R2:
740 stop = sp_last_stop(gradient);
741 {
742 SPStop *prev = sp_prev_stop (stop, gradient);
743 if (prev) {
744 prev->offset = 1;
745 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
746 }
747 }
748 break;
749 case POINT_LG_MID:
750 case POINT_RG_MID1:
751 case POINT_RG_MID2:
752 stop = sp_get_stop_i(gradient, draggable->point_i);
753 break;
754 }
756 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
757 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
758 _("Delete gradient stop"));
759 }
760 } else {
761 // select the dragger
762 dragger->point_original = dragger->point;
764 if ( state & GDK_SHIFT_MASK ) {
765 dragger->parent->setSelected (dragger, true, false);
766 } else {
767 dragger->parent->setSelected (dragger);
768 }
769 }
770 }
772 /**
773 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
774 */
775 static void
776 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
777 {
778 GrDragger *dragger = (GrDragger *) data;
780 dragger->point_original = dragger->point;
782 if (dragger->draggables == NULL)
783 return;
785 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
786 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
787 }
789 /**
790 Act upon all draggables of the dragger, setting them to the dragger's point
791 */
792 void
793 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
794 {
795 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
796 GrDraggable *draggable = (GrDraggable *) i->data;
798 // set local_change flag so that selection_changed callback does not regenerate draggers
799 this->parent->local_change = true;
801 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
802 // to the center, unless it's the first update upon merge when we must snap it to the point
803 if (merging_focus ||
804 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
805 {
806 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
807 }
808 }
809 }
811 /**
812 Checks if the dragger has a draggable with this point_type
813 */
814 bool
815 GrDragger::isA (guint point_type)
816 {
817 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
818 GrDraggable *draggable = (GrDraggable *) i->data;
819 if (draggable->point_type == point_type) {
820 return true;
821 }
822 }
823 return false;
824 }
826 /**
827 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
828 */
829 bool
830 GrDragger::isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
831 {
832 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
833 GrDraggable *draggable = (GrDraggable *) i->data;
834 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
835 return true;
836 }
837 }
838 return false;
839 }
841 bool
842 GrDraggable::mayMerge (GrDraggable *da2)
843 {
844 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
845 // we must not merge the points of the same gradient!
846 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
847 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
848 // except that we can snap center and focus together
849 return false;
850 }
851 }
852 // disable merging of midpoints.
853 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
854 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
855 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
856 return false;
858 return true;
859 }
861 bool
862 GrDragger::mayMerge (GrDragger *other)
863 {
864 if (this == other)
865 return false;
867 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
868 GrDraggable *da1 = (GrDraggable *) i->data;
869 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
870 GrDraggable *da2 = (GrDraggable *) j->data;
871 if (!da1->mayMerge(da2))
872 return false;
873 }
874 }
875 return true;
876 }
878 bool
879 GrDragger::mayMerge (GrDraggable *da2)
880 {
881 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
882 GrDraggable *da1 = (GrDraggable *) i->data;
883 if (!da1->mayMerge(da2))
884 return false;
885 }
886 return true;
887 }
889 /**
890 Updates the statusbar tip of the dragger knot, based on its draggables
891 */
892 void
893 GrDragger::updateTip ()
894 {
895 if (this->knot && this->knot->tip) {
896 g_free (this->knot->tip);
897 this->knot->tip = NULL;
898 }
900 if (g_slist_length (this->draggables) == 1) {
901 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
902 char *item_desc = sp_item_description(draggable->item);
903 switch (draggable->point_type) {
904 case POINT_LG_MID:
905 case POINT_RG_MID1:
906 case POINT_RG_MID2:
907 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"),
908 _(gr_knot_descr[draggable->point_type]),
909 draggable->point_i,
910 item_desc,
911 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
912 break;
914 default:
915 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"),
916 _(gr_knot_descr[draggable->point_type]),
917 item_desc,
918 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
919 break;
920 }
921 g_free(item_desc);
922 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
923 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
924 } else {
925 int length = g_slist_length (this->draggables);
926 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
927 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
928 length),
929 length);
930 }
931 }
933 /**
934 Adds a draggable to the dragger
935 */
936 void
937 GrDragger::updateKnotShape ()
938 {
939 if (!draggables)
940 return;
941 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
942 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
943 }
945 /**
946 Adds a draggable to the dragger
947 */
948 void
949 GrDragger::addDraggable (GrDraggable *draggable)
950 {
951 this->draggables = g_slist_prepend (this->draggables, draggable);
953 this->updateTip();
954 }
957 /**
958 Moves this dragger to the point of the given draggable, acting upon all other draggables
959 */
960 void
961 GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
962 {
963 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
964 this->point_original = this->point;
966 sp_knot_moveto (this->knot, &(this->point));
968 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
969 GrDraggable *da = (GrDraggable *) i->data;
970 if ( (da->item == item) && (da->point_type == point_type) && (da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) {
971 continue;
972 }
973 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
974 }
975 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
976 }
979 /**
980 Moves all midstop draggables that depend on this one
981 */
982 void
983 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
984 SPObject *server;
985 if (draggable->fill_or_stroke)
986 server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
987 else
988 server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
989 guint num = SP_GRADIENT(server)->vector.stops.size();
990 if (num <= 2) return;
992 if ( SP_IS_LINEARGRADIENT(server) ) {
993 for ( guint i = 1; i < num - 1; i++ ) {
994 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
995 }
996 } else if ( SP_IS_RADIALGRADIENT(server) ) {
997 for ( guint i = 1; i < num - 1; i++ ) {
998 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
999 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
1000 }
1001 }
1002 }
1005 /**
1006 Moves all draggables that depend on this one
1007 */
1008 void
1009 GrDragger::updateDependencies (bool write_repr)
1010 {
1011 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1012 GrDraggable *draggable = (GrDraggable *) i->data;
1013 switch (draggable->point_type) {
1014 case POINT_LG_BEGIN:
1015 {
1016 // the end point is dependent only when dragging with ctrl+shift
1017 this->moveOtherToDraggable (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke, write_repr);
1019 this->updateMidstopDependencies (draggable, write_repr);
1020 }
1021 break;
1022 case POINT_LG_END:
1023 {
1024 // the begin point is dependent only when dragging with ctrl+shift
1025 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1027 this->updateMidstopDependencies (draggable, write_repr);
1028 }
1029 break;
1030 case POINT_LG_MID:
1031 // no other nodes depend on mid points.
1032 break;
1033 case POINT_RG_R2:
1034 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1035 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1036 this->updateMidstopDependencies (draggable, write_repr);
1037 break;
1038 case POINT_RG_R1:
1039 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1040 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1041 this->updateMidstopDependencies (draggable, write_repr);
1042 break;
1043 case POINT_RG_CENTER:
1044 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1045 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1046 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1047 this->updateMidstopDependencies (draggable, write_repr);
1048 break;
1049 case POINT_RG_FOCUS:
1050 // nothing can depend on that
1051 break;
1052 case POINT_RG_MID1:
1053 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1054 break;
1055 case POINT_RG_MID2:
1056 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1057 break;
1058 default:
1059 break;
1060 }
1061 }
1062 }
1066 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1067 {
1068 this->draggables = NULL;
1070 this->parent = parent;
1072 this->point = p;
1073 this->point_original = p;
1075 // create the knot
1076 this->knot = sp_knot_new (parent->desktop, NULL);
1077 this->knot->setMode(SP_KNOT_MODE_XOR);
1078 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1079 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1080 sp_knot_update_ctrl(this->knot);
1082 // move knot to the given point
1083 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1084 sp_knot_show (this->knot);
1086 // connect knot's signals
1087 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1088 // luckily, midstops never snap to other nodes so are never unsnapped...
1089 && ( (draggable->point_type == POINT_LG_MID)
1090 || (draggable->point_type == POINT_RG_MID1)
1091 || (draggable->point_type == POINT_RG_MID2) ) )
1092 {
1093 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1094 } else {
1095 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1096 }
1097 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1098 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1099 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1100 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1102 // add the initial draggable
1103 if (draggable)
1104 this->addDraggable (draggable);
1105 updateKnotShape();
1106 }
1108 GrDragger::~GrDragger ()
1109 {
1110 // unselect if it was selected
1111 this->parent->setDeselected(this);
1113 // disconnect signals
1114 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1115 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1116 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1117 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1118 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1120 /* unref should call destroy */
1121 g_object_unref (G_OBJECT (this->knot));
1123 // delete all draggables
1124 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1125 delete ((GrDraggable *) i->data);
1126 }
1127 g_slist_free (this->draggables);
1128 this->draggables = NULL;
1129 }
1131 /**
1132 Select the dragger which has the given draggable.
1133 */
1134 GrDragger *
1135 GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1136 {
1137 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1138 GrDragger *dragger = (GrDragger *) i->data;
1139 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1140 GrDraggable *da2 = (GrDraggable *) j->data;
1141 if ( (da2->item == item) && (da2->point_type == point_type) && (da2->point_i == point_i) && (da2->fill_or_stroke == fill_or_stroke)) {
1142 return (dragger);
1143 }
1144 }
1145 }
1146 return NULL;
1147 }
1150 void
1151 GrDragger::moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
1152 {
1153 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1154 if (d && d != this) {
1155 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1156 }
1157 }
1160 /**
1161 Draw this dragger as selected
1162 */
1163 void
1164 GrDragger::select()
1165 {
1166 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1167 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1168 }
1170 /**
1171 Draw this dragger as normal (deselected)
1172 */
1173 void
1174 GrDragger::deselect()
1175 {
1176 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1177 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1178 }
1182 /**
1183 \brief Deselect all stops/draggers (private)
1184 */
1185 void
1186 GrDrag::deselect_all()
1187 {
1188 while (selected) {
1189 ( (GrDragger*) selected->data)->deselect();
1190 selected = g_list_remove(selected, selected->data);
1191 }
1192 }
1194 /**
1195 \brief Deselect all stops/draggers (public; emits signal)
1196 */
1197 void
1198 GrDrag::deselectAll()
1199 {
1200 deselect_all();
1201 this->desktop->emitToolSubselectionChanged(NULL);
1202 }
1205 /**
1206 \brief Select a dragger
1207 \param dragger The dragger to select
1208 \param add_to_selection If true, add to selection, otherwise deselect others
1209 \param override If true, always select this node, otherwise toggle selected status
1210 */
1211 void
1212 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1213 {
1214 GrDragger *seldragger = NULL;
1216 if (add_to_selection) {
1217 if (!dragger) return;
1218 if (override) {
1219 if (!g_list_find(selected, dragger)) {
1220 selected = g_list_prepend(selected, dragger);
1221 }
1222 dragger->select();
1223 seldragger = dragger;
1224 } else { // toggle
1225 if (g_list_find(selected, dragger)) {
1226 selected = g_list_remove(selected, dragger);
1227 dragger->deselect();
1228 if (selected) {
1229 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1230 }
1231 } else {
1232 selected = g_list_prepend(selected, dragger);
1233 dragger->select();
1234 seldragger = dragger;
1235 }
1236 }
1237 } else {
1238 deselect_all();
1239 if (dragger) {
1240 selected = g_list_prepend(selected, dragger);
1241 dragger->select();
1242 seldragger = dragger;
1243 }
1244 }
1245 if (seldragger) {
1246 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1247 }
1248 }
1250 /**
1251 \brief Deselect a dragger
1252 \param dragger The dragger to deselect
1253 */
1254 void
1255 GrDrag::setDeselected (GrDragger *dragger)
1256 {
1257 if (g_list_find(selected, dragger)) {
1258 selected = g_list_remove(selected, dragger);
1259 dragger->deselect();
1260 }
1261 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1262 }
1266 /**
1267 Create a line from p1 to p2 and add it to the lines list
1268 */
1269 void
1270 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1271 {
1272 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1273 SP_TYPE_CTRLLINE, NULL);
1274 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1275 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1276 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1277 sp_canvas_item_show (line);
1278 this->lines = g_slist_append (this->lines, line);
1279 }
1281 /**
1282 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1283 new dragger and add it to draggers list
1284 */
1285 void
1286 GrDrag::addDragger (GrDraggable *draggable)
1287 {
1288 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1290 for (GList *i = this->draggers; i != NULL; i = i->next) {
1291 GrDragger *dragger = (GrDragger *) i->data;
1292 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1293 // distance is small, merge this draggable into dragger, no need to create new dragger
1294 dragger->addDraggable (draggable);
1295 dragger->updateKnotShape();
1296 return;
1297 }
1298 }
1300 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1301 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1302 this->draggers = g_list_append (this->draggers, new_dragger);
1303 }
1305 /**
1306 Add draggers for the radial gradient rg on item
1307 */
1308 void
1309 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1310 {
1311 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1312 guint num = rg->vector.stops.size();
1313 if (num > 2) {
1314 for ( guint i = 1; i < num - 1; i++ ) {
1315 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1316 }
1317 }
1318 addDragger (new GrDraggable (item, POINT_RG_R1, 0, fill_or_stroke));
1319 if (num > 2) {
1320 for ( guint i = 1; i < num - 1; i++ ) {
1321 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1322 }
1323 }
1324 addDragger (new GrDraggable (item, POINT_RG_R2, 0, fill_or_stroke));
1325 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1326 }
1328 /**
1329 Add draggers for the linear gradient lg on item
1330 */
1331 void
1332 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1333 {
1334 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1335 guint num = lg->vector.stops.size();
1336 if (num > 2) {
1337 for ( guint i = 1; i < num - 1; i++ ) {
1338 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1339 }
1340 }
1341 addDragger (new GrDraggable (item, POINT_LG_END, 0, fill_or_stroke));
1342 }
1344 /**
1345 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1346 */
1347 void
1348 GrDrag::grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1349 {
1350 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1351 if (dragger) {
1352 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1353 }
1354 }
1356 /**
1357 Regenerates the draggers list from the current selection; is called when selection is changed or
1358 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1359 */
1360 void
1361 GrDrag::updateDraggers ()
1362 {
1363 while (selected) {
1364 selected = g_list_remove(selected, selected->data);
1365 }
1366 // delete old draggers
1367 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1368 delete ((GrDragger *) i->data);
1369 }
1370 g_list_free (this->draggers);
1371 this->draggers = NULL;
1373 g_return_if_fail (this->selection != NULL);
1375 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1377 SPItem *item = SP_ITEM(i->data);
1378 SPStyle *style = SP_OBJECT_STYLE (item);
1380 if (style && (style->fill.isPaintserver())) {
1381 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1382 if (SP_IS_LINEARGRADIENT (server)) {
1383 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1384 } else if (SP_IS_RADIALGRADIENT (server)) {
1385 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1386 }
1387 }
1389 if (style && (style->stroke.isPaintserver())) {
1390 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1391 if (SP_IS_LINEARGRADIENT (server)) {
1392 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1393 } else if (SP_IS_RADIALGRADIENT (server)) {
1394 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1395 }
1396 }
1397 }
1398 }
1400 /**
1401 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1402 lines are always in sync with the actual gradient
1403 */
1404 void
1405 GrDrag::updateLines ()
1406 {
1407 // delete old lines
1408 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1409 gtk_object_destroy( GTK_OBJECT (i->data));
1410 }
1411 g_slist_free (this->lines);
1412 this->lines = NULL;
1414 g_return_if_fail (this->selection != NULL);
1416 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1418 SPItem *item = SP_ITEM(i->data);
1420 SPStyle *style = SP_OBJECT_STYLE (item);
1422 if (style && (style->fill.isPaintserver())) {
1423 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1424 if (SP_IS_LINEARGRADIENT (server)) {
1425 this->addLine (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);
1426 } else if (SP_IS_RADIALGRADIENT (server)) {
1427 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1428 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1429 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1430 }
1431 }
1433 if (style && (style->stroke.isPaintserver())) {
1434 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1435 if (SP_IS_LINEARGRADIENT (server)) {
1436 this->addLine (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);
1437 } else if (SP_IS_RADIALGRADIENT (server)) {
1438 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1439 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1440 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1441 }
1442 }
1443 }
1444 }
1446 /**
1447 Regenerates the levels list from the current selection
1448 */
1449 void
1450 GrDrag::updateLevels ()
1451 {
1452 hor_levels.clear();
1453 vert_levels.clear();
1455 g_return_if_fail (this->selection != NULL);
1457 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1458 SPItem *item = SP_ITEM(i->data);
1459 NR::Maybe<NR::Rect> rect = sp_item_bbox_desktop (item);
1460 if (rect) {
1461 // Remember the edges of the bbox and the center axis
1462 hor_levels.push_back(rect->min()[NR::Y]);
1463 hor_levels.push_back(rect->max()[NR::Y]);
1464 hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y]));
1465 vert_levels.push_back(rect->min()[NR::X]);
1466 vert_levels.push_back(rect->max()[NR::X]);
1467 vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X]));
1468 }
1469 }
1470 }
1472 void
1473 GrDrag::selected_reverse_vector ()
1474 {
1475 if (selected == NULL)
1476 return;
1478 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1479 GrDraggable *draggable = (GrDraggable *) i->data;
1481 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1482 }
1483 }
1485 void
1486 GrDrag::selected_move (double x, double y)
1487 {
1488 /*
1489 if (selected == NULL)
1490 return;
1492 if ( (g_list_length(selected) == 1) )
1493 selected->point += NR::Point (x, y);
1494 selected->point_original = selected->point;
1495 sp_knot_moveto (selected->knot, &(selected->point));
1497 selected->fireDraggables (true);
1499 selected->updateDependencies(true);
1501 // we did an undoable action
1502 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
1503 _("Move gradient handle"));
1504 */
1505 }
1507 void
1508 GrDrag::selected_move_screen (double x, double y)
1509 {
1510 gdouble zoom = desktop->current_zoom();
1511 gdouble zx = x / zoom;
1512 gdouble zy = y / zoom;
1514 selected_move (zx, zy);
1515 }
1517 /**
1518 Select the knot next to the last selected one and deselect all other selected.
1519 */
1520 void
1521 GrDrag::select_next ()
1522 {
1523 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1524 if (draggers)
1525 setSelected ((GrDragger *) draggers->data);
1526 } else {
1527 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1528 }
1529 }
1531 /**
1532 Select the knot previous from the last selected one and deselect all other selected.
1533 */
1534 void
1535 GrDrag::select_prev ()
1536 {
1537 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1538 if (draggers)
1539 setSelected ((GrDragger *) g_list_last (draggers)->data);
1540 } else {
1541 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1542 }
1543 }
1546 // FIXME: i.m.o. an ugly function that I just made to work, but... aargh! (Johan)
1547 void
1548 GrDrag::deleteSelected (bool just_one)
1549 {
1550 if (!selected) return;
1552 SPDocument *document = false;
1554 struct StructStopInfo {
1555 SPStop * spstop;
1556 GrDraggable * draggable;
1557 SPGradient * gradient;
1558 SPGradient * vector;
1559 };
1561 GSList *midstoplist = NULL; // list of stops that must be deleted (will be deleted first)
1562 GSList *endstoplist = NULL; // list of stops that must be deleted
1563 while (selected) {
1564 GrDragger *dragger = (GrDragger*) selected->data;
1565 for (GSList * drgble = dragger->draggables; drgble != NULL; drgble = drgble->next) {
1566 GrDraggable *draggable = (GrDraggable*) drgble->data;
1567 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
1568 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
1570 switch (draggable->point_type) {
1571 case POINT_LG_MID:
1572 case POINT_RG_MID1:
1573 case POINT_RG_MID2:
1574 {
1575 SPStop *stop = sp_get_stop_i(vector, draggable->point_i);
1576 // check if already present in list. (e.g. when both RG_MID1 and RG_MID2 were selected)
1577 bool present = false;
1578 for (GSList const * l = midstoplist; l != NULL; l = l->next) {
1579 if ( (SPStop*)l->data == stop ) {
1580 present = true;
1581 break; // no need to search further.
1582 }
1583 }
1584 if (!present)
1585 midstoplist = g_slist_append(midstoplist, stop);
1586 }
1587 break;
1588 case POINT_LG_BEGIN:
1589 case POINT_LG_END:
1590 case POINT_RG_CENTER:
1591 case POINT_RG_R1:
1592 case POINT_RG_R2:
1593 {
1594 SPStop *stop = NULL;
1595 if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) {
1596 stop = sp_first_stop(vector);
1597 } else {
1598 stop = sp_last_stop(vector);
1599 }
1600 if (stop) {
1601 StructStopInfo *stopinfo = new StructStopInfo;
1602 stopinfo->spstop = stop;
1603 stopinfo->draggable = draggable;
1604 stopinfo->gradient = gradient;
1605 stopinfo->vector = vector;
1606 // check if already present in list. (e.g. when both R1 and R2 were selected)
1607 bool present = false;
1608 for (GSList const * l = endstoplist; l != NULL; l = l->next) {
1609 if ( ((StructStopInfo*)l->data)->spstop == stopinfo->spstop ) {
1610 present = true;
1611 break; // no need to search further.
1612 }
1613 }
1614 if (!present)
1615 endstoplist = g_slist_append(endstoplist, stopinfo);
1616 }
1617 }
1618 break;
1619 default:
1620 break;
1621 }
1622 }
1623 selected = g_list_remove(selected, dragger);
1624 if ( just_one ) break; // iterate once if just_one is set.
1625 }
1626 while (midstoplist) {
1627 SPStop *stop = (SPStop*) midstoplist->data;
1628 document = SP_OBJECT_DOCUMENT (stop);
1629 Inkscape::XML::Node * parent = SP_OBJECT_REPR(stop)->parent();
1630 parent->removeChild(SP_OBJECT_REPR(stop));
1631 midstoplist = g_slist_remove(midstoplist, stop);
1632 }
1633 while (endstoplist) {
1634 StructStopInfo *stopinfo = (StructStopInfo*) endstoplist->data;
1635 document = SP_OBJECT_DOCUMENT (stopinfo->spstop);
1637 // 2 is the minimum, cannot delete more than that without deleting the whole vector
1638 // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop
1639 // manually count the children, don't know if there already exists a function for this...
1640 int len = 0;
1641 for ( SPObject *child = sp_object_first_child(stopinfo->vector) ;
1642 child != NULL ;
1643 child = SP_OBJECT_NEXT(child) )
1644 {
1645 if ( SP_IS_STOP(child) ) len ++;
1646 }
1647 if (len > 2)
1648 {
1649 switch (stopinfo->draggable->point_type) {
1650 case POINT_LG_BEGIN:
1651 {
1652 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1654 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1655 NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed);
1656 NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed);
1657 SPStop *stop = sp_first_stop(stopinfo->vector);
1658 gdouble offset = stop->offset;
1659 NR::Point newbegin = oldbegin + offset * (end - oldbegin);
1660 lg->x1.computed = newbegin[NR::X];
1661 lg->y1.computed = newbegin[NR::Y];
1663 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1664 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1665 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1666 stop->offset = 0;
1667 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", 0);
1669 // iterate through midstops to set new offset values such that they won't move on canvas.
1670 SPStop *laststop = sp_last_stop(stopinfo->vector);
1671 stop = sp_next_stop(stop);
1672 while ( stop != laststop ) {
1673 stop->offset = (stop->offset - offset)/(1 - offset);
1674 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1675 stop = sp_next_stop(stop);
1676 }
1677 }
1678 break;
1679 case POINT_LG_END:
1680 {
1681 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1683 SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient);
1684 NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed);
1685 NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed);
1686 SPStop *laststop = sp_last_stop(stopinfo->vector);
1687 gdouble offset = laststop->offset;
1688 NR::Point newend = begin + offset * (oldend - begin);
1689 lg->x2.computed = newend[NR::X];
1690 lg->y2.computed = newend[NR::Y];
1692 Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient);
1693 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1694 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1695 laststop->offset = 1;
1696 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1698 // iterate through midstops to set new offset values such that they won't move on canvas.
1699 SPStop *stop = sp_first_stop(stopinfo->vector);
1700 stop = sp_next_stop(stop);
1701 while ( stop != laststop ) {
1702 stop->offset = stop->offset / offset;
1703 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1704 stop = sp_next_stop(stop);
1705 }
1706 }
1707 break;
1708 case POINT_RG_CENTER:
1709 {
1710 SPStop *newfirst = sp_next_stop (stopinfo->spstop);
1711 if (newfirst) {
1712 newfirst->offset = 0;
1713 sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0);
1714 }
1715 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1716 }
1717 break;
1718 case POINT_RG_R1:
1719 case POINT_RG_R2:
1720 SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop));
1722 SPRadialGradient *rg = SP_RADIALGRADIENT(stopinfo->gradient);
1723 double oldradius = rg->r.computed;
1724 SPStop *laststop = sp_last_stop(stopinfo->vector);
1725 gdouble offset = laststop->offset;
1726 double newradius = offset * oldradius;
1727 rg->r.computed = newradius;
1729 Inkscape::XML::Node *repr = SP_OBJECT_REPR(rg);
1730 sp_repr_set_svg_double(repr, "r", rg->r.computed);
1731 laststop->offset = 1;
1732 sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1);
1734 // iterate through midstops to set new offset values such that they won't move on canvas.
1735 SPStop *stop = sp_first_stop(stopinfo->vector);
1736 stop = sp_next_stop(stop);
1737 while ( stop != laststop ) {
1738 stop->offset = stop->offset / offset;
1739 sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset);
1740 stop = sp_next_stop(stop);
1741 }
1742 break;
1743 }
1744 }
1745 else
1746 { // delete the gradient from the object. set fill to unset FIXME: set to fill of unselected node?
1747 SPCSSAttr *css = sp_repr_css_attr_new ();
1749 // stopinfo->spstop is the selected stop
1750 Inkscape::XML::Node *unselectedrepr = SP_OBJECT_REPR(stopinfo->vector)->firstChild();
1751 if (unselectedrepr == SP_OBJECT_REPR(stopinfo->spstop) ) {
1752 unselectedrepr = unselectedrepr->next();
1753 }
1755 if (unselectedrepr == NULL) {
1756 if (stopinfo->draggable->fill_or_stroke) {
1757 sp_repr_css_unset_property (css, "fill");
1758 } else {
1759 sp_repr_css_unset_property (css, "stroke");
1760 }
1761 } else {
1762 SPCSSAttr *stopcss = sp_repr_css_attr(unselectedrepr, "style");
1763 if (stopinfo->draggable->fill_or_stroke) {
1764 sp_repr_css_set_property(css, "fill", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1765 sp_repr_css_set_property(css, "fill-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1766 } else {
1767 sp_repr_css_set_property(css, "stroke", sp_repr_css_property(stopcss, "stop-color", "inkscape:unset"));
1768 sp_repr_css_set_property(css, "stroke-opacity", sp_repr_css_property(stopcss, "stop-opacity", "1"));
1769 }
1770 sp_repr_css_attr_unref (stopcss);
1771 }
1773 sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style");
1774 sp_repr_css_attr_unref (css);
1775 }
1777 endstoplist = g_slist_remove(endstoplist, stopinfo);
1778 delete stopinfo;
1779 }
1781 if (document) {
1782 sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") );
1783 }
1784 }
1787 /*
1788 Local Variables:
1789 mode:c++
1790 c-file-style:"stroustrup"
1791 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1792 indent-tabs-mode:nil
1793 fill-column:99
1794 End:
1795 */
1796 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :