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