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