39ce9c0aa0c345fb40c66b5b77285939ce1cf14c
[ardour.git] / gtk2_ardour / vca_time_axis.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
20 #include <gtkmm/menu.h>
21
22 #include "pbd/string_convert.h"
23
24 #include "ardour/mute_control.h"
25 #include "ardour/profile.h"
26 #include "ardour/session.h"
27 #include "ardour/solo_control.h"
28 #include "ardour/vca.h"
29
30 #include "gtkmm2ext/doi.h"
31 #include <gtkmm2ext/utils.h>
32
33 #include "gui_thread.h"
34 #include "public_editor.h"
35 #include "tooltips.h"
36 #include "ui_config.h"
37 #include "vca_time_axis.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace ARDOUR_UI_UTILS;
43 using namespace Gtk;
44 using namespace Gtkmm2ext;
45 using namespace PBD;
46
47 VCATimeAxisView::VCATimeAxisView (PublicEditor& ed, Session* s, ArdourCanvas::Canvas& canvas)
48         : SessionHandlePtr (s)
49         , StripableTimeAxisView (ed, s, canvas)
50         , gain_meter (s, true, 75, 14) // XXX stupid magic numbers, match sizes in RouteTimeAxisView
51         , automation_action_menu (0)
52 {
53         solo_button.set_name ("solo button");
54         set_tooltip (solo_button, _("Solo slaves"));
55         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::solo_release), false);
56         solo_button.unset_flags (Gtk::CAN_FOCUS);
57
58         mute_button.set_name ("mute button");
59         mute_button.set_text (S_("Mute|M"));
60         set_tooltip (mute_button, _("Mute slaves"));
61         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::mute_release), false);
62         mute_button.unset_flags (Gtk::CAN_FOCUS);
63
64         drop_button.set_name ("mute button");
65         drop_button.set_text (S_("VCA|D"));
66         set_tooltip (drop_button, _("Unassign all slaves"));
67         drop_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::drop_release), false);
68         drop_button.unset_flags (Gtk::CAN_FOCUS);
69
70         automation_button.set_name ("route button");
71         automation_button.set_text (S_("RTAV|A"));
72         set_tooltip (automation_button, _("Automation"));
73         automation_button.signal_button_press_event().connect (sigc::mem_fun (*this, &VCATimeAxisView::automation_click), false);
74         automation_button.unset_flags (Gtk::CAN_FOCUS);
75
76         mute_button.set_tweaks(ArdourButton::TrackHeader);
77         solo_button.set_tweaks(ArdourButton::TrackHeader);
78         drop_button.set_tweaks(ArdourButton::TrackHeader);
79         automation_button.set_tweaks(ArdourButton::TrackHeader);
80
81         controls_table.attach (mute_button, 2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
82         controls_table.attach (solo_button, 3, 4, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
83         controls_table.attach (automation_button, 2, 3, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
84         controls_table.attach (drop_button, 3, 4, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
85         controls_table.attach (gain_meter.get_gain_slider(), 0, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 1, 0);
86
87         mute_button.show ();
88         solo_button.show ();
89         drop_button.show ();
90         automation_button.show ();
91         gain_meter.get_gain_slider().show ();
92
93         controls_ebox.set_name ("ControlMasterBaseUnselected");
94         time_axis_frame.set_name ("ControlMasterBaseUnselected");
95
96         s->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::parameter_changed, this, _1), gui_context());
97         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&VCATimeAxisView::parameter_changed, this, _1), gui_context());
98         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VCATimeAxisView::parameter_changed));
99 }
100
101 VCATimeAxisView::~VCATimeAxisView ()
102 {
103 }
104
105 void
106 VCATimeAxisView::self_delete ()
107 {
108         /* reset reference immediately rather than deferring to idle */
109         _vca.reset ();
110         delete_when_idle (this);
111 }
112
113 void
114 VCATimeAxisView::parameter_changed (std::string const & p)
115 {
116         if (p == "track-name-number") {
117                 update_track_number_visibility();
118         } else if (p == "use-monitor-bus" || p == "solo-control-is-listen-control" || p == "listen-position") {
119                 set_button_names ();
120         }
121 }
122
123 bool
124 VCATimeAxisView::solo_release (GdkEventButton*)
125 {
126         /* We use NoGroup because VCA controls are never part of a group. This
127            is redundant, but clear.
128         */
129         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
130         return true;
131 }
132
133 bool
134 VCATimeAxisView::mute_release (GdkEventButton*)
135 {
136         /* We use NoGroup because VCA controls are never part of a group. This
137            is redundant, but clear.
138         */
139         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
140         return true;
141 }
142
143 void
144 VCATimeAxisView::set_vca (boost::shared_ptr<VCA> v)
145 {
146         StripableTimeAxisView::set_stripable (v);
147         _vca = v;
148
149         gain_meter.set_controls (boost::shared_ptr<Route>(),
150                                  boost::shared_ptr<PeakMeter>(),
151                                  boost::shared_ptr<Amp>(),
152                                  _vca->gain_control());
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 (&VCATimeAxisView::vca_property_changed, this, _1), gui_context());
157
158         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::update_solo_display, this), gui_context());
159         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::update_mute_display, this), gui_context());
160         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCATimeAxisView::self_delete, this), gui_context());
161
162         solo_button.set_controllable (_vca->solo_control());
163         mute_button.set_controllable (_vca->mute_control());
164
165         /* VCA number never changes */
166         number_label.set_text (PBD::to_string (_vca->number()));
167
168         set_height (preset_height (HeightNormal));
169
170         if (automation_child (GainAutomation) == 0) {
171                 create_automation_child (GainAutomation, false);
172         }
173         if (automation_child (MuteAutomation) == 0) {
174                 create_automation_child (MuteAutomation, false);
175         }
176
177         update_vca_name ();
178         set_button_names ();
179         update_solo_display ();
180         update_mute_display ();
181         update_track_number_visibility ();
182 }
183
184 void
185 VCATimeAxisView::vca_property_changed (PropertyChange const & what_changed)
186 {
187         if (what_changed.contains (ARDOUR::Properties::name)) {
188                 update_vca_name ();
189         }
190 }
191
192 void
193 VCATimeAxisView::update_vca_name ()
194 {
195         name_label.set_text (_vca->full_name());
196 }
197
198 void
199 VCATimeAxisView::update_mute_display ()
200 {
201         if (_vca->mute_control()->muted_by_self()) {
202                 mute_button.set_active_state (ExplicitActive);
203         } else if (_vca->mute_control()->muted_by_masters ()) {
204                 mute_button.set_active_state (ImplicitActive);
205         } else {
206                 mute_button.set_active_state (Gtkmm2ext::Off);
207         }
208 }
209
210 void
211 VCATimeAxisView::update_solo_display ()
212 {
213         if (_vca->solo_control()->self_soloed()) {
214                 solo_button.set_active_state (ExplicitActive);
215         } else if (_vca->solo_control()->soloed_by_masters ()) {
216                 solo_button.set_active_state (ImplicitActive);
217         } else {
218                 solo_button.set_active_state (Gtkmm2ext::Off);
219         }
220
221         update_mute_display ();
222 }
223
224 std::string
225 VCATimeAxisView::name() const
226 {
227         return _vca->name();
228 }
229
230 std::string
231 VCATimeAxisView::state_id() const
232 {
233         return string_compose ("vtv %1", _vca->id().to_s());
234 }
235
236 void
237 VCATimeAxisView::set_button_names ()
238 {
239         if (Config->get_solo_control_is_listen_control()) {
240                 switch (Config->get_listen_position()) {
241                 case AfterFaderListen:
242                         solo_button.set_text (S_("AfterFader|A"));
243                         set_tooltip (solo_button, _("After-fade listen (AFL)"));
244                         break;
245                 case PreFaderListen:
246                         solo_button.set_text (S_("PreFader|P"));
247                         set_tooltip (solo_button, _("Pre-fade listen (PFL)"));
248                         break;
249                 }
250         } else {
251                 solo_button.set_text (S_("Solo|S"));
252                 set_tooltip (solo_button, _("Solo"));
253         }
254 }
255
256 void
257 VCATimeAxisView::update_track_number_visibility ()
258 {
259         DisplaySuspender ds;
260         bool show_label = _session->config.get_track_name_number();
261
262         if (number_label.get_parent()) {
263                 controls_table.remove (number_label);
264         }
265
266         if (show_label) {
267                 if (ARDOUR::Profile->get_mixbus()) {
268                         controls_table.attach (number_label, 3, 4, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 1, 0);
269                 } else {
270                         controls_table.attach (number_label, 0, 1, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 1, 0);
271                 }
272
273                 // see ArdourButton::on_size_request(), we should probably use a global size-group here instead.
274                 // except the width of the number label is subtracted from the name-hbox, so we
275                 // need to explictly calculate it anyway until the name-label & entry become ArdourWidgets.
276
277                 int tnw = (2 + std::max(2u, _session->track_number_decimals())) * number_label.char_pixel_width();
278                 if (tnw & 1) --tnw;
279                 number_label.set_size_request(tnw, -1);
280                 number_label.show ();
281         } else {
282                 number_label.hide ();
283         }
284 }
285
286 bool
287 VCATimeAxisView::automation_click (GdkEventButton* ev)
288 {
289         if (ev->button != 1) {
290                 return true;
291         }
292
293         conditionally_add_to_selection ();
294         build_automation_action_menu (false);
295         Gtkmm2ext::anchored_menu_popup (automation_action_menu, &automation_button, "", 1, ev->time);
296         return true;
297 }
298
299 bool
300 VCATimeAxisView::drop_release (GdkEventButton*)
301 {
302         _vca->Drop (); /* EMIT SIGNAL */
303
304         return true;
305 }
306
307 PresentationInfo const &
308 VCATimeAxisView::presentation_info () const
309 {
310         return _vca->presentation_info();
311 }
312
313 boost::shared_ptr<Stripable>
314 VCATimeAxisView::stripable () const
315 {
316         return _vca;
317 }
318
319 Gdk::Color
320 VCATimeAxisView::color () const
321 {
322         return gdk_color_from_rgb (_vca->presentation_info().color ());
323 }
324
325 void
326 VCATimeAxisView::set_height (uint32_t h, TrackHeightMode m)
327 {
328         TimeAxisView::set_height (h, m);
329         set_gui_property ("height", h);
330         _vca->gui_changed ("track_height", (void*) 0); /* EMIT SIGNAL */
331 }
332
333 bool
334 VCATimeAxisView::marked_for_display () const
335 {
336         return _vca && !_vca->presentation_info().hidden();
337 }
338
339 bool
340 VCATimeAxisView::set_marked_for_display (bool yn)
341 {
342         if (_vca && (yn == _vca->presentation_info().hidden())) {
343                 _vca->presentation_info().set_hidden (!yn);
344                 return true; // things changed
345         }
346         return false;
347 }
348
349 void
350 VCATimeAxisView::create_gain_automation_child (const Evoral::Parameter& param, bool show)
351 {
352         boost::shared_ptr<AutomationControl> c = _vca->gain_control();
353         if (!c) {
354                 error << "VCA has no gain automation, unable to add automation track view." << endmsg;
355                 return;
356         }
357
358         gain_track.reset (new AutomationTimeAxisView (_session,
359                                                       _vca, boost::shared_ptr<Automatable> (), c, param,
360                                                       _editor,
361                                                       *this,
362                                                       false,
363                                                       parent_canvas,
364                                                       /*_route->amp()->describe_parameter(param)*/"Fader"));
365
366         add_automation_child (Evoral::Parameter(GainAutomation), gain_track, show);
367 }
368
369 void
370 VCATimeAxisView::create_mute_automation_child (const Evoral::Parameter& param, bool show)
371 {
372         boost::shared_ptr<AutomationControl> c = _vca->mute_control();
373         if (!c) {
374                 error << "VCA has no mute automation, unable to add automation track view." << endmsg;
375                 return;
376         }
377
378         mute_track.reset (new AutomationTimeAxisView (_session,
379                                                       _vca, boost::shared_ptr<Automatable> (), c, param,
380                                                       _editor,
381                                                       *this,
382                                                       false,
383                                                       parent_canvas,
384                                                       /*_route->describe_parameter(param)*/ "Mute"));
385
386         add_automation_child (Evoral::Parameter(MuteAutomation), mute_track, show);
387 }
388
389 void
390 VCATimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
391 {
392         switch (param.type()) {
393                 case GainAutomation:
394                         create_gain_automation_child (param, show);
395                         break;
396                 case MuteAutomation:
397                         create_mute_automation_child (param, show);
398                         break;
399                 default:
400                         break;
401         }
402 }
403
404 void
405 VCATimeAxisView::build_automation_action_menu (bool for_selection)
406 {
407         using namespace Menu_Helpers;
408         _main_automation_menu_map.clear ();
409         delete automation_action_menu;
410         automation_action_menu = new Menu;
411
412         MenuList& items = automation_action_menu->items();
413
414         automation_action_menu->set_name ("ArdourContextMenu");
415
416         items.push_back (MenuElem (_("Show All Automation"),
417                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::show_all_automation), for_selection)));
418
419         items.push_back (MenuElem (_("Show Existing Automation"),
420                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::show_existing_automation), for_selection)));
421
422         items.push_back (MenuElem (_("Hide All Automation"),
423                                    sigc::bind (sigc::mem_fun (*this, &VCATimeAxisView::hide_all_automation), for_selection)));
424
425         if (gain_track) {
426                 items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &VCATimeAxisView::update_gain_track_visibility)));
427                 gain_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
428                 gain_automation_item->set_active (string_to<bool>(gain_track->gui_property ("visible")));
429
430                 _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
431         }
432
433         if (trim_track) {
434                 items.push_back (CheckMenuElem (_("Trim"), sigc::mem_fun (*this, &VCATimeAxisView::update_trim_track_visibility)));
435                 trim_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
436                 trim_automation_item->set_active (string_to<bool>(trim_track->gui_property ("visible")));
437
438                 _main_automation_menu_map[Evoral::Parameter(TrimAutomation)] = trim_automation_item;
439         }
440
441         if (mute_track) {
442                 items.push_back (CheckMenuElem (_("Mute"), sigc::mem_fun (*this, &VCATimeAxisView::update_mute_track_visibility)));
443                 mute_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
444                 mute_automation_item->set_active (string_to<bool>(mute_track->gui_property ("visible")));
445
446                 _main_automation_menu_map[Evoral::Parameter(MuteAutomation)] = mute_automation_item;
447         }
448 }
449
450 void
451 VCATimeAxisView::show_all_automation (bool apply_to_selection)
452 {
453         assert (!apply_to_selection); // VCAs can't yet be selected
454         no_redraw = true;
455
456         StripableTimeAxisView::show_all_automation ();
457
458         no_redraw = false;
459         request_redraw ();
460 }
461
462 void
463 VCATimeAxisView::show_existing_automation (bool apply_to_selection)
464 {
465         assert (!apply_to_selection); // VCAs can't yet be selected
466         no_redraw = true;
467
468         StripableTimeAxisView::show_existing_automation ();
469
470         no_redraw = false;
471         request_redraw ();
472 }
473
474 void
475 VCATimeAxisView::hide_all_automation (bool apply_to_selection)
476 {
477         assert (!apply_to_selection); // VCAs can't yet be selected
478         no_redraw = true;
479
480         StripableTimeAxisView::hide_all_automation ();
481
482         no_redraw = false;
483         request_redraw ();
484 }