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