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