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"
31 #include "prefs-utils.h"
32 #include "sp-item.h"
33 #include "style.h"
34 #include "knot.h"
35 #include "sp-linear-gradient.h"
36 #include "sp-radial-gradient.h"
37 #include "gradient-chemistry.h"
38 #include "gradient-drag.h"
39 #include "sp-stop.h"
41 #define GR_KNOT_COLOR_NORMAL 0xffffff00
42 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
44 #define GR_LINE_COLOR_FILL 0x0000ff7f
45 #define GR_LINE_COLOR_STROKE 0x9999007f
47 // screen pixels between knots when they snap:
48 #define SNAP_DIST 5
50 // absolute distance between gradient points for them to become a single dragger when the drag is created:
51 #define MERGE_DIST 0.1
53 // knot shapes corresponding to GrPointType enum
54 SPKnotShapeType gr_knot_shapes [] = {
55 SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN
56 SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END
57 SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID
58 SP_KNOT_SHAPE_DIAMOND,
59 SP_KNOT_SHAPE_CIRCLE,
60 SP_KNOT_SHAPE_CIRCLE,
61 SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS
62 SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1
63 SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2
64 };
66 const gchar *gr_knot_descr [] = {
67 N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
68 N_("Linear gradient <b>end</b>"),
69 N_("Linear gradient <b>midstop</b>"),
70 N_("Radial gradient <b>center</b>"),
71 N_("Radial gradient <b>radius</b>"),
72 N_("Radial gradient <b>radius</b>"),
73 N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
74 N_("Linear gradient <b>midstop</b>"),
75 N_("Linear gradient <b>midstop</b>")
76 };
78 static void
79 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
80 {
81 GrDrag *drag = (GrDrag *) data;
82 drag->updateDraggers ();
83 drag->updateLines ();
84 drag->updateLevels ();
85 }
87 static void
88 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
89 {
90 GrDrag *drag = (GrDrag *) data;
91 if (drag->local_change) {
92 drag->local_change = false;
93 } else {
94 drag->updateDraggers ();
95 }
96 drag->updateLines ();
97 drag->updateLevels ();
98 }
100 /**
101 When a _query_style_signal is received, check that \a property requests fill/stroke (otherwise
102 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
103 any.
104 */
105 int
106 gr_drag_style_query (SPStyle *style, int property, gpointer data)
107 {
108 GrDrag *drag = (GrDrag *) data;
110 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE) {
111 return QUERY_STYLE_NOTHING;
112 }
114 if (!drag->selected) {
115 return QUERY_STYLE_NOTHING;
116 } else {
117 int ret = QUERY_STYLE_NOTHING;
119 float cf[4];
120 cf[0] = cf[1] = cf[2] = cf[3] = 0;
122 int count = 0;
124 for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger
125 GrDraggable *draggable = (GrDraggable *) i->data;
127 if (ret == QUERY_STYLE_NOTHING) {
128 ret = QUERY_STYLE_SINGLE;
129 } else if (ret == QUERY_STYLE_SINGLE) {
130 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
131 }
133 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
134 cf[0] += SP_RGBA32_R_F (c);
135 cf[1] += SP_RGBA32_G_F (c);
136 cf[2] += SP_RGBA32_B_F (c);
137 cf[3] += SP_RGBA32_A_F (c);
139 count ++;
140 }
142 if (count) {
143 cf[0] /= count;
144 cf[1] /= count;
145 cf[2] /= count;
146 cf[3] /= count;
148 // set both fill and stroke with our stop-color and stop-opacity
149 sp_color_set_rgb_float((SPColor *) &style->fill.value.color, cf[0], cf[1], cf[2]);
150 style->fill.set = TRUE;
151 style->fill.type = SP_PAINT_TYPE_COLOR;
152 sp_color_set_rgb_float((SPColor *) &style->stroke.value.color, cf[0], cf[1], cf[2]);
153 style->stroke.set = TRUE;
154 style->stroke.type = SP_PAINT_TYPE_COLOR;
156 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
157 style->fill_opacity.set = TRUE;
158 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
159 style->stroke_opacity.set = TRUE;
160 }
162 return ret;
163 }
164 }
166 bool
167 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
168 {
169 GrDrag *drag = (GrDrag *) data;
171 if (!drag->selected)
172 return false;
174 SPCSSAttr *stop = sp_repr_css_attr_new ();
176 // See if the css contains interesting properties, and if so, translate them into the format
177 // acceptable for gradient stops
179 // any of color properties, in order of increasing priority:
180 if (css->attribute("flood-color"))
181 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
183 if (css->attribute("lighting-color"))
184 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
186 if (css->attribute("color"))
187 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
189 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
190 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
192 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
193 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
195 if (css->attribute("stop-color"))
196 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
198 // any of opacity properties, in order of increasing priority:
199 if (css->attribute("flood-opacity"))
200 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("flood-color"));
202 if (css->attribute("opacity")) // TODO: multiply
203 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("color"));
205 if (css->attribute("stroke-opacity")) // TODO: multiply
206 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stroke-opacity"));
208 if (css->attribute("fill-opacity")) // TODO: multiply
209 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("fill-opacity"));
211 if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
212 (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
213 sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
215 if (css->attribute("stop-opacity"))
216 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
218 if (!stop->attributeList()) { // nothing for us here, pass it on
219 sp_repr_css_attr_unref(stop);
220 return false;
221 }
223 for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers
224 GrDragger* dragger = (GrDragger*) sel->data;
225 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
226 GrDraggable *draggable = (GrDraggable *) i->data;
228 drag->local_change = true;
229 sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop);
230 }
231 }
233 //sp_repr_css_print(stop);
234 sp_repr_css_attr_unref(stop);
235 return true;
236 }
238 GrDrag::GrDrag(SPDesktop *desktop) {
240 this->desktop = desktop;
242 this->selection = sp_desktop_selection(desktop);
244 this->draggers = NULL;
245 this->lines = NULL;
246 this->selected = NULL;
248 this->hor_levels.clear();
249 this->vert_levels.clear();
251 this->local_change = false;
253 this->sel_changed_connection = this->selection->connectChanged(
254 sigc::bind (
255 sigc::ptr_fun(&gr_drag_sel_changed),
256 (gpointer)this )
258 );
259 this->sel_modified_connection = this->selection->connectModified(
260 sigc::bind(
261 sigc::ptr_fun(&gr_drag_sel_modified),
262 (gpointer)this )
263 );
265 this->style_set_connection = this->desktop->connectSetStyle(
266 sigc::bind(
267 sigc::ptr_fun(&gr_drag_style_set),
268 (gpointer)this )
269 );
271 this->style_query_connection = this->desktop->connectQueryStyle(
272 sigc::bind(
273 sigc::ptr_fun(&gr_drag_style_query),
274 (gpointer)this )
275 );
277 this->updateDraggers ();
278 this->updateLines ();
279 this->updateLevels ();
281 if (desktop->gr_item) {
282 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke));
283 }
284 }
286 GrDrag::~GrDrag()
287 {
288 this->sel_changed_connection.disconnect();
289 this->sel_modified_connection.disconnect();
290 this->style_set_connection.disconnect();
291 this->style_query_connection.disconnect();
293 if (this->selected) {
294 GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data;
295 desktop->gr_item = draggable->item;
296 desktop->gr_point_type = draggable->point_type;
297 desktop->gr_point_i = draggable->point_i;
298 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
299 } else {
300 desktop->gr_item = NULL;
301 desktop->gr_point_type = 0;
302 desktop->gr_point_i = 0;
303 desktop->gr_fill_or_stroke = true;
304 }
306 deselect_all();
307 for (GList *l = this->draggers; l != NULL; l = l->next) {
308 delete ((GrDragger *) l->data);
309 }
310 g_list_free (this->draggers);
311 this->draggers = NULL;
312 this->selected = NULL;
314 for (GSList *l = this->lines; l != NULL; l = l->next) {
315 gtk_object_destroy( GTK_OBJECT (l->data));
316 }
317 g_slist_free (this->lines);
318 this->lines = NULL;
319 }
321 GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
322 {
323 this->item = item;
324 this->point_type = point_type;
325 this->point_i = point_i;
326 this->fill_or_stroke = fill_or_stroke;
328 g_object_ref (G_OBJECT (this->item));
329 }
331 GrDraggable::~GrDraggable ()
332 {
333 g_object_unref (G_OBJECT (this->item));
334 }
336 // FIXME: make global function in libnr or somewhere.
337 static NR::Point *
338 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
339 {
340 double r = NR::L2 (p - o);
341 if (r < 1e-3)
342 return NULL;
343 double angle = NR::atan2 (p - o);
344 // snap angle to snaps increments, starting from initial:
345 double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
346 // calculate the new position and subtract p to get the vector:
347 return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
348 }
350 // FIXME: make global function in libnr or somewhere.
351 static NR::Point
352 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
353 {
354 double length = NR::L2(end - begin);
355 NR::Point be = (end - begin) / length;
356 double r = NR::dot(p - begin, be);
358 if (r < 0.0) return begin;
359 if (r > length) return end;
361 double snapdist = length * snap;
362 double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
364 return (begin + r_snapped * be);
365 }
367 static void
368 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
369 {
370 GrDragger *dragger = (GrDragger *) data;
371 GrDrag *drag = dragger->parent;
373 NR::Point p = *ppointer;
375 // FIXME: take from prefs
376 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
378 if (state & GDK_SHIFT_MASK) {
379 // with Shift; unsnap if we carry more than one draggable
380 if (dragger->draggables && dragger->draggables->next) {
381 // create a new dragger
382 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
383 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
384 // relink to it all but the first draggable in the list
385 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
386 GrDraggable *draggable = (GrDraggable *) i->data;
387 dr_new->addDraggable (draggable);
388 }
389 dr_new->updateKnotShape();
390 g_slist_free (dragger->draggables->next);
391 dragger->draggables->next = NULL;
392 dragger->updateKnotShape();
393 dragger->updateTip();
394 }
395 } else if (!(state & GDK_CONTROL_MASK)) {
396 // without Shift or Ctrl; see if we need to snap to another dragger
397 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
398 GrDragger *d_new = (GrDragger *) di->data;
399 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
401 // Merge draggers:
402 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
403 GrDraggable *draggable = (GrDraggable *) i->data;
404 // copy draggable to d_new:
405 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
406 d_new->addDraggable (da_new);
407 }
409 // unlink and delete this dragger
410 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
411 delete dragger;
413 // update the new merged dragger
414 d_new->fireDraggables(true, false, true);
415 d_new->parent->updateLines();
416 d_new->parent->setSelected (d_new);
417 d_new->updateKnotShape ();
418 d_new->updateTip ();
419 d_new->updateDependencies(true);
420 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
421 _("Merge gradient handles"));
422 return;
423 }
424 }
425 }
428 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
429 // See if we need to snap to any of the levels
430 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
431 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
432 p[NR::Y] = dragger->parent->hor_levels[i];
433 sp_knot_moveto (knot, &p);
434 }
435 }
436 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
437 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
438 p[NR::X] = dragger->parent->vert_levels[i];
439 sp_knot_moveto (knot, &p);
440 }
441 }
442 }
444 if (state & GDK_CONTROL_MASK) {
445 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
446 /* 0 means no snapping. */
448 // This list will store snap vectors from all draggables of dragger
449 GSList *snap_vectors = NULL;
451 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
452 GrDraggable *draggable = (GrDraggable *) i->data;
454 NR::Point *dr_snap = NULL;
456 if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) {
457 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
458 GrDragger *d_new = (GrDragger *) di->data;
459 if (d_new == dragger)
460 continue;
461 if (d_new->isA (draggable->item,
462 draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN,
463 draggable->point_i,
464 draggable->fill_or_stroke)) {
465 // found the other end of the linear gradient;
466 if (state & GDK_SHIFT_MASK) {
467 // moving linear around center
468 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
469 dr_snap = ¢er;
470 } else {
471 // moving linear around the other end
472 dr_snap = &d_new->point;
473 }
474 }
475 }
476 } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) {
477 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
478 GrDragger *d_new = (GrDragger *) di->data;
479 if (d_new == dragger)
480 continue;
481 if (d_new->isA (draggable->item,
482 POINT_RG_CENTER,
483 draggable->point_i,
484 draggable->fill_or_stroke)) {
485 // found the center of the radial gradient;
486 dr_snap = &(d_new->point);
487 }
488 }
489 } else if (draggable->point_type == POINT_RG_CENTER) {
490 // radial center snaps to hor/vert relative to its original position
491 dr_snap = &(dragger->point_original);
492 }
494 NR::Point *snap_vector = NULL;
495 if (dr_snap) {
496 if (state & GDK_MOD1_MASK) {
497 // with Alt, snap to the original angle and its perpendiculars
498 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
499 } else {
500 // with Ctrl, snap to M_PI/snaps
501 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
502 }
503 }
504 if (snap_vector) {
505 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
506 }
507 }
509 // Move by the smallest of snap vectors:
510 NR::Point move(9999, 9999);
511 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
512 NR::Point *snap_vector = (NR::Point *) i->data;
513 if (NR::L2(*snap_vector) < NR::L2(move))
514 move = *snap_vector;
515 }
516 if (move[NR::X] < 9999) {
517 p += move;
518 sp_knot_moveto (knot, &p);
519 }
521 g_slist_free(snap_vectors);
522 }
524 dragger->point = p;
526 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
527 dragger->fireDraggables (false, true);
528 } else {
529 dragger->fireDraggables (false);
530 }
532 dragger->updateDependencies(false);
534 drag->keep_selection = (bool) g_list_find(drag->selected, dragger);
535 }
538 /**
539 Called when a midpoint knot is dragged.
540 */
541 static void
542 gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
543 {
544 GrDragger *dragger = (GrDragger *) data;
545 GrDrag *drag = dragger->parent;
546 // a midpoint dragger can (logically) only contain one GrDraggable
547 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
549 // FIXME: take from prefs
550 double snap_fraction = 0.1;
552 NR::Point p = *ppointer;
553 NR::Point begin(0,0), end(0,0);
555 SPObject *server;
556 if (draggable->fill_or_stroke)
557 server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
558 else
559 server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
562 // get begin and end points between which dragging is allowed:
563 // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1)
564 GSList *moving = NULL;
565 moving = g_slist_append(moving, dragger);
567 guint lowest_i = draggable->point_i;
568 guint highest_i = draggable->point_i;
569 GrDragger *lowest_dragger = dragger;
570 GrDragger *highest_dragger = dragger;
571 bool is_selected = g_list_find(drag->selected, dragger);
572 if (is_selected) {
573 GrDragger* d_add;
574 while ( true )
575 {
576 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
577 if ( d_add && g_list_find(drag->selected, d_add) ) {
578 lowest_i = lowest_i - 1;
579 moving = g_slist_prepend(moving, d_add);
580 lowest_dragger = d_add;
581 } else {
582 break;
583 }
584 }
586 while ( true )
587 {
588 d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
589 if ( d_add && g_list_find(drag->selected, d_add) ) {
590 highest_i = highest_i + 1;
591 moving = g_slist_append(moving, d_add);
592 highest_dragger = d_add;
593 } else {
594 break;
595 }
596 }
597 }
599 if ( SP_IS_LINEARGRADIENT(server) ) {
600 GrDragger *d_temp;
601 if (lowest_i == 1) {
602 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke);
603 } else {
604 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke);
605 }
606 if (d_temp) begin = d_temp->point;
608 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke);
609 if (d_temp == NULL) {
610 d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke);
611 }
612 if (d_temp) end = d_temp->point;
613 } else if ( SP_IS_RADIALGRADIENT(server) ) {
614 GrDragger *d_temp;
615 if (lowest_i == 1) {
616 d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke);
617 } else {
618 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke);
619 }
620 if (d_temp) begin = d_temp->point;
622 d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke);
623 if (d_temp == NULL) {
624 d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, 0, draggable->fill_or_stroke);
625 }
626 if (d_temp) end = d_temp->point;
627 }
629 NR::Point low_lim = dragger->point - (lowest_dragger->point - begin);
630 NR::Point high_lim = dragger->point - (highest_dragger->point - end);
632 if (state & GDK_CONTROL_MASK) {
633 p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction);
634 } else {
635 p = snap_vector_midpoint (p, low_lim, high_lim, 0);
636 }
637 NR::Point displacement = p - dragger->point;
639 for (GSList const* i = moving; i != NULL; i = i->next) {
640 GrDragger *drg = (GrDragger*) i->data;
641 SPKnot *drgknot = drg->knot;
642 drg->point += displacement;
643 sp_knot_moveto (drgknot, & drg->point);
644 drg->fireDraggables (false);
645 drg->updateDependencies(false);
646 }
648 drag->keep_selection = is_selected;
649 }
653 static void
654 gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
655 {
656 GrDragger *dragger = (GrDragger *) data;
658 sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
659 }
661 /**
662 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
663 */
664 static void
665 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
666 {
667 GrDragger *dragger = (GrDragger *) data;
669 sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
671 dragger->point_original = dragger->point = knot->pos;
673 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
674 dragger->fireDraggables (true, true);
675 } else {
676 dragger->fireDraggables (true);
677 }
679 // make this dragger selected
680 if (!dragger->parent->keep_selection) {
681 dragger->parent->setSelected (dragger);
682 }
683 dragger->parent->keep_selection = false;
685 dragger->updateDependencies(true);
687 // we did an undoable action
688 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
689 _("Move gradient handle"));
690 }
692 /**
693 Called when a dragger knot is clicked; selects the dragger or deletes it depending on the
694 state of the keyboard keys
695 */
696 static void
697 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
698 {
699 GrDragger *dragger = (GrDragger *) data;
700 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
701 if (!draggable) return;
703 if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) {
704 // delete this knot from vector
705 SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke);
706 gradient = sp_gradient_get_vector (gradient, false);
707 if (gradient->vector.stops.size() > 2) { // 2 is the minimum
708 SPStop *stop;
709 switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge
710 case POINT_LG_BEGIN:
711 case POINT_RG_CENTER:
712 stop = sp_first_stop(gradient);
713 {
714 SPStop *next = sp_next_stop (stop);
715 if (next) {
716 next->offset = 0;
717 sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0);
718 }
719 }
720 break;
721 case POINT_LG_END:
722 case POINT_RG_R1:
723 case POINT_RG_R2:
724 stop = sp_last_stop(gradient);
725 {
726 SPStop *prev = sp_prev_stop (stop, gradient);
727 if (prev) {
728 prev->offset = 1;
729 sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1);
730 }
731 }
732 break;
733 case POINT_LG_MID:
734 case POINT_RG_MID1:
735 case POINT_RG_MID2:
736 stop = sp_get_stop_i(gradient, draggable->point_i);
737 break;
738 }
740 SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop));
741 sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT,
742 _("Delete gradient stop"));
743 }
744 } else {
745 // select the dragger
746 dragger->point_original = dragger->point;
748 if ( state & GDK_SHIFT_MASK ) {
749 dragger->parent->setSelected (dragger, true, false);
750 } else {
751 dragger->parent->setSelected (dragger);
752 }
753 }
754 }
756 /**
757 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
758 */
759 static void
760 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
761 {
762 GrDragger *dragger = (GrDragger *) data;
764 dragger->point_original = dragger->point;
766 if (dragger->draggables == NULL)
767 return;
769 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
770 sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
771 }
773 /**
774 Act upon all draggables of the dragger, setting them to the dragger's point
775 */
776 void
777 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
778 {
779 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
780 GrDraggable *draggable = (GrDraggable *) i->data;
782 // set local_change flag so that selection_changed callback does not regenerate draggers
783 this->parent->local_change = true;
785 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
786 // to the center, unless it's the first update upon merge when we must snap it to the point
787 if (merging_focus ||
788 !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke)))
789 sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
790 }
791 }
793 /**
794 Checks if the dragger has a draggable with this point_type
795 */
796 bool
797 GrDragger::isA (guint point_type)
798 {
799 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
800 GrDraggable *draggable = (GrDraggable *) i->data;
801 if (draggable->point_type == point_type) {
802 return true;
803 }
804 }
805 return false;
806 }
808 /**
809 Checks if the dragger has a draggable with this item, point_type, fill_or_stroke
810 */
811 bool
812 GrDragger::isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
813 {
814 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
815 GrDraggable *draggable = (GrDraggable *) i->data;
816 if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) {
817 return true;
818 }
819 }
820 return false;
821 }
823 bool
824 GrDraggable::mayMerge (GrDraggable *da2)
825 {
826 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
827 // we must not merge the points of the same gradient!
828 if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) ||
829 (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) {
830 // except that we can snap center and focus together
831 return false;
832 }
833 }
834 // disable merging of midpoints.
835 if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID)
836 || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1)
837 || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) )
838 return false;
840 return true;
841 }
843 bool
844 GrDragger::mayMerge (GrDragger *other)
845 {
846 if (this == other)
847 return false;
849 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
850 GrDraggable *da1 = (GrDraggable *) i->data;
851 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
852 GrDraggable *da2 = (GrDraggable *) j->data;
853 if (!da1->mayMerge(da2))
854 return false;
855 }
856 }
857 return true;
858 }
860 bool
861 GrDragger::mayMerge (GrDraggable *da2)
862 {
863 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
864 GrDraggable *da1 = (GrDraggable *) i->data;
865 if (!da1->mayMerge(da2))
866 return false;
867 }
868 return true;
869 }
871 /**
872 Updates the statusbar tip of the dragger knot, based on its draggables
873 */
874 void
875 GrDragger::updateTip ()
876 {
877 if (this->knot && this->knot->tip) {
878 g_free (this->knot->tip);
879 this->knot->tip = NULL;
880 }
882 if (g_slist_length (this->draggables) == 1) {
883 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
884 char *item_desc = sp_item_description(draggable->item);
885 switch (draggable->point_type) {
886 case POINT_LG_MID:
887 case POINT_RG_MID1:
888 case POINT_RG_MID2:
889 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"),
890 _(gr_knot_descr[draggable->point_type]),
891 draggable->point_i,
892 item_desc,
893 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
894 break;
896 default:
897 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"),
898 _(gr_knot_descr[draggable->point_type]),
899 item_desc,
900 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
901 break;
902 }
903 g_free(item_desc);
904 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
905 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
906 } else {
907 int length = g_slist_length (this->draggables);
908 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
909 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
910 length),
911 length);
912 }
913 }
915 /**
916 Adds a draggable to the dragger
917 */
918 void
919 GrDragger::updateKnotShape ()
920 {
921 if (!draggables)
922 return;
923 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
924 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL);
925 }
927 /**
928 Adds a draggable to the dragger
929 */
930 void
931 GrDragger::addDraggable (GrDraggable *draggable)
932 {
933 this->draggables = g_slist_prepend (this->draggables, draggable);
935 this->updateTip();
936 }
939 /**
940 Moves this dragger to the point of the given draggable, acting upon all other draggables
941 */
942 void
943 GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
944 {
945 this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke);
946 this->point_original = this->point;
948 sp_knot_moveto (this->knot, &(this->point));
950 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
951 GrDraggable *da = (GrDraggable *) i->data;
952 if ( (da->item == item) && (da->point_type == point_type) && (da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) {
953 continue;
954 }
955 sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false);
956 }
957 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
958 }
961 /**
962 Moves all midstop draggables that depend on this one
963 */
964 void
965 GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) {
966 SPObject *server;
967 if (draggable->fill_or_stroke)
968 server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item);
969 else
970 server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item);
971 guint num = SP_GRADIENT(server)->vector.stops.size();
972 if (num <= 2) return;
974 if ( SP_IS_LINEARGRADIENT(server) ) {
975 for ( guint i = 1; i < num - 1; i++ ) {
976 this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr);
977 }
978 } else if ( SP_IS_RADIALGRADIENT(server) ) {
979 for ( guint i = 1; i < num - 1; i++ ) {
980 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr);
981 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr);
982 }
983 }
984 }
987 /**
988 Moves all draggables that depend on this one
989 */
990 void
991 GrDragger::updateDependencies (bool write_repr)
992 {
993 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
994 GrDraggable *draggable = (GrDraggable *) i->data;
995 switch (draggable->point_type) {
996 case POINT_LG_BEGIN:
997 {
998 // the end point is dependent only when dragging with ctrl+shift
999 this->moveOtherToDraggable (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke, write_repr);
1001 this->updateMidstopDependencies (draggable, write_repr);
1002 }
1003 break;
1004 case POINT_LG_END:
1005 {
1006 // the begin point is dependent only when dragging with ctrl+shift
1007 this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr);
1009 this->updateMidstopDependencies (draggable, write_repr);
1010 }
1011 break;
1012 case POINT_LG_MID:
1013 // no other nodes depend on mid points.
1014 break;
1015 case POINT_RG_R2:
1016 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1017 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1018 this->updateMidstopDependencies (draggable, write_repr);
1019 break;
1020 case POINT_RG_R1:
1021 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1022 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1023 this->updateMidstopDependencies (draggable, write_repr);
1024 break;
1025 case POINT_RG_CENTER:
1026 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr);
1027 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr);
1028 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr);
1029 this->updateMidstopDependencies (draggable, write_repr);
1030 break;
1031 case POINT_RG_FOCUS:
1032 // nothing can depend on that
1033 break;
1034 case POINT_RG_MID1:
1035 this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr);
1036 break;
1037 case POINT_RG_MID2:
1038 this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr);
1039 break;
1040 default:
1041 break;
1042 }
1043 }
1044 }
1048 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
1049 {
1050 this->draggables = NULL;
1052 this->parent = parent;
1054 this->point = p;
1055 this->point_original = p;
1057 // create the knot
1058 this->knot = sp_knot_new (parent->desktop, NULL);
1059 this->knot->setMode(SP_KNOT_MODE_XOR);
1060 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
1061 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1062 sp_knot_update_ctrl(this->knot);
1064 // move knot to the given point
1065 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
1066 sp_knot_show (this->knot);
1068 // connect knot's signals
1069 if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center)
1070 // luckily, midstops never snap to other nodes so are never unsnapped...
1071 && ( (draggable->point_type == POINT_LG_MID)
1072 || (draggable->point_type == POINT_RG_MID1)
1073 || (draggable->point_type == POINT_RG_MID2) ) )
1074 {
1075 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this);
1076 } else {
1077 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
1078 }
1079 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
1080 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
1081 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this);
1082 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
1084 // add the initial draggable
1085 if (draggable)
1086 this->addDraggable (draggable);
1087 updateKnotShape();
1088 }
1090 GrDragger::~GrDragger ()
1091 {
1092 // unselect if it was selected
1093 this->parent->setDeselected(this);
1095 // disconnect signals
1096 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this);
1097 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_clicked_handler), this);
1098 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_doubleclicked_handler), this);
1099 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_grabbed_handler), this);
1100 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_ungrabbed_handler), this);
1102 /* unref should call destroy */
1103 g_object_unref (G_OBJECT (this->knot));
1105 // delete all draggables
1106 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
1107 delete ((GrDraggable *) i->data);
1108 }
1109 g_slist_free (this->draggables);
1110 this->draggables = NULL;
1111 }
1113 /**
1114 Select the dragger which has the given draggable.
1115 */
1116 GrDragger *
1117 GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1118 {
1119 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1120 GrDragger *dragger = (GrDragger *) i->data;
1121 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1122 GrDraggable *da2 = (GrDraggable *) j->data;
1123 if ( (da2->item == item) && (da2->point_type == point_type) && (da2->point_i == point_i) && (da2->fill_or_stroke == fill_or_stroke)) {
1124 return (dragger);
1125 }
1126 }
1127 }
1128 return NULL;
1129 }
1132 void
1133 GrDragger::moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr)
1134 {
1135 GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke);
1136 if (d && d != this) {
1137 d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr);
1138 }
1139 }
1142 /**
1143 Draw this dragger as selected
1144 */
1145 void
1146 GrDragger::select()
1147 {
1148 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
1149 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
1150 }
1152 /**
1153 Draw this dragger as normal (deselected)
1154 */
1155 void
1156 GrDragger::deselect()
1157 {
1158 this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
1159 g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
1160 }
1164 /**
1165 \brief Deselect all stops/draggers
1166 */
1167 void
1168 GrDrag::deselect_all()
1169 {
1170 while (selected) {
1171 ( (GrDragger*) selected->data)->deselect();
1172 selected = g_list_remove(selected, selected->data);
1173 }
1174 }
1176 /**
1177 \brief Select a dragger
1178 \param dragger The dragger to select
1179 \param add_to_selection If true, add to selection, otherwise deselect others
1180 \param override If true, always select this node, otherwise toggle selected status
1181 */
1182 void
1183 GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override)
1184 {
1185 GrDragger *seldragger = NULL;
1187 if (add_to_selection) {
1188 if (!dragger) return;
1189 if (override) {
1190 if (!g_list_find(selected, dragger)) {
1191 selected = g_list_prepend(selected, dragger);
1192 }
1193 dragger->select();
1194 seldragger = dragger;
1195 } else { // toggle
1196 if (g_list_find(selected, dragger)) {
1197 selected = g_list_remove(selected, dragger);
1198 dragger->deselect();
1199 if (selected) {
1200 seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list
1201 }
1202 } else {
1203 selected = g_list_prepend(selected, dragger);
1204 dragger->select();
1205 seldragger = dragger;
1206 }
1207 }
1208 } else {
1209 deselect_all();
1210 if (dragger) {
1211 selected = g_list_prepend(selected, dragger);
1212 dragger->select();
1213 seldragger = dragger;
1214 }
1215 }
1216 if (seldragger) {
1217 this->desktop->emitToolSubselectionChanged((gpointer) seldragger);
1218 }
1219 }
1221 /**
1222 \brief Deselect a dragger
1223 \param dragger The dragger to deselect
1224 */
1225 void
1226 GrDrag::setDeselected (GrDragger *dragger)
1227 {
1228 if (g_list_find(selected, dragger)) {
1229 selected = g_list_remove(selected, dragger);
1230 dragger->deselect();
1231 }
1232 this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL ));
1233 }
1237 /**
1238 Create a line from p1 to p2 and add it to the lines list
1239 */
1240 void
1241 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
1242 {
1243 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
1244 SP_TYPE_CTRLLINE, NULL);
1245 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
1246 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
1247 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
1248 sp_canvas_item_show (line);
1249 this->lines = g_slist_append (this->lines, line);
1250 }
1252 /**
1253 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
1254 new dragger and add it to draggers list
1255 */
1256 void
1257 GrDrag::addDragger (GrDraggable *draggable)
1258 {
1259 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke);
1261 for (GList *i = this->draggers; i != NULL; i = i->next) {
1262 GrDragger *dragger = (GrDragger *) i->data;
1263 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
1264 // distance is small, merge this draggable into dragger, no need to create new dragger
1265 dragger->addDraggable (draggable);
1266 dragger->updateKnotShape();
1267 return;
1268 }
1269 }
1271 GrDragger *new_dragger = new GrDragger(this, p, draggable);
1272 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
1273 this->draggers = g_list_append (this->draggers, new_dragger);
1274 }
1276 /**
1277 Add draggers for the radial gradient rg on item
1278 */
1279 void
1280 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
1281 {
1282 addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke));
1283 guint num = rg->vector.stops.size();
1284 if (num > 2) {
1285 for ( guint i = 1; i < num - 1; i++ ) {
1286 addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke));
1287 }
1288 }
1289 addDragger (new GrDraggable (item, POINT_RG_R1, 0, fill_or_stroke));
1290 if (num > 2) {
1291 for ( guint i = 1; i < num - 1; i++ ) {
1292 addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke));
1293 }
1294 }
1295 addDragger (new GrDraggable (item, POINT_RG_R2, 0, fill_or_stroke));
1296 addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke));
1297 }
1299 /**
1300 Add draggers for the linear gradient lg on item
1301 */
1302 void
1303 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
1304 {
1305 addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke));
1306 guint num = lg->vector.stops.size();
1307 if (num > 2) {
1308 for ( guint i = 1; i < num - 1; i++ ) {
1309 addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke));
1310 }
1311 }
1312 addDragger (new GrDraggable (item, POINT_LG_END, 0, fill_or_stroke));
1313 }
1315 /**
1316 Artificially grab the knot of the dragger with this draggable; used by the gradient context
1317 */
1318 void
1319 GrDrag::grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime)
1320 {
1321 GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke);
1322 if (dragger) {
1323 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
1324 }
1325 }
1327 /**
1328 Regenerates the draggers list from the current selection; is called when selection is changed or
1329 modified, also when a radial dragger needs to update positions of other draggers in the gradient
1330 */
1331 void
1332 GrDrag::updateDraggers ()
1333 {
1334 while (selected) {
1335 selected = g_list_remove(selected, selected->data);
1336 }
1337 // delete old draggers
1338 for (GList const* i = this->draggers; i != NULL; i = i->next) {
1339 delete ((GrDragger *) i->data);
1340 }
1341 g_list_free (this->draggers);
1342 this->draggers = NULL;
1344 g_return_if_fail (this->selection != NULL);
1346 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1348 SPItem *item = SP_ITEM(i->data);
1349 SPStyle *style = SP_OBJECT_STYLE (item);
1351 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1352 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1353 if (SP_IS_LINEARGRADIENT (server)) {
1354 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
1355 } else if (SP_IS_RADIALGRADIENT (server)) {
1356 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
1357 }
1358 }
1360 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1361 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1362 if (SP_IS_LINEARGRADIENT (server)) {
1363 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
1364 } else if (SP_IS_RADIALGRADIENT (server)) {
1365 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
1366 }
1367 }
1368 }
1369 }
1371 /**
1372 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
1373 lines are always in sync with the actual gradient
1374 */
1375 void
1376 GrDrag::updateLines ()
1377 {
1378 // delete old lines
1379 for (GSList const *i = this->lines; i != NULL; i = i->next) {
1380 gtk_object_destroy( GTK_OBJECT (i->data));
1381 }
1382 g_slist_free (this->lines);
1383 this->lines = NULL;
1385 g_return_if_fail (this->selection != NULL);
1387 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1389 SPItem *item = SP_ITEM(i->data);
1391 SPStyle *style = SP_OBJECT_STYLE (item);
1393 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1394 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1395 if (SP_IS_LINEARGRADIENT (server)) {
1396 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);
1397 } else if (SP_IS_RADIALGRADIENT (server)) {
1398 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true);
1399 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL);
1400 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL);
1401 }
1402 }
1404 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1405 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1406 if (SP_IS_LINEARGRADIENT (server)) {
1407 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);
1408 } else if (SP_IS_RADIALGRADIENT (server)) {
1409 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false);
1410 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE);
1411 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE);
1412 }
1413 }
1414 }
1415 }
1417 /**
1418 Regenerates the levels list from the current selection
1419 */
1420 void
1421 GrDrag::updateLevels ()
1422 {
1423 hor_levels.clear();
1424 vert_levels.clear();
1426 g_return_if_fail (this->selection != NULL);
1428 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1429 SPItem *item = SP_ITEM(i->data);
1430 NR::Rect rect = sp_item_bbox_desktop (item);
1431 // Remember the edges of the bbox and the center axis
1432 hor_levels.push_back(rect.min()[NR::Y]);
1433 hor_levels.push_back(rect.max()[NR::Y]);
1434 hor_levels.push_back(0.5 * (rect.min()[NR::Y] + rect.max()[NR::Y]));
1435 vert_levels.push_back(rect.min()[NR::X]);
1436 vert_levels.push_back(rect.max()[NR::X]);
1437 vert_levels.push_back(0.5 * (rect.min()[NR::X] + rect.max()[NR::X]));
1438 }
1439 }
1441 void
1442 GrDrag::selected_reverse_vector ()
1443 {
1444 if (selected == NULL)
1445 return;
1447 for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) {
1448 GrDraggable *draggable = (GrDraggable *) i->data;
1450 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1451 }
1452 }
1454 void
1455 GrDrag::selected_move (double x, double y)
1456 {
1457 /*
1458 if (selected == NULL)
1459 return;
1461 if ( (g_list_length(selected) == 1) )
1462 selected->point += NR::Point (x, y);
1463 selected->point_original = selected->point;
1464 sp_knot_moveto (selected->knot, &(selected->point));
1466 selected->fireDraggables (true);
1468 selected->updateDependencies(true);
1470 // we did an undoable action
1471 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
1472 _("Move gradient handle"));
1473 */
1474 }
1476 void
1477 GrDrag::selected_move_screen (double x, double y)
1478 {
1479 gdouble zoom = desktop->current_zoom();
1480 gdouble zx = x / zoom;
1481 gdouble zy = y / zoom;
1483 selected_move (zx, zy);
1484 }
1486 /**
1487 Select the knot next to the last selected one and deselect all other selected.
1488 */
1489 void
1490 GrDrag::select_next ()
1491 {
1492 if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) {
1493 if (draggers)
1494 setSelected ((GrDragger *) draggers->data);
1495 } else {
1496 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data);
1497 }
1498 }
1500 /**
1501 Select the knot previous from the last selected one and deselect all other selected.
1502 */
1503 void
1504 GrDrag::select_prev ()
1505 {
1506 if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) {
1507 if (draggers)
1508 setSelected ((GrDragger *) g_list_last (draggers)->data);
1509 } else {
1510 setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data);
1511 }
1512 }
1515 /*
1516 Local Variables:
1517 mode:c++
1518 c-file-style:"stroustrup"
1519 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1520 indent-tabs-mode:nil
1521 fill-column:99
1522 End:
1523 */
1524 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :