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