744ec806d498ac0a9c34f1b7bd0d78bce5de22cd
[ardour.git] / gtk2_ardour / automation_line.cc
1 /*
2     Copyright (C) 2002-2003 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 #include <cmath>
21 #include <climits>
22 #include <vector>
23 #include <fstream>
24
25 #include "pbd/stl_delete.h"
26 #include "pbd/memento_command.h"
27 #include "pbd/stacktrace.h"
28
29 #include "ardour/automation_list.h"
30 #include "ardour/dB.h"
31 #include "evoral/Curve.hpp"
32
33 #include "simplerect.h"
34 #include "automation_line.h"
35 #include "control_point.h"
36 #include "gui_thread.h"
37 #include "rgb_macros.h"
38 #include "ardour_ui.h"
39 #include "public_editor.h"
40 #include "utils.h"
41 #include "selection.h"
42 #include "time_axis_view.h"
43 #include "point_selection.h"
44 #include "automation_time_axis.h"
45
46 #include "ardour/event_type_map.h"
47 #include "ardour/session.h"
48
49 #include "i18n.h"
50
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Editing;
55 using namespace Gnome; // for Canvas
56
57 /** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session frames.
58  *  This will not be deleted by AutomationLine.
59  */
60 AutomationLine::AutomationLine (const string& name, TimeAxisView& tv, ArdourCanvas::Group& parent,
61                 boost::shared_ptr<AutomationList> al,
62                 Evoral::TimeConverter<double, framepos_t>* converter)
63         : trackview (tv)
64         , _name (name)
65         , alist (al)
66         , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, framepos_t>)
67         , _parent_group (parent)
68         , _offset (0)
69         , _maximum_time (max_framepos)
70 {
71         if (converter) {
72                 _time_converter = converter;
73                 _our_time_converter = false;
74         } else {
75                 _time_converter = new Evoral::IdentityConverter<double, framepos_t>;
76                 _our_time_converter = true;
77         }
78         
79         _visible = Line;
80
81         update_pending = false;
82         _uses_gain_mapping = false;
83         no_draw = false;
84         _is_boolean = false;
85         terminal_points_can_slide = true;
86         _height = 0;
87
88         group = new ArdourCanvas::Group (parent);
89         group->property_x() = 0.0;
90         group->property_y() = 0.0;
91
92         line = new ArdourCanvas::Line (*group);
93         line->property_width_pixels() = (guint)1;
94         line->set_data ("line", this);
95
96         line->signal_event().connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
97
98         trackview.session()->register_with_memento_command_factory(alist->id(), this);
99
100         if (alist->parameter().type() == GainAutomation ||
101             alist->parameter().type() == EnvelopeAutomation) {
102                 set_uses_gain_mapping (true);
103         }
104
105         interpolation_changed (alist->interpolation ());
106
107         connect_to_list ();
108 }
109
110 AutomationLine::~AutomationLine ()
111 {
112         vector_delete (&control_points);
113         delete group;
114
115         if (_our_time_converter) {
116                 delete _time_converter;
117         }
118 }
119
120 bool
121 AutomationLine::event_handler (GdkEvent* event)
122 {
123         return PublicEditor::instance().canvas_line_event (event, line, this);
124 }
125
126 void
127 AutomationLine::queue_reset ()
128 {
129         if (!update_pending) {
130                 update_pending = true;
131                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationLine::reset, this));
132         }
133 }
134
135 void
136 AutomationLine::show ()
137 {
138         if (_visible & Line) {
139                 if (alist->interpolation() != AutomationList::Discrete) {
140                         line->show();
141                 } else {
142                         line->hide ();
143                 }
144         } else {
145                 line->hide();
146         }
147
148         if (_visible & ControlPoints) {
149                 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
150                         (*i)->set_visible (true);
151                         (*i)->show ();
152                 }
153         } else if (_visible & SelectedControlPoints) {
154                 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
155                         (*i)->set_visible ((*i)->get_selected());
156                 }
157         } else {
158                 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
159                         (*i)->set_visible (false);
160                 }
161         }
162 }
163
164 void
165 AutomationLine::hide ()
166 {
167         set_visibility (VisibleAspects (0));
168 }
169
170 double
171 AutomationLine::control_point_box_size ()
172 {
173         if (alist->interpolation() == AutomationList::Discrete) {
174                 return max((_height*4.0) / (double)(alist->parameter().max() - alist->parameter().min()),
175                                 4.0);
176         }
177
178         if (_height > TimeAxisView::preset_height (HeightLarger)) {
179                 return 8.0;
180         } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
181                 return 6.0;
182         }
183         return 4.0;
184 }
185
186 void
187 AutomationLine::set_height (guint32 h)
188 {
189         if (h != _height) {
190                 _height = h;
191
192                 double bsz = control_point_box_size();
193
194                 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
195                         (*i)->set_size (bsz);
196                 }
197
198                 reset ();
199         }
200 }
201
202 void
203 AutomationLine::set_line_color (uint32_t color)
204 {
205         _line_color = color;
206         line->property_fill_color_rgba() = color;
207 }
208
209 void
210 AutomationLine::set_uses_gain_mapping (bool yn)
211 {
212         if (yn != _uses_gain_mapping) {
213                 _uses_gain_mapping = yn;
214                 reset ();
215         }
216 }
217
218 ControlPoint*
219 AutomationLine::nth (uint32_t n)
220 {
221         if (n < control_points.size()) {
222                 return control_points[n];
223         } else {
224                 return 0;
225         }
226 }
227
228 ControlPoint const *
229 AutomationLine::nth (uint32_t n) const
230 {
231         if (n < control_points.size()) {
232                 return control_points[n];
233         } else {
234                 return 0;
235         }
236 }
237
238 void
239 AutomationLine::modify_point_y (ControlPoint& cp, double y)
240 {
241         /* clamp y-coord appropriately. y is supposed to be a normalized fraction (0.0-1.0),
242            and needs to be converted to a canvas unit distance.
243         */
244
245         y = max (0.0, y);
246         y = min (1.0, y);
247         y = _height - (y * _height);
248
249         double const x = trackview.editor().frame_to_unit_unrounded (_time_converter->to((*cp.model())->when) - _offset);
250
251         trackview.editor().session()->begin_reversible_command (_("automation event move"));
252         trackview.editor().session()->add_command (
253                 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0)
254                 );
255
256         cp.move_to (x, y, ControlPoint::Full);
257
258         reset_line_coords (cp);
259
260         if (line_points.size() > 1) {
261                 line->property_points() = line_points;
262         }
263
264         alist->freeze ();
265         sync_model_with_view_point (cp, 0);
266         alist->thaw ();
267
268         update_pending = false;
269
270         trackview.editor().session()->add_command (
271                 new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state())
272                 );
273
274         trackview.editor().session()->commit_reversible_command ();
275         trackview.editor().session()->set_dirty ();
276 }
277
278 void
279 AutomationLine::reset_line_coords (ControlPoint& cp)
280 {
281         if (cp.view_index() < line_points.size()) {
282                 line_points[cp.view_index()].set_x (cp.get_x());
283                 line_points[cp.view_index()].set_y (cp.get_y());
284         }
285 }
286
287 void
288 AutomationLine::sync_model_with_view_points (list<ControlPoint*> cp, int64_t distance)
289 {
290         update_pending = true;
291
292         for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
293                 sync_model_with_view_point (**i, distance);
294         }
295 }
296
297 string
298 AutomationLine::get_verbose_cursor_string (double fraction) const
299 {
300         std::string s = fraction_to_string (fraction);
301         if (_uses_gain_mapping) {
302                 s += " dB";
303         }
304
305         return s;
306 }
307
308 /**
309  *  @param fraction y fraction
310  *  @return string representation of this value, using dB if appropriate.
311  */
312 string
313 AutomationLine::fraction_to_string (double fraction) const
314 {
315         char buf[32];
316
317         if (_uses_gain_mapping) {
318                 if (fraction == 0.0) {
319                         snprintf (buf, sizeof (buf), "-inf");
320                 } else {
321                         snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, Config->get_max_gain())));
322                 }
323         } else {
324                 view_to_model_coord_y (fraction);
325                 if (EventTypeMap::instance().is_integer (alist->parameter())) {
326                         snprintf (buf, sizeof (buf), "%d", (int)fraction);
327                 } else {
328                         snprintf (buf, sizeof (buf), "%.2f", fraction);
329                 }
330         }
331
332         return buf;
333 }
334
335
336 /**
337  *  @param s Value string in the form as returned by fraction_to_string.
338  *  @return Corresponding y fraction.
339  */
340 double
341 AutomationLine::string_to_fraction (string const & s) const
342 {
343         if (s == "-inf") {
344                 return 0;
345         }
346
347         double v;
348         sscanf (s.c_str(), "%lf", &v);
349
350         if (_uses_gain_mapping) {
351                 v = gain_to_slider_position_with_max (dB_to_coefficient (v), Config->get_max_gain());
352         } else {
353                 double dummy = 0.0;
354                 model_to_view_coord (dummy, v);
355         }
356
357         return v;
358 }
359
360 /** Start dragging a single point, possibly adding others if the supplied point is selected and there
361  *  are other selected points.
362  *
363  *  @param cp Point to drag.
364  *  @param x Initial x position (units).
365  *  @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
366  */
367 void
368 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
369 {
370         trackview.editor().session()->begin_reversible_command (_("automation event move"));
371         trackview.editor().session()->add_command (
372                 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0)
373                 );
374
375         _drag_points.clear ();
376         _drag_points.push_back (cp);
377
378         if (cp->get_selected ()) {
379                 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
380                         if (*i != cp && (*i)->get_selected()) {
381                                 _drag_points.push_back (*i);
382                         }
383                 }
384         }
385
386         start_drag_common (x, fraction);
387 }
388
389 /** Start dragging a line vertically (with no change in x)
390  *  @param i1 Control point index of the `left' point on the line.
391  *  @param i2 Control point index of the `right' point on the line.
392  *  @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
393  */
394 void
395 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
396 {
397         trackview.editor().session()->begin_reversible_command (_("automation range move"));
398         trackview.editor().session()->add_command (
399                 new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0)
400                 );
401
402         _drag_points.clear ();
403         for (uint32_t i = i1; i <= i2; i++) {
404                 _drag_points.push_back (nth (i));
405         }
406
407         start_drag_common (0, fraction);
408 }
409
410 /** Start dragging multiple points (with no change in x)
411  *  @param cp Points to drag.
412  *  @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
413  */
414 void
415 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
416 {
417         trackview.editor().session()->begin_reversible_command (_("automation range move"));
418         trackview.editor().session()->add_command (
419                 new MementoCommand<AutomationList> (memento_command_binder(), state, 0)
420                 );
421
422         _drag_points = cp;
423         start_drag_common (0, fraction);
424 }
425
426
427 struct ControlPointSorter
428 {
429         bool operator() (ControlPoint const * a, ControlPoint const * b) {
430                 return a->get_x() < b->get_x();
431         }
432 };
433
434 /** Common parts of starting a drag.
435  *  @param x Starting x position in units, or 0 if x is being ignored.
436  *  @param fraction Starting y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
437  */
438 void
439 AutomationLine::start_drag_common (double x, float fraction)
440 {
441         _drag_x = x;
442         _drag_distance = 0;
443         _last_drag_fraction = fraction;
444         _drag_had_movement = false;
445         did_push = false;
446
447         _drag_points.sort (ControlPointSorter ());
448
449         /* find the additional points that will be dragged when the user is holding
450            the "push" modifier
451         */
452
453         uint32_t i = _drag_points.back()->view_index () + 1;
454         ControlPoint* p = 0;
455         _push_points.clear ();
456         while ((p = nth (i)) != 0 && p->can_slide()) {
457                 _push_points.push_back (p);
458                 ++i;
459         }
460 }
461
462 /** Should be called to indicate motion during a drag.
463  *  @param x New x position of the drag in canvas units, or undefined if ignore_x == true.
464  *  @param fraction New y fraction.
465  *  @return x position and y fraction that were actually used (once clamped).
466  */
467 pair<double, float>
468 AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool with_push)
469 {
470         /* setup the points that are to be moved this time round */
471         list<ControlPoint*> points = _drag_points;
472         if (with_push) {
473                 copy (_push_points.begin(), _push_points.end(), back_inserter (points));
474                 points.sort (ControlPointSorter ());
475         }
476
477         double dx = ignore_x ? 0 : (x - _drag_x);
478         double dy = fraction - _last_drag_fraction;
479
480         for (list<ControlPoint*>::iterator i = points.begin(); i != points.end(); ++i) {
481                 /* Find the points that aren't being moved before and after
482                    this one on the control_points list
483                 */
484
485                 ControlPoint* before = 0;
486                 ControlPoint* after = 0;
487
488                 ControlPoint* last = 0;
489                 for (vector<ControlPoint*>::iterator j = control_points.begin(); j != control_points.end(); ++j) {
490
491                         if (*j == *i) {
492                                 
493                                 before = last;
494                                 
495                                 vector<ControlPoint*>::iterator k = j;
496
497                                 /* Next point */
498                                 ++k;
499
500                                 /* Now move past any points that are being moved this time */
501                                 while (find (points.begin(), points.end(), *k) != points.end() && k != control_points.end ()) {
502                                         ++k;
503                                 }
504                                 
505                                 if (k != control_points.end()) {
506                                         after = *k;
507                                 }
508                                 break;
509                         }
510
511                         if (find (points.begin(), points.end(), *j) == points.end ()) {
512                                 /* This point isn't being moved, so it's the `last' point we've seen */
513                                 last = *j;
514                         }
515                 }
516
517                 /* Clamp dx for this point */
518                 double const before_x = before ? before->get_x() : 0;
519                 double const after_x = after ? after->get_x() : DBL_MAX;
520
521                 double tx = (*i)->get_x() + dx;
522                 tx = max (tx, before_x);
523                 tx = min (tx, after_x);
524                 dx = tx - (*i)->get_x ();
525         }
526
527         /* clamp y */
528         for (list<ControlPoint*>::iterator i = points.begin(); i != points.end(); ++i) {
529                 double const y = ((_height - (*i)->get_y()) / _height) + dy;
530                 if (y < 0) {
531                         dy -= y;
532                 }
533                 if (y > 1) {
534                         dy -= (y - 1);
535                 }
536         }
537
538         pair<double, float> const clamped (_drag_x + dx, _last_drag_fraction + dy);
539         _drag_distance += dx;
540         _drag_x += dx;
541         _last_drag_fraction = fraction;
542
543         for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
544                 (*i)->move_to ((*i)->get_x() + dx, (*i)->get_y() - _height * dy, ControlPoint::Full);
545                 reset_line_coords (**i);
546         }
547
548         if (with_push) {
549                 /* move push points, preserving their y */
550                 for (list<ControlPoint*>::iterator i = _push_points.begin(); i != _push_points.end(); ++i) {
551                         (*i)->move_to ((*i)->get_x() + dx, (*i)->get_y(), ControlPoint::Full);
552                         reset_line_coords (**i);
553                 }
554         }
555
556         if (line_points.size() > 1) {
557                 line->property_points() = line_points;
558         }
559
560         _drag_had_movement = true;
561         if (with_push) {
562                 did_push = with_push;
563         }
564
565         return clamped;
566 }
567
568 /** Should be called to indicate the end of a drag */
569 void
570 AutomationLine::end_drag ()
571 {
572         if (!_drag_had_movement) {
573                 return;
574         }
575
576         alist->freeze ();
577
578         /* set up the points that were moved this time round */
579         list<ControlPoint*> points = _drag_points;
580         if (did_push) {
581                 copy (_push_points.begin(), _push_points.end(), back_inserter (points));
582                 points.sort (ControlPointSorter ());
583         }
584
585         sync_model_with_view_points (points, trackview.editor().unit_to_frame (_drag_distance));
586
587         alist->thaw ();
588
589         update_pending = false;
590
591         trackview.editor().session()->add_command (
592                 new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state())
593                 );
594
595         trackview.editor().session()->set_dirty ();
596         did_push = false;
597 }
598
599 void
600 AutomationLine::sync_model_with_view_point (ControlPoint& cp, framecnt_t distance)
601 {
602         /* find out where the visual control point is.
603            initial results are in canvas units. ask the
604            line to convert them to something relevant.
605         */
606
607         double view_x = cp.get_x();
608         double view_y = 1.0 - (cp.get_y() / _height);
609
610         /* if xval has not changed, set it directly from the model to avoid rounding errors */
611
612         if (view_x == trackview.editor().frame_to_unit_unrounded (_time_converter->to ((*cp.model())->when)) - _offset) {
613                 view_x = (*cp.model())->when - _offset;
614         } else {
615                 view_x = trackview.editor().unit_to_frame (view_x);
616                 view_x = _time_converter->from (view_x + _offset);
617         }
618
619         update_pending = true;
620
621         view_to_model_coord_y (view_y);
622
623         alist->modify (cp.model(), view_x, view_y);
624 }
625
626 bool
627 AutomationLine::control_points_adjacent (double xval, uint32_t & before, uint32_t& after)
628 {
629         ControlPoint *bcp = 0;
630         ControlPoint *acp = 0;
631         double unit_xval;
632
633         unit_xval = trackview.editor().frame_to_unit_unrounded (xval);
634
635         for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
636
637                 if ((*i)->get_x() <= unit_xval) {
638
639                         if (!bcp || (*i)->get_x() > bcp->get_x()) {
640                                 bcp = *i;
641                                 before = bcp->view_index();
642                         }
643
644                 } else if ((*i)->get_x() > unit_xval) {
645                         acp = *i;
646                         after = acp->view_index();
647                         break;
648                 }
649         }
650
651         return bcp && acp;
652 }
653
654 bool
655 AutomationLine::is_last_point (ControlPoint& cp)
656 {
657         // If the list is not empty, and the point is the last point in the list
658
659         if (alist->empty()) {
660                 return false;
661         }
662
663         AutomationList::const_iterator i = alist->end();
664         --i;
665
666         if (cp.model() == i) {
667                 return true;
668         }
669
670         return false;
671 }
672
673 bool
674 AutomationLine::is_first_point (ControlPoint& cp)
675 {
676         // If the list is not empty, and the point is the first point in the list
677
678         if (!alist->empty() && cp.model() == alist->begin()) {
679                 return true;
680         }
681
682         return false;
683 }
684
685 // This is copied into AudioRegionGainLine
686 void
687 AutomationLine::remove_point (ControlPoint& cp)
688 {
689         trackview.editor().session()->begin_reversible_command (_("remove control point"));
690         XMLNode &before = alist->get_state();
691
692         alist->erase (cp.model());
693         
694         trackview.editor().session()->add_command(
695                 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state())
696                 );
697
698         trackview.editor().session()->commit_reversible_command ();
699         trackview.editor().session()->set_dirty ();
700 }
701
702 /** Get selectable points within an area.
703  *  @param start Start position in session frames.
704  *  @param end End position in session frames.
705  *  @param bot Bottom y range, as a fraction of line height, where 0 is the bottom of the line.
706  *  @param top Top y range, as a fraction of line height, where 0 is the bottom of the line.
707  *  @param result Filled in with selectable things; in this case, ControlPoints.
708  */
709 void
710 AutomationLine::get_selectables (
711         framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results
712         )
713 {
714         /* convert fractions to display coordinates with 0 at the top of the track */
715         double const bot_track = (1 - topfrac) * trackview.current_height ();
716         double const top_track = (1 - botfrac) * trackview.current_height ();
717
718         for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
719                 double const model_when = (*(*i)->model())->when;
720
721                 /* model_when is relative to the start of the source, so we just need to add on the origin_b here
722                    (as it is the session frame position of the start of the source)
723                 */
724                 
725                 framepos_t const session_frames_when = _time_converter->to (model_when) + _time_converter->origin_b ();
726
727                 if (session_frames_when >= start && session_frames_when <= end && (*i)->get_y() >= bot_track && (*i)->get_y() <= top_track) {
728                         results.push_back (*i);
729                 }
730         }
731 }
732
733 void
734 AutomationLine::get_inverted_selectables (Selection&, list<Selectable*>& /*results*/)
735 {
736         // hmmm ....
737 }
738
739 void
740 AutomationLine::set_selected_points (PointSelection const & points)
741 {
742         for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
743                 (*i)->set_selected (false);
744         }
745
746         for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
747                 (*i)->set_selected (true);
748         }
749
750         set_colors ();
751 }
752
753 void AutomationLine::set_colors ()
754 {
755         set_line_color (ARDOUR_UI::config()->canvasvar_AutomationLine.get());
756         for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
757                 (*i)->set_color ();
758         }
759 }
760
761 void
762 AutomationLine::list_changed ()
763 {
764         queue_reset ();
765 }
766
767 void
768 AutomationLine::reset_callback (const Evoral::ControlList& events)
769 {
770         uint32_t vp = 0;
771         uint32_t pi = 0;
772         uint32_t np;
773
774         if (events.empty()) {
775                 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
776                         delete *i;
777                 }
778                 control_points.clear ();
779                 line->hide();
780                 return;
781         }
782
783         /* hide all existing points, and the line */
784
785         for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
786                 (*i)->hide();
787         }
788
789         line->hide ();
790         np = events.size();
791
792         Evoral::ControlList& e = const_cast<Evoral::ControlList&> (events);
793         
794         for (AutomationList::iterator ai = e.begin(); ai != e.end(); ++ai, ++pi) {
795
796                 double tx = (*ai)->when;
797                 double ty = (*ai)->value;
798
799                 /* convert from model coordinates to canonical view coordinates */
800
801                 model_to_view_coord (tx, ty);
802
803                 if (std::isnan (tx) || std::isnan (ty)) {
804                         warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
805                                                    _name) << endmsg;
806                         continue;
807                 }
808                 
809                 if (tx >= max_framepos || tx < 0 || tx >= _maximum_time) {
810                         continue;
811                 }
812                 
813                 /* convert x-coordinate to a canvas unit coordinate (this takes
814                  * zoom and scroll into account).
815                  */
816                         
817                 tx = trackview.editor().frame_to_unit_unrounded (tx);
818                 
819                 /* convert from canonical view height (0..1.0) to actual
820                  * height coordinates (using X11's top-left rooted system)
821                  */
822
823                 ty = _height - (ty * _height);
824
825                 add_visible_control_point (vp, pi, tx, ty, ai, np);
826                 vp++;
827         }
828
829         /* discard extra CP's to avoid confusing ourselves */
830
831         while (control_points.size() > vp) {
832                 ControlPoint* cp = control_points.back();
833                 control_points.pop_back ();
834                 delete cp;
835         }
836
837         if (!terminal_points_can_slide) {
838                 control_points.back()->set_can_slide(false);
839         }
840
841         if (vp > 1) {
842
843                 /* reset the line coordinates given to the CanvasLine */
844
845                 while (line_points.size() < vp) {
846                         line_points.push_back (Art::Point (0,0));
847                 }
848
849                 while (line_points.size() > vp) {
850                         line_points.pop_back ();
851                 }
852
853                 for (uint32_t n = 0; n < vp; ++n) {
854                         line_points[n].set_x (control_points[n]->get_x());
855                         line_points[n].set_y (control_points[n]->get_y());
856                 }
857
858                 line->property_points() = line_points;
859
860                 if (_visible && alist->interpolation() != AutomationList::Discrete) {
861                         line->show();
862                 }
863         }
864
865         set_selected_points (trackview.editor().get_selection().points);
866 }
867
868 void
869 AutomationLine::reset ()
870 {
871         update_pending = false;
872
873         if (no_draw) {
874                 return;
875         }
876
877         alist->apply_to_points (*this, &AutomationLine::reset_callback);
878 }
879
880 void
881 AutomationLine::clear ()
882 {
883         /* parent must create and commit command */
884         XMLNode &before = alist->get_state();
885         alist->clear();
886
887         trackview.editor().session()->add_command (
888                 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state())
889                 );
890 }
891
892 void
893 AutomationLine::change_model (AutomationList::iterator /*i*/, double /*x*/, double /*y*/)
894 {
895 }
896
897 void
898 AutomationLine::set_list (boost::shared_ptr<ARDOUR::AutomationList> list)
899 {
900         alist = list;
901         queue_reset ();
902         connect_to_list ();
903 }
904
905 void
906 AutomationLine::add_visibility (VisibleAspects va)
907 {
908         _visible = VisibleAspects (_visible | va);
909         show ();
910 }
911
912 void
913 AutomationLine::set_visibility (VisibleAspects va)
914 {
915         _visible = va;
916         show ();
917 }
918
919 void
920 AutomationLine::remove_visibility (VisibleAspects va)
921 {
922         _visible = VisibleAspects (_visible & ~va);
923         show ();
924 }
925
926 void
927 AutomationLine::track_entered()
928 {
929         if (alist->interpolation() != AutomationList::Discrete) {
930                 add_visibility (ControlPoints);
931         }
932 }
933
934 void
935 AutomationLine::track_exited()
936 {
937         if (alist->interpolation() != AutomationList::Discrete) {
938                 remove_visibility (ControlPoints);
939         }
940 }
941
942 XMLNode &
943 AutomationLine::get_state (void)
944 {
945         /* function as a proxy for the model */
946         return alist->get_state();
947 }
948
949 int
950 AutomationLine::set_state (const XMLNode &node, int version)
951 {
952         /* function as a proxy for the model */
953         return alist->set_state (node, version);
954 }
955
956 void
957 AutomationLine::view_to_model_coord (double& x, double& y) const
958 {
959         x = _time_converter->from (x);
960         view_to_model_coord_y (y);
961 }
962
963 void
964 AutomationLine::view_to_model_coord_y (double& y) const
965 {
966         /* TODO: This should be more generic ... */
967         if (alist->parameter().type() == GainAutomation ||
968             alist->parameter().type() == EnvelopeAutomation) {
969                 y = slider_position_to_gain_with_max (y, Config->get_max_gain());
970                 y = max (0.0, y);
971                 y = min (2.0, y);
972         } else if (alist->parameter().type() == PanAzimuthAutomation ||
973                    alist->parameter().type() == PanElevationAutomation ||
974                    alist->parameter().type() == PanWidthAutomation) {
975                 y = 1.0 - y;
976         } else if (alist->parameter().type() == PluginAutomation) {
977                 y = y * (double)(alist->get_max_y()- alist->get_min_y()) + alist->get_min_y();
978         } else {
979                 y = rint (y * alist->parameter().max());
980         }
981 }
982
983 void
984 AutomationLine::model_to_view_coord (double& x, double& y) const
985 {
986         /* TODO: This should be more generic ... */
987         if (alist->parameter().type() == GainAutomation ||
988             alist->parameter().type() == EnvelopeAutomation) {
989                 y = gain_to_slider_position_with_max (y, Config->get_max_gain());
990         } else if (alist->parameter().type() == PanAzimuthAutomation ||
991                    alist->parameter().type() == PanElevationAutomation ||
992                    alist->parameter().type() == PanWidthAutomation) {
993                 // vertical coordinate axis reversal
994                 y = 1.0 - y;
995         } else if (alist->parameter().type() == PluginAutomation) {
996                 y = (y - alist->get_min_y()) / (double)(alist->get_max_y()- alist->get_min_y());
997         } else {
998                 y = y / (double)alist->parameter().max(); /* ... like this */
999         }
1000
1001         x = _time_converter->to (x) - _offset;
1002 }
1003
1004 /** Called when our list has announced that its interpolation style has changed */
1005 void
1006 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
1007 {
1008         if (style == AutomationList::Discrete) {
1009                 set_visibility (ControlPoints);
1010                 line->hide();
1011         } else {
1012                 set_visibility (Line);
1013         }
1014 }
1015
1016 void
1017 AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty, 
1018                                            AutomationList::iterator model, uint32_t npoints)
1019 {
1020         ControlPoint::ShapeType shape;
1021
1022         if (view_index >= control_points.size()) {
1023
1024                 /* make sure we have enough control points */
1025
1026                 ControlPoint* ncp = new ControlPoint (*this);
1027                 ncp->set_size (control_point_box_size ());
1028
1029                 control_points.push_back (ncp);
1030         }
1031
1032         if (!terminal_points_can_slide) {
1033                 if (pi == 0) {
1034                         control_points[view_index]->set_can_slide (false);
1035                         if (tx == 0) {
1036                                 shape = ControlPoint::Start;
1037                         } else {
1038                                 shape = ControlPoint::Full;
1039                         }
1040                 } else if (pi == npoints - 1) {
1041                         control_points[view_index]->set_can_slide (false);
1042                         shape = ControlPoint::End;
1043                 } else {
1044                         control_points[view_index]->set_can_slide (true);
1045                         shape = ControlPoint::Full;
1046                 }
1047         } else {
1048                 control_points[view_index]->set_can_slide (true);
1049                 shape = ControlPoint::Full;
1050         }
1051
1052         control_points[view_index]->reset (tx, ty, model, view_index, shape);
1053
1054         /* finally, control visibility */
1055
1056         if (_visible & ControlPoints) {
1057                 control_points[view_index]->show ();
1058                 control_points[view_index]->set_visible (true);
1059         } else {
1060                 control_points[view_index]->set_visible (false);
1061         }
1062 }
1063
1064 void
1065 AutomationLine::connect_to_list ()
1066 {
1067         _list_connections.drop_connections ();
1068
1069         alist->StateChanged.connect (_list_connections, invalidator (*this), boost::bind (&AutomationLine::list_changed, this), gui_context());
1070
1071         alist->InterpolationChanged.connect (
1072                 _list_connections, invalidator (*this), boost::bind (&AutomationLine::interpolation_changed, this, _1), gui_context()
1073                 );
1074 }
1075
1076 MementoCommandBinder<AutomationList>*
1077 AutomationLine::memento_command_binder ()
1078 {
1079         return new SimpleMementoCommandBinder<AutomationList> (*alist.get());
1080 }
1081
1082 /** Set the maximum time that points on this line can be at, relative
1083  *  to the start of the track or region that it is on.
1084  */
1085 void
1086 AutomationLine::set_maximum_time (framecnt_t t)
1087 {
1088         if (_maximum_time == t) {
1089                 return;
1090         }
1091
1092         _maximum_time = t;
1093         reset ();
1094 }
1095
1096
1097 /** @return min and max x positions of points that are in the list, in session frames */
1098 pair<framepos_t, framepos_t>
1099 AutomationLine::get_point_x_range () const
1100 {
1101         pair<framepos_t, framepos_t> r (max_framepos, 0);
1102
1103         for (AutomationList::const_iterator i = the_list()->begin(); i != the_list()->end(); ++i) {
1104                 r.first = min (r.first, session_position (i));
1105                 r.second = max (r.second, session_position (i));
1106         }
1107
1108         return r;
1109 }
1110
1111 framepos_t
1112 AutomationLine::session_position (AutomationList::const_iterator p) const
1113 {
1114         return _time_converter->to ((*p)->when) + _offset + _time_converter->origin_b ();
1115 }
1116
1117 void
1118 AutomationLine::set_offset (framepos_t off)
1119 {
1120         if (_offset == off) {
1121                 return;
1122         }
1123
1124         _offset = off;
1125         reset ();
1126 }