5b8c867f1162a59bb358c954fbadc24f9a4e6d8f
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 <knot-enums.h>
24 struct SPItem;
25 struct SPKnot;
26 namespace NR {
27 class Point;
28 }
30 /**
31 This class represents a single draggable point of a gradient. It remembers the item
32 which has the gradient, whether it's fill or stroke, the point type (from the
33 GrPointType enum), and the point number (needed if more than 2 stops are present).
34 */
35 struct GrDraggable {
36 GrDraggable(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke);
37 ~GrDraggable();
39 SPItem *item;
40 gint point_type;
41 gint point_i; // the stop number of this point ( = 0 POINT_LG_BEGIN and POINT_RG_CENTER)
42 bool fill_or_stroke;
44 bool mayMerge (GrDraggable *da2);
46 inline int equals (GrDraggable *other) {
47 return ((item == other->item) && (point_type == other->point_type) && (point_i == other->point_i) && (fill_or_stroke == other->fill_or_stroke));
48 }
49 };
51 class GrDrag;
53 /**
54 This class holds together a visible on-canvas knot and a list of draggables that need to
55 be moved when the knot moves. Normally there's one draggable in the list, but there may
56 be more when draggers are snapped together.
57 */
58 struct GrDragger {
59 GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable);
60 ~GrDragger();
62 GrDrag *parent;
64 SPKnot *knot;
66 // position of the knot, desktop coords
67 NR::Point point;
68 // position of the knot before it began to drag; updated when released
69 NR::Point point_original;
71 /** Connection to \a knot's "moved" signal, for blocking it (unused?). */
72 guint handler_id;
74 GSList *draggables;
76 void addDraggable(GrDraggable *draggable);
78 void updateKnotShape();
79 void updateTip();
81 void select();
82 void deselect();
83 bool isSelected();
85 void moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr);
86 void moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr);
87 void updateMidstopDependencies (GrDraggable *draggable, bool write_repr);
88 void updateDependencies (bool write_repr);
90 bool mayMerge (GrDragger *other);
91 bool mayMerge (GrDraggable *da2);
93 bool isA (gint point_type);
94 bool isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
96 void fireDraggables (bool write_repr, bool scale_radial = false, bool merging_focus = false);
97 };
99 /**
100 This is the root class of the gradient dragging machinery. It holds lists of GrDraggers
101 and of lines (simple canvas items). It also remembers one of the draggers as selected.
102 */
103 class GrDrag {
104 public: // FIXME: make more of this private!
106 GrDrag(SPDesktop *desktop);
107 ~GrDrag();
109 bool isNonEmpty() {return (draggers != NULL);}
110 bool hasSelection() {return (selected != NULL);}
111 guint numSelected() {return (selected? g_list_length(selected) : 0);}
112 guint numDraggers() {return (draggers? g_list_length(draggers) : 0);}
113 guint singleSelectedDraggerNumDraggables() {return (selected? g_slist_length(((GrDragger *) selected->data)->draggables) : 0);}
114 guint singleSelectedDraggerSingleDraggableType() {return (selected? ((GrDraggable *) ((GrDragger *) selected->data)->draggables->data)->point_type : 0);}
116 // especially the selection must be private, fix gradient-context to remove direct access to it
117 GList *selected; // list of GrDragger*
118 void setSelected (GrDragger *dragger, bool add_to_selection = false, bool override = true);
119 void setDeselected (GrDragger *dragger);
120 void deselectAll();
121 void selectAll();
122 void selectByCoords(std::vector<NR::Point> coords);
124 void deleteSelected (bool just_one = false);
126 bool keep_selection;
128 GrDragger *getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
130 void grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime);
132 bool local_change;
134 SPDesktop *desktop;
136 // lists of edges of selection bboxes, to snap draggers to
137 std::vector<double> hor_levels;
138 std::vector<double> vert_levels;
140 GList *draggers;
141 GSList *lines;
143 void updateDraggers ();
144 void updateLines ();
145 void updateLevels ();
147 void selected_move_nowrite (double x, double y, bool scale_radial);
148 void selected_move (double x, double y, bool write_repr = true, bool scale_radial = false);
149 void selected_move_screen (double x, double y);
151 void select_next ();
152 void select_prev ();
154 void selected_reverse_vector ();
156 private:
157 void deselect_all();
159 void addLine (NR::Point p1, NR::Point p2, guint32 rgba);
161 void addDragger (GrDraggable *draggable);
163 void addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke);
164 void addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke);
166 Inkscape::Selection *selection;
167 sigc::connection sel_changed_connection;
168 sigc::connection sel_modified_connection;
170 sigc::connection style_set_connection;
171 sigc::connection style_query_connection;
172 };
174 #endif