Revert internals of the last layering-related commit, and go back a slightly-cleaned...
[ardour.git] / gtk2_ardour / time_axis_view.cc
1 /*
2     Copyright (C) 2000 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
20 #include <cstdlib>
21 #include <cmath>
22 #include <algorithm>
23 #include <string>
24 #include <list>
25
26 #include <libgnomecanvasmm.h>
27 #include <libgnomecanvasmm/canvas.h>
28 #include <libgnomecanvasmm/item.h>
29
30 #include "pbd/error.h"
31 #include "pbd/convert.h"
32
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/selector.h>
35
36 #include "ardour/session.h"
37 #include "ardour/utils.h"
38 #include "ardour/ladspa_plugin.h"
39 #include "ardour/processor.h"
40 #include "ardour/location.h"
41
42 #include "ardour_ui.h"
43 #include "global_signals.h"
44 #include "gui_thread.h"
45 #include "public_editor.h"
46 #include "time_axis_view.h"
47 #include "region_view.h"
48 #include "ghostregion.h"
49 #include "simplerect.h"
50 #include "simpleline.h"
51 #include "selection.h"
52 #include "keyboard.h"
53 #include "rgb_macros.h"
54 #include "utils.h"
55 #include "streamview.h"
56 #include "editor_drag.h"
57
58 #include "i18n.h"
59
60 using namespace std;
61 using namespace Gtk;
62 using namespace Gdk;
63 using namespace ARDOUR;
64 using namespace PBD;
65 using namespace Editing;
66 using namespace ArdourCanvas;
67 using Gtkmm2ext::Keyboard;
68
69 const double trim_handle_size = 6.0; /* pixels */
70 uint32_t TimeAxisView::button_height = 0;
71 uint32_t TimeAxisView::extra_height = 0;
72 int const TimeAxisView::_max_order = 512;
73 PBD::Signal1<void,TimeAxisView*> TimeAxisView::CatchDeletion;
74
75 TimeAxisView::TimeAxisView (ARDOUR::Session* sess, PublicEditor& ed, TimeAxisView* rent, Canvas& /*canvas*/)
76         : AxisView (sess)
77         , controls_table (2, 8)
78         , _size_menu (0)
79         , _y_position (0)
80         , _editor (ed)
81         , _order (0)
82         , _preresize_cursor (0)
83         , _have_preresize_cursor (false)
84         , _ghost_group (0)
85 {
86         if (extra_height == 0) {
87                 compute_heights ();
88         }
89
90         _canvas_background = new Group (*ed.get_background_group (), 0.0, 0.0);
91         _canvas_display = new Group (*ed.get_trackview_group (), 0.0, 0.0);
92         _canvas_display->hide(); // reveal as needed
93
94         selection_group = new Group (*_canvas_display);
95         selection_group->set_data (X_("timeselection"), (void *) 1);
96         selection_group->hide();
97
98         _ghost_group = new Group (*_canvas_display);
99         _ghost_group->lower_to_bottom();
100         _ghost_group->show();
101
102         control_parent = 0;
103         display_menu = 0;
104         _hidden = false;
105         in_destructor = false;
106         height = 0;
107         _effective_height = 0;
108         parent = rent;
109         last_name_entry_key_press_event = 0;
110         name_packing = NamePackingBits (0);
111         _resize_drag_start = -1;
112
113         /*
114           Create the standard LHS Controls
115           We create the top-level container and name add the name label here,
116           subclasses can add to the layout as required
117         */
118
119         name_entry.set_name ("EditorTrackNameDisplay");
120         name_entry.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_button_release));
121         name_entry.signal_button_press_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_button_press));
122         name_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_key_release));
123         name_entry.signal_activate().connect (sigc::mem_fun(*this, &TimeAxisView::name_entry_activated));
124         name_entry.signal_focus_in_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_focus_in));
125         name_entry.signal_focus_out_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_focus_out));
126         Gtkmm2ext::set_size_request_to_display_given_text (name_entry, N_("gTortnam"), 10, 10); // just represents a short name
127
128         name_label.set_name ("TrackLabel");
129         name_label.set_alignment (0.0, 0.5);
130
131         /* typically, either name_label OR name_entry are visible,
132            but not both. its up to derived classes to show/hide them as they
133            wish.
134         */
135
136         name_hbox.show ();
137
138         controls_table.set_size_request (200);
139         controls_table.set_row_spacings (2);
140         controls_table.set_col_spacings (2);
141         controls_table.set_border_width (2);
142         controls_table.set_homogeneous (true);
143
144         controls_table.attach (name_hbox, 0, 5, 0, 1,  Gtk::FILL|Gtk::EXPAND,  Gtk::FILL|Gtk::EXPAND, 3, 0);
145         controls_table.show_all ();
146         controls_table.set_no_show_all ();
147
148         HSeparator* separator = manage (new HSeparator());
149
150         controls_vbox.pack_start (controls_table, false, false);
151         controls_vbox.show ();
152
153         //controls_ebox.set_name ("TimeAxisViewControlsBaseUnselected");
154         controls_ebox.add (controls_vbox);
155         controls_ebox.add_events (Gdk::BUTTON_PRESS_MASK|
156                                   Gdk::BUTTON_RELEASE_MASK|
157                                   Gdk::POINTER_MOTION_MASK|
158                                   Gdk::ENTER_NOTIFY_MASK|
159                                   Gdk::LEAVE_NOTIFY_MASK|
160                                   Gdk::SCROLL_MASK);
161         controls_ebox.set_flags (CAN_FOCUS);
162
163         /* note that this handler connects *before* the default handler */
164         controls_ebox.signal_scroll_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_scroll), true);
165         controls_ebox.signal_button_press_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_button_press));
166         controls_ebox.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_button_release));
167         controls_ebox.signal_motion_notify_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_motion));
168         controls_ebox.signal_leave_notify_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_leave));
169         controls_ebox.show ();
170
171         controls_hbox.pack_start (controls_ebox, true, true);
172         controls_hbox.show ();
173
174         time_axis_vbox.pack_start (controls_hbox, true, true);
175         time_axis_vbox.pack_end (*separator, false, false);
176         time_axis_vbox.show();
177
178         ColorsChanged.connect (sigc::mem_fun (*this, &TimeAxisView::color_handler));
179
180         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&TimeAxisView::erase_ghost, this, _1), gui_context());
181 }
182
183 TimeAxisView::~TimeAxisView()
184 {
185         in_destructor = true;
186
187         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
188                 delete *i;
189         }
190
191         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
192                 delete (*i)->rect;
193                 delete (*i)->start_trim;
194                 delete (*i)->end_trim;
195
196         }
197
198         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
199                 delete (*i)->rect;
200                 delete (*i)->start_trim;
201                 delete (*i)->end_trim;
202         }
203
204         delete selection_group;
205         selection_group = 0;
206
207         delete _canvas_background;
208         _canvas_background = 0;
209
210         delete _canvas_display;
211         _canvas_display = 0;
212
213         delete display_menu;
214         display_menu = 0;
215
216         delete _size_menu;
217 }
218
219 void
220 TimeAxisView::hide ()
221 {
222         if (_hidden) {
223                 return;
224         }
225
226         _canvas_display->hide ();
227         _canvas_background->hide ();
228
229         if (control_parent) {
230                 control_parent->remove (time_axis_vbox);
231                 control_parent = 0;
232         }
233
234         _y_position = -1;
235         _hidden = true;
236
237         /* now hide children */
238
239         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
240                 (*i)->hide ();
241         }
242
243         /* if its hidden, it cannot be selected */
244         _editor.get_selection().remove (this);
245         /* and neither can its regions */
246         _editor.get_selection().remove_regions (this);
247
248         Hiding ();
249 }
250
251 /** Display this TimeAxisView as the nth component of the parent box, at y.
252 *
253 * @param y y position.
254 * @param nth index for this TimeAxisView, increased if this view has children.
255 * @param parent parent component.
256 * @return height of this TimeAxisView.
257 */
258 guint32
259 TimeAxisView::show_at (double y, int& nth, VBox *parent)
260 {
261         if (control_parent) {
262                 control_parent->reorder_child (time_axis_vbox, nth);
263         } else {
264                 control_parent = parent;
265                 parent->pack_start (time_axis_vbox, false, false);
266                 parent->reorder_child (time_axis_vbox, nth);
267         }
268
269         _order = nth;
270
271         if (_y_position != y) {
272                 _canvas_display->property_y () = y;
273                 _canvas_background->property_y () = y;
274                 /* silly canvas */
275                 _canvas_display->move (0.0, 0.0);
276                 _canvas_background->move (0.0, 0.0);
277                 _y_position = y;
278
279         }
280
281         _canvas_background->raise_to_top ();
282         _canvas_display->raise_to_top ();
283
284         _canvas_background->show ();
285         _canvas_display->show ();
286
287         _hidden = false;
288
289         _effective_height = current_height ();
290
291         /* now show relevant children */
292
293         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
294                 if ((*i)->marked_for_display()) {
295                         ++nth;
296                         _effective_height += (*i)->show_at (y + _effective_height, nth, parent);
297                 } else {
298                         (*i)->hide ();
299                 }
300         }
301
302         return _effective_height;
303 }
304
305 void
306 TimeAxisView::clip_to_viewport ()
307 {
308         if (marked_for_display()) {
309                 if (_y_position + _effective_height < _editor.get_trackview_group_vertical_offset () || _y_position > _editor.get_trackview_group_vertical_offset () + _canvas_display->get_canvas()->get_height()) {
310                         _canvas_background->hide ();
311                         _canvas_display->hide ();
312                         return;
313                 }
314                 _canvas_background->show ();
315                 _canvas_display->show ();
316         }
317         return;
318 }
319
320 bool
321 TimeAxisView::controls_ebox_scroll (GdkEventScroll* ev)
322 {
323         switch (ev->direction) {
324         case GDK_SCROLL_UP:
325                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
326                         step_height (false);
327                         return true;
328                 } else if (Keyboard::no_modifiers_active (ev->state)) {
329                         _editor.scroll_tracks_up_line();
330                         return true;
331                 }
332                 break;
333
334         case GDK_SCROLL_DOWN:
335                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
336                         step_height (true);
337                         return true;
338                 } else if (Keyboard::no_modifiers_active (ev->state)) {
339                         _editor.scroll_tracks_down_line();
340                         return true;
341                 }
342                 break;
343
344         default:
345                 /* no handling for left/right, yet */
346                 break;
347         }
348
349         return false;
350 }
351
352 bool
353 TimeAxisView::controls_ebox_button_press (GdkEventButton* event)
354 {
355         if (maybe_set_cursor (event->y) > 0) {
356                 _resize_drag_start = event->y_root;
357         }
358
359         return true;
360 }
361
362 void
363 TimeAxisView::idle_resize (uint32_t h)
364 {
365         set_height (h);
366 }
367
368
369 bool
370 TimeAxisView::controls_ebox_motion (GdkEventMotion* ev)
371 {
372         if (_resize_drag_start >= 0) {
373                 /* (ab)use the DragManager to do autoscrolling; adjust the event coordinates
374                    into the world coordinate space that DragManager::motion_handler is expecting,
375                    and then fake a DragManager motion event so that when maybe_autoscroll
376                    asks DragManager for the current pointer position it will get the correct
377                    answers.
378                 */
379                 int tx, ty;
380                 controls_ebox.translate_coordinates (*control_parent, ev->x, ev->y, tx, ty);
381                 ev->y = ty - _editor.get_trackview_group_vertical_offset();
382                 _editor.drags()->motion_handler ((GdkEvent *) ev, false);
383                 _editor.maybe_autoscroll (false, true);
384
385                 /* now do the actual TAV resize */
386                 int32_t const delta = (int32_t) floor (ev->y_root - _resize_drag_start);
387                 _editor.add_to_idle_resize (this, delta);
388                 _resize_drag_start = ev->y_root;
389         } else {
390                 /* not dragging but ... */
391                 maybe_set_cursor (ev->y);
392         }
393
394         return true;
395 }
396
397 bool
398 TimeAxisView::controls_ebox_leave (GdkEventCrossing*)
399 {
400         if (_have_preresize_cursor) {
401                 gdk_window_set_cursor (controls_ebox.get_window()->gobj(), _preresize_cursor);
402                 _have_preresize_cursor = false;
403         }
404         return true;
405 }
406
407 bool
408 TimeAxisView::maybe_set_cursor (int y)
409 {
410         /* XXX no Gtkmm Gdk::Window::get_cursor() */
411         Glib::RefPtr<Gdk::Window> win = controls_ebox.get_window();
412
413         if (y > (gint) floor (controls_ebox.get_height() * 0.75)) {
414
415                 /* y-coordinate in lower 25% */
416
417                 if (!_have_preresize_cursor) {
418                         _preresize_cursor = gdk_window_get_cursor (win->gobj());
419                         _have_preresize_cursor = true;
420                         win->set_cursor (Gdk::Cursor(Gdk::SB_V_DOUBLE_ARROW));
421                 }
422
423                 return 1;
424
425         } else if (_have_preresize_cursor) {
426                 gdk_window_set_cursor (win->gobj(), _preresize_cursor);
427                 _have_preresize_cursor = false;
428
429                 return -1;
430         }
431
432         return 0;
433 }
434
435 bool
436 TimeAxisView::controls_ebox_button_release (GdkEventButton* ev)
437 {
438         if (_resize_drag_start >= 0) {
439                 if (_have_preresize_cursor) {
440                         gdk_window_set_cursor (controls_ebox.get_window()->gobj(), _preresize_cursor);
441                         _preresize_cursor = 0;
442                         _have_preresize_cursor = false;
443                 }
444                 _editor.stop_canvas_autoscroll ();
445                 _resize_drag_start = -1;
446         }
447
448         switch (ev->button) {
449         case 1:
450                 selection_click (ev);
451                 break;
452
453         case 3:
454                 popup_display_menu (ev->time);
455                 break;
456         }
457
458         return true;
459 }
460
461 void
462 TimeAxisView::selection_click (GdkEventButton* ev)
463 {
464         Selection::Operation op = ArdourKeyboard::selection_type (ev->state);
465         _editor.set_selected_track (*this, op, false);
466 }
467
468
469 /** Steps through the defined heights for this TrackView.
470  *  @param coarser true if stepping should decrease in size, otherwise false.
471  */
472 void
473 TimeAxisView::step_height (bool coarser)
474 {
475         static const uint32_t step = 25;
476
477         if (coarser) {
478
479                 if (height <= preset_height (HeightSmall)) {
480                         return;
481                 } else if (height <= preset_height (HeightNormal) && height > preset_height (HeightSmall)) {
482                         set_height_enum (HeightSmall);
483                 } else {
484                         set_height (height - step);
485                 }
486
487         } else {
488
489                 if (height <= preset_height(HeightSmall)) {
490                         set_height_enum (HeightNormal);
491                 } else {
492                         set_height (height + step);
493                 }
494
495         }
496 }
497
498 void
499 TimeAxisView::set_heights (uint32_t h)
500 {
501         TrackSelection& ts (_editor.get_selection().tracks);
502
503         for (TrackSelection::iterator i = ts.begin(); i != ts.end(); ++i) {
504                 (*i)->set_height (h);
505         }
506 }
507
508 void
509 TimeAxisView::set_height_enum (Height h, bool apply_to_selection)
510 {
511         if (apply_to_selection) {
512                 _editor.get_selection().tracks.foreach_time_axis (boost::bind (&TimeAxisView::set_height_enum, _1, h, false));
513         } else {
514                 set_height (preset_height (h));
515         }
516 }
517
518 void
519 TimeAxisView::set_height (uint32_t h)
520 {
521         if (h < preset_height (HeightSmall)) {
522                 h = preset_height (HeightSmall);
523         }
524
525         time_axis_vbox.property_height_request () = h;
526         height = h;
527
528         char buf[32];
529         snprintf (buf, sizeof (buf), "%u", height);
530         set_gui_property ("height", buf);
531
532         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
533                 (*i)->set_height ();
534         }
535
536         if (canvas_item_visible (selection_group)) {
537                 /* resize the selection rect */
538                 show_selection (_editor.get_selection().time);
539         }
540 }
541
542 bool
543 TimeAxisView::name_entry_key_release (GdkEventKey* ev)
544 {
545         TrackViewList::iterator i;
546
547         switch (ev->keyval) {
548         case GDK_Escape:
549                 name_entry.select_region (0,0);
550                 controls_ebox.grab_focus ();
551                 name_entry_changed ();
552                 return true;
553
554         /* Shift+Tab Keys Pressed. Note that for Shift+Tab, GDK actually
555          * generates a different ev->keyval, rather than setting
556          * ev->state.
557          */
558         case GDK_ISO_Left_Tab:
559         case GDK_Tab:
560         {
561                 name_entry_changed ();
562                 TrackViewList const & allviews = _editor.get_track_views ();
563                 TrackViewList::const_iterator i = find (allviews.begin(), allviews.end(), this);
564
565                 if (ev->keyval == GDK_Tab) {
566                         if (i != allviews.end()) {
567                                 do {
568                                         if (++i == allviews.end()) {
569                                                 return true;
570                                         }
571
572                                         RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*>(*i);
573
574                                         if (rtav && rtav->route()->record_enabled()) {
575                                                 continue;
576                                         }
577
578                                         if (!(*i)->hidden()) {
579                                                 break;
580                                         }
581
582                                 } while (true);
583                         }
584                 } else {
585                         if (i != allviews.begin()) {
586                                 do {
587                                         if (i == allviews.begin()) {
588                                                 return true;
589                                         }
590
591                                         --i;
592
593                                         RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*>(*i);
594
595                                         if (rtav && rtav->route()->record_enabled()) {
596                                                 continue;
597                                         }
598
599                                         if (!(*i)->hidden()) {
600                                                 break;
601                                         }
602
603                                 } while (true);
604                         }
605                 }
606
607                 if ((i != allviews.end()) && (*i != this) && !(*i)->hidden()) {
608                         (*i)->name_entry.grab_focus();
609                         _editor.ensure_time_axis_view_is_visible (**i);
610                 }
611         }
612         return true;
613
614         case GDK_Up:
615         case GDK_Down:
616                 name_entry_changed ();
617                 return true;
618
619         default:
620                 break;
621         }
622
623 #ifdef TIMEOUT_NAME_EDIT
624         /* adapt the timeout to reflect the user's typing speed */
625
626         guint32 name_entry_timeout;
627
628         if (last_name_entry_key_press_event) {
629                 /* timeout is 1/2 second or 5 times their current inter-char typing speed */
630                 name_entry_timeout = std::max (500U, (5 * (ev->time - last_name_entry_key_press_event)));
631         } else {
632                 /* start with a 1 second timeout */
633                 name_entry_timeout = 1000;
634         }
635
636         last_name_entry_key_press_event = ev->time;
637
638         /* wait 1 seconds and if no more keys are pressed, act as if they pressed enter */
639
640         name_entry_key_timeout.disconnect();
641         name_entry_key_timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_key_timed_out), name_entry_timeout);
642 #endif
643
644         return false;
645 }
646
647 bool
648 TimeAxisView::name_entry_focus_in (GdkEventFocus*)
649 {
650         name_entry.select_region (0, -1);
651         name_entry.set_state (STATE_SELECTED);
652         return false;
653 }
654
655 bool
656 TimeAxisView::name_entry_focus_out (GdkEventFocus*)
657 {
658         /* clean up */
659
660         last_name_entry_key_press_event = 0;
661         name_entry_key_timeout.disconnect ();
662         name_entry.select_region (0,0);
663         name_entry.set_state (STATE_NORMAL);
664
665         /* do the real stuff */
666
667         name_entry_changed ();
668
669         return false;
670 }
671
672 bool
673 TimeAxisView::name_entry_key_timed_out ()
674 {
675         name_entry_activated();
676         return false;
677 }
678
679 void
680 TimeAxisView::name_entry_activated ()
681 {
682         controls_ebox.grab_focus();
683 }
684
685 void
686 TimeAxisView::name_entry_changed ()
687 {
688 }
689
690 bool
691 TimeAxisView::name_entry_button_press (GdkEventButton *ev)
692 {
693         if (ev->button == 3) {
694                 return true;
695         }
696         return false;
697 }
698
699 bool
700 TimeAxisView::name_entry_button_release (GdkEventButton *ev)
701 {
702         if (ev->button == 3) {
703                 popup_display_menu (ev->time);
704                 return true;
705         }
706         return false;
707 }
708
709 void
710 TimeAxisView::conditionally_add_to_selection ()
711 {
712         Selection& s (_editor.get_selection ());
713
714         if (!s.selected (this)) {
715                 _editor.set_selected_track (*this, Selection::Set);
716         }
717 }
718
719 void
720 TimeAxisView::popup_display_menu (guint32 when)
721 {
722         conditionally_add_to_selection ();
723
724         build_display_menu ();
725         display_menu->popup (1, when);
726 }
727
728 void
729 TimeAxisView::set_selected (bool yn)
730 {
731         if (yn == _selected) {
732                 return;
733         }
734
735         Selectable::set_selected (yn);
736
737         if (_selected) {
738                 controls_ebox.set_name (controls_base_selected_name);
739                 time_axis_vbox.set_name (controls_base_selected_name);
740                 controls_vbox.set_name (controls_base_selected_name);
741         } else {
742                 controls_ebox.set_name (controls_base_unselected_name);
743                 time_axis_vbox.set_name (controls_base_unselected_name);
744                 controls_vbox.set_name (controls_base_unselected_name);
745                 hide_selection ();
746
747                 /* children will be set for the yn=true case. but when deselecting
748                    the editor only has a list of top-level trackviews, so we
749                    have to do this here.
750                 */
751
752                 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
753                         (*i)->set_selected (false);
754                 }
755         }
756 }
757
758 void
759 TimeAxisView::build_display_menu ()
760 {
761         using namespace Menu_Helpers;
762
763         delete display_menu;
764
765         display_menu = new Menu;
766         display_menu->set_name ("ArdourContextMenu");
767
768         // Just let implementing classes define what goes into the manu
769 }
770
771 void
772 TimeAxisView::set_samples_per_unit (double spu)
773 {
774         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
775                 (*i)->set_samples_per_unit (spu);
776         }
777
778         AnalysisFeatureList::const_iterator i;
779         list<ArdourCanvas::SimpleLine*>::iterator l;
780 }
781
782 void
783 TimeAxisView::show_timestretch (framepos_t start, framepos_t end)
784 {
785         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
786                 (*i)->show_timestretch (start, end);
787         }
788 }
789
790 void
791 TimeAxisView::hide_timestretch ()
792 {
793         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
794                 (*i)->hide_timestretch ();
795         }
796 }
797
798 void
799 TimeAxisView::show_selection (TimeSelection& ts)
800 {
801         double x1;
802         double x2;
803         double y2;
804         SelectionRect *rect;
805
806         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
807                 (*i)->show_selection (ts);
808         }
809
810         if (canvas_item_visible (selection_group)) {
811                 while (!used_selection_rects.empty()) {
812                         free_selection_rects.push_front (used_selection_rects.front());
813                         used_selection_rects.pop_front();
814                         free_selection_rects.front()->rect->hide();
815                         free_selection_rects.front()->start_trim->hide();
816                         free_selection_rects.front()->end_trim->hide();
817                 }
818                 selection_group->hide();
819         }
820
821         selection_group->show();
822         selection_group->raise_to_top();
823
824         for (list<AudioRange>::iterator i = ts.begin(); i != ts.end(); ++i) {
825                 framepos_t start, end;
826                 framecnt_t cnt;
827
828                 start = (*i).start;
829                 end = (*i).end;
830                 cnt = end - start + 1;
831
832                 rect = get_selection_rect ((*i).id);
833
834                 x1 = _editor.frame_to_unit (start);
835                 x2 = _editor.frame_to_unit (start + cnt - 1);
836                 y2 = current_height();
837
838                 rect->rect->property_x1() = x1;
839                 rect->rect->property_y1() = 1.0;
840                 rect->rect->property_x2() = x2;
841                 rect->rect->property_y2() = y2;
842
843                 // trim boxes are at the top for selections
844
845                 if (x2 > x1) {
846                         rect->start_trim->property_x1() = x1;
847                         rect->start_trim->property_y1() = 1.0;
848                         rect->start_trim->property_x2() = x1 + trim_handle_size;
849                         rect->start_trim->property_y2() = y2;
850
851                         rect->end_trim->property_x1() = x2 - trim_handle_size;
852                         rect->end_trim->property_y1() = 1.0;
853                         rect->end_trim->property_x2() = x2;
854                         rect->end_trim->property_y2() = y2;
855
856                         rect->start_trim->show();
857                         rect->end_trim->show();
858                 } else {
859                         rect->start_trim->hide();
860                         rect->end_trim->hide();
861                 }
862
863                 rect->rect->show ();
864                 used_selection_rects.push_back (rect);
865         }
866 }
867
868 void
869 TimeAxisView::reshow_selection (TimeSelection& ts)
870 {
871         show_selection (ts);
872
873         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
874                 (*i)->show_selection (ts);
875         }
876 }
877
878 void
879 TimeAxisView::hide_selection ()
880 {
881         if (canvas_item_visible (selection_group)) {
882                 while (!used_selection_rects.empty()) {
883                         free_selection_rects.push_front (used_selection_rects.front());
884                         used_selection_rects.pop_front();
885                         free_selection_rects.front()->rect->hide();
886                         free_selection_rects.front()->start_trim->hide();
887                         free_selection_rects.front()->end_trim->hide();
888                 }
889                 selection_group->hide();
890         }
891
892         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
893                 (*i)->hide_selection ();
894         }
895 }
896
897 void
898 TimeAxisView::order_selection_trims (ArdourCanvas::Item *item, bool put_start_on_top)
899 {
900         /* find the selection rect this is for. we have the item corresponding to one
901            of the trim handles.
902          */
903
904         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
905                 if ((*i)->start_trim == item || (*i)->end_trim == item) {
906
907                         /* make one trim handle be "above" the other so that if they overlap,
908                            the top one is the one last used.
909                         */
910
911                         (*i)->rect->raise_to_top ();
912                         (put_start_on_top ? (*i)->start_trim : (*i)->end_trim)->raise_to_top ();
913                         (put_start_on_top ? (*i)->end_trim : (*i)->start_trim)->raise_to_top ();
914
915                         break;
916                 }
917         }
918 }
919
920 SelectionRect *
921 TimeAxisView::get_selection_rect (uint32_t id)
922 {
923         SelectionRect *rect;
924
925         /* check to see if we already have a visible rect for this particular selection ID */
926
927         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
928                 if ((*i)->id == id) {
929                         return (*i);
930                 }
931         }
932
933         /* ditto for the free rect list */
934
935         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
936                 if ((*i)->id == id) {
937                         SelectionRect* ret = (*i);
938                         free_selection_rects.erase (i);
939                         return ret;
940                 }
941         }
942
943         /* no existing matching rect, so go get a new one from the free list, or create one if there are none */
944
945         if (free_selection_rects.empty()) {
946
947                 rect = new SelectionRect;
948
949                 rect->rect = new SimpleRect (*selection_group);
950                 rect->rect->property_outline_what() = 0x0;
951                 rect->rect->property_x1() = 0.0;
952                 rect->rect->property_y1() = 0.0;
953                 rect->rect->property_x2() = 0.0;
954                 rect->rect->property_y2() = 0.0;
955                 rect->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
956
957                 rect->start_trim = new SimpleRect (*selection_group);
958                 rect->start_trim->property_outline_what() = 0x0;
959                 rect->start_trim->property_x1() = 0.0;
960                 rect->start_trim->property_x2() = 0.0;
961
962                 rect->end_trim = new SimpleRect (*selection_group);
963                 rect->end_trim->property_outline_what() = 0x0;
964                 rect->end_trim->property_x1() = 0.0;
965                 rect->end_trim->property_x2() = 0.0;
966
967                 free_selection_rects.push_front (rect);
968
969                 rect->rect->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_rect_event), rect->rect, rect));
970                 rect->start_trim->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_start_trim_event), rect->rect, rect));
971                 rect->end_trim->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_end_trim_event), rect->rect, rect));
972         }
973
974         rect = free_selection_rects.front();
975         rect->id = id;
976         free_selection_rects.pop_front();
977         return rect;
978 }
979
980 struct null_deleter { void operator()(void const *) const {} };
981
982 bool
983 TimeAxisView::is_child (TimeAxisView* tav)
984 {
985         return find (children.begin(), children.end(), boost::shared_ptr<TimeAxisView>(tav, null_deleter())) != children.end();
986 }
987
988 void
989 TimeAxisView::add_child (boost::shared_ptr<TimeAxisView> child)
990 {
991         children.push_back (child);
992 }
993
994 void
995 TimeAxisView::remove_child (boost::shared_ptr<TimeAxisView> child)
996 {
997         Children::iterator i;
998
999         if ((i = find (children.begin(), children.end(), child)) != children.end()) {
1000                 children.erase (i);
1001         }
1002 }
1003
1004 /** Get selectable things within a given range.
1005  *  @param start Start time in session frames.
1006  *  @param end End time in session frames.
1007  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
1008  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
1009  *  @param result Filled in with selectable things.
1010  */
1011 void
1012 TimeAxisView::get_selectables (framepos_t /*start*/, framepos_t /*end*/, double /*top*/, double /*bot*/, list<Selectable*>& /*result*/)
1013 {
1014         return;
1015 }
1016
1017 void
1018 TimeAxisView::get_inverted_selectables (Selection& /*sel*/, list<Selectable*>& /*result*/)
1019 {
1020         return;
1021 }
1022
1023 void
1024 TimeAxisView::add_ghost (RegionView* rv)
1025 {
1026         GhostRegion* gr = rv->add_ghost (*this);
1027
1028         if (gr) {
1029                 ghosts.push_back(gr);
1030         }
1031 }
1032
1033 void
1034 TimeAxisView::remove_ghost (RegionView* rv)
1035 {
1036         rv->remove_ghost_in (*this);
1037 }
1038
1039 void
1040 TimeAxisView::erase_ghost (GhostRegion* gr)
1041 {
1042         if (in_destructor) {
1043                 return;
1044         }
1045
1046         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1047                 if ((*i) == gr) {
1048                         ghosts.erase (i);
1049                         break;
1050                 }
1051         }
1052 }
1053
1054 bool
1055 TimeAxisView::touched (double top, double bot)
1056 {
1057         /* remember: this is X Window - coordinate space starts in upper left and moves down.
1058           y_position is the "origin" or "top" of the track.
1059         */
1060
1061         double mybot = _y_position + current_height();
1062
1063         return ((_y_position <= bot && _y_position >= top) ||
1064                 ((mybot <= bot) && (top < mybot)) ||
1065                 (mybot >= bot && _y_position < top));
1066 }
1067
1068 void
1069 TimeAxisView::set_parent (TimeAxisView& p)
1070 {
1071         parent = &p;
1072 }
1073
1074 void
1075 TimeAxisView::reset_height ()
1076 {
1077         set_height (height);
1078
1079         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1080                 (*i)->set_height ((*i)->height);
1081         }
1082 }
1083
1084 void
1085 TimeAxisView::compute_heights ()
1086 {
1087         Gtk::Window window (Gtk::WINDOW_TOPLEVEL);
1088         Gtk::Table two_row_table (2, 8);
1089         Gtk::Table one_row_table (1, 8);
1090         Button* buttons[5];
1091         const int border_width = 2;
1092
1093         const int separator_height = 2;
1094         extra_height = (2 * border_width) + separator_height;
1095
1096         window.add (one_row_table);
1097
1098         one_row_table.set_border_width (border_width);
1099         one_row_table.set_row_spacings (0);
1100         one_row_table.set_col_spacings (0);
1101         one_row_table.set_homogeneous (true);
1102
1103         two_row_table.set_border_width (border_width);
1104         two_row_table.set_row_spacings (0);
1105         two_row_table.set_col_spacings (0);
1106         two_row_table.set_homogeneous (true);
1107
1108         for (int i = 0; i < 5; ++i) {
1109                 buttons[i] = manage (new Button (X_("f")));
1110                 buttons[i]->set_name ("TrackMuteButton");
1111         }
1112
1113         one_row_table.attach (*buttons[0], 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
1114
1115         one_row_table.show_all ();
1116         Gtk::Requisition req(one_row_table.size_request ());
1117
1118         // height required to show 1 row of buttons
1119         button_height = req.height;
1120 }
1121
1122 void
1123 TimeAxisView::show_name_label ()
1124 {
1125         if (!(name_packing & NameLabelPacked)) {
1126                 name_hbox.pack_start (name_label, true, true);
1127                 name_packing = NamePackingBits (name_packing | NameLabelPacked);
1128                 name_hbox.show ();
1129                 name_label.show ();
1130         }
1131 }
1132
1133 void
1134 TimeAxisView::show_name_entry ()
1135 {
1136         if (!(name_packing & NameEntryPacked)) {
1137                 name_hbox.pack_start (name_entry, true, true);
1138                 name_packing = NamePackingBits (name_packing | NameEntryPacked);
1139                 name_hbox.show ();
1140                 name_entry.show ();
1141         }
1142 }
1143
1144 void
1145 TimeAxisView::hide_name_label ()
1146 {
1147         if (name_packing & NameLabelPacked) {
1148                 name_hbox.remove (name_label);
1149                 name_packing = NamePackingBits (name_packing & ~NameLabelPacked);
1150         }
1151 }
1152
1153 void
1154 TimeAxisView::hide_name_entry ()
1155 {
1156         if (name_packing & NameEntryPacked) {
1157                 name_hbox.remove (name_entry);
1158                 name_packing = NamePackingBits (name_packing & ~NameEntryPacked);
1159         }
1160 }
1161
1162 void
1163 TimeAxisView::color_handler ()
1164 {
1165         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); i++) {
1166                 (*i)->set_colors();
1167         }
1168
1169         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
1170
1171                 (*i)->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
1172                 (*i)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1173
1174                 (*i)->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1175                 (*i)->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1176
1177                 (*i)->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1178                 (*i)->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1179         }
1180
1181         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
1182
1183                 (*i)->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
1184                 (*i)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1185
1186                 (*i)->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1187                 (*i)->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1188
1189                 (*i)->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1190                 (*i)->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1191         }
1192 }
1193
1194 /** @return Pair: TimeAxisView, layer index.
1195  * TimeAxisView is non-0 if this object covers y, or one of its children does.
1196  * If the covering object is a child axis, then the child is returned.
1197  * TimeAxisView is 0 otherwise.
1198  * Layer index is the layer number (possibly fractional) if the TimeAxisView is valid
1199  * and is in stacked or expanded * region display mode, otherwise 0.
1200  */
1201 std::pair<TimeAxisView*, double>
1202 TimeAxisView::covers_y_position (double y)
1203 {
1204         if (hidden()) {
1205                 return std::make_pair ((TimeAxisView *) 0, 0);
1206         }
1207
1208         if (_y_position <= y && y < (_y_position + height)) {
1209
1210                 /* work out the layer index if appropriate */
1211                 double l = 0;
1212                 switch (layer_display ()) {
1213                 case Overlaid:
1214                         break;
1215                 case Stacked:
1216                         if (view ()) {
1217                                 /* compute layer */
1218                                 l = layer_t ((_y_position + height - y) / (view()->child_height ()));
1219                                 /* clamp to max layers to be on the safe side; sometimes the above calculation
1220                                    returns a too-high value */
1221                                 if (l >= view()->layers ()) {
1222                                         l = view()->layers() - 1;
1223                                 }
1224                         }
1225                         break;
1226                 case Expanded:
1227                         if (view ()) {
1228                                 int const n = floor ((_y_position + height - y) / (view()->child_height ()));
1229                                 l = n * 0.5 - 0.5;
1230                                 if (l >= (view()->layers() - 0.5)) {
1231                                         l = view()->layers() - 0.5;
1232                                 }
1233                         }
1234                         break;
1235                 }
1236
1237                 return std::make_pair (this, l);
1238         }
1239
1240         for (Children::const_iterator i = children.begin(); i != children.end(); ++i) {
1241
1242                 std::pair<TimeAxisView*, int> const r = (*i)->covers_y_position (y);
1243                 if (r.first) {
1244                         return r;
1245                 }
1246         }
1247
1248         return std::make_pair ((TimeAxisView *) 0, 0);
1249 }
1250
1251
1252 uint32_t
1253 TimeAxisView::preset_height (Height h)
1254 {
1255         switch (h) {
1256         case HeightLargest:
1257                 return (button_height * 2) + extra_height + 250;
1258         case HeightLarger:
1259                 return (button_height * 2) + extra_height + 150;
1260         case HeightLarge:
1261                 return (button_height * 2) + extra_height + 50;
1262         case HeightNormal:
1263                 return (button_height * 2) + extra_height;
1264         case HeightSmall:
1265                 return button_height + extra_height;
1266         }
1267
1268         /* NOTREACHED */
1269         return 0;
1270 }
1271
1272 /** @return Child time axis views that are not hidden */
1273 TimeAxisView::Children
1274 TimeAxisView::get_child_list ()
1275 {
1276         Children c;
1277
1278         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1279                 if (!(*i)->hidden()) {
1280                         c.push_back(*i);
1281                 }
1282         }
1283
1284         return c;
1285 }
1286
1287 void
1288 TimeAxisView::build_size_menu ()
1289 {
1290         if (_size_menu && _size_menu->gobj ()) {
1291                 return;
1292         }
1293
1294         delete _size_menu;
1295
1296         using namespace Menu_Helpers;
1297
1298         _size_menu = new Menu;
1299         _size_menu->set_name ("ArdourContextMenu");
1300         MenuList& items = _size_menu->items();
1301
1302         items.push_back (MenuElem (_("Largest"), sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightLargest, true)));
1303         items.push_back (MenuElem (_("Larger"),  sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightLarger, true)));
1304         items.push_back (MenuElem (_("Large"),   sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightLarge, true)));
1305         items.push_back (MenuElem (_("Normal"),  sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightNormal, true)));
1306         items.push_back (MenuElem (_("Small"),   sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightSmall, true)));
1307 }
1308
1309 void
1310 TimeAxisView::reset_visual_state ()
1311 {
1312         /* this method is not required to trigger a global redraw */
1313
1314         string str = gui_property ("height");
1315
1316         if (!str.empty()) {
1317                 set_height (atoi (str));
1318         } else {
1319                 set_height (preset_height (HeightNormal));
1320         }
1321 }
1322
1323 TrackViewList
1324 TrackViewList::filter_to_unique_playlists ()
1325 {
1326         std::set<boost::shared_ptr<ARDOUR::Playlist> > playlists;
1327         TrackViewList ts;
1328
1329         for (iterator i = begin(); i != end(); ++i) {
1330                 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*i);
1331                 if (!rtav) {
1332                         /* not a route: include it anyway */
1333                         ts.push_back (*i);
1334                 } else {
1335                         boost::shared_ptr<ARDOUR::Track> t = rtav->track();
1336                         if (t) {
1337                                 if (playlists.insert (t->playlist()).second) {
1338                                         /* playlist not seen yet */
1339                                         ts.push_back (*i);
1340                                 }
1341                         } else {
1342                                 /* not a track: include it anyway */
1343                                 ts.push_back (*i);
1344                         }
1345                 }
1346         }
1347         return ts;
1348 }