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