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