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