Don't add a plugin menu entry for the Amp processor. Desensitize the plugin menu...
[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 (bind (mem_fun (*this, &RouteTimeAxisView::add_processor_to_subplugin_menu), _route));
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         automation_items.back().set_sensitive (!subplugin_menu.items().empty());
473         
474         map<Evoral::Parameter, RouteAutomationNode*>::iterator i;
475         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
476
477                 automation_items.push_back (SeparatorElem());
478
479                 delete i->second->menu_item;
480
481                 automation_items.push_back(CheckMenuElem (_route->describe_parameter(i->second->param), 
482                                 bind (mem_fun(*this, &RouteTimeAxisView::toggle_automation_track), i->second->param)));
483
484                 i->second->menu_item = static_cast<Gtk::CheckMenuItem*>(&automation_items.back());
485
486                 i->second->menu_item->set_active(show_automation(i->second->param));
487                 //i->second->menu_item->set_active(false);
488         }
489 }
490
491 void
492 RouteTimeAxisView::build_display_menu ()
493 {
494         using namespace Menu_Helpers;
495
496         /* get the size menu ready */
497
498         build_size_menu ();
499
500         /* prepare it */
501
502         TimeAxisView::build_display_menu ();
503
504         /* now fill it with our stuff */
505
506         MenuList& items = display_menu->items();
507         display_menu->set_name ("ArdourContextMenu");
508         
509         items.push_back (MenuElem (_("Height"), *size_menu));
510         items.push_back (MenuElem (_("Color"), mem_fun(*this, &RouteTimeAxisView::select_track_color)));
511
512         items.push_back (SeparatorElem());
513
514         if (!Profile->get_sae()) {
515                 build_remote_control_menu ();
516                 items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
517                 /* rebuild this every time */
518                 build_automation_action_menu ();
519                 items.push_back (MenuElem (_("Automation"), *automation_action_menu));
520                 items.push_back (SeparatorElem());
521         }
522
523         // Hook for derived classes to add type specific stuff
524         append_extra_display_menu_items ();
525         items.push_back (SeparatorElem());
526         
527         if (is_track()) {
528
529                 Menu *layers_menu = manage(new Menu);
530                 MenuList &layers_items = layers_menu->items();
531                 layers_menu->set_name("ArdourContextMenu");
532
533                 RadioMenuItem::Group layers_group;
534
535                 layers_items.push_back(RadioMenuElem (layers_group, _("Overlaid"),
536                                 bind (mem_fun (*this, &RouteTimeAxisView::set_layer_display), Overlaid)));
537                 layers_items.push_back(RadioMenuElem (layers_group, _("Stacked"),
538                                 bind (mem_fun (*this, &RouteTimeAxisView::set_layer_display), Stacked)));
539
540                 items.push_back (MenuElem (_("Layers"), *layers_menu));
541
542                 Menu* alignment_menu = manage (new Menu);
543                 MenuList& alignment_items = alignment_menu->items();
544                 alignment_menu->set_name ("ArdourContextMenu");
545
546                 RadioMenuItem::Group align_group;
547
548                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"),
549                                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
550                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
551                 if (get_diskstream()->alignment_style() == ExistingMaterial)
552                         align_existing_item->set_active();
553
554                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"),
555                                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
556                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
557                 if (get_diskstream()->alignment_style() == CaptureTime)
558                         align_capture_item->set_active();
559
560                 if (!Profile->get_sae()) {
561                         items.push_back (MenuElem (_("Alignment"), *alignment_menu));
562                         get_diskstream()->AlignmentStyleChanged.connect (
563                                         mem_fun(*this, &RouteTimeAxisView::align_style_changed));
564                         
565                         RadioMenuItem::Group mode_group;
566                         items.push_back (RadioMenuElem (mode_group, _("Normal mode"), bind (
567                                         mem_fun (*this, &RouteTimeAxisView::set_track_mode),
568                                         ARDOUR::Normal)));
569                         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
570
571                         items.push_back (RadioMenuElem (mode_group, _("Tape mode"), bind (
572                                         mem_fun (*this, &RouteTimeAxisView::set_track_mode),
573                                         ARDOUR::Destructive)));
574                         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
575
576                         items.push_back (RadioMenuElem (mode_group, _("No layering mode"),
577                                                         bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::NonLayered)));
578                         non_layered_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
579  
580
581                         
582                         switch (track()->mode()) {
583                         case ARDOUR::Destructive:
584                                 destructive_track_mode_item->set_active ();
585                                 break;
586                         case ARDOUR::Normal:
587                                 normal_track_mode_item->set_active ();
588                                 break;
589                         case ARDOUR::NonLayered:
590                                 non_layered_track_mode_item->set_active ();
591                                 break;
592                         }
593                 }
594
595                 get_diskstream()->AlignmentStyleChanged.connect (
596                                 mem_fun(*this, &RouteTimeAxisView::align_style_changed));
597
598                 mode_menu = build_mode_menu();
599                 if (mode_menu)
600                         items.push_back (MenuElem (_("Mode"), *mode_menu));
601                         
602                 color_mode_menu = build_color_mode_menu();
603                 if (color_mode_menu)
604                         items.push_back (MenuElem (_("Color Mode"), *color_mode_menu));
605                         
606                 items.push_back (SeparatorElem());
607         }
608
609         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
610         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
611         route_active_menu_item->set_active (_route->active());
612
613         items.push_back (SeparatorElem());
614         items.push_back (MenuElem (_("Hide"), mem_fun(*this, &RouteTimeAxisView::hide_click)));
615         if (!Profile->get_sae()) {
616                 items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
617         } else {
618                 items.push_front (SeparatorElem());
619                 items.push_front (MenuElem (_("Delete"), mem_fun(*this, &RouteUI::remove_this_route)));
620         }
621 }
622
623 static bool __reset_item (RadioMenuItem* item, RadioMenuItem* item_2)
624 {
625         item->set_active ();
626         item_2->set_active ();
627         return false;
628 }
629
630 void
631 RouteTimeAxisView::set_track_mode (TrackMode mode)
632 {
633         RadioMenuItem* item;
634         RadioMenuItem* other_item;
635         RadioMenuItem* other_item_2;
636
637         switch (mode) {
638         case ARDOUR::Normal:
639                 item = normal_track_mode_item;
640                 other_item = non_layered_track_mode_item;
641                 other_item_2 = destructive_track_mode_item;
642                 break;
643         case ARDOUR::NonLayered:
644                 item = non_layered_track_mode_item;
645                 other_item = normal_track_mode_item;
646                 other_item_2 = destructive_track_mode_item;
647                 break;
648         case ARDOUR::Destructive:
649                 item = destructive_track_mode_item;
650                 other_item = normal_track_mode_item;
651                 other_item_2 = non_layered_track_mode_item;
652                 break;
653         default:
654                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", mode) << endmsg;
655                 /*NOTREACHED*/
656                 return;
657         }
658         
659         if (item && other_item && other_item_2 && item->get_active() && track()->mode() != mode) {
660                 _set_track_mode (track().get(), mode, other_item, other_item_2);
661         }
662 }
663
664 void
665 RouteTimeAxisView::_set_track_mode (Track* track, TrackMode mode, RadioMenuItem* reset_item, RadioMenuItem* reset_item_2)
666 {
667         bool needs_bounce;
668
669         if (!track->can_use_mode (mode, needs_bounce)) {
670
671                 if (!needs_bounce) {
672                         /* cannot be done */
673                         Glib::signal_idle().connect (bind (sigc::ptr_fun (__reset_item), reset_item, reset_item_2));
674                         return;
675                 } else {
676                         cerr << "would bounce this one\n";
677                         return;
678                 }
679         }
680
681         track->set_mode (mode);
682
683         rec_enable_button->remove ();
684
685         switch (mode) {
686         case ARDOUR::NonLayered:
687         case ARDOUR::Normal:
688                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
689                 break;
690         case ARDOUR::Destructive:
691                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
692                 break;
693         }
694
695         rec_enable_button->show_all ();
696 }
697
698 void
699 RouteTimeAxisView::track_mode_changed ()
700 {
701         RadioMenuItem* item;
702         
703         switch (track()->mode()) {
704         case ARDOUR::Normal:
705                 item = normal_track_mode_item;
706                 break;
707         case ARDOUR::NonLayered:
708                 item = non_layered_track_mode_item;
709                 break;
710         case ARDOUR::Destructive:
711                 item = destructive_track_mode_item;
712                 break;
713         default:
714                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", track()->mode()) << endmsg;
715                 /*NOTREACHED*/
716                 return;
717         }
718
719         item->set_active ();
720 }
721
722 void
723 RouteTimeAxisView::show_timestretch (nframes_t start, nframes_t end)
724 {
725         double x1;
726         double x2;
727         double y2;
728         
729         TimeAxisView::show_timestretch (start, end);
730
731         hide_timestretch ();
732
733 #if 0   
734         if (ts.empty()) {
735                 return;
736         }
737
738
739         /* check that the time selection was made in our route, or our route group.
740            remember that route_group() == 0 implies the route is *not* in a edit group.
741         */
742
743         if (!(ts.track == this || (ts.group != 0 && ts.group == _route->route_group()))) {
744                 /* this doesn't apply to us */
745                 return;
746         }
747
748         /* ignore it if our edit group is not active */
749         
750         if ((ts.track != this) && _route->route_group() && !_route->route_group()->is_active()) {
751                 return;
752         }
753 #endif
754
755         if (timestretch_rect == 0) {
756                 timestretch_rect = new SimpleRect (*canvas_display ());
757                 timestretch_rect->property_x1() =  0.0;
758                 timestretch_rect->property_y1() =  0.0;
759                 timestretch_rect->property_x2() =  0.0;
760                 timestretch_rect->property_y2() =  0.0;
761                 timestretch_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
762                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
763         }
764
765         timestretch_rect->show ();
766         timestretch_rect->raise_to_top ();
767
768         x1 = start / _editor.get_current_zoom();
769         x2 = (end - 1) / _editor.get_current_zoom();
770         y2 = current_height() - 2;
771         
772         timestretch_rect->property_x1() = x1;
773         timestretch_rect->property_y1() = 1.0;
774         timestretch_rect->property_x2() = x2;
775         timestretch_rect->property_y2() = y2;
776 }
777
778 void
779 RouteTimeAxisView::hide_timestretch ()
780 {
781         TimeAxisView::hide_timestretch ();
782
783         if (timestretch_rect) {
784                 timestretch_rect->hide ();
785         }
786 }
787
788 void
789 RouteTimeAxisView::show_selection (TimeSelection& ts)
790 {
791
792 #if 0
793         /* ignore it if our edit group is not active or if the selection was started
794            in some other track or route group (remember that route_group() == 0 means
795            that the track is not in an route group).
796         */
797
798         if (((ts.track != this && !is_child (ts.track)) && _route->route_group() && !_route->route_group()->is_active()) ||
799             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route->route_group())))) {
800                 hide_selection ();
801                 return;
802         }
803 #endif
804
805         TimeAxisView::show_selection (ts);
806 }
807
808 void
809 RouteTimeAxisView::set_height (uint32_t h)
810 {
811         int gmlen = h - 5;
812         bool height_changed = (height == 0) || (h != height);
813         gm.get_level_meter().setup_meters (gmlen);
814
815         TimeAxisView::set_height (h);
816
817         ensure_xml_node ();
818
819         if (_view) {
820                 _view->set_height ((double) current_height());
821         }
822
823         char buf[32];
824         snprintf (buf, sizeof (buf), "%u", height);
825         xml_node->add_property ("height", buf);
826
827         if (height >= hNormal) {
828                 reset_meter();
829                 show_name_entry ();
830                 hide_name_label ();
831
832                 gm.get_gain_slider().show();
833                 mute_button->show();
834                 if (!_route || _route->is_master()) {
835                         solo_button->hide();
836                 } else {
837                         solo_button->show();
838                 }
839                 if (rec_enable_button)
840                         rec_enable_button->show();
841
842                 route_group_button.show();
843                 hide_button.show();
844                 visual_button.show();
845                 size_button.show();
846                 automation_button.show();
847                 
848                 if (is_track() && track()->mode() == ARDOUR::Normal) {
849                         playlist_button.show();
850                 }
851
852         } else if (height >= hSmaller) {
853
854                 reset_meter();
855                 show_name_entry ();
856                 hide_name_label ();
857
858                 gm.get_gain_slider().hide();
859                 mute_button->show();
860                 if (!_route || _route->is_master()) {
861                         solo_button->hide();
862                 } else {
863                         solo_button->show();
864                 }
865                 if (rec_enable_button)
866                         rec_enable_button->show();
867
868                 route_group_button.hide ();
869                 hide_button.hide ();
870                 visual_button.hide ();
871                 size_button.hide ();
872                 automation_button.hide ();
873                 
874                 if (is_track() && track()->mode() == ARDOUR::Normal) {
875                         playlist_button.hide ();
876                 }
877
878         } else {
879
880
881                 /* don't allow name_entry to be hidden while
882                    it has focus, otherwise the GUI becomes unusable.
883                 */
884
885                 if (name_entry.has_focus()) {
886                         if (name_entry.get_text() != _route->name()) {
887                                 name_entry_changed ();
888                         }
889                         controls_ebox.grab_focus ();
890                 }
891
892                 hide_name_entry ();
893                 show_name_label ();
894                 
895                 gm.get_gain_slider().hide();
896                 mute_button->hide();
897                 solo_button->hide();
898                 if (rec_enable_button)
899                         rec_enable_button->hide();
900
901                 route_group_button.hide ();
902                 hide_button.hide ();
903                 visual_button.hide ();
904                 size_button.hide ();
905                 automation_button.hide ();
906                 playlist_button.hide ();
907                 name_label.set_text (_route->name());
908         }
909
910         if (height_changed) {
911                 /* only emit the signal if the height really changed */
912                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
913         }
914 }
915
916 void
917 RouteTimeAxisView::select_track_color ()
918 {
919         if (RouteUI::choose_color ()) {
920
921                 if (_view) {
922                         _view->apply_color (_color, StreamView::RegionColor);
923                 }
924         }
925 }
926
927 void
928 RouteTimeAxisView::reset_samples_per_unit ()
929 {
930         set_samples_per_unit (_editor.get_current_zoom());
931 }
932
933 void
934 RouteTimeAxisView::set_samples_per_unit (double spu)
935 {
936         double speed = 1.0;
937
938         if (get_diskstream() != 0) {
939                 speed = get_diskstream()->speed();
940         }
941         
942         if (_view) {
943                 _view->set_samples_per_unit (spu * speed);
944         }
945
946         TimeAxisView::set_samples_per_unit (spu * speed);
947 }
948
949 void
950 RouteTimeAxisView::align_style_changed ()
951 {
952         switch (get_diskstream()->alignment_style()) {
953         case ExistingMaterial:
954                 if (!align_existing_item->get_active()) {
955                         align_existing_item->set_active();
956                 }
957                 break;
958         case CaptureTime:
959                 if (!align_capture_item->get_active()) {
960                         align_capture_item->set_active();
961                 }
962                 break;
963         }
964 }
965
966 void
967 RouteTimeAxisView::set_align_style (AlignStyle style)
968 {
969         RadioMenuItem* item;
970
971         switch (style) {
972         case ExistingMaterial:
973                 item = align_existing_item;
974                 break;
975         case CaptureTime:
976                 item = align_capture_item;
977                 break;
978         default:
979                 fatal << string_compose (_("programming error: %1 %2"), "illegal align style in RouteTimeAxisView::set_align_style", style) << endmsg;
980                 /*NOTREACHED*/
981                 return;
982         }
983
984         if (item->get_active()) {
985                 get_diskstream()->set_align_style (style);
986         }
987 }
988
989 void
990 RouteTimeAxisView::rename_current_playlist ()
991 {
992         ArdourPrompter prompter (true);
993         string name;
994
995         boost::shared_ptr<Diskstream> ds = get_diskstream();
996         if (!ds || ds->destructive())
997                 return;
998
999         boost::shared_ptr<Playlist> pl = ds->playlist();
1000         if (!pl)
1001                 return;
1002
1003         prompter.set_prompt (_("Name for playlist"));
1004         prompter.set_initial_text (pl->name());
1005         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1006         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1007
1008         switch (prompter.run ()) {
1009         case Gtk::RESPONSE_ACCEPT:
1010                 prompter.get_result (name);
1011                 if (name.length()) {
1012                         pl->set_name (name);
1013                 }
1014                 break;
1015
1016         default:
1017                 break;
1018         }
1019 }
1020
1021 std::string 
1022 RouteTimeAxisView::resolve_new_group_playlist_name(std::string &basename, vector<boost::shared_ptr<Playlist> > const & playlists)
1023 {
1024         std::string ret (basename);
1025
1026         std::string const group_string = "." + route_group()->name() + ".";
1027
1028         // iterate through all playlists
1029         int maxnumber = 0;
1030         for (vector<boost::shared_ptr<Playlist> >::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
1031                 std::string tmp = (*i)->name();
1032
1033                 std::string::size_type idx = tmp.find(group_string);                    
1034                 // find those which belong to this group
1035                 if (idx != string::npos) {
1036                         tmp = tmp.substr(idx + group_string.length());
1037
1038                         // and find the largest current number
1039                         int x = atoi(tmp.c_str());
1040                         if (x > maxnumber) {
1041                                 maxnumber = x;
1042                         }
1043                 }
1044         }
1045
1046         maxnumber++;
1047
1048         char buf[32];
1049         snprintf (buf, sizeof(buf), "%d", maxnumber);
1050                
1051         ret = this->name() + "." + route_group()->name () + "." + buf;
1052
1053         return ret;
1054 }
1055
1056 void
1057 RouteTimeAxisView::use_copy_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1058 {
1059         string name;
1060         
1061         boost::shared_ptr<Diskstream> ds = get_diskstream();
1062         if (!ds || ds->destructive())
1063                 return;
1064
1065         boost::shared_ptr<const Playlist> pl = ds->playlist();
1066         if (!pl)
1067                 return;
1068
1069         name = pl->name();
1070         
1071         if (route_group() && route_group()->is_active()) {
1072                 name = resolve_new_group_playlist_name(name, playlists_before_op);
1073         }
1074
1075         while (_session.playlist_by_name(name)) {
1076                 name = Playlist::bump_name (name, _session);
1077         }
1078
1079         // TODO: The prompter "new" button should be de-activated if the user
1080         // specifies a playlist name which already exists in the session.
1081
1082         if (prompt) {
1083
1084                 ArdourPrompter prompter (true);
1085                 
1086                 prompter.set_prompt (_("Name for Playlist"));
1087                 prompter.set_initial_text (name);
1088                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1089                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1090                 prompter.show_all ();
1091                 
1092                 switch (prompter.run ()) {
1093                 case Gtk::RESPONSE_ACCEPT:
1094                         prompter.get_result (name);
1095                         break;
1096                         
1097                 default:
1098                         return;
1099                 }
1100         }
1101
1102         if (name.length()) {
1103                 ds->use_copy_playlist ();
1104                 ds->playlist()->set_name (name);
1105         }
1106 }
1107
1108 void
1109 RouteTimeAxisView::use_new_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1110 {
1111         string name;
1112         
1113         boost::shared_ptr<Diskstream> ds = get_diskstream();
1114         if (!ds || ds->destructive())
1115                 return;
1116
1117         boost::shared_ptr<const Playlist> pl = ds->playlist();
1118         if (!pl)
1119                 return;
1120
1121         name = pl->name();
1122         
1123         if (route_group() && route_group()->is_active()) {
1124                 name = resolve_new_group_playlist_name(name,playlists_before_op);
1125         }
1126
1127         while (_session.playlist_by_name(name)) {
1128                 name = Playlist::bump_name (name, _session);
1129         }
1130
1131
1132         if (prompt) {
1133                 
1134                 ArdourPrompter prompter (true);
1135                 
1136                 prompter.set_prompt (_("Name for Playlist"));
1137                 prompter.set_initial_text (name);
1138                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1139                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1140
1141                 switch (prompter.run ()) {
1142                 case Gtk::RESPONSE_ACCEPT:
1143                         prompter.get_result (name);
1144                         break;
1145                         
1146                 default:
1147                         return;
1148                 }
1149         }
1150
1151         if (name.length()) {
1152                 ds->use_new_playlist ();
1153                 ds->playlist()->set_name (name);
1154         }
1155 }
1156
1157 void
1158 RouteTimeAxisView::clear_playlist ()
1159 {
1160         boost::shared_ptr<Diskstream> ds = get_diskstream();
1161         if (!ds || ds->destructive())
1162                 return;
1163
1164         boost::shared_ptr<Playlist> pl = ds->playlist();
1165         if (!pl)
1166                 return;
1167
1168         _editor.clear_playlist (pl);
1169 }
1170
1171 void
1172 RouteTimeAxisView::speed_changed ()
1173 {
1174         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
1175 }
1176
1177 void
1178 RouteTimeAxisView::diskstream_changed ()
1179 {
1180         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::update_diskstream_display));
1181 }       
1182
1183 void
1184 RouteTimeAxisView::update_diskstream_display ()
1185 {
1186         if (!get_diskstream()) // bus
1187                 return;
1188
1189         set_playlist (get_diskstream()->playlist());
1190         map_frozen ();
1191 }       
1192
1193 void
1194 RouteTimeAxisView::selection_click (GdkEventButton* ev)
1195 {
1196         if (Keyboard::modifier_state_equals (ev->state, (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier))) {
1197
1198                 /* special case: select/deselect all tracks */
1199                 if (_editor.get_selection().selected (this)) {
1200                         _editor.get_selection().clear_tracks ();
1201                 } else {
1202                         _editor.select_all_tracks ();
1203                 }
1204
1205                 return;
1206         } 
1207
1208         PublicEditor::TrackViewList* tracks = _editor.get_valid_views (this, _route->route_group());
1209
1210         switch (Keyboard::selection_type (ev->state)) {
1211         case Selection::Toggle:
1212                 _editor.get_selection().toggle (*tracks);
1213                 break;
1214                 
1215         case Selection::Set:
1216                 _editor.get_selection().set (*tracks);
1217                 break;
1218
1219         case Selection::Extend:
1220                 if (tracks->size() > 1) {
1221                         /* add each one, do not "extend" */
1222                         _editor.get_selection().add (*tracks);
1223                 } else {
1224                         /* extend to the single track */
1225                         _editor.extend_selection_to_track (*tracks->front());
1226                 }
1227                 break;
1228
1229         case Selection::Add:
1230                 _editor.get_selection().add (*tracks);
1231                 break;
1232         }
1233
1234         delete tracks;
1235 }
1236
1237 void
1238 RouteTimeAxisView::set_selected_points (PointSelection& points)
1239 {
1240         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1241                 (*i)->set_selected_points (points);
1242         }
1243 }
1244
1245 void
1246 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
1247 {
1248         if (_view) {
1249                 _view->set_selected_regionviews (regions);
1250         }
1251 }
1252
1253 /** Add the selectable things that we have to a list.
1254  * @param results List to add things to.
1255  */
1256 void
1257 RouteTimeAxisView::get_selectables (nframes_t start, nframes_t end, double top, double bot, list<Selectable*>& results)
1258 {
1259         double speed = 1.0;
1260         
1261         if (get_diskstream() != 0) {
1262                 speed = get_diskstream()->speed();
1263         }
1264         
1265         nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
1266         nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
1267
1268         if ((_view && ((top < 0.0 && bot < 0.0))) || touched (top, bot)) {
1269                 _view->get_selectables (start_adjusted, end_adjusted, results);
1270         }
1271
1272         /* pick up visible automation tracks */
1273         
1274         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1275                 if (!(*i)->hidden()) {
1276                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1277                 }
1278         }
1279 }
1280
1281 void
1282 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1283 {
1284         if (_view) {
1285                 _view->get_inverted_selectables (sel, results);
1286         }
1287
1288         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1289                 if (!(*i)->hidden()) {
1290                         (*i)->get_inverted_selectables (sel, results);
1291                 }
1292         }
1293
1294         return;
1295 }
1296
1297 bool
1298 RouteTimeAxisView::show_automation(Evoral::Parameter param)
1299 {
1300         return (_show_automation.find(param) != _show_automation.end());
1301 }
1302
1303 /** Retuns 0 if track for \a param doesn't exist.
1304  */
1305 RouteTimeAxisView::RouteAutomationNode*
1306 RouteTimeAxisView::automation_track (Evoral::Parameter param)
1307 {
1308         map<Evoral::Parameter, RouteAutomationNode*>::iterator i = _automation_tracks.find (param);
1309
1310         if (i != _automation_tracks.end()) {
1311                 return i->second;
1312         } else {
1313                 return 0;
1314         }
1315 }
1316
1317 /** Shorthand for GainAutomation, etc.
1318  */     
1319 RouteTimeAxisView::RouteAutomationNode*
1320 RouteTimeAxisView::automation_track (AutomationType type)
1321 {
1322         return automation_track (Evoral::Parameter(type));
1323 }
1324
1325 RouteGroup*
1326 RouteTimeAxisView::route_group () const
1327 {
1328         return _route->route_group();
1329 }
1330
1331 string
1332 RouteTimeAxisView::name() const
1333 {
1334         return _route->name();
1335 }
1336
1337 boost::shared_ptr<Playlist>
1338 RouteTimeAxisView::playlist () const 
1339 {
1340         boost::shared_ptr<Diskstream> ds;
1341
1342         if ((ds = get_diskstream()) != 0) {
1343                 return ds->playlist(); 
1344         } else {
1345                 return boost::shared_ptr<Playlist> ();
1346         }
1347 }
1348
1349 void
1350 RouteTimeAxisView::name_entry_changed ()
1351 {
1352         string x;
1353
1354         x = name_entry.get_text ();
1355         
1356         if (x == _route->name()) {
1357                 return;
1358         }
1359
1360         strip_whitespace_edges(x);
1361
1362         if (x.length() == 0) {
1363                 name_entry.set_text (_route->name());
1364                 return;
1365         }
1366
1367         if (!_session.route_name_unique (x)) {
1368                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1369                 name_entry.set_text (_route->name());
1370         } else if (_session.route_name_internal (x)) {
1371                 ARDOUR_UI::instance()->popup_error (_("You cannot create a track with that name as it is reserved for Ardour"));
1372                 name_entry.set_text (_route->name());
1373         } else {
1374                 _route->set_name (x);
1375         }
1376 }
1377
1378 void
1379 RouteTimeAxisView::visual_click ()
1380 {
1381         popup_display_menu (0);
1382 }
1383
1384 void
1385 RouteTimeAxisView::hide_click ()
1386 {
1387         // LAME fix for hide_button refresh fix
1388         hide_button.set_sensitive(false);
1389         
1390         _editor.hide_track_in_display (*this);
1391         
1392         hide_button.set_sensitive(true);
1393 }
1394
1395 boost::shared_ptr<Region>
1396 RouteTimeAxisView::find_next_region (nframes_t pos, RegionPoint point, int32_t dir)
1397 {
1398         boost::shared_ptr<Diskstream> stream;
1399         boost::shared_ptr<Playlist> playlist;
1400
1401         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1402                 return playlist->find_next_region (pos, point, dir);
1403         }
1404
1405         return boost::shared_ptr<Region> ();
1406 }
1407
1408 nframes64_t 
1409 RouteTimeAxisView::find_next_region_boundary (nframes64_t pos, int32_t dir)
1410 {
1411         boost::shared_ptr<Diskstream> stream;
1412         boost::shared_ptr<Playlist> playlist;
1413
1414         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1415                 return playlist->find_next_region_boundary (pos, dir);
1416         }
1417
1418         return -1;
1419 }
1420
1421 bool
1422 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1423 {
1424         boost::shared_ptr<Playlist> what_we_got;
1425         boost::shared_ptr<Diskstream> ds = get_diskstream();
1426         boost::shared_ptr<Playlist> playlist;
1427         bool ret = false;
1428
1429         if (ds == 0) {
1430                 /* route is a bus, not a track */
1431                 return false;
1432         }
1433
1434         playlist = ds->playlist();
1435
1436         TimeSelection time (selection.time);
1437         float speed = ds->speed();
1438         if (speed != 1.0f) {
1439                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1440                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1441                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1442                 }
1443         }
1444         
1445         XMLNode &before = playlist->get_state();
1446         switch (op) {
1447         case Cut:
1448                 if ((what_we_got = playlist->cut (time)) != 0) {
1449                         _editor.get_cut_buffer().add (what_we_got);
1450                         _session.add_command( new MementoCommand<Playlist>(*playlist.get(), &before, &playlist->get_state()));
1451                         ret = true;
1452                 }
1453                 break;
1454         case Copy:
1455                 if ((what_we_got = playlist->copy (time)) != 0) {
1456                         _editor.get_cut_buffer().add (what_we_got);
1457                 }
1458                 break;
1459
1460         case Clear:
1461                 if ((what_we_got = playlist->cut (time)) != 0) {
1462                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1463                         what_we_got->release ();
1464                         ret = true;
1465                 }
1466                 break;
1467         }
1468
1469         return ret;
1470 }
1471
1472 bool
1473 RouteTimeAxisView::paste (nframes_t pos, float times, Selection& selection, size_t nth)
1474 {
1475         if (!is_track()) {
1476                 return false;
1477         }
1478
1479         boost::shared_ptr<Playlist> playlist = get_diskstream()->playlist();
1480         PlaylistSelection::iterator p;
1481         
1482         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth) {}
1483
1484         if (p == selection.playlists.end()) {
1485                 return false;
1486         }
1487
1488         if (get_diskstream()->speed() != 1.0f)
1489                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1490         
1491         XMLNode &before = playlist->get_state();
1492         playlist->paste (*p, pos, times);
1493         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1494
1495         return true;
1496 }
1497
1498
1499 TimeAxisView::Children
1500 RouteTimeAxisView::get_child_list()
1501 {
1502         TimeAxisView::Children redirect_children;
1503         
1504         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1505                 if (!(*i)->hidden()) {
1506                         redirect_children.push_back(*i);
1507                 }
1508         }
1509         return redirect_children;
1510 }
1511
1512
1513 void
1514 RouteTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1515 {
1516         using namespace Menu_Helpers;
1517
1518         if (!menu || !is_track()) {
1519                 return;
1520         }
1521
1522         MenuList& playlist_items = menu->items();
1523         menu->set_name ("ArdourContextMenu");
1524         playlist_items.clear();
1525
1526         delete playlist_menu;
1527
1528         playlist_menu = new Menu;
1529         playlist_menu->set_name ("ArdourContextMenu");
1530
1531         vector<boost::shared_ptr<Playlist> > playlists;
1532         boost::shared_ptr<Diskstream> ds = get_diskstream();
1533         RadioMenuItem::Group playlist_group;
1534
1535         _session.get_playlists (playlists);
1536         
1537         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1538
1539                 if ((*i)->get_orig_diskstream_id() == ds->id()) {
1540                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist),
1541                                                                                                      boost::weak_ptr<Playlist> (*i))));
1542
1543                         if (ds->playlist()->id() == (*i)->id()) {
1544                                 static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1545                         }
1546                 } else if (ds->playlist()->id() == (*i)->id()) {
1547                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist), 
1548                                                                                                      boost::weak_ptr<Playlist>(*i))));
1549                         static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1550                         
1551                 }
1552         }
1553
1554         playlist_items.push_back (SeparatorElem());
1555         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1556         playlist_items.push_back (SeparatorElem());
1557
1558         if (!route_group() || !route_group()->is_active()) {
1559                 playlist_items.push_back (MenuElem (_("New"), bind(mem_fun(_editor, &PublicEditor::new_playlists), this)));
1560                 playlist_items.push_back (MenuElem (_("New Copy"), bind(mem_fun(_editor, &PublicEditor::copy_playlists), this)));
1561
1562         } else {
1563                 // Use a label which tells the user what is happening
1564                 playlist_items.push_back (MenuElem (_("New Take"), bind(mem_fun(_editor, &PublicEditor::new_playlists), this)));
1565                 playlist_items.push_back (MenuElem (_("Copy Take"), bind(mem_fun(_editor, &PublicEditor::copy_playlists), this)));
1566                 
1567         }
1568
1569         playlist_items.push_back (SeparatorElem());
1570         playlist_items.push_back (MenuElem (_("Clear Current"), bind(mem_fun(_editor, &PublicEditor::clear_playlists), this)));
1571         playlist_items.push_back (SeparatorElem());
1572
1573         playlist_items.push_back (MenuElem(_("Select from all ..."), mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1574 }
1575
1576 void
1577 RouteTimeAxisView::use_playlist (boost::weak_ptr<Playlist> wpl)
1578 {
1579         assert (is_track());
1580
1581         boost::shared_ptr<Playlist> pl (wpl.lock());
1582
1583         if (!pl) {
1584                 return;
1585         }
1586
1587         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
1588         
1589         if (apl) {
1590                 if (get_diskstream()->playlist() == apl) {
1591                         // radio button cotnrols mean this function is called for both the 
1592                         // old and new playlist
1593                         return;
1594                 }
1595                 get_diskstream()->use_playlist (apl);
1596
1597
1598                 if (route_group() && route_group()->is_active()) {
1599                         //PBD::stacktrace(cerr, 20);
1600                         std::string group_string = "."+route_group()->name()+".";
1601
1602                         std::string take_name = apl->name();
1603                         std::string::size_type idx = take_name.find(group_string);
1604
1605                         if (idx == std::string::npos)
1606                                 return;
1607
1608                         take_name = take_name.substr(idx + group_string.length()); // find the bit containing the take number / name
1609                         
1610                         for (list<Route*>::const_iterator i = route_group()->route_list().begin(); i != route_group()->route_list().end(); ++i) {
1611                                 if ( (*i) == this->route().get()) {
1612                                         continue;
1613                                 }
1614                                 
1615                                 std::string playlist_name = (*i)->name()+group_string+take_name;
1616
1617                                 Track *track = dynamic_cast<Track *>(*i);
1618                                 if (!track) {
1619                                         std::cerr << "route " << (*i)->name() << " is not a Track" << std::endl;
1620                                         continue;
1621                                 }
1622
1623                                 boost::shared_ptr<Playlist> ipl = session().playlist_by_name(playlist_name);
1624                                 if (!ipl) {
1625                                         // No playlist for this track for this take yet, make it
1626                                         track->diskstream()->use_new_playlist();
1627                                         track->diskstream()->playlist()->set_name(playlist_name);
1628                                 } else {
1629                                         track->diskstream()->use_playlist(ipl);
1630                                 }
1631                                 
1632                                 //(*i)->get_dis
1633                         }
1634                 }
1635         }
1636 }
1637
1638 void
1639 RouteTimeAxisView::show_playlist_selector ()
1640 {
1641         _editor.playlist_selector().show_for (this);
1642 }
1643
1644 void
1645 RouteTimeAxisView::map_frozen ()
1646 {
1647         if (!is_track()) {
1648                 return;
1649         }
1650
1651         ENSURE_GUI_THREAD (mem_fun(*this, &RouteTimeAxisView::map_frozen));
1652
1653         switch (track()->freeze_state()) {
1654         case Track::Frozen:
1655                 playlist_button.set_sensitive (false);
1656                 rec_enable_button->set_sensitive (false);
1657                 break;
1658         default:
1659                 playlist_button.set_sensitive (true);
1660                 rec_enable_button->set_sensitive (true);
1661                 break;
1662         }
1663 }
1664
1665 void
1666 RouteTimeAxisView::color_handler ()
1667 {
1668         //case cTimeStretchOutline:
1669         if (timestretch_rect) {
1670                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
1671         }
1672         //case cTimeStretchFill:
1673         if (timestretch_rect) {
1674                 timestretch_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
1675         }
1676
1677         reset_meter();
1678 }
1679
1680 void
1681 RouteTimeAxisView::toggle_automation_track (Evoral::Parameter param)
1682 {
1683         RouteAutomationNode* node = automation_track(param);
1684
1685         if (!node)
1686                 return;
1687
1688         bool showit = node->menu_item->get_active();
1689
1690         if (showit != node->track->marked_for_display()) {
1691                 if (showit) {
1692                         node->track->set_marked_for_display (true);
1693                         node->track->canvas_display()->show();
1694                         node->track->get_state_node()->add_property ("shown", X_("yes"));
1695                 } else {
1696                         node->track->set_marked_for_display (false);
1697                         node->track->hide ();
1698                         node->track->get_state_node()->add_property ("shown", X_("no"));
1699                 }
1700
1701                 /* now trigger a redisplay */
1702                 
1703                 if (!no_redraw) {
1704                          _route->gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
1705                 }
1706         }
1707 }
1708
1709 void
1710 RouteTimeAxisView::automation_track_hidden (Evoral::Parameter param)
1711 {
1712         RouteAutomationNode* ran = automation_track(param);
1713         if (!ran) {
1714                 return;
1715         }
1716         
1717         // if Evoral::Parameter::operator< doesn't obey strict weak ordering, we may crash here....
1718         _show_automation.erase(param);
1719         ran->track->get_state_node()->add_property (X_("shown"), X_("no"));
1720
1721         if (ran->menu_item && !_hidden) {
1722                 ran->menu_item->set_active (false);
1723         }
1724
1725          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1726 }
1727
1728
1729 void
1730 RouteTimeAxisView::show_all_automation ()
1731 {
1732         no_redraw = true;
1733         
1734         /* Show our automation */
1735
1736         map<Evoral::Parameter, RouteAutomationNode*>::iterator i;
1737         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1738                 i->second->track->set_marked_for_display (true);
1739                 i->second->track->canvas_display()->show();
1740                 i->second->track->get_state_node()->add_property ("shown", X_("yes"));
1741                 i->second->menu_item->set_active(true);
1742         }
1743
1744
1745         /* Show processor automation */
1746
1747         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1748                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1749                         if ((*ii)->view == 0) {
1750                                 add_processor_automation_curve ((*i)->processor, (*ii)->what);
1751                         } 
1752
1753                         (*ii)->menu_item->set_active (true);
1754                 }
1755         }
1756
1757         no_redraw = false;
1758
1759         /* Redraw */
1760
1761          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1762 }
1763
1764 void
1765 RouteTimeAxisView::show_existing_automation ()
1766 {
1767         no_redraw = true;
1768         
1769         /* Show our automation */
1770
1771         map<Evoral::Parameter, RouteAutomationNode*>::iterator i;
1772         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1773                 if (i->second->track->line() && i->second->track->line()->npoints() > 0) {
1774                         i->second->track->set_marked_for_display (true);
1775                         i->second->track->canvas_display()->show();
1776                         i->second->track->get_state_node()->add_property ("shown", X_("yes"));
1777                         i->second->menu_item->set_active(true);
1778                 }
1779         }
1780
1781
1782         /* Show processor automation */
1783
1784         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1785                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1786                         if ((*ii)->view != 0 && (*i)->processor->data().control((*ii)->what)->list()->size() > 0) {
1787                                 (*ii)->menu_item->set_active (true);
1788                         }
1789                 }
1790         }
1791
1792         no_redraw = false;
1793         
1794         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1795 }
1796
1797 void
1798 RouteTimeAxisView::hide_all_automation ()
1799 {
1800         no_redraw = true;
1801
1802         /* Hide our automation */
1803
1804         for (map<Evoral::Parameter, RouteAutomationNode*>::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1805                 i->second->track->set_marked_for_display (false);
1806                 i->second->track->hide ();
1807                 i->second->track->get_state_node()->add_property ("shown", X_("no"));
1808                 i->second->menu_item->set_active (false);
1809         }
1810
1811         /* Hide processor automation */
1812
1813         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1814                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1815                         (*ii)->menu_item->set_active (false);
1816                 }
1817         }
1818
1819         _show_automation.clear();
1820
1821         no_redraw = false;
1822          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1823 }
1824
1825
1826 void
1827 RouteTimeAxisView::region_view_added (RegionView* rv)
1828 {
1829         /* XXX need to find out if automation children have automationstreamviews. If yes, no ghosts */
1830         if(is_audio_track()) {
1831                 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1832                         boost::shared_ptr<AutomationTimeAxisView> atv;
1833                         
1834                         if ((atv = boost::dynamic_pointer_cast<AutomationTimeAxisView> (*i)) != 0) {
1835                                 atv->add_ghost(rv);
1836                         }
1837                 }
1838         }
1839
1840         for (UnderlayMirrorList::iterator i = _underlay_mirrors.begin(); i != _underlay_mirrors.end(); ++i) {
1841                 (*i)->add_ghost(rv);
1842         }
1843 }
1844
1845 RouteTimeAxisView::ProcessorAutomationInfo::~ProcessorAutomationInfo ()
1846 {
1847         for (vector<ProcessorAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1848                 delete *i;
1849         }
1850 }
1851
1852
1853 RouteTimeAxisView::ProcessorAutomationNode::~ProcessorAutomationNode ()
1854 {
1855         parent.remove_processor_automation_node (this);
1856 }
1857
1858 void
1859 RouteTimeAxisView::remove_processor_automation_node (ProcessorAutomationNode* pan)
1860 {
1861         if (pan->view) {
1862                 remove_child (pan->view);
1863         }
1864 }
1865
1866 RouteTimeAxisView::ProcessorAutomationNode*
1867 RouteTimeAxisView::find_processor_automation_node (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
1868 {
1869         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1870
1871                 if ((*i)->processor == processor) {
1872
1873                         for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1874                                 if ((*ii)->what == what) {
1875                                         return *ii;
1876                                 }
1877                         }
1878                 }
1879         }
1880
1881         return 0;
1882 }
1883
1884 static string 
1885 legalize_for_xml_node (string str)
1886 {
1887         string::size_type pos;
1888         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=:";
1889         string legal;
1890
1891         legal = str;
1892         pos = 0;
1893
1894         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1895                 legal.replace (pos, 1, "_");
1896                 pos += 1;
1897         }
1898
1899         return legal;
1900 }
1901
1902
1903 void
1904 RouteTimeAxisView::add_processor_automation_curve (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
1905 {
1906         string name;
1907         ProcessorAutomationNode* pan;
1908
1909         if ((pan = find_processor_automation_node (processor, what)) == 0) {
1910                 fatal << _("programming error: ")
1911                       << string_compose (X_("processor automation curve for %1:%2 not registered with track!"),
1912                                   processor->name(), what)
1913                       << endmsg;
1914                 /*NOTREACHED*/
1915                 return;
1916         }
1917
1918         if (pan->view) {
1919                 return;
1920         }
1921
1922         name = processor->describe_parameter (what);
1923
1924         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1925
1926         /* FIXME: ew */
1927
1928         char state_name[256];
1929         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (processor->name()).c_str(), what.id());
1930
1931         boost::shared_ptr<AutomationControl> control
1932                         = boost::dynamic_pointer_cast<AutomationControl>(processor->data().control(what, true));
1933
1934         pan->view = boost::shared_ptr<AutomationTimeAxisView>(
1935                         new AutomationTimeAxisView (_session, _route, processor, control,
1936                                 _editor, *this, false, parent_canvas, name, state_name));
1937
1938         pan->view->Hiding.connect (bind (mem_fun(*this, &RouteTimeAxisView::processor_automation_track_hidden), pan, processor));
1939
1940         if (!pan->view->marked_for_display()) {
1941                 pan->view->hide ();
1942         } else {
1943                 pan->menu_item->set_active (true);
1944         }
1945
1946         add_child (pan->view);
1947
1948         if (_view) {
1949                 _view->foreach_regionview (mem_fun(*pan->view.get(), &TimeAxisView::add_ghost));
1950         }
1951
1952         processor->mark_automation_visible (what, true);
1953 }
1954
1955 void
1956 RouteTimeAxisView::processor_automation_track_hidden (RouteTimeAxisView::ProcessorAutomationNode* pan, boost::shared_ptr<Processor> i)
1957 {
1958         if (!_hidden) {
1959                 pan->menu_item->set_active (false);
1960         }
1961
1962         i->mark_automation_visible (pan->what, false);
1963
1964          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1965 }
1966
1967 void
1968 RouteTimeAxisView::add_existing_processor_automation_curves (boost::weak_ptr<Processor> p)
1969 {
1970         boost::shared_ptr<Processor> processor (p.lock ());
1971         if (!processor) {
1972                 return;
1973         }
1974         
1975         set<Evoral::Parameter> s;
1976         boost::shared_ptr<AutomationLine> al;
1977
1978         processor->what_has_visible_data (s);
1979
1980         for (set<Evoral::Parameter>::iterator i = s.begin(); i != s.end(); ++i) {
1981                 
1982                 if ((al = find_processor_automation_curve (processor, *i)) != 0) {
1983                         al->queue_reset ();
1984                 } else {
1985                         add_processor_automation_curve (processor, (*i));
1986                 }
1987         }
1988 }
1989
1990 void
1991 RouteTimeAxisView::add_automation_child(Evoral::Parameter param, boost::shared_ptr<AutomationTimeAxisView> track, bool show)
1992 {
1993         using namespace Menu_Helpers;
1994
1995         XMLProperty* prop;
1996         XMLNode* node;
1997
1998         add_child (track);
1999
2000         track->Hiding.connect (bind (mem_fun (*this, &RouteTimeAxisView::automation_track_hidden), param));
2001
2002         bool hideit = (!show);
2003
2004         if ((node = track->get_state_node()) != 0) {
2005                 if  ((prop = node->property ("shown")) != 0) {
2006                         if (prop->value() == "yes") {
2007                                 hideit = false;
2008                         }
2009                 } 
2010         }
2011
2012         _automation_tracks.insert(std::make_pair(param, new RouteAutomationNode(param, NULL, track)));
2013
2014         if (hideit) {
2015                 track->hide ();
2016         } else {
2017                 _show_automation.insert (param);
2018
2019
2020                 if (!no_redraw) {
2021                         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
2022                 }
2023         }
2024
2025         build_display_menu();
2026 }
2027
2028
2029 void
2030 RouteTimeAxisView::add_processor_to_subplugin_menu (boost::weak_ptr<Processor> p, boost::weak_ptr<Route> r)
2031 {
2032         boost::shared_ptr<Processor> processor (p.lock ());
2033         boost::shared_ptr<Route> route (r.lock ());
2034         if (!processor || !route) {
2035                 return;
2036         }
2037
2038         if (processor == route->amp ()) {
2039                 /* don't add an entry for the amp processor */
2040                 return;
2041         }
2042         
2043         using namespace Menu_Helpers;
2044         ProcessorAutomationInfo *rai;
2045         list<ProcessorAutomationInfo*>::iterator x;
2046         
2047         const std::set<Evoral::Parameter>& automatable = processor->what_can_be_automated ();
2048         std::set<Evoral::Parameter> has_visible_automation;
2049
2050         processor->what_has_visible_data(has_visible_automation);
2051
2052         if (automatable.empty()) {
2053                 return;
2054         }
2055
2056         for (x = processor_automation.begin(); x != processor_automation.end(); ++x) {
2057                 if ((*x)->processor == processor) {
2058                         break;
2059                 }
2060         }
2061
2062         if (x == processor_automation.end()) {
2063
2064                 rai = new ProcessorAutomationInfo (processor);
2065                 processor_automation.push_back (rai);
2066
2067         } else {
2068
2069                 rai = *x;
2070
2071         }
2072
2073         /* any older menu was deleted at the top of processors_changed()
2074            when we cleared the subplugin menu.
2075         */
2076
2077         rai->menu = manage (new Menu);
2078         MenuList& items = rai->menu->items();
2079         rai->menu->set_name ("ArdourContextMenu");
2080
2081         items.clear ();
2082
2083         for (std::set<Evoral::Parameter>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
2084
2085                 ProcessorAutomationNode* pan;
2086                 CheckMenuItem* mitem;
2087                 
2088                 string name = processor->describe_parameter (*i);
2089                 
2090                 items.push_back (CheckMenuElem (name));
2091                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
2092
2093                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
2094                         mitem->set_active(true);
2095                 }
2096
2097                 if ((pan = find_processor_automation_node (processor, *i)) == 0) {
2098
2099                         /* new item */
2100                         
2101                         pan = new ProcessorAutomationNode (*i, mitem, *this);
2102                         
2103                         rai->lines.push_back (pan);
2104
2105                 } else {
2106
2107                         pan->menu_item = mitem;
2108
2109                 }
2110
2111                 mitem->signal_toggled().connect (bind (mem_fun(*this, &RouteTimeAxisView::processor_menu_item_toggled), rai, pan));
2112         }
2113
2114         /* add the menu for this processor, because the subplugin
2115            menu is always cleared at the top of processors_changed().
2116            this is the result of some poor design in gtkmm and/or
2117            GTK+.
2118         */
2119
2120         subplugin_menu.items().push_back (MenuElem (processor->name(), *rai->menu));
2121         rai->valid = true;
2122 }
2123
2124 void
2125 RouteTimeAxisView::processor_menu_item_toggled (RouteTimeAxisView::ProcessorAutomationInfo* rai,
2126                                                RouteTimeAxisView::ProcessorAutomationNode* pan)
2127 {
2128         bool showit = pan->menu_item->get_active();
2129         bool redraw = false;
2130
2131         if (pan->view == 0 && showit) {
2132                 add_processor_automation_curve (rai->processor, pan->what);
2133                 redraw = true;
2134         }
2135
2136         if (pan->view && showit != pan->view->marked_for_display()) {
2137
2138                 if (showit) {
2139                         pan->view->set_marked_for_display (true);
2140                         pan->view->canvas_display()->show();
2141                         pan->view->canvas_background()->show();
2142                 } else {
2143                         rai->processor->mark_automation_visible (pan->what, true);
2144                         pan->view->set_marked_for_display (false);
2145                         pan->view->hide ();
2146                 }
2147
2148                 redraw = true;
2149
2150         }
2151
2152         if (redraw && !no_redraw) {
2153
2154                 /* now trigger a redisplay */
2155                 
2156                  _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
2157
2158         }
2159 }
2160
2161 void
2162 RouteTimeAxisView::processors_changed ()
2163 {
2164         using namespace Menu_Helpers;
2165         
2166         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
2167                 (*i)->valid = false;
2168         }
2169
2170         subplugin_menu.items().clear ();
2171
2172         _route->foreach_processor (bind (mem_fun (*this, &RouteTimeAxisView::add_processor_to_subplugin_menu), _route));
2173         _route->foreach_processor (mem_fun (*this, &RouteTimeAxisView::add_existing_processor_automation_curves));
2174
2175         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ) {
2176
2177                 list<ProcessorAutomationInfo*>::iterator tmp;
2178
2179                 tmp = i;
2180                 ++tmp;
2181
2182                 if (!(*i)->valid) {
2183
2184                         delete *i;
2185                         processor_automation.erase (i);
2186
2187                 } 
2188
2189                 i = tmp;
2190         }
2191
2192         /* change in visibility was possible */
2193
2194         _route->gui_changed ("visible_tracks", this);
2195 }
2196
2197 boost::shared_ptr<AutomationLine>
2198 RouteTimeAxisView::find_processor_automation_curve (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
2199 {
2200         ProcessorAutomationNode* pan;
2201
2202         if ((pan = find_processor_automation_node (processor, what)) != 0) {
2203                 if (pan->view) {
2204                         pan->view->line();
2205                 } 
2206         }
2207
2208         return boost::shared_ptr<AutomationLine>();
2209 }
2210
2211 void
2212 RouteTimeAxisView::reset_processor_automation_curves ()
2213 {
2214         for (ProcessorAutomationCurves::iterator i = processor_automation_curves.begin(); i != processor_automation_curves.end(); ++i) {
2215                 (*i)->reset();
2216         }
2217 }
2218
2219 void
2220 RouteTimeAxisView::update_rec_display ()
2221 {
2222         RouteUI::update_rec_display ();
2223         name_entry.set_sensitive (!_route->record_enabled());
2224 }
2225                 
2226 void
2227 RouteTimeAxisView::set_layer_display (LayerDisplay d)
2228 {
2229         if (_view) {
2230                 _view->set_layer_display (d);
2231         }
2232 }
2233
2234 LayerDisplay
2235 RouteTimeAxisView::layer_display () const
2236 {
2237         if (_view) {
2238                 return _view->layer_display ();
2239         }
2240
2241         /* we don't know, since we don't have a _view, so just return something */
2242         return Overlaid;
2243 }
2244
2245         
2246
2247 boost::shared_ptr<AutomationTimeAxisView>
2248 RouteTimeAxisView::automation_child(Evoral::Parameter param)
2249 {
2250         AutomationTracks::iterator i = _automation_tracks.find(param);
2251         if (i != _automation_tracks.end())
2252                 return i->second->track;
2253         else
2254                 return boost::shared_ptr<AutomationTimeAxisView>();
2255 }
2256
2257 void
2258 RouteTimeAxisView::fast_update ()
2259 {
2260         gm.get_level_meter().update_meters ();
2261 }
2262
2263 void
2264 RouteTimeAxisView::hide_meter ()
2265 {
2266         clear_meter ();
2267         gm.get_level_meter().hide_meters ();
2268 }
2269
2270 void
2271 RouteTimeAxisView::show_meter ()
2272 {
2273         reset_meter ();
2274 }
2275
2276 void
2277 RouteTimeAxisView::reset_meter ()
2278 {
2279         if (Config->get_show_track_meters()) {
2280                 gm.get_level_meter().setup_meters (height-5);
2281         } else {
2282                 hide_meter ();
2283         }
2284 }
2285
2286 void
2287 RouteTimeAxisView::clear_meter ()
2288 {
2289         gm.get_level_meter().clear_meters ();
2290 }
2291
2292 void
2293 RouteTimeAxisView::meter_changed (void *src)
2294 {
2295         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RouteTimeAxisView::meter_changed), src));
2296         reset_meter();
2297 }
2298
2299 void
2300 RouteTimeAxisView::io_changed (IOChange change, void *src)
2301 {
2302         reset_meter ();
2303 }
2304
2305 void
2306 RouteTimeAxisView::build_underlay_menu(Gtk::Menu* parent_menu) {
2307         using namespace Menu_Helpers;
2308
2309         if(!_underlay_streams.empty()) {
2310                 MenuList& parent_items = parent_menu->items();
2311                 Menu* gs_menu = manage (new Menu);
2312                 gs_menu->set_name ("ArdourContextMenu");
2313                 MenuList& gs_items = gs_menu->items();
2314                 
2315                 parent_items.push_back (MenuElem (_("Underlays"), *gs_menu));
2316                 
2317                 for(UnderlayList::iterator it = _underlay_streams.begin(); it != _underlay_streams.end(); ++it) {
2318                         gs_items.push_back(MenuElem(string_compose(_("Remove \"%1\""), (*it)->trackview().name()),
2319                                                     bind(mem_fun(*this, &RouteTimeAxisView::remove_underlay), *it)));
2320                 }
2321         }
2322 }
2323
2324 bool
2325 RouteTimeAxisView::set_underlay_state() 
2326 {
2327         if(!underlay_xml_node) {
2328                 return false;
2329         }
2330
2331         XMLNodeList nlist = underlay_xml_node->children();
2332         XMLNodeConstIterator niter;
2333         XMLNode *child_node;
2334         
2335         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2336                 child_node = *niter;
2337
2338                 if(child_node->name() != "Underlay") {
2339                         continue;
2340                 }
2341
2342                 XMLProperty* prop = child_node->property ("id");
2343                 if (prop) {
2344                         PBD::ID id (prop->value());
2345
2346                         RouteTimeAxisView* v = _editor.get_route_view_by_id (id);
2347
2348                         if (v) {
2349                                 add_underlay(v->view(), false);
2350                         }
2351                 }
2352         }
2353
2354         return false;
2355 }
2356
2357 void
2358 RouteTimeAxisView::add_underlay(StreamView* v, bool update_xml) 
2359 {
2360         if(!v) {
2361                 return;
2362         }
2363
2364         RouteTimeAxisView& other = v->trackview();
2365
2366         if(find(_underlay_streams.begin(), _underlay_streams.end(), v) == _underlay_streams.end()) {
2367                 if(find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this) != other._underlay_mirrors.end()) {
2368                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2369                         /*NOTREACHED*/
2370                 }
2371
2372                 _underlay_streams.push_back(v);
2373                 other._underlay_mirrors.push_back(this);
2374
2375                 v->foreach_regionview(mem_fun(*this, &RouteTimeAxisView::add_ghost));
2376
2377                 if(update_xml) {
2378                         if(!underlay_xml_node) {
2379                                 ensure_xml_node();
2380                                 underlay_xml_node = xml_node->add_child("Underlays");
2381                         }
2382
2383                         XMLNode* node = underlay_xml_node->add_child("Underlay");
2384                         XMLProperty* prop = node->add_property("id");
2385                         prop->set_value(v->trackview().route()->id().to_s());
2386                 }
2387         }
2388 }
2389
2390 void
2391 RouteTimeAxisView::remove_underlay(StreamView* v) 
2392 {
2393         if(!v) {
2394                 return;
2395         }
2396
2397         UnderlayList::iterator it = find(_underlay_streams.begin(), _underlay_streams.end(), v);
2398         RouteTimeAxisView& other = v->trackview();
2399
2400         if(it != _underlay_streams.end()) {
2401                 UnderlayMirrorList::iterator gm = find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this);
2402
2403                 if(gm == other._underlay_mirrors.end()) {
2404                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2405                         /*NOTREACHED*/
2406                 }
2407
2408                 v->foreach_regionview(mem_fun(*this, &RouteTimeAxisView::remove_ghost));
2409
2410                 _underlay_streams.erase(it);
2411                 other._underlay_mirrors.erase(gm);
2412
2413                 if(underlay_xml_node) {
2414                         underlay_xml_node->remove_nodes_and_delete("id", v->trackview().route()->id().to_s());
2415                 }
2416         }
2417 }
2418
2419 void
2420 RouteTimeAxisView::set_button_names ()
2421 {
2422         rec_enable_button_label.set_text (_("r"));
2423         solo_button_label.set_text (_("s"));
2424         mute_button_label.set_text (_("m"));
2425 }
2426
2427 void
2428 RouteTimeAxisView::set_route_group_to_new ()
2429 {
2430         RouteGroup* g = new RouteGroup (_session, "", RouteGroup::Active);
2431
2432         RouteGroupDialog d (g, Gtk::Stock::NEW);
2433         int const r = d.do_run ();
2434
2435         if (r == Gtk::RESPONSE_OK) {
2436                 _session.add_route_group (g);
2437                 _route->set_route_group (g, this);
2438         } else {
2439                 delete g;
2440         }
2441 }