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