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