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