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