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