OSC: Fix to make sure buses created with Ardour 4.7 sessions show up
[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
22 #include <boost/algorithm/string.hpp>
23
24 #include <gtkmm/separator.h>
25
26 #include "pbd/error.h"
27 #include "pbd/memento_command.h"
28 #include "pbd/stacktrace.h"
29 #include "pbd/string_convert.h"
30 #include "pbd/types_convert.h"
31 #include "pbd/unwind.h"
32
33 #include "ardour/automation_control.h"
34 #include "ardour/beats_frames_converter.h"
35 #include "ardour/event_type_map.h"
36 #include "ardour/parameter_types.h"
37 #include "ardour/profile.h"
38 #include "ardour/route.h"
39 #include "ardour/session.h"
40
41 #include "gtkmm2ext/utils.h"
42
43 #include "canvas/debug.h"
44
45 #include "widgets/tooltips.h"
46
47 #include "automation_time_axis.h"
48 #include "automation_streamview.h"
49 #include "gui_thread.h"
50 #include "route_time_axis.h"
51 #include "automation_line.h"
52 #include "paste_context.h"
53 #include "public_editor.h"
54 #include "selection.h"
55 #include "rgb_macros.h"
56 #include "point_selection.h"
57 #include "control_point.h"
58 #include "utils.h"
59 #include "item_counts.h"
60 #include "ui_config.h"
61
62 #include "pbd/i18n.h"
63
64 using namespace std;
65 using namespace ARDOUR;
66 using namespace ArdourWidgets;
67 using namespace ARDOUR_UI_UTILS;
68 using namespace PBD;
69 using namespace Gtk;
70 using namespace Gtkmm2ext;
71 using namespace Editing;
72
73 Pango::FontDescription AutomationTimeAxisView::name_font;
74 bool AutomationTimeAxisView::have_name_font = false;
75
76
77 /** \a a the automatable object this time axis is to display data for.
78  * For route/track automation (e.g. gain) pass the route for both \r and \a.
79  * For route child (e.g. plugin) automation, pass the child for \a.
80  * For region automation (e.g. MIDI CC), pass null for \a.
81  */
82 AutomationTimeAxisView::AutomationTimeAxisView (
83         Session* s,
84         boost::shared_ptr<Stripable> strip,
85         boost::shared_ptr<Automatable> a,
86         boost::shared_ptr<AutomationControl> c,
87         Evoral::Parameter p,
88         PublicEditor& e,
89         TimeAxisView& parent,
90         bool show_regions,
91         ArdourCanvas::Canvas& canvas,
92         const string & nom,
93         const string & nomparent
94         )
95         : SessionHandlePtr (s)
96         , TimeAxisView (s, e, &parent, canvas)
97         , _stripable (strip)
98         , _control (c)
99         , _automatable (a)
100         , _parameter (p)
101         , _base_rect (new ArdourCanvas::Rectangle (_canvas_display))
102         , _view (show_regions ? new AutomationStreamView (*this) : 0)
103         , auto_dropdown ()
104         , _show_regions (show_regions)
105 {
106         //concatenate plugin name and param name into the tooltip
107         string tipname = nomparent;
108         if (!tipname.empty()) {
109                 tipname += ": ";
110         }
111         tipname += nom;
112         set_tooltip(controls_ebox, tipname);
113
114         //plugin name and param name appear on 2 separate lines in the track header
115         tipname = nomparent;
116         if (!tipname.empty()) {
117                 tipname += "\n";
118         }
119         tipname += nom;
120         _name = tipname;
121
122         CANVAS_DEBUG_NAME (_canvas_display, string_compose ("main for auto %2/%1", _name, strip->name()));
123         CANVAS_DEBUG_NAME (selection_group, string_compose ("selections for auto %2/%1", _name, strip->name()));
124         CANVAS_DEBUG_NAME (_ghost_group, string_compose ("ghosts for auto %2/%1", _name, strip->name()));
125
126         if (!have_name_font) {
127                 name_font = get_font_for_style (X_("AutomationTrackName"));
128                 have_name_font = true;
129         }
130
131         if (_control) {
132                 _controller = AutomationController::create (_control->parameter(), _control->desc(), _control);
133         }
134
135         const std::string fill_color_name = (dynamic_cast<MidiTimeAxisView*>(&parent)
136                                              ? "midi automation track fill"
137                                              : "audio automation track fill");
138
139         auto_off_item = 0;
140         auto_touch_item = 0;
141         auto_write_item = 0;
142         auto_play_item = 0;
143         mode_discrete_item = 0;
144         mode_line_item = 0;
145         mode_log_item = 0;
146         mode_exp_item = 0;
147
148         ignore_state_request = false;
149         ignore_mode_request = false;
150         first_call_to_set_height = true;
151
152         CANVAS_DEBUG_NAME (_base_rect, string_compose ("base rect for %1", _name));
153         _base_rect->set_x1 (ArdourCanvas::COORD_MAX);
154         _base_rect->set_outline (false);
155         _base_rect->set_fill_color (UIConfiguration::instance().color_mod (fill_color_name, "automation track fill"));
156         _base_rect->set_data ("trackview", this);
157         _base_rect->Event.connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_automation_track_event), _base_rect, this));
158         if (!a) {
159                 _base_rect->lower_to_bottom();
160         }
161
162         using namespace Menu_Helpers;
163
164         auto_dropdown.AddMenuElem (MenuElem (S_("Automation|Manual"), sigc::bind (sigc::mem_fun(*this,
165                                                 &AutomationTimeAxisView::set_automation_state), (AutoState) ARDOUR::Off)));
166         auto_dropdown.AddMenuElem (MenuElem (_("Play"), sigc::bind (sigc::mem_fun(*this,
167                                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Play)));
168         auto_dropdown.AddMenuElem (MenuElem (_("Write"), sigc::bind (sigc::mem_fun(*this,
169                                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Write)));
170         auto_dropdown.AddMenuElem (MenuElem (_("Touch"), sigc::bind (sigc::mem_fun(*this,
171                                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Touch)));
172
173         /* XXX translators: use a string here that will be at least as long
174            as the longest automation label (see ::automation_state_changed()
175            below). be sure to include a descender.
176         */
177         auto_dropdown.set_sizing_text(_("Mgnual"));
178
179         hide_button.set_icon (ArdourIcon::CloseCross);
180         hide_button.set_tweaks(ArdourButton::TrackHeader);
181
182         auto_dropdown.set_name ("route button");
183         hide_button.set_name ("route button");
184
185         auto_dropdown.unset_flags (Gtk::CAN_FOCUS);
186         hide_button.unset_flags (Gtk::CAN_FOCUS);
187
188         controls_table.set_no_show_all();
189
190         set_tooltip(auto_dropdown, _("automation state"));
191         set_tooltip(hide_button, _("hide track"));
192
193         uint32_t height;
194         if (get_gui_property ("height", height)) {
195                 set_height (height);
196         } else {
197                 set_height (preset_height (HeightNormal));
198         }
199
200         //name label isn't editable on an automation track; remove the tooltip
201         set_tooltip (name_label, X_(""));
202
203         /* repack the name label, which TimeAxisView has already attached to
204          * the controls_table
205          */
206
207         if (name_label.get_parent()) {
208                 name_label.get_parent()->remove (name_label);
209         }
210
211         name_label.set_text (_name);
212         name_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
213         name_label.set_name (X_("TrackParameterName"));
214         name_label.set_ellipsize (Pango::ELLIPSIZE_END);
215
216         /* add the buttons */
217         controls_table.set_border_width (1);
218         controls_table.attach (hide_button, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
219         controls_table.attach (name_label,  2, 3, 1, 3, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 2, 0);
220         controls_table.attach (auto_dropdown, 3, 4, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
221
222         Gtk::DrawingArea *blank0 = manage (new Gtk::DrawingArea());
223         Gtk::DrawingArea *blank1 = manage (new Gtk::DrawingArea());
224
225         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&parent);
226         // TODO use rtv->controls_base_unselected_name
227         // subscribe to route_active_changed, ...
228         if (rtv && rtv->is_audio_track()) {
229                 blank0->set_name ("AudioTrackControlsBaseUnselected");
230         } else if (rtv && rtv->is_midi_track()) {
231                 blank0->set_name ("MidiTrackControlsBaseUnselected");
232         } else if (rtv) {
233                 blank0->set_name ("AudioBusControlsBaseUnselected");
234         } else {
235                 blank0->set_name ("UnknownControlsBaseUnselected");
236         }
237         blank0->set_size_request (-1, -1);
238         blank1->set_size_request (1, 0);
239         VSeparator* separator = manage (new VSeparator());
240         separator->set_name("TrackSeparator");
241         separator->set_size_request (1, -1);
242
243         controls_button_size_group->add_widget(hide_button);
244         controls_button_size_group->add_widget(*blank0);
245
246         time_axis_hbox.pack_start (*blank0, false, false);
247         time_axis_hbox.pack_start (*separator, false, false);
248         time_axis_hbox.reorder_child (*blank0, 0);
249         time_axis_hbox.reorder_child (*separator, 1);
250         time_axis_hbox.reorder_child (time_axis_vbox, 2);
251
252         if (!ARDOUR::Profile->get_mixbus() ) {
253                 time_axis_hbox.pack_start (*blank1, false, false);
254         }
255
256         blank0->show();
257         separator->show();
258         name_label.show ();
259         hide_button.show ();
260
261         if (_controller) {
262                 _controller->disable_vertical_scroll ();
263                 controls_table.attach (*_controller.get(), 2, 4, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
264         }
265
266         controls_table.show_all ();
267
268         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked));
269
270         controls_base_selected_name = X_("AutomationTrackControlsBaseSelected");
271         controls_base_unselected_name = X_("AutomationTrackControlsBase");
272
273         controls_ebox.set_name (controls_base_unselected_name);
274         time_axis_frame.set_name (controls_base_unselected_name);
275
276         /* ask for notifications of any new RegionViews */
277         if (show_regions) {
278
279                 if (_view) {
280                         _view->attach ();
281                 }
282
283         } else {
284                 /* no regions, just a single line for the entire track (e.g. bus gain) */
285
286                 assert (_control);
287
288                 boost::shared_ptr<AutomationLine> line (
289                         new AutomationLine (
290                                 ARDOUR::EventTypeMap::instance().to_symbol(_parameter),
291                                 *this,
292                                 *_canvas_display,
293                                 _control->alist(),
294                                 _control->desc()
295                                 )
296                         );
297
298                 line->set_line_color (UIConfiguration::instance().color ("processor automation line"));
299                 line->set_fill (true);
300                 line->queue_reset ();
301                 add_line (line);
302         }
303
304         /* make sure labels etc. are correct */
305
306         automation_state_changed ();
307         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AutomationTimeAxisView::color_handler));
308
309         _stripable->DropReferences.connect (
310                 _stripable_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::route_going_away, this), gui_context ()
311                 );
312 }
313
314 AutomationTimeAxisView::~AutomationTimeAxisView ()
315 {
316         if (_stripable) {
317                 cleanup_gui_properties ();
318         }
319         delete _view;
320 }
321
322 void
323 AutomationTimeAxisView::route_going_away ()
324 {
325         cleanup_gui_properties ();
326         _stripable.reset ();
327 }
328
329 void
330 AutomationTimeAxisView::set_automation_state (AutoState state)
331 {
332         if (ignore_state_request) {
333                 return;
334         }
335
336         if (_automatable) {
337                 _automatable->set_parameter_automation_state (_parameter, state);
338         }
339         else if (_control) {
340                 _control->set_automation_state (state);
341                 _session->set_dirty ();
342         }
343
344         if (_view) {
345                 _view->set_automation_state (state);
346
347                 /* AutomationStreamViews don't signal when their automation state changes, so handle
348                    our updates `manually'.
349                 */
350                 automation_state_changed ();
351         }
352 }
353
354 void
355 AutomationTimeAxisView::automation_state_changed ()
356 {
357         AutoState state;
358
359         /* update button label */
360
361         if (_view) {
362                 state = _view->automation_state ();
363         } else if (_line) {
364                 assert (_control);
365                 state = _control->alist()->automation_state ();
366         } else {
367                 state = ARDOUR::Off;
368         }
369
370         switch (state & (ARDOUR::Off|Play|Touch|Write)) {
371         case ARDOUR::Off:
372                 auto_dropdown.set_text (S_("Automation|Manual"));
373                 if (auto_off_item) {
374                         ignore_state_request = true;
375                         auto_off_item->set_active (true);
376                         auto_play_item->set_active (false);
377                         auto_touch_item->set_active (false);
378                         auto_write_item->set_active (false);
379                         ignore_state_request = false;
380                 }
381                 break;
382         case Play:
383                 auto_dropdown.set_text (_("Play"));
384                 if (auto_play_item) {
385                         ignore_state_request = true;
386                         auto_play_item->set_active (true);
387                         auto_off_item->set_active (false);
388                         auto_touch_item->set_active (false);
389                         auto_write_item->set_active (false);
390                         ignore_state_request = false;
391                 }
392                 break;
393         case Write:
394                 auto_dropdown.set_text (_("Write"));
395                 if (auto_write_item) {
396                         ignore_state_request = true;
397                         auto_write_item->set_active (true);
398                         auto_off_item->set_active (false);
399                         auto_play_item->set_active (false);
400                         auto_touch_item->set_active (false);
401                         ignore_state_request = false;
402                 }
403                 break;
404         case Touch:
405                 auto_dropdown.set_text (_("Touch"));
406                 if (auto_touch_item) {
407                         ignore_state_request = true;
408                         auto_touch_item->set_active (true);
409                         auto_off_item->set_active (false);
410                         auto_play_item->set_active (false);
411                         auto_write_item->set_active (false);
412                         ignore_state_request = false;
413                 }
414                 break;
415         default:
416                 auto_dropdown.set_text (_("???"));
417                 break;
418         }
419 }
420
421 /** The interpolation style of our AutomationList has changed, so update */
422 void
423 AutomationTimeAxisView::interpolation_changed (AutomationList::InterpolationStyle s)
424 {
425         if (ignore_mode_request) {
426                 return;
427         }
428         PBD::Unwinder<bool> uw (ignore_mode_request, true);
429         switch (s) {
430                 case AutomationList::Discrete:
431                         if (mode_discrete_item) {
432                                 mode_discrete_item->set_active(true);
433                         }
434                         break;
435                 case AutomationList::Linear:
436                         if (mode_line_item) {
437                                 mode_line_item->set_active(true);
438                         }
439                         break;
440                 case AutomationList::Logarithmic:
441                         if (mode_log_item) {
442                                 mode_log_item->set_active(true);
443                         }
444                         break;
445                 case AutomationList::Exponential:
446                         if (mode_exp_item) {
447                                 mode_exp_item->set_active(true);
448                         }
449                         break;
450                 default:
451                         break;
452         }
453 }
454
455 /** A menu item has been selected to change our interpolation mode */
456 void
457 AutomationTimeAxisView::set_interpolation (AutomationList::InterpolationStyle style)
458 {
459         /* Tell our view's list, if we have one, otherwise tell our own.
460          * Everything else will be signalled back from that.
461          */
462
463         if (_view) {
464                 _view->set_interpolation (style);
465         } else {
466                 assert (_control);
467                 _control->list()->set_interpolation (style);
468         }
469 }
470
471 void
472 AutomationTimeAxisView::clear_clicked ()
473 {
474         assert (_line || _view);
475
476         _editor.begin_reversible_command (_("clear automation"));
477
478         if (_line) {
479                 _line->clear ();
480         } else if (_view) {
481                 _view->clear ();
482         }
483         set_automation_state ((AutoState) ARDOUR::Off);
484         _editor.commit_reversible_command ();
485         _session->set_dirty ();
486 }
487
488 void
489 AutomationTimeAxisView::set_height (uint32_t h, TrackHeightMode m)
490 {
491         bool const changed = (height != (uint32_t) h) || first_call_to_set_height;
492         uint32_t const normal = preset_height (HeightNormal);
493         bool const changed_between_small_and_normal = ( (height < normal && h >= normal) || (height >= normal || h < normal) );
494
495         TimeAxisView::set_height (h, m);
496
497         _base_rect->set_y1 (h);
498
499         if (_line) {
500                 _line->set_height(h - 2.5);
501         }
502
503         if (_view) {
504                 _view->set_height(h);
505                 _view->update_contents_height();
506         }
507
508         if (changed_between_small_and_normal || first_call_to_set_height) {
509
510                 first_call_to_set_height = false;
511
512                 if (h >= preset_height (HeightNormal)) {
513                         if (!(_parameter.type() >= MidiCCAutomation &&
514                               _parameter.type() <= MidiChannelPressureAutomation)) {
515                                 auto_dropdown.show();
516                         }
517                         name_label.show();
518                         hide_button.show();
519
520                 } else if (h >= preset_height (HeightSmall)) {
521                         controls_table.hide_all ();
522                         auto_dropdown.hide();
523                         name_label.hide();
524                 }
525         }
526
527         if (changed) {
528                 if (_canvas_display->visible() && _stripable) {
529                         /* only emit the signal if the height really changed and we were visible */
530                         _stripable->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
531                 }
532         }
533 }
534
535 void
536 AutomationTimeAxisView::set_samples_per_pixel (double fpp)
537 {
538         TimeAxisView::set_samples_per_pixel (fpp);
539
540         if (_line) {
541                 _line->reset ();
542         }
543
544         if (_view) {
545                 _view->set_samples_per_pixel (fpp);
546         }
547 }
548
549 void
550 AutomationTimeAxisView::hide_clicked ()
551 {
552         hide_button.set_sensitive(false);
553         set_marked_for_display (false);
554         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*>(parent);
555         if (stv) {
556                 stv->request_redraw ();
557         }
558         hide_button.set_sensitive(true);
559 }
560
561 void
562 AutomationTimeAxisView::build_display_menu ()
563 {
564         using namespace Menu_Helpers;
565
566         /* prepare it */
567
568         TimeAxisView::build_display_menu ();
569
570         /* now fill it with our stuff */
571
572         MenuList& items = display_menu->items();
573
574         items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked)));
575         items.push_back (SeparatorElem());
576         items.push_back (MenuElem (_("Clear"), sigc::mem_fun(*this, &AutomationTimeAxisView::clear_clicked)));
577         items.push_back (SeparatorElem());
578
579         /* state menu */
580
581         Menu* auto_state_menu = manage (new Menu);
582         auto_state_menu->set_name ("ArdourContextMenu");
583         MenuList& as_items = auto_state_menu->items();
584
585         as_items.push_back (CheckMenuElem (S_("Automation|Manual"), sigc::bind (
586                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
587                         (AutoState) ARDOUR::Off)));
588         auto_off_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
589
590         as_items.push_back (CheckMenuElem (_("Play"), sigc::bind (
591                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
592                         (AutoState) Play)));
593         auto_play_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
594
595         as_items.push_back (CheckMenuElem (_("Write"), sigc::bind (
596                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
597                         (AutoState) Write)));
598         auto_write_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
599
600         as_items.push_back (CheckMenuElem (_("Touch"), sigc::bind (
601                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
602                         (AutoState) Touch)));
603         auto_touch_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
604
605         if (!(_parameter.type() >= MidiCCAutomation &&
606               _parameter.type() <= MidiChannelPressureAutomation)) {
607                 items.push_back (MenuElem (_("State"), *auto_state_menu));
608         }
609
610         /* mode menu */
611
612         /* current interpolation state */
613         AutomationList::InterpolationStyle const s = _view ? _view->interpolation() : _control->list()->interpolation ();
614
615         if (ARDOUR::parameter_is_midi((AutomationType)_parameter.type())) {
616
617                 Menu* auto_mode_menu = manage (new Menu);
618                 auto_mode_menu->set_name ("ArdourContextMenu");
619                 MenuList& am_items = auto_mode_menu->items();
620
621                 RadioMenuItem::Group group;
622
623                 am_items.push_back (RadioMenuElem (group, _("Discrete"), sigc::bind (
624                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
625                                 AutomationList::Discrete)));
626                 mode_discrete_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
627
628                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
629                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
630                                 AutomationList::Linear)));
631                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
632
633                 items.push_back (MenuElem (_("Mode"), *auto_mode_menu));
634
635         } else {
636 #ifdef XXX_NEW_INTERPOLATON__BREAK_SESSION_FORMAT_XXX
637                 Menu* auto_mode_menu = manage (new Menu);
638                 auto_mode_menu->set_name ("ArdourContextMenu");
639                 MenuList& am_items = auto_mode_menu->items();
640                 bool have_options = false;
641
642                 RadioMenuItem::Group group;
643
644                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
645                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
646                                 AutomationList::Linear)));
647                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
648
649                 if (_control->desc().logarithmic) {
650                         am_items.push_back (RadioMenuElem (group, _("Logarithmic"), sigc::bind (
651                                                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
652                                                         AutomationList::Logarithmic)));
653                         mode_log_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
654                         have_options = true;
655                 } else {
656                         mode_log_item = 0;
657                 }
658
659                 if (_line->get_uses_gain_mapping () && !_control->desc().logarithmic) {
660                         am_items.push_back (RadioMenuElem (group, _("Exponential"), sigc::bind (
661                                                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
662                                                         AutomationList::Exponential)));
663                         mode_exp_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
664                         have_options = true;
665                 } else {
666                         mode_exp_item = 0;
667                 }
668
669                 if (have_options) {
670                         items.push_back (MenuElem (_("Interpolation"), *auto_mode_menu));
671                 } else {
672                         mode_line_item = 0;
673                         delete auto_mode_menu;
674                         auto_mode_menu = 0;
675                 }
676 #endif
677         }
678
679         /* make sure the automation menu state is correct */
680
681         automation_state_changed ();
682         interpolation_changed (s);
683 }
684
685 void
686 AutomationTimeAxisView::add_automation_event (GdkEvent* event, framepos_t frame, double y, bool with_guard_points)
687 {
688         if (!_line) {
689                 return;
690         }
691
692         boost::shared_ptr<AutomationList> list = _line->the_list ();
693
694         if (list->in_write_pass()) {
695                 /* do not allow the GUI to add automation events during an
696                    automation write pass.
697                 */
698                 return;
699         }
700
701         MusicFrame when (frame, 0);
702         _editor.snap_to_with_modifier (when, event);
703
704         if (UIConfiguration::instance().get_new_automation_points_on_lane()) {
705                 if (_control->list()->size () == 0) {
706                         y = _control->get_value ();
707                 } else {
708                         y = _control->list()->eval (when.frame);
709                 }
710         } else {
711                 double x = 0;
712                 _line->grab_item().canvas_to_item (x, y);
713                 /* compute vertical fractional position */
714                 y = 1.0 - (y / _line->height());
715                 /* map using line */
716                 _line->view_to_model_coord (x, y);
717         }
718
719         XMLNode& before = list->get_state();
720         std::list<Selectable*> results;
721
722         if (list->editor_add (when.frame, y, with_guard_points)) {
723                 XMLNode& after = list->get_state();
724                 _editor.begin_reversible_command (_("add automation event"));
725                 _session->add_command (new MementoCommand<ARDOUR::AutomationList> (*list.get (), &before, &after));
726
727                 _line->get_selectables (when.frame, when.frame, 0.0, 1.0, results);
728                 _editor.get_selection ().set (results);
729
730                 _editor.commit_reversible_command ();
731                 _session->set_dirty ();
732         }
733 }
734
735 bool
736 AutomationTimeAxisView::paste (framepos_t pos, const Selection& selection, PasteContext& ctx, const int32_t divisions)
737 {
738         if (_line) {
739                 return paste_one (pos, ctx.count, ctx.times, selection, ctx.counts, ctx.greedy);
740         } else if (_view) {
741                 AutomationSelection::const_iterator l = selection.lines.get_nth(_parameter, ctx.counts.n_lines(_parameter));
742                 if (l == selection.lines.end()) {
743                         if (ctx.greedy && selection.lines.size() == 1) {
744                                 l = selection.lines.begin();
745                         }
746                 }
747                 if (l != selection.lines.end() && _view->paste (pos, ctx.count, ctx.times, *l)) {
748                         ctx.counts.increase_n_lines(_parameter);
749                         return true;
750                 }
751         }
752
753         return false;
754 }
755
756 bool
757 AutomationTimeAxisView::paste_one (framepos_t pos, unsigned paste_count, float times, const Selection& selection, ItemCounts& counts, bool greedy)
758 {
759         boost::shared_ptr<AutomationList> alist(_line->the_list());
760
761         if (_session->transport_rolling() && alist->automation_write()) {
762                 /* do not paste if this control is in write mode and we're rolling */
763                 return false;
764         }
765
766         /* Get appropriate list from selection. */
767         AutomationSelection::const_iterator p = selection.lines.get_nth(_parameter, counts.n_lines(_parameter));
768         if (p == selection.lines.end()) {
769                 if (greedy && selection.lines.size() == 1) {
770                         p = selection.lines.begin();
771                 } else {
772                         return false;
773                 }
774         }
775         counts.increase_n_lines(_parameter);
776
777         /* add multi-paste offset if applicable */
778
779         AutomationType src_type = (AutomationType)(*p)->parameter().type ();
780         double len = (*p)->length();
781
782         if (parameter_is_midi (src_type)) {
783                 // convert length to samples (incl tempo-ramps)
784                 len = DoubleBeatsFramesConverter (_session->tempo_map(), pos).to (len * paste_count);
785                 pos += _editor.get_paste_offset (pos, paste_count > 0 ? 1 : 0, len);
786         } else {
787                 pos += _editor.get_paste_offset (pos, paste_count, len);
788         }
789
790         /* convert sample-position to model's unit and position */
791         double const model_pos = _line->time_converter().from (pos - _line->time_converter().origin_b ());
792
793         XMLNode &before = alist->get_state();
794         alist->paste (**p, model_pos, DoubleBeatsFramesConverter (_session->tempo_map(), pos));
795         _session->add_command (new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
796
797         return true;
798 }
799
800 void
801 AutomationTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results, bool /*within*/)
802 {
803         if (!_line && !_view) {
804                 return;
805         }
806
807         if (touched (top, bot)) {
808
809                 /* remember: this is X Window - coordinate space starts in upper left and moves down.
810                    _y_position is the "origin" or "top" of the track.
811                 */
812
813                 /* bottom of our track */
814                 double const mybot = _y_position + height;
815
816                 double topfrac;
817                 double botfrac;
818
819                 if (_y_position >= top && mybot <= bot) {
820
821                         /* _y_position is below top, mybot is above bot, so we're fully
822                            covered vertically.
823                         */
824
825                         topfrac = 1.0;
826                         botfrac = 0.0;
827
828                 } else {
829
830                         /* top and bot are within _y_position .. mybot */
831
832                         topfrac = 1.0 - ((top - _y_position) / height);
833                         botfrac = 1.0 - ((bot - _y_position) / height);
834
835                 }
836
837                 if (_line) {
838                         _line->get_selectables (start, end, botfrac, topfrac, results);
839                 } else if (_view) {
840                         _view->get_selectables (start, end, botfrac, topfrac, results);
841                 }
842         }
843 }
844
845 void
846 AutomationTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
847 {
848         if (_line) {
849                 _line->get_inverted_selectables (sel, result);
850         }
851 }
852
853 void
854 AutomationTimeAxisView::set_selected_points (PointSelection& points)
855 {
856         if (_line) {
857                 _line->set_selected_points (points);
858         } else if (_view) {
859                 _view->set_selected_points (points);
860         }
861 }
862
863 void
864 AutomationTimeAxisView::clear_lines ()
865 {
866         _line.reset();
867         _list_connections.drop_connections ();
868 }
869
870 void
871 AutomationTimeAxisView::add_line (boost::shared_ptr<AutomationLine> line)
872 {
873         if (_control && line) {
874                 assert(line->the_list() == _control->list());
875
876                 _control->alist()->automation_state_changed.connect (
877                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::automation_state_changed, this), gui_context()
878                         );
879
880                 _control->alist()->InterpolationChanged.connect (
881                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::interpolation_changed, this, _1), gui_context()
882                         );
883         }
884
885         _line = line;
886
887         line->set_height (height - 2.5);
888
889         /* pick up the current state */
890         automation_state_changed ();
891
892         line->add_visibility (AutomationLine::Line);
893 }
894
895 bool
896 AutomationTimeAxisView::propagate_time_selection () const
897 {
898         /* MIDI automation is part of the MIDI region. It is always
899          * implicily selected with the parent, regardless of TAV selection
900          */
901         if (_parameter.type() >= MidiCCAutomation &&
902             _parameter.type() <= MidiChannelPressureAutomation) {
903                 return true;
904         }
905         return false;
906 }
907
908 void
909 AutomationTimeAxisView::entered()
910 {
911         if (_line) {
912                 _line->track_entered();
913         }
914 }
915
916 void
917 AutomationTimeAxisView::exited ()
918 {
919         if (_line) {
920                 _line->track_exited();
921         }
922 }
923
924 void
925 AutomationTimeAxisView::color_handler ()
926 {
927         if (_line) {
928                 _line->set_colors();
929         }
930 }
931
932 int
933 AutomationTimeAxisView::set_state_2X (const XMLNode& node, int /*version*/)
934 {
935         if (node.name() == X_("gain") && _parameter == Evoral::Parameter (GainAutomation)) {
936
937                 bool shown;
938                 if (node.get_property (X_("shown"), shown)) {
939                         if (shown) {
940                                 _canvas_display->show (); /* FIXME: necessary? show_at? */
941                                 set_gui_property ("visible", shown);
942                         }
943                 } else {
944                         set_gui_property ("visible", false);
945                 }
946         }
947
948         return 0;
949 }
950
951 int
952 AutomationTimeAxisView::set_state (const XMLNode&, int /*version*/)
953 {
954         return 0;
955 }
956
957 void
958 AutomationTimeAxisView::what_has_visible_automation (const boost::shared_ptr<Automatable>& automatable, set<Evoral::Parameter>& visible)
959 {
960         /* this keeps "knowledge" of how we store visibility information
961            in XML private to this class.
962         */
963
964         assert (automatable);
965
966         Automatable::Controls& controls (automatable->controls());
967
968         for (Automatable::Controls::iterator i = controls.begin(); i != controls.end(); ++i) {
969
970                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
971
972                 if (ac && ac->alist()) {
973
974                         const XMLNode* gui_node = ac->extra_xml ("GUI");
975
976                         if (gui_node) {
977                                 bool shown;
978                                 if (gui_node->get_property ("shown", shown) && shown) {
979                                         visible.insert (i->first);
980                                 }
981                         }
982                 }
983         }
984 }
985
986
987 /** @return true if this view has any automation data to display */
988 bool
989 AutomationTimeAxisView::has_automation () const
990 {
991         return ( (_line && _line->npoints() > 0) || (_view && _view->has_automation()) );
992 }
993
994 list<boost::shared_ptr<AutomationLine> >
995 AutomationTimeAxisView::lines () const
996 {
997         list<boost::shared_ptr<AutomationLine> > lines;
998
999         if (_line) {
1000                 lines.push_back (_line);
1001         } else if (_view) {
1002                 lines = _view->get_lines ();
1003         }
1004
1005         return lines;
1006 }
1007
1008 string
1009 AutomationTimeAxisView::state_id() const
1010 {
1011         if (_parameter && _stripable && _automatable == _stripable) {
1012                 const string parameter_str = PBD::to_string (_parameter.type()) + "/" +
1013                                              PBD::to_string (_parameter.id()) + "/" +
1014                                              PBD::to_string (_parameter.channel ());
1015
1016                 return string("automation ") + PBD::to_string(_stripable->id()) + " " + parameter_str;
1017         } else if (_automatable != _stripable && _control) {
1018                 return string("automation ") + _control->id().to_s();
1019         } else {
1020                 error << "Automation time axis has no state ID" << endmsg;
1021                 return "";
1022         }
1023 }
1024
1025 /** Given a state id string, see if it is one generated by
1026  *  this class.  If so, parse it into its components.
1027  *  @param state_id State ID string to parse.
1028  *  @param route_id Filled in with the route's ID if the state ID string is parsed.
1029  *  @param has_parameter Filled in with true if the state ID has a parameter, otherwise false.
1030  *  @param parameter Filled in with the state ID's parameter, if it has one.
1031  *  @return true if this is a state ID generated by this class, otherwise false.
1032  */
1033
1034 bool
1035 AutomationTimeAxisView::parse_state_id (
1036         string const & state_id,
1037         PBD::ID & route_id,
1038         bool & has_parameter,
1039         Evoral::Parameter & parameter)
1040 {
1041         stringstream ss;
1042         ss << state_id;
1043
1044         string a, b, c;
1045         ss >> a >> b >> c;
1046
1047         if (a != X_("automation")) {
1048                 return false;
1049         }
1050
1051         route_id = PBD::ID (b);
1052
1053         if (c.empty ()) {
1054                 has_parameter = false;
1055                 return true;
1056         }
1057
1058         has_parameter = true;
1059
1060         vector<string> p;
1061         boost::split (p, c, boost::is_any_of ("/"));
1062
1063         assert (p.size() == 3);
1064
1065         parameter = Evoral::Parameter (
1066                 PBD::string_to<uint32_t>(p[0]), // type
1067                 PBD::string_to<uint8_t>(p[2]), // channel
1068                 PBD::string_to<uint32_t>(p[1]) // id
1069                 );
1070
1071         return true;
1072 }
1073
1074 void
1075 AutomationTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1076 {
1077         list<boost::shared_ptr<AutomationLine> > lines;
1078         if (_line) {
1079                 lines.push_back (_line);
1080         } else if (_view) {
1081                 lines = _view->get_lines ();
1082         }
1083
1084         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
1085                 cut_copy_clear_one (**i, selection, op);
1086         }
1087 }
1088
1089 void
1090 AutomationTimeAxisView::cut_copy_clear_one (AutomationLine& line, Selection& selection, CutCopyOp op)
1091 {
1092         boost::shared_ptr<Evoral::ControlList> what_we_got;
1093         boost::shared_ptr<AutomationList> alist (line.the_list());
1094
1095         XMLNode &before = alist->get_state();
1096
1097         /* convert time selection to automation list model coordinates */
1098         const Evoral::TimeConverter<double, ARDOUR::framepos_t>& tc = line.time_converter ();
1099         double const start = tc.from (selection.time.front().start - tc.origin_b ());
1100         double const end = tc.from (selection.time.front().end - tc.origin_b ());
1101
1102         switch (op) {
1103         case Delete:
1104                 if (alist->cut (start, end) != 0) {
1105                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1106                 }
1107                 break;
1108
1109         case Cut:
1110
1111                 if ((what_we_got = alist->cut (start, end)) != 0) {
1112                         _editor.get_cut_buffer().add (what_we_got);
1113                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1114                 }
1115                 break;
1116         case Copy:
1117                 if ((what_we_got = alist->copy (start, end)) != 0) {
1118                         _editor.get_cut_buffer().add (what_we_got);
1119                 }
1120                 break;
1121
1122         case Clear:
1123                 if ((what_we_got = alist->cut (start, end)) != 0) {
1124                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1125                 }
1126                 break;
1127         }
1128
1129         if (what_we_got) {
1130                 for (AutomationList::iterator x = what_we_got->begin(); x != what_we_got->end(); ++x) {
1131                         double when = (*x)->when;
1132                         double val  = (*x)->value;
1133                         line.model_to_view_coord (when, val);
1134                         (*x)->when = when;
1135                         (*x)->value = val;
1136                 }
1137         }
1138 }
1139
1140 PresentationInfo const &
1141 AutomationTimeAxisView::presentation_info () const
1142 {
1143         return _stripable->presentation_info();
1144 }
1145
1146 boost::shared_ptr<Stripable>
1147 AutomationTimeAxisView::stripable () const
1148 {
1149         return _stripable;
1150 }
1151
1152 Gdk::Color
1153 AutomationTimeAxisView::color () const
1154 {
1155         return gdk_color_from_rgb (_stripable->presentation_info().color());
1156 }
1157