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