Pan automation/serialization fixes.
[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/audioplaylist.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/processor.h>
43 #include <ardour/location.h>
44 #include <ardour/panner.h>
45 #include <ardour/playlist.h>
46 #include <ardour/session.h>
47 #include <ardour/session_playlist.h>
48 #include <ardour/utils.h>
49
50 #include "ardour_ui.h"
51 #include "audio_time_axis.h"
52 #include "automation_line.h"
53 #include "canvas_impl.h"
54 #include "crossfade_view.h"
55 #include "enums.h"
56 #include "automation_time_axis.h"
57 #include "keyboard.h"
58 #include "playlist_selector.h"
59 #include "prompter.h"
60 #include "public_editor.h"
61 #include "audio_region_view.h"
62 #include "simplerect.h"
63 #include "audio_streamview.h"
64 #include "utils.h"
65
66 #include <ardour/audio_track.h>
67
68 #include "i18n.h"
69
70 using namespace ARDOUR;
71 using namespace PBD;
72 using namespace Gtk;
73 using namespace Editing;
74
75
76 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
77         : AxisView(sess)
78         , RouteTimeAxisView(ed, sess, rt, canvas)
79 {
80         // Make sure things are sane...
81         assert(!is_track() || is_audio_track());
82
83         subplugin_menu.set_name ("ArdourContextMenu");
84         waveform_item = 0;
85
86         _view = new AudioStreamView (*this);
87
88         create_automation_child (GainAutomation);
89         create_automation_child (PanAutomation);
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 ("AudioTimeAxisViewControlsBaseUnselected");
98         else // bus
99                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
100
101         /* map current state of the route */
102
103         processors_changed ();
104         reset_processor_automation_curves ();
105
106         ensure_xml_node ();
107
108         set_state (*xml_node);
109         
110         _route->panner().Changed.connect (mem_fun(*this, &AudioTimeAxisView::update_pans));
111
112         update_control_names ();
113
114         if (is_audio_track()) {
115
116                 /* ask for notifications of any new RegionViews */
117                 _view->RegionViewAdded.connect (mem_fun(*this, &AudioTimeAxisView::region_view_added));
118                 _view->attach ();
119         }
120
121         post_construct ();
122 }
123
124 AudioTimeAxisView::~AudioTimeAxisView ()
125 {
126 }
127
128 AudioStreamView*
129 AudioTimeAxisView::audio_view()
130 {
131         return dynamic_cast<AudioStreamView*>(_view);
132 }
133
134 guint32
135 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
136 {
137         ensure_xml_node ();
138         xml_node->add_property ("shown_editor", "yes");
139                 
140         return TimeAxisView::show_at (y, nth, parent);
141 }
142
143 void
144 AudioTimeAxisView::hide ()
145 {
146         ensure_xml_node ();
147         xml_node->add_property ("shown_editor", "no");
148
149         TimeAxisView::hide ();
150 }
151
152 void
153 AudioTimeAxisView::append_extra_display_menu_items ()
154 {
155         using namespace Menu_Helpers;
156
157         MenuList& items = display_menu->items();
158
159         // crossfade stuff
160         items.push_back (MenuElem (_("Hide all crossfades"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
161         items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
162
163         // waveform menu
164         Menu *waveform_menu = manage(new Menu);
165         MenuList& waveform_items = waveform_menu->items();
166         waveform_menu->set_name ("ArdourContextMenu");
167         
168         waveform_items.push_back (CheckMenuElem (_("Show waveforms"), mem_fun(*this, &AudioTimeAxisView::toggle_waveforms)));
169         waveform_item = static_cast<CheckMenuItem *> (&waveform_items.back());
170         ignore_toggle = true;
171         waveform_item->set_active (editor.show_waveforms());
172         ignore_toggle = false;
173
174         waveform_items.push_back (SeparatorElem());
175         
176         RadioMenuItem::Group group;
177         
178         waveform_items.push_back (RadioMenuElem (group, _("Traditional"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Traditional)));
179         traditional_item = static_cast<RadioMenuItem *> (&waveform_items.back());
180
181         waveform_items.push_back (RadioMenuElem (group, _("Rectified"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Rectified)));
182         rectified_item = static_cast<RadioMenuItem *> (&waveform_items.back());
183
184         waveform_items.push_back (SeparatorElem());
185         
186         RadioMenuItem::Group group2;
187
188         waveform_items.push_back (RadioMenuElem (group2, _("Linear"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_scale), LinearWaveform)));
189         linearscale_item = static_cast<RadioMenuItem *> (&waveform_items.back());
190
191         waveform_items.push_back (RadioMenuElem (group2, _("Logarithmic"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_scale), LogWaveform)));
192         logscale_item = static_cast<RadioMenuItem *> (&waveform_items.back());
193
194         // setting initial item state
195         AudioStreamView* asv = audio_view();
196         if (asv) {
197                 ignore_toggle = true;
198                 if (asv->get_waveform_shape() == Rectified) 
199                         rectified_item->set_active(true);
200                 else traditional_item->set_active(true);
201
202                 if (asv->get_waveform_scale() == LogWaveform) 
203                         logscale_item->set_active(true);
204                 else linearscale_item->set_active(true);
205                 ignore_toggle = false;
206         }
207
208         items.push_back (MenuElem (_("Waveform"), *waveform_menu));
209
210
211         Menu *layers_menu = manage(new Menu);
212         MenuList &layers_items = layers_menu->items();
213         layers_menu->set_name("ArdourContextMenu");
214
215         RadioMenuItem::Group layers_group;
216         
217         layers_items.push_back(RadioMenuElem (layers_group, _("Overlaid"), bind (mem_fun (*this, &AudioTimeAxisView::set_layer_display), Overlaid)));
218         layers_items.push_back(RadioMenuElem (layers_group, _("Stacked"), bind (mem_fun (*this, &AudioTimeAxisView::set_layer_display), Stacked)));
219
220         items.push_back (MenuElem (_("Layers"), *layers_menu));
221 }
222
223 void
224 AudioTimeAxisView::toggle_waveforms ()
225 {
226         AudioStreamView* asv = audio_view();
227         assert(asv);
228
229         if (asv && waveform_item && !ignore_toggle) {
230                 asv->set_show_waveforms (waveform_item->get_active());
231         }
232 }
233
234 void
235 AudioTimeAxisView::set_show_waveforms (bool yn)
236 {
237         AudioStreamView* asv = audio_view();
238         assert(asv);
239
240         if (waveform_item) {
241                 waveform_item->set_active (yn);
242         } else {
243                 asv->set_show_waveforms (yn);
244         }
245 }
246
247 void
248 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
249 {
250         AudioStreamView* asv = audio_view();
251
252         if (asv) {
253                 asv->set_show_waveforms_recording (yn);
254         }
255 }
256
257 void
258 AudioTimeAxisView::set_waveform_shape (WaveformShape shape)
259 {
260         AudioStreamView* asv = audio_view();
261
262         if (asv && !ignore_toggle) {
263                 asv->set_waveform_shape (shape);
264         }
265
266         map_frozen ();
267 }       
268
269 void
270 AudioTimeAxisView::set_waveform_scale (WaveformScale scale)
271 {
272         AudioStreamView* asv = audio_view();
273
274         if (asv && !ignore_toggle) {
275                 asv->set_waveform_scale (scale);
276         }
277
278         map_frozen ();
279 }       
280
281 void
282 AudioTimeAxisView::create_automation_child (ParamID param)
283 {
284         if (param.type() == GainAutomation) {
285
286                 boost::shared_ptr<AutomationControl> c = _route->gain_control();
287                 if (!c) {
288                         error << "Route has no gain automation, unable to add automation track view." << endmsg;
289                         return;
290                 }
291
292                 boost::shared_ptr<AutomationTimeAxisView> gain_track(new AutomationTimeAxisView (_session,
293                                 _route, _route, c,
294                                 editor,
295                                 *this,
296                                 parent_canvas,
297                                 _route->describe_parameter(param),
298                                 c->list()->param_id().to_string() /* FIXME: correct state name? */));
299
300                 add_automation_child(ParamID(GainAutomation), gain_track);
301
302         } else if (param.type() == PanAutomation) {
303
304                 ensure_xml_node ();
305                 update_pans ();
306
307         } else {
308                 error << "AudioTimeAxisView: unknown automation child " << param.to_string() << endmsg;
309         }
310 }
311
312 void
313 AudioTimeAxisView::update_pans ()
314 {
315         Panner::iterator p;
316
317         /* This is a filthy kludge until the panner stuff gets up to speed. */
318
319         /* Remove all our old automation tracks.  Slowly. */
320         /*while (true) {
321                 bool found = false;
322                 for (AutomationTracks::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
323                         if (i->first.type() == PanAutomation) {
324                                 remove_child(i->second->track);
325                                 delete i->second;
326                                 _automation_tracks.erase(i);
327                                 found = true;
328                                 break;
329                         }
330                 }
331
332                 if ( ! found)
333                         break;
334         }*/
335         
336         /* Man I hate that damn stereo->stereo panner */
337         uint32_t i = 0;
338         for (p = _route->panner().begin(); p != _route->panner().end(); ++p) {
339                 boost::shared_ptr<AutomationControl> pan_control = (*p)->pan_control();
340                 
341                 if (pan_control->list()->param_id().type() == NullAutomation) {
342                         error << "Pan control has NULL automation type!" << endmsg;
343                         continue;
344                 }
345
346                 boost::shared_ptr<AutomationTimeAxisView> pan_track(new AutomationTimeAxisView (_session,
347                                         _route, _route/*FIXME*/, pan_control, 
348                                         editor,
349                                         *this,
350                                         parent_canvas,
351                                         _route->describe_parameter(pan_control->list()->param_id()),
352                                         pan_control->list()->param_id().to_string()/* FIXME: correct state name? */));
353                 add_automation_child(ParamID(PanAutomation, i), pan_track);
354                 ++i;
355         }
356 }
357                 
358 void
359 AudioTimeAxisView::show_all_automation ()
360 {
361         no_redraw = true;
362
363         pan_automation_item->set_active (true);
364         gain_automation_item->set_active (true);
365         
366         RouteTimeAxisView::show_all_automation ();
367
368         no_redraw = false;
369
370          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
371 }
372
373 void
374 AudioTimeAxisView::show_existing_automation ()
375 {
376         no_redraw = true;
377
378         pan_automation_item->set_active (true);
379         gain_automation_item->set_active (true);
380
381         RouteTimeAxisView::show_existing_automation ();
382
383         no_redraw = false;
384
385          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
386 }
387
388 void
389 AudioTimeAxisView::hide_all_automation ()
390 {
391         no_redraw = true;
392
393         pan_automation_item->set_active (false);
394         gain_automation_item->set_active (false);
395
396         RouteTimeAxisView::hide_all_automation();
397
398         no_redraw = false;
399          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
400 }
401
402 void
403 AudioTimeAxisView::show_all_xfades ()
404 {
405         AudioStreamView* asv = audio_view();
406
407         if (asv) {
408                 asv->show_all_xfades ();
409         }
410 }
411
412 void
413 AudioTimeAxisView::hide_all_xfades ()
414 {
415         AudioStreamView* asv = audio_view();
416         
417         if (asv) {
418                 asv->hide_all_xfades ();
419         }
420 }
421
422 void
423 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
424 {
425         AudioStreamView* asv = audio_view();
426         AudioRegionView* rv;
427
428         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
429                 asv->hide_xfades_involving (*rv);
430         }
431 }
432
433 void
434 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
435 {
436         AudioStreamView* asv = audio_view();
437         AudioRegionView* rv;
438
439         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
440                 asv->reveal_xfades_involving (*rv);
441         }
442 }
443
444 void
445 AudioTimeAxisView::route_active_changed ()
446 {
447         RouteTimeAxisView::route_active_changed ();
448         update_control_names ();
449 }
450
451
452 /**
453  *    Set up the names of the controls so that they are coloured
454  *    correctly depending on whether this route is inactive or
455  *    selected.
456  */
457
458 void
459 AudioTimeAxisView::update_control_names ()
460 {
461         if (is_audio_track()) {
462                 if (_route->active()) {
463                         controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
464                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
465                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
466                 } else {
467                         controls_ebox.set_name ("AudioTrackControlsBaseInactiveUnselected");
468                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
469                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
470                 }
471         } else {
472                 if (_route->active()) {
473                         controls_ebox.set_name ("BusControlsBaseUnselected");
474                         controls_base_selected_name = "BusControlsBaseSelected";
475                         controls_base_unselected_name = "BusControlsBaseUnselected";
476                 } else {
477                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
478                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
479                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
480                 }
481         }
482 }
483
484 void
485 AudioTimeAxisView::set_layer_display (LayerDisplay d)
486 {
487         AudioStreamView* asv = audio_view ();
488         if (asv) {
489                 asv->set_layer_display (d);
490         }
491 }