Fix mute display on session load (#4480).
[ardour.git] / gtk2_ardour / audio_time_axis.cc
1 /*
2     Copyright (C) 2000-2006 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 <cstdlib>
21 #include <cmath>
22 #include <cassert>
23
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27
28 #include <sigc++/bind.h>
29
30 #include "pbd/error.h"
31 #include "pbd/stl_delete.h"
32 #include "pbd/memento_command.h"
33
34 #include <gtkmm2ext/gtk_ui.h>
35 #include <gtkmm2ext/selector.h>
36 #include <gtkmm2ext/bindable_button.h>
37 #include <gtkmm2ext/utils.h>
38
39 #include "ardour/amp.h"
40 #include "ardour/audioplaylist.h"
41 #include "ardour/event_type_map.h"
42 #include "ardour/location.h"
43 #include "ardour/pannable.h"
44 #include "ardour/panner.h"
45 #include "ardour/panner_shell.h"
46 #include "ardour/playlist.h"
47 #include "ardour/processor.h"
48 #include "ardour/profile.h"
49 #include "ardour/session.h"
50 #include "ardour/session_playlist.h"
51 #include "ardour/utils.h"
52
53 #include "ardour_button.h"
54 #include "ardour_ui.h"
55 #include "audio_time_axis.h"
56 #include "automation_line.h"
57 #include "canvas_impl.h"
58 #include "crossfade_view.h"
59 #include "enums.h"
60 #include "gui_thread.h"
61 #include "automation_time_axis.h"
62 #include "keyboard.h"
63 #include "playlist_selector.h"
64 #include "prompter.h"
65 #include "public_editor.h"
66 #include "audio_region_view.h"
67 #include "simplerect.h"
68 #include "audio_streamview.h"
69 #include "utils.h"
70
71 #include "ardour/audio_track.h"
72
73 #include "i18n.h"
74
75 using namespace std;
76 using namespace ARDOUR;
77 using namespace PBD;
78 using namespace Gtk;
79 using namespace Editing;
80
81 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session* sess, Canvas& canvas)
82         : AxisView(sess)
83         , RouteTimeAxisView(ed, sess, canvas)
84 {
85 }
86
87 void
88 AudioTimeAxisView::set_route (boost::shared_ptr<Route> rt)
89 {
90         RouteTimeAxisView::set_route (rt);
91
92         // Make sure things are sane...
93         assert(!is_track() || is_audio_track());
94
95         subplugin_menu.set_name ("ArdourContextMenu");
96
97         _view = new AudioStreamView (*this);
98
99         ignore_toggle = false;
100
101         if (is_audio_track()) {
102                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
103         } else { // bus
104                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
105         }
106
107         /* if set_state above didn't create a gain automation child, we need to make one */
108         if (automation_child (GainAutomation) == 0) {
109                 create_automation_child (GainAutomation, false);
110         }
111
112         if (_route->panner()) {
113                 _route->panner_shell()->Changed.connect (*this, invalidator (*this),
114                                                          boost::bind (&AudioTimeAxisView::ensure_pan_views, this, false), gui_context());
115         }
116
117         /* map current state of the route */
118
119         processors_changed (RouteProcessorChange ());
120         reset_processor_automation_curves ();
121         ensure_pan_views (false);
122         update_control_names ();
123
124         if (is_audio_track()) {
125
126                 /* ask for notifications of any new RegionViews */
127                 _view->RegionViewAdded.connect (sigc::mem_fun(*this, &AudioTimeAxisView::region_view_added));
128
129                 if (!_editor.have_idled()) {
130                         /* first idle will do what we need */
131                 } else {
132                         first_idle ();
133                 }
134
135         } else {
136                 post_construct ();
137         }
138 }
139
140 AudioTimeAxisView::~AudioTimeAxisView ()
141 {
142 }
143
144 void
145 AudioTimeAxisView::first_idle ()
146 {
147         _view->attach ();
148         post_construct ();
149 }
150
151 AudioStreamView*
152 AudioTimeAxisView::audio_view()
153 {
154         return dynamic_cast<AudioStreamView*>(_view);
155 }
156
157 guint32
158 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
159 {
160         set_gui_property ("visible", true);
161         return TimeAxisView::show_at (y, nth, parent);
162 }
163
164 void
165 AudioTimeAxisView::hide ()
166 {
167         set_gui_property ("visible", false);
168         TimeAxisView::hide ();
169 }
170
171
172 void
173 AudioTimeAxisView::append_extra_display_menu_items ()
174 {
175         using namespace Menu_Helpers;
176
177         MenuList& items = display_menu->items();
178
179         // crossfade stuff
180         if (!Profile->get_sae() && is_track ()) {
181                 items.push_back (MenuElem (_("Hide All Crossfades"), sigc::bind (sigc::mem_fun(*this, &AudioTimeAxisView::hide_all_xfades), true)));
182                 items.push_back (MenuElem (_("Show All Crossfades"), sigc::bind (sigc::mem_fun(*this, &AudioTimeAxisView::show_all_xfades), true)));
183                 items.push_back (SeparatorElem ());
184         }
185 }
186
187 void
188 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
189 {
190         if (param.type() == NullAutomation) {
191                 cerr << "WARNING: Attempt to create NullAutomation child, ignoring" << endl;
192                 return;
193         }
194
195         AutomationTracks::iterator existing = _automation_tracks.find (param);
196
197         if (existing != _automation_tracks.end()) {
198                 
199                 /* automation track created because we had existing data for
200                  * the processor, but visibility may need to be controlled
201                  * since it will have been set visible by default.
202                  */
203
204                 existing->second->set_marked_for_display (show);
205                 
206                 if (!no_redraw) {
207                         request_redraw ();
208                 }
209
210                 return;
211         }
212
213         if (param.type() == GainAutomation) {
214
215                 create_gain_automation_child (param, show);
216
217         } else if (param.type() == PanWidthAutomation ||
218                    param.type() == PanElevationAutomation ||
219                    param.type() == PanAzimuthAutomation) {
220
221                 ensure_pan_views (show);
222
223         } else if (param.type() == PluginAutomation) {
224
225                 /* handled elsewhere */
226
227         } else {
228                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
229         }
230 }
231
232 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
233  *  panners that we have.
234  *
235  *  @param show true to show any new views that we create, otherwise false.
236  */
237 void
238 AudioTimeAxisView::ensure_pan_views (bool show)
239 {
240         if (!_route->panner()) {
241                 return;
242         }
243
244         set<Evoral::Parameter> params = _route->panner()->what_can_be_automated();
245         set<Evoral::Parameter>::iterator p;
246
247         for (p = params.begin(); p != params.end(); ++p) {
248                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control = _route->pannable()->automation_control(*p);
249
250                 if (pan_control->parameter().type() == NullAutomation) {
251                         error << "Pan control has NULL automation type!" << endmsg;
252                         continue;
253                 }
254
255                 if (automation_child (pan_control->parameter ()).get () == 0) {
256
257                         /* we don't already have an AutomationTimeAxisView for this parameter */
258
259                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
260
261                         boost::shared_ptr<AutomationTimeAxisView> t (
262                                 new AutomationTimeAxisView (_session,
263                                                             _route,
264                                                             _route->pannable(),
265                                                             pan_control,
266                                                             pan_control->parameter (),
267                                                             _editor,
268                                                             *this,
269                                                             false,
270                                                             parent_canvas,
271                                                             name)
272                                 );
273
274                         pan_tracks.push_back (t);
275                         add_automation_child (*p, t, show);
276                 }
277         }
278 }
279
280 void
281 AudioTimeAxisView::update_gain_track_visibility ()
282 {
283         bool const showit = gain_automation_item->get_active();
284
285         if (showit != string_is_affirmative (gain_track->gui_property ("visible"))) {
286                 gain_track->set_marked_for_display (showit);
287
288                 /* now trigger a redisplay */
289
290                 if (!no_redraw) {
291                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
292                 }
293         }
294 }
295
296 void
297 AudioTimeAxisView::update_pan_track_visibility ()
298 {
299         bool const showit = pan_automation_item->get_active();
300         bool changed = false;
301
302         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
303                 if ((*i)->set_marked_for_display (showit)) {
304                         changed = true;
305                 }
306         }
307
308         if (changed) {
309                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
310         }
311 }
312
313 void
314 AudioTimeAxisView::show_all_automation (bool apply_to_selection)
315 {
316         if (apply_to_selection) {
317                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_automation, _1, false));
318         } else {
319
320                 no_redraw = true;
321
322                 RouteTimeAxisView::show_all_automation ();
323
324                 no_redraw = false;
325                 request_redraw ();
326         }
327 }
328
329 void
330 AudioTimeAxisView::show_existing_automation (bool apply_to_selection)
331 {
332         if (apply_to_selection) {
333                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_existing_automation, _1, false));
334         } else {
335                 no_redraw = true;
336
337                 RouteTimeAxisView::show_existing_automation ();
338
339                 no_redraw = false;
340
341                 request_redraw ();
342         }
343 }
344
345 void
346 AudioTimeAxisView::hide_all_automation (bool apply_to_selection)
347 {
348         if (apply_to_selection) {
349                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_automation, _1, false));
350         } else {
351                 no_redraw = true;
352
353                 RouteTimeAxisView::hide_all_automation();
354
355                 no_redraw = false;
356                 request_redraw ();
357         }
358 }
359
360 void
361 AudioTimeAxisView::show_all_xfades (bool apply_to_selection)
362 {
363         if (apply_to_selection) {
364                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_xfades, _1, false));
365         } else {
366                 AudioStreamView* asv = audio_view ();
367                 if (asv) {
368                         asv->show_all_xfades ();
369                 }
370         }
371 }
372
373 void
374 AudioTimeAxisView::hide_all_xfades (bool apply_to_selection)
375 {
376         if (apply_to_selection) {
377                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_xfades, _1, false));
378         } else {
379                 AudioStreamView* asv = audio_view ();
380                 if (asv) {
381                         asv->hide_all_xfades ();
382                 }
383         }
384 }
385
386 void
387 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
388 {
389         AudioStreamView* asv = audio_view();
390         AudioRegionView* rv;
391
392         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
393                 asv->hide_xfades_involving (*rv);
394         }
395 }
396
397 void
398 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
399 {
400         AudioStreamView* asv = audio_view();
401         AudioRegionView* rv;
402
403         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
404                 asv->reveal_xfades_involving (*rv);
405         }
406 }
407
408 void
409 AudioTimeAxisView::route_active_changed ()
410 {
411         update_control_names ();
412 }
413
414
415 /**
416  *    Set up the names of the controls so that they are coloured
417  *    correctly depending on whether this route is inactive or
418  *    selected.
419  */
420
421 void
422 AudioTimeAxisView::update_control_names ()
423 {
424         if (is_audio_track()) {
425                 if (_route->active()) {
426                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
427                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
428                 } else {
429                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
430                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
431                 }
432         } else {
433                 if (_route->active()) {
434                         controls_base_selected_name = "BusControlsBaseSelected";
435                         controls_base_unselected_name = "BusControlsBaseUnselected";
436                 } else {
437                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
438                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
439                 }
440         }
441
442         if (get_selected()) {
443                 controls_ebox.set_name (controls_base_selected_name);
444         } else {
445                 controls_ebox.set_name (controls_base_unselected_name);
446         }
447 }
448
449 void
450 AudioTimeAxisView::build_automation_action_menu (bool for_selection)
451 {
452         using namespace Menu_Helpers;
453
454         RouteTimeAxisView::build_automation_action_menu (for_selection);
455
456         MenuList& automation_items = automation_action_menu->items ();
457
458         automation_items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &AudioTimeAxisView::update_gain_track_visibility)));
459         gain_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
460         gain_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) && 
461                                           (gain_track && string_is_affirmative (gain_track->gui_property ("visible"))));
462
463         _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
464
465         automation_items.push_back (CheckMenuElem (_("Pan"), sigc::mem_fun (*this, &AudioTimeAxisView::update_pan_track_visibility)));
466         pan_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
467         pan_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) &&
468                                          (!pan_tracks.empty() && string_is_affirmative (pan_tracks.front()->gui_property ("visible"))));
469
470         set<Evoral::Parameter> const & params = _route->pannable()->what_can_be_automated ();
471         for (set<Evoral::Parameter>::iterator p = params.begin(); p != params.end(); ++p) {
472                 _main_automation_menu_map[*p] = pan_automation_item;
473         }
474 }
475
476 void
477 AudioTimeAxisView::enter_internal_edit_mode ()
478 {
479         if (audio_view()) {
480                 audio_view()->enter_internal_edit_mode ();
481         }
482 }
483
484 void
485 AudioTimeAxisView::leave_internal_edit_mode ()
486 {
487         if (audio_view()) {
488                 audio_view()->leave_internal_edit_mode ();
489         }
490 }