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