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