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