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