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