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