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