noop: clarifying comment about a magic number
[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 #include <gtkmm/colorselection.h>
21
22 #include "pbd/convert.h"
23
24 #include "ardour/rc_configuration.h"
25 #include "ardour/session.h"
26 #include "ardour/vca.h"
27 #include "ardour/vca_manager.h"
28
29 #include "gtkmm2ext/doi.h"
30 #include "gtkmm2ext/keyboard.h"
31
32 #include "ardour_dialog.h"
33 #include "floating_text_entry.h"
34 #include "gui_thread.h"
35 #include "mixer_ui.h"
36 #include "tooltips.h"
37 #include "ui_config.h"
38 #include "utils.h"
39 #include "vca_master_strip.h"
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44 using namespace ARDOUR_UI_UTILS;
45 using namespace Gtkmm2ext;
46 using namespace Gtk;
47 using namespace PBD;
48 using std::string;
49
50 PBD::Signal1<void,VCAMasterStrip*> VCAMasterStrip::CatchDeletion;
51
52 VCAMasterStrip::VCAMasterStrip (Session* s, boost::shared_ptr<VCA> v)
53         : SessionHandlePtr (s)
54         , _vca (v)
55         , gain_meter (s, 254) /* magic number, don't adjust blindly */
56         , context_menu (0)
57         , delete_dialog (0)
58         , control_slave_ui (s)
59 {
60
61         PresentationInfo::color_t c = _vca->presentation_info().color ();
62
63         /* set color for the VCA, if not already done. */
64
65         if (!_vca->presentation_info().color_set()) {
66                 _vca->presentation_info().set_color (gdk_color_to_rgba (unique_random_color()));
67         }
68
69         control_slave_ui.set_stripable (boost::dynamic_pointer_cast<Stripable> (v));
70
71         gain_meter.set_controls (boost::shared_ptr<Route>(),
72                                  boost::shared_ptr<PeakMeter>(),
73                                  boost::shared_ptr<Amp>(),
74                                  _vca->gain_control());
75
76         solo_button.set_name ("solo button");
77         set_tooltip (solo_button, _("Solo slaves"));
78         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::solo_release), false);
79
80         mute_button.set_name ("mute button");
81         mute_button.set_text (_("M"));
82         set_tooltip (mute_button, _("Mute slaves"));
83         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::mute_release), false);
84
85         hide_button.set_icon (ArdourIcon::CloseCross);
86         set_tooltip (&hide_button, _("Hide this VCA strip"));
87
88         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &VCAMasterStrip::hide_clicked));
89
90         solo_mute_box.set_spacing (2);
91         solo_mute_box.pack_start (mute_button, true, true);
92         solo_mute_box.pack_start (solo_button, true, true);
93
94         number_label.set_text (to_string (v->number(), std::dec));
95         number_label.set_elements((ArdourButton::Element)(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text|ArdourButton::Inactive));
96         number_label.set_no_show_all ();
97         number_label.set_name ("generic button");
98         number_label.set_alignment (.5, .5);
99         number_label.set_fallthrough_to_parent (true);
100
101         bottom_padding.set_size_request (-1, 32); /* this one is a hack. there's no trivial way to compute it */
102
103         //Glib::RefPtr<Pango::Layout> layout = vertical_button.get_layout ();
104         // layout->set_justify (JUSTIFY_CENTER);
105         /* horizontally centered, with a little space (5%) at the top */
106         vertical_button.set_angle (90);
107         vertical_button.set_layout_font (UIConfiguration::instance().get_NormalBoldFont());
108         vertical_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vertical_button_press));
109         vertical_button.set_fallthrough_to_parent (true);
110         vertical_button.set_active_color (_vca->presentation_info().color ());
111         set_tooltip (vertical_button, _("Click to show slaves only")); /* tooltip updated dynamically */
112
113         drop_button.set_text(_("drop"));
114         drop_button.signal_clicked.connect (sigc::mem_fun (*this, &VCAMasterStrip::drop_button_press));
115         set_tooltip (drop_button, _("Unassign all slaves from this control master"));
116
117         global_vpacker.set_border_width (1);
118         global_vpacker.set_spacing (0);
119
120         global_vpacker.pack_start (number_label, false, false);
121         global_vpacker.pack_start (hide_button, false, false);
122         global_vpacker.pack_start (vertical_button, true, true);
123         global_vpacker.pack_start (solo_mute_box, false, false);
124         global_vpacker.pack_start (gain_meter, false, false, 2);
125         global_vpacker.pack_start (control_slave_ui, false, false);
126         global_vpacker.pack_start (drop_button, false, false);
127         global_vpacker.pack_start (bottom_padding, false, false);
128
129         global_frame.add (global_vpacker);
130         global_frame.set_shadow_type (Gtk::SHADOW_IN);
131         global_frame.set_name ("BaseFrame");
132
133         add (global_frame);
134
135         global_vpacker.show ();
136         global_frame.show ();
137         top_padding.show ();
138         bottom_padding.show ();
139         vertical_button.show ();
140         hide_button.show ();
141         number_label.show ();
142         gain_meter.show ();
143         solo_mute_box.show_all ();
144         control_slave_ui.show ();
145         drop_button.show ();
146
147         /* force setting of visible selected status */
148
149         _selected = true;
150         set_selected (false);
151         set_solo_text ();
152         update_vca_name ();
153         solo_changed ();
154         mute_changed ();
155         spill_change (boost::shared_ptr<VCA>());
156
157         Mixer_UI::instance()->show_vca_change.connect (sigc::mem_fun (*this, &VCAMasterStrip::spill_change));
158
159         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
160         _vca->presentation_info().PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
161         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::self_delete, this), gui_context());
162
163         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::solo_changed, this), gui_context());
164         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::mute_changed, this), gui_context());
165
166
167         s->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::parameter_changed, this, _1), gui_context());
168         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCAMasterStrip::parameter_changed, this, _1), gui_context());
169         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VCAMasterStrip::parameter_changed));
170 }
171
172 VCAMasterStrip::~VCAMasterStrip ()
173 {
174         if ((_session && !_session->deletion_in_progress()) && Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
175                 /* cancel spill for this VCA */
176                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
177         }
178
179         delete delete_dialog;
180         delete context_menu;
181
182         CatchDeletion (this); /* EMIT SIGNAL */
183 }
184
185 void
186 VCAMasterStrip::self_delete ()
187 {
188         if ((_session && !_session->deletion_in_progress()) && Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
189                 /* cancel spill for this VCA */
190                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
191         }
192         /* Drop reference immediately, delete self when idle */
193         _vca.reset ();
194         delete_when_idle (this);
195 }
196
197 void
198 VCAMasterStrip::parameter_changed (std::string const & p)
199 {
200         if (p == "use-monitor-bus" || p == "solo-control-is-listen-control" || p == "listen-position") {
201                 set_button_names ();
202         }
203 }
204
205 void
206 VCAMasterStrip::set_button_names ()
207 {
208         if (Config->get_solo_control_is_listen_control()) {
209                 switch (Config->get_listen_position()) {
210                 case AfterFaderListen:
211                         solo_button.set_text (S_("AfterFader|A"));
212                         set_tooltip (solo_button, _("After-fade listen (AFL)"));
213                         break;
214                 case PreFaderListen:
215                         solo_button.set_text (S_("PreFader|P"));
216                         set_tooltip (solo_button, _("Pre-fade listen (PFL)"));
217                         break;
218                 }
219         } else {
220                 solo_button.set_text (S_("Solo|S"));
221                 set_tooltip (solo_button, _("Solo"));
222         }
223 }
224
225 string
226 VCAMasterStrip::name() const
227 {
228         return _vca->name();
229 }
230
231 void
232 VCAMasterStrip::hide_clicked ()
233 {
234         if (!delete_dialog) {
235                 delete_dialog = new MessageDialog (_("Removing a Master will deassign all slaves. Remove it anyway?"),
236                                                    true, MESSAGE_WARNING, BUTTONS_YES_NO, true);
237                 delete_dialog->signal_response().connect (sigc::mem_fun (*this, &VCAMasterStrip::hide_confirmation));
238         }
239
240         delete_dialog->set_position (Gtk::WIN_POS_MOUSE);
241         delete_dialog->present ();
242 }
243
244 void
245 VCAMasterStrip::hide_confirmation (int response)
246 {
247         delete_dialog->hide ();
248
249         switch (response) {
250         case RESPONSE_YES:
251                 /* get everything to deassign. This will also delete ourselves (when
252                  * idle) and that in turn will remove us from the Mixer GUI
253                  */
254                 _session->vca_manager().remove_vca (_vca);
255                 break;
256         default:
257                 break;
258         }
259 }
260
261 bool
262 VCAMasterStrip::width_button_pressed (GdkEventButton* ev)
263 {
264         return false;
265 }
266
267 void
268 VCAMasterStrip::set_selected (bool yn)
269 {
270         AxisView::set_selected (yn);
271
272         if (_selected) {
273                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
274                 global_frame.set_name ("MixerStripSelectedFrame");
275         } else {
276                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
277                 global_frame.set_name ("MixerStripFrame");
278         }
279
280         global_frame.queue_draw ();
281 }
282
283 bool
284 VCAMasterStrip::solo_release (GdkEventButton*)
285 {
286         /* We use NoGroup because VCA controls are never part of a group. This
287            is redundant, but clear.
288         */
289         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
290         return true;
291 }
292
293 bool
294 VCAMasterStrip::mute_release (GdkEventButton*)
295 {
296         /* We use NoGroup because VCA controls are never part of a group. This
297            is redundant, but clear.
298         */
299         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
300         return true;
301 }
302
303 void
304 VCAMasterStrip::set_solo_text ()
305 {
306         if (Config->get_solo_control_is_listen_control ()) {
307                 switch (Config->get_listen_position()) {
308                 case AfterFaderListen:
309                         solo_button.set_text (_("A"));
310                         break;
311                 case PreFaderListen:
312                         solo_button.set_text (_("P"));
313                         break;
314                         }
315         } else {
316                 solo_button.set_text (_("S"));
317         }
318 }
319
320 void
321 VCAMasterStrip::mute_changed ()
322 {
323         if (_vca->mute_control()->muted_by_self()) {
324                 mute_button.set_active_state (ExplicitActive);
325         } else if (_vca->mute_control()->muted_by_masters ()) {
326                 mute_button.set_active_state (ImplicitActive);
327         } else {
328                 mute_button.set_active_state (Gtkmm2ext::Off);
329         }
330 }
331
332 void
333 VCAMasterStrip::solo_changed ()
334 {
335         if (_vca->solo_control()->self_soloed()) {
336                 solo_button.set_active_state (ExplicitActive);
337         } else if (_vca->solo_control()->soloed_by_masters ()) {
338                 solo_button.set_active_state (ImplicitActive);
339         } else {
340                 solo_button.set_active_state (Gtkmm2ext::Off);
341         }
342 }
343
344 bool
345 VCAMasterStrip::vertical_button_press (GdkEventButton* ev)
346 {
347         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
348                 start_name_edit ();
349                 return true;
350         }
351
352         if (Keyboard::is_context_menu_event (ev)) {
353                 if (!context_menu) {
354                         build_context_menu ();
355                 }
356                 context_menu->popup (1, ev->time);
357                 return true;
358         }
359
360         if (ev->button == 1) {
361                 spill ();
362         }
363
364         return true;
365 }
366
367 void
368 VCAMasterStrip::start_name_edit ()
369 {
370         Gtk::Window* win = dynamic_cast<Gtk::Window*>(get_toplevel());
371         FloatingTextEntry* fte = new FloatingTextEntry (win, _vca->name());
372         fte->use_text.connect (sigc::mem_fun (*this, &VCAMasterStrip::finish_name_edit));
373         fte->present ();
374 }
375
376 void
377 VCAMasterStrip::finish_name_edit (std::string str, int)
378 {
379         _vca->set_name (str);
380 }
381
382 void
383 VCAMasterStrip::vca_property_changed (PropertyChange const & what_changed)
384 {
385         if (what_changed.contains (ARDOUR::Properties::name)) {
386                 update_vca_name ();
387         }
388
389         if (what_changed.contains (ARDOUR::Properties::color)) {
390                 vertical_button.set_active_color (_vca->presentation_info().color ());
391         }
392 }
393
394 void
395 VCAMasterStrip::update_vca_name ()
396 {
397         /* 20 is a rough guess at the number of letters we can fit. */
398         vertical_button.set_text (short_version (_vca->name(), 20));
399 }
400
401 void
402 VCAMasterStrip::build_context_menu ()
403 {
404         using namespace Gtk::Menu_Helpers;
405         context_menu = new Menu;
406         MenuList& items = context_menu->items();
407         items.push_back (MenuElem (_("Rename"), sigc::mem_fun (*this, &VCAMasterStrip::start_name_edit)));
408         items.push_back (MenuElem (_("Color..."), sigc::mem_fun (*this, &VCAMasterStrip::start_color_edit)));
409         items.push_back (SeparatorElem());
410         items.push_back (MenuElem (_("Drop All Slaves"), sigc::mem_fun (*this, &VCAMasterStrip::drop_all_slaves)));
411         items.push_back (SeparatorElem());
412         items.push_back (MenuElem (_("Remove"), sigc::mem_fun (*this, &VCAMasterStrip::remove)));
413 }
414
415 void
416 VCAMasterStrip::spill ()
417 {
418         if (Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
419                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
420         } else {
421                 Mixer_UI::instance()->show_vca_slaves (_vca);
422         }
423 }
424
425 void
426 VCAMasterStrip::spill_change (boost::shared_ptr<VCA> vca)
427 {
428         if (vca != _vca) {
429                 vertical_button.set_active_state (Gtkmm2ext::Off);
430                 set_tooltip (vertical_button, _("Click to show slaves only"));
431         } else {
432                 vertical_button.set_active_state (Gtkmm2ext::ExplicitActive);
433                 set_tooltip (vertical_button, _("Click to show normal mixer"));
434         }
435 }
436
437 void
438 VCAMasterStrip::remove ()
439 {
440         if (!_session) {
441                 return;
442         }
443
444         _session->vca_manager().remove_vca (_vca);
445 }
446
447 void
448 VCAMasterStrip::drop_all_slaves ()
449 {
450         _vca->Drop (); /* EMIT SIGNAL */
451
452         if (Mixer_UI::instance()->showing_vca_slaves_for (_vca)) {
453                 Mixer_UI::instance()->show_vca_slaves (boost::shared_ptr<VCA>());
454         }
455 }
456
457 void
458 VCAMasterStrip::drop_button_press ()
459 {
460         drop_all_slaves ();
461 }
462
463 Gdk::Color
464 VCAMasterStrip::color () const
465 {
466         return gdk_color_from_rgba (_vca->presentation_info().color ());
467 }
468
469 string
470 VCAMasterStrip::state_id () const
471 {
472         return string_compose (X_("vms-%1"), _vca->number());
473 }
474
475 void
476 VCAMasterStrip::start_color_edit ()
477 {
478         Gtk::ColorSelectionDialog* color_dialog = new Gtk::ColorSelectionDialog;
479
480         color_dialog->get_colorsel()->set_has_opacity_control (false);
481         color_dialog->get_colorsel()->set_has_palette (true);
482
483         Gdk::Color c = gdk_color_from_rgba (_vca->presentation_info().color ());
484
485         color_dialog->get_colorsel()->set_previous_color (c);
486         color_dialog->get_colorsel()->set_current_color (c);
487
488         color_dialog->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &VCAMasterStrip::finish_color_edit), color_dialog));
489         color_dialog->present ();
490 }
491
492 void
493 VCAMasterStrip::finish_color_edit (int response, Gtk::ColorSelectionDialog* dialog)
494 {
495         switch (response) {
496         case RESPONSE_OK:
497                 _vca->presentation_info().set_color (gdk_color_to_rgba (dialog->get_colorsel()->get_current_color()));
498                 break;
499         }
500
501         delete_when_idle (dialog);
502 }
503
504 bool
505 VCAMasterStrip::marked_for_display () const
506 {
507         return !_vca->presentation_info().hidden();
508 }
509
510 bool
511 VCAMasterStrip::set_marked_for_display (bool yn)
512 {
513         if (yn == _vca->presentation_info().hidden()) {
514                 _vca->presentation_info().set_hidden (!yn);
515                 return true; // things changed
516         }
517         return false;
518 }