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