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