4e370b8b940f4aad5bf5b17426981084658b99ce
[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         if (!EventTypeMap::instance().type_is_midi (_control->parameter().type())) {
484                 set_automation_state ((AutoState) ARDOUR::Off);
485         }
486         _editor.commit_reversible_command ();
487         _session->set_dirty ();
488 }
489
490 void
491 AutomationTimeAxisView::set_height (uint32_t h, TrackHeightMode m)
492 {
493         bool const changed = (height != (uint32_t) h) || first_call_to_set_height;
494         uint32_t const normal = preset_height (HeightNormal);
495         bool const changed_between_small_and_normal = ( (height < normal && h >= normal) || (height >= normal || h < normal) );
496
497         TimeAxisView::set_height (h, m);
498
499         _base_rect->set_y1 (h);
500
501         if (_line) {
502                 _line->set_height(h - 2.5);
503         }
504
505         if (_view) {
506                 _view->set_height(h);
507                 _view->update_contents_height();
508         }
509
510         if (changed_between_small_and_normal || first_call_to_set_height) {
511
512                 first_call_to_set_height = false;
513
514                 if (h >= preset_height (HeightNormal)) {
515                         if (!(_parameter.type() >= MidiCCAutomation &&
516                               _parameter.type() <= MidiChannelPressureAutomation)) {
517                                 auto_dropdown.show();
518                         }
519                         name_label.show();
520                         hide_button.show();
521
522                 } else if (h >= preset_height (HeightSmall)) {
523                         controls_table.hide_all ();
524                         auto_dropdown.hide();
525                         name_label.hide();
526                 }
527         }
528
529         if (changed) {
530                 if (_canvas_display->visible() && _stripable) {
531                         /* only emit the signal if the height really changed and we were visible */
532                         _stripable->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
533                 }
534         }
535 }
536
537 void
538 AutomationTimeAxisView::set_samples_per_pixel (double fpp)
539 {
540         TimeAxisView::set_samples_per_pixel (fpp);
541
542         if (_line) {
543                 _line->reset ();
544         }
545
546         if (_view) {
547                 _view->set_samples_per_pixel (fpp);
548         }
549 }
550
551 void
552 AutomationTimeAxisView::hide_clicked ()
553 {
554         hide_button.set_sensitive(false);
555         set_marked_for_display (false);
556         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*>(parent);
557         if (stv) {
558                 stv->request_redraw ();
559         }
560         hide_button.set_sensitive(true);
561 }
562
563 void
564 AutomationTimeAxisView::build_display_menu ()
565 {
566         using namespace Menu_Helpers;
567
568         /* prepare it */
569
570         TimeAxisView::build_display_menu ();
571
572         /* now fill it with our stuff */
573
574         MenuList& items = display_menu->items();
575
576         items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked)));
577         items.push_back (SeparatorElem());
578         items.push_back (MenuElem (_("Clear"), sigc::mem_fun(*this, &AutomationTimeAxisView::clear_clicked)));
579         items.push_back (SeparatorElem());
580
581         /* state menu */
582
583         Menu* auto_state_menu = manage (new Menu);
584         auto_state_menu->set_name ("ArdourContextMenu");
585         MenuList& as_items = auto_state_menu->items();
586
587         as_items.push_back (CheckMenuElem (S_("Automation|Manual"), sigc::bind (
588                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
589                         (AutoState) ARDOUR::Off)));
590         auto_off_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
591
592         as_items.push_back (CheckMenuElem (_("Play"), sigc::bind (
593                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
594                         (AutoState) Play)));
595         auto_play_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
596
597         as_items.push_back (CheckMenuElem (_("Write"), sigc::bind (
598                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
599                         (AutoState) Write)));
600         auto_write_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
601
602         as_items.push_back (CheckMenuElem (_("Touch"), sigc::bind (
603                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
604                         (AutoState) Touch)));
605         auto_touch_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
606
607         if (!(_parameter.type() >= MidiCCAutomation &&
608               _parameter.type() <= MidiChannelPressureAutomation)) {
609                 items.push_back (MenuElem (_("State"), *auto_state_menu));
610         }
611
612         /* mode menu */
613
614         /* current interpolation state */
615         AutomationList::InterpolationStyle const s = _view ? _view->interpolation() : _control->list()->interpolation ();
616
617         if (ARDOUR::parameter_is_midi((AutomationType)_parameter.type())) {
618
619                 Menu* auto_mode_menu = manage (new Menu);
620                 auto_mode_menu->set_name ("ArdourContextMenu");
621                 MenuList& am_items = auto_mode_menu->items();
622
623                 RadioMenuItem::Group group;
624
625                 am_items.push_back (RadioMenuElem (group, _("Discrete"), sigc::bind (
626                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
627                                 AutomationList::Discrete)));
628                 mode_discrete_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
629
630                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
631                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
632                                 AutomationList::Linear)));
633                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
634
635                 items.push_back (MenuElem (_("Mode"), *auto_mode_menu));
636
637         } else {
638 #ifdef XXX_NEW_INTERPOLATON__BREAK_SESSION_FORMAT_XXX
639                 Menu* auto_mode_menu = manage (new Menu);
640                 auto_mode_menu->set_name ("ArdourContextMenu");
641                 MenuList& am_items = auto_mode_menu->items();
642                 bool have_options = false;
643
644                 RadioMenuItem::Group group;
645
646                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
647                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
648                                 AutomationList::Linear)));
649                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
650
651                 if (_control->desc().logarithmic) {
652                         am_items.push_back (RadioMenuElem (group, _("Logarithmic"), sigc::bind (
653                                                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
654                                                         AutomationList::Logarithmic)));
655                         mode_log_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
656                         have_options = true;
657                 } else {
658                         mode_log_item = 0;
659                 }
660
661                 if (_line->get_uses_gain_mapping () && !_control->desc().logarithmic) {
662                         am_items.push_back (RadioMenuElem (group, _("Exponential"), sigc::bind (
663                                                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
664                                                         AutomationList::Exponential)));
665                         mode_exp_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
666                         have_options = true;
667                 } else {
668                         mode_exp_item = 0;
669                 }
670
671                 if (have_options) {
672                         items.push_back (MenuElem (_("Interpolation"), *auto_mode_menu));
673                 } else {
674                         mode_line_item = 0;
675                         delete auto_mode_menu;
676                         auto_mode_menu = 0;
677                 }
678 #endif
679         }
680
681         /* make sure the automation menu state is correct */
682
683         automation_state_changed ();
684         interpolation_changed (s);
685 }
686
687 void
688 AutomationTimeAxisView::add_automation_event (GdkEvent* event, framepos_t frame, double y, bool with_guard_points)
689 {
690         if (!_line) {
691                 return;
692         }
693
694         boost::shared_ptr<AutomationList> list = _line->the_list ();
695
696         if (list->in_write_pass()) {
697                 /* do not allow the GUI to add automation events during an
698                    automation write pass.
699                 */
700                 return;
701         }
702
703         MusicFrame when (frame, 0);
704         _editor.snap_to_with_modifier (when, event);
705
706         if (UIConfiguration::instance().get_new_automation_points_on_lane()) {
707                 if (_control->list()->size () == 0) {
708                         y = _control->get_value ();
709                 } else {
710                         y = _control->list()->eval (when.frame);
711                 }
712         } else {
713                 double x = 0;
714                 _line->grab_item().canvas_to_item (x, y);
715                 /* compute vertical fractional position */
716                 y = 1.0 - (y / _line->height());
717                 /* map using line */
718                 _line->view_to_model_coord (x, y);
719         }
720
721         XMLNode& before = list->get_state();
722         std::list<Selectable*> results;
723
724         if (list->editor_add (when.frame, y, with_guard_points)) {
725                 XMLNode& after = list->get_state();
726                 _editor.begin_reversible_command (_("add automation event"));
727                 _session->add_command (new MementoCommand<ARDOUR::AutomationList> (*list.get (), &before, &after));
728
729                 _line->get_selectables (when.frame, when.frame, 0.0, 1.0, results);
730                 _editor.get_selection ().set (results);
731
732                 _editor.commit_reversible_command ();
733                 _session->set_dirty ();
734         }
735 }
736
737 bool
738 AutomationTimeAxisView::paste (framepos_t pos, const Selection& selection, PasteContext& ctx, const int32_t divisions)
739 {
740         if (_line) {
741                 return paste_one (pos, ctx.count, ctx.times, selection, ctx.counts, ctx.greedy);
742         } else if (_view) {
743                 AutomationSelection::const_iterator l = selection.lines.get_nth(_parameter, ctx.counts.n_lines(_parameter));
744                 if (l == selection.lines.end()) {
745                         if (ctx.greedy && selection.lines.size() == 1) {
746                                 l = selection.lines.begin();
747                         }
748                 }
749                 if (l != selection.lines.end() && _view->paste (pos, ctx.count, ctx.times, *l)) {
750                         ctx.counts.increase_n_lines(_parameter);
751                         return true;
752                 }
753         }
754
755         return false;
756 }
757
758 bool
759 AutomationTimeAxisView::paste_one (framepos_t pos, unsigned paste_count, float times, const Selection& selection, ItemCounts& counts, bool greedy)
760 {
761         boost::shared_ptr<AutomationList> alist(_line->the_list());
762
763         if (_session->transport_rolling() && alist->automation_write()) {
764                 /* do not paste if this control is in write mode and we're rolling */
765                 return false;
766         }
767
768         /* Get appropriate list from selection. */
769         AutomationSelection::const_iterator p = selection.lines.get_nth(_parameter, counts.n_lines(_parameter));
770         if (p == selection.lines.end()) {
771                 if (greedy && selection.lines.size() == 1) {
772                         p = selection.lines.begin();
773                 } else {
774                         return false;
775                 }
776         }
777         counts.increase_n_lines(_parameter);
778
779         /* add multi-paste offset if applicable */
780
781         AutomationType src_type = (AutomationType)(*p)->parameter().type ();
782         double len = (*p)->length();
783
784         if (parameter_is_midi (src_type)) {
785                 // convert length to samples (incl tempo-ramps)
786                 len = DoubleBeatsFramesConverter (_session->tempo_map(), pos).to (len * paste_count);
787                 pos += _editor.get_paste_offset (pos, paste_count > 0 ? 1 : 0, len);
788         } else {
789                 pos += _editor.get_paste_offset (pos, paste_count, len);
790         }
791
792         /* convert sample-position to model's unit and position */
793         double const model_pos = _line->time_converter().from (pos - _line->time_converter().origin_b ());
794
795         XMLNode &before = alist->get_state();
796         alist->paste (**p, model_pos, DoubleBeatsFramesConverter (_session->tempo_map(), pos));
797         _session->add_command (new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
798
799         return true;
800 }
801
802 void
803 AutomationTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results, bool /*within*/)
804 {
805         if (!_line && !_view) {
806                 return;
807         }
808
809         if (touched (top, bot)) {
810
811                 /* remember: this is X Window - coordinate space starts in upper left and moves down.
812                    _y_position is the "origin" or "top" of the track.
813                 */
814
815                 /* bottom of our track */
816                 double const mybot = _y_position + height;
817
818                 double topfrac;
819                 double botfrac;
820
821                 if (_y_position >= top && mybot <= bot) {
822
823                         /* _y_position is below top, mybot is above bot, so we're fully
824                            covered vertically.
825                         */
826
827                         topfrac = 1.0;
828                         botfrac = 0.0;
829
830                 } else {
831
832                         /* top and bot are within _y_position .. mybot */
833
834                         topfrac = 1.0 - ((top - _y_position) / height);
835                         botfrac = 1.0 - ((bot - _y_position) / height);
836
837                 }
838
839                 if (_line) {
840                         _line->get_selectables (start, end, botfrac, topfrac, results);
841                 } else if (_view) {
842                         _view->get_selectables (start, end, botfrac, topfrac, results);
843                 }
844         }
845 }
846
847 void
848 AutomationTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
849 {
850         if (_line) {
851                 _line->get_inverted_selectables (sel, result);
852         }
853 }
854
855 void
856 AutomationTimeAxisView::set_selected_points (PointSelection& points)
857 {
858         if (_line) {
859                 _line->set_selected_points (points);
860         } else if (_view) {
861                 _view->set_selected_points (points);
862         }
863 }
864
865 void
866 AutomationTimeAxisView::clear_lines ()
867 {
868         _line.reset();
869         _list_connections.drop_connections ();
870 }
871
872 void
873 AutomationTimeAxisView::add_line (boost::shared_ptr<AutomationLine> line)
874 {
875         if (_control && line) {
876                 assert(line->the_list() == _control->list());
877
878                 _control->alist()->automation_state_changed.connect (
879                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::automation_state_changed, this), gui_context()
880                         );
881
882                 _control->alist()->InterpolationChanged.connect (
883                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::interpolation_changed, this, _1), gui_context()
884                         );
885         }
886
887         _line = line;
888
889         line->set_height (height - 2.5);
890
891         /* pick up the current state */
892         automation_state_changed ();
893
894         line->add_visibility (AutomationLine::Line);
895 }
896
897 bool
898 AutomationTimeAxisView::propagate_time_selection () const
899 {
900         /* MIDI automation is part of the MIDI region. It is always
901          * implicily selected with the parent, regardless of TAV selection
902          */
903         if (_parameter.type() >= MidiCCAutomation &&
904             _parameter.type() <= MidiChannelPressureAutomation) {
905                 return true;
906         }
907         return false;
908 }
909
910 void
911 AutomationTimeAxisView::entered()
912 {
913         if (_line) {
914                 _line->track_entered();
915         }
916 }
917
918 void
919 AutomationTimeAxisView::exited ()
920 {
921         if (_line) {
922                 _line->track_exited();
923         }
924 }
925
926 void
927 AutomationTimeAxisView::color_handler ()
928 {
929         if (_line) {
930                 _line->set_colors();
931         }
932 }
933
934 int
935 AutomationTimeAxisView::set_state_2X (const XMLNode& node, int /*version*/)
936 {
937         if (node.name() == X_("gain") && _parameter == Evoral::Parameter (GainAutomation)) {
938
939                 bool shown;
940                 if (node.get_property (X_("shown"), shown)) {
941                         if (shown) {
942                                 _canvas_display->show (); /* FIXME: necessary? show_at? */
943                                 set_gui_property ("visible", shown);
944                         }
945                 } else {
946                         set_gui_property ("visible", false);
947                 }
948         }
949
950         return 0;
951 }
952
953 int
954 AutomationTimeAxisView::set_state (const XMLNode&, int /*version*/)
955 {
956         return 0;
957 }
958
959 void
960 AutomationTimeAxisView::what_has_visible_automation (const boost::shared_ptr<Automatable>& automatable, set<Evoral::Parameter>& visible)
961 {
962         /* this keeps "knowledge" of how we store visibility information
963            in XML private to this class.
964         */
965
966         assert (automatable);
967
968         Automatable::Controls& controls (automatable->controls());
969
970         for (Automatable::Controls::iterator i = controls.begin(); i != controls.end(); ++i) {
971
972                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
973
974                 if (ac && ac->alist()) {
975
976                         const XMLNode* gui_node = ac->extra_xml ("GUI");
977
978                         if (gui_node) {
979                                 bool shown;
980                                 if (gui_node->get_property ("shown", shown) && shown) {
981                                         visible.insert (i->first);
982                                 }
983                         }
984                 }
985         }
986 }
987
988
989 /** @return true if this view has any automation data to display */
990 bool
991 AutomationTimeAxisView::has_automation () const
992 {
993         return ( (_line && _line->npoints() > 0) || (_view && _view->has_automation()) );
994 }
995
996 list<boost::shared_ptr<AutomationLine> >
997 AutomationTimeAxisView::lines () const
998 {
999         list<boost::shared_ptr<AutomationLine> > lines;
1000
1001         if (_line) {
1002                 lines.push_back (_line);
1003         } else if (_view) {
1004                 lines = _view->get_lines ();
1005         }
1006
1007         return lines;
1008 }
1009
1010 string
1011 AutomationTimeAxisView::state_id() const
1012 {
1013         if (_parameter && _stripable && _automatable == _stripable) {
1014                 const string parameter_str = PBD::to_string (_parameter.type()) + "/" +
1015                                              PBD::to_string (_parameter.id()) + "/" +
1016                                              PBD::to_string (_parameter.channel ());
1017
1018                 return string("automation ") + PBD::to_string(_stripable->id()) + " " + parameter_str;
1019         } else if (_automatable != _stripable && _control) {
1020                 return string("automation ") + _control->id().to_s();
1021         } else {
1022                 error << "Automation time axis has no state ID" << endmsg;
1023                 return "";
1024         }
1025 }
1026
1027 /** Given a state id string, see if it is one generated by
1028  *  this class.  If so, parse it into its components.
1029  *  @param state_id State ID string to parse.
1030  *  @param route_id Filled in with the route's ID if the state ID string is parsed.
1031  *  @param has_parameter Filled in with true if the state ID has a parameter, otherwise false.
1032  *  @param parameter Filled in with the state ID's parameter, if it has one.
1033  *  @return true if this is a state ID generated by this class, otherwise false.
1034  */
1035
1036 bool
1037 AutomationTimeAxisView::parse_state_id (
1038         string const & state_id,
1039         PBD::ID & route_id,
1040         bool & has_parameter,
1041         Evoral::Parameter & parameter)
1042 {
1043         stringstream ss;
1044         ss << state_id;
1045
1046         string a, b, c;
1047         ss >> a >> b >> c;
1048
1049         if (a != X_("automation")) {
1050                 return false;
1051         }
1052
1053         route_id = PBD::ID (b);
1054
1055         if (c.empty ()) {
1056                 has_parameter = false;
1057                 return true;
1058         }
1059
1060         has_parameter = true;
1061
1062         vector<string> p;
1063         boost::split (p, c, boost::is_any_of ("/"));
1064
1065         assert (p.size() == 3);
1066
1067         parameter = Evoral::Parameter (
1068                 PBD::string_to<uint32_t>(p[0]), // type
1069                 PBD::string_to<uint8_t>(p[2]), // channel
1070                 PBD::string_to<uint32_t>(p[1]) // id
1071                 );
1072
1073         return true;
1074 }
1075
1076 void
1077 AutomationTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1078 {
1079         list<boost::shared_ptr<AutomationLine> > lines;
1080         if (_line) {
1081                 lines.push_back (_line);
1082         } else if (_view) {
1083                 lines = _view->get_lines ();
1084         }
1085
1086         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
1087                 cut_copy_clear_one (**i, selection, op);
1088         }
1089 }
1090
1091 void
1092 AutomationTimeAxisView::cut_copy_clear_one (AutomationLine& line, Selection& selection, CutCopyOp op)
1093 {
1094         boost::shared_ptr<Evoral::ControlList> what_we_got;
1095         boost::shared_ptr<AutomationList> alist (line.the_list());
1096
1097         XMLNode &before = alist->get_state();
1098
1099         /* convert time selection to automation list model coordinates */
1100         const Evoral::TimeConverter<double, ARDOUR::framepos_t>& tc = line.time_converter ();
1101         double const start = tc.from (selection.time.front().start - tc.origin_b ());
1102         double const end = tc.from (selection.time.front().end - tc.origin_b ());
1103
1104         switch (op) {
1105         case Delete:
1106                 if (alist->cut (start, end) != 0) {
1107                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1108                 }
1109                 break;
1110
1111         case Cut:
1112
1113                 if ((what_we_got = alist->cut (start, end)) != 0) {
1114                         _editor.get_cut_buffer().add (what_we_got);
1115                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1116                 }
1117                 break;
1118         case Copy:
1119                 if ((what_we_got = alist->copy (start, end)) != 0) {
1120                         _editor.get_cut_buffer().add (what_we_got);
1121                 }
1122                 break;
1123
1124         case Clear:
1125                 if ((what_we_got = alist->cut (start, end)) != 0) {
1126                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1127                 }
1128                 break;
1129         }
1130
1131         if (what_we_got) {
1132                 for (AutomationList::iterator x = what_we_got->begin(); x != what_we_got->end(); ++x) {
1133                         double when = (*x)->when;
1134                         double val  = (*x)->value;
1135                         line.model_to_view_coord (when, val);
1136                         (*x)->when = when;
1137                         (*x)->value = val;
1138                 }
1139         }
1140 }
1141
1142 PresentationInfo const &
1143 AutomationTimeAxisView::presentation_info () const
1144 {
1145         return _stripable->presentation_info();
1146 }
1147
1148 boost::shared_ptr<Stripable>
1149 AutomationTimeAxisView::stripable () const
1150 {
1151         return _stripable;
1152 }
1153
1154 Gdk::Color
1155 AutomationTimeAxisView::color () const
1156 {
1157         return gdk_color_from_rgb (_stripable->presentation_info().color());
1158 }
1159