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