Time unit translation for AutomationLine (correctly display MIDI controller data).
[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
38 #include <ardour/automation_list.h>
39
40 class AutomationLine;
41 class ControlPoint;
42 class PointSelection;
43 class TimeAxisView;
44 class AutomationTimeAxisView;
45 class Selectable;
46 class Selection;
47
48 namespace Gnome {
49         namespace Canvas {
50                 class SimpleRect;
51         }
52 }
53
54 class AutomationLine : public sigc::trackable, public PBD::StatefulThingWithGoingAway
55 {
56   public:
57         AutomationLine (const string& name, TimeAxisView&, ArdourCanvas::Group&,
58                         boost::shared_ptr<ARDOUR::AutomationList>,
59                         const Evoral::TimeConverter<double, nframes_t>& converter);
60         virtual ~AutomationLine ();
61
62         void queue_reset ();
63         void reset ();
64         void clear();
65
66         void set_selected_points (PointSelection&);
67         void get_selectables (nframes_t& start, nframes_t& end,
68                               double botfrac, double topfrac, 
69                               std::list<Selectable*>& results);
70         void get_inverted_selectables (Selection&, std::list<Selectable*>& results);
71
72         virtual void remove_point (ControlPoint&);
73         bool control_points_adjacent (double xval, uint32_t& before, uint32_t& after);
74         
75         /* dragging API */
76         virtual void start_drag (ControlPoint*, nframes_t x, float fraction);
77         virtual void point_drag(ControlPoint&, nframes_t x, float, bool with_push);
78         virtual void end_drag (ControlPoint*);
79         virtual void line_drag(uint32_t i1, uint32_t i2, float, bool with_push);
80
81         ControlPoint* nth (uint32_t);
82         uint32_t npoints() const { return control_points.size(); }
83
84         string  name()    const { return _name; }
85         bool    visible() const { return _visible; }
86         guint32 height()  const { return _height; }
87
88         void     set_line_color (uint32_t);
89         uint32_t get_line_color() const { return _line_color; }
90
91         void set_interpolation(ARDOUR::AutomationList::InterpolationStyle style);
92
93         void    show ();
94         void    hide ();
95         void    set_height (guint32);
96         void    set_uses_gain_mapping (bool yn);
97         bool    get_uses_gain_mapping () const { return _uses_gain_mapping; }
98
99         TimeAxisView& trackview;
100
101         ArdourCanvas::Group& canvas_group() const { return *group; }
102         ArdourCanvas::Item&  parent_group() const { return _parent_group; }
103         ArdourCanvas::Item&  grab_item() const { return *line; }
104
105         void show_selection();
106         void hide_selection ();
107
108         string get_verbose_cursor_string (double) const;
109         string fraction_to_string (double) const;
110         double string_to_fraction (string const &) const;
111         void   view_to_model_coord (double& x, double& y) const;
112         void   model_to_view_coord (double& x, double& y) const;
113
114         void set_list(boost::shared_ptr<ARDOUR::AutomationList> list);
115         boost::shared_ptr<ARDOUR::AutomationList> the_list() const { return alist; }
116
117         void show_all_control_points ();
118         void hide_all_but_selected_control_points ();
119
120         void track_entered();
121         void track_exited();
122
123         bool is_last_point (ControlPoint &);
124         bool is_first_point (ControlPoint &);
125
126         XMLNode& get_state (void);
127         int set_state (const XMLNode&);
128         void set_colors();
129
130         void modify_point_y (ControlPoint&, double);
131
132   protected:
133
134         string    _name;
135         guint32   _height;
136         uint32_t  _line_color;
137         
138         boost::shared_ptr<ARDOUR::AutomationList> alist;
139
140         bool    _visible                  : 1;
141         bool    _uses_gain_mapping        : 1;
142         bool    terminal_points_can_slide : 1;
143         bool    update_pending            : 1;
144         bool    no_draw                   : 1;
145         bool    points_visible            : 1;
146         bool    did_push;
147
148         ArdourCanvas::Group&        _parent_group;
149         ArdourCanvas::Group*        group;
150         ArdourCanvas::Line*         line; /* line */
151         ArdourCanvas::Points        line_points; /* coordinates for canvas line */
152         std::vector<ControlPoint*>  control_points; /* visible control points */
153
154         struct ALPoint {
155             double x;
156             double y;
157             ALPoint (double xx, double yy) : x(xx), y(yy) {}
158         };
159
160         typedef std::vector<ALPoint> ALPoints;
161
162         static void invalidate_point (ALPoints&, uint32_t index);
163         static bool invalid_point (ALPoints&, uint32_t index);
164         
165         void determine_visible_control_points (ALPoints&);
166         void sync_model_with_view_point (ControlPoint&, bool did_push, int64_t distance);
167         void sync_model_with_view_line (uint32_t, uint32_t);
168         
169         virtual void change_model (ARDOUR::AutomationList::iterator, double x, double y);
170
171         void reset_callback (const Evoral::ControlList&);
172         void list_changed ();
173
174         virtual bool event_handler (GdkEvent*);
175         virtual void add_model_point (ALPoints& tmp_points, double frame, double yfract);
176         
177   private:
178         uint32_t drags;
179         double   first_drag_fraction;
180         double   last_drag_fraction;
181         uint32_t line_drag_cp1;
182         uint32_t line_drag_cp2;
183         int64_t  drag_x;
184         int64_t  drag_distance;
185
186         const Evoral::TimeConverter<double, nframes_t>& _time_converter;
187         ARDOUR::AutomationList::InterpolationStyle      _interpolation;
188
189         void modify_view_point (ControlPoint&, double, double, bool with_push);
190         void reset_line_coords (ControlPoint&);
191
192         double control_point_box_size ();
193
194         struct ModelRepresentation {
195             ARDOUR::AutomationList::iterator start;
196             ARDOUR::AutomationList::iterator end;
197             double xpos;
198             double ypos;
199             double xmin;
200             double ymin;
201             double xmax;
202             double ymax;
203             double xval;
204             double yval;
205         };
206
207         void model_representation (ControlPoint&, ModelRepresentation&);
208
209         friend class AudioRegionGainLine;
210 };
211
212 #endif /* __ardour_automation_line_h__ */
213