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