Optimize automation-event process splitting
[ardour.git] / gtk2_ardour / vca_time_axis.cc
1 /*
2     Copyright (C) 2016 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 <gtkmm/menu.h>
21
22 #include "pbd/string_convert.h"
23
24 #include "ardour/mute_control.h"
25 #include "ardour/profile.h"
26 #include "ardour/session.h"
27 #include "ardour/solo_control.h"
28 #include "ardour/vca.h"
29
30 #include "gtkmm2ext/doi.h"
31 #include "gtkmm2ext/utils.h"
32 #include "widgets/tooltips.h"
33
34 #include "gui_thread.h"
35 #include "public_editor.h"
36 #include "mixer_ui.h"
37 #include "ui_config.h"
38 #include "vca_time_axis.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace ARDOUR;
43 using namespace ArdourWidgets;
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46 using namespace PBD;
47
48 VCATimeAxisView::VCATimeAxisView (PublicEditor& ed, Session* s, ArdourCanvas::Canvas& canvas)
49         : SessionHandlePtr (s)
50         , StripableTimeAxisView (ed, s, canvas)
51         , gain_meter (s, true, 75, 14) // XXX stupid magic numbers, match sizes in RouteTimeAxisView
52         , automation_action_menu (0)
53 {
54
55         controls_base_selected_name = X_("ControlMasterBaseSelected");
56         controls_base_unselected_name = X_("ControlMasterBaseUnselected");
57
58         solo_button.set_name ("solo button");
59         set_tooltip (solo_button, _("Solo slaves"));
60         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::solo_release), false);
61         solo_button.unset_flags (Gtk::CAN_FOCUS);
62
63         mute_button.set_name ("mute button");
64         mute_button.set_text (S_("Mute|M"));
65         set_tooltip (mute_button, _("Mute slaves"));
66         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::mute_release), false);
67         mute_button.unset_flags (Gtk::CAN_FOCUS);
68
69         drop_button.set_name ("mute button");
70         drop_button.set_text (S_("VCA|D"));
71         set_tooltip (drop_button, _("Unassign all slaves"));
72         drop_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::drop_release), false);
73         drop_button.unset_flags (Gtk::CAN_FOCUS);
74
75         automation_button.set_name ("route button");
76         automation_button.set_text (S_("RTAV|A"));
77         set_tooltip (automation_button, _("Automation"));
78         automation_button.signal_button_press_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::automation_click), false);
79         automation_button.unset_flags (Gtk::CAN_FOCUS);
80
81         mute_button.set_tweaks(ArdourButton::TrackHeader);
82         solo_button.set_tweaks(ArdourButton::TrackHeader);
83         drop_button.set_tweaks(ArdourButton::TrackHeader);
84         automation_button.set_tweaks(ArdourButton::TrackHeader);
85
86         if (ARDOUR::Profile->get_mixbus()) {
87                 controls_button_size_group->add_widget(mute_button);
88
89                 Gtk::Fixed *blank = manage(new Gtk::Fixed());
90                 controls_button_size_group->add_widget(*blank);
91                 controls_table.attach (*blank, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
92                 blank->show();
93
94                 controls_table.attach (mute_button, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
95                 controls_table.attach (solo_button, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
96                 controls_table.attach (automation_button, 1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
97                 controls_table.attach (drop_button, 2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
98                 controls_table.attach (gain_meter.get_gain_slider(), 3, 5, 2, 3, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 1, 0);
99         } else {
100                 controls_table.attach (mute_button, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
101                 controls_table.attach (solo_button, 3, 4, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
102                 controls_table.attach (automation_button, 2, 3, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
103                 controls_table.attach (drop_button, 3, 4, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
104                 controls_table.attach (gain_meter.get_gain_slider(), 0, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 1, 0);
105         }
106
107         mute_button.show ();
108         solo_button.show ();
109         drop_button.show ();
110         automation_button.show ();
111         gain_meter.get_gain_slider().show ();
112
113         controls_ebox.set_name (controls_base_unselected_name);
114         time_axis_frame.set_name (controls_base_unselected_name);
115
116         s->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::parameter_changed, this, _1), gui_context());
117         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::parameter_changed, this, _1), gui_context());
118         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VCATimeAxisView::parameter_changed));
119 }
120
121 VCATimeAxisView::~VCATimeAxisView ()
122 {
123 }
124
125 void
126 VCATimeAxisView::self_delete ()
127 {
128         /* reset reference immediately rather than deferring to idle */
129         _vca.reset ();
130         delete_when_idle (this);
131 }
132
133 void
134 VCATimeAxisView::parameter_changed (std::string const & p)
135 {
136         if (p == "track-name-number") {
137                 update_track_number_visibility();
138         } else if (p == "use-monitor-bus" || p == "solo-control-is-listen-control" || p == "listen-position") {
139                 set_button_names ();
140         }
141 }
142
143 bool
144 VCATimeAxisView::solo_release (GdkEventButton*)
145 {
146         /* We use NoGroup because VCA controls are never part of a group. This
147            is redundant, but clear.
148         */
149         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
150         return true;
151 }
152
153 bool
154 VCATimeAxisView::mute_release (GdkEventButton*)
155 {
156         /* We use NoGroup because VCA controls are never part of a group. This
157            is redundant, but clear.
158         */
159         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
160         return true;
161 }
162
163 void
164 VCATimeAxisView::set_vca (boost::shared_ptr<VCA> v)
165 {
166         StripableTimeAxisView::set_stripable (v);
167         _vca = v;
168
169         gain_meter.set_controls (boost::shared_ptr<Route>(),
170                                  boost::shared_ptr<PeakMeter>(),
171                                  boost::shared_ptr<Amp>(),
172                                  _vca->gain_control());
173
174         // Mixer_UI::instance()->show_vca_change.connect (sigc::mem_fun (*this, &VCAMasterStrip::spill_change));
175
176         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::vca_property_changed, this, _1), gui_context());
177
178         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::update_solo_display, this), gui_context());
179         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::update_mute_display, this), gui_context());
180         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::self_delete, this), gui_context());
181
182         solo_button.set_controllable (_vca->solo_control());
183         mute_button.set_controllable (_vca->mute_control());
184
185         /* VCA number never changes */
186         number_label.set_text (PBD::to_string (_vca->number()));
187
188         set_height (preset_height (HeightNormal));
189
190         if (automation_child (GainAutomation) == 0) {
191                 create_automation_child (GainAutomation, false);
192         }
193         if (automation_child (MuteAutomation) == 0) {
194                 create_automation_child (MuteAutomation, false);
195         }
196
197         update_vca_name ();
198         set_button_names ();
199         update_solo_display ();
200         update_mute_display ();
201         update_track_number_visibility ();
202 }
203
204 void
205 VCATimeAxisView::vca_property_changed (PropertyChange const & what_changed)
206 {
207         if (what_changed.contains (ARDOUR::Properties::name)) {
208                 update_vca_name ();
209         }
210 }
211
212 void
213 VCATimeAxisView::update_vca_name ()
214 {
215         name_label.set_text (_vca->full_name());
216 }
217
218 bool
219 VCATimeAxisView::name_entry_changed (std::string const& str)
220 {
221         _vca->set_name (str);
222         return true;
223 }
224
225 void
226 VCATimeAxisView::update_mute_display ()
227 {
228         if (_vca->mute_control()->muted_by_self()) {
229                 mute_button.set_active_state (ExplicitActive);
230         } else if (_vca->mute_control()->muted_by_masters ()) {
231                 mute_button.set_active_state (ImplicitActive);
232         } else {
233                 mute_button.set_active_state (Gtkmm2ext::Off);
234         }
235 }
236
237 void
238 VCATimeAxisView::update_solo_display ()
239 {
240         if (_vca->solo_control()->self_soloed()) {
241                 solo_button.set_active_state (ExplicitActive);
242         } else if (_vca->solo_control()->soloed_by_masters ()) {
243                 solo_button.set_active_state (ImplicitActive);
244         } else {
245                 solo_button.set_active_state (Gtkmm2ext::Off);
246         }
247
248         update_mute_display ();
249 }
250
251 std::string
252 VCATimeAxisView::name() const
253 {
254         return _vca->name();
255 }
256
257 std::string
258 VCATimeAxisView::state_id() const
259 {
260         return string_compose ("vtv %1", _vca->id().to_s());
261 }
262
263 void
264 VCATimeAxisView::set_button_names ()
265 {
266         if (Config->get_solo_control_is_listen_control()) {
267                 switch (Config->get_listen_position()) {
268                 case AfterFaderListen:
269                         solo_button.set_text (S_("AfterFader|A"));
270                         set_tooltip (solo_button, _("After-fade listen (AFL)"));
271                         break;
272                 case PreFaderListen:
273                         solo_button.set_text (S_("PreFader|P"));
274                         set_tooltip (solo_button, _("Pre-fade listen (PFL)"));
275                         break;
276                 }
277         } else {
278                 solo_button.set_text (S_("Solo|S"));
279                 set_tooltip (solo_button, _("Solo"));
280         }
281 }
282
283 void
284 VCATimeAxisView::update_track_number_visibility ()
285 {
286         DisplaySuspender ds;
287         bool show_label = _session->config.get_track_name_number();
288
289         if (number_label.get_parent()) {
290                 controls_table.remove (number_label);
291         }
292
293         if (show_label) {
294                 if (ARDOUR::Profile->get_mixbus()) {
295                         controls_table.attach (number_label, 3, 4, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 1, 0);
296                 } else {
297                         controls_table.attach (number_label, 0, 1, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 1, 0);
298                 }
299
300                 // see ArdourButton::on_size_request(), we should probably use a global size-group here instead.
301                 // except the width of the number label is subtracted from the name-hbox, so we
302                 // need to explictly calculate it anyway until the name-label & entry become ArdourWidgets.
303
304                 int tnw = (2 + std::max(2u, _session->track_number_decimals())) * number_label.char_pixel_width();
305                 if (tnw & 1) --tnw;
306                 number_label.set_size_request(tnw, -1);
307                 number_label.show ();
308         } else {
309                 number_label.hide ();
310         }
311 }
312
313 bool
314 VCATimeAxisView::automation_click (GdkEventButton* ev)
315 {
316         if (ev->button != 1) {
317                 return true;
318         }
319
320         conditionally_add_to_selection ();
321         build_automation_action_menu (false);
322         Gtkmm2ext::anchored_menu_popup (automation_action_menu, &automation_button, "", 1, ev->time);
323         return true;
324 }
325
326 bool
327 VCATimeAxisView::drop_release (GdkEventButton*)
328 {
329         _vca->Drop (); /* EMIT SIGNAL */
330
331         return true;
332 }
333
334 PresentationInfo const &
335 VCATimeAxisView::presentation_info () const
336 {
337         return _vca->presentation_info();
338 }
339
340 boost::shared_ptr<Stripable>
341 VCATimeAxisView::stripable () const
342 {
343         return _vca;
344 }
345
346 Gdk::Color
347 VCATimeAxisView::color () const
348 {
349         return ARDOUR_UI_UTILS::gdk_color_from_rgb (_vca->presentation_info().color ());
350 }
351
352 void
353 VCATimeAxisView::set_height (uint32_t h, TrackHeightMode m)
354 {
355         TimeAxisView::set_height (h, m);
356         if (height >= preset_height (HeightNormal)) {
357                 drop_button.show ();
358                 automation_button.show ();
359                 gain_meter.get_gain_slider().show ();
360         } else {
361                 drop_button.hide ();
362                 automation_button.hide ();
363                 gain_meter.get_gain_slider().hide ();
364         }
365
366         set_gui_property ("height", h);
367         _vca->gui_changed ("track_height", (void*) 0); /* EMIT SIGNAL */
368 }
369
370 bool
371 VCATimeAxisView::marked_for_display () const
372 {
373         return _vca && !_vca->presentation_info().hidden();
374 }
375
376 bool
377 VCATimeAxisView::set_marked_for_display (bool yn)
378 {
379         if (_vca && (yn == _vca->presentation_info().hidden())) {
380                 _vca->presentation_info().set_hidden (!yn);
381                 return true; // things changed
382         }
383         return false;
384 }
385
386 void
387 VCATimeAxisView::create_gain_automation_child (const Evoral::Parameter& param, bool show)
388 {
389         boost::shared_ptr<AutomationControl> c = _vca->gain_control();
390         if (!c) {
391                 error << "VCA has no gain automation, unable to add automation track view." << endmsg;
392                 return;
393         }
394
395         gain_track.reset (new AutomationTimeAxisView (_session,
396                                                       _vca, boost::shared_ptr<Automatable> (), c, param,
397                                                       _editor,
398                                                       *this,
399                                                       false,
400                                                       parent_canvas,
401                                                       /*_route->amp()->describe_parameter(param)*/"Fader"));
402
403         add_automation_child (Evoral::Parameter(GainAutomation), gain_track, show);
404 }
405
406 void
407 VCATimeAxisView::create_mute_automation_child (const Evoral::Parameter& param, bool show)
408 {
409         boost::shared_ptr<AutomationControl> c = _vca->mute_control();
410         if (!c) {
411                 error << "VCA has no mute automation, unable to add automation track view." << endmsg;
412                 return;
413         }
414
415         mute_track.reset (new AutomationTimeAxisView (_session,
416                                                       _vca, boost::shared_ptr<Automatable> (), c, param,
417                                                       _editor,
418                                                       *this,
419                                                       false,
420                                                       parent_canvas,
421                                                       /*_route->describe_parameter(param)*/ "Mute"));
422
423         add_automation_child (Evoral::Parameter(MuteAutomation), mute_track, show);
424 }
425
426 void
427 VCATimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
428 {
429         switch (param.type()) {
430                 case GainAutomation:
431                         create_gain_automation_child (param, show);
432                         break;
433                 case MuteAutomation:
434                         create_mute_automation_child (param, show);
435                         break;
436                 default:
437                         break;
438         }
439 }
440
441 void
442 VCATimeAxisView::build_display_menu ()
443 {
444         using namespace Menu_Helpers;
445         /* prepare it */
446         TimeAxisView::build_display_menu ();
447
448         MenuList& items = display_menu->items();
449         items.push_back (MenuElem (_("Color..."), sigc::mem_fun (*this, &VCATimeAxisView::choose_color)));
450         if (_size_menu) {
451                 detach_menu (*_size_menu);
452         }
453         build_size_menu ();
454         items.push_back (MenuElem (_("Height"), *_size_menu));
455         items.push_back (SeparatorElem());
456
457         build_automation_action_menu (true);
458         items.push_back (MenuElem (_("Automation"), *automation_action_menu));
459
460         items.push_back (SeparatorElem());
461         items.push_back (MenuElem (_("Drop All Slaves"), sigc::mem_fun (*this, &VCATimeAxisView::drop_all_slaves)));
462         items.push_back (SeparatorElem());
463         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(_editor, &PublicEditor::remove_tracks)));
464 }
465
466
467 void
468 VCATimeAxisView::build_automation_action_menu (bool for_selection)
469 {
470         using namespace Menu_Helpers;
471         _main_automation_menu_map.clear ();
472         delete automation_action_menu;
473         automation_action_menu = new Menu;
474
475         MenuList& items = automation_action_menu->items();
476
477         automation_action_menu->set_name ("ArdourContextMenu");
478
479         items.push_back (MenuElem (_("Show All Automation"),
480                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::show_all_automation), for_selection)));
481
482         items.push_back (MenuElem (_("Show Existing Automation"),
483                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::show_existing_automation), for_selection)));
484
485         items.push_back (MenuElem (_("Hide All Automation"),
486                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::hide_all_automation), for_selection)));
487
488         if (gain_track) {
489                 items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &VCATimeAxisView::update_gain_track_visibility)));
490                 gain_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
491                 gain_automation_item->set_active (string_to<bool>(gain_track->gui_property ("visible")));
492
493                 _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
494         }
495
496         if (trim_track) {
497                 items.push_back (CheckMenuElem (_("Trim"), sigc::mem_fun (*this, &VCATimeAxisView::update_trim_track_visibility)));
498                 trim_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
499                 trim_automation_item->set_active (string_to<bool>(trim_track->gui_property ("visible")));
500
501                 _main_automation_menu_map[Evoral::Parameter(TrimAutomation)] = trim_automation_item;
502         }
503
504         if (mute_track) {
505                 items.push_back (CheckMenuElem (_("Mute"), sigc::mem_fun (*this, &VCATimeAxisView::update_mute_track_visibility)));
506                 mute_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
507                 mute_automation_item->set_active (string_to<bool>(mute_track->gui_property ("visible")));
508
509                 _main_automation_menu_map[Evoral::Parameter(MuteAutomation)] = mute_automation_item;
510         }
511 }
512
513 void
514 VCATimeAxisView::show_all_automation (bool apply_to_selection)
515 {
516         assert (!apply_to_selection); // VCAs can't yet be selected
517         no_redraw = true;
518
519         StripableTimeAxisView::show_all_automation ();
520
521         no_redraw = false;
522         request_redraw ();
523 }
524
525 void
526 VCATimeAxisView::show_existing_automation (bool apply_to_selection)
527 {
528         assert (!apply_to_selection); // VCAs can't yet be selected
529         no_redraw = true;
530
531         StripableTimeAxisView::show_existing_automation ();
532
533         no_redraw = false;
534         request_redraw ();
535 }
536
537 void
538 VCATimeAxisView::hide_all_automation (bool apply_to_selection)
539 {
540         assert (!apply_to_selection); // VCAs can't yet be selected
541         no_redraw = true;
542
543         StripableTimeAxisView::hide_all_automation ();
544
545         no_redraw = false;
546         request_redraw ();
547 }
548
549 void
550 VCATimeAxisView::drop_all_slaves ()
551 {
552         _vca->Drop (); /* EMIT SIGNAL */
553
554         if (Mixer_UI::instance()->showing_spill_for (_vca)) {
555                 Mixer_UI::instance()->show_spill (boost::shared_ptr<Stripable>());
556         }
557 }
558
559 void
560 VCATimeAxisView::choose_color () {
561         _color_picker.popup (_vca);
562 }