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