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