Merge remote-tracking branch 'origin/master' into export-dialog
[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/event_type_map.h"
40 #include "ardour/pannable.h"
41 #include "ardour/panner.h"
42 #include "ardour/panner_shell.h"
43
44 #include "ardour_button.h"
45 #include "ardour_ui.h"
46 #include "audio_time_axis.h"
47 #include "automation_line.h"
48 #include "canvas_impl.h"
49 #include "enums.h"
50 #include "gui_thread.h"
51 #include "automation_time_axis.h"
52 #include "keyboard.h"
53 #include "playlist_selector.h"
54 #include "prompter.h"
55 #include "public_editor.h"
56 #include "audio_region_view.h"
57 #include "simplerect.h"
58 #include "audio_streamview.h"
59 #include "utils.h"
60
61 #include "i18n.h"
62
63 using namespace std;
64 using namespace ARDOUR;
65 using namespace PBD;
66 using namespace Gtk;
67 using namespace Editing;
68
69 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session* sess, Canvas& canvas)
70         : AxisView(sess)
71         , RouteTimeAxisView(ed, sess, canvas)
72 {
73 }
74
75 void
76 AudioTimeAxisView::set_route (boost::shared_ptr<Route> rt)
77 {
78         _route = rt;
79
80         /* RouteTimeAxisView::set_route() sets up some things in the View,
81            so it must be created before RouteTimeAxis::set_route() is
82            called.
83         */
84         _view = new AudioStreamView (*this);
85
86         RouteTimeAxisView::set_route (rt);
87
88         _view->apply_color (color (), StreamView::RegionColor);
89
90         // Make sure things are sane...
91         assert(!is_track() || is_audio_track());
92
93         subplugin_menu.set_name ("ArdourContextMenu");
94
95         ignore_toggle = false;
96
97         if (is_audio_track()) {
98                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
99         } else { // bus
100                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
101         }
102
103         /* if set_state above didn't create a gain automation child, we need to make one */
104         if (automation_child (GainAutomation) == 0) {
105                 create_automation_child (GainAutomation, false);
106         }
107
108         if (_route->panner_shell()) {
109                 _route->panner_shell()->Changed.connect (*this, invalidator (*this),
110                                                          boost::bind (&AudioTimeAxisView::ensure_pan_views, this, false), gui_context());
111         }
112
113         /* map current state of the route */
114
115         processors_changed (RouteProcessorChange ());
116         reset_processor_automation_curves ();
117         ensure_pan_views (false);
118         update_control_names ();
119
120         if (is_audio_track()) {
121
122                 /* ask for notifications of any new RegionViews */
123                 _view->RegionViewAdded.connect (sigc::mem_fun(*this, &AudioTimeAxisView::region_view_added));
124
125                 if (!_editor.have_idled()) {
126                         /* first idle will do what we need */
127                 } else {
128                         first_idle ();
129                 }
130
131         } else {
132                 post_construct ();
133         }
134 }
135
136 AudioTimeAxisView::~AudioTimeAxisView ()
137 {
138 }
139
140 void
141 AudioTimeAxisView::first_idle ()
142 {
143         _view->attach ();
144         post_construct ();
145 }
146
147 AudioStreamView*
148 AudioTimeAxisView::audio_view()
149 {
150         return dynamic_cast<AudioStreamView*>(_view);
151 }
152
153 guint32
154 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
155 {
156         set_gui_property ("visible", true);
157         return TimeAxisView::show_at (y, nth, parent);
158 }
159
160 void
161 AudioTimeAxisView::hide ()
162 {
163         set_gui_property ("visible", false);
164         TimeAxisView::hide ();
165 }
166
167 void
168 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
169 {
170         if (param.type() == NullAutomation) {
171                 return;
172         }
173
174         AutomationTracks::iterator existing = _automation_tracks.find (param);
175
176         if (existing != _automation_tracks.end()) {
177                 
178                 /* automation track created because we had existing data for
179                  * the processor, but visibility may need to be controlled
180                  * since it will have been set visible by default.
181                  */
182
183                 existing->second->set_marked_for_display (show);
184                 
185                 if (!no_redraw) {
186                         request_redraw ();
187                 }
188
189                 return;
190         }
191
192         if (param.type() == GainAutomation) {
193
194                 create_gain_automation_child (param, show);
195
196         } else if (param.type() == PanWidthAutomation ||
197                    param.type() == PanElevationAutomation ||
198                    param.type() == PanAzimuthAutomation) {
199
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         bool changed = false;
220         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
221                 changed = true;
222                 (*i)->set_marked_for_display (false);
223         }
224         if (changed) {
225                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
226         }
227         pan_tracks.clear();
228
229         if (!_route->panner()) {
230                 return;
231         }
232
233         set<Evoral::Parameter> params = _route->panner()->what_can_be_automated();
234         set<Evoral::Parameter>::iterator p;
235
236         for (p = params.begin(); p != params.end(); ++p) {
237                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control = _route->pannable()->automation_control(*p);
238
239                 if (pan_control->parameter().type() == NullAutomation) {
240                         error << "Pan control has NULL automation type!" << endmsg;
241                         continue;
242                 }
243
244                 if (automation_child (pan_control->parameter ()).get () == 0) {
245
246                         /* we don't already have an AutomationTimeAxisView for this parameter */
247
248                         std::string const name = _route->panner()->describe_parameter (pan_control->parameter ());
249
250                         boost::shared_ptr<AutomationTimeAxisView> t (
251                                 new AutomationTimeAxisView (_session,
252                                                             _route,
253                                                             _route->pannable(),
254                                                             pan_control,
255                                                             pan_control->parameter (),
256                                                             _editor,
257                                                             *this,
258                                                             false,
259                                                             parent_canvas,
260                                                             name)
261                                 );
262
263                         pan_tracks.push_back (t);
264                         add_automation_child (*p, t, show);
265                 } else {
266                         pan_tracks.push_back (automation_child (pan_control->parameter ()));
267                 }
268         }
269 }
270
271 void
272 AudioTimeAxisView::update_gain_track_visibility ()
273 {
274         bool const showit = gain_automation_item->get_active();
275
276         if (showit != string_is_affirmative (gain_track->gui_property ("visible"))) {
277                 gain_track->set_marked_for_display (showit);
278
279                 /* now trigger a redisplay */
280
281                 if (!no_redraw) {
282                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
283                 }
284         }
285 }
286
287 void
288 AudioTimeAxisView::update_pan_track_visibility ()
289 {
290         bool const showit = pan_automation_item->get_active();
291         bool changed = false;
292
293         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
294                 if ((*i)->set_marked_for_display (showit)) {
295                         changed = true;
296                 }
297         }
298
299         if (changed) {
300                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
301         }
302 }
303
304 void
305 AudioTimeAxisView::show_all_automation (bool apply_to_selection)
306 {
307         if (apply_to_selection) {
308                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_automation, _1, false));
309         } else {
310
311                 no_redraw = true;
312
313                 RouteTimeAxisView::show_all_automation ();
314
315                 no_redraw = false;
316                 request_redraw ();
317         }
318 }
319
320 void
321 AudioTimeAxisView::show_existing_automation (bool apply_to_selection)
322 {
323         if (apply_to_selection) {
324                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_existing_automation, _1, false));
325         } else {
326                 no_redraw = true;
327
328                 RouteTimeAxisView::show_existing_automation ();
329
330                 no_redraw = false;
331
332                 request_redraw ();
333         }
334 }
335
336 void
337 AudioTimeAxisView::hide_all_automation (bool apply_to_selection)
338 {
339         if (apply_to_selection) {
340                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_automation, _1, false));
341         } else {
342                 no_redraw = true;
343
344                 RouteTimeAxisView::hide_all_automation();
345
346                 no_redraw = false;
347                 request_redraw ();
348         }
349 }
350
351 void
352 AudioTimeAxisView::route_active_changed ()
353 {
354         update_control_names ();
355 }
356
357
358 /**
359  *    Set up the names of the controls so that they are coloured
360  *    correctly depending on whether this route is inactive or
361  *    selected.
362  */
363
364 void
365 AudioTimeAxisView::update_control_names ()
366 {
367         if (is_audio_track()) {
368                 if (_route->active()) {
369                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
370                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
371                 } else {
372                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
373                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
374                 }
375         } else {
376                 if (_route->active()) {
377                         controls_base_selected_name = "BusControlsBaseSelected";
378                         controls_base_unselected_name = "BusControlsBaseUnselected";
379                 } else {
380                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
381                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
382                 }
383         }
384
385         if (get_selected()) {
386                 controls_ebox.set_name (controls_base_selected_name);
387         } else {
388                 controls_ebox.set_name (controls_base_unselected_name);
389         }
390 }
391
392 void
393 AudioTimeAxisView::build_automation_action_menu (bool for_selection)
394 {
395         using namespace Menu_Helpers;
396
397         RouteTimeAxisView::build_automation_action_menu (for_selection);
398
399         MenuList& automation_items = automation_action_menu->items ();
400
401         automation_items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &AudioTimeAxisView::update_gain_track_visibility)));
402         gain_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
403         gain_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) && 
404                                           (gain_track && string_is_affirmative (gain_track->gui_property ("visible"))));
405
406         _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
407
408         if (!pan_tracks.empty()) {
409                 automation_items.push_back (CheckMenuElem (_("Pan"), sigc::mem_fun (*this, &AudioTimeAxisView::update_pan_track_visibility)));
410                 pan_automation_item = dynamic_cast<CheckMenuItem*> (&automation_items.back ());
411                 pan_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) &&
412                                                  (!pan_tracks.empty() && string_is_affirmative (pan_tracks.front()->gui_property ("visible"))));
413
414                 set<Evoral::Parameter> const & params = _route->pannable()->what_can_be_automated ();
415                 for (set<Evoral::Parameter>::iterator p = params.begin(); p != params.end(); ++p) {
416                         _main_automation_menu_map[*p] = pan_automation_item;
417                 }
418         }
419 }
420
421 void
422 AudioTimeAxisView::enter_internal_edit_mode ()
423 {
424         if (audio_view()) {
425                 audio_view()->enter_internal_edit_mode ();
426         }
427 }
428
429 void
430 AudioTimeAxisView::leave_internal_edit_mode ()
431 {
432         if (audio_view()) {
433                 audio_view()->leave_internal_edit_mode ();
434         }
435 }