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