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