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