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