Delete trailing whitespace
[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/bindable_button.h>
37 #include <gtkmm2ext/utils.h>
38
39 #include "ardour/amp.h"
40 #include "ardour/audioplaylist.h"
41 #include "ardour/event_type_map.h"
42 #include "ardour/location.h"
43 #include "ardour/pannable.h"
44 #include "ardour/panner.h"
45 #include "ardour/panner_shell.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_child (GainAutomation) == 0) {
108                 create_automation_child (GainAutomation, false);
109         }
110
111         if (_route->panner()) {
112                 _route->panner_shell()->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() && is_track ()) {
184                 items.push_back (MenuElem (_("Hide All Crossfades"), sigc::bind (sigc::mem_fun(*this, &AudioTimeAxisView::hide_all_xfades), true)));
185                 items.push_back (MenuElem (_("Show All Crossfades"), sigc::bind (sigc::mem_fun(*this, &AudioTimeAxisView::show_all_xfades), true)));
186                 items.push_back (SeparatorElem ());
187         }
188 }
189
190 void
191 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
192 {
193         if (param.type() == GainAutomation) {
194
195                 create_gain_automation_child (param, show);
196
197         } else if (param.type() == PanWidthAutomation ||
198                    param.type() == PanElevationAutomation ||
199                    param.type() == PanAzimuthAutomation) {
200
201                 ensure_xml_node ();
202                 ensure_pan_views (show);
203
204         } else if (param.type() == PluginAutomation) {
205
206                 /* handled elsewhere */
207
208         } else {
209                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
210         }
211 }
212
213 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
214  *  panners that we have.
215  *
216  *  @param show true to show any new views that we create, otherwise false.
217  */
218 void
219 AudioTimeAxisView::ensure_pan_views (bool show)
220 {
221         if (!_route->panner()) {
222                 return;
223         }
224
225         set<Evoral::Parameter> params = _route->panner()->what_can_be_automated();
226         set<Evoral::Parameter>::iterator p;
227
228         for (p = params.begin(); p != params.end(); ++p) {
229                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control = _route->pannable()->automation_control(*p);
230
231                 if (pan_control->parameter().type() == NullAutomation) {
232                         error << "Pan control has NULL automation type!" << endmsg;
233                         continue;
234                 }
235
236                 if (automation_child (pan_control->parameter ()).get () == 0) {
237
238                         /* we don't already have an AutomationTimeAxisView for this parameter */
239
240                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
241
242                         boost::shared_ptr<AutomationTimeAxisView> t (
243                                 new AutomationTimeAxisView (_session,
244                                                             _route,
245                                                             _route->pannable(),
246                                                             pan_control,
247                                                             pan_control->parameter (),
248                                                             _editor,
249                                                             *this,
250                                                             false,
251                                                             parent_canvas,
252                                                             name)
253                                 );
254
255                         pan_tracks.push_back (t);
256                         add_automation_child (*p, t, show);
257                 }
258         }
259 }
260
261 void
262 AudioTimeAxisView::update_gain_track_visibility ()
263 {
264         bool const showit = gain_automation_item->get_active();
265
266         if (showit != gain_track->marked_for_display()) {
267                 if (showit) {
268                         gain_track->set_marked_for_display (true);
269                         gain_track->canvas_display()->show();
270                         gain_track->canvas_background()->show();
271                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
272                 } else {
273                         gain_track->set_marked_for_display (false);
274                         gain_track->hide ();
275                         gain_track->get_state_node()->add_property ("shown", X_("no"));
276                 }
277
278                 /* now trigger a redisplay */
279
280                 if (!no_redraw) {
281                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
282                 }
283         }
284 }
285
286 void
287 AudioTimeAxisView::update_pan_track_visibility ()
288 {
289         bool const showit = pan_automation_item->get_active();
290
291         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
292
293                 if (showit != (*i)->marked_for_display()) {
294                         if (showit) {
295                                 (*i)->set_marked_for_display (true);
296                                 (*i)->canvas_display()->show();
297                                 (*i)->canvas_background()->show();
298                                 (*i)->get_state_node()->add_property ("shown", X_("yes"));
299                         } else {
300                                 (*i)->set_marked_for_display (false);
301                                 (*i)->hide ();
302                                 (*i)->get_state_node()->add_property ("shown", X_("no"));
303                         }
304
305                         /* now trigger a redisplay */
306                         if (!no_redraw) {
307                                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
308                         }
309                 }
310         }
311 }
312
313 void
314 AudioTimeAxisView::show_all_automation (bool apply_to_selection)
315 {
316         if (apply_to_selection) {
317                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_automation, _1, false));
318         } else {
319
320                 no_redraw = true;
321
322                 RouteTimeAxisView::show_all_automation ();
323
324                 no_redraw = false;
325
326                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
327         }
328 }
329
330 void
331 AudioTimeAxisView::show_existing_automation (bool apply_to_selection)
332 {
333         if (apply_to_selection) {
334                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_existing_automation, _1, false));
335         } else {
336                 no_redraw = true;
337
338                 RouteTimeAxisView::show_existing_automation ();
339
340                 no_redraw = false;
341
342                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
343         }
344 }
345
346 void
347 AudioTimeAxisView::hide_all_automation (bool apply_to_selection)
348 {
349         if (apply_to_selection) {
350                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_automation, _1, false));
351         } else {
352                 no_redraw = true;
353
354                 RouteTimeAxisView::hide_all_automation();
355
356                 no_redraw = false;
357                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
358         }
359 }
360
361 void
362 AudioTimeAxisView::show_all_xfades (bool apply_to_selection)
363 {
364         if (apply_to_selection) {
365                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_xfades, _1, false));
366         } else {
367                 AudioStreamView* asv = audio_view ();
368                 if (asv) {
369                         asv->show_all_xfades ();
370                 }
371         }
372 }
373
374 void
375 AudioTimeAxisView::hide_all_xfades (bool apply_to_selection)
376 {
377         if (apply_to_selection) {
378                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_xfades, _1, false));
379         } else {
380                 AudioStreamView* asv = audio_view ();
381                 if (asv) {
382                         asv->hide_all_xfades ();
383                 }
384         }
385 }
386
387 void
388 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
389 {
390         AudioStreamView* asv = audio_view();
391         AudioRegionView* rv;
392
393         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
394                 asv->hide_xfades_involving (*rv);
395         }
396 }
397
398 void
399 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
400 {
401         AudioStreamView* asv = audio_view();
402         AudioRegionView* rv;
403
404         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
405                 asv->reveal_xfades_involving (*rv);
406         }
407 }
408
409 void
410 AudioTimeAxisView::route_active_changed ()
411 {
412         update_control_names ();
413 }
414
415
416 /**
417  *    Set up the names of the controls so that they are coloured
418  *    correctly depending on whether this route is inactive or
419  *    selected.
420  */
421
422 void
423 AudioTimeAxisView::update_control_names ()
424 {
425         if (is_audio_track()) {
426                 if (_route->active()) {
427                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
428                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
429                 } else {
430                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
431                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
432                 }
433         } else {
434                 if (_route->active()) {
435                         controls_base_selected_name = "BusControlsBaseSelected";
436                         controls_base_unselected_name = "BusControlsBaseUnselected";
437                 } else {
438                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
439                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
440                 }
441         }
442
443         if (get_selected()) {
444                 controls_ebox.set_name (controls_base_selected_name);
445         } else {
446                 controls_ebox.set_name (controls_base_unselected_name);
447         }
448 }
449
450 void
451 AudioTimeAxisView::build_automation_action_menu (bool for_selection)
452 {
453         using namespace Menu_Helpers;
454
455         RouteTimeAxisView::build_automation_action_menu (for_selection);
456
457         MenuList& automation_items = automation_action_menu->items ();
458
459         automation_items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &AudioTimeAxisView::update_gain_track_visibility)));
460         gain_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
461         gain_automation_item->set_active (gain_track->marked_for_display () && (!for_selection || _editor.get_selection().tracks.size() == 1));
462
463         _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
464
465         automation_items.push_back (CheckMenuElem (_("Pan"), sigc::mem_fun (*this, &AudioTimeAxisView::update_pan_track_visibility)));
466         pan_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
467         pan_automation_item->set_active (pan_tracks.front()->marked_for_display () && (!for_selection || _editor.get_selection().tracks.size() == 1));
468
469         set<Evoral::Parameter> const & params = _route->pannable()->what_can_be_automated ();
470         for (set<Evoral::Parameter>::iterator p = params.begin(); p != params.end(); ++p) {
471                 _main_automation_menu_map[*p] = pan_automation_item;
472         }
473 }
474
475 void
476 AudioTimeAxisView::add_processor_to_subplugin_menu (boost::weak_ptr<Processor> wp)
477 {
478         /* we use this override to veto the Amp processor from the plugin menu,
479            as its automation lane can be accessed using the special "Fader" menu
480            option
481         */
482
483         boost::shared_ptr<Processor> p = wp.lock ();
484         if (!p) {
485                 return;
486         }
487
488         if (boost::dynamic_pointer_cast<Amp> (p) == 0) {
489                 RouteTimeAxisView::add_processor_to_subplugin_menu (wp);
490         }
491 }
492
493 void
494 AudioTimeAxisView::enter_internal_edit_mode ()
495 {
496         if (audio_view()) {
497                 audio_view()->enter_internal_edit_mode ();
498         }
499 }
500
501 void
502 AudioTimeAxisView::leave_internal_edit_mode ()
503 {
504         if (audio_view()) {
505                 audio_view()->leave_internal_edit_mode ();
506         }
507 }