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