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