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