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