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