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