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