Clean up some confusion with AutomationControls in AutomationTimeAxisViews that have...
[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/playlist.h"
46 #include "ardour/processor.h"
47 #include "ardour/profile.h"
48 #include "ardour/session.h"
49 #include "ardour/session_playlist.h"
50 #include "ardour/utils.h"
51
52 #include "ardour_ui.h"
53 #include "audio_time_axis.h"
54 #include "automation_line.h"
55 #include "canvas_impl.h"
56 #include "crossfade_view.h"
57 #include "enums.h"
58 #include "gui_thread.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, Stateful::loading_state_version);
104
105         /* if set_state above didn't create a gain automation child, we need to make one */
106         if (automation_child (GainAutomation) == 0) {
107                 create_automation_child (GainAutomation, false);
108         }
109
110         if (_route->panner()) {
111                 _route->panner()->Changed.connect (*this, invalidator (*this), 
112                                                    boost::bind (&AudioTimeAxisView::ensure_pan_views, this, false), gui_context());
113         }
114
115         /* map current state of the route */
116
117         processors_changed (RouteProcessorChange ());
118         reset_processor_automation_curves ();
119         ensure_pan_views (false);
120         update_control_names ();
121
122         if (is_audio_track()) {
123
124                 /* ask for notifications of any new RegionViews */
125                 _view->RegionViewAdded.connect (sigc::mem_fun(*this, &AudioTimeAxisView::region_view_added));
126
127                 if (!_editor.have_idled()) {
128                         /* first idle will do what we need */
129                 } else {
130                         first_idle ();
131                 }
132
133         } else {
134                 post_construct ();
135         }
136 }
137
138 AudioTimeAxisView::~AudioTimeAxisView ()
139 {
140 }
141
142 void
143 AudioTimeAxisView::first_idle ()
144 {
145         _view->attach ();
146         post_construct ();
147 }
148
149 AudioStreamView*
150 AudioTimeAxisView::audio_view()
151 {
152         return dynamic_cast<AudioStreamView*>(_view);
153 }
154
155 guint32
156 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
157 {
158         ensure_xml_node ();
159         xml_node->add_property ("shown-editor", "yes");
160
161         return TimeAxisView::show_at (y, nth, parent);
162 }
163
164 void
165 AudioTimeAxisView::hide ()
166 {
167         ensure_xml_node ();
168         xml_node->add_property ("shown-editor", "no");
169
170         TimeAxisView::hide ();
171 }
172
173
174 void
175 AudioTimeAxisView::append_extra_display_menu_items ()
176 {
177         using namespace Menu_Helpers;
178
179         MenuList& items = display_menu->items();
180
181         // crossfade stuff
182         if (!Profile->get_sae()) {
183                 items.push_back (MenuElem (_("Hide All Crossfades"), sigc::mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
184                 items.push_back (MenuElem (_("Show All Crossfades"), sigc::mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
185         }
186 }
187
188 void
189 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
190 {
191         if (param.type() == GainAutomation) {
192
193                 create_gain_automation_child (param, show);
194
195         } else if (param.type() == PanWidthAutomation ||
196                    param.type() == PanElevationAutomation ||
197                    param.type() == PanAzimuthAutomation) {
198
199                 ensure_xml_node ();
200                 ensure_pan_views (show);
201
202         } else if (param.type() == PluginAutomation) {
203                 
204                 /* handled elsewhere */
205
206         } else {
207                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
208         }
209 }
210
211 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
212  *  panners that we have.
213  *
214  *  @param show true to show any new views that we create, otherwise false.
215  */
216 void
217 AudioTimeAxisView::ensure_pan_views (bool show)
218 {
219         if (!_route->panner()) {
220                 return;
221         }
222
223         set<Evoral::Parameter> params = _route->panner()->what_can_be_automated();
224         set<Evoral::Parameter>::iterator p;
225
226         for (p = params.begin(); p != params.end(); ++p) {
227                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control = _route->pannable()->automation_control(*p);
228
229                 if (pan_control->parameter().type() == NullAutomation) {
230                         error << "Pan control has NULL automation type!" << endmsg;
231                         continue;
232                 }
233
234                 if (automation_child (pan_control->parameter ()).get () == 0) {
235
236                         /* we don't already have an AutomationTimeAxisView for this parameter */
237
238                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
239
240                         boost::shared_ptr<AutomationTimeAxisView> t (
241                                 new AutomationTimeAxisView (_session,
242                                                             _route, 
243                                                             _route->pannable(), 
244                                                             pan_control,
245                                                             pan_control->parameter (),
246                                                             _editor,
247                                                             *this,
248                                                             false,
249                                                             parent_canvas,
250                                                             name)
251                                 );
252
253                         pan_tracks.push_back (t);
254                         add_automation_child (*p, t, show);
255                 }
256         }
257 }
258
259 void
260 AudioTimeAxisView::update_gain_track_visibility ()
261 {
262         bool const showit = gain_automation_item->get_active();
263
264         if (showit != gain_track->marked_for_display()) {
265                 if (showit) {
266                         gain_track->set_marked_for_display (true);
267                         gain_track->canvas_display()->show();
268                         gain_track->canvas_background()->show();
269                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
270                 } else {
271                         gain_track->set_marked_for_display (false);
272                         gain_track->hide ();
273                         gain_track->get_state_node()->add_property ("shown", X_("no"));
274                 }
275
276                 /* now trigger a redisplay */
277
278                 if (!no_redraw) {
279                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
280                 }
281         }
282 }
283
284 void
285 AudioTimeAxisView::update_pan_track_visibility ()
286 {
287         bool const showit = pan_automation_item->get_active();
288
289         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
290
291                 if (showit != (*i)->marked_for_display()) {
292                         if (showit) {
293                                 (*i)->set_marked_for_display (true);
294                                 (*i)->canvas_display()->show();
295                                 (*i)->canvas_background()->show();
296                                 (*i)->get_state_node()->add_property ("shown", X_("yes"));
297                         } else {
298                                 (*i)->set_marked_for_display (false);
299                                 (*i)->hide ();
300                                 (*i)->get_state_node()->add_property ("shown", X_("no"));
301                         }
302                         
303                         /* now trigger a redisplay */
304                         if (!no_redraw) {
305                                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
306                         }
307                 }
308         }
309 }
310
311 void
312 AudioTimeAxisView::show_all_automation ()
313 {
314         no_redraw = true;
315
316         RouteTimeAxisView::show_all_automation ();
317
318         no_redraw = false;
319
320         _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
321 }
322
323 void
324 AudioTimeAxisView::show_existing_automation ()
325 {
326         no_redraw = true;
327
328         RouteTimeAxisView::show_existing_automation ();
329
330         no_redraw = false;
331
332         _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
333 }
334
335 void
336 AudioTimeAxisView::hide_all_automation ()
337 {
338         no_redraw = true;
339
340         RouteTimeAxisView::hide_all_automation();
341
342         no_redraw = false;
343         _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
344 }
345
346 void
347 AudioTimeAxisView::show_all_xfades ()
348 {
349         AudioStreamView* asv = audio_view();
350
351         if (asv) {
352                 asv->show_all_xfades ();
353         }
354 }
355
356 void
357 AudioTimeAxisView::hide_all_xfades ()
358 {
359         AudioStreamView* asv = audio_view();
360
361         if (asv) {
362                 asv->hide_all_xfades ();
363         }
364 }
365
366 void
367 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
368 {
369         AudioStreamView* asv = audio_view();
370         AudioRegionView* rv;
371
372         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
373                 asv->hide_xfades_involving (*rv);
374         }
375 }
376
377 void
378 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
379 {
380         AudioStreamView* asv = audio_view();
381         AudioRegionView* rv;
382
383         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
384                 asv->reveal_xfades_involving (*rv);
385         }
386 }
387
388 void
389 AudioTimeAxisView::route_active_changed ()
390 {
391         RouteTimeAxisView::route_active_changed ();
392         update_control_names ();
393 }
394
395
396 /**
397  *    Set up the names of the controls so that they are coloured
398  *    correctly depending on whether this route is inactive or
399  *    selected.
400  */
401
402 void
403 AudioTimeAxisView::update_control_names ()
404 {
405         if (is_audio_track()) {
406                 if (_route->active()) {
407                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
408                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
409                 } else {
410                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
411                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
412                 }
413         } else {
414                 if (_route->active()) {
415                         controls_base_selected_name = "BusControlsBaseSelected";
416                         controls_base_unselected_name = "BusControlsBaseUnselected";
417                 } else {
418                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
419                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
420                 }
421         }
422
423         if (get_selected()) {
424                 controls_ebox.set_name (controls_base_selected_name);
425         } else {
426                 controls_ebox.set_name (controls_base_unselected_name);
427         }
428 }
429
430 void
431 AudioTimeAxisView::build_automation_action_menu ()
432 {
433         using namespace Menu_Helpers;
434
435         RouteTimeAxisView::build_automation_action_menu ();
436
437         MenuList& automation_items = automation_action_menu->items ();
438
439         automation_items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &AudioTimeAxisView::update_gain_track_visibility)));
440         gain_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
441         gain_automation_item->set_active (gain_track->marked_for_display ());
442
443         _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
444
445         automation_items.push_back (CheckMenuElem (_("Pan"), sigc::mem_fun (*this, &AudioTimeAxisView::update_pan_track_visibility)));
446         pan_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
447         pan_automation_item->set_active (pan_tracks.front()->marked_for_display ());
448
449         set<Evoral::Parameter> const & params = _route->pannable()->what_can_be_automated ();
450         for (set<Evoral::Parameter>::iterator p = params.begin(); p != params.end(); ++p) {
451                 _main_automation_menu_map[*p] = pan_automation_item;
452         }
453 }
454
455 void
456 AudioTimeAxisView::add_processor_to_subplugin_menu (boost::weak_ptr<Processor> wp)
457 {
458         /* we use this override to veto the Amp processor from the plugin menu,
459            as its automation lane can be accessed using the special "Fader" menu
460            option
461         */
462         
463         boost::shared_ptr<Processor> p = wp.lock ();
464         if (!p) {
465                 return;
466         }
467
468         if (boost::dynamic_pointer_cast<Amp> (p) == 0) {
469                 RouteTimeAxisView::add_processor_to_subplugin_menu (wp);
470         }
471 }