Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / automation_time_axis.cc
1 /*
2     Copyright (C) 2000-2007 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 <utility>
21 #include <gtkmm2ext/barcontroller.h>
22 #include <gtkmm2ext/utils.h>
23 #include <boost/algorithm/string.hpp>
24 #include <boost/lexical_cast.hpp>
25
26 #include "pbd/memento_command.h"
27 #include "pbd/stacktrace.h"
28
29 #include "ardour/automation_control.h"
30 #include "ardour/event_type_map.h"
31 #include "ardour/route.h"
32 #include "ardour/session.h"
33
34 #include "canvas/debug.h"
35
36 #include "ardour_ui.h"
37 #include "automation_time_axis.h"
38 #include "automation_streamview.h"
39 #include "global_signals.h"
40 #include "gui_thread.h"
41 #include "route_time_axis.h"
42 #include "automation_line.h"
43 #include "public_editor.h"
44 #include "selection.h"
45 #include "rgb_macros.h"
46 #include "point_selection.h"
47 #include "control_point.h"
48 #include "utils.h"
49
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Gtk;
56 using namespace Gtkmm2ext;
57 using namespace Editing;
58
59 Pango::FontDescription AutomationTimeAxisView::name_font;
60 bool AutomationTimeAxisView::have_name_font = false;
61
62
63 /** \a a the automatable object this time axis is to display data for.
64  * For route/track automation (e.g. gain) pass the route for both \r and \a.
65  * For route child (e.g. plugin) automation, pass the child for \a.
66  * For region automation (e.g. MIDI CC), pass null for \a.
67  */
68 AutomationTimeAxisView::AutomationTimeAxisView (
69         Session* s,
70         boost::shared_ptr<Route> r,
71         boost::shared_ptr<Automatable> a,
72         boost::shared_ptr<AutomationControl> c,
73         Evoral::Parameter p,
74         PublicEditor& e,
75         TimeAxisView& parent,
76         bool show_regions,
77         ArdourCanvas::Canvas& canvas,
78         const string & nom,
79         const string & nomparent
80         )
81         : AxisView (s)
82         , TimeAxisView (s, e, &parent, canvas)
83         , _route (r)
84         , _control (c)
85         , _automatable (a)
86         , _parameter (p)
87         , _base_rect (new ArdourCanvas::Rectangle (_canvas_display))
88         , _name (nom)
89         , _view (show_regions ? new AutomationStreamView (*this) : 0)
90         , auto_button (X_("")) /* force addition of a label */
91         , _show_regions (show_regions)
92 {
93
94         CANVAS_DEBUG_NAME (_canvas_display, string_compose ("main for auto %2/%1", _name, r->name()));
95         CANVAS_DEBUG_NAME (selection_group, string_compose ("selections for auto %2/%1", _name, r->name()));
96         CANVAS_DEBUG_NAME (_ghost_group, string_compose ("ghosts for auto %2/%1", _name, r->name()));
97
98         if (!have_name_font) {
99                 name_font = get_font_for_style (X_("AutomationTrackName"));
100                 have_name_font = true;
101         }
102
103         if (_automatable && _control) {
104                 _controller = AutomationController::create (_automatable, _control->parameter(), _control);
105         }
106
107         automation_menu = 0;
108         auto_off_item = 0;
109         auto_touch_item = 0;
110         auto_write_item = 0;
111         auto_play_item = 0;
112         mode_discrete_item = 0;
113         mode_line_item = 0;
114
115         ignore_state_request = false;
116         first_call_to_set_height = true;
117
118         CANVAS_DEBUG_NAME (_base_rect, string_compose ("base rect for %1", _name));
119         _base_rect->set_x1 (ArdourCanvas::COORD_MAX);
120         _base_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_AutomationTrackOutline());
121         _base_rect->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
122         _base_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_AutomationTrackFill());
123         _base_rect->set_data ("trackview", this);
124         _base_rect->Event.connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_automation_track_event), _base_rect, this));
125         if (!a) {
126                 _base_rect->lower_to_bottom();
127         }
128
129         hide_button.add (*(manage (new Gtk::Image (::get_icon("hide")))));
130
131         auto_button.set_name ("TrackVisualButton");
132         hide_button.set_name ("TrackRemoveButton");
133
134         auto_button.unset_flags (Gtk::CAN_FOCUS);
135         hide_button.unset_flags (Gtk::CAN_FOCUS);
136
137         controls_table.set_no_show_all();
138
139         ARDOUR_UI::instance()->set_tip(auto_button, _("automation state"));
140         ARDOUR_UI::instance()->set_tip(hide_button, _("hide track"));
141
142         const string str = gui_property ("height");
143         if (!str.empty()) {
144                 set_height (atoi (str));
145         } else {
146                 set_height (preset_height (HeightNormal));
147         }
148
149         /* repack the name label */
150
151         if (name_label.get_parent()) {
152                 name_label.get_parent()->remove (name_label);
153         }
154         
155         name_label.set_text (_name);
156         name_label.set_alignment (Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
157         name_label.set_name (X_("TrackParameterName"));
158         name_label.set_ellipsize (Pango::ELLIPSIZE_END);
159
160         string tipname = nomparent;
161         if (!tipname.empty()) {
162                 tipname += ": ";
163         }
164         tipname += _name;
165         ARDOUR_UI::instance()->set_tip(controls_ebox, tipname);
166
167         /* add the buttons */
168         controls_table.attach (hide_button, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
169         controls_table.attach (name_label, 0, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
170         controls_table.attach (auto_button, 6, 8, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
171
172         name_label.show ();
173
174         if (_controller) {
175                 _controller.get()->set_size_request(-1, 24);
176                 /* add bar controller */
177                 controls_table.attach (*_controller.get(), 1, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
178                 /* note that this handler connects *before* the default handler */
179                 _controller->event_widget().signal_scroll_event().connect (mem_fun (*this, &AutomationTimeAxisView::controls_ebox_scroll), false);
180         }
181
182         controls_table.show_all ();
183
184         hide_button.signal_clicked().connect (sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked));
185         auto_button.signal_clicked().connect (sigc::mem_fun(*this, &AutomationTimeAxisView::auto_clicked));
186
187         controls_base_selected_name = X_("AutomationTrackControlsBaseSelected");
188         controls_base_unselected_name = X_("AutomationTrackControlsBase");
189         controls_ebox.set_name (controls_base_unselected_name);
190
191         /* ask for notifications of any new RegionViews */
192         if (show_regions) {
193
194                 if (_view) {
195                         _view->attach ();
196                 }
197
198         } else {
199                 /* no regions, just a single line for the entire track (e.g. bus gain) */
200
201                 assert (_control);
202
203                 boost::shared_ptr<AutomationLine> line (
204                         new AutomationLine (
205                                 ARDOUR::EventTypeMap::instance().to_symbol(_parameter),
206                                 *this,
207                                 *_canvas_display,
208                                 _control->alist()
209                                 )
210                         );
211
212                 line->set_line_color (ARDOUR_UI::config()->get_canvasvar_ProcessorAutomationLine());
213                 line->queue_reset ();
214                 add_line (line);
215         }
216
217         /* make sure labels etc. are correct */
218
219         automation_state_changed ();
220         ColorsChanged.connect (sigc::mem_fun (*this, &AutomationTimeAxisView::color_handler));
221
222         _route->DropReferences.connect (
223                 _route_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::route_going_away, this), gui_context ()
224                 );
225 }
226
227 AutomationTimeAxisView::~AutomationTimeAxisView ()
228 {
229         delete _view;
230 }
231
232 void
233 AutomationTimeAxisView::route_going_away ()
234 {
235         _route.reset ();
236 }
237
238 void
239 AutomationTimeAxisView::auto_clicked ()
240 {
241         using namespace Menu_Helpers;
242
243         if (automation_menu == 0) {
244                 automation_menu = manage (new Menu);
245                 automation_menu->set_name ("ArdourContextMenu");
246                 MenuList& items (automation_menu->items());
247
248                 items.push_back (MenuElem (S_("Automation|Manual"), sigc::bind (sigc::mem_fun(*this,
249                                                                                               &AutomationTimeAxisView::set_automation_state), (AutoState) ARDOUR::Off)));
250                 items.push_back (MenuElem (_("Play"), sigc::bind (sigc::mem_fun(*this,
251                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Play)));
252                 items.push_back (MenuElem (_("Write"), sigc::bind (sigc::mem_fun(*this,
253                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Write)));
254                 items.push_back (MenuElem (_("Touch"), sigc::bind (sigc::mem_fun(*this,
255                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Touch)));
256         }
257
258         automation_menu->popup (1, gtk_get_current_event_time());
259 }
260
261 void
262 AutomationTimeAxisView::set_automation_state (AutoState state)
263 {
264         if (ignore_state_request) {
265                 return;
266         }
267
268         if (_automatable) {
269                 _automatable->set_parameter_automation_state (_parameter, state);
270         }
271
272         if (_view) {
273                 _view->set_automation_state (state);
274
275                 /* AutomationStreamViews don't signal when their automation state changes, so handle
276                    our updates `manually'.
277                 */
278                 automation_state_changed ();
279         }
280 }
281
282 void
283 AutomationTimeAxisView::automation_state_changed ()
284 {
285         AutoState state;
286
287         /* update button label */
288
289         if (_view) {
290                 state = _view->automation_state ();
291         } else if (_line) {
292                 assert (_control);
293                 state = _control->alist()->automation_state ();
294         } else {
295                 state = ARDOUR::Off;
296         }
297
298         switch (state & (ARDOUR::Off|Play|Touch|Write)) {
299         case ARDOUR::Off:
300                 auto_button.set_label (S_("Automation|Manual"));
301                 if (auto_off_item) {
302                         ignore_state_request = true;
303                         auto_off_item->set_active (true);
304                         auto_play_item->set_active (false);
305                         auto_touch_item->set_active (false);
306                         auto_write_item->set_active (false);
307                         ignore_state_request = false;
308                 }
309                 break;
310         case Play:
311                 auto_button.set_label (_("Play"));
312                 if (auto_play_item) {
313                         ignore_state_request = true;
314                         auto_play_item->set_active (true);
315                         auto_off_item->set_active (false);
316                         auto_touch_item->set_active (false);
317                         auto_write_item->set_active (false);
318                         ignore_state_request = false;
319                 }
320                 break;
321         case Write:
322                 auto_button.set_label (_("Write"));
323                 if (auto_write_item) {
324                         ignore_state_request = true;
325                         auto_write_item->set_active (true);
326                         auto_off_item->set_active (false);
327                         auto_play_item->set_active (false);
328                         auto_touch_item->set_active (false);
329                         ignore_state_request = false;
330                 }
331                 break;
332         case Touch:
333                 auto_button.set_label (_("Touch"));
334                 if (auto_touch_item) {
335                         ignore_state_request = true;
336                         auto_touch_item->set_active (true);
337                         auto_off_item->set_active (false);
338                         auto_play_item->set_active (false);
339                         auto_write_item->set_active (false);
340                         ignore_state_request = false;
341                 }
342                 break;
343         default:
344                 auto_button.set_label (_("???"));
345                 break;
346         }
347 }
348
349 /** The interpolation style of our AutomationList has changed, so update */
350 void
351 AutomationTimeAxisView::interpolation_changed (AutomationList::InterpolationStyle s)
352 {
353         if (mode_line_item && mode_discrete_item) {
354                 if (s == AutomationList::Discrete) {
355                         mode_discrete_item->set_active(true);
356                         mode_line_item->set_active(false);
357                 } else {
358                         mode_line_item->set_active(true);
359                         mode_discrete_item->set_active(false);
360                 }
361         }
362 }
363
364 /** A menu item has been selected to change our interpolation mode */
365 void
366 AutomationTimeAxisView::set_interpolation (AutomationList::InterpolationStyle style)
367 {
368         /* Tell our view's list, if we have one, otherwise tell our own.
369          * Everything else will be signalled back from that.
370          */
371
372         if (_view) {
373                 _view->set_interpolation (style);
374         } else {
375                 assert (_control);
376                 _control->list()->set_interpolation (style);
377         }
378 }
379
380 void
381 AutomationTimeAxisView::clear_clicked ()
382 {
383         assert (_line || _view);
384
385         _session->begin_reversible_command (_("clear automation"));
386
387         if (_line) {
388                 _line->clear ();
389         } else if (_view) {
390                 _view->clear ();
391         }
392
393         _session->commit_reversible_command ();
394         _session->set_dirty ();
395 }
396
397 void
398 AutomationTimeAxisView::set_height (uint32_t h)
399 {
400         bool const changed = (height != (uint32_t) h) || first_call_to_set_height;
401         uint32_t const normal = preset_height (HeightNormal);
402         bool const changed_between_small_and_normal = ( (height < normal && h >= normal) || (height >= normal || h < normal) );
403
404         TimeAxisView::set_height (h);
405
406         _base_rect->set_y1 (h);
407
408         if (_line) {
409                 _line->set_height(h);
410         }
411
412         if (_view) {
413                 _view->set_height(h);
414                 _view->update_contents_height();
415         }
416
417         if (changed_between_small_and_normal || first_call_to_set_height) {
418
419                 first_call_to_set_height = false;
420
421                 if (h >= preset_height (HeightNormal)) {
422                         auto_button.show();
423                         hide_button.show_all();
424
425                 } else if (h >= preset_height (HeightSmall)) {
426                         controls_table.hide_all ();
427                         auto_button.hide();
428                 }
429         }
430
431         if (changed) {
432                 if (_canvas_display->visible() && _route) {
433                         /* only emit the signal if the height really changed and we were visible */
434                         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
435                 }
436         }
437 }
438
439 void
440 AutomationTimeAxisView::set_samples_per_pixel (double fpp)
441 {
442         TimeAxisView::set_samples_per_pixel (fpp);
443
444         if (_line) {
445                 _line->reset ();
446         }
447
448         if (_view) {
449                 _view->set_samples_per_pixel (fpp);
450         }
451 }
452
453 void
454 AutomationTimeAxisView::hide_clicked ()
455 {
456         hide_button.set_sensitive(false);
457         set_marked_for_display (false);
458         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(parent);
459         if (rtv) {
460                 rtv->request_redraw ();
461         }
462         hide_button.set_sensitive(true);
463 }
464
465 void
466 AutomationTimeAxisView::build_display_menu ()
467 {
468         using namespace Menu_Helpers;
469
470         /* prepare it */
471
472         TimeAxisView::build_display_menu ();
473
474         /* now fill it with our stuff */
475
476         MenuList& items = display_menu->items();
477
478         items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked)));
479         items.push_back (SeparatorElem());
480         items.push_back (MenuElem (_("Clear"), sigc::mem_fun(*this, &AutomationTimeAxisView::clear_clicked)));
481         items.push_back (SeparatorElem());
482
483         /* state menu */
484
485         Menu* auto_state_menu = manage (new Menu);
486         auto_state_menu->set_name ("ArdourContextMenu");
487         MenuList& as_items = auto_state_menu->items();
488
489         as_items.push_back (CheckMenuElem (S_("Automation|Manual"), sigc::bind (
490                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
491                         (AutoState) ARDOUR::Off)));
492         auto_off_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
493
494         as_items.push_back (CheckMenuElem (_("Play"), sigc::bind (
495                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
496                         (AutoState) Play)));
497         auto_play_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
498
499         as_items.push_back (CheckMenuElem (_("Write"), sigc::bind (
500                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
501                         (AutoState) Write)));
502         auto_write_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
503
504         as_items.push_back (CheckMenuElem (_("Touch"), sigc::bind (
505                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
506                         (AutoState) Touch)));
507         auto_touch_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
508
509         items.push_back (MenuElem (_("State"), *auto_state_menu));
510
511         /* mode menu */
512
513         /* current interpolation state */
514         AutomationList::InterpolationStyle const s = _view ? _view->interpolation() : _control->list()->interpolation ();
515
516         if (EventTypeMap::instance().is_midi_parameter(_parameter)) {
517
518                 Menu* auto_mode_menu = manage (new Menu);
519                 auto_mode_menu->set_name ("ArdourContextMenu");
520                 MenuList& am_items = auto_mode_menu->items();
521
522                 RadioMenuItem::Group group;
523
524                 am_items.push_back (RadioMenuElem (group, _("Discrete"), sigc::bind (
525                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
526                                 AutomationList::Discrete)));
527                 mode_discrete_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
528                 mode_discrete_item->set_active (s == AutomationList::Discrete);
529
530                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
531                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
532                                 AutomationList::Linear)));
533                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
534                 mode_line_item->set_active (s == AutomationList::Linear);
535
536                 items.push_back (MenuElem (_("Mode"), *auto_mode_menu));
537         }
538
539         /* make sure the automation menu state is correct */
540
541         automation_state_changed ();
542         interpolation_changed (s);
543 }
544
545 void
546 AutomationTimeAxisView::add_automation_event (GdkEvent* event, framepos_t when, double y)
547 {
548         if (!_line) {
549                 return;
550         }
551
552         boost::shared_ptr<AutomationList> list = _line->the_list ();
553         
554         if (list->in_write_pass()) {
555                 /* do not allow the GUI to add automation events during an
556                    automation write pass.
557                 */
558                 return;
559         }
560
561         double x = 0;
562
563         _canvas_display->canvas_to_item (x, y);
564
565         /* compute vertical fractional position */
566
567         y = 1.0 - (y / height);
568
569         /* map using line */
570
571         _line->view_to_model_coord (x, y);
572
573
574         _editor.snap_to_with_modifier (when, event);
575
576         _session->begin_reversible_command (_("add automation event"));
577         XMLNode& before = list->get_state();
578
579         list->add (when, y);
580
581         XMLNode& after = list->get_state();
582         _session->commit_reversible_command (new MementoCommand<ARDOUR::AutomationList> (*list, &before, &after));
583         _session->set_dirty ();
584 }
585
586 /** Paste a selection.
587  *  @param pos Position to paste to (session frames).
588  *  @param times Number of times to paste.
589  *  @param selection Selection to paste.
590  *  @param nth Index of the AutomationList within the selection to paste from.
591  */
592 bool
593 AutomationTimeAxisView::paste (framepos_t pos, float times, Selection& selection, size_t nth)
594 {
595         boost::shared_ptr<AutomationLine> line;
596
597         if (_line) {
598                 line = _line;
599         } else if (_view) {
600                 line = _view->paste_line (pos);
601         }
602
603         if (!line) {
604                 return false;
605         }
606
607         return paste_one (*line, pos, times, selection, nth);
608 }
609
610 bool
611 AutomationTimeAxisView::paste_one (AutomationLine& line, framepos_t pos, float times, Selection& selection, size_t nth)
612 {
613         AutomationSelection::iterator p;
614         boost::shared_ptr<AutomationList> alist(line.the_list());
615
616         if (_session->transport_rolling() && alist->automation_write()) {
617                 /* do not paste if this control is in write mode and we're rolling */
618                 return false;
619         }
620
621         for (p = selection.lines.begin(); p != selection.lines.end() && nth; ++p, --nth) {}
622
623         if (p == selection.lines.end()) {
624                 return false;
625         }
626
627         double const model_pos = line.time_converter().from (pos - line.time_converter().origin_b ());
628
629         XMLNode &before = alist->get_state();
630         alist->paste (**p, model_pos, times);
631         _session->add_command (new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
632
633         return true;
634 }
635
636 void
637 AutomationTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results)
638 {
639         if (!_line && !_view) {
640                 return;
641         }
642
643         if (touched (top, bot)) {
644
645                 /* remember: this is X Window - coordinate space starts in upper left and moves down.
646                    _y_position is the "origin" or "top" of the track.
647                 */
648
649                 /* bottom of our track */
650                 double const mybot = _y_position + height;
651
652                 double topfrac;
653                 double botfrac;
654
655                 if (_y_position >= top && mybot <= bot) {
656
657                         /* _y_position is below top, mybot is above bot, so we're fully
658                            covered vertically.
659                         */
660
661                         topfrac = 1.0;
662                         botfrac = 0.0;
663
664                 } else {
665
666                         /* top and bot are within _y_position .. mybot */
667
668                         topfrac = 1.0 - ((top - _y_position) / height);
669                         botfrac = 1.0 - ((bot - _y_position) / height);
670
671                 }
672
673                 if (_line) {
674                         _line->get_selectables (start, end, botfrac, topfrac, results);
675                 } else if (_view) {
676                         _view->get_selectables (start, end, botfrac, topfrac, results);
677                 }
678         }
679 }
680
681 void
682 AutomationTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
683 {
684         if (_line) {
685                 _line->get_inverted_selectables (sel, result);
686         }
687 }
688
689 void
690 AutomationTimeAxisView::set_selected_points (PointSelection& points)
691 {
692         if (_line) {
693                 _line->set_selected_points (points);
694         } else if (_view) {
695                 _view->set_selected_points (points);
696         }
697 }
698
699 void
700 AutomationTimeAxisView::clear_lines ()
701 {
702         _line.reset();
703         _list_connections.drop_connections ();
704 }
705
706 void
707 AutomationTimeAxisView::add_line (boost::shared_ptr<AutomationLine> line)
708 {
709         if (_control && line) {
710                 assert(line->the_list() == _control->list());
711                 
712                 _control->alist()->automation_state_changed.connect (
713                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::automation_state_changed, this), gui_context()
714                         );
715                 
716                 _control->alist()->InterpolationChanged.connect (
717                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::interpolation_changed, this, _1), gui_context()
718                         );
719         }
720
721         _line = line;
722
723         line->set_height (height);
724
725         /* pick up the current state */
726         automation_state_changed ();
727
728         line->add_visibility (AutomationLine::Line);
729 }
730
731 void
732 AutomationTimeAxisView::entered()
733 {
734         if (_line) {
735                 _line->track_entered();
736         }
737 }
738
739 void
740 AutomationTimeAxisView::exited ()
741 {
742         if (_line) {
743                 _line->track_exited();
744         }
745 }
746
747 void
748 AutomationTimeAxisView::color_handler ()
749 {
750         if (_line) {
751                 _line->set_colors();
752         }
753 }
754
755 int
756 AutomationTimeAxisView::set_state_2X (const XMLNode& node, int /*version*/)
757 {
758         if (node.name() == X_("gain") && _parameter == Evoral::Parameter (GainAutomation)) {
759                 XMLProperty const * shown = node.property (X_("shown"));
760                 if (shown) {
761                         bool yn = string_is_affirmative (shown->value ());
762                         if (yn) {
763                                 _canvas_display->show (); /* FIXME: necessary? show_at? */
764                         }
765                         set_gui_property ("visible", yn);
766                 } else {
767                         set_gui_property ("visible", false);
768                 }
769         }
770
771         return 0;
772 }
773
774 int
775 AutomationTimeAxisView::set_state (const XMLNode&, int /*version*/)
776 {
777         return 0;
778 }
779
780 void
781 AutomationTimeAxisView::what_has_visible_automation (const boost::shared_ptr<Automatable>& automatable, set<Evoral::Parameter>& visible)
782 {
783         /* this keeps "knowledge" of how we store visibility information
784            in XML private to this class.
785         */
786
787         assert (automatable);
788
789         Automatable::Controls& controls (automatable->controls());
790         
791         for (Automatable::Controls::iterator i = controls.begin(); i != controls.end(); ++i) {
792                 
793                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
794
795                 if (ac) {
796                         
797                         const XMLNode* gui_node = ac->extra_xml ("GUI");
798                         
799                         if (gui_node) {
800                                 const XMLProperty* prop = gui_node->property ("shown");
801                                 if (prop) {
802                                         if (string_is_affirmative (prop->value())) {
803                                                 visible.insert (i->first);
804                                         }
805                                 }
806                         }
807                 }
808         }
809 }
810
811
812 /** @return true if this view has any automation data to display */
813 bool
814 AutomationTimeAxisView::has_automation () const
815 {
816         return ( (_line && _line->npoints() > 0) || (_view && _view->has_automation()) );
817 }
818
819 list<boost::shared_ptr<AutomationLine> >
820 AutomationTimeAxisView::lines () const
821 {
822         list<boost::shared_ptr<AutomationLine> > lines;
823
824         if (_line) {
825                 lines.push_back (_line);
826         } else if (_view) {
827                 lines = _view->get_lines ();
828         }
829
830         return lines;
831 }
832
833 string
834 AutomationTimeAxisView::state_id() const
835 {
836         if (_control) {
837                 return string_compose ("automation %1", _control->id().to_s());
838         } else {
839                 assert (_parameter);
840                 return string_compose ("automation %1 %2/%3/%4", 
841                                        _route->id(), 
842                                        _parameter.type(),
843                                        _parameter.id(),
844                                        (int) _parameter.channel());
845         }
846 }
847
848 /** Given a state id string, see if it is one generated by
849  *  this class.  If so, parse it into its components.
850  *  @param state_id State ID string to parse.
851  *  @param route_id Filled in with the route's ID if the state ID string is parsed.
852  *  @param has_parameter Filled in with true if the state ID has a parameter, otherwise false.
853  *  @param parameter Filled in with the state ID's parameter, if it has one.
854  *  @return true if this is a state ID generated by this class, otherwise false.
855  */
856
857 bool
858 AutomationTimeAxisView::parse_state_id (
859         string const & state_id,
860         PBD::ID & route_id,
861         bool & has_parameter,
862         Evoral::Parameter & parameter)
863 {
864         stringstream s;
865         s << state_id;
866
867         string a, b, c;
868         s >> a >> b >> c;
869
870         if (a != X_("automation")) {
871                 return false;
872         }
873
874         route_id = PBD::ID (b);
875
876         if (c.empty ()) {
877                 has_parameter = false;
878                 return true;
879         }
880
881         has_parameter = true;
882
883         vector<string> p;
884         boost::split (p, c, boost::is_any_of ("/"));
885
886         assert (p.size() == 3);
887
888         parameter = Evoral::Parameter (
889                 boost::lexical_cast<int> (p[0]),
890                 boost::lexical_cast<int> (p[2]),
891                 boost::lexical_cast<int> (p[1])
892                 );
893
894         return true;
895 }
896
897 void
898 AutomationTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
899 {
900         list<boost::shared_ptr<AutomationLine> > lines;
901         if (_line) {
902                 lines.push_back (_line);
903         } else if (_view) {
904                 lines = _view->get_lines ();
905         }
906
907         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
908                 cut_copy_clear_one (**i, selection, op);
909         }
910 }
911
912 void
913 AutomationTimeAxisView::cut_copy_clear_one (AutomationLine& line, Selection& selection, CutCopyOp op)
914 {
915         boost::shared_ptr<Evoral::ControlList> what_we_got;
916         boost::shared_ptr<AutomationList> alist (line.the_list());
917
918         XMLNode &before = alist->get_state();
919
920         /* convert time selection to automation list model coordinates */
921         const Evoral::TimeConverter<double, ARDOUR::framepos_t>& tc = line.time_converter ();
922         double const start = tc.from (selection.time.front().start - tc.origin_b ());
923         double const end = tc.from (selection.time.front().end - tc.origin_b ());
924
925         switch (op) {
926         case Delete:
927                 if (alist->cut (start, end) != 0) {
928                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
929                 }
930                 break;
931
932         case Cut:
933
934                 if ((what_we_got = alist->cut (start, end)) != 0) {
935                         _editor.get_cut_buffer().add (what_we_got);
936                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
937                 }
938                 break;
939         case Copy:
940                 if ((what_we_got = alist->copy (start, end)) != 0) {
941                         _editor.get_cut_buffer().add (what_we_got);
942                 }
943                 break;
944
945         case Clear:
946                 if ((what_we_got = alist->cut (start, end)) != 0) {
947                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
948                 }
949                 break;
950         }
951
952         if (what_we_got) {
953                 for (AutomationList::iterator x = what_we_got->begin(); x != what_we_got->end(); ++x) {
954                         double when = (*x)->when;
955                         double val  = (*x)->value;
956                         line.model_to_view_coord (when, val);
957                         (*x)->when = when;
958                         (*x)->value = val;
959                 }
960         }
961 }