Code

Next roud of NR ==> Geom conversion
[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 <libnr/nr-forward.h>
23 #include <knot-enums.h>
25 struct SPItem;
26 struct SPKnot;
28 /**
29 This class represents a single draggable point of a gradient. It remembers the item
30 which has the gradient, whether it's fill or stroke, the point type (from the
31 GrPointType enum), and the point number (needed if more than 2 stops are present).
32 */
33 struct GrDraggable {
34         GrDraggable(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke);
35     virtual ~GrDraggable();
37         SPItem *item;
38         gint point_type;
39         gint point_i;  // the stop number of this point ( = 0 POINT_LG_BEGIN and POINT_RG_CENTER)
40         bool fill_or_stroke;
42         SPObject *getServer();
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     virtual ~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();
80         
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, bool fill_or_stroke);
95     bool isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
97         void fireDraggables (bool write_repr, bool scale_radial = false, bool merging_focus = false);
98 };
100 /**
101 This is the root class of the gradient dragging machinery. It holds lists of GrDraggers
102 and of lines (simple canvas items). It also remembers one of the draggers as selected.
103 */
104 class GrDrag {
105 public: // FIXME: make more of this private!
107     GrDrag(SPDesktop *desktop);
108     virtual ~GrDrag();
110                 bool isNonEmpty() {return (draggers != NULL);}
111                 bool hasSelection() {return (selected != NULL);}
112                 guint numSelected() {return (selected? g_list_length(selected) : 0);}
113                 guint numDraggers() {return (draggers? g_list_length(draggers) : 0);}
114                 guint singleSelectedDraggerNumDraggables() {return (selected? g_slist_length(((GrDragger *) selected->data)->draggables) : 0);}
115                 guint singleSelectedDraggerSingleDraggableType() {return (selected? ((GrDraggable *) ((GrDragger *) selected->data)->draggables->data)->point_type : 0);}
117     // especially the selection must be private, fix gradient-context to remove direct access to it
118     GList *selected; // list of GrDragger*
119     void setSelected (GrDragger *dragger, bool add_to_selection = false, bool override = true);
120     void setDeselected (GrDragger *dragger);
121     void deselectAll();
122                 void selectAll();
123                 void selectByCoords(std::vector<NR::Point> coords);
124     void selectRect(Geom::Rect const &r);
126     bool dropColor(SPItem *item, gchar *c, NR::Point p);
128                 SPStop *addStopNearPoint (SPItem *item, NR::Point mouse_p, double tolerance);
129     
130     void deleteSelected (bool just_one = false);
132     guint32 getColor();
133     
134     bool keep_selection;    
135     
136     GrDragger *getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke);
138     void grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime);
139     void grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime);
141     bool local_change;
143     SPDesktop *desktop;
145     // lists of edges of selection bboxes, to snap draggers to
146     std::vector<double> hor_levels;
147     std::vector<double> vert_levels;
149     GList *draggers;
150     GSList *lines;
152     void updateDraggers ();
153     void updateLines ();
154     void updateLevels ();
156     void selected_move_nowrite (double x, double y, bool scale_radial);
157     void selected_move (double x, double y, bool write_repr = true, bool scale_radial = false);
158     void selected_move_screen (double x, double y);
160     GrDragger *select_next ();
161     GrDragger *select_prev ();
163     void selected_reverse_vector ();
165 private: 
166     void deselect_all();
168     void addLine (SPItem *item, NR::Point p1, NR::Point p2, guint32 rgba);
170     void addDragger (GrDraggable *draggable);
172     void addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke);
173     void addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke);
175     Inkscape::Selection *selection;
176     sigc::connection sel_changed_connection;
177     sigc::connection sel_modified_connection;
179     sigc::connection style_set_connection;
180     sigc::connection style_query_connection;
181 };
183 #endif