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