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