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