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