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