1 #define __GRADIENT_DRAG_C__
3 /*
4 * On-canvas gradient dragging
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 *
9 * Copyright (C) 2005 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
18 #include <glibmm/i18n.h>
20 #include "desktop-handles.h"
21 #include "selection.h"
22 #include "desktop.h"
23 #include "desktop-style.h"
24 #include "document.h"
25 #include "display/sp-ctrlline.h"
27 #include "xml/repr.h"
29 #include "prefs-utils.h"
30 #include "sp-item.h"
31 #include "style.h"
32 #include "knot.h"
33 #include "sp-linear-gradient.h"
34 #include "sp-radial-gradient.h"
35 #include "gradient-chemistry.h"
36 #include "gradient-drag.h"
38 #define GR_KNOT_COLOR_NORMAL 0xffffff00
39 #define GR_KNOT_COLOR_SELECTED 0x0000ff00
41 #define GR_LINE_COLOR_FILL 0x0000ff7f
42 #define GR_LINE_COLOR_STROKE 0x9999007f
44 // screen pixels between knots when they snap:
45 #define SNAP_DIST 5
47 // absolute distance between gradient points for them to become a single dragger when the drag is created:
48 #define MERGE_DIST 0.1
50 // knot shapes corresponding to GrPoint enum
51 SPKnotShapeType gr_knot_shapes [] = {
52 SP_KNOT_SHAPE_SQUARE, //POINT_LG_P1
53 SP_KNOT_SHAPE_SQUARE,
54 SP_KNOT_SHAPE_DIAMOND,
55 SP_KNOT_SHAPE_CIRCLE,
56 SP_KNOT_SHAPE_CIRCLE,
57 SP_KNOT_SHAPE_CROSS // POINT_RG_FOCUS
58 };
60 const gchar *gr_knot_descr [] = {
61 N_("Linear gradient <b>start</b>"), //POINT_LG_P1
62 N_("Linear gradient <b>end</b>"),
63 N_("Radial gradient <b>center</b>"),
64 N_("Radial gradient <b>radius</b>"),
65 N_("Radial gradient <b>radius</b>"),
66 N_("Radial gradient <b>focus</b>") // POINT_RG_FOCUS
67 };
69 static void
70 gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
71 {
72 GrDrag *drag = (GrDrag *) data;
73 drag->updateDraggers ();
74 drag->updateLines ();
75 drag->updateLevels ();
76 }
78 static void
79 gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
80 {
81 GrDrag *drag = (GrDrag *) data;
82 if (drag->local_change) {
83 drag->local_change = false;
84 } else {
85 drag->updateDraggers ();
86 }
87 drag->updateLines ();
88 drag->updateLevels ();
89 }
91 /**
92 When a _query_style_signal is received, check that \a property requests fill/stroke (otherwise
93 skip), and fill the \a style with the averaged color of all draggables of the selected dragger, if
94 any.
95 */
96 int
97 gr_drag_style_query (SPStyle *style, int property, gpointer data)
98 {
99 GrDrag *drag = (GrDrag *) data;
101 if (property != QUERY_STYLE_PROPERTY_FILL && property != QUERY_STYLE_PROPERTY_STROKE) {
102 return QUERY_STYLE_NOTHING;
103 }
105 if (!drag->selected) {
106 return QUERY_STYLE_NOTHING;
107 } else {
108 int ret = QUERY_STYLE_NOTHING;
110 float cf[4];
111 cf[0] = cf[1] = cf[2] = cf[3] = 0;
113 int count = 0;
115 for (GSList const* i = drag->selected->draggables; i != NULL; i = i->next) { // for all draggables of dragger
116 GrDraggable *draggable = (GrDraggable *) i->data;
118 if (ret == QUERY_STYLE_NOTHING) {
119 ret = QUERY_STYLE_SINGLE;
120 } else if (ret == QUERY_STYLE_SINGLE) {
121 ret = QUERY_STYLE_MULTIPLE_AVERAGED;
122 }
124 guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_num, draggable->fill_or_stroke);
125 cf[0] += SP_RGBA32_R_F (c);
126 cf[1] += SP_RGBA32_G_F (c);
127 cf[2] += SP_RGBA32_B_F (c);
128 cf[3] += SP_RGBA32_A_F (c);
130 count ++;
131 }
133 if (count) {
134 cf[0] /= count;
135 cf[1] /= count;
136 cf[2] /= count;
137 cf[3] /= count;
139 // set both fill and stroke with our stop-color and stop-opacity
140 sp_color_set_rgb_float((SPColor *) &style->fill.value.color, cf[0], cf[1], cf[2]);
141 style->fill.set = TRUE;
142 style->fill.type = SP_PAINT_TYPE_COLOR;
143 sp_color_set_rgb_float((SPColor *) &style->stroke.value.color, cf[0], cf[1], cf[2]);
144 style->stroke.set = TRUE;
145 style->stroke.type = SP_PAINT_TYPE_COLOR;
147 style->fill_opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
148 style->fill_opacity.set = TRUE;
149 style->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (cf[3]);
150 style->stroke_opacity.set = TRUE;
151 }
153 return ret;
154 }
155 }
157 bool
158 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
159 {
160 GrDrag *drag = (GrDrag *) data;
162 if (!drag->selected)
163 return false;
165 SPCSSAttr *stop = sp_repr_css_attr_new ();
167 // See if the css contains interesting properties, and if so, translate them into the format
168 // acceptable for gradient stops
170 // any of color properties, in order of increasing priority:
171 if (css->attribute("flood-color"))
172 sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color"));
174 if (css->attribute("lighting-color"))
175 sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color"));
177 if (css->attribute("color"))
178 sp_repr_css_set_property (stop, "stop-color", css->attribute("color"));
180 if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none"))
181 sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke"));
183 if (css->attribute("fill") && strcmp(css->attribute("fill"), "none"))
184 sp_repr_css_set_property (stop, "stop-color", css->attribute("fill"));
186 if (css->attribute("stop-color"))
187 sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color"));
189 // any of opacity properties, in order of increasing priority:
190 if (css->attribute("flood-opacity"))
191 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("flood-color"));
193 if (css->attribute("opacity")) // TODO: multiply
194 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("color"));
196 if (css->attribute("stroke-opacity")) // TODO: multiply
197 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stroke-opacity"));
199 if (css->attribute("fill-opacity")) // TODO: multiply
200 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("fill-opacity"));
202 if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) ||
203 (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none")))
204 sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0
206 if (css->attribute("stop-opacity"))
207 sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity"));
209 if (!stop->attributeList()) { // nothing for us here, pass it on
210 sp_repr_css_attr_unref(stop);
211 return false;
212 }
214 for (GSList const* i = drag->selected->draggables; i != NULL; i = i->next) { // for all draggables of dragger
215 GrDraggable *draggable = (GrDraggable *) i->data;
217 drag->local_change = true;
218 sp_item_gradient_stop_set_style (draggable->item, draggable->point_num, draggable->fill_or_stroke, stop);
219 }
221 //sp_repr_css_print(stop);
222 sp_repr_css_attr_unref(stop);
223 return true;
224 }
226 GrDrag::GrDrag(SPDesktop *desktop) {
228 this->desktop = desktop;
230 this->selection = sp_desktop_selection(desktop);
232 this->draggers = NULL;
233 this->lines = NULL;
234 this->selected = NULL;
236 this->hor_levels.clear();
237 this->vert_levels.clear();
239 this->local_change = false;
241 this->sel_changed_connection = this->selection->connectChanged(
242 sigc::bind (
243 sigc::ptr_fun(&gr_drag_sel_changed),
244 (gpointer)this )
246 );
247 this->sel_modified_connection = this->selection->connectModified(
248 sigc::bind(
249 sigc::ptr_fun(&gr_drag_sel_modified),
250 (gpointer)this )
251 );
253 this->style_set_connection = this->desktop->connectSetStyle(
254 sigc::bind(
255 sigc::ptr_fun(&gr_drag_style_set),
256 (gpointer)this )
257 );
259 this->style_query_connection = this->desktop->connectQueryStyle(
260 sigc::bind(
261 sigc::ptr_fun(&gr_drag_style_query),
262 (gpointer)this )
263 );
265 this->updateDraggers ();
266 this->updateLines ();
267 this->updateLevels ();
269 if (desktop->gr_item) {
270 this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_num, desktop->gr_fill_or_stroke));
271 }
272 }
274 GrDrag::~GrDrag()
275 {
276 this->sel_changed_connection.disconnect();
277 this->sel_modified_connection.disconnect();
278 this->style_set_connection.disconnect();
279 this->style_query_connection.disconnect();
281 if (this->selected) {
282 GrDraggable *draggable = (GrDraggable *) this->selected->draggables->data;
283 desktop->gr_item = draggable->item;
284 desktop->gr_point_num = draggable->point_num;
285 desktop->gr_fill_or_stroke = draggable->fill_or_stroke;
286 } else {
287 desktop->gr_item = NULL;
288 desktop->gr_point_num = 0;
289 desktop->gr_fill_or_stroke = true;
290 }
292 for (GList *l = this->draggers; l != NULL; l = l->next) {
293 delete ((GrDragger *) l->data);
294 }
295 g_list_free (this->draggers);
296 this->draggers = NULL;
297 this->selected = NULL;
299 for (GSList *l = this->lines; l != NULL; l = l->next) {
300 gtk_object_destroy( GTK_OBJECT (l->data));
301 }
302 g_slist_free (this->lines);
303 this->lines = NULL;
304 }
306 GrDraggable::GrDraggable (SPItem *item, guint point_num, bool fill_or_stroke)
307 {
308 this->item = item;
309 this->point_num = point_num;
310 this->fill_or_stroke = fill_or_stroke;
312 g_object_ref (G_OBJECT (this->item));
313 }
315 GrDraggable::~GrDraggable ()
316 {
317 g_object_unref (G_OBJECT (this->item));
318 }
320 NR::Point *
321 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
322 {
323 double r = NR::L2 (p - o);
324 if (r < 1e-3)
325 return NULL;
326 double angle = NR::atan2 (p - o);
327 // snap angle to snaps increments, starting from initial:
328 double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
329 // calculate the new position and subtract p to get the vector:
330 return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
331 }
333 static void
334 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
335 {
336 GrDragger *dragger = (GrDragger *) data;
338 NR::Point p = *ppointer;
340 // FIXME: take from prefs
341 double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom();
343 if (state & GDK_SHIFT_MASK) {
344 // with Shift; unsnap if we carry more than one draggable
345 if (dragger->draggables && dragger->draggables->next) {
346 // create a new dragger
347 GrDragger *dr_new = new GrDragger (dragger->parent, dragger->point, NULL);
348 dragger->parent->draggers = g_list_prepend (dragger->parent->draggers, dr_new);
349 // relink to it all but the first draggable in the list
350 for (GSList const* i = dragger->draggables->next; i != NULL; i = i->next) {
351 GrDraggable *draggable = (GrDraggable *) i->data;
352 dr_new->addDraggable (draggable);
353 }
354 dr_new->updateKnotShape();
355 g_slist_free (dragger->draggables->next);
356 dragger->draggables->next = NULL;
357 dragger->updateKnotShape();
358 dragger->updateTip();
359 }
360 } else if (!(state & GDK_CONTROL_MASK)) {
361 // without Shift or Ctrl; see if we need to snap to another dragger
362 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
363 GrDragger *d_new = (GrDragger *) di->data;
364 if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) {
366 // Merge draggers:
367 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger
368 GrDraggable *draggable = (GrDraggable *) i->data;
369 // copy draggable to d_new:
370 GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_num, draggable->fill_or_stroke);
371 d_new->addDraggable (da_new);
372 }
374 // unlink and delete this dragger
375 dragger->parent->draggers = g_list_remove (dragger->parent->draggers, dragger);
376 delete dragger;
378 // update the new merged dragger
379 d_new->fireDraggables(true, false, true);
380 d_new->parent->updateLines();
381 d_new->parent->setSelected (d_new);
382 d_new->updateKnotShape ();
383 d_new->updateTip ();
384 d_new->updateDependencies(true);
385 sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
386 _("Merge gradient handles"));
387 return;
388 }
389 }
390 }
392 if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
393 // See if we need to snap to any of the levels
394 for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
395 if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
396 p[NR::Y] = dragger->parent->hor_levels[i];
397 sp_knot_moveto (knot, &p);
398 }
399 }
400 for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
401 if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
402 p[NR::X] = dragger->parent->vert_levels[i];
403 sp_knot_moveto (knot, &p);
404 }
405 }
406 }
408 if (state & GDK_CONTROL_MASK) {
409 unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
410 /* 0 means no snapping. */
412 // This list will store snap vectors from all draggables of dragger
413 GSList *snap_vectors = NULL;
415 for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
416 GrDraggable *draggable = (GrDraggable *) i->data;
418 NR::Point *dr_snap = NULL;
420 if (draggable->point_num == POINT_LG_P1 || draggable->point_num == POINT_LG_P2) {
421 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
422 GrDragger *d_new = (GrDragger *) di->data;
423 if (d_new == dragger)
424 continue;
425 if (d_new->isA (draggable->item,
426 draggable->point_num == POINT_LG_P1? POINT_LG_P2 : POINT_LG_P1,
427 draggable->fill_or_stroke)) {
428 // found the other end of the linear gradient;
429 if (state & GDK_SHIFT_MASK) {
430 // moving linear around center
431 NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
432 dr_snap = ¢er;
433 } else {
434 // moving linear around the other end
435 dr_snap = &d_new->point;
436 }
437 }
438 }
439 } else if (draggable->point_num == POINT_RG_R1 || draggable->point_num == POINT_RG_R2 || draggable->point_num == POINT_RG_FOCUS) {
440 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
441 GrDragger *d_new = (GrDragger *) di->data;
442 if (d_new == dragger)
443 continue;
444 if (d_new->isA (draggable->item,
445 POINT_RG_CENTER,
446 draggable->fill_or_stroke)) {
447 // found the center of the radial gradient;
448 dr_snap = &(d_new->point);
449 }
450 }
451 } else if (draggable->point_num == POINT_RG_CENTER) {
452 // radial center snaps to hor/vert relative to its original position
453 dr_snap = &(dragger->point_original);
454 }
456 NR::Point *snap_vector = NULL;
457 if (dr_snap) {
458 if (state & GDK_MOD1_MASK) {
459 // with Alt, snap to the original angle and its perpendiculars
460 snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
461 } else {
462 // with Ctrl, snap to M_PI/snaps
463 snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
464 }
465 }
466 if (snap_vector) {
467 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
468 }
469 }
471 // Move by the smallest of snap vectors:
472 NR::Point move(9999, 9999);
473 for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
474 NR::Point *snap_vector = (NR::Point *) i->data;
475 if (NR::L2(*snap_vector) < NR::L2(move))
476 move = *snap_vector;
477 }
478 if (move[NR::X] < 9999) {
479 p += move;
480 sp_knot_moveto (knot, &p);
481 }
482 }
484 dragger->point = p;
486 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
487 dragger->fireDraggables (false, true);
488 } else {
489 dragger->fireDraggables (false);
490 }
492 dragger->updateDependencies(false);
493 }
495 /**
496 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
497 */
498 static void
499 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
500 {
501 GrDragger *dragger = (GrDragger *) data;
503 dragger->point_original = dragger->point = knot->pos;
505 if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
506 dragger->fireDraggables (true, true);
507 } else {
508 dragger->fireDraggables (true);
509 }
511 // make this dragger selected
512 dragger->parent->setSelected (dragger);
514 dragger->updateDependencies(true);
516 // we did an undoable action
517 sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT,
518 _("Move gradient handle"));
519 }
521 /**
522 Called when a dragger knot is clicked; selects the dragger
523 */
524 static void
525 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
526 {
527 GrDragger *dragger = (GrDragger *) data;
529 dragger->point_original = dragger->point;
531 dragger->parent->setSelected (dragger);
532 }
534 /**
535 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
536 */
537 static void
538 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
539 {
540 GrDragger *dragger = (GrDragger *) data;
542 dragger->point_original = dragger->point;
544 if (dragger->draggables == NULL)
545 return;
547 GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
548 sp_item_gradient_edit_stop (draggable->item, draggable->point_num, draggable->fill_or_stroke);
549 }
551 /**
552 Act upon all draggables of the dragger, setting them to the dragger's point
553 */
554 void
555 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
556 {
557 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
558 GrDraggable *draggable = (GrDraggable *) i->data;
560 // set local_change flag so that selection_changed callback does not regenerate draggers
561 this->parent->local_change = true;
563 // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
564 // to the center, unless it's the first update upon merge when we must snap it to the point
565 if (merging_focus ||
566 !(draggable->point_num == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->fill_or_stroke)))
567 sp_item_gradient_set_coords (draggable->item, draggable->point_num, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
568 }
569 }
571 /**
572 Checks if the dragger has a draggable with this point_num
573 */
574 bool
575 GrDragger::isA (guint point_num)
576 {
577 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
578 GrDraggable *draggable = (GrDraggable *) i->data;
579 if (draggable->point_num == point_num) {
580 return true;
581 }
582 }
583 return false;
584 }
586 /**
587 Checks if the dragger has a draggable with this item, point_num, fill_or_stroke
588 */
589 bool
590 GrDragger::isA (SPItem *item, guint point_num, bool fill_or_stroke)
591 {
592 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
593 GrDraggable *draggable = (GrDraggable *) i->data;
594 if (draggable->point_num == point_num && draggable->item == item && draggable->fill_or_stroke == fill_or_stroke) {
595 return true;
596 }
597 }
598 return false;
599 }
601 bool
602 GrDraggable::mayMerge (GrDraggable *da2)
603 {
604 if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
605 // we must not merge the points of the same gradient!
606 if (!((this->point_num == POINT_RG_FOCUS && da2->point_num == POINT_RG_CENTER) ||
607 (this->point_num == POINT_RG_CENTER && da2->point_num == POINT_RG_FOCUS))) {
608 // except that we can snap center and focus together
609 return false;
610 }
611 }
612 return true;
613 }
615 bool
616 GrDragger::mayMerge (GrDragger *other)
617 {
618 if (this == other)
619 return false;
621 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
622 GrDraggable *da1 = (GrDraggable *) i->data;
623 for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
624 GrDraggable *da2 = (GrDraggable *) j->data;
625 if (!da1->mayMerge(da2))
626 return false;
627 }
628 }
629 return true;
630 }
632 bool
633 GrDragger::mayMerge (GrDraggable *da2)
634 {
635 for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
636 GrDraggable *da1 = (GrDraggable *) i->data;
637 if (!da1->mayMerge(da2))
638 return false;
639 }
640 return true;
641 }
643 /**
644 Updates the statusbar tip of the dragger knot, based on its draggables
645 */
646 void
647 GrDragger::updateTip ()
648 {
649 if (this->knot && this->knot->tip) {
650 g_free (this->knot->tip);
651 this->knot->tip = NULL;
652 }
654 if (g_slist_length (this->draggables) == 1) {
655 GrDraggable *draggable = (GrDraggable *) this->draggables->data;
656 char *item_desc = sp_item_description(draggable->item);
657 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"),
658 _(gr_knot_descr[draggable->point_num]),
659 item_desc,
660 draggable->fill_or_stroke == false ? _(" (stroke)") : "");
661 g_free(item_desc);
662 } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
663 this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
664 } else {
665 int length = g_slist_length (this->draggables);
666 this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
667 "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
668 length),
669 length);
670 }
671 }
673 /**
674 Adds a draggable to the dragger
675 */
676 void
677 GrDragger::updateKnotShape ()
678 {
679 if (!draggables)
680 return;
681 GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
682 g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_num], NULL);
683 }
685 /**
686 Adds a draggable to the dragger
687 */
688 void
689 GrDragger::addDraggable (GrDraggable *draggable)
690 {
691 this->draggables = g_slist_prepend (this->draggables, draggable);
693 this->updateTip();
694 }
697 /**
698 Moves this dragger to the point of the given draggable, acting upon all other draggables
699 */
700 void
701 GrDragger::moveThisToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr)
702 {
703 this->point = sp_item_gradient_get_coords (item, point_num, fill_or_stroke);
704 this->point_original = this->point;
706 sp_knot_moveto (this->knot, &(this->point));
708 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
709 GrDraggable *da = (GrDraggable *) i->data;
710 if (da->item == item && da->point_num == point_num && da->fill_or_stroke == fill_or_stroke) {
711 continue;
712 }
713 sp_item_gradient_set_coords (da->item, da->point_num, this->point, da->fill_or_stroke, write_repr, false);
714 }
715 // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
716 }
719 /**
720 Moves all draggables that depend on this one
721 */
722 void
723 GrDragger::updateDependencies (bool write_repr)
724 {
725 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
726 GrDraggable *draggable = (GrDraggable *) i->data;
727 switch (draggable->point_num) {
728 case POINT_LG_P1:
729 // the other point is dependent only when dragging with ctrl+shift
730 this->moveOtherToDraggable (draggable->item, POINT_LG_P2, draggable->fill_or_stroke, write_repr);
731 break;
732 case POINT_LG_P2:
733 this->moveOtherToDraggable (draggable->item, POINT_LG_P1, draggable->fill_or_stroke, write_repr);
734 break;
735 case POINT_RG_R2:
736 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, draggable->fill_or_stroke, write_repr);
737 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr);
738 break;
739 case POINT_RG_R1:
740 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, draggable->fill_or_stroke, write_repr);
741 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr);
742 break;
743 case POINT_RG_CENTER:
744 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, draggable->fill_or_stroke, write_repr);
745 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, draggable->fill_or_stroke, write_repr);
746 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr);
747 break;
748 case POINT_RG_FOCUS:
749 // nothing can depend on that
750 break;
751 default:
752 break;
753 }
754 }
755 }
759 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
760 {
761 this->draggables = NULL;
763 this->parent = parent;
765 this->point = p;
766 this->point_original = p;
768 // create the knot
769 this->knot = sp_knot_new (parent->desktop, NULL);
770 this->knot->setMode(SP_KNOT_MODE_XOR);
771 this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
772 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
773 sp_knot_update_ctrl(this->knot);
775 // move knot to the given point
776 sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
777 sp_knot_show (this->knot);
779 // connect knot's signals
780 this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
781 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
782 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
783 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
785 // add the initial draggable
786 if (draggable)
787 this->addDraggable (draggable);
788 updateKnotShape();
789 }
791 GrDragger::~GrDragger ()
792 {
793 // unselect if it was selected
794 if (this->parent->selected == this)
795 this->parent->setSelected (NULL);
797 /* unref should call destroy */
798 g_object_unref (G_OBJECT (this->knot));
800 // delete all draggables
801 for (GSList const* i = this->draggables; i != NULL; i = i->next) {
802 delete ((GrDraggable *) i->data);
803 }
804 g_slist_free (this->draggables);
805 this->draggables = NULL;
806 }
808 /**
809 Select the dragger which has the given draggable.
810 */
811 GrDragger *
812 GrDrag::getDraggerFor (SPItem *item, guint point_num, bool fill_or_stroke)
813 {
814 for (GList const* i = this->draggers; i != NULL; i = i->next) {
815 GrDragger *dragger = (GrDragger *) i->data;
816 for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
817 GrDraggable *da2 = (GrDraggable *) j->data;
818 if (da2->item == item && da2->point_num == point_num && da2->fill_or_stroke == fill_or_stroke) {
819 return (dragger);
820 }
821 }
822 }
823 return NULL;
824 }
827 void
828 GrDragger::moveOtherToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr)
829 {
830 GrDragger *d = this->parent->getDraggerFor (item, point_num, fill_or_stroke);
831 if (d && d != this) {
832 d->moveThisToDraggable (item, point_num, fill_or_stroke, write_repr);
833 }
834 }
837 /**
838 Set selected dragger
839 */
840 void
841 GrDrag::setSelected (GrDragger *dragger)
842 {
843 if (this->selected) {
844 this->selected->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
845 g_object_set (G_OBJECT (this->selected->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
846 }
847 if (dragger) {
848 dragger->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
849 g_object_set (G_OBJECT (dragger->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
850 }
851 this->selected = dragger;
853 this->desktop->emitToolSubselectionChanged((gpointer) dragger);
854 }
856 /**
857 Create a line from p1 to p2 and add it to the lines list
858 */
859 void
860 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
861 {
862 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
863 SP_TYPE_CTRLLINE, NULL);
864 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
865 if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
866 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
867 sp_canvas_item_show (line);
868 this->lines = g_slist_append (this->lines, line);
869 }
871 /**
872 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
873 new dragger and add it to draggers list
874 */
875 void
876 GrDrag::addDragger (GrDraggable *draggable)
877 {
878 NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_num, draggable->fill_or_stroke);
880 for (GList *i = this->draggers; i != NULL; i = i->next) {
881 GrDragger *dragger = (GrDragger *) i->data;
882 if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
883 // distance is small, merge this draggable into dragger, no need to create new dragger
884 dragger->addDraggable (draggable);
885 dragger->updateKnotShape();
886 return;
887 }
888 }
890 GrDragger *new_dragger = new GrDragger(this, p, draggable);
891 this->draggers = g_list_prepend (this->draggers, new_dragger);
892 }
894 /**
895 Add draggers for the radial gradient rg on item
896 */
897 void
898 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
899 {
900 addDragger (new GrDraggable (item, POINT_RG_CENTER, fill_or_stroke));
901 addDragger (new GrDraggable (item, POINT_RG_FOCUS, fill_or_stroke));
902 addDragger (new GrDraggable (item, POINT_RG_R1, fill_or_stroke));
903 addDragger (new GrDraggable (item, POINT_RG_R2, fill_or_stroke));
904 }
906 /**
907 Add draggers for the linear gradient lg on item
908 */
909 void
910 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
911 {
912 addDragger (new GrDraggable (item, POINT_LG_P1, fill_or_stroke));
913 addDragger (new GrDraggable (item, POINT_LG_P2, fill_or_stroke));
914 }
916 /**
917 Artificially grab the knot of the dragger with this draggable; used by the gradient context
918 */
919 void
920 GrDrag::grabKnot (SPItem *item, guint point_num, bool fill_or_stroke, gint x, gint y, guint32 etime)
921 {
922 GrDragger *dragger = getDraggerFor (item, point_num, fill_or_stroke);
923 if (dragger) {
924 sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
925 }
926 }
928 /**
929 Regenerates the draggers list from the current selection; is called when selection is changed or
930 modified, also when a radial dragger needs to update positions of other draggers in the gradient
931 */
932 void
933 GrDrag::updateDraggers ()
934 {
935 // delete old draggers and deselect
936 for (GList const* i = this->draggers; i != NULL; i = i->next) {
937 delete ((GrDragger *) i->data);
938 }
939 g_list_free (this->draggers);
940 this->draggers = NULL;
941 this->selected = NULL;
943 g_return_if_fail (this->selection != NULL);
945 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
947 SPItem *item = SP_ITEM(i->data);
948 SPStyle *style = SP_OBJECT_STYLE (item);
950 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
951 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
952 if (SP_IS_LINEARGRADIENT (server)) {
953 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
954 } else if (SP_IS_RADIALGRADIENT (server)) {
955 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
956 }
957 }
959 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
960 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
961 if (SP_IS_LINEARGRADIENT (server)) {
962 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
963 } else if (SP_IS_RADIALGRADIENT (server)) {
964 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
965 }
966 }
969 }
970 }
972 /**
973 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
974 lines are always in sync with the actual gradient
975 */
976 void
977 GrDrag::updateLines ()
978 {
979 // delete old lines
980 for (GSList const *i = this->lines; i != NULL; i = i->next) {
981 gtk_object_destroy( GTK_OBJECT (i->data));
982 }
983 g_slist_free (this->lines);
984 this->lines = NULL;
986 g_return_if_fail (this->selection != NULL);
988 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
990 SPItem *item = SP_ITEM(i->data);
992 SPStyle *style = SP_OBJECT_STYLE (item);
994 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
995 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
996 if (SP_IS_LINEARGRADIENT (server)) {
997 this->addLine (sp_item_gradient_get_coords (item, POINT_LG_P1, true), sp_item_gradient_get_coords (item, POINT_LG_P2, true), GR_LINE_COLOR_FILL);
998 } else if (SP_IS_RADIALGRADIENT (server)) {
999 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, true);
1000 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, true), GR_LINE_COLOR_FILL);
1001 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, true), GR_LINE_COLOR_FILL);
1002 }
1003 }
1005 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1006 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1007 if (SP_IS_LINEARGRADIENT (server)) {
1008 this->addLine (sp_item_gradient_get_coords (item, POINT_LG_P1, false), sp_item_gradient_get_coords (item, POINT_LG_P2, false), GR_LINE_COLOR_STROKE);
1009 } else if (SP_IS_RADIALGRADIENT (server)) {
1010 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, false);
1011 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, false), GR_LINE_COLOR_STROKE);
1012 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, false), GR_LINE_COLOR_STROKE);
1013 }
1014 }
1015 }
1016 }
1018 /**
1019 Regenerates the levels list from the current selection
1020 */
1021 void
1022 GrDrag::updateLevels ()
1023 {
1024 hor_levels.clear();
1025 vert_levels.clear();
1027 g_return_if_fail (this->selection != NULL);
1029 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1030 SPItem *item = SP_ITEM(i->data);
1031 NR::Rect rect = sp_item_bbox_desktop (item);
1032 // Remember the edges of the bbox and the center axis
1033 hor_levels.push_back(rect.min()[NR::Y]);
1034 hor_levels.push_back(rect.max()[NR::Y]);
1035 hor_levels.push_back(0.5 * (rect.min()[NR::Y] + rect.max()[NR::Y]));
1036 vert_levels.push_back(rect.min()[NR::X]);
1037 vert_levels.push_back(rect.max()[NR::X]);
1038 vert_levels.push_back(0.5 * (rect.min()[NR::X] + rect.max()[NR::X]));
1039 }
1040 }
1042 void
1043 GrDrag::selected_reverse_vector ()
1044 {
1045 if (selected == NULL)
1046 return;
1048 for (GSList const* i = selected->draggables; i != NULL; i = i->next) {
1049 GrDraggable *draggable = (GrDraggable *) i->data;
1051 sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1052 }
1053 }
1055 void
1056 GrDrag::selected_move (double x, double y)
1057 {
1058 if (selected == NULL)
1059 return;
1061 selected->point += NR::Point (x, y);
1062 selected->point_original = selected->point;
1063 sp_knot_moveto (selected->knot, &(selected->point));
1065 selected->fireDraggables (true);
1067 selected->updateDependencies(true);
1069 // we did an undoable action
1070 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
1071 _("Move gradient handle"));
1072 }
1074 void
1075 GrDrag::selected_move_screen (double x, double y)
1076 {
1077 gdouble zoom = desktop->current_zoom();
1078 gdouble zx = x / zoom;
1079 gdouble zy = y / zoom;
1081 selected_move (zx, zy);
1082 }
1084 void
1085 GrDrag::select_next ()
1086 {
1087 if (selected == NULL || g_list_find(draggers, selected)->next == NULL) {
1088 if (draggers)
1089 setSelected ((GrDragger *) draggers->data);
1090 } else {
1091 setSelected ((GrDragger *) g_list_find(draggers, selected)->next->data);
1092 }
1093 }
1095 void
1096 GrDrag::select_prev ()
1097 {
1098 if (selected == NULL || g_list_find(draggers, selected)->prev == NULL) {
1099 if (draggers)
1100 setSelected ((GrDragger *) g_list_last (draggers)->data);
1101 } else {
1102 setSelected ((GrDragger *) g_list_find(draggers, selected)->prev->data);
1103 }
1104 }
1107 /*
1108 Local Variables:
1109 mode:c++
1110 c-file-style:"stroustrup"
1111 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1112 indent-tabs-mode:nil
1113 fill-column:99
1114 End:
1115 */
1116 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :