fix conflicts and 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 "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         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 != string_is_affirmative (gain_track->gui_property ("visible"))) {
265                 gain_track->set_marked_for_display (showit);
266
267                 /* now trigger a redisplay */
268
269                 if (!no_redraw) {
270                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
271                 }
272         }
273 }
274
275 void
276 AudioTimeAxisView::update_pan_track_visibility ()
277 {
278         bool const showit = pan_automation_item->get_active();
279         bool changed = false;
280
281         for (list<boost::shared_ptr<AutomationTimeAxisView> >::iterator i = pan_tracks.begin(); i != pan_tracks.end(); ++i) {
282                 if ((*i)->set_marked_for_display (showit)) {
283                         changed = true;
284                 }
285         }
286
287         if (changed) {
288                 _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
289         }
290 }
291
292 void
293 AudioTimeAxisView::show_all_automation (bool apply_to_selection)
294 {
295         if (apply_to_selection) {
296                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_all_automation, _1, false));
297         } else {
298
299                 no_redraw = true;
300
301                 RouteTimeAxisView::show_all_automation ();
302
303                 no_redraw = false;
304                 request_redraw ();
305         }
306 }
307
308 void
309 AudioTimeAxisView::show_existing_automation (bool apply_to_selection)
310 {
311         if (apply_to_selection) {
312                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::show_existing_automation, _1, false));
313         } else {
314                 no_redraw = true;
315
316                 RouteTimeAxisView::show_existing_automation ();
317
318                 no_redraw = false;
319
320                 request_redraw ();
321         }
322 }
323
324 void
325 AudioTimeAxisView::hide_all_automation (bool apply_to_selection)
326 {
327         if (apply_to_selection) {
328                 _editor.get_selection().tracks.foreach_audio_time_axis (boost::bind (&AudioTimeAxisView::hide_all_automation, _1, false));
329         } else {
330                 no_redraw = true;
331
332                 RouteTimeAxisView::hide_all_automation();
333
334                 no_redraw = false;
335                 request_redraw ();
336         }
337 }
338
339 void
340 AudioTimeAxisView::route_active_changed ()
341 {
342         update_control_names ();
343 }
344
345
346 /**
347  *    Set up the names of the controls so that they are coloured
348  *    correctly depending on whether this route is inactive or
349  *    selected.
350  */
351
352 void
353 AudioTimeAxisView::update_control_names ()
354 {
355         if (is_audio_track()) {
356                 if (_route->active()) {
357                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
358                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
359                 } else {
360                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
361                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
362                 }
363         } else {
364                 if (_route->active()) {
365                         controls_base_selected_name = "BusControlsBaseSelected";
366                         controls_base_unselected_name = "BusControlsBaseUnselected";
367                 } else {
368                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
369                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
370                 }
371         }
372
373         if (get_selected()) {
374                 controls_ebox.set_name (controls_base_selected_name);
375         } else {
376                 controls_ebox.set_name (controls_base_unselected_name);
377         }
378 }
379
380 void
381 AudioTimeAxisView::build_automation_action_menu (bool for_selection)
382 {
383         using namespace Menu_Helpers;
384
385         RouteTimeAxisView::build_automation_action_menu (for_selection);
386
387         MenuList& automation_items = automation_action_menu->items ();
388
389         automation_items.push_back (CheckMenuElem (_("Fader"), sigc::mem_fun (*this, &AudioTimeAxisView::update_gain_track_visibility)));
390         gain_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&automation_items.back ());
391         gain_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) && 
392                                           (gain_track && string_is_affirmative (gain_track->gui_property ("visible"))));
393
394         _main_automation_menu_map[Evoral::Parameter(GainAutomation)] = gain_automation_item;
395
396         automation_items.push_back (CheckMenuElem (_("Pan"), sigc::mem_fun (*this, &AudioTimeAxisView::update_pan_track_visibility)));
397         pan_automation_item = dynamic_cast<Gtk::CheckMenuItem*> (&automation_items.back ());
398         pan_automation_item->set_active ((!for_selection || _editor.get_selection().tracks.size() == 1) &&
399                                          (!pan_tracks.empty() && string_is_affirmative (pan_tracks.front()->gui_property ("visible"))));
400
401         set<Evoral::Parameter> const & params = _route->pannable()->what_can_be_automated ();
402         for (set<Evoral::Parameter>::iterator p = params.begin(); p != params.end(); ++p) {
403                 _main_automation_menu_map[*p] = pan_automation_item;
404         }
405 }
406
407 void
408 AudioTimeAxisView::enter_internal_edit_mode ()
409 {
410         if (audio_view()) {
411                 audio_view()->enter_internal_edit_mode ();
412         }
413 }
414
415 void
416 AudioTimeAxisView::leave_internal_edit_mode ()
417 {
418         if (audio_view()) {
419                 audio_view()->leave_internal_edit_mode ();
420         }
421 }