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