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