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