Enable Menu > Quit to work again after startup on macOS
[ardour.git] / gtk2_ardour / vca_master_strip.cc
1 /*
2  * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
4  * Copyright (C) 2017-2018 Ben Loftis <ben@harrisonconsoles.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <gtkmm/stock.h>
22
23 #include "pbd/string_convert.h"
24
25 #include "ardour/rc_configuration.h"
26 #include "ardour/session.h"
27 #include "ardour/vca.h"
28 #include "ardour/vca_manager.h"
29
30 #include "gtkmm2ext/doi.h"
31 #include "gtkmm2ext/keyboard.h"
32 #include "widgets/tooltips.h"
33
34 #include "ardour_dialog.h"
35 #include "floating_text_entry.h"
36 #include "gui_thread.h"
37 #include "mixer_ui.h"
38 #include "ui_config.h"
39 #include "utils.h"
40 #include "vca_master_strip.h"
41
42 #include "pbd/i18n.h"
43
44 using namespace ARDOUR;
45 using namespace ArdourWidgets;
46 using namespace Gtkmm2ext;
47 using namespace Gtk;
48 using namespace PBD;
49 using std::string;
50
51 PBD::Signal1<void,VCAMasterStrip*> VCAMasterStrip::CatchDeletion;
52
53 VCAMasterStrip::VCAMasterStrip (Session* s, boost::shared_ptr<VCA> v)
54         : SessionHandlePtr (s)
55         , _vca (v)
56         , gain_meter (s, 254) /* magic number, don't adjust blindly */
57         , context_menu (0)
58         , delete_dialog (0)
59         , control_slave_ui (s)
60 {
61         /* set color for the VCA, if not already done. */
62
63         if (!_vca->presentation_info().color_set()) {
64                 _vca->presentation_info().set_color (ARDOUR_UI_UTILS::gdk_color_to_rgba (unique_random_color()));
65         }
66
67         control_slave_ui.set_stripable (boost::dynamic_pointer_cast<Stripable> (v));
68
69         gain_meter.set_controls (boost::shared_ptr<Route>(),
70                                  boost::shared_ptr<PeakMeter>(),
71                                  boost::shared_ptr<Amp>(),
72                                  _vca->gain_control());
73
74         solo_button.set_name ("solo button");
75         set_tooltip (solo_button, _("Solo slaves"));
76         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::solo_release), false);
77
78         mute_button.set_name ("mute button");
79         mute_button.set_text (_("M"));
80         set_tooltip (mute_button, _("Mute slaves"));
81         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::mute_release), false);
82
83         hide_button.set_icon (ArdourIcon::HideEye);
84         set_tooltip (&hide_button, _("Hide this VCA strip"));
85
86         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &VCAMasterStrip::hide_clicked));
87
88         solo_mute_box.set_spacing (2);
89         solo_mute_box.pack_start (mute_button, true, true);
90         solo_mute_box.pack_start (solo_button, true, true);
91
92         mute_button.set_controllable (_vca->mute_control());
93         solo_button.set_controllable (_vca->solo_control());
94
95         number_label.set_text (PBD::to_string (v->number()));
96         number_label.set_elements((ArdourButton::Element)(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text|ArdourButton::Inactive));
97         number_label.set_no_show_all ();
98         number_label.set_name ("generic button");
99         number_label.set_alignment (.5, .5);
100         number_label.set_fallthrough_to_parent (true);
101         number_label.set_inactive_color (_vca->presentation_info().color ());
102         number_label.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::number_button_press), false);
103
104         update_bottom_padding ();
105
106         //Glib::RefPtr<Pango::Layout> layout = vertical_button.get_layout ();
107         // layout->set_justify (JUSTIFY_CENTER);
108         /* horizontally centered, with a little space (5%) at the top */
109         vertical_button.set_angle (90);
110         vertical_button.set_layout_font (UIConfiguration::instance().get_NormalBoldFont());
111         vertical_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vertical_button_press));
112         vertical_button.set_fallthrough_to_parent (true);
113         vertical_button.set_active_color (_vca->presentation_info().color ());
114         set_tooltip (vertical_button, _("Click to show slaves only")); /* tooltip updated dynamically */
115
116         global_vpacker.set_border_width (0);
117         global_vpacker.set_spacing (0);
118         gain_meter.set_spacing(4);
119
120         global_vpacker.pack_start (number_label, false, false, 1);
121         global_vpacker.pack_start (hide_button, false, false, 1);
122         global_vpacker.pack_start (vertical_button, true, true, 1);
123         global_vpacker.pack_start (solo_mute_box, false, false, 1);
124         global_vpacker.pack_start (gain_meter, false, false, 1);
125         global_vpacker.pack_start (control_slave_ui, false, false, 1);
126         global_vpacker.pack_start (gain_meter.gain_automation_state_button, false, false, 1);
127         global_vpacker.pack_start (bottom_padding, false, false, 0);
128
129         global_frame.add (global_vpacker);
130         global_frame.set_shadow_type (Gtk::SHADOW_IN);
131         global_frame.set_name ("BaseFrame");
132
133         add (global_frame);
134
135         global_vpacker.show ();
136         global_frame.show ();
137         vertical_button.show ();
138         hide_button.show ();
139         number_label.show ();
140         gain_meter.show ();
141         solo_mute_box.show_all ();
142         control_slave_ui.show ();
143         gain_meter.gain_automation_state_button.show ();
144
145         /* force setting of visible selected status */
146
147         _selected = true;
148         set_selected (false);
149         set_solo_text ();
150         update_vca_name ();
151         solo_changed ();
152         mute_changed ();
153         spill_change (boost::shared_ptr<VCA>());
154
155         Mixer_UI::instance()->show_spill_change.connect (sigc::mem_fun (*this, &VCAMasterStrip::spill_change));
156
157         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
158         _vca->presentation_info().PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
159         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::self_delete, this), gui_context());
160
161         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::solo_changed, this), gui_context());
162         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::mute_changed, this), gui_context());
163
164         _session->MonitorBusAddedOrRemoved.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::set_button_names, this), gui_context());
165
166         s->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::parameter_changed, this, _1), gui_context());
167         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::parameter_changed, this, _1), gui_context());
168         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VCAMasterStrip::parameter_changed));
169 }
170
171 VCAMasterStrip::~VCAMasterStrip ()
172 {
173         if ((_session && !_session->deletion_in_progress()) && Mixer_UI::instance()->showing_spill_for (_vca)) {
174                 /* cancel spill for this VCA */
175                 Mixer_UI::instance()->show_spill (boost::shared_ptr<Stripable>());
176         }
177
178         delete delete_dialog;
179         delete context_menu;
180
181         CatchDeletion (this); /* EMIT SIGNAL */
182 }
183
184 void
185 VCAMasterStrip::self_delete ()
186 {
187         if ((_session && !_session->deletion_in_progress()) && Mixer_UI::instance()->showing_spill_for (_vca)) {
188                 /* cancel spill for this VCA */
189                 Mixer_UI::instance()->show_spill (boost::shared_ptr<Stripable>());
190         }
191         /* Drop reference immediately, delete self when idle */
192         _vca.reset ();
193         gain_meter.set_controls (boost::shared_ptr<Route>(),
194                                  boost::shared_ptr<PeakMeter>(),
195                                  boost::shared_ptr<Amp>(),
196                                  boost::shared_ptr<GainControl>());
197         delete_when_idle (this);
198 }
199
200 void
201 VCAMasterStrip::parameter_changed (std::string const & p)
202 {
203         if (p == "solo-control-is-listen-control" || p == "listen-position") {
204                 set_button_names ();
205         } else if (p == "mixer-element-visibility") {
206                 update_bottom_padding ();
207         }
208 }
209
210 void
211 VCAMasterStrip::set_button_names ()
212 {
213         if (Config->get_solo_control_is_listen_control()) {
214                 switch (Config->get_listen_position()) {
215                 case AfterFaderListen:
216                         solo_button.set_text (S_("AfterFader|A"));
217                         set_tooltip (solo_button, _("After-fade listen (AFL)"));
218                         break;
219                 case PreFaderListen:
220                         solo_button.set_text (S_("PreFader|P"));
221                         set_tooltip (solo_button, _("Pre-fade listen (PFL)"));
222                         break;
223                 }
224         } else {
225                 solo_button.set_text (S_("Solo|S"));
226                 set_tooltip (solo_button, _("Solo"));
227         }
228 }
229
230 void
231 VCAMasterStrip::update_bottom_padding ()
232 {
233         std::string viz = UIConfiguration::instance().get_mixer_strip_visibility ();
234
235         ArdourButton output_button (_("Output"));
236         ArdourButton comment_button (_("Comments"));
237
238         output_button.set_name ("mixer strip button");
239         comment_button.set_name ("generic button");
240
241         if (viz.find ("VCA") == std::string::npos) {
242                 control_slave_ui.hide ();
243         } else {
244                 control_slave_ui.show ();
245         }
246
247         int h = 0;
248         if (viz.find ("Output") != std::string::npos) {
249                 Gtk::Window window (WINDOW_TOPLEVEL);
250                 window.add (output_button);
251                 Gtk::Requisition requisition(output_button.size_request ());
252                 h += requisition.height + 2;
253         }
254         if (viz.find ("Comments") != std::string::npos) {
255                 Gtk::Window window (WINDOW_TOPLEVEL);
256                 window.add (comment_button);
257                 Gtk::Requisition requisition(comment_button.size_request ());
258                 h += requisition.height + 2;
259         }
260         if (h <= 0) {
261                 bottom_padding.set_size_request (-1, 1);
262                 bottom_padding.hide ();
263         } else {
264                 bottom_padding.set_size_request (-1, h);
265                 bottom_padding.show ();
266         }
267 }
268
269 string
270 VCAMasterStrip::name() const
271 {
272         return _vca->name();
273 }
274
275 void
276 VCAMasterStrip::hide_clicked ()
277 {
278         _vca->presentation_info().set_hidden (true);
279 }
280
281 void
282 VCAMasterStrip::hide_confirmation (int response)
283 {
284         delete_dialog->hide ();
285
286         switch (response) {
287         case RESPONSE_YES:
288                 /* get everything to deassign. This will also delete ourselves (when
289                  * idle) and that in turn will remove us from the Mixer GUI
290                  */
291                 _session->vca_manager().remove_vca (_vca);
292                 break;
293         default:
294                 break;
295         }
296 }
297
298 bool
299 VCAMasterStrip::width_button_pressed (GdkEventButton* ev)
300 {
301         return false;
302 }
303
304 void
305 VCAMasterStrip::set_selected (bool yn)
306 {
307         AxisView::set_selected (yn);
308
309         if (_selected) {
310                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
311                 global_frame.set_name ("MixerStripSelectedFrame");
312         } else {
313                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
314                 global_frame.set_name ("MixerStripFrame");
315         }
316
317         global_frame.queue_draw ();
318 }
319
320 bool
321 VCAMasterStrip::solo_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->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
327         return true;
328 }
329
330 bool
331 VCAMasterStrip::mute_release (GdkEventButton*)
332 {
333         /* We use NoGroup because VCA controls are never part of a group. This
334            is redundant, but clear.
335         */
336         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
337         return true;
338 }
339
340 void
341 VCAMasterStrip::set_solo_text ()
342 {
343         if (Config->get_solo_control_is_listen_control ()) {
344                 switch (Config->get_listen_position()) {
345                 case AfterFaderListen:
346                         solo_button.set_text (_("A"));
347                         break;
348                 case PreFaderListen:
349                         solo_button.set_text (_("P"));
350                         break;
351                         }
352         } else {
353                 solo_button.set_text (_("S"));
354         }
355 }
356
357 void
358 VCAMasterStrip::mute_changed ()
359 {
360         if (_vca->mute_control()->muted_by_self()) {
361                 mute_button.set_active_state (ExplicitActive);
362         } else if (_vca->mute_control()->muted_by_masters ()) {
363                 mute_button.set_active_state (ImplicitActive);
364         } else {
365                 mute_button.set_active_state (Gtkmm2ext::Off);
366         }
367 }
368
369 void
370 VCAMasterStrip::solo_changed ()
371 {
372         if (_vca->solo_control()->self_soloed()) {
373                 solo_button.set_active_state (ExplicitActive);
374         } else if (_vca->solo_control()->soloed_by_masters ()) {
375                 solo_button.set_active_state (ImplicitActive);
376         } else {
377                 solo_button.set_active_state (Gtkmm2ext::Off);
378         }
379 }
380
381 bool
382 VCAMasterStrip::vertical_button_press (GdkEventButton* ev)
383 {
384         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
385                 start_name_edit ();
386                 return true;
387         }
388
389         if (Keyboard::is_context_menu_event (ev)) {
390                 if (!context_menu) {
391                         build_context_menu ();
392                 }
393                 context_menu->popup (1, ev->time);
394                 return true;
395         }
396
397         if (ev->button == 1) {
398                 spill ();
399         }
400
401         return true;
402 }
403
404 bool
405 VCAMasterStrip::number_button_press (GdkEventButton* ev)
406 {
407         if (Keyboard::is_context_menu_event (ev)) {
408                 if (!context_menu) {
409                         build_context_menu ();
410                 }
411                 context_menu->popup (1, ev->time);
412                 return true;
413         }
414         return false;
415 }
416
417 void
418 VCAMasterStrip::start_name_edit ()
419 {
420         Gtk::Window* win = dynamic_cast<Gtk::Window*>(get_toplevel());
421         FloatingTextEntry* fte = new FloatingTextEntry (win, _vca->name());
422         fte->use_text.connect (sigc::mem_fun (*this, &VCAMasterStrip::finish_name_edit));
423         fte->present ();
424 }
425
426 void
427 VCAMasterStrip::finish_name_edit (std::string str, int)
428 {
429         _vca->set_name (str);
430 }
431
432 void
433 VCAMasterStrip::vca_property_changed (PropertyChange const & what_changed)
434 {
435         if (what_changed.contains (ARDOUR::Properties::name)) {
436                 update_vca_name ();
437         }
438
439         if (what_changed.contains (ARDOUR::Properties::color)) {
440                 vertical_button.set_active_color (_vca->presentation_info().color ());
441                 number_label.set_inactive_color (_vca->presentation_info().color ());
442         }
443
444         if (what_changed.contains (ARDOUR::Properties::hidden)) {
445
446         }
447 }
448
449 void
450 VCAMasterStrip::update_vca_name ()
451 {
452         /* 20 is a rough guess at the number of letters we can fit. */
453         vertical_button.set_text (short_version (_vca->full_name(), 20));
454 }
455
456 void
457 VCAMasterStrip::build_context_menu ()
458 {
459         using namespace Gtk::Menu_Helpers;
460         context_menu = new Menu;
461         MenuList& items = context_menu->items();
462         items.push_back (MenuElem (_("Rename"), sigc::mem_fun (*this, &VCAMasterStrip::start_name_edit)));
463         items.push_back (MenuElem (_("Color..."), sigc::mem_fun (*this, &VCAMasterStrip::start_color_edit)));
464         items.push_back (SeparatorElem());
465         items.push_back (MenuElem (_("Assign Selected Channels"), sigc::mem_fun (*this, &VCAMasterStrip::assign_all_selected)));
466         items.push_back (MenuElem (_("Drop Selected Channels"), sigc::mem_fun (*this, &VCAMasterStrip::unassign_all_selected)));
467         items.push_back (SeparatorElem());
468         items.push_back (MenuElem (_("Drop All Slaves"), sigc::mem_fun (*this, &VCAMasterStrip::drop_all_slaves)));
469         items.push_back (SeparatorElem());
470         items.push_back (MenuElem (_("Remove"), sigc::mem_fun (*this, &VCAMasterStrip::remove)));
471 }
472
473 void
474 VCAMasterStrip::spill ()
475 {
476         if (Mixer_UI::instance()->showing_spill_for (_vca)) {
477                 Mixer_UI::instance()->show_spill (boost::shared_ptr<Stripable>());
478         } else {
479                 Mixer_UI::instance()->show_spill (_vca);
480         }
481 }
482
483 void
484 VCAMasterStrip::spill_change (boost::shared_ptr<Stripable> vca)
485 {
486         if (vca != _vca) {
487                 vertical_button.set_active_state (Gtkmm2ext::Off);
488                 set_tooltip (vertical_button, _("Click to show slaves only"));
489         } else {
490                 vertical_button.set_active_state (Gtkmm2ext::ExplicitActive);
491                 set_tooltip (vertical_button, _("Click to show normal mixer"));
492         }
493 }
494
495 void
496 VCAMasterStrip::remove ()
497 {
498         if (!_session) {
499                 return;
500         }
501
502         _session->vca_manager().remove_vca (_vca);
503 }
504
505 void
506 VCAMasterStrip::assign_all_selected ()
507 {
508         Mixer_UI::instance()->do_vca_assign (_vca);
509 }
510
511 void
512 VCAMasterStrip::unassign_all_selected ()
513 {
514         Mixer_UI::instance()->do_vca_unassign (_vca);
515 }
516
517 void
518 VCAMasterStrip::drop_all_slaves ()
519 {
520         _vca->Drop (); /* EMIT SIGNAL */
521
522         if (Mixer_UI::instance()->showing_spill_for (_vca)) {
523                 Mixer_UI::instance()->show_spill (boost::shared_ptr<Stripable>());
524         }
525 }
526
527 Gdk::Color
528 VCAMasterStrip::color () const
529 {
530         return ARDOUR_UI_UTILS::gdk_color_from_rgba (_vca->presentation_info().color ());
531 }
532
533 string
534 VCAMasterStrip::state_id () const
535 {
536         return string_compose (X_("vms-%1"), _vca->number());
537 }
538
539 void
540 VCAMasterStrip::start_color_edit ()
541 {
542         _color_picker.popup (_vca);
543 }
544
545 bool
546 VCAMasterStrip::marked_for_display () const
547 {
548         return !_vca->presentation_info().hidden();
549 }
550
551 bool
552 VCAMasterStrip::set_marked_for_display (bool yn)
553 {
554         if (yn == _vca->presentation_info().hidden()) {
555                 _vca->presentation_info().set_hidden (!yn);
556                 return true; // things changed
557         }
558         return false;
559 }
560
561 PresentationInfo const &
562 VCAMasterStrip::presentation_info () const
563 {
564         return _vca->presentation_info();
565 }
566
567 boost::shared_ptr<Stripable>
568 VCAMasterStrip::stripable () const
569 {
570         return _vca;
571 }