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