special case track-header buttons
[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         hide_button.set_tweaks(ArdourButton::TrackHeader);
133
134         auto_button.set_name ("route button");
135         hide_button.set_name ("route button");
136
137         auto_button.unset_flags (Gtk::CAN_FOCUS);
138         hide_button.unset_flags (Gtk::CAN_FOCUS);
139
140         controls_table.set_no_show_all();
141
142         ARDOUR_UI::instance()->set_tip(auto_button, _("automation state"));
143         ARDOUR_UI::instance()->set_tip(hide_button, _("hide track"));
144
145         const string str = gui_property ("height");
146         if (!str.empty()) {
147                 set_height (atoi (str));
148         } else {
149                 set_height (preset_height (HeightNormal));
150         }
151
152         //name label isn't editable on an automation track; remove the tooltip
153         ARDOUR_UI::instance()->set_tip (name_label, X_(""));
154
155         /* repack the name label */
156
157         if (name_label.get_parent()) {
158                 name_label.get_parent()->remove (name_label);
159         }
160         
161         name_label.set_text (_name);
162         name_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
163         name_label.set_name (X_("TrackParameterName"));
164         name_label.set_ellipsize (Pango::ELLIPSIZE_END);
165
166         string tipname = nomparent;
167         if (!tipname.empty()) {
168                 tipname += ": ";
169         }
170         tipname += _name;
171         ARDOUR_UI::instance()->set_tip(controls_ebox, tipname);
172
173         /* add the buttons */
174         controls_table.remove (name_hbox);
175         if (ARDOUR::Profile->get_mixbus()) {
176                 controls_table.attach (hide_button, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
177         } else {
178                 controls_table.attach (hide_button, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 1, 0);
179         }
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, 1, 0);
182
183         Gtk::DrawingArea *blank0 = manage (new Gtk::DrawingArea());
184         Gtk::DrawingArea *blankB = 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                 blankB->set_name ("AudioTrackControlsBaseUnselected");
192         }
193         else if (rtv && rtv->is_midi_track()) {
194                 blank0->set_name ("MidiTrackControlsBaseUnselected");
195                 blankB->set_name ("MidiTrackControlsBaseUnselected");
196         }
197         else {
198                 blank0->set_name ("AudioBusControlsBaseUnselected");
199                 blankB->set_name ("AudioBusControlsBaseUnselected");
200         }
201         blank0->set_size_request (-1, -1);
202         // one button width (blank0) + 2 * table colspacing - 1 * sep line
203         // -> align with 2nd button in Mixbus profile
204         blankB->set_size_request (3, -1);
205
206         VSeparator* separator = manage (new VSeparator());
207         separator->set_name("TrackSeparator");
208         separator->set_size_request (1, -1);
209
210         controls_button_size_group->add_widget(*blank0);
211         controls_button_size_group->add_widget(hide_button);
212
213         time_axis_hbox.pack_start (*blank0, false, false);
214         time_axis_hbox.pack_start (*blankB, false, false);
215         time_axis_hbox.pack_start (*separator, false, false);
216         time_axis_hbox.reorder_child (*blank0, 0);
217         time_axis_hbox.reorder_child (*blankB, 1);
218         time_axis_hbox.reorder_child (*separator, 2);
219         time_axis_hbox.reorder_child (time_axis_vbox, 3);
220
221         blank0->show();
222         blank0->show();
223         separator->show();
224         name_label.show ();
225         hide_button.show ();
226
227         if (_controller) {
228                 _controller.get()->set_tweaks (PixFader::Tweaks(_controller.get()->tweaks() | PixFader::NoVerticalScroll));
229                 controls_table.attach (*_controller.get(), 2, 4, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 1, 0);
230         }
231
232         controls_table.show_all ();
233
234         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked));
235         auto_button.signal_clicked.connect (sigc::mem_fun(*this, &AutomationTimeAxisView::auto_clicked));
236
237         controls_base_selected_name = X_("AutomationTrackControlsBaseSelected");
238         controls_base_unselected_name = X_("AutomationTrackControlsBase");
239
240         controls_ebox.set_name (controls_base_unselected_name);
241         time_axis_frame.set_name (controls_base_unselected_name);
242
243         /* ask for notifications of any new RegionViews */
244         if (show_regions) {
245
246                 if (_view) {
247                         _view->attach ();
248                 }
249
250         } else {
251                 /* no regions, just a single line for the entire track (e.g. bus gain) */
252
253                 assert (_control);
254
255                 boost::shared_ptr<AutomationLine> line (
256                         new AutomationLine (
257                                 ARDOUR::EventTypeMap::instance().to_symbol(_parameter),
258                                 *this,
259                                 *_canvas_display,
260                                 _control->alist()
261                                 )
262                         );
263
264                 line->set_line_color (ARDOUR_UI::config()->get_canvasvar_ProcessorAutomationLine());
265                 line->queue_reset ();
266                 add_line (line);
267         }
268
269         /* make sure labels etc. are correct */
270
271         automation_state_changed ();
272         ColorsChanged.connect (sigc::mem_fun (*this, &AutomationTimeAxisView::color_handler));
273
274         _route->DropReferences.connect (
275                 _route_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::route_going_away, this), gui_context ()
276                 );
277 }
278
279 AutomationTimeAxisView::~AutomationTimeAxisView ()
280 {
281         delete _view;
282 }
283
284 void
285 AutomationTimeAxisView::route_going_away ()
286 {
287         _route.reset ();
288 }
289
290 void
291 AutomationTimeAxisView::auto_clicked ()
292 {
293         using namespace Menu_Helpers;
294
295         if (automation_menu == 0) {
296                 automation_menu = manage (new Menu);
297                 automation_menu->set_name ("ArdourContextMenu");
298                 MenuList& items (automation_menu->items());
299
300                 items.push_back (MenuElem (S_("Automation|Manual"), sigc::bind (sigc::mem_fun(*this,
301                                                                                               &AutomationTimeAxisView::set_automation_state), (AutoState) ARDOUR::Off)));
302                 items.push_back (MenuElem (_("Play"), sigc::bind (sigc::mem_fun(*this,
303                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Play)));
304                 items.push_back (MenuElem (_("Write"), sigc::bind (sigc::mem_fun(*this,
305                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Write)));
306                 items.push_back (MenuElem (_("Touch"), sigc::bind (sigc::mem_fun(*this,
307                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Touch)));
308         }
309
310         automation_menu->popup (1, gtk_get_current_event_time());
311 }
312
313 void
314 AutomationTimeAxisView::set_automation_state (AutoState state)
315 {
316         if (ignore_state_request) {
317                 return;
318         }
319
320         if (_automatable) {
321                 _automatable->set_parameter_automation_state (_parameter, state);
322         }
323
324         if (_view) {
325                 _view->set_automation_state (state);
326
327                 /* AutomationStreamViews don't signal when their automation state changes, so handle
328                    our updates `manually'.
329                 */
330                 automation_state_changed ();
331         }
332 }
333
334 void
335 AutomationTimeAxisView::automation_state_changed ()
336 {
337         AutoState state;
338
339         /* update button label */
340
341         if (_view) {
342                 state = _view->automation_state ();
343         } else if (_line) {
344                 assert (_control);
345                 state = _control->alist()->automation_state ();
346         } else {
347                 state = ARDOUR::Off;
348         }
349
350         switch (state & (ARDOUR::Off|Play|Touch|Write)) {
351         case ARDOUR::Off:
352                 auto_button.set_text (S_("Automation|Manual"));
353                 if (auto_off_item) {
354                         ignore_state_request = true;
355                         auto_off_item->set_active (true);
356                         auto_play_item->set_active (false);
357                         auto_touch_item->set_active (false);
358                         auto_write_item->set_active (false);
359                         ignore_state_request = false;
360                 }
361                 break;
362         case Play:
363                 auto_button.set_text (_("Play"));
364                 if (auto_play_item) {
365                         ignore_state_request = true;
366                         auto_play_item->set_active (true);
367                         auto_off_item->set_active (false);
368                         auto_touch_item->set_active (false);
369                         auto_write_item->set_active (false);
370                         ignore_state_request = false;
371                 }
372                 break;
373         case Write:
374                 auto_button.set_text (_("Write"));
375                 if (auto_write_item) {
376                         ignore_state_request = true;
377                         auto_write_item->set_active (true);
378                         auto_off_item->set_active (false);
379                         auto_play_item->set_active (false);
380                         auto_touch_item->set_active (false);
381                         ignore_state_request = false;
382                 }
383                 break;
384         case Touch:
385                 auto_button.set_text (_("Touch"));
386                 if (auto_touch_item) {
387                         ignore_state_request = true;
388                         auto_touch_item->set_active (true);
389                         auto_off_item->set_active (false);
390                         auto_play_item->set_active (false);
391                         auto_write_item->set_active (false);
392                         ignore_state_request = false;
393                 }
394                 break;
395         default:
396                 auto_button.set_text (_("???"));
397                 break;
398         }
399 }
400
401 /** The interpolation style of our AutomationList has changed, so update */
402 void
403 AutomationTimeAxisView::interpolation_changed (AutomationList::InterpolationStyle s)
404 {
405         if (mode_line_item && mode_discrete_item) {
406                 if (s == AutomationList::Discrete) {
407                         mode_discrete_item->set_active(true);
408                         mode_line_item->set_active(false);
409                 } else {
410                         mode_line_item->set_active(true);
411                         mode_discrete_item->set_active(false);
412                 }
413         }
414 }
415
416 /** A menu item has been selected to change our interpolation mode */
417 void
418 AutomationTimeAxisView::set_interpolation (AutomationList::InterpolationStyle style)
419 {
420         /* Tell our view's list, if we have one, otherwise tell our own.
421          * Everything else will be signalled back from that.
422          */
423
424         if (_view) {
425                 _view->set_interpolation (style);
426         } else {
427                 assert (_control);
428                 _control->list()->set_interpolation (style);
429         }
430 }
431
432 void
433 AutomationTimeAxisView::clear_clicked ()
434 {
435         assert (_line || _view);
436
437         _session->begin_reversible_command (_("clear automation"));
438
439         if (_line) {
440                 _line->clear ();
441         } else if (_view) {
442                 _view->clear ();
443         }
444
445         _session->commit_reversible_command ();
446         _session->set_dirty ();
447 }
448
449 void
450 AutomationTimeAxisView::set_height (uint32_t h)
451 {
452         bool const changed = (height != (uint32_t) h) || first_call_to_set_height;
453         uint32_t const normal = preset_height (HeightNormal);
454         bool const changed_between_small_and_normal = ( (height < normal && h >= normal) || (height >= normal || h < normal) );
455
456         TimeAxisView::set_height (h);
457
458         _base_rect->set_y1 (h);
459
460         if (_line) {
461                 _line->set_height(h);
462         }
463
464         if (_view) {
465                 _view->set_height(h);
466                 _view->update_contents_height();
467         }
468
469         if (changed_between_small_and_normal || first_call_to_set_height) {
470
471                 first_call_to_set_height = false;
472
473                 if (h >= preset_height (HeightNormal)) {
474                         auto_button.show();
475                         name_label.show();
476                         hide_button.show();
477
478                 } else if (h >= preset_height (HeightSmall)) {
479                         controls_table.hide_all ();
480                         auto_button.hide();
481                         name_label.hide();
482                 }
483         }
484
485         if (changed) {
486                 if (_canvas_display->visible() && _route) {
487                         /* only emit the signal if the height really changed and we were visible */
488                         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
489                 }
490         }
491 }
492
493 void
494 AutomationTimeAxisView::set_samples_per_pixel (double fpp)
495 {
496         TimeAxisView::set_samples_per_pixel (fpp);
497
498         if (_line) {
499                 _line->reset ();
500         }
501
502         if (_view) {
503                 _view->set_samples_per_pixel (fpp);
504         }
505 }
506
507 void
508 AutomationTimeAxisView::hide_clicked ()
509 {
510         hide_button.set_sensitive(false);
511         set_marked_for_display (false);
512         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(parent);
513         if (rtv) {
514                 rtv->request_redraw ();
515         }
516         hide_button.set_sensitive(true);
517 }
518
519 void
520 AutomationTimeAxisView::build_display_menu ()
521 {
522         using namespace Menu_Helpers;
523
524         /* prepare it */
525
526         TimeAxisView::build_display_menu ();
527
528         /* now fill it with our stuff */
529
530         MenuList& items = display_menu->items();
531
532         items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked)));
533         items.push_back (SeparatorElem());
534         items.push_back (MenuElem (_("Clear"), sigc::mem_fun(*this, &AutomationTimeAxisView::clear_clicked)));
535         items.push_back (SeparatorElem());
536
537         /* state menu */
538
539         Menu* auto_state_menu = manage (new Menu);
540         auto_state_menu->set_name ("ArdourContextMenu");
541         MenuList& as_items = auto_state_menu->items();
542
543         as_items.push_back (CheckMenuElem (S_("Automation|Manual"), sigc::bind (
544                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
545                         (AutoState) ARDOUR::Off)));
546         auto_off_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
547
548         as_items.push_back (CheckMenuElem (_("Play"), sigc::bind (
549                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
550                         (AutoState) Play)));
551         auto_play_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
552
553         as_items.push_back (CheckMenuElem (_("Write"), sigc::bind (
554                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
555                         (AutoState) Write)));
556         auto_write_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
557
558         as_items.push_back (CheckMenuElem (_("Touch"), sigc::bind (
559                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
560                         (AutoState) Touch)));
561         auto_touch_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
562
563         items.push_back (MenuElem (_("State"), *auto_state_menu));
564
565         /* mode menu */
566
567         /* current interpolation state */
568         AutomationList::InterpolationStyle const s = _view ? _view->interpolation() : _control->list()->interpolation ();
569
570         if (EventTypeMap::instance().is_midi_parameter(_parameter)) {
571
572                 Menu* auto_mode_menu = manage (new Menu);
573                 auto_mode_menu->set_name ("ArdourContextMenu");
574                 MenuList& am_items = auto_mode_menu->items();
575
576                 RadioMenuItem::Group group;
577
578                 am_items.push_back (RadioMenuElem (group, _("Discrete"), sigc::bind (
579                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
580                                 AutomationList::Discrete)));
581                 mode_discrete_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
582                 mode_discrete_item->set_active (s == AutomationList::Discrete);
583
584                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
585                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
586                                 AutomationList::Linear)));
587                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
588                 mode_line_item->set_active (s == AutomationList::Linear);
589
590                 items.push_back (MenuElem (_("Mode"), *auto_mode_menu));
591         }
592
593         /* make sure the automation menu state is correct */
594
595         automation_state_changed ();
596         interpolation_changed (s);
597 }
598
599 void
600 AutomationTimeAxisView::add_automation_event (GdkEvent* event, framepos_t when, double y, bool with_guard_points)
601 {
602         if (!_line) {
603                 return;
604         }
605
606         boost::shared_ptr<AutomationList> list = _line->the_list ();
607         
608         if (list->in_write_pass()) {
609                 /* do not allow the GUI to add automation events during an
610                    automation write pass.
611                 */
612                 return;
613         }
614
615         double x = 0;
616
617         _canvas_display->canvas_to_item (x, y);
618
619         /* compute vertical fractional position */
620
621         y = 1.0 - (y / height);
622
623         /* map using line */
624
625         _line->view_to_model_coord (x, y);
626
627
628         _editor.snap_to_with_modifier (when, event);
629
630         _session->begin_reversible_command (_("add automation event"));
631         XMLNode& before = list->get_state();
632
633         list->add (when, y, with_guard_points);
634
635         XMLNode& after = list->get_state();
636         _session->commit_reversible_command (new MementoCommand<ARDOUR::AutomationList> (*list, &before, &after));
637         _session->set_dirty ();
638 }
639
640 /** Paste a selection.
641  *  @param pos Position to paste to (session frames).
642  *  @param times Number of times to paste.
643  *  @param selection Selection to paste.
644  *  @param nth Index of the AutomationList within the selection to paste from.
645  */
646 bool
647 AutomationTimeAxisView::paste (framepos_t pos, float times, Selection& selection, size_t nth)
648 {
649         boost::shared_ptr<AutomationLine> line;
650
651         if (_line) {
652                 line = _line;
653         } else if (_view) {
654                 line = _view->paste_line (pos);
655         }
656
657         if (!line) {
658                 return false;
659         }
660
661         return paste_one (*line, pos, times, selection, nth);
662 }
663
664 bool
665 AutomationTimeAxisView::paste_one (AutomationLine& line, framepos_t pos, float times, Selection& selection, size_t nth)
666 {
667         AutomationSelection::iterator p;
668         boost::shared_ptr<AutomationList> alist(line.the_list());
669
670         if (_session->transport_rolling() && alist->automation_write()) {
671                 /* do not paste if this control is in write mode and we're rolling */
672                 return false;
673         }
674
675         for (p = selection.lines.begin(); p != selection.lines.end() && nth; ++p, --nth) {}
676
677         if (p == selection.lines.end()) {
678                 return false;
679         }
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) {
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 (_control) {
891                 return string_compose ("automation %1", _control->id().to_s());
892         } else {
893                 assert (_parameter);
894                 return string_compose ("automation %1 %2/%3/%4", 
895                                        _route->id(), 
896                                        _parameter.type(),
897                                        _parameter.id(),
898                                        (int) _parameter.channel());
899         }
900 }
901
902 /** Given a state id string, see if it is one generated by
903  *  this class.  If so, parse it into its components.
904  *  @param state_id State ID string to parse.
905  *  @param route_id Filled in with the route's ID if the state ID string is parsed.
906  *  @param has_parameter Filled in with true if the state ID has a parameter, otherwise false.
907  *  @param parameter Filled in with the state ID's parameter, if it has one.
908  *  @return true if this is a state ID generated by this class, otherwise false.
909  */
910
911 bool
912 AutomationTimeAxisView::parse_state_id (
913         string const & state_id,
914         PBD::ID & route_id,
915         bool & has_parameter,
916         Evoral::Parameter & parameter)
917 {
918         stringstream s;
919         s << state_id;
920
921         string a, b, c;
922         s >> a >> b >> c;
923
924         if (a != X_("automation")) {
925                 return false;
926         }
927
928         route_id = PBD::ID (b);
929
930         if (c.empty ()) {
931                 has_parameter = false;
932                 return true;
933         }
934
935         has_parameter = true;
936
937         vector<string> p;
938         boost::split (p, c, boost::is_any_of ("/"));
939
940         assert (p.size() == 3);
941
942         parameter = Evoral::Parameter (
943                 boost::lexical_cast<int> (p[0]),
944                 boost::lexical_cast<int> (p[2]),
945                 boost::lexical_cast<int> (p[1])
946                 );
947
948         return true;
949 }
950
951 void
952 AutomationTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
953 {
954         list<boost::shared_ptr<AutomationLine> > lines;
955         if (_line) {
956                 lines.push_back (_line);
957         } else if (_view) {
958                 lines = _view->get_lines ();
959         }
960
961         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
962                 cut_copy_clear_one (**i, selection, op);
963         }
964 }
965
966 void
967 AutomationTimeAxisView::cut_copy_clear_one (AutomationLine& line, Selection& selection, CutCopyOp op)
968 {
969         boost::shared_ptr<Evoral::ControlList> what_we_got;
970         boost::shared_ptr<AutomationList> alist (line.the_list());
971
972         XMLNode &before = alist->get_state();
973
974         /* convert time selection to automation list model coordinates */
975         const Evoral::TimeConverter<double, ARDOUR::framepos_t>& tc = line.time_converter ();
976         double const start = tc.from (selection.time.front().start - tc.origin_b ());
977         double const end = tc.from (selection.time.front().end - tc.origin_b ());
978
979         switch (op) {
980         case Delete:
981                 if (alist->cut (start, end) != 0) {
982                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
983                 }
984                 break;
985
986         case Cut:
987
988                 if ((what_we_got = alist->cut (start, end)) != 0) {
989                         _editor.get_cut_buffer().add (what_we_got);
990                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
991                 }
992                 break;
993         case Copy:
994                 if ((what_we_got = alist->copy (start, end)) != 0) {
995                         _editor.get_cut_buffer().add (what_we_got);
996                 }
997                 break;
998
999         case Clear:
1000                 if ((what_we_got = alist->cut (start, end)) != 0) {
1001                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1002                 }
1003                 break;
1004         }
1005
1006         if (what_we_got) {
1007                 for (AutomationList::iterator x = what_we_got->begin(); x != what_we_got->end(); ++x) {
1008                         double when = (*x)->when;
1009                         double val  = (*x)->value;
1010                         line.model_to_view_coord (when, val);
1011                         (*x)->when = when;
1012                         (*x)->value = val;
1013                 }
1014         }
1015 }