53c1a84848f5bec2a7e00d96197ad2ec3278ca6c
[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                                 _automation_tracks.erase(i);
325                                 found = true;
326                                 break;
327                         }
328                 }
329
330                 if ( ! found)
331                         break;
332         }
333         
334         /* Man I hate that damn stereo->stereo panner */
335         for (p = _route->panner().begin(); p != _route->panner().end(); ++p) {
336                 boost::shared_ptr<AutomationTimeAxisView> pan_track(new AutomationTimeAxisView (_session,
337                                         _route, _route/*FIXME*/, (*p)->pan_control(), 
338                                         editor,
339                                         *this,
340                                         parent_canvas,
341                                         _route->describe_parameter((*p)->pan_control()->list()->param_id()),
342                                         (*p)->pan_control()->list()->param_id().to_string() /* FIXME: correct state name? */));
343         }
344 }
345                 
346 void
347 AudioTimeAxisView::show_all_automation ()
348 {
349         no_redraw = true;
350
351         pan_automation_item->set_active (true);
352         gain_automation_item->set_active (true);
353         
354         RouteTimeAxisView::show_all_automation ();
355
356         no_redraw = false;
357
358          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
359 }
360
361 void
362 AudioTimeAxisView::show_existing_automation ()
363 {
364         no_redraw = true;
365
366         pan_automation_item->set_active (true);
367         gain_automation_item->set_active (true);
368
369         RouteTimeAxisView::show_existing_automation ();
370
371         no_redraw = false;
372
373          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
374 }
375
376 void
377 AudioTimeAxisView::hide_all_automation ()
378 {
379         no_redraw = true;
380
381         pan_automation_item->set_active (false);
382         gain_automation_item->set_active (false);
383
384         RouteTimeAxisView::hide_all_automation();
385
386         no_redraw = false;
387          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
388 }
389
390 void
391 AudioTimeAxisView::show_all_xfades ()
392 {
393         AudioStreamView* asv = audio_view();
394
395         if (asv) {
396                 asv->show_all_xfades ();
397         }
398 }
399
400 void
401 AudioTimeAxisView::hide_all_xfades ()
402 {
403         AudioStreamView* asv = audio_view();
404         
405         if (asv) {
406                 asv->hide_all_xfades ();
407         }
408 }
409
410 void
411 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
412 {
413         AudioStreamView* asv = audio_view();
414         AudioRegionView* rv;
415
416         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
417                 asv->hide_xfades_involving (*rv);
418         }
419 }
420
421 void
422 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
423 {
424         AudioStreamView* asv = audio_view();
425         AudioRegionView* rv;
426
427         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
428                 asv->reveal_xfades_involving (*rv);
429         }
430 }
431
432 void
433 AudioTimeAxisView::route_active_changed ()
434 {
435         RouteTimeAxisView::route_active_changed ();
436         update_control_names ();
437 }
438
439
440 /**
441  *    Set up the names of the controls so that they are coloured
442  *    correctly depending on whether this route is inactive or
443  *    selected.
444  */
445
446 void
447 AudioTimeAxisView::update_control_names ()
448 {
449         if (is_audio_track()) {
450                 if (_route->active()) {
451                         controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
452                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
453                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
454                 } else {
455                         controls_ebox.set_name ("AudioTrackControlsBaseInactiveUnselected");
456                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
457                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
458                 }
459         } else {
460                 if (_route->active()) {
461                         controls_ebox.set_name ("BusControlsBaseUnselected");
462                         controls_base_selected_name = "BusControlsBaseSelected";
463                         controls_base_unselected_name = "BusControlsBaseUnselected";
464                 } else {
465                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
466                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
467                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
468                 }
469         }
470 }
471
472 void
473 AudioTimeAxisView::set_layer_display (LayerDisplay d)
474 {
475         AudioStreamView* asv = audio_view ();
476         if (asv) {
477                 asv->set_layer_display (d);
478         }
479 }