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