0105e1846cc5e0214d4aed22bee3b27bb6f6f9ac
[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/stop_signal.h>
37 #include <gtkmm2ext/bindable_button.h>
38 #include <gtkmm2ext/utils.h>
39
40 #include "ardour/amp.h"
41 #include "ardour/audio_diskstream.h"
42 #include "ardour/audioplaylist.h"
43 #include "ardour/event_type_map.h"
44 #include "ardour/location.h"
45 #include "ardour/panner.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_ui.h"
54 #include "audio_time_axis.h"
55 #include "automation_line.h"
56 #include "canvas_impl.h"
57 #include "crossfade_view.h"
58 #include "enums.h"
59 #include "automation_time_axis.h"
60 #include "keyboard.h"
61 #include "playlist_selector.h"
62 #include "prompter.h"
63 #include "public_editor.h"
64 #include "audio_region_view.h"
65 #include "simplerect.h"
66 #include "audio_streamview.h"
67 #include "utils.h"
68
69 #include "ardour/audio_track.h"
70
71 #include "i18n.h"
72
73 using namespace std;
74 using namespace ARDOUR;
75 using namespace PBD;
76 using namespace Gtk;
77 using namespace Editing;
78
79 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session* sess, boost::shared_ptr<Route> rt, Canvas& canvas)
80         : AxisView(sess)
81         , RouteTimeAxisView(ed, sess, rt, canvas)
82 {
83         // Make sure things are sane...
84         assert(!is_track() || is_audio_track());
85
86         subplugin_menu.set_name ("ArdourContextMenu");
87
88         _view = new AudioStreamView (*this);
89
90         ignore_toggle = false;
91
92         mute_button->set_active (false);
93         solo_button->set_active (false);
94
95         if (is_audio_track()) {
96                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
97         } else { // bus
98                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
99         }
100
101         ensure_xml_node ();
102
103         set_state (*xml_node, Stateful::loading_state_version);
104
105         /* if set_state above didn't create a gain automation child, we need to make one */
106         if (automation_track (GainAutomation) == 0) {
107                 create_automation_child (GainAutomation, false);
108         }
109
110         if (_route->panner()) {
111                 _route->panner()->Changed.connect (*this, (boost::bind (&AudioTimeAxisView::ensure_pan_views, this, false)));
112         }
113
114         /* map current state of the route */
115
116         processors_changed (RouteProcessorChange ());
117         reset_processor_automation_curves ();
118         ensure_pan_views (false);
119         update_control_names ();
120
121         if (is_audio_track()) {
122
123                 /* ask for notifications of any new RegionViews */
124                 _view->RegionViewAdded.connect (sigc::mem_fun(*this, &AudioTimeAxisView::region_view_added));
125
126                 if (!_editor.have_idled()) {
127                         /* first idle will do what we need */
128                 } else {
129                         first_idle ();
130                 }
131
132         } else {
133                 post_construct ();
134         }
135 }
136
137 AudioTimeAxisView::~AudioTimeAxisView ()
138 {
139 }
140
141 void
142 AudioTimeAxisView::first_idle ()
143 {
144         _view->attach ();
145         post_construct ();
146 }
147
148 AudioStreamView*
149 AudioTimeAxisView::audio_view()
150 {
151         return dynamic_cast<AudioStreamView*>(_view);
152 }
153
154 guint32
155 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
156 {
157         ensure_xml_node ();
158         xml_node->add_property ("shown-editor", "yes");
159
160         return TimeAxisView::show_at (y, nth, parent);
161 }
162
163 void
164 AudioTimeAxisView::hide ()
165 {
166         ensure_xml_node ();
167         xml_node->add_property ("shown-editor", "no");
168
169         TimeAxisView::hide ();
170 }
171
172
173 void
174 AudioTimeAxisView::append_extra_display_menu_items ()
175 {
176         using namespace Menu_Helpers;
177
178         MenuList& items = display_menu->items();
179
180         // crossfade stuff
181         if (!Profile->get_sae()) {
182                 items.push_back (MenuElem (_("Hide all crossfades"), sigc::mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
183                 items.push_back (MenuElem (_("Show all crossfades"), sigc::mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
184         }
185 }
186
187 Gtk::Menu*
188 AudioTimeAxisView::build_mode_menu()
189 {
190         using namespace Menu_Helpers;
191
192         Menu* mode_menu = manage (new Menu);
193         MenuList& items = mode_menu->items();
194         mode_menu->set_name ("ArdourContextMenu");
195
196         RadioMenuItem::Group mode_group;
197
198         items.push_back (RadioMenuElem (mode_group, _("Normal"),
199                                 sigc::bind (sigc::mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::Normal)));
200         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
201
202         items.push_back (RadioMenuElem (mode_group, _("Non Overlapping"),
203                                 sigc::bind (sigc::mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::NonLayered)));
204         non_layered_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
205
206         items.push_back (RadioMenuElem (mode_group, _("Tape"),
207                                 sigc::bind (sigc::mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::Destructive)));
208         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
209
210         switch (track()->mode()) {
211                 case ARDOUR::Destructive:
212                         destructive_track_mode_item->set_active ();
213                         break;
214                 case ARDOUR::NonLayered:
215                         non_layered_track_mode_item->set_active ();
216                         break;
217                 case ARDOUR::Normal:
218                         normal_track_mode_item->set_active ();
219                         break;
220         }
221
222         return mode_menu;
223 }
224
225 void
226 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
227 {
228         AudioStreamView* asv = audio_view();
229
230         if (asv) {
231                 asv->set_show_waveforms_recording (yn);
232         }
233 }
234
235 void
236 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
237 {
238         if (param.type() == GainAutomation) {
239
240                 boost::shared_ptr<AutomationControl> c = _route->gain_control();
241                 if (!c) {
242                         error << "Route has no gain automation, unable to add automation track view." << endmsg;
243                         return;
244                 }
245
246                 boost::shared_ptr<AutomationTimeAxisView>
247                         gain_track(new AutomationTimeAxisView (_session,
248                                                                _route, _route->amp(), c,
249                                                                _editor,
250                                                                *this,
251                                                                false,
252                                                                parent_canvas,
253                                                                _route->amp()->describe_parameter(param)));
254
255                 add_automation_child(Evoral::Parameter(GainAutomation), gain_track, show);
256
257         } else if (param.type() == PanAutomation) {
258
259                 ensure_xml_node ();
260                 ensure_pan_views (show);
261
262         } else {
263                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
264         }
265 }
266
267 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
268  *  panners that we have.
269  *
270  *  @param show true to show any new views that we create, otherwise false.
271  */
272 void
273 AudioTimeAxisView::ensure_pan_views (bool show)
274 {
275         if (!_route->panner()) {
276                 return;
277         }
278
279         const set<Evoral::Parameter>& params = _route->panner()->what_can_be_automated();
280         set<Evoral::Parameter>::iterator p;
281
282         for (p = params.begin(); p != params.end(); ++p) {
283                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control
284                         = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
285                                 _route->panner()->data().control(*p));
286
287                 if (pan_control->parameter().type() == NullAutomation) {
288                         error << "Pan control has NULL automation type!" << endmsg;
289                         continue;
290                 }
291
292                 if (automation_child (pan_control->parameter ()).get () == 0) {
293
294                         /* we don't already have an AutomationTimeAxisView for this parameter */
295
296                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
297
298                         boost::shared_ptr<AutomationTimeAxisView> pan_track (
299                                 new AutomationTimeAxisView (_session,
300                                                             _route, _route->panner(), pan_control,
301                                                             _editor,
302                                                             *this,
303                                                             false,
304                                                             parent_canvas,
305                                                             name));
306
307                         add_automation_child (*p, pan_track, show);
308                 }
309         }
310 }
311 #if 0
312 void
313 AudioTimeAxisView::toggle_gain_track ()
314 {
315         bool showit = gain_automation_item->get_active();
316
317         if (showit != gain_track->marked_for_display()) {
318                 if (showit) {
319                         gain_track->set_marked_for_display (true);
320                         gain_track->canvas_display->show();
321                         gain_track->canvas_background->show();
322                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
323                 } else {
324                         gain_track->set_marked_for_display (false);
325                         gain_track->hide ();
326                         gain_track->get_state_node()->add_property ("shown", X_("no"));
327                 }
328
329                 /* now trigger a redisplay */
330
331                 if (!no_redraw) {
332                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
333                 }
334         }
335 }
336
337 void
338 AudioTimeAxisView::gain_hidden ()
339 {
340         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
341
342         if (gain_automation_item && !_hidden) {
343                 gain_automation_item->set_active (false);
344         }
345
346          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
347 }
348
349 void
350 AudioTimeAxisView::toggle_pan_track ()
351 {
352         bool showit = pan_automation_item->get_active();
353
354         if (showit != pan_track->marked_for_display()) {
355                 if (showit) {
356                         pan_track->set_marked_for_display (true);
357                         pan_track->canvas_display->show();
358                         pan_track->canvas_background->show();
359                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
360                 } else {
361                         pan_track->set_marked_for_display (false);
362                         pan_track->hide ();
363                         pan_track->get_state_node()->add_property ("shown", X_("no"));
364                 }
365
366                 /* now trigger a redisplay */
367         }
368 }
369 #endif
370
371 void
372 AudioTimeAxisView::show_all_automation ()
373 {
374         no_redraw = true;
375
376         RouteTimeAxisView::show_all_automation ();
377
378         no_redraw = false;
379
380          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
381 }
382
383 void
384 AudioTimeAxisView::show_existing_automation ()
385 {
386         no_redraw = true;
387
388         RouteTimeAxisView::show_existing_automation ();
389
390         no_redraw = false;
391
392          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
393 }
394
395 void
396 AudioTimeAxisView::hide_all_automation ()
397 {
398         no_redraw = true;
399
400         RouteTimeAxisView::hide_all_automation();
401
402         no_redraw = false;
403          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
404 }
405
406 void
407 AudioTimeAxisView::show_all_xfades ()
408 {
409         AudioStreamView* asv = audio_view();
410
411         if (asv) {
412                 asv->show_all_xfades ();
413         }
414 }
415
416 void
417 AudioTimeAxisView::hide_all_xfades ()
418 {
419         AudioStreamView* asv = audio_view();
420
421         if (asv) {
422                 asv->hide_all_xfades ();
423         }
424 }
425
426 void
427 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
428 {
429         AudioStreamView* asv = audio_view();
430         AudioRegionView* rv;
431
432         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
433                 asv->hide_xfades_involving (*rv);
434         }
435 }
436
437 void
438 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
439 {
440         AudioStreamView* asv = audio_view();
441         AudioRegionView* rv;
442
443         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
444                 asv->reveal_xfades_involving (*rv);
445         }
446 }
447
448 void
449 AudioTimeAxisView::route_active_changed ()
450 {
451         RouteTimeAxisView::route_active_changed ();
452         update_control_names ();
453 }
454
455
456 /**
457  *    Set up the names of the controls so that they are coloured
458  *    correctly depending on whether this route is inactive or
459  *    selected.
460  */
461
462 void
463 AudioTimeAxisView::update_control_names ()
464 {
465         if (is_audio_track()) {
466                 if (_route->active()) {
467                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
468                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
469                 } else {
470                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
471                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
472                 }
473         } else {
474                 if (_route->active()) {
475                         controls_base_selected_name = "BusControlsBaseSelected";
476                         controls_base_unselected_name = "BusControlsBaseUnselected";
477                 } else {
478                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
479                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
480                 }
481         }
482
483         if (get_selected()) {
484                 controls_ebox.set_name (controls_base_selected_name);
485         } else {
486                 controls_ebox.set_name (controls_base_unselected_name);
487         }
488 }