Code

do some include changes more correctly
[inkscape.git] / src / gradient-drag.cpp
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     }
157 bool
158 gr_drag_style_set (const SPCSSAttr *css, gpointer data)
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;
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     }
274 GrDrag::~GrDrag()
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;
306 GrDraggable::GrDraggable (SPItem *item, guint point_num, bool fill_or_stroke)
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));
315 GrDraggable::~GrDraggable ()
317     g_object_unref (G_OBJECT (this->item));
320 NR::Point *
321 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
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);
333 static void
334 gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
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));
386                 return;
387             }
388         }
389     }
391     if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) {
392         // See if we need to snap to any of the levels
393         for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) {
394             if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) {
395                 p[NR::Y] = dragger->parent->hor_levels[i];
396                 sp_knot_moveto (knot, &p);
397             }
398         }
399         for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) {
400             if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) {
401                 p[NR::X] = dragger->parent->vert_levels[i];
402                 sp_knot_moveto (knot, &p);
403             }
404         }
405     }
407     if (state & GDK_CONTROL_MASK) {
408         unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
409         /* 0 means no snapping. */
411         // This list will store snap vectors from all draggables of dragger
412         GSList *snap_vectors = NULL;
414         for (GSList const* i = dragger->draggables; i != NULL; i = i->next) {
415             GrDraggable *draggable = (GrDraggable *) i->data;
417             NR::Point *dr_snap = NULL;
419             if (draggable->point_num == POINT_LG_P1 || draggable->point_num == POINT_LG_P2) {
420                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
421                     GrDragger *d_new = (GrDragger *) di->data;
422                     if (d_new == dragger)
423                         continue;
424                     if (d_new->isA (draggable->item,
425                                     draggable->point_num == POINT_LG_P1? POINT_LG_P2 : POINT_LG_P1,
426                                     draggable->fill_or_stroke)) {
427                         // found the other end of the linear gradient;
428                         if (state & GDK_SHIFT_MASK) {
429                             // moving linear around center
430                             NR::Point center = NR::Point (0.5*(d_new->point + dragger->point));
431                             dr_snap = &center;
432                         } else {
433                             // moving linear around the other end
434                             dr_snap = &d_new->point;
435                         }
436                     }
437                 }
438             } else if (draggable->point_num == POINT_RG_R1 || draggable->point_num == POINT_RG_R2 || draggable->point_num == POINT_RG_FOCUS) {
439                 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
440                     GrDragger *d_new = (GrDragger *) di->data;
441                     if (d_new == dragger)
442                         continue;
443                     if (d_new->isA (draggable->item,
444                                     POINT_RG_CENTER,
445                                     draggable->fill_or_stroke)) {
446                         // found the center of the radial gradient;
447                         dr_snap = &(d_new->point);
448                     }
449                 }
450             } else if (draggable->point_num == POINT_RG_CENTER) {
451                 // radial center snaps to hor/vert relative to its original position
452                 dr_snap = &(dragger->point_original);
453             }
455             NR::Point *snap_vector = NULL;
456             if (dr_snap) {
457                 if (state & GDK_MOD1_MASK) {
458                     // with Alt, snap to the original angle and its perpendiculars
459                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap));
460                 } else {
461                     // with Ctrl, snap to M_PI/snaps
462                     snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0);
463                 }
464             }
465             if (snap_vector) {
466                 snap_vectors = g_slist_prepend (snap_vectors, snap_vector);
467             }
468         }
470         // Move by the smallest of snap vectors:
471         NR::Point move(9999, 9999);
472         for (GSList const *i = snap_vectors; i != NULL; i = i->next) {
473             NR::Point *snap_vector = (NR::Point *) i->data;
474             if (NR::L2(*snap_vector) < NR::L2(move))
475                 move = *snap_vector;
476         }
477         if (move[NR::X] < 9999) {
478             p += move;
479             sp_knot_moveto (knot, &p);
480         }
481     }
483     dragger->point = p;
485     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
486         dragger->fireDraggables (false, true);
487     } else {
488         dragger->fireDraggables (false);
489     }
491     dragger->updateDependencies(false);
494 /**
495 Called when the mouse releases a dragger knot; changes gradient writing to repr, updates other draggers if needed
496 */
497 static void
498 gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
500     GrDragger *dragger = (GrDragger *) data;
502     dragger->point_original = dragger->point = knot->pos;
504     if ((state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK)) {
505         dragger->fireDraggables (true, true);
506     } else {
507         dragger->fireDraggables (true);
508     }
510     // make this dragger selected
511     dragger->parent->setSelected (dragger);
513     dragger->updateDependencies(true);
515     // we did an undoable action
516     sp_document_done (sp_desktop_document (dragger->parent->desktop));
519 /**
520 Called when a dragger knot is clicked; selects the dragger
521 */
522 static void
523 gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
525    GrDragger *dragger = (GrDragger *) data;
527    dragger->point_original = dragger->point;
529    dragger->parent->setSelected (dragger);
532 /**
533 Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable
534 */
535 static void
536 gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data)
538    GrDragger *dragger = (GrDragger *) data;
540    dragger->point_original = dragger->point;
542    if (dragger->draggables == NULL)
543        return;
545    GrDraggable *draggable = (GrDraggable *) dragger->draggables->data;
546    sp_item_gradient_edit_stop (draggable->item, draggable->point_num, draggable->fill_or_stroke);
549 /**
550 Act upon all draggables of the dragger, setting them to the dragger's point
551 */
552 void
553 GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focus)
555     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
556         GrDraggable *draggable = (GrDraggable *) i->data;
558         // set local_change flag so that selection_changed callback does not regenerate draggers
559         this->parent->local_change = true;
561         // change gradient, optionally writing to repr; prevent focus from moving if it's snapped
562         // to the center, unless it's the first update upon merge when we must snap it to the point
563         if (merging_focus ||
564             !(draggable->point_num == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->fill_or_stroke)))
565             sp_item_gradient_set_coords (draggable->item, draggable->point_num, this->point, draggable->fill_or_stroke, write_repr, scale_radial);
566     }
569 /**
570 Checks if the dragger has a draggable with this point_num
571  */
572 bool
573 GrDragger::isA (guint point_num)
575     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
576         GrDraggable *draggable = (GrDraggable *) i->data;
577         if (draggable->point_num == point_num) {
578             return true;
579         }
580     }
581     return false;
584 /**
585 Checks if the dragger has a draggable with this item, point_num, fill_or_stroke
586  */
587 bool
588 GrDragger::isA (SPItem *item, guint point_num, bool fill_or_stroke)
590     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
591         GrDraggable *draggable = (GrDraggable *) i->data;
592         if (draggable->point_num == point_num && draggable->item == item && draggable->fill_or_stroke == fill_or_stroke) {
593             return true;
594         }
595     }
596     return false;
599 bool
600 GrDraggable::mayMerge (GrDraggable *da2)
602     if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) {
603         // we must not merge the points of the same gradient!
604         if (!((this->point_num == POINT_RG_FOCUS && da2->point_num == POINT_RG_CENTER) ||
605               (this->point_num == POINT_RG_CENTER && da2->point_num == POINT_RG_FOCUS))) {
606             // except that we can snap center and focus together
607             return false;
608         }
609     }
610     return true;
613 bool
614 GrDragger::mayMerge (GrDragger *other)
616     if (this == other)
617         return false;
619     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
620         GrDraggable *da1 = (GrDraggable *) i->data;
621         for (GSList const* j = other->draggables; j != NULL; j = j->next) { // for all draggables of other
622             GrDraggable *da2 = (GrDraggable *) j->data;
623             if (!da1->mayMerge(da2))
624                 return false;
625         }
626     }
627     return true;
630 bool
631 GrDragger::mayMerge (GrDraggable *da2)
633     for (GSList const* i = this->draggables; i != NULL; i = i->next) { // for all draggables of this
634         GrDraggable *da1 = (GrDraggable *) i->data;
635         if (!da1->mayMerge(da2))
636             return false;
637     }
638     return true;
641 /**
642 Updates the statusbar tip of the dragger knot, based on its draggables
643  */
644 void
645 GrDragger::updateTip ()
647         if (this->knot && this->knot->tip) {
648                 g_free (this->knot->tip);
649                 this->knot->tip = NULL;
650         }
652     if (g_slist_length (this->draggables) == 1) {
653         GrDraggable *draggable = (GrDraggable *) this->draggables->data;
654         char *item_desc = sp_item_description(draggable->item);
655         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"),
656                                            _(gr_knot_descr[draggable->point_num]),
657                                            item_desc,
658                                            draggable->fill_or_stroke == false ? _(" (stroke)") : "");
659         g_free(item_desc);
660     } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) {
661         this->knot->tip = g_strdup_printf (_("Radial gradient <b>center</b> and <b>focus</b>; drag with <b>Shift</b> to separate focus"));
662     } else {
663         int length = g_slist_length (this->draggables);
664         this->knot->tip = g_strdup_printf (ngettext("Gradient point shared by <b>%d</b> gradient; drag with <b>Shift</b> to separate",
665                                                     "Gradient point shared by <b>%d</b> gradients; drag with <b>Shift</b> to separate",
666                                                     length),
667                                            length);
668     }
671 /**
672 Adds a draggable to the dragger
673  */
674 void
675 GrDragger::updateKnotShape ()
677     if (!draggables)
678         return;
679     GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data;
680     g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_num], NULL);
683 /**
684 Adds a draggable to the dragger
685  */
686 void
687 GrDragger::addDraggable (GrDraggable *draggable)
689     this->draggables = g_slist_prepend (this->draggables, draggable);
691     this->updateTip();
695 /**
696 Moves this dragger to the point of the given draggable, acting upon all other draggables
697  */
698 void
699 GrDragger::moveThisToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr)
701     this->point = sp_item_gradient_get_coords (item, point_num, fill_or_stroke);
702     this->point_original = this->point;
704     sp_knot_moveto (this->knot, &(this->point));
706     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
707         GrDraggable *da = (GrDraggable *) i->data;
708         if (da->item == item && da->point_num == point_num && da->fill_or_stroke == fill_or_stroke) {
709             continue;
710         }
711         sp_item_gradient_set_coords (da->item, da->point_num, this->point, da->fill_or_stroke, write_repr, false);
712     }
713     // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops?
717 /**
718 Moves all draggables that depend on this one
719  */
720 void
721 GrDragger::updateDependencies (bool write_repr)
723     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
724         GrDraggable *draggable = (GrDraggable *) i->data;
725         switch (draggable->point_num) {
726             case POINT_LG_P1:
727                 // the other point is dependent only when dragging with ctrl+shift
728                 this->moveOtherToDraggable (draggable->item, POINT_LG_P2, draggable->fill_or_stroke, write_repr);
729                 break;
730             case POINT_LG_P2:
731                 this->moveOtherToDraggable (draggable->item, POINT_LG_P1, draggable->fill_or_stroke, write_repr);
732                 break;
733             case POINT_RG_R2:
734                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, draggable->fill_or_stroke, write_repr);
735                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr);
736                 break;
737             case POINT_RG_R1:
738                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, draggable->fill_or_stroke, write_repr);
739                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr);
740                 break;
741             case POINT_RG_CENTER:
742                 this->moveOtherToDraggable (draggable->item, POINT_RG_R1, draggable->fill_or_stroke, write_repr);
743                 this->moveOtherToDraggable (draggable->item, POINT_RG_R2, draggable->fill_or_stroke, write_repr);
744                 this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr);
745                 break;
746             case POINT_RG_FOCUS:
747                 // nothing can depend on that
748                 break;
749             default:
750                 break;
751         }
752     }
757 GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable)
759     this->draggables = NULL;
761     this->parent = parent;
763     this->point = p;
764     this->point_original = p;
766     // create the knot
767     this->knot = sp_knot_new (parent->desktop, NULL);
768     this->knot->setMode(SP_KNOT_MODE_XOR);
769     this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL);
770     this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
771     sp_knot_update_ctrl(this->knot);
773     // move knot to the given point
774     sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL);
775     sp_knot_show (this->knot);
777     // connect knot's signals
778     this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this);
779     g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this);
780     g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this);
781     g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (gr_knot_ungrabbed_handler), this);
783     // add the initial draggable
784     if (draggable)
785         this->addDraggable (draggable);
786     updateKnotShape();
789 GrDragger::~GrDragger ()
791     // unselect if it was selected
792     if (this->parent->selected == this)
793         this->parent->setSelected (NULL);
795     /* unref should call destroy */
796     g_object_unref (G_OBJECT (this->knot));
798     // delete all draggables
799     for (GSList const* i = this->draggables; i != NULL; i = i->next) {
800         delete ((GrDraggable *) i->data);
801     }
802     g_slist_free (this->draggables);
803     this->draggables = NULL;
806 /**
807 Select the dragger which has the given draggable.
808 */
809 GrDragger *
810 GrDrag::getDraggerFor (SPItem *item, guint point_num, bool fill_or_stroke)
812     for (GList const* i = this->draggers; i != NULL; i = i->next) {
813         GrDragger *dragger = (GrDragger *) i->data;
814         for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
815             GrDraggable *da2 = (GrDraggable *) j->data;
816             if (da2->item == item && da2->point_num == point_num && da2->fill_or_stroke == fill_or_stroke) {
817                 return (dragger);
818             }
819         }
820     }
821     return NULL;
825 void
826 GrDragger::moveOtherToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr)
828     GrDragger *d = this->parent->getDraggerFor (item, point_num, fill_or_stroke);
829     if (d && d !=  this) {
830         d->moveThisToDraggable (item, point_num, fill_or_stroke, write_repr);
831     }
835 /**
836 Set selected dragger
837 */
838 void
839 GrDrag::setSelected (GrDragger *dragger)
841     if (this->selected) {
842        this->selected->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL;
843        g_object_set (G_OBJECT (this->selected->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL);
844     }
845     if (dragger) {
846         dragger->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED;
847         g_object_set (G_OBJECT (dragger->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL);
848     }
849     this->selected = dragger;
851     this->desktop->emitToolSubselectionChanged((gpointer) dragger);
854 /**
855 Create a line from p1 to p2 and add it to the lines list
856  */
857 void
858 GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
860     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop),
861                                                             SP_TYPE_CTRLLINE, NULL);
862     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
863     if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
864         sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
865     sp_canvas_item_show (line);
866     this->lines = g_slist_append (this->lines, line);
869 /**
870 If there already exists a dragger within MERGE_DIST of p, add the draggable to it; otherwise create
871 new dragger and add it to draggers list
872  */
873 void
874 GrDrag::addDragger (GrDraggable *draggable)
876     NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_num, draggable->fill_or_stroke);
878     for (GList *i = this->draggers; i != NULL; i = i->next) {
879         GrDragger *dragger = (GrDragger *) i->data;
880         if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) {
881             // distance is small, merge this draggable into dragger, no need to create new dragger
882             dragger->addDraggable (draggable);
883             dragger->updateKnotShape();
884             return;
885         }
886     }
888     GrDragger *new_dragger = new GrDragger(this, p, draggable);
889     this->draggers = g_list_prepend (this->draggers, new_dragger);
892 /**
893 Add draggers for the radial gradient rg on item
894 */
895 void
896 GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke)
898     addDragger (new GrDraggable (item, POINT_RG_CENTER, fill_or_stroke));
899     addDragger (new GrDraggable (item, POINT_RG_FOCUS, fill_or_stroke));
900     addDragger (new GrDraggable (item, POINT_RG_R1, fill_or_stroke));
901     addDragger (new GrDraggable (item, POINT_RG_R2, fill_or_stroke));
904 /**
905 Add draggers for the linear gradient lg on item
906 */
907 void
908 GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke)
910     addDragger (new GrDraggable (item, POINT_LG_P1, fill_or_stroke));
911     addDragger (new GrDraggable (item, POINT_LG_P2, fill_or_stroke));
914 /**
915 Artificially grab the knot of the dragger with this draggable; used by the gradient context
916 */
917 void
918 GrDrag::grabKnot (SPItem *item, guint point_num, bool fill_or_stroke, gint x, gint y, guint32 etime)
920     GrDragger *dragger = getDraggerFor (item, point_num, fill_or_stroke);
921     if (dragger) {
922         sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime);
923     }
926 /**
927 Regenerates the draggers list from the current selection; is called when selection is changed or
928 modified, also when a radial dragger needs to update positions of other draggers in the gradient
929 */
930 void
931 GrDrag::updateDraggers ()
933     // delete old draggers and deselect
934     for (GList const* i = this->draggers; i != NULL; i = i->next) {
935         delete ((GrDragger *) i->data);
936     }
937     g_list_free (this->draggers);
938     this->draggers = NULL;
939     this->selected = NULL;
941     g_return_if_fail (this->selection != NULL);
943     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
945         SPItem *item = SP_ITEM(i->data);
946         SPStyle *style = SP_OBJECT_STYLE (item);
948         if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
949             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
950             if (SP_IS_LINEARGRADIENT (server)) {
951                 addDraggersLinear (SP_LINEARGRADIENT (server), item, true);
952             } else if (SP_IS_RADIALGRADIENT (server)) {
953                 addDraggersRadial (SP_RADIALGRADIENT (server), item, true);
954             }
955         }
957         if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
958             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
959             if (SP_IS_LINEARGRADIENT (server)) {
960                 addDraggersLinear (SP_LINEARGRADIENT (server), item, false);
961             } else if (SP_IS_RADIALGRADIENT (server)) {
962                 addDraggersRadial (SP_RADIALGRADIENT (server), item, false);
963             }
964         }
967     }
970 /**
971 Regenerates the lines list from the current selection; is called on each move of a dragger, so that
972 lines are always in sync with the actual gradient
973 */
974 void
975 GrDrag::updateLines ()
977     // delete old lines
978     for (GSList const *i = this->lines; i != NULL; i = i->next) {
979         gtk_object_destroy( GTK_OBJECT (i->data));
980     }
981     g_slist_free (this->lines);
982     this->lines = NULL;
984     g_return_if_fail (this->selection != NULL);
986     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
988         SPItem *item = SP_ITEM(i->data);
990         SPStyle *style = SP_OBJECT_STYLE (item);
992         if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
993             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
994             if (SP_IS_LINEARGRADIENT (server)) {
995                 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);
996             } else if (SP_IS_RADIALGRADIENT (server)) {
997                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, true);
998                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, true), GR_LINE_COLOR_FILL);
999                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, true), GR_LINE_COLOR_FILL);
1000             }
1001         }
1003         if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1004             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1005             if (SP_IS_LINEARGRADIENT (server)) {
1006                 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);
1007             } else if (SP_IS_RADIALGRADIENT (server)) {
1008                 NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, false);
1009                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, false), GR_LINE_COLOR_STROKE);
1010                 this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, false), GR_LINE_COLOR_STROKE);
1011             }
1012         }
1013     }
1016 /**
1017 Regenerates the levels list from the current selection
1018 */
1019 void
1020 GrDrag::updateLevels ()
1022     hor_levels.clear();
1023     vert_levels.clear();
1025     g_return_if_fail (this->selection != NULL);
1027     for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
1028         SPItem *item = SP_ITEM(i->data);
1029         NR::Rect rect = sp_item_bbox_desktop (item);
1030         // Remember the edges of the bbox and the center axis
1031         hor_levels.push_back(rect.min()[NR::Y]);
1032         hor_levels.push_back(rect.max()[NR::Y]);
1033         hor_levels.push_back(0.5 * (rect.min()[NR::Y] + rect.max()[NR::Y]));
1034         vert_levels.push_back(rect.min()[NR::X]);
1035         vert_levels.push_back(rect.max()[NR::X]);
1036         vert_levels.push_back(0.5 * (rect.min()[NR::X] + rect.max()[NR::X]));
1037     }
1040 void
1041 GrDrag::selected_reverse_vector ()
1043     if (selected == NULL)
1044         return;
1046     for (GSList const* i = selected->draggables; i != NULL; i = i->next) {
1047         GrDraggable *draggable = (GrDraggable *) i->data;
1049         sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke);
1050     }
1053 void
1054 GrDrag::selected_move (double x, double y)
1056     if (selected == NULL)
1057         return;
1059     selected->point += NR::Point (x, y);
1060     selected->point_original = selected->point;
1061     sp_knot_moveto (selected->knot, &(selected->point));
1063     selected->fireDraggables (true);
1065     selected->updateDependencies(true);
1067     // we did an undoable action
1068     sp_document_done (sp_desktop_document (desktop));
1071 void
1072 GrDrag::selected_move_screen (double x, double y)
1074     gdouble zoom = desktop->current_zoom();
1075     gdouble zx = x / zoom;
1076     gdouble zy = y / zoom;
1078     selected_move (zx, zy);
1081 void
1082 GrDrag::select_next ()
1084     if (selected == NULL || g_list_find(draggers, selected)->next == NULL) {
1085         if (draggers)
1086             setSelected ((GrDragger *) draggers->data);
1087     } else {
1088         setSelected ((GrDragger *) g_list_find(draggers, selected)->next->data);
1089     }
1092 void
1093 GrDrag::select_prev ()
1095     if (selected == NULL || g_list_find(draggers, selected)->prev == NULL) {
1096         if (draggers)
1097             setSelected ((GrDragger *) g_list_last (draggers)->data);
1098     } else {
1099         setSelected ((GrDragger *) g_list_find(draggers, selected)->prev->data);
1100     }
1104 /*
1105   Local Variables:
1106   mode:c++
1107   c-file-style:"stroustrup"
1108   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1109   indent-tabs-mode:nil
1110   fill-column:99
1111   End:
1112 */
1113 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :