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