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