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