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