Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / automation_line.h
1 /*
2     Copyright (C) 2002 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_automation_line_h__
21 #define __ardour_automation_line_h__
22
23 #include <vector>
24 #include <list>
25 #include <string>
26 #include <sys/types.h>
27
28 #include <sigc++/signal.h>
29
30 #include "evoral/TimeConverter.hpp"
31
32 #include "pbd/undo.h"
33 #include "pbd/statefuldestructible.h"
34 #include "pbd/memento_command.h"
35
36 #include "ardour/automation_list.h"
37 #include "ardour/types.h"
38
39 #include "canvas/types.h"
40 #include "canvas/group.h"
41 #include "canvas/line.h"
42
43 class AutomationLine;
44 class ControlPoint;
45 class PointSelection;
46 class TimeAxisView;
47 class AutomationTimeAxisView;
48 class Selectable;
49 class Selection;
50
51 namespace ArdourCanvas {
52         class Rectangle;
53 }
54
55 /** A GUI representation of an ARDOUR::AutomationList */
56 class AutomationLine : public sigc::trackable, public PBD::StatefulDestructible
57 {
58   public:
59         enum VisibleAspects {
60                 Line = 0x1,
61                 ControlPoints = 0x2,
62                 SelectedControlPoints = 0x4
63         };
64         
65         AutomationLine (const std::string& name, TimeAxisView&, ArdourCanvas::Group&,
66                         boost::shared_ptr<ARDOUR::AutomationList>,
67                         Evoral::TimeConverter<double, ARDOUR::framepos_t>* converter = 0);
68         virtual ~AutomationLine ();
69
70         void queue_reset ();
71         void reset ();
72         void clear ();
73
74         void set_selected_points (PointSelection const &);
75         void get_selectables (ARDOUR::framepos_t, ARDOUR::framepos_t, double, double, std::list<Selectable*>&);
76         void get_inverted_selectables (Selection&, std::list<Selectable*>& results);
77
78         virtual void remove_point (ControlPoint&);
79         bool control_points_adjacent (double xval, uint32_t& before, uint32_t& after);
80
81         /* dragging API */
82         virtual void start_drag_single (ControlPoint*, double, float);
83         virtual void start_drag_line (uint32_t, uint32_t, float);
84         virtual void start_drag_multiple (std::list<ControlPoint*>, float, XMLNode *);
85         virtual std::pair<double, float> drag_motion (double, float, bool, bool with_push, uint32_t& final_index);
86         virtual void end_drag (bool with_push, uint32_t final_index);
87
88         ControlPoint* nth (uint32_t);
89         ControlPoint const * nth (uint32_t) const;
90         uint32_t npoints() const { return control_points.size(); }
91
92         std::string  name()    const { return _name; }
93         bool    visible() const { return _visible != VisibleAspects(0); }
94         guint32 height()  const { return _height; }
95
96         void     set_line_color (uint32_t);
97         uint32_t get_line_color() const { return _line_color; }
98
99         void set_visibility (VisibleAspects);
100         void add_visibility (VisibleAspects);
101         void remove_visibility (VisibleAspects);
102
103         void hide ();
104         void set_height (guint32);
105         void set_uses_gain_mapping (bool yn);
106         bool get_uses_gain_mapping () const { return _uses_gain_mapping; }
107
108         TimeAxisView& trackview;
109
110         ArdourCanvas::Group& canvas_group() const { return *group; }
111         ArdourCanvas::Item&  parent_group() const { return _parent_group; }
112         ArdourCanvas::Item&  grab_item() const { return *line; }
113
114         std::string get_verbose_cursor_string (double) const;
115         std::string get_verbose_cursor_relative_string (double, double) const;
116         std::string fraction_to_string (double) const;
117         std::string fraction_to_relative_string (double, double) const;
118         double string_to_fraction (std::string const &) const;
119         void   view_to_model_coord (double& x, double& y) const;
120         void   view_to_model_coord_y (double &) const;
121         void   model_to_view_coord (double& x, double& y) const;
122
123         void set_list(boost::shared_ptr<ARDOUR::AutomationList> list);
124         boost::shared_ptr<ARDOUR::AutomationList> the_list() const { return alist; }
125
126         void track_entered();
127         void track_exited();
128
129         bool is_last_point (ControlPoint &);
130         bool is_first_point (ControlPoint &);
131
132         XMLNode& get_state (void);
133         int set_state (const XMLNode&, int version);
134         void set_colors();
135
136         void modify_point_y (ControlPoint&, double);
137
138         virtual MementoCommandBinder<ARDOUR::AutomationList>* memento_command_binder ();
139
140         const Evoral::TimeConverter<double, ARDOUR::framepos_t>& time_converter () const {
141                 return *_time_converter;
142         }
143
144         std::pair<ARDOUR::framepos_t, ARDOUR::framepos_t> get_point_x_range () const;
145
146         void set_maximum_time (ARDOUR::framecnt_t);
147         ARDOUR::framecnt_t maximum_time () const {
148                 return _maximum_time;
149         }
150
151         void set_offset (ARDOUR::framecnt_t);
152         void set_width (ARDOUR::framecnt_t);
153
154         framepos_t session_position (ARDOUR::AutomationList::const_iterator) const;
155
156   protected:
157
158         std::string    _name;
159         guint32   _height;
160         uint32_t  _line_color;
161
162         boost::shared_ptr<ARDOUR::AutomationList> alist;
163         Evoral::TimeConverter<double, ARDOUR::framepos_t>* _time_converter;
164         /** true if _time_converter belongs to us (ie we should delete it on destruction) */
165         bool _our_time_converter;
166
167         VisibleAspects _visible;
168  
169         bool    _uses_gain_mapping;
170         bool    terminal_points_can_slide;
171         bool    update_pending;
172         bool    have_timeout;
173         bool    no_draw;
174         bool    _is_boolean;
175         /** true if we did a push at any point during the current drag */
176         bool    did_push;
177
178         ArdourCanvas::Group&        _parent_group;
179         ArdourCanvas::Group*        group;
180         ArdourCanvas::PolyLine*     line; /* line */
181         ArdourCanvas::Points        line_points; /* coordinates for canvas line */
182         std::vector<ControlPoint*>  control_points; /* visible control points */
183
184         class ContiguousControlPoints : public std::list<ControlPoint*> {
185           public:
186             ContiguousControlPoints (AutomationLine& al);
187             double clamp_dx (double dx);
188             void move (double dx, double dy);
189             void compute_x_bounds ();
190           private:
191             AutomationLine& line;
192             double before_x;
193             double after_x;
194         };
195
196         friend class ContiguousControlPoints;
197
198         typedef boost::shared_ptr<ContiguousControlPoints> CCP;
199         std::vector<CCP> contiguous_points;
200
201         void sync_model_with_view_point (ControlPoint&);
202         void sync_model_with_view_points (std::list<ControlPoint*>);
203         void start_drag_common (double, float);
204
205         virtual void change_model (ARDOUR::AutomationList::iterator, double x, double y);
206
207         void reset_callback (const Evoral::ControlList&);
208         void list_changed ();
209
210         virtual bool event_handler (GdkEvent*);
211
212   private:
213         std::list<ControlPoint*> _drag_points; ///< points we are dragging
214         std::list<ControlPoint*> _push_points; ///< additional points we are dragging if "push" is enabled
215         bool _drag_had_movement; ///< true if the drag has seen movement, otherwise false
216         double _drag_x; ///< last x position of the drag, in units
217         double _drag_distance; ///< total x movement of the drag, in canvas units
218         double _last_drag_fraction; ///< last y position of the drag, as a fraction
219         /** offset from the start of the automation list to the start of the line, so that
220          *  a +ve offset means that the 0 on the line is at _offset in the list
221          */
222         ARDOUR::framecnt_t _offset;
223
224         void show ();
225         void reset_line_coords (ControlPoint&);
226         void add_visible_control_point (uint32_t, uint32_t, double, double, ARDOUR::AutomationList::iterator, uint32_t);
227         double control_point_box_size ();
228         void connect_to_list ();
229         void interpolation_changed (ARDOUR::AutomationList::InterpolationStyle);
230
231         PBD::ScopedConnectionList _list_connections;
232
233         /** maximum time that a point on this line can be at, relative to the position of its region or start of its track */
234         ARDOUR::framecnt_t _maximum_time;
235
236         friend class AudioRegionGainLine;
237 };
238
239 #endif /* __ardour_automation_line_h__ */
240