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