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