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