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 virtual ~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 SPObject *getServer();
46 bool mayMerge (GrDraggable *da2);
48 inline int equals (GrDraggable *other) {
49 return ((item == other->item) && (point_type == other->point_type) && (point_i == other->point_i) && (fill_or_stroke == other->fill_or_stroke));
50 }
51 };
53 class GrDrag;
55 /**
56 This class holds together a visible on-canvas knot and a list of draggables that need to
57 be moved when the knot moves. Normally there's one draggable in the list, but there may
58 be more when draggers are snapped together.
59 */
60 struct GrDragger {
61 GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable);
62 virtual ~GrDragger();
64 GrDrag *parent;
66 SPKnot *knot;
68 // position of the knot, desktop coords
69 NR::Point point;
70 // position of the knot before it began to drag; updated when released
71 NR::Point point_original;
73 /** Connection to \a knot's "moved" signal, for blocking it (unused?). */
74 guint handler_id;
76 GSList *draggables;
78 void addDraggable(GrDraggable *draggable);
80 void updateKnotShape();
81 void updateTip();
83 void select();
84 void deselect();
85 bool isSelected();
87 void moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr);
88 void moveOtherToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr);
89 void updateMidstopDependencies (GrDraggable *draggable, bool write_repr);
90 void updateDependencies (bool write_repr);
92 bool mayMerge (GrDragger *other);
93 bool mayMerge (GrDraggable *da2);
95 bool isA (gint point_type);
96 bool isA (SPItem *item, gint point_type, bool fill_or_stroke);
97 bool isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
99 void fireDraggables (bool write_repr, bool scale_radial = false, bool merging_focus = false);
100 };
102 /**
103 This is the root class of the gradient dragging machinery. It holds lists of GrDraggers
104 and of lines (simple canvas items). It also remembers one of the draggers as selected.
105 */
106 class GrDrag {
107 public: // FIXME: make more of this private!
109 GrDrag(SPDesktop *desktop);
110 virtual ~GrDrag();
112 bool isNonEmpty() {return (draggers != NULL);}
113 bool hasSelection() {return (selected != NULL);}
114 guint numSelected() {return (selected? g_list_length(selected) : 0);}
115 guint numDraggers() {return (draggers? g_list_length(draggers) : 0);}
116 guint singleSelectedDraggerNumDraggables() {return (selected? g_slist_length(((GrDragger *) selected->data)->draggables) : 0);}
117 guint singleSelectedDraggerSingleDraggableType() {return (selected? ((GrDraggable *) ((GrDragger *) selected->data)->draggables->data)->point_type : 0);}
119 // especially the selection must be private, fix gradient-context to remove direct access to it
120 GList *selected; // list of GrDragger*
121 void setSelected (GrDragger *dragger, bool add_to_selection = false, bool override = true);
122 void setDeselected (GrDragger *dragger);
123 void deselectAll();
124 void selectAll();
125 void selectByCoords(std::vector<NR::Point> coords);
126 void selectRect(NR::Rect const &r);
128 bool dropColor(SPItem *item, gchar *c, NR::Point p);
130 SPStop *addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance);
132 void deleteSelected (bool just_one = false);
134 guint32 getColor();
136 bool keep_selection;
138 GrDragger *getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
140 void grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime);
141 void grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime);
143 bool local_change;
145 SPDesktop *desktop;
147 // lists of edges of selection bboxes, to snap draggers to
148 std::vector<double> hor_levels;
149 std::vector<double> vert_levels;
151 GList *draggers;
152 GSList *lines;
154 void updateDraggers ();
155 void updateLines ();
156 void updateLevels ();
158 void selected_move_nowrite (double x, double y, bool scale_radial);
159 void selected_move (double x, double y, bool write_repr = true, bool scale_radial = false);
160 void selected_move_screen (double x, double y);
162 GrDragger *select_next ();
163 GrDragger *select_prev ();
165 void selected_reverse_vector ();
167 private:
168 void deselect_all();
170 void addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba);
172 void addDragger (GrDraggable *draggable);
174 void addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke);
175 void addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke);
177 Inkscape::Selection *selection;
178 sigc::connection sel_changed_connection;
179 sigc::connection sel_modified_connection;
181 sigc::connection style_set_connection;
182 sigc::connection style_query_connection;
183 };
185 #endif