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