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