9769037461519266f85ea8b7a781ee095bb604a2
[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
27 #include <sigc++/bind.h>
28
29 #include <pbd/error.h>
30 #include <pbd/stl_delete.h>
31 #include <pbd/whitespace.h>
32 #include <pbd/memento_command.h>
33
34 #include <gtkmm/menu.h>
35 #include <gtkmm/menuitem.h>
36 #include <gtkmm2ext/gtk_ui.h>
37 #include <gtkmm2ext/selector.h>
38 #include <gtkmm2ext/stop_signal.h>
39 #include <gtkmm2ext/bindable_button.h>
40 #include <gtkmm2ext/utils.h>
41
42 #include <ardour/playlist.h>
43 #include <ardour/audioplaylist.h>
44 #include <ardour/diskstream.h>
45 #include <ardour/insert.h>
46 #include <ardour/ladspa_plugin.h>
47 #include <ardour/location.h>
48 #include <ardour/panner.h>
49 #include <ardour/playlist.h>
50 #include <ardour/session.h>
51 #include <ardour/session_playlist.h>
52 #include <ardour/utils.h>
53
54 #include "ardour_ui.h"
55 #include "route_time_axis.h"
56 #include "automation_time_axis.h"
57 #include "redirect_automation_time_axis.h"
58 #include "redirect_automation_line.h"
59 #include "canvas_impl.h"
60 #include "crossfade_view.h"
61 #include "enums.h"
62 #include "gui_thread.h"
63 #include "keyboard.h"
64 #include "playlist_selector.h"
65 #include "point_selection.h"
66 #include "prompter.h"
67 #include "public_editor.h"
68 #include "region_view.h"
69 #include "rgb_macros.h"
70 #include "selection.h"
71 #include "simplerect.h"
72 #include "streamview.h"
73 #include "utils.h"
74
75 #include <ardour/track.h>
76
77 #include "i18n.h"
78
79 using namespace ARDOUR;
80 using namespace PBD;
81 using namespace Gtk;
82 using namespace Editing;
83
84
85 RouteTimeAxisView::RouteTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
86         : AxisView(sess),
87           RouteUI(rt, sess, _("m"), _("s"), _("r")), // mute, solo, and record
88           TimeAxisView(sess,ed,(TimeAxisView*) 0, canvas),
89           parent_canvas (canvas),
90           button_table (3, 3),
91           edit_group_button (_("g")), // group
92           playlist_button (_("p")), 
93           size_button (_("h")), // height
94           automation_button (_("a")),
95           visual_button (_("v"))
96
97 {
98         _has_state = true;
99         playlist_menu = 0;
100         playlist_action_menu = 0;
101         automation_action_menu = 0;
102         _view = 0;
103         timestretch_rect = 0;
104         no_redraw = false;
105
106         ignore_toggle = false;
107
108         edit_group_button.set_name ("TrackGroupButton");
109         playlist_button.set_name ("TrackPlaylistButton");
110         automation_button.set_name ("TrackAutomationButton");
111         size_button.set_name ("TrackSizeButton");
112         visual_button.set_name ("TrackVisualButton");
113         hide_button.set_name ("TrackRemoveButton");
114
115         hide_button.add (*(manage (new Image (::get_icon("hide")))));
116         hide_button.show_all ();
117
118         edit_group_button.signal_button_release_event().connect (mem_fun(*this, &RouteTimeAxisView::edit_click), false);
119         playlist_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::playlist_click));
120         automation_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::automation_click));
121         size_button.signal_button_release_event().connect (mem_fun(*this, &RouteTimeAxisView::size_click), false);
122         visual_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::visual_click));
123         hide_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::hide_click));
124
125         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
126         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release));
127         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
128         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release));
129
130         if (is_track()) {
131
132                 /* use icon */
133
134                 rec_enable_button->remove ();
135                 switch (track()->mode()) {
136                 case ARDOUR::Normal:
137                         rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
138                         break;
139                 case ARDOUR::Destructive:
140                         rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
141                         break;
142                 }
143                 rec_enable_button->show_all ();
144
145                 rec_enable_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::rec_enable_press), false);
146                 rec_enable_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::rec_enable_release));
147                 controls_table.attach (*rec_enable_button, 5, 6, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
148                 ARDOUR_UI::instance()->tooltips().set_tip(*rec_enable_button, _("Record"));
149         }
150
151         controls_table.attach (*mute_button, 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
152         controls_table.attach (*solo_button, 7, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::FILL|Gtk::EXPAND, 0, 0);
153
154         controls_table.attach (edit_group_button, 6, 7, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
155
156         ARDOUR_UI::instance()->tooltips().set_tip(*solo_button,_("Solo"));
157         ARDOUR_UI::instance()->tooltips().set_tip(*mute_button,_("Mute"));
158         ARDOUR_UI::instance()->tooltips().set_tip(edit_group_button,_("Edit Group"));
159         ARDOUR_UI::instance()->tooltips().set_tip(size_button,_("Display Height"));
160         ARDOUR_UI::instance()->tooltips().set_tip(playlist_button,_("Playlist"));
161         ARDOUR_UI::instance()->tooltips().set_tip(automation_button, _("Automation"));
162         ARDOUR_UI::instance()->tooltips().set_tip(visual_button, _("Visual options"));
163         ARDOUR_UI::instance()->tooltips().set_tip(hide_button, _("Hide this track"));
164         
165         label_view ();
166
167         controls_table.attach (hide_button, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
168         controls_table.attach (visual_button, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
169         controls_table.attach (size_button, 2, 3, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
170         controls_table.attach (automation_button, 3, 4, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
171
172         if (is_track() && track()->mode() == ARDOUR::Normal) {
173                 controls_table.attach (playlist_button, 5, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
174         }
175
176         /* remove focus from the buttons */
177         
178         y_position = -1;
179
180         _route->redirects_changed.connect (mem_fun(*this, &RouteTimeAxisView::redirects_changed));
181         _route->name_changed.connect (mem_fun(*this, &RouteTimeAxisView::route_name_changed));
182
183         if (is_track()) {
184
185                 track()->TrackModeChanged.connect (mem_fun(*this, &RouteTimeAxisView::track_mode_changed));
186                 track()->FreezeChange.connect (mem_fun(*this, &RouteTimeAxisView::map_frozen));
187                 track()->DiskstreamChanged.connect (mem_fun(*this, &RouteTimeAxisView::diskstream_changed));
188                 get_diskstream()->SpeedChanged.connect (mem_fun(*this, &RouteTimeAxisView::speed_changed));
189
190                 /* pick up the correct freeze state */
191                 map_frozen ();
192
193         }
194
195         editor.ZoomChanged.connect (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
196         ColorChanged.connect (mem_fun (*this, &RouteTimeAxisView::color_handler));
197 }
198
199 RouteTimeAxisView::~RouteTimeAxisView ()
200 {
201         GoingAway (); /* EMIT_SIGNAL */
202
203         vector_delete (&redirect_automation_curves);
204
205         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
206                 delete *i;
207         }
208
209         if (playlist_menu) {
210                 delete playlist_menu;
211                 playlist_menu = 0;
212         }
213   
214         if (playlist_action_menu) {
215                 delete playlist_action_menu;
216                 playlist_action_menu = 0;
217         }
218
219         if (_view) {
220                 delete _view;
221                 _view = 0;
222         }
223 }
224
225 void
226 RouteTimeAxisView::post_construct ()
227 {
228         /* map current state of the route */
229
230         update_diskstream_display ();
231         _route->foreach_redirect (this, &RouteTimeAxisView::add_redirect_to_subplugin_menu);
232         _route->foreach_redirect (this, &RouteTimeAxisView::add_existing_redirect_automation_curves);
233         reset_redirect_automation_curves ();
234 }
235
236 void
237 RouteTimeAxisView::set_playlist (boost::shared_ptr<Playlist> newplaylist)
238 {
239         boost::shared_ptr<Playlist> pl = playlist();
240         assert(pl);
241
242         modified_connection.disconnect ();
243         modified_connection = pl->Modified.connect (mem_fun(*this, &RouteTimeAxisView::playlist_modified));
244 }
245
246 void
247 RouteTimeAxisView::playlist_modified ()
248 {
249 }
250
251 gint
252 RouteTimeAxisView::edit_click (GdkEventButton *ev)
253 {
254         if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
255                 _route->set_edit_group (0, this);
256                 return FALSE;
257         } 
258
259         using namespace Menu_Helpers;
260
261         MenuList& items = edit_group_menu.items ();
262         RadioMenuItem::Group group;
263
264         items.clear ();
265         items.push_back (RadioMenuElem (group, _("No group"), 
266                                         bind (mem_fun(*this, &RouteTimeAxisView::set_edit_group_from_menu), (RouteGroup *) 0)));
267         
268         if (_route->edit_group() == 0) {
269                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
270         }
271         
272         _session.foreach_edit_group (bind (mem_fun (*this, &RouteTimeAxisView::add_edit_group_menu_item), &group));
273         edit_group_menu.popup (ev->button, ev->time);
274
275         return FALSE;
276 }
277
278 void
279 RouteTimeAxisView::add_edit_group_menu_item (RouteGroup *eg, RadioMenuItem::Group* group)
280 {
281         using namespace Menu_Helpers;
282
283         MenuList &items = edit_group_menu.items();
284
285         items.push_back (RadioMenuElem (*group, eg->name(), bind (mem_fun(*this, &RouteTimeAxisView::set_edit_group_from_menu), eg)));
286         if (_route->edit_group() == eg) {
287                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
288         }
289 }
290
291 void
292 RouteTimeAxisView::set_edit_group_from_menu (RouteGroup *eg)
293 {
294         _route->set_edit_group (eg, this);
295 }
296
297 void
298 RouteTimeAxisView::playlist_changed ()
299
300 {
301         label_view ();
302
303         if (is_track()) {
304                 set_playlist (get_diskstream()->playlist());
305         }
306 }
307
308 void
309 RouteTimeAxisView::label_view ()
310 {
311         string x = _route->name();
312
313         if (x != name_entry.get_text()) {
314                 name_entry.set_text (x);
315         }
316
317         ARDOUR_UI::instance()->tooltips().set_tip (name_entry, x);
318 }
319
320 void
321 RouteTimeAxisView::route_name_changed (void *src)
322 {
323         editor.route_name_changed (this);
324         label_view ();
325 }
326
327 void
328 RouteTimeAxisView::take_name_changed (void *src)
329
330 {
331         if (src != this) {
332                 label_view ();
333         }
334 }
335
336 void
337 RouteTimeAxisView::playlist_click ()
338 {
339         // always build a new action menu
340         
341         if (playlist_action_menu != 0) {
342                 delete playlist_action_menu;
343         } 
344
345         playlist_action_menu = new Menu;
346         playlist_action_menu->set_name ("ArdourContextMenu");
347         
348         build_playlist_menu (playlist_action_menu);
349         editor.set_selected_track (*this, Selection::Add);
350         playlist_action_menu->popup (1, gtk_get_current_event_time());
351 }
352
353 void
354 RouteTimeAxisView::automation_click ()
355 {
356         if (automation_action_menu == 0) {
357                 /* this seems odd, but the automation action
358                    menu is built as part of the display menu.
359                 */
360                 build_display_menu ();
361         }
362         editor.set_selected_track (*this, Selection::Add);
363         automation_action_menu->popup (1, gtk_get_current_event_time());
364 }
365
366 void
367 RouteTimeAxisView::build_automation_action_menu ()
368 {
369         using namespace Menu_Helpers;
370
371         automation_action_menu = manage (new Menu);
372         MenuList& automation_items = automation_action_menu->items();
373         automation_action_menu->set_name ("ArdourContextMenu");
374         
375         automation_items.push_back (MenuElem (_("Show all automation"),
376                                               mem_fun(*this, &RouteTimeAxisView::show_all_automation)));
377
378         automation_items.push_back (MenuElem (_("Show existing automation"),
379                                               mem_fun(*this, &RouteTimeAxisView::show_existing_automation)));
380
381         automation_items.push_back (MenuElem (_("Hide all automation"),
382                                               mem_fun(*this, &RouteTimeAxisView::hide_all_automation)));
383
384         automation_items.push_back (MenuElem (_("Plugins"), subplugin_menu));
385 }
386
387 void
388 RouteTimeAxisView::build_display_menu ()
389 {
390         using namespace Menu_Helpers;
391
392         /* get the size menu ready */
393
394         build_size_menu ();
395
396         /* prepare it */
397
398         TimeAxisView::build_display_menu ();
399
400         /* now fill it with our stuff */
401
402         MenuList& items = display_menu->items();
403         display_menu->set_name ("ArdourContextMenu");
404         
405         items.push_back (MenuElem (_("Height"), *size_menu));
406         items.push_back (MenuElem (_("Color"), mem_fun(*this, &RouteTimeAxisView::select_track_color)));
407
408         items.push_back (SeparatorElem());
409
410         build_remote_control_menu ();
411         items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
412
413         build_automation_action_menu ();
414         items.push_back (MenuElem (_("Automation"), *automation_action_menu));
415
416         // Hook for derived classes to add type specific stuff
417         items.push_back (SeparatorElem());
418         append_extra_display_menu_items ();
419         items.push_back (SeparatorElem());
420         
421         if (is_track()) {
422
423                 Menu* alignment_menu = manage (new Menu);
424                 MenuList& alignment_items = alignment_menu->items();
425                 alignment_menu->set_name ("ArdourContextMenu");
426
427                 RadioMenuItem::Group align_group;
428                 
429                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"),
430                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
431                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
432                 if (get_diskstream()->alignment_style() == ExistingMaterial)
433                         align_existing_item->set_active();
434                 
435                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"),
436                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
437                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
438                 if (get_diskstream()->alignment_style() == CaptureTime)
439                         align_capture_item->set_active();
440                 
441                 items.push_back (MenuElem (_("Alignment"), *alignment_menu));
442
443                 get_diskstream()->AlignmentStyleChanged.connect (
444                         mem_fun(*this, &RouteTimeAxisView::align_style_changed));
445
446                 RadioMenuItem::Group mode_group;
447                 items.push_back (RadioMenuElem (mode_group, _("Normal mode"),
448                                                 bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::Normal)));
449                 normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
450                 items.push_back (RadioMenuElem (mode_group, _("Tape mode"),
451                                                 bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::Destructive)));
452                 destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
453                                  
454                 
455                 switch (track()->mode()) {
456                 case ARDOUR::Destructive:
457                         destructive_track_mode_item->set_active ();
458                         break;
459                 case ARDOUR::Normal:
460                         normal_track_mode_item->set_active ();
461                         break;
462                 }
463         }
464
465         items.push_back (SeparatorElem());
466         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
467         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
468         route_active_menu_item->set_active (_route->active());
469
470         items.push_back (SeparatorElem());
471         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
472 }
473
474 static bool __reset_item (RadioMenuItem* item)
475 {
476         item->set_active ();
477         return false;
478 }
479
480 void
481 RouteTimeAxisView::set_track_mode (TrackMode mode)
482 {
483         RadioMenuItem* item;
484         RadioMenuItem* other_item;
485
486         switch (mode) {
487         case ARDOUR::Normal:
488                 item = normal_track_mode_item;
489                 other_item = destructive_track_mode_item;
490                 break;
491         case ARDOUR::Destructive:
492                 item = destructive_track_mode_item;
493                 other_item = normal_track_mode_item;
494                 break;
495         default:
496                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", mode) << endmsg;
497                 /*NOTREACHED*/
498                 return;
499         }
500
501         if (item->get_active () && track()->mode() != mode) {
502                 _set_track_mode (track(), mode, other_item);
503         }
504 }
505
506 void
507 RouteTimeAxisView::_set_track_mode (Track* track, TrackMode mode, RadioMenuItem* reset_item)
508 {
509         bool needs_bounce;
510
511         if (!track->can_use_mode (mode, needs_bounce)) {
512
513                 if (!needs_bounce) {
514                         /* cannot be done */
515                         Glib::signal_idle().connect (bind (sigc::ptr_fun (__reset_item), reset_item));
516                         return;
517                 } else {
518                         cerr << "would bounce this one\n";
519                         return;
520                 }
521         }
522
523         track->set_mode (mode);
524
525         rec_enable_button->remove ();
526         switch (mode) {
527         case ARDOUR::Normal:
528                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
529                 break;
530         case ARDOUR::Destructive:
531                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
532                 break;
533         }
534         rec_enable_button->show_all ();
535
536 }
537
538 void
539 RouteTimeAxisView::track_mode_changed ()
540 {
541         RadioMenuItem* item;
542         
543         switch (track()->mode()) {
544         case ARDOUR::Normal:
545                 item = normal_track_mode_item;
546                 break;
547         case ARDOUR::Destructive:
548                 item = destructive_track_mode_item;
549                 break;
550         default:
551                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", track()->mode()) << endmsg;
552                 /*NOTREACHED*/
553                 return;
554         }
555
556         item->set_active ();
557 }
558
559 void
560 RouteTimeAxisView::show_timestretch (nframes_t start, nframes_t end)
561 {
562         double x1;
563         double x2;
564         double y2;
565         
566         TimeAxisView::show_timestretch (start, end);
567
568         hide_timestretch ();
569
570 #if 0   
571         if (ts.empty()) {
572                 return;
573         }
574
575
576         /* check that the time selection was made in our route, or our edit group.
577            remember that edit_group() == 0 implies the route is *not* in a edit group.
578         */
579
580         if (!(ts.track == this || (ts.group != 0 && ts.group == _route->edit_group()))) {
581                 /* this doesn't apply to us */
582                 return;
583         }
584
585         /* ignore it if our edit group is not active */
586         
587         if ((ts.track != this) && _route->edit_group() && !_route->edit_group()->is_active()) {
588                 return;
589         }
590 #endif
591
592         if (timestretch_rect == 0) {
593                 timestretch_rect = new SimpleRect (*canvas_display);
594                 timestretch_rect->property_x1() =  0.0;
595                 timestretch_rect->property_y1() =  0.0;
596                 timestretch_rect->property_x2() =  0.0;
597                 timestretch_rect->property_y2() =  0.0;
598                 timestretch_rect->property_fill_color_rgba() =  color_map[cTimeStretchFill];
599                 timestretch_rect->property_outline_color_rgba() = color_map[cTimeStretchOutline];
600         }
601
602         timestretch_rect->show ();
603         timestretch_rect->raise_to_top ();
604
605         x1 = start / editor.get_current_zoom();
606         x2 = (end - 1) / editor.get_current_zoom();
607         y2 = height - 2;
608         
609         timestretch_rect->property_x1() = x1;
610         timestretch_rect->property_y1() = 1.0;
611         timestretch_rect->property_x2() = x2;
612         timestretch_rect->property_y2() = y2;
613 }
614
615 void
616 RouteTimeAxisView::hide_timestretch ()
617 {
618         TimeAxisView::hide_timestretch ();
619
620         if (timestretch_rect) {
621                 timestretch_rect->hide ();
622         }
623 }
624
625 void
626 RouteTimeAxisView::show_selection (TimeSelection& ts)
627 {
628
629 #if 0
630         /* ignore it if our edit group is not active or if the selection was started
631            in some other track or edit group (remember that edit_group() == 0 means
632            that the track is not in an edit group).
633         */
634
635         if (((ts.track != this && !is_child (ts.track)) && _route->edit_group() && !_route->edit_group()->is_active()) ||
636             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route->edit_group())))) {
637                 hide_selection ();
638                 return;
639         }
640 #endif
641
642         TimeAxisView::show_selection (ts);
643 }
644
645 void
646 RouteTimeAxisView::set_height (TrackHeight h)
647 {
648         bool height_changed = (height == 0) || (h != height_style);
649
650         TimeAxisView::set_height (h);
651
652         ensure_xml_node ();
653
654         if (_view) {
655                 _view->set_height ((double) height);
656         }
657
658         switch (height_style) {
659         case Largest:
660                 xml_node->add_property ("track_height", "largest");
661                 break;
662
663         case Large:
664                 xml_node->add_property ("track_height", "large");
665                 break;
666
667         case Larger:
668                 xml_node->add_property ("track_height", "larger");
669                 break;
670
671         case Normal:
672                 xml_node->add_property ("track_height", "normal");
673                 break;
674
675         case Smaller:
676                 xml_node->add_property ("track_height", "smaller");
677                 break;
678
679         case Small:
680                 xml_node->add_property ("track_height", "small");
681                 break;
682         }
683
684         switch (height_style) {
685         case Largest:
686         case Large:
687         case Larger:
688         case Normal:
689                 show_name_entry ();
690                 hide_name_label ();
691
692                 mute_button->show();
693                 solo_button->show();
694                 if (rec_enable_button)
695                         rec_enable_button->show();
696
697                 edit_group_button.show();
698                 hide_button.show();
699                 visual_button.show();
700                 size_button.show();
701                 automation_button.show();
702                 
703                 if (is_track() && track()->mode() == ARDOUR::Normal) {
704                         playlist_button.show();
705                 }
706                 break;
707
708         case Smaller:
709                 show_name_entry ();
710                 hide_name_label ();
711
712                 mute_button->show();
713                 solo_button->show();
714                 if (rec_enable_button)
715                         rec_enable_button->show();
716
717                 edit_group_button.hide ();
718                 hide_button.hide ();
719                 visual_button.hide ();
720                 size_button.hide ();
721                 automation_button.hide ();
722                 
723                 if (is_track() && track()->mode() == ARDOUR::Normal) {
724                         playlist_button.hide ();
725                 }
726                 break;
727
728         case Small:
729                 hide_name_entry ();
730                 show_name_label ();
731
732                 mute_button->hide();
733                 solo_button->hide();
734                 if (rec_enable_button)
735                         rec_enable_button->hide();
736
737                 edit_group_button.hide ();
738                 hide_button.hide ();
739                 visual_button.hide ();
740                 size_button.hide ();
741                 automation_button.hide ();
742                 playlist_button.hide ();
743                 name_label.set_text (_route->name());
744                 break;
745         }
746
747         if (height_changed) {
748                 /* only emit the signal if the height really changed */
749                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
750         }
751 }
752
753 void
754 RouteTimeAxisView::select_track_color ()
755 {
756         if (RouteUI::choose_color ()) {
757
758                 if (_view) {
759                         _view->apply_color (_color, StreamView::RegionColor);
760                 }
761         }
762 }
763
764 void
765 RouteTimeAxisView::reset_samples_per_unit ()
766 {
767         set_samples_per_unit (editor.get_current_zoom());
768 }
769
770 void
771 RouteTimeAxisView::set_samples_per_unit (double spu)
772 {
773         double speed = 1.0;
774
775         if (get_diskstream() != 0) {
776                 speed = get_diskstream()->speed();
777         }
778         
779         if (_view) {
780                 _view->set_samples_per_unit (spu * speed);
781         }
782
783         TimeAxisView::set_samples_per_unit (spu * speed);
784 }
785
786 void
787 RouteTimeAxisView::align_style_changed ()
788 {
789         switch (get_diskstream()->alignment_style()) {
790         case ExistingMaterial:
791                 if (!align_existing_item->get_active()) {
792                         align_existing_item->set_active();
793                 }
794                 break;
795         case CaptureTime:
796                 if (!align_capture_item->get_active()) {
797                         align_capture_item->set_active();
798                 }
799                 break;
800         }
801 }
802
803 void
804 RouteTimeAxisView::set_align_style (AlignStyle style)
805 {
806         RadioMenuItem* item;
807
808         switch (style) {
809         case ExistingMaterial:
810                 item = align_existing_item;
811                 break;
812         case CaptureTime:
813                 item = align_capture_item;
814                 break;
815         default:
816                 fatal << string_compose (_("programming error: %1 %2"), "illegal align style in RouteTimeAxisView::set_align_style", style) << endmsg;
817                 /*NOTREACHED*/
818                 return;
819         }
820
821         if (item->get_active()) {
822                 get_diskstream()->set_align_style (style);
823         }
824 }
825
826 void
827 RouteTimeAxisView::rename_current_playlist ()
828 {
829         ArdourPrompter prompter (true);
830         string name;
831
832         boost::shared_ptr<Diskstream> ds = get_diskstream();
833         if (!ds || ds->destructive())
834                 return;
835
836         boost::shared_ptr<Playlist> pl = ds->playlist();
837         if (!pl)
838                 return;
839
840         prompter.set_prompt (_("Name for playlist"));
841         prompter.set_initial_text (pl->name());
842         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
843         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
844
845         switch (prompter.run ()) {
846         case Gtk::RESPONSE_ACCEPT:
847                 prompter.get_result (name);
848                 if (name.length()) {
849                         pl->set_name (name);
850                 }
851                 break;
852
853         default:
854                 break;
855         }
856 }
857
858 void
859 RouteTimeAxisView::use_copy_playlist (bool prompt)
860 {
861         string name;
862         
863         boost::shared_ptr<Diskstream> ds = get_diskstream();
864         if (!ds || ds->destructive())
865                 return;
866
867         boost::shared_ptr<const Playlist> pl = ds->playlist();
868         if (!pl)
869                 return;
870
871         name = pl->name();
872
873         do {
874                 name = Playlist::bump_name (name, _session);
875         } while (_session.playlist_by_name(name));
876
877         // TODO: The prompter "new" button should be de-activated if the user
878         // specifies a playlist name which already exists in the session.
879
880         if (prompt) {
881
882                 ArdourPrompter prompter (true);
883                 
884                 prompter.set_prompt (_("Name for Playlist"));
885                 prompter.set_initial_text (name);
886                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
887                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
888                 prompter.show_all ();
889                 
890                 switch (prompter.run ()) {
891                 case Gtk::RESPONSE_ACCEPT:
892                         prompter.get_result (name);
893                         break;
894                         
895                 default:
896                         return;
897                 }
898         }
899
900         if (name.length()) {
901                 ds->use_copy_playlist ();
902                 ds->playlist()->set_name (name);
903         }
904 }
905
906 void
907 RouteTimeAxisView::use_new_playlist (bool prompt)
908 {
909         string name;
910         
911         boost::shared_ptr<Diskstream> ds = get_diskstream();
912         if (!ds || ds->destructive())
913                 return;
914
915         boost::shared_ptr<const Playlist> pl = ds->playlist();
916         if (!pl)
917                 return;
918
919         name = pl->name();
920
921         do {
922                 name = Playlist::bump_name (name, _session);
923         } while (_session.playlist_by_name(name));
924
925
926         if (prompt) {
927                 
928                 ArdourPrompter prompter (true);
929                 
930                 prompter.set_prompt (_("Name for Playlist"));
931                 prompter.set_initial_text (name);
932                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
933                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
934
935                 switch (prompter.run ()) {
936                 case Gtk::RESPONSE_ACCEPT:
937                         prompter.get_result (name);
938                         break;
939                         
940                 default:
941                         return;
942                 }
943         }
944
945         if (name.length()) {
946                 ds->use_new_playlist ();
947                 ds->playlist()->set_name (name);
948         }
949 }
950
951 void
952 RouteTimeAxisView::clear_playlist ()
953 {
954         boost::shared_ptr<Diskstream> ds = get_diskstream();
955         if (!ds || ds->destructive())
956                 return;
957
958         boost::shared_ptr<Playlist> pl = ds->playlist();
959         if (!pl)
960                 return;
961
962         editor.clear_playlist (pl);
963 }
964
965 void
966 RouteTimeAxisView::speed_changed ()
967 {
968         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
969 }
970
971 void
972 RouteTimeAxisView::diskstream_changed ()
973 {
974         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::update_diskstream_display));
975 }       
976
977 void
978 RouteTimeAxisView::update_diskstream_display ()
979 {
980         if (!get_diskstream()) // bus
981                 return;
982
983         set_playlist (get_diskstream()->playlist());
984         map_frozen ();
985 }       
986
987 void
988 RouteTimeAxisView::selection_click (GdkEventButton* ev)
989 {
990         if (Keyboard::modifier_state_equals (ev->state, (Keyboard::Shift|Keyboard::Control))) {
991
992                 /* special case: select/deselect all tracks */
993                 if (editor.get_selection().selected (this)) {
994                         editor.get_selection().clear_tracks ();
995                 } else {
996                         editor.select_all_tracks ();
997                 }
998
999                 return;
1000         } 
1001
1002         PublicEditor::TrackViewList* tracks = editor.get_valid_views (this, _route->edit_group());
1003
1004         switch (Keyboard::selection_type (ev->state)) {
1005         case Selection::Toggle:
1006                 editor.get_selection().toggle (*tracks);
1007                 break;
1008                 
1009         case Selection::Set:
1010                 editor.get_selection().set (*tracks);
1011                 break;
1012
1013         case Selection::Extend:
1014                 if (tracks->size() > 1) {
1015                         /* add each one, do not "extend" */
1016                         editor.get_selection().add (*tracks);
1017                 } else {
1018                         /* extend to the single track */
1019                         editor.extend_selection_to_track (*tracks->front());
1020                 }
1021                 break;
1022
1023         case Selection::Add:
1024                 editor.get_selection().add (*tracks);
1025                 break;
1026         }
1027
1028         delete tracks;
1029 }
1030
1031 void
1032 RouteTimeAxisView::set_selected_points (PointSelection& points)
1033 {
1034         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1035                 (*i)->set_selected_points (points);
1036         }
1037 }
1038
1039 void
1040 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
1041 {
1042         if (_view) {
1043                 _view->set_selected_regionviews (regions);
1044         }
1045 }
1046
1047 void
1048 RouteTimeAxisView::get_selectables (nframes_t start, nframes_t end, double top, double bot, list<Selectable*>& results)
1049 {
1050         double speed = 1.0;
1051         
1052         if (get_diskstream() != 0) {
1053                 speed = get_diskstream()->speed();
1054         }
1055         
1056         nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
1057         nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
1058
1059         if (_view && ((top < 0.0 && bot < 0.0)) || touched (top, bot)) {
1060                 _view->get_selectables (start_adjusted, end_adjusted, results);
1061         }
1062
1063         /* pick up visible automation tracks */
1064         
1065         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1066                 if (!(*i)->hidden()) {
1067                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1068                 }
1069         }
1070 }
1071
1072 void
1073 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1074 {
1075         if (_view) {
1076                 _view->get_inverted_selectables (sel, results);
1077         }
1078
1079         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1080                 if (!(*i)->hidden()) {
1081                         (*i)->get_inverted_selectables (sel, results);
1082                 }
1083         }
1084
1085         return;
1086 }
1087
1088 RouteGroup*
1089 RouteTimeAxisView::edit_group() const
1090 {
1091         return _route->edit_group();
1092 }
1093
1094 string
1095 RouteTimeAxisView::name() const
1096 {
1097         return _route->name();
1098 }
1099
1100 boost::shared_ptr<Playlist>
1101 RouteTimeAxisView::playlist () const 
1102 {
1103         boost::shared_ptr<Diskstream> ds;
1104
1105         if ((ds = get_diskstream()) != 0) {
1106                 return ds->playlist(); 
1107         } else {
1108                 return boost::shared_ptr<Playlist> ();
1109         }
1110 }
1111
1112 void
1113 RouteTimeAxisView::name_entry_changed ()
1114 {
1115         string x;
1116
1117         x = name_entry.get_text ();
1118         
1119         if (x == _route->name()) {
1120                 return;
1121         }
1122
1123         strip_whitespace_edges(x);
1124
1125         if (x.length() == 0) {
1126                 name_entry.set_text (_route->name());
1127                 return;
1128         }
1129
1130         if (_session.route_name_unique (x)) {
1131                 _route->set_name (x, this);
1132         } else {
1133                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1134                 name_entry.set_text (_route->name());
1135         }
1136 }
1137
1138 void
1139 RouteTimeAxisView::visual_click ()
1140 {
1141         popup_display_menu (0);
1142 }
1143
1144 void
1145 RouteTimeAxisView::hide_click ()
1146 {
1147         // LAME fix for hide_button refresh fix
1148         hide_button.set_sensitive(false);
1149         
1150         editor.hide_track_in_display (*this);
1151         
1152         hide_button.set_sensitive(true);
1153 }
1154
1155 boost::shared_ptr<Region>
1156 RouteTimeAxisView::find_next_region (nframes_t pos, RegionPoint point, int32_t dir)
1157 {
1158         boost::shared_ptr<Diskstream> stream;
1159         boost::shared_ptr<Playlist> playlist;
1160
1161         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1162                 return playlist->find_next_region (pos, point, dir);
1163         }
1164
1165         return boost::shared_ptr<Region> ();
1166 }
1167
1168 bool
1169 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1170 {
1171         boost::shared_ptr<Playlist> what_we_got;
1172         boost::shared_ptr<Diskstream> ds = get_diskstream();
1173         boost::shared_ptr<Playlist> playlist;
1174         bool ret = false;
1175
1176         if (ds == 0) {
1177                 /* route is a bus, not a track */
1178                 return false;
1179         }
1180
1181         playlist = ds->playlist();
1182
1183         TimeSelection time (selection.time);
1184         float speed = ds->speed();
1185         if (speed != 1.0f) {
1186                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1187                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1188                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1189                 }
1190         }
1191         
1192         XMLNode &before = playlist->get_state();
1193         switch (op) {
1194         case Cut:
1195                 if ((what_we_got = playlist->cut (time)) != 0) {
1196                         editor.get_cut_buffer().add (what_we_got);
1197                         _session.add_command( new MementoCommand<Playlist>(*playlist.get(), &before, &playlist->get_state()));
1198                         ret = true;
1199                 }
1200                 break;
1201         case Copy:
1202                 if ((what_we_got = playlist->copy (time)) != 0) {
1203                         editor.get_cut_buffer().add (what_we_got);
1204                 }
1205                 break;
1206
1207         case Clear:
1208                 if ((what_we_got = playlist->cut (time)) != 0) {
1209                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1210                         what_we_got->release ();
1211                         ret = true;
1212                 }
1213                 break;
1214         }
1215
1216         return ret;
1217 }
1218
1219 bool
1220 RouteTimeAxisView::paste (nframes_t pos, float times, Selection& selection, size_t nth)
1221 {
1222         if (!is_track()) {
1223                 return false;
1224         }
1225
1226         boost::shared_ptr<Playlist> playlist = get_diskstream()->playlist();
1227         PlaylistSelection::iterator p;
1228         
1229         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth);
1230
1231         if (p == selection.playlists.end()) {
1232                 return false;
1233         }
1234
1235         if (get_diskstream()->speed() != 1.0f)
1236                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1237         
1238         XMLNode &before = playlist->get_state();
1239         playlist->paste (*p, pos, times);
1240         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1241
1242         return true;
1243 }
1244
1245
1246 list<TimeAxisView*>
1247 RouteTimeAxisView::get_child_list()
1248 {
1249   
1250         list<TimeAxisView*>redirect_children;
1251         
1252         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1253                 if (!(*i)->hidden()) {
1254                         redirect_children.push_back(*i);
1255                 }
1256         }
1257         return redirect_children;
1258 }
1259
1260
1261 void
1262 RouteTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1263 {
1264         using namespace Menu_Helpers;
1265
1266         if (!menu || !is_track()) {
1267                 return;
1268         }
1269
1270         MenuList& playlist_items = menu->items();
1271         menu->set_name ("ArdourContextMenu");
1272         playlist_items.clear();
1273
1274         if (playlist_menu) {
1275                 delete playlist_menu;
1276         }
1277
1278         playlist_menu = new Menu;
1279         playlist_menu->set_name ("ArdourContextMenu");
1280
1281         vector<boost::shared_ptr<Playlist> > playlists;
1282         boost::shared_ptr<Diskstream> ds = get_diskstream();
1283         RadioMenuItem::Group playlist_group;
1284
1285         _session.get_playlists (playlists);
1286         
1287         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1288
1289                 if ((*i)->get_orig_diskstream_id() == ds->id()) {
1290                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist),
1291                                                                                                      boost::weak_ptr<Playlist> (*i))));
1292
1293                         if (ds->playlist()->id() == (*i)->id()) {
1294                                 static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1295                         }
1296                 } else if (ds->playlist()->id() == (*i)->id()) {
1297                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist), 
1298                                                                                                      boost::weak_ptr<Playlist>(*i))));
1299                         static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1300                         
1301                 }
1302         }
1303
1304         playlist_items.push_back (SeparatorElem());
1305         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1306         playlist_items.push_back (SeparatorElem());
1307
1308         playlist_items.push_back (MenuElem (_("New"), mem_fun(editor, &PublicEditor::new_playlists)));
1309         playlist_items.push_back (MenuElem (_("New Copy"), mem_fun(editor, &PublicEditor::copy_playlists)));
1310         playlist_items.push_back (SeparatorElem());
1311         playlist_items.push_back (MenuElem (_("Clear Current"), mem_fun(editor, &PublicEditor::clear_playlists)));
1312         playlist_items.push_back (SeparatorElem());
1313
1314         playlist_items.push_back (MenuElem(_("Select from all ..."), mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1315 }
1316
1317 void
1318 RouteTimeAxisView::use_playlist (boost::weak_ptr<Playlist> wpl)
1319 {
1320         assert (is_track());
1321
1322         boost::shared_ptr<Playlist> pl (wpl.lock());
1323
1324         if (!pl) {
1325                 return;
1326         }
1327
1328         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
1329         
1330         if (apl) {
1331                 get_diskstream()->use_playlist (apl);
1332         }
1333 }
1334
1335 void
1336 RouteTimeAxisView::show_playlist_selector ()
1337 {
1338         editor.playlist_selector().show_for (this);
1339 }
1340
1341 void
1342 RouteTimeAxisView::map_frozen ()
1343 {
1344         if (!is_track()) {
1345                 return;
1346         }
1347
1348         ENSURE_GUI_THREAD (mem_fun(*this, &RouteTimeAxisView::map_frozen));
1349
1350         switch (track()->freeze_state()) {
1351         case Track::Frozen:
1352                 playlist_button.set_sensitive (false);
1353                 rec_enable_button->set_sensitive (false);
1354                 break;
1355         default:
1356                 playlist_button.set_sensitive (true);
1357                 rec_enable_button->set_sensitive (true);
1358                 break;
1359         }
1360 }
1361
1362 void
1363 RouteTimeAxisView::color_handler (ColorID id, uint32_t val)
1364 {
1365         switch (id) {
1366         case cTimeStretchOutline:
1367                 timestretch_rect->property_outline_color_rgba() = val;
1368                 break;
1369         case cTimeStretchFill:
1370                 timestretch_rect->property_fill_color_rgba() = val;
1371                 break;
1372         default:
1373                 break;
1374         }
1375 }
1376
1377 void
1378 RouteTimeAxisView::show_all_automation ()
1379 {
1380         no_redraw = true;
1381
1382         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1383                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1384                         if ((*ii)->view == 0) {
1385                                 add_redirect_automation_curve ((*i)->redirect, (*ii)->what);
1386                         } 
1387
1388                         (*ii)->menu_item->set_active (true);
1389                 }
1390         }
1391
1392         no_redraw = false;
1393
1394          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1395 }
1396
1397 void
1398 RouteTimeAxisView::show_existing_automation ()
1399 {
1400         no_redraw = true;
1401
1402         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1403                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1404                         if ((*ii)->view != 0) {
1405                                 (*ii)->menu_item->set_active (true);
1406                         }
1407                 }
1408         }
1409
1410         no_redraw = false;
1411
1412          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1413 }
1414
1415 void
1416 RouteTimeAxisView::hide_all_automation ()
1417 {
1418         no_redraw = true;
1419
1420         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1421                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1422                         (*ii)->menu_item->set_active (false);
1423                 }
1424         }
1425
1426         no_redraw = false;
1427          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1428 }
1429
1430
1431 void
1432 RouteTimeAxisView::region_view_added (RegionView* rv)
1433 {
1434         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1435                 AutomationTimeAxisView* atv;
1436
1437                 if ((atv = dynamic_cast<AutomationTimeAxisView*> (*i)) != 0) {
1438                         rv->add_ghost (*atv);
1439                 }
1440         }
1441 }
1442
1443 void
1444 RouteTimeAxisView::add_ghost_to_redirect (RegionView* rv, AutomationTimeAxisView* atv)
1445 {
1446         rv->add_ghost (*atv);
1447 }
1448
1449 RouteTimeAxisView::RedirectAutomationInfo::~RedirectAutomationInfo ()
1450 {
1451         for (vector<RedirectAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1452                 delete *i;
1453         }
1454 }
1455
1456
1457 RouteTimeAxisView::RedirectAutomationNode::~RedirectAutomationNode ()
1458 {
1459         parent.remove_ran (this);
1460
1461         if (view) {
1462                 delete view;
1463         }
1464 }
1465
1466 void
1467 RouteTimeAxisView::remove_ran (RedirectAutomationNode* ran)
1468 {
1469         if (ran->view) {
1470                 remove_child (ran->view);
1471         }
1472 }
1473
1474 RouteTimeAxisView::RedirectAutomationNode*
1475 RouteTimeAxisView::find_redirect_automation_node (boost::shared_ptr<Redirect> redirect, uint32_t what)
1476 {
1477         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1478
1479                 if ((*i)->redirect == redirect) {
1480
1481                         for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1482                                 if ((*ii)->what == what) {
1483                                         return *ii;
1484                                 }
1485                         }
1486                 }
1487         }
1488
1489         return 0;
1490 }
1491
1492 // FIXME: duplicated in midi_time_axis.cc
1493 static string 
1494 legalize_for_xml_node (string str)
1495 {
1496         string::size_type pos;
1497         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=:";
1498         string legal;
1499
1500         legal = str;
1501         pos = 0;
1502
1503         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1504                 legal.replace (pos, 1, "_");
1505                 pos += 1;
1506         }
1507
1508         return legal;
1509 }
1510
1511
1512 void
1513 RouteTimeAxisView::add_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
1514 {
1515         RedirectAutomationLine* ral;
1516         string name;
1517         RedirectAutomationNode* ran;
1518
1519         if ((ran = find_redirect_automation_node (redirect, what)) == 0) {
1520                 fatal << _("programming error: ")
1521                       << string_compose (X_("redirect automation curve for %1:%2 not registered with audio track!"),
1522                                   redirect->name(), what)
1523                       << endmsg;
1524                 /*NOTREACHED*/
1525                 return;
1526         }
1527
1528         if (ran->view) {
1529                 return;
1530         }
1531
1532         name = redirect->describe_parameter (what);
1533
1534         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1535
1536         char state_name[256];
1537         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (redirect->name()).c_str(), what);
1538
1539         ran->view = new RedirectAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, name, what, *redirect, state_name);
1540
1541         ral = new RedirectAutomationLine (name, 
1542                                           *redirect, what, _session, *ran->view,
1543                                           *ran->view->canvas_display, redirect->automation_list (what));
1544         
1545         ral->set_line_color (color_map[cRedirectAutomationLine]);
1546         ral->queue_reset ();
1547
1548         ran->view->add_line (*ral);
1549
1550         ran->view->Hiding.connect (bind (mem_fun(*this, &RouteTimeAxisView::redirect_automation_track_hidden), ran, redirect));
1551
1552         if (!ran->view->marked_for_display()) {
1553                 ran->view->hide ();
1554         } else {
1555                 ran->menu_item->set_active (true);
1556         }
1557
1558         add_child (ran->view);
1559
1560         if (_view) {
1561                 _view->foreach_regionview (bind (mem_fun(*this, &RouteTimeAxisView::add_ghost_to_redirect), ran->view));
1562         }
1563
1564         redirect->mark_automation_visible (what, true);
1565 }
1566
1567 void
1568 RouteTimeAxisView::redirect_automation_track_hidden (RouteTimeAxisView::RedirectAutomationNode* ran, boost::shared_ptr<Redirect> r)
1569 {
1570         if (!_hidden) {
1571                 ran->menu_item->set_active (false);
1572         }
1573
1574         r->mark_automation_visible (ran->what, false);
1575
1576          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1577 }
1578
1579 void
1580 RouteTimeAxisView::add_existing_redirect_automation_curves (boost::shared_ptr<Redirect> redirect)
1581 {
1582         set<uint32_t> s;
1583         RedirectAutomationLine *ral;
1584
1585         redirect->what_has_visible_automation (s);
1586
1587         for (set<uint32_t>::iterator i = s.begin(); i != s.end(); ++i) {
1588                 
1589                 if ((ral = find_redirect_automation_curve (redirect, *i)) != 0) {
1590                         ral->queue_reset ();
1591                 } else {
1592                         add_redirect_automation_curve (redirect, (*i));
1593                 }
1594         }
1595 }
1596
1597 void
1598 RouteTimeAxisView::add_redirect_to_subplugin_menu (boost::shared_ptr<Redirect> r)
1599 {
1600         using namespace Menu_Helpers;
1601         RedirectAutomationInfo *rai;
1602         list<RedirectAutomationInfo*>::iterator x;
1603         
1604         const std::set<uint32_t>& automatable = r->what_can_be_automated ();
1605         std::set<uint32_t> has_visible_automation;
1606
1607         r->what_has_visible_automation(has_visible_automation);
1608
1609         if (automatable.empty()) {
1610                 return;
1611         }
1612
1613         for (x = redirect_automation.begin(); x != redirect_automation.end(); ++x) {
1614                 if ((*x)->redirect == r) {
1615                         break;
1616                 }
1617         }
1618
1619         if (x == redirect_automation.end()) {
1620
1621                 rai = new RedirectAutomationInfo (r);
1622                 redirect_automation.push_back (rai);
1623
1624         } else {
1625
1626                 rai = *x;
1627
1628         }
1629
1630         /* any older menu was deleted at the top of redirects_changed()
1631            when we cleared the subplugin menu.
1632         */
1633
1634         rai->menu = manage (new Menu);
1635         MenuList& items = rai->menu->items();
1636         rai->menu->set_name ("ArdourContextMenu");
1637
1638         items.clear ();
1639
1640         for (std::set<uint32_t>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
1641
1642                 RedirectAutomationNode* ran;
1643                 CheckMenuItem* mitem;
1644                 
1645                 string name = r->describe_parameter (*i);
1646                 
1647                 items.push_back (CheckMenuElem (name));
1648                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
1649
1650                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
1651                         mitem->set_active(true);
1652                 }
1653
1654                 if ((ran = find_redirect_automation_node (r, *i)) == 0) {
1655
1656                         /* new item */
1657                         
1658                         ran = new RedirectAutomationNode (*i, mitem, *this);
1659                         
1660                         rai->lines.push_back (ran);
1661
1662                 } else {
1663
1664                         ran->menu_item = mitem;
1665
1666                 }
1667
1668                 mitem->signal_toggled().connect (bind (mem_fun(*this, &RouteTimeAxisView::redirect_menu_item_toggled), rai, ran));
1669         }
1670
1671         /* add the menu for this redirect, because the subplugin
1672            menu is always cleared at the top of redirects_changed().
1673            this is the result of some poor design in gtkmm and/or
1674            GTK+.
1675         */
1676
1677         subplugin_menu.items().push_back (MenuElem (r->name(), *rai->menu));
1678         rai->valid = true;
1679 }
1680
1681 void
1682 RouteTimeAxisView::redirect_menu_item_toggled (RouteTimeAxisView::RedirectAutomationInfo* rai,
1683                                                RouteTimeAxisView::RedirectAutomationNode* ran)
1684 {
1685         bool showit = ran->menu_item->get_active();
1686         bool redraw = false;
1687
1688         if (ran->view == 0 && showit) {
1689                 add_redirect_automation_curve (rai->redirect, ran->what);
1690                 redraw = true;
1691         }
1692
1693         if (showit != ran->view->marked_for_display()) {
1694
1695                 if (showit) {
1696                         ran->view->set_marked_for_display (true);
1697                         ran->view->canvas_display->show();
1698                 } else {
1699                         rai->redirect->mark_automation_visible (ran->what, true);
1700                         ran->view->set_marked_for_display (false);
1701                         ran->view->hide ();
1702                 }
1703
1704                 redraw = true;
1705
1706         }
1707
1708         if (redraw && !no_redraw) {
1709
1710                 /* now trigger a redisplay */
1711                 
1712                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1713
1714         }
1715 }
1716
1717 void
1718 RouteTimeAxisView::redirects_changed (void *src)
1719 {
1720         using namespace Menu_Helpers;
1721
1722         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1723                 (*i)->valid = false;
1724         }
1725
1726         subplugin_menu.items().clear ();
1727
1728         _route->foreach_redirect (this, &RouteTimeAxisView::add_redirect_to_subplugin_menu);
1729         _route->foreach_redirect (this, &RouteTimeAxisView::add_existing_redirect_automation_curves);
1730
1731         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ) {
1732
1733                 list<RedirectAutomationInfo*>::iterator tmp;
1734
1735                 tmp = i;
1736                 ++tmp;
1737
1738                 if (!(*i)->valid) {
1739
1740                         delete *i;
1741                         redirect_automation.erase (i);
1742
1743                 } 
1744
1745                 i = tmp;
1746         }
1747
1748         /* change in visibility was possible */
1749
1750         _route->gui_changed ("track_height", this);
1751 }
1752
1753 RedirectAutomationLine *
1754 RouteTimeAxisView::find_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
1755 {
1756         RedirectAutomationNode* ran;
1757
1758         if ((ran = find_redirect_automation_node (redirect, what)) != 0) {
1759                 if (ran->view) {
1760                         return dynamic_cast<RedirectAutomationLine*> (ran->view->lines.front());
1761                 } 
1762         }
1763
1764         return 0;
1765 }
1766
1767 void
1768 RouteTimeAxisView::reset_redirect_automation_curves ()
1769 {
1770         for (vector<RedirectAutomationLine*>::iterator i = redirect_automation_curves.begin(); i != redirect_automation_curves.end(); ++i) {
1771                 (*i)->reset();
1772         }
1773 }
1774