Make Clear option work for MIDI automation tracks. Fixes part of #3137.
[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         AutomationLine (const std::string& name, TimeAxisView&, ArdourCanvas::Group&,
61                         boost::shared_ptr<ARDOUR::AutomationList>,
62                         const Evoral::TimeConverter<double, ARDOUR::sframes_t>* converter = 0);
63         virtual ~AutomationLine ();
64
65         void queue_reset ();
66         void reset ();
67         void clear ();
68
69         std::list<ControlPoint*> point_selection_to_control_points (PointSelection const &); 
70         void set_selected_points (PointSelection&);
71         void get_selectables (nframes_t& start, nframes_t& end,
72                               double botfrac, double topfrac,
73                               std::list<Selectable*>& results);
74         void get_inverted_selectables (Selection&, std::list<Selectable*>& results);
75
76         virtual void remove_point (ControlPoint&);
77         bool control_points_adjacent (double xval, uint32_t& before, uint32_t& after);
78
79         /* dragging API */
80         virtual void start_drag_single (ControlPoint*, double, float);
81         virtual void start_drag_line (uint32_t, uint32_t, float);
82         virtual void start_drag_multiple (std::list<ControlPoint*>, float, XMLNode *);
83         virtual std::pair<double, float> drag_motion (double, float, bool, bool);
84         virtual void end_drag ();
85
86         ControlPoint* nth (uint32_t);
87         uint32_t npoints() const { return control_points.size(); }
88
89         std::string  name()    const { return _name; }
90         bool    visible() const { return _visible; }
91         guint32 height()  const { return _height; }
92
93         void     set_line_color (uint32_t);
94         uint32_t get_line_color() const { return _line_color; }
95
96         void    show ();
97         void    hide ();
98         void    set_height (guint32);
99         void    set_uses_gain_mapping (bool yn);
100         bool    get_uses_gain_mapping () const { return _uses_gain_mapping; }
101
102         TimeAxisView& trackview;
103
104         ArdourCanvas::Group& canvas_group() const { return *group; }
105         ArdourCanvas::Item&  parent_group() const { return _parent_group; }
106         ArdourCanvas::Item&  grab_item() const { return *line; }
107
108         std::string get_verbose_cursor_string (double) const;
109         std::string fraction_to_string (double) const;
110         double string_to_fraction (std::string const &) const;
111         void   view_to_model_coord (double& x, double& y) const;
112         void   view_to_model_coord_x (double &) const;
113         void   view_to_model_coord_y (double &) const;
114         void   model_to_view_coord (double& x, double& y) const;
115
116         void set_list(boost::shared_ptr<ARDOUR::AutomationList> list);
117         boost::shared_ptr<ARDOUR::AutomationList> the_list() const { return alist; }
118
119         void show_all_control_points ();
120         void hide_all_but_selected_control_points ();
121
122         void track_entered();
123         void track_exited();
124
125         bool is_last_point (ControlPoint &);
126         bool is_first_point (ControlPoint &);
127
128         XMLNode& get_state (void);
129         int set_state (const XMLNode&, int version);
130         void set_colors();
131
132         void modify_point_y (ControlPoint&, double);
133
134         void add_always_in_view (double);
135         void clear_always_in_view ();
136
137         virtual MementoCommandBinder<ARDOUR::AutomationList>* memento_command_binder ();
138
139   protected:
140
141         std::string    _name;
142         guint32   _height;
143         uint32_t  _line_color;
144
145         boost::shared_ptr<ARDOUR::AutomationList> alist;
146
147         bool    _visible                  : 1;
148         bool    _uses_gain_mapping        : 1;
149         bool    terminal_points_can_slide : 1;
150         bool    update_pending            : 1;
151         bool    no_draw                   : 1;
152         bool    points_visible            : 1;
153         bool    did_push;
154
155         ArdourCanvas::Group&        _parent_group;
156         ArdourCanvas::Group*        group;
157         ArdourCanvas::Line*         line; /* line */
158         ArdourCanvas::Points        line_points; /* coordinates for canvas line */
159         std::vector<ControlPoint*>  control_points; /* visible control points */
160
161         struct ALPoint {
162             double x;
163             double y;
164             ALPoint (double xx, double yy) : x(xx), y(yy) {}
165         };
166
167         typedef std::vector<ALPoint> ALPoints;
168
169         static void invalidate_point (ALPoints&, uint32_t index);
170         static bool invalid_point (ALPoints&, uint32_t index);
171
172         void determine_visible_control_points (ALPoints&);
173         void sync_model_with_view_point (ControlPoint&, bool, int64_t);
174         void sync_model_with_view_points (std::list<ControlPoint*>, bool, int64_t);
175         void start_drag_common (double, float);
176
177         virtual void change_model (ARDOUR::AutomationList::iterator, double x, double y);
178
179         void reset_callback (const Evoral::ControlList&);
180         void list_changed ();
181
182         virtual bool event_handler (GdkEvent*);
183         virtual void add_model_point (ALPoints& tmp_points, double frame, double yfract);
184
185   private:
186         std::list<ControlPoint*> _drag_points; ///< points we are dragging
187         std::list<ControlPoint*> _push_points; ///< additional points we are dragging if "push" is enabled
188         bool _drag_had_movement; ///< true if the drag has seen movement, otherwise false
189         double _drag_x; ///< last x position of the drag, in units
190         double _drag_distance; ///< total x movement of the drag, in units
191         double _last_drag_fraction; ///< last y position of the drag, as a fraction
192         std::list<double> _always_in_view;
193
194         const Evoral::TimeConverter<double, ARDOUR::sframes_t>& _time_converter;
195
196         void reset_line_coords (ControlPoint&);
197         void add_visible_control_point (uint32_t, uint32_t, double, double, ARDOUR::AutomationList::iterator, uint32_t);
198         double control_point_box_size ();
199         void connect_to_list ();
200         void interpolation_changed (ARDOUR::AutomationList::InterpolationStyle);
201
202         struct ModelRepresentation {
203             ARDOUR::AutomationList::iterator start;
204             ARDOUR::AutomationList::iterator end;
205             double xpos;
206             double ypos;
207             double xmin;
208             double ymin;
209             double xmax;
210             double ymax;
211             double xval;
212             double yval;
213         };
214
215         void model_representation (ControlPoint&, ModelRepresentation&);
216
217         PBD::ScopedConnectionList _list_connections;
218         
219         friend class AudioRegionGainLine;
220 };
221
222 #endif /* __ardour_automation_line_h__ */
223