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