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