Update comments & icon of rubberband example Lua script
[ardour.git] / gtk2_ardour / vca_time_axis.cc
1 /*
2  * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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->MonitorBusAddedOrRemoved.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::set_button_names, this), gui_context());
117
118         s->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::parameter_changed, this, _1), gui_context());
119         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::parameter_changed, this, _1), gui_context());
120         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VCATimeAxisView::parameter_changed));
121 }
122
123 VCATimeAxisView::~VCATimeAxisView ()
124 {
125         delete automation_action_menu;
126         CatchDeletion (this);
127 }
128
129 void
130 VCATimeAxisView::self_delete ()
131 {
132         /* reset reference immediately rather than deferring to idle */
133         _vca.reset ();
134         delete_when_idle (this);
135 }
136
137 void
138 VCATimeAxisView::parameter_changed (std::string const & p)
139 {
140         if (p == "track-name-number") {
141                 update_track_number_visibility();
142         } else if (p == "solo-control-is-listen-control" || p == "listen-position") {
143                 set_button_names ();
144         }
145 }
146
147 bool
148 VCATimeAxisView::solo_release (GdkEventButton*)
149 {
150         /* We use NoGroup because VCA controls are never part of a group. This
151            is redundant, but clear.
152         */
153         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
154         return true;
155 }
156
157 bool
158 VCATimeAxisView::mute_release (GdkEventButton*)
159 {
160         /* We use NoGroup because VCA controls are never part of a group. This
161            is redundant, but clear.
162         */
163         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
164         return true;
165 }
166
167 void
168 VCATimeAxisView::set_vca (boost::shared_ptr<VCA> v)
169 {
170         StripableTimeAxisView::set_stripable (v);
171         _vca = v;
172
173         gain_meter.set_controls (boost::shared_ptr<Route>(),
174                                  boost::shared_ptr<PeakMeter>(),
175                                  boost::shared_ptr<Amp>(),
176                                  _vca->gain_control());
177
178         // Mixer_UI::instance()->show_vca_change.connect (sigc::mem_fun (*this, &VCAMasterStrip::spill_change));
179
180         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::vca_property_changed, this, _1), gui_context());
181
182         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::update_solo_display, this), gui_context());
183         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::update_mute_display, this), gui_context());
184         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::self_delete, this), gui_context());
185
186         solo_button.set_controllable (_vca->solo_control());
187         mute_button.set_controllable (_vca->mute_control());
188
189         /* VCA number never changes */
190         number_label.set_text (PBD::to_string (_vca->number()));
191
192         set_height (preset_height (HeightNormal));
193
194         if (automation_child (GainAutomation) == 0) {
195                 create_automation_child (GainAutomation, false);
196         }
197         if (automation_child (MuteAutomation) == 0) {
198                 create_automation_child (MuteAutomation, false);
199         }
200
201         update_vca_name ();
202         set_button_names ();
203         update_solo_display ();
204         update_mute_display ();
205         update_track_number_visibility ();
206 }
207
208 void
209 VCATimeAxisView::vca_property_changed (PropertyChange const & what_changed)
210 {
211         if (what_changed.contains (ARDOUR::Properties::name)) {
212                 update_vca_name ();
213         }
214 }
215
216 void
217 VCATimeAxisView::update_vca_name ()
218 {
219         name_label.set_text (_vca->full_name());
220 }
221
222 bool
223 VCATimeAxisView::name_entry_changed (std::string const& str)
224 {
225         _vca->set_name (str);
226         return true;
227 }
228
229 void
230 VCATimeAxisView::update_mute_display ()
231 {
232         if (_vca->mute_control()->muted_by_self()) {
233                 mute_button.set_active_state (ExplicitActive);
234         } else if (_vca->mute_control()->muted_by_masters ()) {
235                 mute_button.set_active_state (ImplicitActive);
236         } else {
237                 mute_button.set_active_state (Gtkmm2ext::Off);
238         }
239 }
240
241 void
242 VCATimeAxisView::update_solo_display ()
243 {
244         if (_vca->solo_control()->self_soloed()) {
245                 solo_button.set_active_state (ExplicitActive);
246         } else if (_vca->solo_control()->soloed_by_masters ()) {
247                 solo_button.set_active_state (ImplicitActive);
248         } else {
249                 solo_button.set_active_state (Gtkmm2ext::Off);
250         }
251
252         update_mute_display ();
253 }
254
255 std::string
256 VCATimeAxisView::name() const
257 {
258         return _vca->name();
259 }
260
261 std::string
262 VCATimeAxisView::state_id() const
263 {
264         return string_compose ("vtv %1", _vca->id().to_s());
265 }
266
267 void
268 VCATimeAxisView::set_button_names ()
269 {
270         if (Config->get_solo_control_is_listen_control()) {
271                 switch (Config->get_listen_position()) {
272                 case AfterFaderListen:
273                         solo_button.set_text (S_("AfterFader|A"));
274                         set_tooltip (solo_button, _("After-fade listen (AFL)"));
275                         break;
276                 case PreFaderListen:
277                         solo_button.set_text (S_("PreFader|P"));
278                         set_tooltip (solo_button, _("Pre-fade listen (PFL)"));
279                         break;
280                 }
281         } else {
282                 solo_button.set_text (S_("Solo|S"));
283                 set_tooltip (solo_button, _("Solo"));
284         }
285 }
286
287 void
288 VCATimeAxisView::update_track_number_visibility ()
289 {
290         DisplaySuspender ds;
291         bool show_label = _session->config.get_track_name_number();
292
293         if (number_label.get_parent()) {
294                 controls_table.remove (number_label);
295         }
296
297         if (show_label) {
298                 if (ARDOUR::Profile->get_mixbus()) {
299                         controls_table.attach (number_label, 3, 4, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 1, 0);
300                 } else {
301                         controls_table.attach (number_label, 0, 1, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 1, 0);
302                 }
303
304                 // see ArdourButton::on_size_request(), we should probably use a global size-group here instead.
305                 // except the width of the number label is subtracted from the name-hbox, so we
306                 // need to explictly calculate it anyway until the name-label & entry become ArdourWidgets.
307
308                 int tnw = (2 + std::max(2u, _session->track_number_decimals())) * number_label.char_pixel_width();
309                 if (tnw & 1) --tnw;
310                 number_label.set_size_request(tnw, -1);
311                 number_label.show ();
312         } else {
313                 number_label.hide ();
314         }
315 }
316
317 bool
318 VCATimeAxisView::automation_click (GdkEventButton* ev)
319 {
320         if (ev->button != 1) {
321                 return true;
322         }
323
324         conditionally_add_to_selection ();
325         build_automation_action_menu (false);
326         Gtkmm2ext::anchored_menu_popup (automation_action_menu, &automation_button, "", 1, ev->time);
327         return true;
328 }
329
330 bool
331 VCATimeAxisView::drop_release (GdkEventButton*)
332 {
333         _vca->Drop (); /* EMIT SIGNAL */
334
335         return true;
336 }
337
338 PresentationInfo const &
339 VCATimeAxisView::presentation_info () const
340 {
341         return _vca->presentation_info();
342 }
343
344 boost::shared_ptr<Stripable>
345 VCATimeAxisView::stripable () const
346 {
347         return _vca;
348 }
349
350 Gdk::Color
351 VCATimeAxisView::color () const
352 {
353         return ARDOUR_UI_UTILS::gdk_color_from_rgb (_vca->presentation_info().color ());
354 }
355
356 void
357 VCATimeAxisView::set_height (uint32_t h, TrackHeightMode m)
358 {
359         TimeAxisView::set_height (h, m);
360         if (height >= preset_height (HeightNormal)) {
361                 drop_button.show ();
362                 automation_button.show ();
363                 gain_meter.get_gain_slider().show ();
364         } else {
365                 drop_button.hide ();
366                 automation_button.hide ();
367                 gain_meter.get_gain_slider().hide ();
368         }
369
370         set_gui_property ("height", h);
371         _vca->gui_changed ("track_height", (void*) 0); /* EMIT SIGNAL */
372 }
373
374 bool
375 VCATimeAxisView::marked_for_display () const
376 {
377         return _vca && !_vca->presentation_info().hidden();
378 }
379
380 bool
381 VCATimeAxisView::set_marked_for_display (bool yn)
382 {
383         if (_vca && (yn == _vca->presentation_info().hidden())) {
384                 _vca->presentation_info().set_hidden (!yn);
385                 return true; // things changed
386         }
387         return false;
388 }
389
390 void
391 VCATimeAxisView::create_gain_automation_child (const Evoral::Parameter& param, bool show)
392 {
393         boost::shared_ptr<AutomationControl> c = _vca->gain_control();
394         if (!c) {
395                 error << "VCA has no gain automation, unable to add automation track view." << endmsg;
396                 return;
397         }
398
399         gain_track.reset (new AutomationTimeAxisView (_session,
400                                                       _vca, boost::shared_ptr<Automatable> (), c, param,
401                                                       _editor,
402                                                       *this,
403                                                       false,
404                                                       parent_canvas,
405                                                       /*_route->amp()->describe_parameter(param)*/"Fader"));
406
407         add_automation_child (Evoral::Parameter(GainAutomation), gain_track, show);
408 }
409
410 void
411 VCATimeAxisView::create_mute_automation_child (const Evoral::Parameter& param, bool show)
412 {
413         boost::shared_ptr<AutomationControl> c = _vca->mute_control();
414         if (!c) {
415                 error << "VCA has no mute automation, unable to add automation track view." << endmsg;
416                 return;
417         }
418
419         mute_track.reset (new AutomationTimeAxisView (_session,
420                                                       _vca, boost::shared_ptr<Automatable> (), c, param,
421                                                       _editor,
422                                                       *this,
423                                                       false,
424                                                       parent_canvas,
425                                                       /*_route->describe_parameter(param)*/ "Mute"));
426
427         add_automation_child (Evoral::Parameter(MuteAutomation), mute_track, show);
428 }
429
430 void
431 VCATimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
432 {
433         switch (param.type()) {
434                 case GainAutomation:
435                         create_gain_automation_child (param, show);
436                         break;
437                 case MuteAutomation:
438                         create_mute_automation_child (param, show);
439                         break;
440                 default:
441                         break;
442         }
443 }
444
445 void
446 VCATimeAxisView::build_display_menu ()
447 {
448         using namespace Menu_Helpers;
449         /* prepare it */
450         TimeAxisView::build_display_menu ();
451
452         MenuList& items = display_menu->items();
453         items.push_back (MenuElem (_("Color..."), sigc::mem_fun (*this, &VCATimeAxisView::choose_color)));
454         if (_size_menu) {
455                 detach_menu (*_size_menu);
456         }
457         build_size_menu ();
458         items.push_back (MenuElem (_("Height"), *_size_menu));
459         items.push_back (SeparatorElem());
460
461         build_automation_action_menu (true);
462         items.push_back (MenuElem (_("Automation"), *automation_action_menu));
463
464         items.push_back (SeparatorElem());
465         items.push_back (MenuElem (_("Drop All Slaves"), sigc::mem_fun (*this, &VCATimeAxisView::drop_all_slaves)));
466         items.push_back (SeparatorElem());
467         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(_editor, &PublicEditor::remove_tracks)));
468 }
469
470
471 void
472 VCATimeAxisView::build_automation_action_menu (bool for_selection)
473 {
474         using namespace Menu_Helpers;
475         _main_automation_menu_map.clear ();
476         delete automation_action_menu;
477         automation_action_menu = new Menu;
478
479         MenuList& items = automation_action_menu->items();
480
481         automation_action_menu->set_name ("ArdourContextMenu");
482
483         items.push_back (MenuElem (_("Show All Automation"),
484                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::show_all_automation), for_selection)));
485
486         items.push_back (MenuElem (_("Show Existing Automation"),
487                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::show_existing_automation), for_selection)));
488
489         items.push_back (MenuElem (_("Hide All Automation"),
490                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::hide_all_automation), for_selection)));
491
492         if (gain_track) {
493                 items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &VCATimeAxisView::update_gain_track_visibility)));
494                 gain_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
495                 gain_automation_item->set_active (string_to<bool>(gain_track->gui_property ("visible")));
496
497                 _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
498         }
499
500         if (trim_track) {
501                 items.push_back (CheckMenuElem (_("Trim"), sigc::mem_fun (*this, &VCATimeAxisView::update_trim_track_visibility)));
502                 trim_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
503                 trim_automation_item->set_active (string_to<bool>(trim_track->gui_property ("visible")));
504
505                 _main_automation_menu_map[Evoral::Parameter(TrimAutomation)] = trim_automation_item;
506         }
507
508         if (mute_track) {
509                 items.push_back (CheckMenuElem (_("Mute"), sigc::mem_fun (*this, &VCATimeAxisView::update_mute_track_visibility)));
510                 mute_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
511                 mute_automation_item->set_active (string_to<bool>(mute_track->gui_property ("visible")));
512
513                 _main_automation_menu_map[Evoral::Parameter(MuteAutomation)] = mute_automation_item;
514         }
515 }
516
517 void
518 VCATimeAxisView::show_all_automation (bool apply_to_selection)
519 {
520         assert (!apply_to_selection); // VCAs can't yet be selected
521         no_redraw = true;
522
523         StripableTimeAxisView::show_all_automation ();
524
525         no_redraw = false;
526         request_redraw ();
527 }
528
529 void
530 VCATimeAxisView::show_existing_automation (bool apply_to_selection)
531 {
532         assert (!apply_to_selection); // VCAs can't yet be selected
533         no_redraw = true;
534
535         StripableTimeAxisView::show_existing_automation ();
536
537         no_redraw = false;
538         request_redraw ();
539 }
540
541 void
542 VCATimeAxisView::hide_all_automation (bool apply_to_selection)
543 {
544         assert (!apply_to_selection); // VCAs can't yet be selected
545         no_redraw = true;
546
547         StripableTimeAxisView::hide_all_automation ();
548
549         no_redraw = false;
550         request_redraw ();
551 }
552
553 void
554 VCATimeAxisView::drop_all_slaves ()
555 {
556         _vca->Drop (); /* EMIT SIGNAL */
557
558         if (Mixer_UI::instance()->showing_spill_for (_vca)) {
559                 Mixer_UI::instance()->show_spill (boost::shared_ptr<Stripable>());
560         }
561 }
562
563 void
564 VCATimeAxisView::choose_color () {
565         _color_picker.popup (_vca);
566 }