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