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