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