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