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