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