Code

implement moving midstops with arrow keys, some refactoring
[inkscape.git] / src / gradient-drag.h
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         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         ~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();
82         
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, 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     ~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<NR::Point> coords);
125     
126     void deleteSelected (bool just_one = false);
127     
128     bool keep_selection;    
129     
130     GrDragger *getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
132     void grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime);
134     bool local_change;
136     SPDesktop *desktop;
138     // lists of edges of selection bboxes, to snap draggers to
139     std::vector<double> hor_levels;
140     std::vector<double> vert_levels;
142     GList *draggers;
143     GSList *lines;
145     void updateDraggers ();
146     void updateLines ();
147     void updateLevels ();
149     void selected_move_nowrite (double x, double y, bool scale_radial);
150     void selected_move (double x, double y, bool write_repr = true, bool scale_radial = false);
151     void selected_move_screen (double x, double y);
153     void select_next ();
154     void select_prev ();
156     void selected_reverse_vector ();
158 private: 
159     void deselect_all();
161     void addLine (NR::Point p1, NR::Point p2, guint32 rgba);
163     void addDragger (GrDraggable *draggable);
165     void addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke);
166     void addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke);
168     Inkscape::Selection *selection;
169     sigc::connection sel_changed_connection;
170     sigc::connection sel_modified_connection;
172     sigc::connection style_set_connection;
173     sigc::connection style_query_connection;
174 };
176 #endif