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