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