drop reference to VCA from VCAMasterStrip immediately rather than waiting for idle
[ardour.git] / gtk2_ardour / vca_master_strip.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 #include <gtkmm/stock.h>
20
21 #include "pbd/convert.h"
22
23 #include "ardour/rc_configuration.h"
24 #include "ardour/session.h"
25 #include "ardour/vca.h"
26 #include "ardour/vca_manager.h"
27
28 #include "gtkmm2ext/doi.h"
29 #include "gtkmm2ext/keyboard.h"
30
31 #include "ardour_dialog.h"
32 #include "floating_text_entry.h"
33 #include "gui_thread.h"
34 #include "mixer_ui.h"
35 #include "tooltips.h"
36 #include "ui_config.h"
37 #include "vca_master_strip.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace ARDOUR_UI_UTILS;
43 using namespace Gtkmm2ext;
44 using namespace Gtk;
45 using namespace PBD;
46 using std::string;
47
48 PBD::Signal1<void,VCAMasterStrip*> VCAMasterStrip::CatchDeletion;
49
50 VCAMasterStrip::VCAMasterStrip (Session* s, boost::shared_ptr<VCA> v)
51         : SessionHandlePtr (s)
52         , _vca (v)
53         , gain_meter (s, 250)
54         , context_menu (0)
55         , delete_dialog (0)
56 {
57         gain_meter.set_controls (boost::shared_ptr<Route>(),
58                                  boost::shared_ptr<PeakMeter>(),
59                                  boost::shared_ptr<Amp>(),
60                                  _vca->gain_control());
61
62         solo_button.set_name ("solo button");
63         set_tooltip (solo_button, _("Solo slaves"));
64         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::solo_release), false);
65
66         mute_button.set_name ("mute button");
67         mute_button.set_text (_("M"));
68         set_tooltip (mute_button, _("Mute slaves"));
69         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::mute_release), false);
70
71         hide_button.set_icon (ArdourIcon::CloseCross);
72         set_tooltip (&hide_button, _("Hide this VCA strip"));
73
74         assign_button.set_name (X_("vca assign"));
75         set_tooltip (assign_button, _("Click to assign a VCA Master to this VCA"));
76         assign_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vca_button_release), false);
77
78         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &VCAMasterStrip::hide_clicked));
79
80         width_hide_box.pack_start (number_label, true, true);
81         width_hide_box.pack_end (hide_button, false, true);
82
83         solo_mute_box.set_spacing (2);
84         solo_mute_box.pack_start (mute_button, true, true);
85         solo_mute_box.pack_start (solo_button, true, true);
86
87         number_label.set_text (to_string (v->number(), std::dec));
88         number_label.set_elements((ArdourButton::Element)(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text|ArdourButton::Inactive));
89         number_label.set_no_show_all ();
90         number_label.set_name ("generic button");
91         number_label.set_alignment (.5, .5);
92         number_label.set_fallthrough_to_parent (true);
93
94         top_padding.set_size_request (-1, 16); /* must match height in GroupTabs::set_size_request() */
95         bottom_padding.set_size_request (-1, 30); /* this one is a hack. there's no trivial way to compute it */
96
97         //Glib::RefPtr<Pango::Layout> layout = vertical_button.get_layout ();
98         // layout->set_justify (JUSTIFY_CENTER);
99         /* horizontally centered, with a little space (5%) at the top */
100         vertical_button.set_angle (90);
101         vertical_button.set_layout_font (UIConfiguration::instance().get_NormalBoldFont());
102         vertical_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vertical_button_press));
103         vertical_button.set_fallthrough_to_parent (true);
104         set_tooltip (vertical_button, _("Click to show slaves only")); /* tooltip updated dynamically */
105
106         drop_button.set_text(_("drop"));
107         drop_button.signal_clicked.connect (sigc::mem_fun (*this, &VCAMasterStrip::drop_button_press));
108         set_tooltip (drop_button, _("Unassign all slaves from this control master"));
109
110         global_vpacker.set_border_width (1);
111         global_vpacker.set_spacing (0);
112
113         global_vpacker.pack_start (top_padding, false, false);
114         global_vpacker.pack_start (width_hide_box, false, false);
115         global_vpacker.pack_start (vertical_button, true, true);
116         global_vpacker.pack_start (solo_mute_box, false, false);
117         global_vpacker.pack_start (gain_meter, false, false, 2);
118         global_vpacker.pack_start (assign_button, false, false);
119         global_vpacker.pack_start (drop_button, false, false);
120         global_vpacker.pack_start (bottom_padding, false, false);
121
122         global_frame.add (global_vpacker);
123         global_frame.set_shadow_type (Gtk::SHADOW_IN);
124         global_frame.set_name ("BaseFrame");
125
126         add (global_frame);
127
128         global_vpacker.show ();
129         global_frame.show ();
130         top_padding.show ();
131         bottom_padding.show ();
132         vertical_button.show ();
133         hide_button.show ();
134         number_label.show ();
135         width_hide_box.show ();
136         gain_meter.show ();
137         solo_mute_box.show_all ();
138         assign_button.show ();
139         drop_button.show ();
140
141         /* force setting of visible selected status */
142
143         _selected = true;
144         set_selected (false);
145         set_solo_text ();
146         update_vca_display ();
147         update_vca_name ();
148         solo_changed ();
149         mute_changed ();
150         spill_change (boost::shared_ptr<VCA>());
151
152         Mixer_UI::instance()->show_vca_change.connect (sigc::mem_fun (*this, &VCAMasterStrip::spill_change));
153
154         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
155
156         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::solo_changed, this), gui_context());
157         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::mute_changed, this), gui_context());
158
159         /* only need to connect to one of these to update VCA status */
160
161         _vca->gain_control()->MasterStatusChange.connect (vca_connections,
162                                                   invalidator (*this),
163                                                   boost::bind (&VCAMasterStrip::update_vca_display, this),
164                                                   gui_context());
165
166
167         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::self_delete, this), gui_context());
168
169         s->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::parameter_changed, this, _1), gui_context());
170         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::parameter_changed, this, _1), gui_context());
171         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VCAMasterStrip::parameter_changed));
172 }
173
174 VCAMasterStrip::~VCAMasterStrip ()
175 {
176         if ((_session && !_session->deletion_in_progress()) && Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
177                 /* cancel spill for this VCA */
178                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
179         }
180
181         delete delete_dialog;
182         delete context_menu;
183
184         CatchDeletion (this); /* EMIT SIGNAL */
185 }
186
187 void
188 VCAMasterStrip::self_delete ()
189 {
190         if ((_session && !_session->deletion_in_progress()) && Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
191                 /* cancel spill for this VCA */
192                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
193         }
194         /* Drop reference immediately, delete self when idle */
195         _vca.reset ();
196         delete_when_idle (this);
197 }
198
199 void
200 VCAMasterStrip::parameter_changed (std::string const & p)
201 {
202         if (p == "use-monitor-bus" || p == "solo-control-is-listen-control" || p == "listen-position") {
203                 set_button_names ();
204         }
205 }
206
207 void
208 VCAMasterStrip::set_button_names ()
209 {
210         if (Config->get_solo_control_is_listen_control()) {
211                 switch (Config->get_listen_position()) {
212                 case AfterFaderListen:
213                         solo_button.set_text (S_("AfterFader|A"));
214                         set_tooltip (solo_button, _("After-fade listen (AFL)"));
215                         break;
216                 case PreFaderListen:
217                         solo_button.set_text (S_("PreFader|P"));
218                         set_tooltip (solo_button, _("Pre-fade listen (PFL)"));
219                         break;
220                 }
221         } else {
222                 solo_button.set_text (S_("Solo|S"));
223                 set_tooltip (solo_button, _("Solo"));
224         }
225 }
226
227 void
228 VCAMasterStrip::update_vca_display ()
229 {
230         VCAList vcas (_session->vca_manager().vcas());
231         string label;
232
233         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
234                 if (_vca->slaved_to (*v)) {
235                         if (!label.empty()) {
236                                 label += ' ';
237                         }
238                         label += to_string ((*v)->number(), std::dec);
239                 }
240         }
241
242         if (label.empty()) {
243                 label = _("-vca-");
244                 assign_button.set_active_state (Gtkmm2ext::Off);
245         } else {
246                 assign_button.set_active_state (Gtkmm2ext::ExplicitActive);
247         }
248
249         assign_button.set_text (label);
250 }
251
252 string
253 VCAMasterStrip::name() const
254 {
255         return _vca->name();
256 }
257
258 void
259 VCAMasterStrip::hide_clicked ()
260 {
261         if (!delete_dialog) {
262                 delete_dialog = new MessageDialog (_("Removing a Master will deassign all slaves. Remove it anyway?"),
263                                                    true, MESSAGE_WARNING, BUTTONS_YES_NO, true);
264                 delete_dialog->signal_response().connect (sigc::mem_fun (*this, &VCAMasterStrip::hide_confirmation));
265         }
266
267         delete_dialog->set_position (Gtk::WIN_POS_MOUSE);
268         delete_dialog->present ();
269 }
270
271 void
272 VCAMasterStrip::hide_confirmation (int response)
273 {
274         delete_dialog->hide ();
275
276         switch (response) {
277         case RESPONSE_YES:
278                 /* get everything to deassign. This will also delete ourselves (when
279                  * idle) and that in turn will remove us from the Mixer GUI
280                  */
281                 _session->vca_manager().remove_vca (_vca);
282                 break;
283         default:
284                 break;
285         }
286 }
287
288 bool
289 VCAMasterStrip::width_button_pressed (GdkEventButton* ev)
290 {
291         return false;
292 }
293
294 void
295 VCAMasterStrip::set_selected (bool yn)
296 {
297         AxisView::set_selected (yn);
298
299         if (_selected) {
300                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
301                 global_frame.set_name ("MixerStripSelectedFrame");
302         } else {
303                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
304                 global_frame.set_name ("MixerStripFrame");
305         }
306
307         global_frame.queue_draw ();
308 }
309
310 bool
311 VCAMasterStrip::solo_release (GdkEventButton*)
312 {
313         /* We use NoGroup because VCA controls are never part of a group. This
314            is redundant, but clear.
315         */
316         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
317         return true;
318 }
319
320 bool
321 VCAMasterStrip::mute_release (GdkEventButton*)
322 {
323         /* We use NoGroup because VCA controls are never part of a group. This
324            is redundant, but clear.
325         */
326         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
327         return true;
328 }
329
330 void
331 VCAMasterStrip::set_solo_text ()
332 {
333         if (Config->get_solo_control_is_listen_control ()) {
334                 switch (Config->get_listen_position()) {
335                 case AfterFaderListen:
336                         solo_button.set_text (_("A"));
337                         break;
338                 case PreFaderListen:
339                         solo_button.set_text (_("P"));
340                         break;
341                         }
342         } else {
343                 solo_button.set_text (_("S"));
344         }
345 }
346
347 void
348 VCAMasterStrip::mute_changed ()
349 {
350         if (_vca->mute_control()->muted_by_self()) {
351                 mute_button.set_active_state (ExplicitActive);
352         } else if (_vca->mute_control()->muted_by_masters ()) {
353                 mute_button.set_active_state (ImplicitActive);
354         } else {
355                 mute_button.set_active_state (Gtkmm2ext::Off);
356         }
357 }
358
359 void
360 VCAMasterStrip::solo_changed ()
361 {
362         if (_vca->solo_control()->self_soloed()) {
363                 solo_button.set_active_state (ExplicitActive);
364         } else if (_vca->solo_control()->soloed_by_masters ()) {
365                 solo_button.set_active_state (ImplicitActive);
366         } else {
367                 solo_button.set_active_state (Gtkmm2ext::Off);
368         }
369 }
370
371 void
372 VCAMasterStrip::vca_menu_toggle (Gtk::CheckMenuItem* menuitem, uint32_t n)
373 {
374         boost::shared_ptr<VCA> vca = _session->vca_manager().vca_by_number (n);
375
376         if (!menuitem->get_active()) {
377                 if (!vca) {
378                         /* null VCA means drop all VCA assignments */
379                         _vca->unassign (boost::shared_ptr<VCA>());
380
381                 } else {
382                         _vca->unassign (vca);
383                 }
384         } else {
385                 if (vca) {
386                         _vca->assign (vca);
387                 }
388         }
389 }
390
391 void
392 VCAMasterStrip::unassign ()
393 {
394         _vca->unassign (boost::shared_ptr<VCA>());
395 }
396
397 bool
398 VCAMasterStrip::vca_button_release (GdkEventButton* ev)
399 {
400         using namespace Gtk::Menu_Helpers;
401
402         if (!_session) {
403                 return false;
404         }
405
406         /* primary click only */
407
408         if (ev->button != 1) {
409                 return false;
410         }
411
412         VCAList vcas (_session->vca_manager().vcas());
413
414         if (vcas.empty()) {
415                 /* XXX should probably show a message saying "No VCA masters" */
416                 return true;
417         }
418
419         Menu* menu = new Menu;
420         MenuList& items = menu->items();
421
422         items.push_back (MenuElem (_("Unassign"), sigc::mem_fun (*this, &VCAMasterStrip::unassign)));
423
424         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
425
426                 if (*v == _vca) {
427                         /* no self-mastering */
428                         continue;
429                 }
430
431                 items.push_back (CheckMenuElem ((*v)->name()));
432                 Gtk::CheckMenuItem* item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back());
433                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
434                         item->set_active (true);
435                 }
436                 item->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &VCAMasterStrip::vca_menu_toggle), item, (*v)->number()));
437         }
438
439         menu->popup (1, ev->time);
440
441         return true;
442 }
443
444 bool
445 VCAMasterStrip::vertical_button_press (GdkEventButton* ev)
446 {
447         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
448                 start_name_edit ();
449                 return true;
450         }
451
452         if (Keyboard::is_context_menu_event (ev)) {
453                 if (!context_menu) {
454                         build_context_menu ();
455                 }
456                 context_menu->popup (1, ev->time);
457                 return true;
458         }
459
460         if (ev->button == 1) {
461                 spill ();
462         }
463
464         return true;
465 }
466
467 void
468 VCAMasterStrip::start_name_edit ()
469 {
470         Gtk::Window* win = dynamic_cast<Gtk::Window*>(get_toplevel());
471         FloatingTextEntry* fte = new FloatingTextEntry (win, _vca->name());
472         fte->use_text.connect (sigc::mem_fun (*this, &VCAMasterStrip::finish_name_edit));
473         fte->present ();
474 }
475
476 void
477 VCAMasterStrip::finish_name_edit (std::string str, int)
478 {
479         _vca->set_name (str);
480 }
481
482 void
483 VCAMasterStrip::vca_property_changed (PropertyChange const & what_changed)
484 {
485         if (what_changed.contains (ARDOUR::Properties::name)) {
486                 update_vca_name ();
487         }
488 }
489
490 void
491 VCAMasterStrip::update_vca_name ()
492 {
493         /* 20 is a rough guess at the number of letters we can fit. */
494         vertical_button.set_text (short_version (_vca->name(), 20));
495 }
496
497 void
498 VCAMasterStrip::build_context_menu ()
499 {
500         using namespace Gtk::Menu_Helpers;
501         context_menu = new Menu;
502         MenuList& items = context_menu->items();
503         items.push_back (MenuElem (_("Rename"), sigc::mem_fun (*this, &VCAMasterStrip::start_name_edit)));
504         items.push_back (SeparatorElem());
505         items.push_back (MenuElem (_("Drop All Slaves"), sigc::mem_fun (*this, &VCAMasterStrip::drop_all_slaves)));
506         items.push_back (SeparatorElem());
507         items.push_back (MenuElem (_("Remove"), sigc::mem_fun (*this, &VCAMasterStrip::remove)));
508 }
509
510 void
511 VCAMasterStrip::spill ()
512 {
513         if (Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
514                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
515         } else {
516                 Mixer_UI::instance()->show_vca_slaves (_vca);
517         }
518 }
519
520 void
521 VCAMasterStrip::spill_change (boost::shared_ptr<VCA> vca)
522 {
523         if (vca != _vca) {
524                 vertical_button.set_active_state (Gtkmm2ext::Off);
525                 set_tooltip (vertical_button, _("Click to show slaves only"));
526         } else {
527                 vertical_button.set_active_state (Gtkmm2ext::ExplicitActive);
528                 set_tooltip (vertical_button, _("Click to show normal mixer"));
529         }
530 }
531
532 void
533 VCAMasterStrip::remove ()
534 {
535         if (!_session) {
536                 return;
537         }
538
539         _session->vca_manager().remove_vca (_vca);
540 }
541
542 void
543 VCAMasterStrip::drop_all_slaves ()
544 {
545         _vca->Drop (); /* EMIT SIGNAL */
546
547         if (Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
548                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
549         }
550 }
551
552 void
553 VCAMasterStrip::drop_button_press ()
554 {
555         drop_all_slaves ();
556 }
557
558 Gdk::Color
559 VCAMasterStrip::color () const
560 {
561         return gdk_color_from_rgb (_vca->presentation_info().color ());
562 }