add partial support for mute automation (playback does not work, data is not recorded...
[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/event_type_map.h"
40 #include "ardour/pannable.h"
41 #include "ardour/panner.h"
42 #include "ardour/panner_shell.h"
43
44 #include "ardour_button.h"
45 #include "ardour_ui.h"
46 #include "audio_time_axis.h"
47 #include "automation_line.h"
48 #include "enums.h"
49 #include "gui_thread.h"
50 #include "automation_time_axis.h"
51 #include "keyboard.h"
52 #include "playlist_selector.h"
53 #include "prompter.h"
54 #include "public_editor.h"
55 #include "audio_region_view.h"
56 #include "audio_streamview.h"
57 #include "utils.h"
58
59 #include "i18n.h"
60
61 using namespace std;
62 using namespace ARDOUR;
63 using namespace ARDOUR_UI_UTILS;
64 using namespace PBD;
65 using namespace Gtk;
66 using namespace Editing;
67
68 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session* sess, ArdourCanvas::Canvas& canvas)
69         : AxisView(sess)
70         , RouteTimeAxisView(ed, sess, canvas)
71 {
72 }
73
74 void
75 AudioTimeAxisView::set_route (boost::shared_ptr<Route> rt)
76 {
77         _route = rt;
78
79         /* RouteTimeAxisView::set_route() sets up some things in the View,
80            so it must be created before RouteTimeAxis::set_route() is
81            called.
82         */
83         _view = new AudioStreamView (*this);
84
85         RouteTimeAxisView::set_route (rt);
86
87         _view->apply_color (gdk_color_to_rgba (color()), StreamView::RegionColor);
88
89         // Make sure things are sane...
90         assert(!is_track() || is_audio_track());
91
92         subplugin_menu.set_name ("ArdourContextMenu");
93
94         ignore_toggle = false;
95
96         if (is_audio_track()) {
97                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
98         } else { // bus
99                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
100         }
101
102         /* if set_state above didn't create a gain automation child, we need to make one */
103         if (automation_child (GainAutomation) == 0) {
104                 create_automation_child (GainAutomation, false);
105         }
106
107         /* if set_state above didn't create a mute automation child, we need to make one */
108         if (automation_child (MuteAutomation) == 0) {
109                 create_automation_child (MuteAutomation, false);
110         }
111
112         if (_route->panner_shell()) {
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 void
172 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
173 {
174         if (param.type() == NullAutomation) {
175                 return;
176         }
177
178         AutomationTracks::iterator existing = _automation_tracks.find (param);
179
180         if (existing != _automation_tracks.end()) {
181                 
182                 /* automation track created because we had existing data for
183                  * the processor, but visibility may need to be controlled
184                  * since it will have been set visible by default.
185                  */
186
187                 existing->second->set_marked_for_display (show);
188                 
189                 if (!no_redraw) {
190                         request_redraw ();
191                 }
192
193                 return;
194         }
195
196         if (param.type() == GainAutomation) {
197
198                 create_gain_automation_child (param, show);
199
200         } else if (param.type() == PanWidthAutomation ||
201                    param.type() == PanElevationAutomation ||
202                    param.type() == PanAzimuthAutomation) {
203
204                 ensure_pan_views (show);
205
206         } else if (param.type() == PluginAutomation) {
207
208                 /* handled elsewhere */
209
210         } else if (param.type() == MuteAutomation) {
211
212                 create_mute_automation_child (param, show);
213                 
214
215         } else {
216                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
217         }
218 }
219
220 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
221  *  panners that we have.
222  *
223  *  @param show true to show any new views that we create, otherwise false.
224  */
225 void
226 AudioTimeAxisView::ensure_pan_views (bool show)
227 {
228         bool changed = false;
229         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
230                 changed = true;
231                 (*i)->set_marked_for_display (false);
232         }
233         if (changed) {
234                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
235         }
236         pan_tracks.clear();
237
238         if (!_route->panner()) {
239                 return;
240         }
241
242         set<Evoral::Parameter> params = _route->panner()->what_can_be_automated();
243         set<Evoral::Parameter>::iterator p;
244
245         for (p = params.begin(); p != params.end(); ++p) {
246                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control = _route->pannable()->automation_control(*p);
247
248                 if (pan_control->parameter().type() == NullAutomation) {
249                         error << "Pan control has NULL automation type!" << endmsg;
250                         continue;
251                 }
252
253                 if (automation_child (pan_control->parameter ()).get () == 0) {
254
255                         /* we don't already have an AutomationTimeAxisView for this parameter */
256
257                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
258
259                         boost::shared_ptr<AutomationTimeAxisView> t (
260                                 new AutomationTimeAxisView (_session,
261                                                             _route,
262                                                             _route->pannable(),
263                                                             pan_control,
264                                                             pan_control->parameter (),
265                                                             _editor,
266                                                             *this,
267                                                             false,
268                                                             parent_canvas,
269                                                             name)
270                                 );
271
272                         pan_tracks.push_back (t);
273                         add_automation_child (*p, t, show);
274                 } else {
275                         pan_tracks.push_back (automation_child (pan_control->parameter ()));
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_mute_track_visibility ()
298 {
299         bool const showit = mute_automation_item->get_active();
300
301         if (showit != string_is_affirmative (mute_track->gui_property ("visible"))) {
302                 mute_track->set_marked_for_display (showit);
303
304                 /* now trigger a redisplay */
305
306                 if (!no_redraw) {
307                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
308                 }
309         }
310 }
311
312 void
313 AudioTimeAxisView::update_pan_track_visibility ()
314 {
315         bool const showit = pan_automation_item->get_active();
316         bool changed = false;
317
318         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
319                 if ((*i)->set_marked_for_display (showit)) {
320                         changed = true;
321                 }
322         }
323
324         if (changed) {
325                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
326         }
327 }
328
329 void
330 AudioTimeAxisView::show_all_automation (bool apply_to_selection)
331 {
332         if (apply_to_selection) {
333                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_automation, _1, false));
334         } else {
335
336                 no_redraw = true;
337
338                 RouteTimeAxisView::show_all_automation ();
339
340                 no_redraw = false;
341                 request_redraw ();
342         }
343 }
344
345 void
346 AudioTimeAxisView::show_existing_automation (bool apply_to_selection)
347 {
348         if (apply_to_selection) {
349                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_existing_automation, _1, false));
350         } else {
351                 no_redraw = true;
352
353                 RouteTimeAxisView::show_existing_automation ();
354
355                 no_redraw = false;
356
357                 request_redraw ();
358         }
359 }
360
361 void
362 AudioTimeAxisView::hide_all_automation (bool apply_to_selection)
363 {
364         if (apply_to_selection) {
365                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_automation, _1, false));
366         } else {
367                 no_redraw = true;
368
369                 RouteTimeAxisView::hide_all_automation();
370
371                 no_redraw = false;
372                 request_redraw ();
373         }
374 }
375
376 void
377 AudioTimeAxisView::route_active_changed ()
378 {
379         update_control_names ();
380 }
381
382
383 /**
384  *    Set up the names of the controls so that they are coloured
385  *    correctly depending on whether this route is inactive or
386  *    selected.
387  */
388
389 void
390 AudioTimeAxisView::update_control_names ()
391 {
392         if (is_audio_track()) {
393                 if (_route->active()) {
394                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
395                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
396                 } else {
397                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
398                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
399                 }
400         } else {
401                 if (_route->active()) {
402                         controls_base_selected_name = "BusControlsBaseSelected";
403                         controls_base_unselected_name = "BusControlsBaseUnselected";
404                 } else {
405                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
406                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
407                 }
408         }
409
410         if (get_selected()) {
411                 controls_ebox.set_name (controls_base_selected_name);
412         } else {
413                 controls_ebox.set_name (controls_base_unselected_name);
414         }
415 }
416
417 void
418 AudioTimeAxisView::build_automation_action_menu (bool for_selection)
419 {
420         using namespace Menu_Helpers;
421
422         RouteTimeAxisView::build_automation_action_menu (for_selection);
423
424         MenuList& automation_items = automation_action_menu->items ();
425
426         automation_items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &AudioTimeAxisView::update_gain_track_visibility)));
427         gain_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&automation_items.back ());
428         gain_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) && 
429                                           (gain_track && string_is_affirmative (gain_track->gui_property ("visible"))));
430
431         _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
432
433         automation_items.push_back (CheckMenuElem (_("Mute"), sigc::mem_fun (*this, &AudioTimeAxisView::update_mute_track_visibility)));
434         mute_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&automation_items.back ());
435         mute_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) && 
436                                           (mute_track && string_is_affirmative (mute_track->gui_property ("visible"))));
437
438         _main_automation_menu_map[Evoral::Parameter(MuteAutomation)] = mute_automation_item;
439
440         if (!pan_tracks.empty()) {
441                 automation_items.push_back (CheckMenuElem (_("Pan"), sigc::mem_fun (*this, &AudioTimeAxisView::update_pan_track_visibility)));
442                 pan_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&automation_items.back ());
443                 pan_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) &&
444                                                  (!pan_tracks.empty() && string_is_affirmative (pan_tracks.front()->gui_property ("visible"))));
445
446                 set<Evoral::Parameter> const & params = _route->pannable()->what_can_be_automated ();
447                 for (set<Evoral::Parameter>::const_iterator p = params.begin(); p != params.end(); ++p) {
448                         _main_automation_menu_map[*p] = pan_automation_item;
449                 }
450         }
451 }
452
453 void
454 AudioTimeAxisView::enter_internal_edit_mode ()
455 {
456         if (audio_view()) {
457                 audio_view()->enter_internal_edit_mode ();
458         }
459 }
460
461 void
462 AudioTimeAxisView::leave_internal_edit_mode ()
463 {
464         if (audio_view()) {
465                 audio_view()->leave_internal_edit_mode ();
466         }
467 }