4ec62f5fcc8f220777a63857950ac17a480be08e
[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         if (x != name_label.get_text()) {
325                 name_label.set_text (x);
326         }
327
328         ARDOUR_UI::instance()->set_tip (name_entry, x);
329 }
330
331 void
332 RouteTimeAxisView::route_property_changed (const PropertyChange& what_changed)
333 {
334         if (what_changed.contains (ARDOUR::Properties::name)) {
335                 label_view ();
336         }
337 }
338
339 void
340 RouteTimeAxisView::take_name_changed (void *src)
341 {
342         if (src != this) {
343                 label_view ();
344         }
345 }
346
347 void
348 RouteTimeAxisView::playlist_click ()
349 {
350         build_playlist_menu ();
351         conditionally_add_to_selection ();
352         playlist_action_menu->popup (1, gtk_get_current_event_time());
353 }
354
355 void
356 RouteTimeAxisView::automation_click ()
357 {
358         conditionally_add_to_selection ();
359         build_automation_action_menu ();
360         automation_action_menu->popup (1, gtk_get_current_event_time());
361 }
362
363 int
364 RouteTimeAxisView::set_state (const XMLNode& node, int version)
365 {
366         TimeAxisView::set_state (node, version);
367
368         XMLNodeList kids = node.children();
369         XMLNodeConstIterator iter;
370         const XMLProperty* prop;
371
372         if (_view && (prop = node.property ("layer-display"))) {
373                 set_layer_display (LayerDisplay (string_2_enum (prop->value(), _view->layer_display ())));
374         }
375
376         for (iter = kids.begin(); iter != kids.end(); ++iter) {
377                 if ((*iter)->name() == AutomationTimeAxisView::state_node_name) {
378                         if ((prop = (*iter)->property ("automation-id")) != 0) {
379
380                                 Evoral::Parameter param = ARDOUR::EventTypeMap::instance().new_parameter(prop->value());
381                                 bool show = ((prop = (*iter)->property ("shown")) != 0) && string_is_affirmative (prop->value());
382                                 create_automation_child(param, show);
383                         } else {
384                                 warning << "Automation child has no ID" << endmsg;
385                         }
386                 }
387         }
388
389         return 0;
390 }
391
392 void
393 RouteTimeAxisView::build_automation_action_menu ()
394 {
395         using namespace Menu_Helpers;
396
397         /* detach subplugin_menu from automation_action_menu before we delete automation_action_menu,
398            otherwise bad things happen (see comment for similar case in MidiTimeAxisView::build_automation_action_menu)
399         */
400
401         detach_menu (subplugin_menu);
402
403         _main_automation_menu_map.clear ();
404         delete automation_action_menu;
405         automation_action_menu = new Menu;
406
407         MenuList& items = automation_action_menu->items();
408
409         automation_action_menu->set_name ("ArdourContextMenu");
410         
411         items.push_back (MenuElem (_("Show All Automation"),
412                                    sigc::mem_fun(*this, &RouteTimeAxisView::show_all_automation)));
413         
414         items.push_back (MenuElem (_("Show Existing Automation"),
415                                    sigc::mem_fun(*this, &RouteTimeAxisView::show_existing_automation)));
416         
417         items.push_back (MenuElem (_("Hide All Automation"),
418                                    sigc::mem_fun(*this, &RouteTimeAxisView::hide_all_automation)));
419
420         items.push_back (SeparatorElem ());
421         
422         /* Attach the plugin submenu. It may have previously been used elsewhere,
423            so it was detached above */
424
425         items.push_back (MenuElem (_("Plugins"), subplugin_menu));
426         items.back().set_sensitive (!subplugin_menu.items().empty());
427 }
428
429 void
430 RouteTimeAxisView::build_display_menu ()
431 {
432         using namespace Menu_Helpers;
433
434         /* prepare it */
435
436         TimeAxisView::build_display_menu ();
437
438         /* now fill it with our stuff */
439
440         MenuList& items = display_menu->items();
441         display_menu->set_name ("ArdourContextMenu");
442
443         items.push_back (MenuElem (_("Color..."), sigc::mem_fun(*this, &RouteTimeAxisView::select_track_color)));
444
445         items.push_back (SeparatorElem());
446
447         if (!Profile->get_sae()) {
448                 items.push_back (MenuElem (_("Remote Control ID..."), sigc::mem_fun (*this, &RouteUI::open_remote_control_id_dialog)));
449                 /* rebuild this every time */
450                 build_automation_action_menu ();
451                 detach_menu (*automation_action_menu);
452                 items.push_back (MenuElem (_("Automation"), *automation_action_menu));
453                 items.push_back (SeparatorElem());
454         }
455
456         // Hook for derived classes to add type specific stuff
457         append_extra_display_menu_items ();
458         items.push_back (SeparatorElem());
459
460         if (is_track()) {
461
462                 Menu *layers_menu = manage(new Menu);
463                 MenuList &layers_items = layers_menu->items();
464                 layers_menu->set_name("ArdourContextMenu");
465
466                 RadioMenuItem::Group layers_group;
467
468                 layers_items.push_back(RadioMenuElem (layers_group, _("Overlaid"),
469                                 sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::set_layer_display), Overlaid)));
470                 layers_items.push_back(RadioMenuElem (layers_group, _("Stacked"),
471                                 sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::set_layer_display), Stacked)));
472
473                 items.push_back (MenuElem (_("Layers"), *layers_menu));
474
475                 if (!Profile->get_sae()) {
476
477                         Menu* alignment_menu = manage (new Menu);
478                         MenuList& alignment_items = alignment_menu->items();
479                         alignment_menu->set_name ("ArdourContextMenu");
480                         
481                         RadioMenuItem::Group align_group;
482                         
483                         alignment_items.push_back (RadioMenuElem (align_group, _("Align With Existing Material"),
484                                                                   sigc::bind (sigc::mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
485                         align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
486                         if (track()->alignment_style() == ExistingMaterial) {
487                                 align_existing_item->set_active();
488                         }
489                         
490                         alignment_items.push_back (RadioMenuElem (align_group, _("Align With Capture Time"),
491                                                                   sigc::bind (sigc::mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
492                         align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
493                         if (track()->alignment_style() == CaptureTime) {
494                                 align_capture_item->set_active();
495                         }
496                         
497                         items.push_back (MenuElem (_("Alignment"), *alignment_menu));
498                         track()->AlignmentStyleChanged.connect (route_connections, invalidator (*this), boost::bind (&RouteTimeAxisView::align_style_changed, this), gui_context());
499
500                         Menu* mode_menu = manage (new Menu);
501                         MenuList& mode_items = mode_menu->items ();
502                         mode_menu->set_name ("ArdourContextMenu");
503
504                         RadioMenuItem::Group mode_group;
505
506                         mode_items.push_back (RadioMenuElem (mode_group, _("Normal Mode"), sigc::bind (
507                                         sigc::mem_fun (*this, &RouteTimeAxisView::set_track_mode),
508                                         ARDOUR::Normal)));
509                         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&mode_items.back());
510
511                         mode_items.push_back (RadioMenuElem (mode_group, _("Tape Mode"), sigc::bind (
512                                         sigc::mem_fun (*this, &RouteTimeAxisView::set_track_mode),
513                                         ARDOUR::Destructive)));
514                         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&mode_items.back());
515
516                         mode_items.push_back (RadioMenuElem (mode_group, _("Non-Layered Mode"),
517                                                         sigc::bind (sigc::mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::NonLayered)));
518                         non_layered_track_mode_item = dynamic_cast<RadioMenuItem*>(&mode_items.back());
519
520
521                         _ignore_track_mode_change = true;
522                         
523                         switch (track()->mode()) {
524                         case ARDOUR::Destructive:
525                                 destructive_track_mode_item->set_active ();
526                                 break;
527                         case ARDOUR::Normal:
528                                 normal_track_mode_item->set_active ();
529                                 break;
530                         case ARDOUR::NonLayered:
531                                 non_layered_track_mode_item->set_active ();
532                                 break;
533                         }
534                         
535                         _ignore_track_mode_change = false;
536
537                         items.push_back (MenuElem (_("Mode"), *mode_menu));
538                 }
539
540                 color_mode_menu = build_color_mode_menu();
541                 if (color_mode_menu) {
542                         items.push_back (MenuElem (_("Color Mode"), *color_mode_menu));
543                 }
544
545                 items.push_back (SeparatorElem());
546
547                 build_playlist_menu ();
548                 items.push_back (MenuElem (_("Playlist"), *playlist_action_menu));
549
550                 route_group_menu->rebuild (_route->route_group ());
551                 items.push_back (MenuElem (_("Route Group"), *route_group_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::select_track_color ()
864 {
865         if (RouteUI::choose_color ()) {
866
867                 if (_view) {
868                         _view->apply_color (_color, StreamView::RegionColor);
869                 }
870         }
871 }
872
873 void
874 RouteTimeAxisView::reset_samples_per_unit ()
875 {
876         set_samples_per_unit (_editor.get_current_zoom());
877 }
878
879 void
880 RouteTimeAxisView::horizontal_position_changed ()
881 {
882         if (_view) {
883                 _view->horizontal_position_changed ();
884         }
885 }
886
887 void
888 RouteTimeAxisView::set_samples_per_unit (double spu)
889 {
890         double speed = 1.0;
891
892         if (track()) {
893                 speed = track()->speed();
894         }
895
896         if (_view) {
897                 _view->set_samples_per_unit (spu * speed);
898         }
899
900         TimeAxisView::set_samples_per_unit (spu * speed);
901 }
902
903 void
904 RouteTimeAxisView::align_style_changed ()
905 {
906         switch (track()->alignment_style()) {
907         case ExistingMaterial:
908                 if (!align_existing_item->get_active()) {
909                         align_existing_item->set_active();
910                 }
911                 break;
912         case CaptureTime:
913                 if (!align_capture_item->get_active()) {
914                         align_capture_item->set_active();
915                 }
916                 break;
917         }
918 }
919
920 void
921 RouteTimeAxisView::set_align_style (AlignStyle style)
922 {
923         RadioMenuItem* item;
924
925         switch (style) {
926         case ExistingMaterial:
927                 item = align_existing_item;
928                 break;
929         case CaptureTime:
930                 item = align_capture_item;
931                 break;
932         default:
933                 fatal << string_compose (_("programming error: %1 %2"), "illegal align style in RouteTimeAxisView::set_align_style", style) << endmsg;
934                 /*NOTREACHED*/
935                 return;
936         }
937
938         if (item->get_active()) {
939                 track()->set_align_style (style);
940         }
941 }
942
943 void
944 RouteTimeAxisView::rename_current_playlist ()
945 {
946         ArdourPrompter prompter (true);
947         string name;
948
949         boost::shared_ptr<Track> tr = track();
950         if (!tr || tr->destructive()) {
951                 return;
952         }
953
954         boost::shared_ptr<Playlist> pl = tr->playlist();
955         if (!pl) {
956                 return;
957         }
958
959         prompter.set_title (_("Rename Playlist"));
960         prompter.set_prompt (_("New name for playlist:"));
961         prompter.set_initial_text (pl->name());
962         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
963         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
964
965         switch (prompter.run ()) {
966         case Gtk::RESPONSE_ACCEPT:
967                 prompter.get_result (name);
968                 if (name.length()) {
969                         pl->set_name (name);
970                 }
971                 break;
972
973         default:
974                 break;
975         }
976 }
977
978 std::string
979 RouteTimeAxisView::resolve_new_group_playlist_name(std::string &basename, vector<boost::shared_ptr<Playlist> > const & playlists)
980 {
981         std::string ret (basename);
982
983         std::string const group_string = "." + route_group()->name() + ".";
984
985         // iterate through all playlists
986         int maxnumber = 0;
987         for (vector<boost::shared_ptr<Playlist> >::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
988                 std::string tmp = (*i)->name();
989
990                 std::string::size_type idx = tmp.find(group_string);
991                 // find those which belong to this group
992                 if (idx != string::npos) {
993                         tmp = tmp.substr(idx + group_string.length());
994
995                         // and find the largest current number
996                         int x = atoi(tmp.c_str());
997                         if (x > maxnumber) {
998                                 maxnumber = x;
999                         }
1000                 }
1001         }
1002
1003         maxnumber++;
1004
1005         char buf[32];
1006         snprintf (buf, sizeof(buf), "%d", maxnumber);
1007
1008         ret = this->name() + "." + route_group()->name () + "." + buf;
1009
1010         return ret;
1011 }
1012
1013 void
1014 RouteTimeAxisView::use_copy_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1015 {
1016         string name;
1017
1018         boost::shared_ptr<Track> tr = track ();
1019         if (!tr || tr->destructive()) {
1020                 return;
1021         }
1022
1023         boost::shared_ptr<const Playlist> pl = tr->playlist();
1024         if (!pl) {
1025                 return;
1026         }
1027
1028         name = pl->name();
1029
1030         if (route_group() && route_group()->is_active()) {
1031                 name = resolve_new_group_playlist_name(name, playlists_before_op);
1032         }
1033
1034         while (_session->playlists->by_name(name)) {
1035                 name = Playlist::bump_name (name, *_session);
1036         }
1037
1038         // TODO: The prompter "new" button should be de-activated if the user
1039         // specifies a playlist name which already exists in the session.
1040
1041         if (prompt) {
1042
1043                 ArdourPrompter prompter (true);
1044
1045                 prompter.set_title (_("New Copy Playlist"));
1046                 prompter.set_prompt (_("Name for new playlist:"));
1047                 prompter.set_initial_text (name);
1048                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1049                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1050                 prompter.show_all ();
1051
1052                 switch (prompter.run ()) {
1053                 case Gtk::RESPONSE_ACCEPT:
1054                         prompter.get_result (name);
1055                         break;
1056
1057                 default:
1058                         return;
1059                 }
1060         }
1061
1062         if (name.length()) {
1063                 tr->use_copy_playlist ();
1064                 tr->playlist()->set_name (name);
1065         }
1066 }
1067
1068 void
1069 RouteTimeAxisView::use_new_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1070 {
1071         string name;
1072
1073         boost::shared_ptr<Track> tr = track ();
1074         if (!tr || tr->destructive()) {
1075                 return;
1076         }
1077
1078         boost::shared_ptr<const Playlist> pl = tr->playlist();
1079         if (!pl) {
1080                 return;
1081         }
1082
1083         name = pl->name();
1084
1085         if (route_group() && route_group()->is_active()) {
1086                 name = resolve_new_group_playlist_name(name,playlists_before_op);
1087         }
1088
1089         while (_session->playlists->by_name(name)) {
1090                 name = Playlist::bump_name (name, *_session);
1091         }
1092
1093
1094         if (prompt) {
1095
1096                 ArdourPrompter prompter (true);
1097
1098                 prompter.set_title (_("New Playlist"));
1099                 prompter.set_prompt (_("Name for new playlist:"));
1100                 prompter.set_initial_text (name);
1101                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1102                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1103
1104                 switch (prompter.run ()) {
1105                 case Gtk::RESPONSE_ACCEPT:
1106                         prompter.get_result (name);
1107                         break;
1108
1109                 default:
1110                         return;
1111                 }
1112         }
1113
1114         if (name.length()) {
1115                 tr->use_new_playlist ();
1116                 tr->playlist()->set_name (name);
1117         }
1118 }
1119
1120 void
1121 RouteTimeAxisView::clear_playlist ()
1122 {
1123         boost::shared_ptr<Track> tr = track ();
1124         if (!tr || tr->destructive()) {
1125                 return;
1126         }
1127
1128         boost::shared_ptr<Playlist> pl = tr->playlist();
1129         if (!pl) {
1130                 return;
1131         }
1132
1133         _editor.clear_playlist (pl);
1134 }
1135
1136 void
1137 RouteTimeAxisView::speed_changed ()
1138 {
1139         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&RouteTimeAxisView::reset_samples_per_unit, this));
1140 }
1141
1142 void
1143 RouteTimeAxisView::update_diskstream_display ()
1144 {
1145         if (!track()) {
1146                 return;
1147         }
1148
1149         map_frozen ();
1150 }
1151
1152 void
1153 RouteTimeAxisView::selection_click (GdkEventButton* ev)
1154 {
1155         if (Keyboard::modifier_state_equals (ev->state, (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier))) {
1156
1157                 /* special case: select/deselect all tracks */
1158                 if (_editor.get_selection().selected (this)) {
1159                         _editor.get_selection().clear_tracks ();
1160                 } else {
1161                         _editor.select_all_tracks ();
1162                 }
1163
1164                 return;
1165         }
1166
1167         switch (ArdourKeyboard::selection_type (ev->state)) {
1168         case Selection::Toggle:
1169                 _editor.get_selection().toggle (this);
1170                 break;
1171
1172         case Selection::Set:
1173                 _editor.get_selection().set (this);
1174                 break;
1175
1176         case Selection::Extend:
1177                 _editor.extend_selection_to_track (*this);
1178                 break;
1179
1180         case Selection::Add:
1181                 _editor.get_selection().add (this);
1182                 break;
1183         }
1184 }
1185
1186 void
1187 RouteTimeAxisView::set_selected_points (PointSelection& points)
1188 {
1189         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1190                 (*i)->set_selected_points (points);
1191         }
1192 }
1193
1194 void
1195 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
1196 {
1197         if (_view) {
1198                 _view->set_selected_regionviews (regions);
1199         }
1200 }
1201
1202 /** Add the selectable things that we have to a list.
1203  * @param results List to add things to.
1204  */
1205 void
1206 RouteTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results)
1207 {
1208         double speed = 1.0;
1209
1210         if (track() != 0) {
1211                 speed = track()->speed();
1212         }
1213
1214         framepos_t const start_adjusted = session_frame_to_track_frame(start, speed);
1215         framepos_t const end_adjusted   = session_frame_to_track_frame(end, speed);
1216
1217         if ((_view && ((top < 0.0 && bot < 0.0))) || touched (top, bot)) {
1218                 _view->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1219         }
1220
1221         /* pick up visible automation tracks */
1222
1223         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1224                 if (!(*i)->hidden()) {
1225                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1226                 }
1227         }
1228 }
1229
1230 void
1231 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1232 {
1233         if (_view) {
1234                 _view->get_inverted_selectables (sel, results);
1235         }
1236
1237         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1238                 if (!(*i)->hidden()) {
1239                         (*i)->get_inverted_selectables (sel, results);
1240                 }
1241         }
1242
1243         return;
1244 }
1245
1246 RouteGroup*
1247 RouteTimeAxisView::route_group () const
1248 {
1249         return _route->route_group();
1250 }
1251
1252 string
1253 RouteTimeAxisView::name() const
1254 {
1255         return _route->name();
1256 }
1257
1258 boost::shared_ptr<Playlist>
1259 RouteTimeAxisView::playlist () const
1260 {
1261         boost::shared_ptr<Track> tr;
1262
1263         if ((tr = track()) != 0) {
1264                 return tr->playlist();
1265         } else {
1266                 return boost::shared_ptr<Playlist> ();
1267         }
1268 }
1269
1270 void
1271 RouteTimeAxisView::name_entry_changed ()
1272 {
1273         string x;
1274
1275         x = name_entry.get_text ();
1276
1277         if (x == _route->name()) {
1278                 return;
1279         }
1280
1281         strip_whitespace_edges(x);
1282
1283         if (x.length() == 0) {
1284                 name_entry.set_text (_route->name());
1285                 return;
1286         }
1287
1288         if (!_session->route_name_unique (x)) {
1289                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1290                 name_entry.set_text (_route->name());
1291         } else if (_session->route_name_internal (x)) {
1292                 ARDOUR_UI::instance()->popup_error (string_compose (_("You cannot create a track with that name as it is reserved for %1"),
1293                                                                     PROGRAM_NAME));
1294                 name_entry.set_text (_route->name());
1295         } else {
1296                 _route->set_name (x);
1297         }
1298 }
1299
1300 boost::shared_ptr<Region>
1301 RouteTimeAxisView::find_next_region (framepos_t pos, RegionPoint point, int32_t dir)
1302 {
1303         boost::shared_ptr<Playlist> pl = playlist ();
1304
1305         if (pl) {
1306                 return pl->find_next_region (pos, point, dir);
1307         }
1308
1309         return boost::shared_ptr<Region> ();
1310 }
1311
1312 framepos_t
1313 RouteTimeAxisView::find_next_region_boundary (framepos_t pos, int32_t dir)
1314 {
1315         boost::shared_ptr<Playlist> pl = playlist ();
1316
1317         if (pl) {
1318                 return pl->find_next_region_boundary (pos, dir);
1319         }
1320
1321         return -1;
1322 }
1323
1324 void
1325 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1326 {
1327         boost::shared_ptr<Playlist> what_we_got;
1328         boost::shared_ptr<Track> tr = track ();
1329         boost::shared_ptr<Playlist> playlist;
1330
1331         if (tr == 0) {
1332                 /* route is a bus, not a track */
1333                 return;
1334         }
1335
1336         playlist = tr->playlist();
1337
1338         TimeSelection time (selection.time);
1339         float const speed = tr->speed();
1340         if (speed != 1.0f) {
1341                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1342                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1343                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1344                 }
1345         }
1346
1347         playlist->clear_changes ();
1348         playlist->clear_owned_changes ();
1349
1350         switch (op) {
1351         case Cut:
1352                 if ((what_we_got = playlist->cut (time)) != 0) {
1353                         _editor.get_cut_buffer().add (what_we_got);
1354
1355                         vector<Command*> cmds;
1356                         playlist->rdiff (cmds);
1357                         _session->add_commands (cmds);
1358                         
1359                         _session->add_command (new StatefulDiffCommand (playlist));
1360                 }
1361                 break;
1362         case Copy:
1363                 if ((what_we_got = playlist->copy (time)) != 0) {
1364                         _editor.get_cut_buffer().add (what_we_got);
1365                 }
1366                 break;
1367
1368         case Clear:
1369                 if ((what_we_got = playlist->cut (time)) != 0) {
1370
1371                         vector<Command*> cmds;
1372                         playlist->rdiff (cmds);
1373                         _session->add_commands (cmds);
1374                         _session->add_command (new StatefulDiffCommand (playlist));
1375                         what_we_got->release ();
1376                 }
1377                 break;
1378         }
1379 }
1380
1381 bool
1382 RouteTimeAxisView::paste (framepos_t pos, float times, Selection& selection, size_t nth)
1383 {
1384         if (!is_track()) {
1385                 return false;
1386         }
1387
1388         boost::shared_ptr<Playlist> pl = playlist ();
1389         PlaylistSelection::iterator p;
1390
1391         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth) {}
1392
1393         if (p == selection.playlists.end()) {
1394                 return false;
1395         }
1396
1397         if (track()->speed() != 1.0f) {
1398                 pos = session_frame_to_track_frame (pos, track()->speed());
1399         }
1400
1401         pl->clear_changes ();
1402         pl->paste (*p, pos, times);
1403         _session->add_command (new StatefulDiffCommand (pl));
1404
1405         return true;
1406 }
1407
1408
1409 struct PlaylistSorter {
1410     bool operator() (boost::shared_ptr<Playlist> a, boost::shared_ptr<Playlist> b) const {
1411             return a->sort_id() < b->sort_id();
1412     }
1413 };
1414
1415 void
1416 RouteTimeAxisView::build_playlist_menu ()
1417 {
1418         using namespace Menu_Helpers;
1419
1420         if (!is_track()) {
1421                 return;
1422         }
1423
1424         delete playlist_action_menu;
1425         playlist_action_menu = new Menu;
1426         playlist_action_menu->set_name ("ArdourContextMenu");
1427
1428         MenuList& playlist_items = playlist_action_menu->items();
1429         playlist_action_menu->set_name ("ArdourContextMenu");
1430         playlist_items.clear();
1431
1432         delete playlist_menu;
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,
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,
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 }