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