get VCA status correct in strips on first load
[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 "pbd/convert.h"
20
21 #include "ardour/rc_configuration.h"
22 #include "ardour/session.h"
23 #include "ardour/vca.h"
24 #include "ardour/vca_manager.h"
25
26 #include "gtkmm2ext/keyboard.h"
27
28 #include "gui_thread.h"
29 #include "floating_text_entry.h"
30 #include "tooltips.h"
31 #include "vca_master_strip.h"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace ARDOUR_UI_UTILS;
37 using namespace Gtkmm2ext;
38 using namespace Gtk;
39 using namespace PBD;
40 using std::string;
41
42 VCAMasterStrip::VCAMasterStrip (Session* s, boost::shared_ptr<VCA> v)
43         : AxisView (s)
44         , _vca (v)
45         , gain_meter (s, 250)
46         , context_menu (0)
47 {
48         gain_meter.set_controls (boost::shared_ptr<Route>(),
49                                  boost::shared_ptr<PeakMeter>(),
50                                  boost::shared_ptr<Amp>(),
51                                  _vca->gain_control());
52
53         solo_button.set_name ("solo button");
54         set_tooltip (solo_button, _("Solo slaves"));
55         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::solo_release), false);
56
57         mute_button.set_name ("mute button");
58         mute_button.set_text (_("M"));
59         set_tooltip (mute_button, _("Mute slaves"));
60         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::mute_release), false);
61
62         hide_button.set_icon (ArdourIcon::CloseCross);
63         set_tooltip (&hide_button, _("Hide this VCA strip"));
64
65         assign_button.set_name (X_("vca assign"));
66         set_tooltip (assign_button, _("Click to assign a VCA Master to this VCA"));
67         assign_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vca_button_release), false);
68
69         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &VCAMasterStrip::hide_clicked));
70
71         width_hide_box.pack_start (number_label, true, true);
72         width_hide_box.pack_end (hide_button, false, true);
73
74         solo_mute_box.set_spacing (2);
75         solo_mute_box.pack_start (mute_button, true, true);
76         solo_mute_box.pack_start (solo_button, true, true);
77
78         number_label.set_text (to_string (v->number(), std::dec));
79         number_label.set_elements((ArdourButton::Element)(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text|ArdourButton::Inactive));
80         number_label.set_no_show_all ();
81         number_label.set_name ("generic button");
82         number_label.set_alignment (.5, .5);
83         number_label.set_fallthrough_to_parent (true);
84
85         name_button.signal_button_press_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::name_button_press), false);
86
87         top_padding.set_size_request (-1, 16); /* must match height in GroupTabs::set_size_request() */
88         bottom_padding.set_size_request (-1, 50); /* this one is a hack. there's no trivial way to compute it */
89
90         global_vpacker.set_border_width (1);
91         global_vpacker.set_spacing (0);
92
93         global_vpacker.pack_start (top_padding, false, false);
94         global_vpacker.pack_start (width_hide_box, false, false);
95         global_vpacker.pack_start (name_button, false, false);
96         global_vpacker.pack_start (vertical_padding, true, true);
97         global_vpacker.pack_start (solo_mute_box, false, false);
98         global_vpacker.pack_start (gain_meter, false, false);
99         global_vpacker.pack_start (assign_button, false, false);
100         global_vpacker.pack_start (bottom_padding, false, false);
101
102         global_frame.add (global_vpacker);
103         global_frame.set_shadow_type (Gtk::SHADOW_IN);
104         global_frame.set_name ("BaseFrame");
105
106         add (global_frame);
107
108         global_vpacker.show ();
109         global_frame.show ();
110         top_padding.show ();
111         bottom_padding.show ();
112         vertical_padding.show ();
113         hide_button.show ();
114         number_label.show ();
115         width_hide_box.show ();
116         name_button.show ();
117         gain_meter.show ();
118         solo_mute_box.show_all ();
119         assign_button.show ();
120
121         /* force setting of visible selected status */
122
123         _selected = true;
124         set_selected (false);
125         set_solo_text ();
126         update_vca_display ();
127         update_vca_name ();
128         solo_changed ();
129         mute_changed ();
130
131         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
132
133         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::solo_changed, this), gui_context());
134         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::mute_changed, this), gui_context());
135
136         /* only need to connect to one of these to update VCA status */
137
138         _vca->gain_control()->MasterStatusChange.connect (vca_connections,
139                                                   invalidator (*this),
140                                                   boost::bind (&VCAMasterStrip::update_vca_display, this),
141                                                   gui_context());
142 }
143
144 void
145 VCAMasterStrip::update_vca_display ()
146 {
147         VCAList vcas (_session->vca_manager().vcas());
148         string label;
149
150         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
151                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
152                         if (!label.empty()) {
153                                 label += ' ';
154                         }
155                         label += to_string ((*v)->number(), std::dec);
156                 }
157         }
158
159         if (label.empty()) {
160                 label = _("-vca-");
161                 assign_button.set_active_state (Gtkmm2ext::Off);
162         } else {
163                 assign_button.set_active_state (Gtkmm2ext::ExplicitActive);
164         }
165
166         assign_button.set_text (label);
167 }
168
169 string
170 VCAMasterStrip::name() const
171 {
172         return _vca->name();
173 }
174
175 void
176 VCAMasterStrip::hide_clicked ()
177 {
178 }
179
180 bool
181 VCAMasterStrip::width_button_pressed (GdkEventButton* ev)
182 {
183         return false;
184 }
185
186 void
187 VCAMasterStrip::set_selected (bool yn)
188 {
189         AxisView::set_selected (yn);
190
191         if (_selected) {
192                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
193                 global_frame.set_name ("MixerStripSelectedFrame");
194         } else {
195                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
196                 global_frame.set_name ("MixerStripFrame");
197         }
198
199         global_frame.queue_draw ();
200 }
201
202 bool
203 VCAMasterStrip::solo_release (GdkEventButton*)
204 {
205         /* We use NoGroup because VCA controls are never part of a group. This
206            is redundant, but clear.
207         */
208         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
209         return true;
210 }
211
212 bool
213 VCAMasterStrip::mute_release (GdkEventButton*)
214 {
215         /* We use NoGroup because VCA controls are never part of a group. This
216            is redundant, but clear.
217         */
218         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
219         return true;
220 }
221
222 void
223 VCAMasterStrip::set_solo_text ()
224 {
225         if (Config->get_solo_control_is_listen_control ()) {
226                 switch (Config->get_listen_position()) {
227                 case AfterFaderListen:
228                         solo_button.set_text (_("A"));
229                         break;
230                 case PreFaderListen:
231                         solo_button.set_text (_("P"));
232                         break;
233                         }
234         } else {
235                 solo_button.set_text (_("S"));
236         }
237 }
238
239 void
240 VCAMasterStrip::mute_changed ()
241 {
242         if (_vca->mute_control()->muted_by_self()) {
243                 mute_button.set_active_state (ExplicitActive);
244         } else if (_vca->mute_control()->muted_by_masters ()) {
245                 mute_button.set_active_state (ImplicitActive);
246         } else {
247                 mute_button.set_active_state (Gtkmm2ext::Off);
248         }
249 }
250
251 void
252 VCAMasterStrip::solo_changed ()
253 {
254         if (_vca->solo_control()->self_soloed()) {
255                 solo_button.set_active_state (ExplicitActive);
256         } else if (_vca->solo_control()->soloed_by_masters ()) {
257                 solo_button.set_active_state (ImplicitActive);
258         } else {
259                 solo_button.set_active_state (Gtkmm2ext::Off);
260         }
261 }
262
263 void
264 VCAMasterStrip::vca_menu_toggle (CheckMenuItem* menuitem, uint32_t n)
265 {
266         boost::shared_ptr<VCA> vca = _session->vca_manager().vca_by_number (n);
267
268         if (!menuitem->get_active()) {
269                 if (!vca) {
270                         /* null VCA means drop all VCA assignments */
271                         vca_unassign ();
272                 } else {
273                         _vca->gain_control()->remove_master (vca->gain_control());
274                         _vca->solo_control()->remove_master (vca->solo_control());
275                         _vca->mute_control()->remove_master (vca->mute_control());
276                 }
277         } else {
278                 if (vca) {
279                         _vca->gain_control()->add_master (vca->gain_control());
280                         _vca->mute_control()->add_master (vca->mute_control());
281                         _vca->solo_control()->add_master (vca->solo_control());
282                 }
283         }
284 }
285
286 void
287 VCAMasterStrip::vca_unassign ()
288 {
289         _vca->gain_control()->clear_masters ();
290         _vca->solo_control()->clear_masters ();
291         _vca->mute_control()->clear_masters ();
292 }
293
294 bool
295 VCAMasterStrip::vca_button_release (GdkEventButton* ev)
296 {
297         using namespace Gtk::Menu_Helpers;
298
299         if (!_session) {
300                 return false;
301         }
302
303         /* primary click only */
304
305         if (ev->button != 1) {
306                 return false;
307         }
308
309         VCAList vcas (_session->vca_manager().vcas());
310
311         if (vcas.empty()) {
312                 /* XXX should probably show a message saying "No VCA masters" */
313                 return true;
314         }
315
316         Menu* menu = new Menu;
317         MenuList& items = menu->items();
318
319         items.push_back (MenuElem (_("Unassign"), sigc::mem_fun (*this, &VCAMasterStrip::vca_unassign)));
320
321         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
322
323                 if (*v == _vca) {
324                         /* no self-mastering */
325                         continue;
326                 }
327
328                 items.push_back (CheckMenuElem ((*v)->name()));
329                 CheckMenuItem* item = dynamic_cast<CheckMenuItem*> (&items.back());
330                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
331                         item->set_active (true);
332                 }
333                 item->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &VCAMasterStrip::vca_menu_toggle), item, (*v)->number()));
334         }
335
336         menu->popup (1, ev->time);
337
338         return true;
339 }
340
341 bool
342 VCAMasterStrip::name_button_press (GdkEventButton* ev)
343 {
344         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
345                 Gtk::Window* win = dynamic_cast<Gtk::Window*>(get_toplevel());
346                 FloatingTextEntry* fte = new FloatingTextEntry (win, _vca->name());
347                 fte->use_text.connect (sigc::mem_fun (*this, &VCAMasterStrip::finish_name_edit));
348                 fte->present ();
349                 return true;
350         }
351
352         if (Keyboard::is_context_menu_event (ev)) {
353                 if (!context_menu) {
354                         build_context_menu ();
355                 }
356                 context_menu->popup (1, ev->time);
357                 return true;
358         }
359
360         return false;
361 }
362
363 void
364 VCAMasterStrip::finish_name_edit (std::string str)
365 {
366         _vca->set_name (str);
367 }
368
369 void
370 VCAMasterStrip::vca_property_changed (PropertyChange const & what_changed)
371 {
372         if (what_changed.contains (ARDOUR::Properties::name)) {
373                 update_vca_name ();
374         }
375 }
376
377 void
378 VCAMasterStrip::update_vca_name ()
379 {
380         name_button.set_text (short_version (_vca->name(), 8));
381 }
382
383 void
384 VCAMasterStrip::build_context_menu ()
385 {
386         using namespace Gtk::Menu_Helpers;
387         context_menu = new Menu;
388         MenuList& items = context_menu->items();
389         items.push_back (MenuElem (_("Remove")));
390 }