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