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