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