1 #ifndef __GRADIENT_DRAG_H__
2 #define __GRADIENT_DRAG_H__
4 /*
5 * On-canvas gradient dragging
6 *
7 * Authors:
8 * bulia byak <bulia@users.sf.net>
9 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
10 *
11 * Copyright (C) 2007 Johan Engelen
12 * Copyright (C) 2005 Authors
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #include <glib/gslist.h>
18 #include <sigc++/sigc++.h>
19 #include <vector>
21 #include <forward.h>
22 #include <libnr/nr-forward.h>
23 #include <2geom/point.h>
24 #include <knot-enums.h>
26 struct SPItem;
27 struct SPKnot;
29 /**
30 This class represents a single draggable point of a gradient. It remembers the item
31 which has the gradient, whether it's fill or stroke, the point type (from the
32 GrPointType enum), and the point number (needed if more than 2 stops are present).
33 */
34 struct GrDraggable {
35 GrDraggable(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke);
36 virtual ~GrDraggable();
38 SPItem *item;
39 gint point_type;
40 gint point_i; // the stop number of this point ( = 0 POINT_LG_BEGIN and POINT_RG_CENTER)
41 bool fill_or_stroke;
43 SPObject *getServer();
45 bool mayMerge (GrDraggable *da2);
47 inline int equals (GrDraggable *other) {
48 return ((item == other->item) && (point_type == other->point_type) && (point_i == other->point_i) && (fill_or_stroke == other->fill_or_stroke));
49 }
50 };
52 class GrDrag;
54 /**
55 This class holds together a visible on-canvas knot and a list of draggables that need to
56 be moved when the knot moves. Normally there's one draggable in the list, but there may
57 be more when draggers are snapped together.
58 */
59 struct GrDragger {
60 GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable);
61 virtual ~GrDragger();
63 GrDrag *parent;
65 SPKnot *knot;
67 // position of the knot, desktop coords
68 Geom::Point point;
69 // position of the knot before it began to drag; updated when released
70 Geom::Point point_original;
72 /** Connection to \a knot's "moved" signal, for blocking it (unused?). */
73 guint handler_id;
75 GSList *draggables;
77 void addDraggable(GrDraggable *draggable);
79 void updateKnotShape();
80 void updateTip();
82 void select();
83 void deselect();
84 bool isSelected();
86 void moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr);
87 void moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr);
88 void updateMidstopDependencies (GrDraggable *draggable, bool write_repr);
89 void updateDependencies (bool write_repr);
91 bool mayMerge (GrDragger *other);
92 bool mayMerge (GrDraggable *da2);
94 bool isA (gint point_type);
95 bool isA (SPItem *item, gint point_type, bool fill_or_stroke);
96 bool isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
98 void fireDraggables (bool write_repr, bool scale_radial = false, bool merging_focus = false);
99 };
101 /**
102 This is the root class of the gradient dragging machinery. It holds lists of GrDraggers
103 and of lines (simple canvas items). It also remembers one of the draggers as selected.
104 */
105 class GrDrag {
106 public: // FIXME: make more of this private!
108 GrDrag(SPDesktop *desktop);
109 virtual ~GrDrag();
111 bool isNonEmpty() {return (draggers != NULL);}
112 bool hasSelection() {return (selected != NULL);}
113 guint numSelected() {return (selected? g_list_length(selected) : 0);}
114 guint numDraggers() {return (draggers? g_list_length(draggers) : 0);}
115 guint singleSelectedDraggerNumDraggables() {return (selected? g_slist_length(((GrDragger *) selected->data)->draggables) : 0);}
116 guint singleSelectedDraggerSingleDraggableType() {return (selected? ((GrDraggable *) ((GrDragger *) selected->data)->draggables->data)->point_type : 0);}
118 // especially the selection must be private, fix gradient-context to remove direct access to it
119 GList *selected; // list of GrDragger*
120 void setSelected (GrDragger *dragger, bool add_to_selection = false, bool override = true);
121 void setDeselected (GrDragger *dragger);
122 void deselectAll();
123 void selectAll();
124 void selectByCoords(std::vector<Geom::Point> coords);
125 void selectRect(Geom::Rect const &r);
127 bool dropColor(SPItem *item, gchar const *c, Geom::Point p);
129 SPStop *addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance);
131 void deleteSelected (bool just_one = false);
133 guint32 getColor();
135 bool keep_selection;
137 GrDragger *getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
139 void grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime);
140 void grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime);
142 bool local_change;
144 SPDesktop *desktop;
146 // lists of edges of selection bboxes, to snap draggers to
147 std::vector<double> hor_levels;
148 std::vector<double> vert_levels;
150 GList *draggers;
151 GSList *lines;
153 void updateDraggers ();
154 void updateLines ();
155 void updateLevels ();
157 bool mouseOver();
159 void selected_move_nowrite (double x, double y, bool scale_radial);
160 void selected_move (double x, double y, bool write_repr = true, bool scale_radial = false);
161 void selected_move_screen (double x, double y);
163 GrDragger *select_next ();
164 GrDragger *select_prev ();
166 void selected_reverse_vector ();
168 private:
169 void deselect_all();
171 void addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba);
173 void addDragger (GrDraggable *draggable);
175 void addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke);
176 void addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke);
178 bool styleSet( const SPCSSAttr *css );
180 Glib::ustring makeStopSafeColor( gchar const *str, bool &isNull );
182 Inkscape::Selection *selection;
183 sigc::connection sel_changed_connection;
184 sigc::connection sel_modified_connection;
186 sigc::connection style_set_connection;
187 sigc::connection style_query_connection;
188 };
190 #endif