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