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