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