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