Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[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);
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 (bind (
112                                 mem_fun(*this, &AudioTimeAxisView::ensure_pan_views),
113                                 false));
114         }
115
116         /* map current state of the route */
117
118         processors_changed ();
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 (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"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
185                 items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
186         }
187 }
188
189 Gtk::Menu*
190 AudioTimeAxisView::build_mode_menu()
191 {
192         using namespace Menu_Helpers;
193
194         Menu* mode_menu = manage (new Menu);
195         MenuList& items = mode_menu->items();
196         mode_menu->set_name ("ArdourContextMenu");
197
198         RadioMenuItem::Group mode_group;
199
200         items.push_back (RadioMenuElem (mode_group, _("Normal"),
201                                 bind (mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::Normal)));
202         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
203
204         items.push_back (RadioMenuElem (mode_group, _("Non Overlapping"),
205                                 bind (mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::NonLayered)));
206         non_layered_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
207
208         items.push_back (RadioMenuElem (mode_group, _("Tape"),
209                                 bind (mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::Destructive)));
210         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
211
212         switch (track()->mode()) {
213                 case ARDOUR::Destructive:
214                         destructive_track_mode_item->set_active ();
215                         break;
216                 case ARDOUR::NonLayered:
217                         non_layered_track_mode_item->set_active ();
218                         break;
219                 case ARDOUR::Normal:
220                         normal_track_mode_item->set_active ();
221                         break;
222         }
223
224         return mode_menu;
225 }
226
227 void
228 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
229 {
230         AudioStreamView* asv = audio_view();
231
232         if (asv) {
233                 asv->set_show_waveforms_recording (yn);
234         }
235 }
236
237 void
238 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
239 {
240         if (param.type() == GainAutomation) {
241
242                 boost::shared_ptr<AutomationControl> c = _route->gain_control();
243                 if (!c) {
244                         error << "Route has no gain automation, unable to add automation track view." << endmsg;
245                         return;
246                 }
247
248                 boost::shared_ptr<AutomationTimeAxisView>
249                         gain_track(new AutomationTimeAxisView (_session,
250                                                                _route, _route->amp(), c,
251                                                                _editor,
252                                                                *this,
253                                                                false,
254                                                                parent_canvas,
255                                                                _route->amp()->describe_parameter(param)));
256
257                 add_automation_child(Evoral::Parameter(GainAutomation), gain_track, show);
258
259         } else if (param.type() == PanAutomation) {
260
261                 ensure_xml_node ();
262                 ensure_pan_views (show);
263
264         } else {
265                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
266         }
267 }
268
269 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
270  *  panners that we have.
271  *
272  *  @param show true to show any new views that we create, otherwise false.
273  */
274 void
275 AudioTimeAxisView::ensure_pan_views (bool show)
276 {
277         if (!_route->panner()) {
278                 return;
279         }
280
281         const set<Evoral::Parameter>& params = _route->panner()->what_can_be_automated();
282         set<Evoral::Parameter>::iterator p;
283
284         for (p = params.begin(); p != params.end(); ++p) {
285                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control
286                         = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
287                                 _route->panner()->data().control(*p));
288
289                 if (pan_control->parameter().type() == NullAutomation) {
290                         error << "Pan control has NULL automation type!" << endmsg;
291                         continue;
292                 }
293
294                 if (automation_child (pan_control->parameter ()).get () == 0) {
295
296                         /* we don't already have an AutomationTimeAxisView for this parameter */
297
298                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
299
300                         boost::shared_ptr<AutomationTimeAxisView> pan_track (
301                                 new AutomationTimeAxisView (_session,
302                                                             _route, _route->panner(), pan_control,
303                                                             _editor,
304                                                             *this,
305                                                             false,
306                                                             parent_canvas,
307                                                             name));
308
309                         add_automation_child (*p, pan_track, show);
310                 }
311         }
312 }
313 #if 0
314 void
315 AudioTimeAxisView::toggle_gain_track ()
316 {
317         bool showit = gain_automation_item->get_active();
318
319         if (showit != gain_track->marked_for_display()) {
320                 if (showit) {
321                         gain_track->set_marked_for_display (true);
322                         gain_track->canvas_display->show();
323                         gain_track->canvas_background->show();
324                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
325                 } else {
326                         gain_track->set_marked_for_display (false);
327                         gain_track->hide ();
328                         gain_track->get_state_node()->add_property ("shown", X_("no"));
329                 }
330
331                 /* now trigger a redisplay */
332
333                 if (!no_redraw) {
334                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
335                 }
336         }
337 }
338
339 void
340 AudioTimeAxisView::gain_hidden ()
341 {
342         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
343
344         if (gain_automation_item && !_hidden) {
345                 gain_automation_item->set_active (false);
346         }
347
348          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
349 }
350
351 void
352 AudioTimeAxisView::toggle_pan_track ()
353 {
354         bool showit = pan_automation_item->get_active();
355
356         if (showit != pan_track->marked_for_display()) {
357                 if (showit) {
358                         pan_track->set_marked_for_display (true);
359                         pan_track->canvas_display->show();
360                         pan_track->canvas_background->show();
361                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
362                 } else {
363                         pan_track->set_marked_for_display (false);
364                         pan_track->hide ();
365                         pan_track->get_state_node()->add_property ("shown", X_("no"));
366                 }
367
368                 /* now trigger a redisplay */
369         }
370 }
371 #endif
372
373 void
374 AudioTimeAxisView::show_all_automation ()
375 {
376         no_redraw = true;
377
378         RouteTimeAxisView::show_all_automation ();
379
380         no_redraw = false;
381
382          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
383 }
384
385 void
386 AudioTimeAxisView::show_existing_automation ()
387 {
388         no_redraw = true;
389
390         RouteTimeAxisView::show_existing_automation ();
391
392         no_redraw = false;
393
394          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
395 }
396
397 void
398 AudioTimeAxisView::hide_all_automation ()
399 {
400         no_redraw = true;
401
402         RouteTimeAxisView::hide_all_automation();
403
404         no_redraw = false;
405          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
406 }
407
408 void
409 AudioTimeAxisView::show_all_xfades ()
410 {
411         AudioStreamView* asv = audio_view();
412
413         if (asv) {
414                 asv->show_all_xfades ();
415         }
416 }
417
418 void
419 AudioTimeAxisView::hide_all_xfades ()
420 {
421         AudioStreamView* asv = audio_view();
422
423         if (asv) {
424                 asv->hide_all_xfades ();
425         }
426 }
427
428 void
429 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
430 {
431         AudioStreamView* asv = audio_view();
432         AudioRegionView* rv;
433
434         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
435                 asv->hide_xfades_involving (*rv);
436         }
437 }
438
439 void
440 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
441 {
442         AudioStreamView* asv = audio_view();
443         AudioRegionView* rv;
444
445         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
446                 asv->reveal_xfades_involving (*rv);
447         }
448 }
449
450 void
451 AudioTimeAxisView::route_active_changed ()
452 {
453         RouteTimeAxisView::route_active_changed ();
454         update_control_names ();
455 }
456
457
458 /**
459  *    Set up the names of the controls so that they are coloured
460  *    correctly depending on whether this route is inactive or
461  *    selected.
462  */
463
464 void
465 AudioTimeAxisView::update_control_names ()
466 {
467         if (is_audio_track()) {
468                 if (_route->active()) {
469                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
470                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
471                 } else {
472                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
473                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
474                 }
475         } else {
476                 if (_route->active()) {
477                         controls_base_selected_name = "BusControlsBaseSelected";
478                         controls_base_unselected_name = "BusControlsBaseUnselected";
479                 } else {
480                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
481                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
482                 }
483         }
484
485         if (get_selected()) {
486                 controls_ebox.set_name (controls_base_selected_name);
487         } else {
488                 controls_ebox.set_name (controls_base_unselected_name);
489         }
490 }