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