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