Move RDF-based preset stuff into LadspaPlugin, to make way for a set of evil hacks...
[ardour.git] / gtk2_ardour / route_time_axis.cc
1 /*
2     Copyright (C) 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 #include <cstdlib>
20 #include <cmath>
21 #include <cassert>
22
23 #include <algorithm>
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <utility>
28
29 #include <sigc++/bind.h>
30
31 #include "pbd/error.h"
32 #include "pbd/stl_delete.h"
33 #include "pbd/whitespace.h"
34 #include "pbd/memento_command.h"
35 #include "pbd/enumwriter.h"
36 #include "pbd/stateful_diff_command.h"
37
38 #include <gtkmm/menu.h>
39 #include <gtkmm/menuitem.h>
40 #include <gtkmm2ext/gtk_ui.h>
41 #include <gtkmm2ext/selector.h>
42 #include <gtkmm2ext/bindable_button.h>
43 #include <gtkmm2ext/utils.h>
44
45 #include "ardour/amp.h"
46 #include "ardour/audioplaylist.h"
47 #include "ardour/diskstream.h"
48 #include "ardour/event_type_map.h"
49 #include "ardour/ladspa_plugin.h"
50 #include "ardour/location.h"
51 #include "ardour/panner.h"
52 #include "ardour/playlist.h"
53 #include "ardour/playlist.h"
54 #include "ardour/processor.h"
55 #include "ardour/profile.h"
56 #include "ardour/route_group.h"
57 #include "ardour/session.h"
58 #include "ardour/session_playlist.h"
59 #include "ardour/debug.h"
60 #include "ardour/utils.h"
61 #include "evoral/Parameter.hpp"
62
63 #include "ardour_ui.h"
64 #include "global_signals.h"
65 #include "route_time_axis.h"
66 #include "automation_time_axis.h"
67 #include "canvas_impl.h"
68 #include "crossfade_view.h"
69 #include "enums.h"
70 #include "gui_thread.h"
71 #include "keyboard.h"
72 #include "playlist_selector.h"
73 #include "point_selection.h"
74 #include "prompter.h"
75 #include "public_editor.h"
76 #include "region_view.h"
77 #include "rgb_macros.h"
78 #include "selection.h"
79 #include "simplerect.h"
80 #include "streamview.h"
81 #include "utils.h"
82 #include "route_group_menu.h"
83
84 #include "ardour/track.h"
85
86 #include "i18n.h"
87
88 using namespace ARDOUR;
89 using namespace PBD;
90 using namespace Gtkmm2ext;
91 using namespace Gtk;
92 using namespace Editing;
93 using namespace std;
94
95 Glib::RefPtr<Gdk::Pixbuf> RouteTimeAxisView::slider;
96
97 void
98 RouteTimeAxisView::setup_slider_pix ()
99 {
100         if ((slider = ::get_icon ("fader_belt_h")) == 0) {
101                 throw failed_constructor ();
102         }
103 }
104
105 RouteTimeAxisView::RouteTimeAxisView (PublicEditor& ed, Session* sess, boost::shared_ptr<Route> rt, Canvas& canvas)
106         : AxisView(sess)
107         , RouteUI(rt, sess)
108         , TimeAxisView(sess,ed,(TimeAxisView*) 0, canvas)
109         , parent_canvas (canvas)
110         , button_table (3, 3)
111         , route_group_button (_("g"))
112         , playlist_button (_("p"))
113         , automation_button (_("a"))
114         , gm (sess, slider, true, 115)
115         , _ignore_track_mode_change (false)
116 {
117         gm.set_controls (_route, _route->shared_peak_meter(), _route->amp());
118         gm.get_level_meter().set_no_show_all();
119         gm.get_level_meter().setup_meters(50);
120
121         _has_state = true;
122         playlist_menu = 0;
123         playlist_action_menu = 0;
124         automation_action_menu = 0;
125         plugins_submenu_item = 0;
126         mode_menu = 0;
127         _view = 0;
128
129         if (!_route->is_hidden()) {
130                 _marked_for_display = true;
131         }
132
133         mute_changed (0);
134         update_solo_display ();
135
136         timestretch_rect = 0;
137         no_redraw = false;
138         destructive_track_mode_item = 0;
139         normal_track_mode_item = 0;
140         non_layered_track_mode_item = 0;
141
142         ignore_toggle = false;
143
144         route_group_button.set_name ("TrackGroupButton");
145         playlist_button.set_name ("TrackPlaylistButton");
146         automation_button.set_name ("TrackAutomationButton");
147
148         route_group_button.unset_flags (Gtk::CAN_FOCUS);
149         playlist_button.unset_flags (Gtk::CAN_FOCUS);
150         automation_button.unset_flags (Gtk::CAN_FOCUS);
151
152         route_group_button.signal_button_release_event().connect (sigc::mem_fun(*this, &RouteTimeAxisView::route_group_click), false);
153         playlist_button.signal_clicked().connect (sigc::mem_fun(*this, &RouteTimeAxisView::playlist_click));
154         automation_button.signal_clicked().connect (sigc::mem_fun(*this, &RouteTimeAxisView::automation_click));
155
156         if (is_track()) {
157
158                 /* use icon */
159
160                 rec_enable_button->remove ();
161
162                 switch (track()->mode()) {
163                 case ARDOUR::Normal:
164                 case ARDOUR::NonLayered:
165                         rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
166                         break;
167                 case ARDOUR::Destructive:
168                         rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
169                         break;
170                 }
171                 rec_enable_button->show_all ();
172
173                 controls_table.attach (*rec_enable_button, 5, 6, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
174
175                 if (is_midi_track()) {
176                         ARDOUR_UI::instance()->set_tip(*rec_enable_button, _("Record (Right-click for Step Edit)"));
177                 } else {
178                         ARDOUR_UI::instance()->set_tip(*rec_enable_button, _("Record"));
179                 }
180
181                 rec_enable_button->set_sensitive (_session->writable());
182         }
183
184         controls_hbox.pack_start(gm.get_level_meter(), false, false);
185         _route->meter_change.connect (*this, invalidator (*this), bind (&RouteTimeAxisView::meter_changed, this), gui_context());
186         _route->input()->changed.connect (*this, invalidator (*this), ui_bind (&RouteTimeAxisView::io_changed, this, _1, _2), gui_context());
187         _route->output()->changed.connect (*this, invalidator (*this), ui_bind (&RouteTimeAxisView::io_changed, this, _1, _2), gui_context());
188
189         controls_table.attach (*mute_button, 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
190
191         if (!_route->is_master()) {
192                 controls_table.attach (*solo_button, 7, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
193         }
194
195         controls_table.attach (route_group_button, 7, 8, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
196         controls_table.attach (gm.get_gain_slider(), 0, 5, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
197
198         ARDOUR_UI::instance()->set_tip(*solo_button,_("Solo"));
199         ARDOUR_UI::instance()->set_tip(*mute_button,_("Mute"));
200         ARDOUR_UI::instance()->set_tip(route_group_button, _("Route Group"));
201         ARDOUR_UI::instance()->set_tip(playlist_button,_("Playlist"));
202         ARDOUR_UI::instance()->set_tip(automation_button, _("Automation"));
203
204         label_view ();
205
206         controls_table.attach (automation_button, 6, 7, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
207
208         if (is_track() && track()->mode() == ARDOUR::Normal) {
209                 controls_table.attach (playlist_button, 5, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
210         }
211
212         _y_position = -1;
213
214         _route->processors_changed.connect (*this, invalidator (*this), ui_bind (&RouteTimeAxisView::processors_changed, this, _1), gui_context());
215         _route->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RouteTimeAxisView::route_property_changed, this, _1), gui_context());
216
217         if (is_track()) {
218
219                 track()->TrackModeChanged.connect (*this, invalidator (*this), boost::bind (&RouteTimeAxisView::track_mode_changed, this), gui_context());
220                 track()->FreezeChange.connect (*this, invalidator (*this), boost::bind (&RouteTimeAxisView::map_frozen, this), gui_context());
221                 track()->SpeedChanged.connect (*this, invalidator (*this), boost::bind (&RouteTimeAxisView::speed_changed, this), gui_context());
222
223                 /* pick up the correct freeze state */
224                 map_frozen ();
225
226         }
227
228         _editor.ZoomChanged.connect (sigc::mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
229         _editor.HorizontalPositionChanged.connect (sigc::mem_fun (*this, &RouteTimeAxisView::horizontal_position_changed));
230         ColorsChanged.connect (sigc::mem_fun (*this, &RouteTimeAxisView::color_handler));
231
232         PropertyList* plist = new PropertyList();
233         
234         plist->add (ARDOUR::Properties::edit, true);
235         plist->add (ARDOUR::Properties::mute, true);
236         plist->add (ARDOUR::Properties::solo, true);
237         
238         route_group_menu = new RouteGroupMenu (_session, plist);
239         route_group_menu->GroupSelected.connect (sigc::mem_fun (*this, &RouteTimeAxisView::set_route_group_from_menu));
240
241         gm.get_gain_slider().signal_scroll_event().connect(sigc::mem_fun(*this, &RouteTimeAxisView::controls_ebox_scroll), false);
242         gm.get_gain_slider().set_name ("TrackGainFader");
243 }
244
245 RouteTimeAxisView::~RouteTimeAxisView ()
246 {
247         CatchDeletion (this);
248
249         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
250                 delete *i;
251         }
252
253         delete playlist_menu;
254         playlist_menu = 0;
255
256         delete playlist_action_menu;
257         playlist_action_menu = 0;
258
259         delete _view;
260         _view = 0;
261
262         _automation_tracks.clear ();
263
264         delete route_group_menu;
265 }
266
267 void
268 RouteTimeAxisView::post_construct ()
269 {
270         /* map current state of the route */
271
272         update_diskstream_display ();
273
274         _subplugin_menu_map.clear ();
275         subplugin_menu.items().clear ();
276         _route->foreach_processor (sigc::mem_fun (*this, &RouteTimeAxisView::add_processor_to_subplugin_menu));
277         _route->foreach_processor (sigc::mem_fun (*this, &RouteTimeAxisView::add_existing_processor_automation_curves));
278         reset_processor_automation_curves ();
279 }
280
281 gint
282 RouteTimeAxisView::route_group_click (GdkEventButton *ev)
283 {
284         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
285                 if (_route->route_group()) {
286                         _route->route_group()->remove (_route);
287                 }
288                 return false;
289         }
290
291         route_group_menu->rebuild (_route->route_group ());
292         route_group_menu->popup (ev->button, ev->time);
293
294         return false;
295 }
296
297 void
298 RouteTimeAxisView::set_route_group_from_menu (RouteGroup *eg)
299 {
300         if (eg) {
301                 eg->add (_route);
302         } else {
303                 if (_route->route_group()) {
304                         _route->route_group()->remove (_route);
305                 }
306         }
307 }
308
309 void
310 RouteTimeAxisView::playlist_changed ()
311 {
312         label_view ();
313 }
314
315 void
316 RouteTimeAxisView::label_view ()
317 {
318         string x = _route->name();
319
320         if (x != name_entry.get_text()) {
321                 name_entry.set_text (x);
322         }
323
324         ARDOUR_UI::instance()->set_tip (name_entry, x);
325 }
326
327 void
328 RouteTimeAxisView::route_property_changed (const PropertyChange& what_changed)
329 {
330         if (what_changed.contains (ARDOUR::Properties::name)) {
331                 label_view ();
332         }
333 }
334
335 void
336 RouteTimeAxisView::take_name_changed (void *src)
337
338 {
339         if (src != this) {
340                 label_view ();
341         }
342 }
343
344 void
345 RouteTimeAxisView::playlist_click ()
346 {
347         build_playlist_menu ();
348         conditionally_add_to_selection ();
349         playlist_action_menu->popup (1, gtk_get_current_event_time());
350 }
351
352 void
353 RouteTimeAxisView::automation_click ()
354 {
355         conditionally_add_to_selection ();
356         build_automation_action_menu ();
357         automation_action_menu->popup (1, gtk_get_current_event_time());
358 }
359
360 int
361 RouteTimeAxisView::set_state (const XMLNode& node, int version)
362 {
363         TimeAxisView::set_state (node, version);
364
365         XMLNodeList kids = node.children();
366         XMLNodeConstIterator iter;
367         const XMLProperty* prop;
368
369         if (_view && (prop = node.property ("layer-display"))) {
370                 set_layer_display (LayerDisplay (string_2_enum (prop->value(), _view->layer_display ())));
371         }
372
373         for (iter = kids.begin(); iter != kids.end(); ++iter) {
374                 if ((*iter)->name() == AutomationTimeAxisView::state_node_name) {
375                         if ((prop = (*iter)->property ("automation-id")) != 0) {
376
377                                 Evoral::Parameter param = ARDOUR::EventTypeMap::instance().new_parameter(prop->value());
378                                 bool show = ((prop = (*iter)->property ("shown")) != 0) && string_is_affirmative (prop->value());
379                                 create_automation_child(param, show);
380                         } else {
381                                 warning << "Automation child has no ID" << endmsg;
382                         }
383                 }
384         }
385
386         return 0;
387 }
388
389 void
390 RouteTimeAxisView::build_automation_action_menu ()
391 {
392         using namespace Menu_Helpers;
393
394         /* detach subplugin_menu from automation_action_menu before we delete automation_action_menu,
395            otherwise bad things happen (see comment for similar case in MidiTimeAxisView::build_automation_action_menu)
396         */
397
398         detach_menu (subplugin_menu);
399
400         _main_automation_menu_map.clear ();
401         delete automation_action_menu;
402         automation_action_menu = new Menu;
403
404         MenuList& items = automation_action_menu->items();
405
406         automation_action_menu->set_name ("ArdourContextMenu");
407         
408         items.push_back (MenuElem (_("Show All Automation"),
409                                    sigc::mem_fun(*this, &RouteTimeAxisView::show_all_automation)));
410         
411         items.push_back (MenuElem (_("Show Existing Automation"),
412                                    sigc::mem_fun(*this, &RouteTimeAxisView::show_existing_automation)));
413         
414         items.push_back (MenuElem (_("Hide All Automation"),
415                                    sigc::mem_fun(*this, &RouteTimeAxisView::hide_all_automation)));
416
417         items.push_back (SeparatorElem ());
418         
419         /* Attach the plugin submenu. It may have previously been used elsewhere,
420            so it was detached above */
421
422         items.push_back (MenuElem (_("Plugins"), subplugin_menu));
423         items.back().set_sensitive (!subplugin_menu.items().empty());
424 }
425
426 void
427 RouteTimeAxisView::build_display_menu ()
428 {
429         using namespace Menu_Helpers;
430
431         /* prepare it */
432
433         TimeAxisView::build_display_menu ();
434
435         /* now fill it with our stuff */
436
437         MenuList& items = display_menu->items();
438         display_menu->set_name ("ArdourContextMenu");
439
440         items.push_back (MenuElem (_("Color..."), sigc::mem_fun(*this, &RouteTimeAxisView::select_track_color)));
441
442         items.push_back (SeparatorElem());
443
444         if (!Profile->get_sae()) {
445                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
446                 /* rebuild this every time */
447                 build_automation_action_menu ();
448                 detach_menu (*automation_action_menu);
449                 items.push_back (MenuElem (_("Automation"), *automation_action_menu));
450                 items.push_back (SeparatorElem());
451         }
452
453         // Hook for derived classes to add type specific stuff
454         append_extra_display_menu_items ();
455         items.push_back (SeparatorElem());
456
457         if (is_track()) {
458
459                 Menu *layers_menu = manage(new Menu);
460                 MenuList &layers_items = layers_menu->items();
461                 layers_menu->set_name("ArdourContextMenu");
462
463                 RadioMenuItem::Group layers_group;
464
465                 layers_items.push_back(RadioMenuElem (layers_group, _("Overlaid"),
466                                 sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::set_layer_display), Overlaid)));
467                 layers_items.push_back(RadioMenuElem (layers_group, _("Stacked"),
468                                 sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::set_layer_display), Stacked)));
469
470                 items.push_back (MenuElem (_("Layers"), *layers_menu));
471
472                 if (!Profile->get_sae()) {
473
474                         Menu* alignment_menu = manage (new Menu);
475                         MenuList& alignment_items = alignment_menu->items();
476                         alignment_menu->set_name ("ArdourContextMenu");
477                         
478                         RadioMenuItem::Group align_group;
479                         
480                         alignment_items.push_back (RadioMenuElem (align_group, _("Align With Existing Material"),
481                                                                   sigc::bind (sigc::mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
482                         align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
483                         if (track()->alignment_style() == ExistingMaterial) {
484                                 align_existing_item->set_active();
485                         }
486                         
487                         alignment_items.push_back (RadioMenuElem (align_group, _("Align With Capture Time"),
488                                                                   sigc::bind (sigc::mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
489                         align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
490                         if (track()->alignment_style() == CaptureTime) {
491                                 align_capture_item->set_active();
492                         }
493                         
494                         items.push_back (MenuElem (_("Alignment"), *alignment_menu));
495                         track()->AlignmentStyleChanged.connect (route_connections, invalidator (*this), boost::bind (&RouteTimeAxisView::align_style_changed, this), gui_context());
496
497                         Menu* mode_menu = manage (new Menu);
498                         MenuList& mode_items = mode_menu->items ();
499                         mode_menu->set_name ("ArdourContextMenu");
500
501                         RadioMenuItem::Group mode_group;
502
503                         mode_items.push_back (RadioMenuElem (mode_group, _("Normal Mode"), sigc::bind (
504                                         sigc::mem_fun (*this, &RouteTimeAxisView::set_track_mode),
505                                         ARDOUR::Normal)));
506                         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&mode_items.back());
507
508                         mode_items.push_back (RadioMenuElem (mode_group, _("Tape Mode"), sigc::bind (
509                                         sigc::mem_fun (*this, &RouteTimeAxisView::set_track_mode),
510                                         ARDOUR::Destructive)));
511                         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&mode_items.back());
512
513                         mode_items.push_back (RadioMenuElem (mode_group, _("Non-Layered Mode"),
514                                                         sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::NonLayered)));
515                         non_layered_track_mode_item = dynamic_cast<RadioMenuItem*>(&mode_items.back());
516
517
518                         _ignore_track_mode_change = true;
519                         
520                         switch (track()->mode()) {
521                         case ARDOUR::Destructive:
522                                 destructive_track_mode_item->set_active ();
523                                 break;
524                         case ARDOUR::Normal:
525                                 normal_track_mode_item->set_active ();
526                                 break;
527                         case ARDOUR::NonLayered:
528                                 non_layered_track_mode_item->set_active ();
529                                 break;
530                         }
531                         
532                         _ignore_track_mode_change = false;
533
534                         items.push_back (MenuElem (_("Mode"), *mode_menu));
535                 }
536
537                 color_mode_menu = build_color_mode_menu();
538                 if (color_mode_menu) {
539                         items.push_back (MenuElem (_("Color Mode"), *color_mode_menu));
540                 }
541
542                 items.push_back (SeparatorElem());
543
544                 build_playlist_menu ();
545                 items.push_back (MenuElem (_("Playlist"), *playlist_action_menu));
546
547                 route_group_menu->rebuild (_route->route_group ());
548                 items.push_back (MenuElem (_("Route Group"), *route_group_menu));
549
550                 build_automation_action_menu ();
551                 items.push_back (MenuElem (_("Automation"), *automation_action_menu));
552
553                 items.push_back (SeparatorElem());
554         }
555
556         items.push_back (CheckMenuElem (_("Active"), sigc::mem_fun(*this, &RouteUI::toggle_route_active)));
557         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
558         route_active_menu_item->set_active (_route->active());
559
560         items.push_back (SeparatorElem());
561         items.push_back (MenuElem (_("Hide"), sigc::bind (sigc::mem_fun(_editor, &PublicEditor::hide_track_in_display), this, false)));
562         if (!Profile->get_sae()) {
563                 items.push_back (MenuElem (_("Remove"), sigc::mem_fun(*this, &RouteUI::remove_this_route)));
564         } else {
565                 items.push_front (SeparatorElem());
566                 items.push_front (MenuElem (_("Delete"), sigc::mem_fun(*this, &RouteUI::remove_this_route)));
567         }
568 }
569
570 static bool __reset_item (RadioMenuItem* item, RadioMenuItem* item_2)
571 {
572         item->set_active ();
573         item_2->set_active ();
574         return false;
575 }
576
577 void
578 RouteTimeAxisView::set_track_mode (TrackMode mode)
579 {
580         if (_ignore_track_mode_change) {
581                 return;
582         }
583         
584         RadioMenuItem* item;
585         RadioMenuItem* other_item;
586         RadioMenuItem* other_item_2;
587
588         switch (mode) {
589         case ARDOUR::Normal:
590                 item = normal_track_mode_item;
591                 other_item = non_layered_track_mode_item;
592                 other_item_2 = destructive_track_mode_item;
593                 break;
594         case ARDOUR::NonLayered:
595                 item = non_layered_track_mode_item;
596                 other_item = normal_track_mode_item;
597                 other_item_2 = destructive_track_mode_item;
598                 break;
599         case ARDOUR::Destructive:
600                 item = destructive_track_mode_item;
601                 other_item = normal_track_mode_item;
602                 other_item_2 = non_layered_track_mode_item;
603                 break;
604         default:
605                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", mode) << endmsg;
606                 /*NOTREACHED*/
607                 return;
608         }
609
610         if (item && other_item && other_item_2 && track()->mode() != mode) {
611                 _set_track_mode (track().get(), mode, other_item, other_item_2);
612         }
613 }
614
615 void
616 RouteTimeAxisView::_set_track_mode (Track* track, TrackMode mode, RadioMenuItem* reset_item, RadioMenuItem* reset_item_2)
617 {
618         bool needs_bounce;
619
620         if (!track->can_use_mode (mode, needs_bounce)) {
621
622                 if (!needs_bounce) {
623                         /* cannot be done */
624                         Glib::signal_idle().connect (sigc::bind (sigc::ptr_fun (__reset_item), reset_item, reset_item_2));
625                         return;
626                 } else {
627                         cerr << "would bounce this one\n";
628                         /* XXX: radio menu item becomes inconsistent with track state in this case */
629                         return;
630                 }
631         }
632
633         track->set_mode (mode);
634
635         rec_enable_button->remove ();
636
637         switch (mode) {
638         case ARDOUR::NonLayered:
639         case ARDOUR::Normal:
640                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
641                 break;
642         case ARDOUR::Destructive:
643                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
644                 break;
645         }
646
647         rec_enable_button->show_all ();
648 }
649
650 void
651 RouteTimeAxisView::track_mode_changed ()
652 {
653         RadioMenuItem* item;
654
655         switch (track()->mode()) {
656         case ARDOUR::Normal:
657                 item = normal_track_mode_item;
658                 break;
659         case ARDOUR::NonLayered:
660                 item = non_layered_track_mode_item;
661                 break;
662         case ARDOUR::Destructive:
663                 item = destructive_track_mode_item;
664                 break;
665         default:
666                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", track()->mode()) << endmsg;
667                 /*NOTREACHED*/
668                 return;
669         }
670
671         item->set_active ();
672 }
673
674 void
675 RouteTimeAxisView::show_timestretch (framepos_t start, framepos_t end)
676 {
677         double x1;
678         double x2;
679         double y2;
680
681         TimeAxisView::show_timestretch (start, end);
682
683         hide_timestretch ();
684
685 #if 0
686         if (ts.empty()) {
687                 return;
688         }
689
690
691         /* check that the time selection was made in our route, or our route group.
692            remember that route_group() == 0 implies the route is *not* in a edit group.
693         */
694
695         if (!(ts.track == this || (ts.group != 0 && ts.group == _route->route_group()))) {
696                 /* this doesn't apply to us */
697                 return;
698         }
699
700         /* ignore it if our edit group is not active */
701
702         if ((ts.track != this) && _route->route_group() && !_route->route_group()->is_active()) {
703                 return;
704         }
705 #endif
706
707         if (timestretch_rect == 0) {
708                 timestretch_rect = new SimpleRect (*canvas_display ());
709                 timestretch_rect->property_x1() =  0.0;
710                 timestretch_rect->property_y1() =  0.0;
711                 timestretch_rect->property_x2() =  0.0;
712                 timestretch_rect->property_y2() =  0.0;
713                 timestretch_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
714                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
715         }
716
717         timestretch_rect->show ();
718         timestretch_rect->raise_to_top ();
719
720         x1 = start / _editor.get_current_zoom();
721         x2 = (end - 1) / _editor.get_current_zoom();
722         y2 = current_height() - 2;
723
724         timestretch_rect->property_x1() = x1;
725         timestretch_rect->property_y1() = 1.0;
726         timestretch_rect->property_x2() = x2;
727         timestretch_rect->property_y2() = y2;
728 }
729
730 void
731 RouteTimeAxisView::hide_timestretch ()
732 {
733         TimeAxisView::hide_timestretch ();
734
735         if (timestretch_rect) {
736                 timestretch_rect->hide ();
737         }
738 }
739
740 void
741 RouteTimeAxisView::show_selection (TimeSelection& ts)
742 {
743
744 #if 0
745         /* ignore it if our edit group is not active or if the selection was started
746            in some other track or route group (remember that route_group() == 0 means
747            that the track is not in an route group).
748         */
749
750         if (((ts.track != this && !is_child (ts.track)) && _route->route_group() && !_route->route_group()->is_active()) ||
751             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route->route_group())))) {
752                 hide_selection ();
753                 return;
754         }
755 #endif
756
757         TimeAxisView::show_selection (ts);
758 }
759
760 void
761 RouteTimeAxisView::set_height (uint32_t h)
762 {
763         int gmlen = h - 5;
764         bool height_changed = (height == 0) || (h != height);
765         gm.get_level_meter().setup_meters (gmlen);
766
767         TimeAxisView::set_height (h);
768
769         ensure_xml_node ();
770
771         if (_view) {
772                 _view->set_height ((double) current_height());
773         }
774
775         char buf[32];
776         snprintf (buf, sizeof (buf), "%u", height);
777         xml_node->add_property ("height", buf);
778
779         if (height >= preset_height (HeightNormal)) {
780                 reset_meter();
781                 show_name_entry ();
782                 hide_name_label ();
783
784                 gm.get_gain_slider().show();
785                 mute_button->show();
786                 if (!_route || _route->is_monitor()) {
787                         solo_button->hide();
788                 } else {
789                         solo_button->show();
790                 }
791                 if (rec_enable_button)
792                         rec_enable_button->show();
793
794                 route_group_button.show();
795                 automation_button.show();
796
797                 if (is_track() && track()->mode() == ARDOUR::Normal) {
798                         playlist_button.show();
799                 }
800
801         } else if (height >= preset_height (HeightSmaller)) {
802
803                 reset_meter();
804                 show_name_entry ();
805                 hide_name_label ();
806
807                 gm.get_gain_slider().hide();
808                 mute_button->show();
809                 if (!_route || _route->is_monitor()) {
810                         solo_button->hide();
811                 } else {
812                         solo_button->show();
813                 }
814                 if (rec_enable_button)
815                         rec_enable_button->show();
816
817                 route_group_button.hide ();
818                 automation_button.hide ();
819
820                 if (is_track() && track()->mode() == ARDOUR::Normal) {
821                         playlist_button.hide ();
822                 }
823
824         } else {
825
826
827                 /* don't allow name_entry to be hidden while
828                    it has focus, otherwise the GUI becomes unusable.
829                 */
830
831                 if (name_entry.has_focus()) {
832                         if (name_entry.get_text() != _route->name()) {
833                                 name_entry_changed ();
834                         }
835                         controls_ebox.grab_focus ();
836                 }
837
838                 hide_name_entry ();
839                 show_name_label ();
840
841                 gm.get_gain_slider().hide();
842                 mute_button->hide();
843                 solo_button->hide();
844                 if (rec_enable_button)
845                         rec_enable_button->hide();
846
847                 route_group_button.hide ();
848                 automation_button.hide ();
849                 playlist_button.hide ();
850                 name_label.set_text (_route->name());
851         }
852
853         if (height_changed && !no_redraw) {
854                 /* only emit the signal if the height really changed */
855                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
856         }
857 }
858
859 void
860 RouteTimeAxisView::select_track_color ()
861 {
862         if (RouteUI::choose_color ()) {
863
864                 if (_view) {
865                         _view->apply_color (_color, StreamView::RegionColor);
866                 }
867         }
868 }
869
870 void
871 RouteTimeAxisView::reset_samples_per_unit ()
872 {
873         set_samples_per_unit (_editor.get_current_zoom());
874 }
875
876 void
877 RouteTimeAxisView::horizontal_position_changed ()
878 {
879         if (_view) {
880                 _view->horizontal_position_changed ();
881         }
882 }
883
884 void
885 RouteTimeAxisView::set_samples_per_unit (double spu)
886 {
887         double speed = 1.0;
888
889         if (track()) {
890                 speed = track()->speed();
891         }
892
893         if (_view) {
894                 _view->set_samples_per_unit (spu * speed);
895         }
896
897         TimeAxisView::set_samples_per_unit (spu * speed);
898 }
899
900 void
901 RouteTimeAxisView::align_style_changed ()
902 {
903         switch (track()->alignment_style()) {
904         case ExistingMaterial:
905                 if (!align_existing_item->get_active()) {
906                         align_existing_item->set_active();
907                 }
908                 break;
909         case CaptureTime:
910                 if (!align_capture_item->get_active()) {
911                         align_capture_item->set_active();
912                 }
913                 break;
914         }
915 }
916
917 void
918 RouteTimeAxisView::set_align_style (AlignStyle style)
919 {
920         RadioMenuItem* item;
921
922         switch (style) {
923         case ExistingMaterial:
924                 item = align_existing_item;
925                 break;
926         case CaptureTime:
927                 item = align_capture_item;
928                 break;
929         default:
930                 fatal << string_compose (_("programming error: %1 %2"), "illegal align style in RouteTimeAxisView::set_align_style", style) << endmsg;
931                 /*NOTREACHED*/
932                 return;
933         }
934
935         if (item->get_active()) {
936                 track()->set_align_style (style);
937         }
938 }
939
940 void
941 RouteTimeAxisView::rename_current_playlist ()
942 {
943         ArdourPrompter prompter (true);
944         string name;
945
946         boost::shared_ptr<Track> tr = track();
947         if (!tr || tr->destructive()) {
948                 return;
949         }
950
951         boost::shared_ptr<Playlist> pl = tr->playlist();
952         if (!pl) {
953                 return;
954         }
955
956         prompter.set_title (_("Rename Playlist"));
957         prompter.set_prompt (_("New name for playlist:"));
958         prompter.set_initial_text (pl->name());
959         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
960         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
961
962         switch (prompter.run ()) {
963         case Gtk::RESPONSE_ACCEPT:
964                 prompter.get_result (name);
965                 if (name.length()) {
966                         pl->set_name (name);
967                 }
968                 break;
969
970         default:
971                 break;
972         }
973 }
974
975 std::string
976 RouteTimeAxisView::resolve_new_group_playlist_name(std::string &basename, vector<boost::shared_ptr<Playlist> > const & playlists)
977 {
978         std::string ret (basename);
979
980         std::string const group_string = "." + route_group()->name() + ".";
981
982         // iterate through all playlists
983         int maxnumber = 0;
984         for (vector<boost::shared_ptr<Playlist> >::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
985                 std::string tmp = (*i)->name();
986
987                 std::string::size_type idx = tmp.find(group_string);
988                 // find those which belong to this group
989                 if (idx != string::npos) {
990                         tmp = tmp.substr(idx + group_string.length());
991
992                         // and find the largest current number
993                         int x = atoi(tmp.c_str());
994                         if (x > maxnumber) {
995                                 maxnumber = x;
996                         }
997                 }
998         }
999
1000         maxnumber++;
1001
1002         char buf[32];
1003         snprintf (buf, sizeof(buf), "%d", maxnumber);
1004
1005         ret = this->name() + "." + route_group()->name () + "." + buf;
1006
1007         return ret;
1008 }
1009
1010 void
1011 RouteTimeAxisView::use_copy_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1012 {
1013         string name;
1014
1015         boost::shared_ptr<Track> tr = track ();
1016         if (!tr || tr->destructive()) {
1017                 return;
1018         }
1019
1020         boost::shared_ptr<const Playlist> pl = tr->playlist();
1021         if (!pl) {
1022                 return;
1023         }
1024
1025         name = pl->name();
1026
1027         if (route_group() && route_group()->is_active()) {
1028                 name = resolve_new_group_playlist_name(name, playlists_before_op);
1029         }
1030
1031         while (_session->playlists->by_name(name)) {
1032                 name = Playlist::bump_name (name, *_session);
1033         }
1034
1035         // TODO: The prompter "new" button should be de-activated if the user
1036         // specifies a playlist name which already exists in the session.
1037
1038         if (prompt) {
1039
1040                 ArdourPrompter prompter (true);
1041
1042                 prompter.set_title (_("New Copy Playlist"));
1043                 prompter.set_prompt (_("Name for new playlist:"));
1044                 prompter.set_initial_text (name);
1045                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1046                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1047                 prompter.show_all ();
1048
1049                 switch (prompter.run ()) {
1050                 case Gtk::RESPONSE_ACCEPT:
1051                         prompter.get_result (name);
1052                         break;
1053
1054                 default:
1055                         return;
1056                 }
1057         }
1058
1059         if (name.length()) {
1060                 tr->use_copy_playlist ();
1061                 tr->playlist()->set_name (name);
1062         }
1063 }
1064
1065 void
1066 RouteTimeAxisView::use_new_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1067 {
1068         string name;
1069
1070         boost::shared_ptr<Track> tr = track ();
1071         if (!tr || tr->destructive()) {
1072                 return;
1073         }
1074
1075         boost::shared_ptr<const Playlist> pl = tr->playlist();
1076         if (!pl) {
1077                 return;
1078         }
1079
1080         name = pl->name();
1081
1082         if (route_group() && route_group()->is_active()) {
1083                 name = resolve_new_group_playlist_name(name,playlists_before_op);
1084         }
1085
1086         while (_session->playlists->by_name(name)) {
1087                 name = Playlist::bump_name (name, *_session);
1088         }
1089
1090
1091         if (prompt) {
1092
1093                 ArdourPrompter prompter (true);
1094
1095                 prompter.set_title (_("New Playlist"));
1096                 prompter.set_prompt (_("Name for new playlist:"));
1097                 prompter.set_initial_text (name);
1098                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1099                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1100
1101                 switch (prompter.run ()) {
1102                 case Gtk::RESPONSE_ACCEPT:
1103                         prompter.get_result (name);
1104                         break;
1105
1106                 default:
1107                         return;
1108                 }
1109         }
1110
1111         if (name.length()) {
1112                 tr->use_new_playlist ();
1113                 tr->playlist()->set_name (name);
1114         }
1115 }
1116
1117 void
1118 RouteTimeAxisView::clear_playlist ()
1119 {
1120         boost::shared_ptr<Track> tr = track ();
1121         if (!tr || tr->destructive()) {
1122                 return;
1123         }
1124
1125         boost::shared_ptr<Playlist> pl = tr->playlist();
1126         if (!pl) {
1127                 return;
1128         }
1129
1130         _editor.clear_playlist (pl);
1131 }
1132
1133 void
1134 RouteTimeAxisView::speed_changed ()
1135 {
1136         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&RouteTimeAxisView::reset_samples_per_unit, this));
1137 }
1138
1139 void
1140 RouteTimeAxisView::update_diskstream_display ()
1141 {
1142         if (!track()) {
1143                 return;
1144         }
1145
1146         map_frozen ();
1147 }
1148
1149 void
1150 RouteTimeAxisView::selection_click (GdkEventButton* ev)
1151 {
1152         if (Keyboard::modifier_state_equals (ev->state, (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier))) {
1153
1154                 /* special case: select/deselect all tracks */
1155                 if (_editor.get_selection().selected (this)) {
1156                         _editor.get_selection().clear_tracks ();
1157                 } else {
1158                         _editor.select_all_tracks ();
1159                 }
1160
1161                 return;
1162         }
1163
1164         switch (ArdourKeyboard::selection_type (ev->state)) {
1165         case Selection::Toggle:
1166                 _editor.get_selection().toggle (this);
1167                 break;
1168
1169         case Selection::Set:
1170                 _editor.get_selection().set (this);
1171                 break;
1172
1173         case Selection::Extend:
1174                 _editor.extend_selection_to_track (*this);
1175                 break;
1176
1177         case Selection::Add:
1178                 _editor.get_selection().add (this);
1179                 break;
1180         }
1181 }
1182
1183 void
1184 RouteTimeAxisView::set_selected_points (PointSelection& points)
1185 {
1186         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1187                 (*i)->set_selected_points (points);
1188         }
1189 }
1190
1191 void
1192 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
1193 {
1194         if (_view) {
1195                 _view->set_selected_regionviews (regions);
1196         }
1197 }
1198
1199 /** Add the selectable things that we have to a list.
1200  * @param results List to add things to.
1201  */
1202 void
1203 RouteTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results)
1204 {
1205         double speed = 1.0;
1206
1207         if (track() != 0) {
1208                 speed = track()->speed();
1209         }
1210
1211         framepos_t const start_adjusted = session_frame_to_track_frame(start, speed);
1212         framepos_t const end_adjusted   = session_frame_to_track_frame(end, speed);
1213
1214         if ((_view && ((top < 0.0 && bot < 0.0))) || touched (top, bot)) {
1215                 _view->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1216         }
1217
1218         /* pick up visible automation tracks */
1219
1220         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1221                 if (!(*i)->hidden()) {
1222                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1223                 }
1224         }
1225 }
1226
1227 void
1228 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1229 {
1230         if (_view) {
1231                 _view->get_inverted_selectables (sel, results);
1232         }
1233
1234         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1235                 if (!(*i)->hidden()) {
1236                         (*i)->get_inverted_selectables (sel, results);
1237                 }
1238         }
1239
1240         return;
1241 }
1242
1243 RouteGroup*
1244 RouteTimeAxisView::route_group () const
1245 {
1246         return _route->route_group();
1247 }
1248
1249 string
1250 RouteTimeAxisView::name() const
1251 {
1252         return _route->name();
1253 }
1254
1255 boost::shared_ptr<Playlist>
1256 RouteTimeAxisView::playlist () const
1257 {
1258         boost::shared_ptr<Track> tr;
1259
1260         if ((tr = track()) != 0) {
1261                 return tr->playlist();
1262         } else {
1263                 return boost::shared_ptr<Playlist> ();
1264         }
1265 }
1266
1267 void
1268 RouteTimeAxisView::name_entry_changed ()
1269 {
1270         string x;
1271
1272         x = name_entry.get_text ();
1273
1274         if (x == _route->name()) {
1275                 return;
1276         }
1277
1278         strip_whitespace_edges(x);
1279
1280         if (x.length() == 0) {
1281                 name_entry.set_text (_route->name());
1282                 return;
1283         }
1284
1285         if (!_session->route_name_unique (x)) {
1286                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1287                 name_entry.set_text (_route->name());
1288         } else if (_session->route_name_internal (x)) {
1289                 ARDOUR_UI::instance()->popup_error (string_compose (_("You cannot create a track with that name as it is reserved for %1"),
1290                                                                     PROGRAM_NAME));
1291                 name_entry.set_text (_route->name());
1292         } else {
1293                 _route->set_name (x);
1294         }
1295 }
1296
1297 boost::shared_ptr<Region>
1298 RouteTimeAxisView::find_next_region (framepos_t pos, RegionPoint point, int32_t dir)
1299 {
1300         boost::shared_ptr<Playlist> pl = playlist ();
1301
1302         if (pl) {
1303                 return pl->find_next_region (pos, point, dir);
1304         }
1305
1306         return boost::shared_ptr<Region> ();
1307 }
1308
1309 framepos_t
1310 RouteTimeAxisView::find_next_region_boundary (framepos_t pos, int32_t dir)
1311 {
1312         boost::shared_ptr<Playlist> pl = playlist ();
1313
1314         if (pl) {
1315                 return pl->find_next_region_boundary (pos, dir);
1316         }
1317
1318         return -1;
1319 }
1320
1321 void
1322 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1323 {
1324         boost::shared_ptr<Playlist> what_we_got;
1325         boost::shared_ptr<Track> tr = track ();
1326         boost::shared_ptr<Playlist> playlist;
1327
1328         if (tr == 0) {
1329                 /* route is a bus, not a track */
1330                 return;
1331         }
1332
1333         playlist = tr->playlist();
1334
1335         TimeSelection time (selection.time);
1336         float const speed = tr->speed();
1337         if (speed != 1.0f) {
1338                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1339                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1340                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1341                 }
1342         }
1343
1344         playlist->clear_changes ();
1345         playlist->clear_owned_changes ();
1346
1347         switch (op) {
1348         case Cut:
1349                 if ((what_we_got = playlist->cut (time)) != 0) {
1350                         _editor.get_cut_buffer().add (what_we_got);
1351
1352                         vector<Command*> cmds;
1353                         playlist->rdiff (cmds);
1354                         _session->add_commands (cmds);
1355                         
1356                         _session->add_command (new StatefulDiffCommand (playlist));
1357                 }
1358                 break;
1359         case Copy:
1360                 if ((what_we_got = playlist->copy (time)) != 0) {
1361                         _editor.get_cut_buffer().add (what_we_got);
1362                 }
1363                 break;
1364
1365         case Clear:
1366                 if ((what_we_got = playlist->cut (time)) != 0) {
1367
1368                         vector<Command*> cmds;
1369                         playlist->rdiff (cmds);
1370                         _session->add_commands (cmds);
1371                         _session->add_command (new StatefulDiffCommand (playlist));
1372                         what_we_got->release ();
1373                 }
1374                 break;
1375         }
1376 }
1377
1378 bool
1379 RouteTimeAxisView::paste (framepos_t pos, float times, Selection& selection, size_t nth)
1380 {
1381         if (!is_track()) {
1382                 return false;
1383         }
1384
1385         boost::shared_ptr<Playlist> pl = playlist ();
1386         PlaylistSelection::iterator p;
1387
1388         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth) {}
1389
1390         if (p == selection.playlists.end()) {
1391                 return false;
1392         }
1393
1394         if (track()->speed() != 1.0f) {
1395                 pos = session_frame_to_track_frame (pos, track()->speed());
1396         }
1397
1398         pl->clear_changes ();
1399         pl->paste (*p, pos, times);
1400         _session->add_command (new StatefulDiffCommand (pl));
1401
1402         return true;
1403 }
1404
1405
1406 struct PlaylistSorter {
1407     bool operator() (boost::shared_ptr<Playlist> a, boost::shared_ptr<Playlist> b) const {
1408             return a->sort_id() < b->sort_id();
1409     }
1410 };
1411
1412 void
1413 RouteTimeAxisView::build_playlist_menu ()
1414 {
1415         using namespace Menu_Helpers;
1416
1417         if (!is_track()) {
1418                 return;
1419         }
1420
1421         delete playlist_action_menu;
1422         playlist_action_menu = new Menu;
1423         playlist_action_menu->set_name ("ArdourContextMenu");
1424
1425         MenuList& playlist_items = playlist_action_menu->items();
1426         playlist_action_menu->set_name ("ArdourContextMenu");
1427         playlist_items.clear();
1428
1429         delete playlist_menu;
1430
1431         vector<boost::shared_ptr<Playlist> > playlists, playlists_tr;
1432         boost::shared_ptr<Track> tr = track();
1433         RadioMenuItem::Group playlist_group;
1434
1435         _session->playlists->get (playlists);
1436
1437         /* find the playlists for this diskstream */
1438         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1439                 if (((*i)->get_orig_diskstream_id() == tr->diskstream_id()) || (tr->playlist()->id() == (*i)->id())) {
1440                         playlists_tr.push_back(*i);
1441                 }
1442         }
1443
1444         /* sort the playlists */
1445         PlaylistSorter cmp;
1446         sort (playlists_tr.begin(), playlists_tr.end(), cmp);
1447         
1448         /* add the playlists to the menu */
1449         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists_tr.begin(); i != playlists_tr.end(); ++i) {
1450                 playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name()));
1451                 RadioMenuItem *item = static_cast<RadioMenuItem*>(&playlist_items.back());
1452                 item->signal_toggled().connect(sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::use_playlist), item, boost::weak_ptr<Playlist> (*i)));
1453                 
1454                 if (tr->playlist()->id() == (*i)->id()) {
1455                         item->set_active();
1456                         
1457                 }
1458         }
1459         
1460         playlist_items.push_back (SeparatorElem());
1461         playlist_items.push_back (MenuElem (_("Rename..."), sigc::mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1462         playlist_items.push_back (SeparatorElem());
1463
1464         if (!route_group() || !route_group()->is_active()) {
1465                 playlist_items.push_back (MenuElem (_("New..."), sigc::bind(sigc::mem_fun(_editor, &PublicEditor::new_playlists), this)));
1466                 playlist_items.push_back (MenuElem (_("New Copy..."), sigc::bind(sigc::mem_fun(_editor, &PublicEditor::copy_playlists), this)));
1467
1468         } else {
1469                 // Use a label which tells the user what is happening
1470                 playlist_items.push_back (MenuElem (_("New Take"), sigc::bind(sigc::mem_fun(_editor, &PublicEditor::new_playlists), this)));
1471                 playlist_items.push_back (MenuElem (_("Copy Take"), sigc::bind(sigc::mem_fun(_editor, &PublicEditor::copy_playlists), this)));
1472
1473         }
1474
1475         playlist_items.push_back (SeparatorElem());
1476         playlist_items.push_back (MenuElem (_("Clear Current"), sigc::bind(sigc::mem_fun(_editor, &PublicEditor::clear_playlists), this)));
1477         playlist_items.push_back (SeparatorElem());
1478
1479         playlist_items.push_back (MenuElem(_("Select from all..."), sigc::mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1480 }
1481
1482 void
1483 RouteTimeAxisView::use_playlist (RadioMenuItem *item, boost::weak_ptr<Playlist> wpl)
1484 {
1485         assert (is_track());
1486
1487         // exit if we were triggered by deactivating the old playlist
1488         if (!item->get_active()) {
1489                 return;
1490         }
1491
1492         boost::shared_ptr<Playlist> pl (wpl.lock());
1493
1494         if (!pl) {
1495                 return;
1496         }
1497
1498         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
1499
1500         if (apl) {
1501                 if (track()->playlist() == apl) {
1502                         // exit when use_playlist is called by the creation of the playlist menu
1503                         // or the playlist choice is unchanged
1504                         return;
1505                 }
1506                 track()->use_playlist (apl);
1507
1508                 if (route_group() && route_group()->is_active()) {
1509                         std::string group_string = "."+route_group()->name()+".";
1510
1511                         std::string take_name = apl->name();
1512                         std::string::size_type idx = take_name.find(group_string);
1513
1514                         if (idx == std::string::npos)
1515                                 return;
1516
1517                         take_name = take_name.substr(idx + group_string.length()); // find the bit containing the take number / name
1518
1519                         boost::shared_ptr<RouteList> rl (route_group()->route_list());
1520
1521                         for (RouteList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
1522                                 if ( (*i) == this->route()) {
1523                                         continue;
1524                                 }
1525
1526                                 std::string playlist_name = (*i)->name()+group_string+take_name;
1527
1528                                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track>(*i);
1529                                 if (!track) {
1530                                         std::cerr << "route " << (*i)->name() << " is not a Track" << std::endl;
1531                                         continue;
1532                                 }
1533
1534                                 boost::shared_ptr<Playlist> ipl = session()->playlists->by_name(playlist_name);
1535                                 if (!ipl) {
1536                                         // No playlist for this track for this take yet, make it
1537                                         track->use_new_playlist();
1538                                         track->playlist()->set_name(playlist_name);
1539                                 } else {
1540                                         track->use_playlist(ipl);
1541                                 }
1542                         }
1543                 }
1544         }
1545 }
1546
1547 void
1548 RouteTimeAxisView::show_playlist_selector ()
1549 {
1550         _editor.playlist_selector().show_for (this);
1551 }
1552
1553 void
1554 RouteTimeAxisView::map_frozen ()
1555 {
1556         if (!is_track()) {
1557                 return;
1558         }
1559
1560         ENSURE_GUI_THREAD (*this, &RouteTimeAxisView::map_frozen)
1561
1562         switch (track()->freeze_state()) {
1563         case Track::Frozen:
1564                 playlist_button.set_sensitive (false);
1565                 rec_enable_button->set_sensitive (false);
1566                 break;
1567         default:
1568                 playlist_button.set_sensitive (true);
1569                 rec_enable_button->set_sensitive (true);
1570                 break;
1571         }
1572 }
1573
1574 void
1575 RouteTimeAxisView::color_handler ()
1576 {
1577         //case cTimeStretchOutline:
1578         if (timestretch_rect) {
1579                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
1580         }
1581         //case cTimeStretchFill:
1582         if (timestretch_rect) {
1583                 timestretch_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
1584         }
1585
1586         reset_meter();
1587 }
1588
1589 /** Toggle an automation track for a fully-specified Parameter (type,channel,id)
1590  *  Will add track if necessary.
1591  */
1592 void
1593 RouteTimeAxisView::toggle_automation_track (const Evoral::Parameter& param)
1594 {
1595         boost::shared_ptr<AutomationTimeAxisView> track = automation_child (param);
1596         Gtk::CheckMenuItem* menu = automation_child_menu_item (param);
1597         
1598         if (!track) {
1599                 /* it doesn't exist yet, so we don't care about the button state: just add it */
1600                 create_automation_child (param, true);
1601         } else {
1602                 assert (menu);
1603                 bool yn = menu->get_active();
1604                 if (track->set_visibility (menu->get_active()) && yn) {
1605                         
1606                         /* we made it visible, now trigger a redisplay. if it was hidden, then automation_track_hidden()
1607                            will have done that for us.
1608                         */
1609                         
1610                         if (!no_redraw) {
1611                                 _route->gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
1612                         } 
1613                 }
1614         }
1615 }
1616
1617 void
1618 RouteTimeAxisView::automation_track_hidden (Evoral::Parameter param)
1619 {
1620         boost::shared_ptr<AutomationTimeAxisView> track = automation_child (param);
1621
1622         if (!track) {
1623                 return;
1624         }
1625
1626         Gtk::CheckMenuItem* menu = automation_child_menu_item (param);
1627
1628         // if Evoral::Parameter::operator< doesn't obey strict weak ordering, we may crash here....
1629         track->get_state_node()->add_property (X_("shown"), X_("no"));
1630
1631         if (menu && !_hidden) {
1632                 ignore_toggle = true;
1633                 menu->set_active (false);
1634                 ignore_toggle = false;
1635         }
1636
1637         if (_route && !no_redraw) {
1638                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1639         }
1640 }
1641
1642
1643 void
1644 RouteTimeAxisView::show_all_automation ()
1645 {
1646         no_redraw = true;
1647
1648         /* Show our automation */
1649
1650         for (AutomationTracks::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1651                 i->second->set_marked_for_display (true);
1652                 i->second->canvas_display()->show();
1653                 i->second->get_state_node()->add_property ("shown", X_("yes"));
1654
1655                 Gtk::CheckMenuItem* menu = automation_child_menu_item (i->first);
1656                 
1657                 if (menu) {
1658                         menu->set_active(true);
1659                 }
1660         }
1661
1662
1663         /* Show processor automation */
1664
1665         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1666                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1667                         if ((*ii)->view == 0) {
1668                                 add_processor_automation_curve ((*i)->processor, (*ii)->what);
1669                         }
1670
1671                         (*ii)->menu_item->set_active (true);
1672                 }
1673         }
1674
1675         no_redraw = false;
1676
1677         /* Redraw */
1678
1679          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1680 }
1681
1682 void
1683 RouteTimeAxisView::show_existing_automation ()
1684 {
1685         no_redraw = true;
1686
1687         /* Show our automation */
1688
1689         for (AutomationTracks::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1690                 if (i->second->has_automation()) {
1691                         i->second->set_marked_for_display (true);
1692                         i->second->canvas_display()->show();
1693                         i->second->get_state_node()->add_property ("shown", X_("yes"));
1694
1695                         Gtk::CheckMenuItem* menu = automation_child_menu_item (i->first);
1696                         if (menu) {
1697                                 menu->set_active(true);
1698                         }
1699                 }
1700         }
1701
1702
1703         /* Show processor automation */
1704
1705         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1706                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1707                         if ((*ii)->view != 0 && (*i)->processor->control((*ii)->what)->list()->size() > 0) {
1708                                 (*ii)->menu_item->set_active (true);
1709                         }
1710                 }
1711         }
1712
1713         no_redraw = false;
1714
1715         _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1716 }
1717
1718 void
1719 RouteTimeAxisView::hide_all_automation ()
1720 {
1721         no_redraw = true;
1722
1723         /* Hide our automation */
1724
1725         for (AutomationTracks::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1726                 i->second->set_marked_for_display (false);
1727                 i->second->hide ();
1728                 i->second->get_state_node()->add_property ("shown", X_("no"));
1729
1730                 Gtk::CheckMenuItem* menu = automation_child_menu_item (i->first);
1731                 
1732                 if (menu) {
1733                         menu->set_active (false);
1734                 }
1735         }
1736
1737         /* Hide processor automation */
1738
1739         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1740                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1741                         (*ii)->menu_item->set_active (false);
1742                 }
1743         }
1744
1745         no_redraw = false;
1746          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1747 }
1748
1749
1750 void
1751 RouteTimeAxisView::region_view_added (RegionView* rv)
1752 {
1753         /* XXX need to find out if automation children have automationstreamviews. If yes, no ghosts */
1754         if (is_audio_track()) {
1755                 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1756                         boost::shared_ptr<AutomationTimeAxisView> atv;
1757
1758                         if ((atv = boost::dynamic_pointer_cast<AutomationTimeAxisView> (*i)) != 0) {
1759                                 atv->add_ghost(rv);
1760                         }
1761                 }
1762         }
1763
1764         for (UnderlayMirrorList::iterator i = _underlay_mirrors.begin(); i != _underlay_mirrors.end(); ++i) {
1765                 (*i)->add_ghost(rv);
1766         }
1767 }
1768
1769 RouteTimeAxisView::ProcessorAutomationInfo::~ProcessorAutomationInfo ()
1770 {
1771         for (vector<ProcessorAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1772                 delete *i;
1773         }
1774 }
1775
1776
1777 RouteTimeAxisView::ProcessorAutomationNode::~ProcessorAutomationNode ()
1778 {
1779         parent.remove_processor_automation_node (this);
1780 }
1781
1782 void
1783 RouteTimeAxisView::remove_processor_automation_node (ProcessorAutomationNode* pan)
1784 {
1785         if (pan->view) {
1786                 remove_child (pan->view);
1787         }
1788 }
1789
1790 RouteTimeAxisView::ProcessorAutomationNode*
1791 RouteTimeAxisView::find_processor_automation_node (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
1792 {
1793         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1794
1795                 if ((*i)->processor == processor) {
1796
1797                         for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1798                                 if ((*ii)->what == what) {
1799                                         return *ii;
1800                                 }
1801                         }
1802                 }
1803         }
1804
1805         return 0;
1806 }
1807
1808 static string
1809 legalize_for_xml_node (string str)
1810 {
1811         string::size_type pos;
1812         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=:";
1813         string legal;
1814
1815         legal = str;
1816         pos = 0;
1817
1818         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1819                 legal.replace (pos, 1, "_");
1820                 pos += 1;
1821         }
1822
1823         return legal;
1824 }
1825
1826
1827 void
1828 RouteTimeAxisView::add_processor_automation_curve (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
1829 {
1830         string name;
1831         ProcessorAutomationNode* pan;
1832
1833         if ((pan = find_processor_automation_node (processor, what)) == 0) {
1834                 /* session state may never have been saved with new plugin */
1835                 error << _("programming error: ")
1836                       << string_compose (X_("processor automation curve for %1:%2/%3/%4 not registered with track!"),
1837                                          processor->name(), what.type(), (int) what.channel(), what.id() )
1838                       << endmsg;
1839                 /*NOTREACHED*/
1840                 return;
1841         }
1842
1843         if (pan->view) {
1844                 return;
1845         }
1846
1847         name = processor->describe_parameter (what);
1848
1849         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1850
1851         /* FIXME: ew */
1852
1853         char state_name[256];
1854         snprintf (state_name, sizeof (state_name), "%s-%" PRIu32, legalize_for_xml_node (processor->name()).c_str(), what.id());
1855
1856         boost::shared_ptr<AutomationControl> control
1857                         = boost::dynamic_pointer_cast<AutomationControl>(processor->control(what, true));
1858
1859         pan->view = boost::shared_ptr<AutomationTimeAxisView>(
1860                         new AutomationTimeAxisView (_session, _route, processor, control,
1861                                 _editor, *this, false, parent_canvas, name, state_name));
1862
1863         pan->view->Hiding.connect (sigc::bind (sigc::mem_fun(*this, &RouteTimeAxisView::processor_automation_track_hidden), pan, processor));
1864
1865         if (!pan->view->marked_for_display()) {
1866                 pan->view->hide ();
1867         } else {
1868                 pan->menu_item->set_active (true);
1869         }
1870
1871         add_child (pan->view);
1872
1873         if (_view) {
1874                 _view->foreach_regionview (sigc::mem_fun(*pan->view.get(), &TimeAxisView::add_ghost));
1875         }
1876
1877         processor->mark_automation_visible (what, true);
1878 }
1879
1880 void
1881 RouteTimeAxisView::processor_automation_track_hidden (RouteTimeAxisView::ProcessorAutomationNode* pan, boost::shared_ptr<Processor> i)
1882 {
1883         if (!_hidden) {
1884                 pan->menu_item->set_active (false);
1885         }
1886
1887         i->mark_automation_visible (pan->what, false);
1888
1889         if (!no_redraw) {
1890                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1891         }
1892 }
1893
1894 void
1895 RouteTimeAxisView::add_existing_processor_automation_curves (boost::weak_ptr<Processor> p)
1896 {
1897         boost::shared_ptr<Processor> processor (p.lock ());
1898         if (!processor) {
1899                 return;
1900         }
1901
1902         set<Evoral::Parameter> s;
1903         boost::shared_ptr<AutomationLine> al;
1904
1905         processor->what_has_visible_data (s);
1906
1907         for (set<Evoral::Parameter>::iterator i = s.begin(); i != s.end(); ++i) {
1908
1909                 if ((al = find_processor_automation_curve (processor, *i)) != 0) {
1910                         al->queue_reset ();
1911                 } else {
1912                         add_processor_automation_curve (processor, (*i));
1913                 }
1914         }
1915 }
1916
1917 void
1918 RouteTimeAxisView::add_automation_child (Evoral::Parameter param, boost::shared_ptr<AutomationTimeAxisView> track, bool show)
1919 {
1920         using namespace Menu_Helpers;
1921
1922         XMLProperty* prop;
1923         XMLNode* node;
1924
1925         add_child (track);
1926
1927         track->Hiding.connect (sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::automation_track_hidden), param));
1928
1929         bool hideit = (!show);
1930
1931         if ((node = track->get_state_node()) != 0) {
1932                 if  ((prop = node->property ("shown")) != 0) {
1933                         if (string_is_affirmative (prop->value())) {
1934                                 hideit = false;
1935                         }
1936                 }
1937         }
1938
1939         _automation_tracks[param] = track;
1940
1941         track->set_visibility (!hideit);
1942
1943         if (!no_redraw) {
1944                 _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1945         }
1946
1947         if (!EventTypeMap::instance().is_midi_parameter(param)) {
1948                 /* MIDI-related parameters are always in the menu, there's no
1949                    reason to rebuild the menu just because we added a automation
1950                    lane for one of them. But if we add a non-MIDI automation
1951                    lane, then we need to invalidate the display menu.
1952                 */
1953                 delete display_menu;
1954                 display_menu = 0;
1955         }
1956 }
1957
1958 void
1959 RouteTimeAxisView::add_processor_to_subplugin_menu (boost::weak_ptr<Processor> p)
1960 {
1961         boost::shared_ptr<Processor> processor (p.lock ());
1962
1963         if (!processor || !processor->display_to_user ()) {
1964                 return;
1965         }
1966
1967         using namespace Menu_Helpers;
1968         ProcessorAutomationInfo *rai;
1969         list<ProcessorAutomationInfo*>::iterator x;
1970
1971         const std::set<Evoral::Parameter>& automatable = processor->what_can_be_automated ();
1972         std::set<Evoral::Parameter> has_visible_automation;
1973
1974         processor->what_has_visible_data(has_visible_automation);
1975
1976         if (automatable.empty()) {
1977                 return;
1978         }
1979
1980         for (x = processor_automation.begin(); x != processor_automation.end(); ++x) {
1981                 if ((*x)->processor == processor) {
1982                         break;
1983                 }
1984         }
1985
1986         if (x == processor_automation.end()) {
1987
1988                 rai = new ProcessorAutomationInfo (processor);
1989                 processor_automation.push_back (rai);
1990
1991         } else {
1992
1993                 rai = *x;
1994
1995         }
1996
1997         /* any older menu was deleted at the top of processors_changed()
1998            when we cleared the subplugin menu.
1999         */
2000
2001         rai->menu = manage (new Menu);
2002         MenuList& items = rai->menu->items();
2003         rai->menu->set_name ("ArdourContextMenu");
2004
2005         items.clear ();
2006
2007         for (std::set<Evoral::Parameter>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
2008
2009                 ProcessorAutomationNode* pan;
2010                 CheckMenuItem* mitem;
2011
2012                 string name = processor->describe_parameter (*i);
2013
2014                 items.push_back (CheckMenuElem (name));
2015                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
2016
2017                 _subplugin_menu_map[*i] = mitem;
2018
2019                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
2020                         mitem->set_active(true);
2021                 }
2022
2023                 if ((pan = find_processor_automation_node (processor, *i)) == 0) {
2024
2025                         /* new item */
2026
2027                         pan = new ProcessorAutomationNode (*i, mitem, *this);
2028
2029                         rai->lines.push_back (pan);
2030
2031                 } else {
2032
2033                         pan->menu_item = mitem;
2034
2035                 }
2036
2037                 mitem->signal_toggled().connect (sigc::bind (sigc::mem_fun(*this, &RouteTimeAxisView::processor_menu_item_toggled), rai, pan));
2038         }
2039
2040         /* add the menu for this processor, because the subplugin
2041            menu is always cleared at the top of processors_changed().
2042            this is the result of some poor design in gtkmm and/or
2043            GTK+.
2044         */
2045
2046         subplugin_menu.items().push_back (MenuElem (processor->name(), *rai->menu));
2047         rai->valid = true;
2048 }
2049
2050 void
2051 RouteTimeAxisView::processor_menu_item_toggled (RouteTimeAxisView::ProcessorAutomationInfo* rai,
2052                                                RouteTimeAxisView::ProcessorAutomationNode* pan)
2053 {
2054         bool showit = pan->menu_item->get_active();
2055         bool redraw = false;
2056
2057         if (pan->view == 0 && showit) {
2058                 add_processor_automation_curve (rai->processor, pan->what);
2059                 redraw = true;
2060         }
2061
2062         if (pan->view && showit != pan->view->marked_for_display()) {
2063
2064                 if (showit) {
2065                         pan->view->set_marked_for_display (true);
2066                         pan->view->canvas_display()->show();
2067                         pan->view->canvas_background()->show();
2068                 } else {
2069                         rai->processor->mark_automation_visible (pan->what, true);
2070                         pan->view->set_marked_for_display (false);
2071                         pan->view->hide ();
2072                 }
2073
2074                 redraw = true;
2075
2076         }
2077
2078         if (redraw && !no_redraw) {
2079                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
2080
2081         }
2082 }
2083
2084 void
2085 RouteTimeAxisView::processors_changed (RouteProcessorChange c)
2086 {
2087         if (c.type == RouteProcessorChange::MeterPointChange) {
2088                 /* nothing to do if only the meter point has changed */
2089                 return;
2090         }
2091
2092         using namespace Menu_Helpers;
2093
2094         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
2095                 (*i)->valid = false;
2096         }
2097
2098         _subplugin_menu_map.clear ();
2099         subplugin_menu.items().clear ();
2100
2101         _route->foreach_processor (sigc::mem_fun (*this, &RouteTimeAxisView::add_processor_to_subplugin_menu));
2102         _route->foreach_processor (sigc::mem_fun (*this, &RouteTimeAxisView::add_existing_processor_automation_curves));
2103
2104         bool deleted_processor_automation = false;
2105
2106         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ) {
2107
2108                 list<ProcessorAutomationInfo*>::iterator tmp;
2109
2110                 tmp = i;
2111                 ++tmp;
2112
2113                 if (!(*i)->valid) {
2114
2115                         delete *i;
2116                         processor_automation.erase (i);
2117                         deleted_processor_automation = true;
2118
2119                 }
2120
2121                 i = tmp;
2122         }
2123
2124         if (deleted_processor_automation && !no_redraw) {
2125                 _route->gui_changed ("track_height", this);
2126         }
2127 }
2128
2129 boost::shared_ptr<AutomationLine>
2130 RouteTimeAxisView::find_processor_automation_curve (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
2131 {
2132         ProcessorAutomationNode* pan;
2133
2134         if ((pan = find_processor_automation_node (processor, what)) != 0) {
2135                 if (pan->view) {
2136                         pan->view->line();
2137                 }
2138         }
2139
2140         return boost::shared_ptr<AutomationLine>();
2141 }
2142
2143 void
2144 RouteTimeAxisView::reset_processor_automation_curves ()
2145 {
2146         for (ProcessorAutomationCurves::iterator i = processor_automation_curves.begin(); i != processor_automation_curves.end(); ++i) {
2147                 (*i)->reset();
2148         }
2149 }
2150
2151 void
2152 RouteTimeAxisView::update_rec_display ()
2153 {
2154         RouteUI::update_rec_display ();
2155         name_entry.set_sensitive (!_route->record_enabled());
2156 }
2157
2158 void
2159 RouteTimeAxisView::set_layer_display (LayerDisplay d)
2160 {
2161         if (_view) {
2162                 _view->set_layer_display (d);
2163         }
2164
2165         ensure_xml_node ();
2166         xml_node->add_property (N_("layer-display"), enum_2_string (d));
2167 }
2168
2169 LayerDisplay
2170 RouteTimeAxisView::layer_display () const
2171 {
2172         if (_view) {
2173                 return _view->layer_display ();
2174         }
2175
2176         /* we don't know, since we don't have a _view, so just return something */
2177         return Overlaid;
2178 }
2179
2180
2181
2182 boost::shared_ptr<AutomationTimeAxisView>
2183 RouteTimeAxisView::automation_child(Evoral::Parameter param)
2184 {
2185         AutomationTracks::iterator i = _automation_tracks.find(param);
2186         if (i != _automation_tracks.end()) {
2187                 return i->second;
2188         } else {
2189                 return boost::shared_ptr<AutomationTimeAxisView>();
2190         }
2191 }
2192
2193 void
2194 RouteTimeAxisView::fast_update ()
2195 {
2196         gm.get_level_meter().update_meters ();
2197 }
2198
2199 void
2200 RouteTimeAxisView::hide_meter ()
2201 {
2202         clear_meter ();
2203         gm.get_level_meter().hide_meters ();
2204 }
2205
2206 void
2207 RouteTimeAxisView::show_meter ()
2208 {
2209         reset_meter ();
2210 }
2211
2212 void
2213 RouteTimeAxisView::reset_meter ()
2214 {
2215         if (Config->get_show_track_meters()) {
2216                 gm.get_level_meter().setup_meters (height-5);
2217         } else {
2218                 hide_meter ();
2219         }
2220 }
2221
2222 void
2223 RouteTimeAxisView::clear_meter ()
2224 {
2225         gm.get_level_meter().clear_meters ();
2226 }
2227
2228 void
2229 RouteTimeAxisView::meter_changed ()
2230 {
2231         ENSURE_GUI_THREAD (*this, &RouteTimeAxisView::meter_changed)
2232         reset_meter();
2233 }
2234
2235 void
2236 RouteTimeAxisView::io_changed (IOChange /*change*/, void */*src*/)
2237 {
2238         reset_meter ();
2239 }
2240
2241 void
2242 RouteTimeAxisView::build_underlay_menu(Gtk::Menu* parent_menu)
2243 {
2244         using namespace Menu_Helpers;
2245
2246         if (!_underlay_streams.empty()) {
2247                 MenuList& parent_items = parent_menu->items();
2248                 Menu* gs_menu = manage (new Menu);
2249                 gs_menu->set_name ("ArdourContextMenu");
2250                 MenuList& gs_items = gs_menu->items();
2251
2252                 parent_items.push_back (MenuElem (_("Underlays"), *gs_menu));
2253
2254                 for(UnderlayList::iterator it = _underlay_streams.begin(); it != _underlay_streams.end(); ++it) {
2255                         gs_items.push_back(MenuElem(string_compose(_("Remove \"%1\""), (*it)->trackview().name()),
2256                                                     sigc::bind(sigc::mem_fun(*this, &RouteTimeAxisView::remove_underlay), *it)));
2257                 }
2258         }
2259 }
2260
2261 bool
2262 RouteTimeAxisView::set_underlay_state()
2263 {
2264         if (!underlay_xml_node) {
2265                 return false;
2266         }
2267
2268         XMLNodeList nlist = underlay_xml_node->children();
2269         XMLNodeConstIterator niter;
2270         XMLNode *child_node;
2271
2272         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2273                 child_node = *niter;
2274
2275                 if (child_node->name() != "Underlay") {
2276                         continue;
2277                 }
2278
2279                 XMLProperty* prop = child_node->property ("id");
2280                 if (prop) {
2281                         PBD::ID id (prop->value());
2282
2283                         RouteTimeAxisView* v = _editor.get_route_view_by_route_id (id);
2284
2285                         if (v) {
2286                                 add_underlay(v->view(), false);
2287                         }
2288                 }
2289         }
2290
2291         return false;
2292 }
2293
2294 void
2295 RouteTimeAxisView::add_underlay (StreamView* v, bool update_xml)
2296 {
2297         if (!v) {
2298                 return;
2299         }
2300
2301         RouteTimeAxisView& other = v->trackview();
2302
2303         if (find(_underlay_streams.begin(), _underlay_streams.end(), v) == _underlay_streams.end()) {
2304                 if (find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this) != other._underlay_mirrors.end()) {
2305                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2306                         /*NOTREACHED*/
2307                 }
2308
2309                 _underlay_streams.push_back(v);
2310                 other._underlay_mirrors.push_back(this);
2311
2312                 v->foreach_regionview(sigc::mem_fun(*this, &RouteTimeAxisView::add_ghost));
2313
2314                 if (update_xml) {
2315                         if (!underlay_xml_node) {
2316                                 ensure_xml_node();
2317                                 underlay_xml_node = xml_node->add_child("Underlays");
2318                         }
2319
2320                         XMLNode* node = underlay_xml_node->add_child("Underlay");
2321                         XMLProperty* prop = node->add_property("id");
2322                         prop->set_value(v->trackview().route()->id().to_s());
2323                 }
2324         }
2325 }
2326
2327 void
2328 RouteTimeAxisView::remove_underlay (StreamView* v)
2329 {
2330         if (!v) {
2331                 return;
2332         }
2333
2334         UnderlayList::iterator it = find(_underlay_streams.begin(), _underlay_streams.end(), v);
2335         RouteTimeAxisView& other = v->trackview();
2336
2337         if (it != _underlay_streams.end()) {
2338                 UnderlayMirrorList::iterator gm = find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this);
2339
2340                 if (gm == other._underlay_mirrors.end()) {
2341                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2342                         /*NOTREACHED*/
2343                 }
2344
2345                 v->foreach_regionview(sigc::mem_fun(*this, &RouteTimeAxisView::remove_ghost));
2346
2347                 _underlay_streams.erase(it);
2348                 other._underlay_mirrors.erase(gm);
2349
2350                 if (underlay_xml_node) {
2351                         underlay_xml_node->remove_nodes_and_delete("id", v->trackview().route()->id().to_s());
2352                 }
2353         }
2354 }
2355
2356 void
2357 RouteTimeAxisView::set_button_names ()
2358 {
2359         rec_enable_button_label.set_text (_("r"));
2360
2361         if (_route && _route->solo_safe()) {
2362                 solo_button_label.set_text (X_("!"));
2363         } else {
2364                 if (Config->get_solo_control_is_listen_control()) {
2365                         switch (Config->get_listen_position()) {
2366                         case AfterFaderListen:
2367                                 solo_button_label.set_text (_("A"));
2368                                 break;
2369                         case PreFaderListen:
2370                                 solo_button_label.set_text (_("P"));
2371                                 break;
2372                         }
2373                 } else {
2374                         solo_button_label.set_text (_("s"));
2375                 }
2376         }
2377         mute_button_label.set_text (_("m"));
2378 }
2379
2380 Gtk::CheckMenuItem*
2381 RouteTimeAxisView::automation_child_menu_item (Evoral::Parameter param)
2382 {
2383         ParameterMenuMap::iterator i = _main_automation_menu_map.find (param);
2384         if (i != _main_automation_menu_map.end()) {
2385                 return i->second;
2386         }
2387         
2388         i = _subplugin_menu_map.find (param);
2389         if (i != _subplugin_menu_map.end()) {
2390                 return i->second;
2391         }
2392
2393         return 0;
2394 }
2395
2396 void
2397 RouteTimeAxisView::create_gain_automation_child (const Evoral::Parameter& param, bool show)
2398 {
2399         boost::shared_ptr<AutomationControl> c = _route->gain_control();
2400         if (!c) {
2401                 error << "Route has no gain automation, unable to add automation track view." << endmsg;
2402                 return;
2403         }
2404
2405         gain_track.reset (new AutomationTimeAxisView (_session,
2406                                                       _route, _route->amp(), c,
2407                                                       _editor,
2408                                                       *this,
2409                                                       false,
2410                                                       parent_canvas,
2411                                                       _route->amp()->describe_parameter(param)));
2412         
2413         add_automation_child (Evoral::Parameter(GainAutomation), gain_track, show);
2414 }