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