change selection behaviour for track header context click
[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_ETCHED_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 + 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 + ((order == 0) ? 1 : 0));
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
519         if (!get_selected()) {
520                 editor.set_selected_track (*this, Selection::Set);
521         }
522
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         AxisView::set_selected (yn);
547
548         if (_selected) {
549                 controls_ebox.set_name (controls_base_selected_name);
550                 controls_frame.set_name (controls_base_selected_name);
551
552                 /* propagate any existing selection, if the mode is right */
553
554                 if (editor.current_mouse_mode() == Editing::MouseRange && !editor.get_selection().time.empty()) {
555                         show_selection (editor.get_selection().time);
556                 }
557
558         } else {
559                 controls_ebox.set_name (controls_base_unselected_name);
560                 controls_frame.set_name (controls_base_unselected_name);
561
562                 hide_selection ();
563
564                 /* children will be set for the yn=true case. but when deselecting
565                    the editor only has a list of top-level trackviews, so we
566                    have to do this here.
567                 */
568
569                 for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
570                         (*i)->set_selected (false);
571                 }
572
573                 
574         }
575 }
576
577 void
578 TimeAxisView::build_size_menu ()
579 {
580         using namespace Menu_Helpers;
581
582         size_menu = new Menu;
583         size_menu->set_name ("ArdourContextMenu");
584         MenuList& items = size_menu->items();
585         
586         items.push_back (MenuElem (_("Largest"), bind (mem_fun (*this, &TimeAxisView::set_height), Largest)));
587         items.push_back (MenuElem (_("Large"), bind (mem_fun (*this, &TimeAxisView::set_height), Large)));
588         items.push_back (MenuElem (_("Larger"), bind (mem_fun (*this, &TimeAxisView::set_height), Larger)));
589         items.push_back (MenuElem (_("Normal"), bind (mem_fun (*this, &TimeAxisView::set_height), Normal)));
590         items.push_back (MenuElem (_("Smaller"), bind (mem_fun (*this, &TimeAxisView::set_height),Smaller)));
591         items.push_back (MenuElem (_("Small"), bind (mem_fun (*this, &TimeAxisView::set_height), Small)));
592 }
593
594 void
595 TimeAxisView::build_display_menu ()
596 {
597         using namespace Menu_Helpers;
598
599         display_menu = new Menu;
600         display_menu->set_name ("ArdourContextMenu");
601
602         // Just let implementing classes define what goes into the manu
603 }
604
605 void
606 TimeAxisView::set_samples_per_unit (double spu)
607 {
608         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
609                 (*i)->set_samples_per_unit (spu);
610         }
611 }
612
613 void
614 TimeAxisView::show_timestretch (nframes_t start, nframes_t end)
615 {
616         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
617                 (*i)->show_timestretch (start, end);
618         }
619 }
620
621 void
622 TimeAxisView::hide_timestretch ()
623 {
624         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
625                 (*i)->hide_timestretch ();
626         }
627 }
628
629 void
630 TimeAxisView::show_selection (TimeSelection& ts)
631 {
632         double x1;
633         double x2;
634         double y2;
635         SelectionRect *rect;
636
637         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
638                 (*i)->show_selection (ts);
639         }
640
641         if (canvas_item_visible (selection_group)) {
642                 while (!used_selection_rects.empty()) {
643                         free_selection_rects.push_front (used_selection_rects.front());
644                         used_selection_rects.pop_front();
645                         free_selection_rects.front()->rect->hide();
646                         free_selection_rects.front()->start_trim->hide();
647                         free_selection_rects.front()->end_trim->hide();
648                 }
649                 selection_group->hide();
650         }
651
652         selection_group->show();
653         selection_group->raise_to_top();
654         
655         for (list<AudioRange>::iterator i = ts.begin(); i != ts.end(); ++i) {
656                 nframes_t start, end, cnt;
657
658                 start = (*i).start;
659                 end = (*i).end;
660                 cnt = end - start + 1;
661
662                 rect = get_selection_rect ((*i).id);
663                 
664                 x1 = editor.frame_to_unit (start);
665                 x2 = editor.frame_to_unit (start + cnt - 1);
666                 y2 = height;
667
668                 rect->rect->property_x1() = x1;
669                 rect->rect->property_y1() = 1.0;
670                 rect->rect->property_x2() = x2;
671                 rect->rect->property_y2() = y2;
672                 
673                 // trim boxes are at the top for selections
674                 
675                 if (x2 > x1) {
676                         rect->start_trim->property_x1() = x1;
677                         rect->start_trim->property_y1() = 1.0;
678                         rect->start_trim->property_x2() = x1 + trim_handle_size;
679                         rect->start_trim->property_y2() = 1.0 + trim_handle_size;
680
681                         rect->end_trim->property_x1() = x2 - trim_handle_size;
682                         rect->end_trim->property_y1() = 1.0;
683                         rect->end_trim->property_x2() = x2;
684                         rect->end_trim->property_y2() = 1.0 + trim_handle_size;
685
686                         rect->start_trim->show();
687                         rect->end_trim->show();
688                 } else {
689                         rect->start_trim->hide();
690                         rect->end_trim->hide();
691                 }
692
693                 rect->rect->show ();
694                 used_selection_rects.push_back (rect);
695         }
696 }
697
698 void
699 TimeAxisView::reshow_selection (TimeSelection& ts)
700 {
701         show_selection (ts);
702
703         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
704                 (*i)->show_selection (ts);
705         }
706 }
707
708 void
709 TimeAxisView::hide_selection ()
710 {
711         if (canvas_item_visible (selection_group)) {
712                 while (!used_selection_rects.empty()) {
713                         free_selection_rects.push_front (used_selection_rects.front());
714                         used_selection_rects.pop_front();
715                         free_selection_rects.front()->rect->hide();
716                         free_selection_rects.front()->start_trim->hide();
717                         free_selection_rects.front()->end_trim->hide();
718                 }
719                 selection_group->hide();
720         }
721         
722         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
723                 (*i)->hide_selection ();
724         }
725 }
726
727 void
728 TimeAxisView::order_selection_trims (ArdourCanvas::Item *item, bool put_start_on_top)
729 {
730         /* find the selection rect this is for. we have the item corresponding to one
731            of the trim handles.
732          */
733
734         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
735                 if ((*i)->start_trim == item || (*i)->end_trim == item) {
736                         
737                         /* make one trim handle be "above" the other so that if they overlap,
738                            the top one is the one last used.
739                         */
740                         
741                         (*i)->rect->raise_to_top ();
742                         (put_start_on_top ? (*i)->start_trim : (*i)->end_trim)->raise_to_top ();
743                         (put_start_on_top ? (*i)->end_trim : (*i)->start_trim)->raise_to_top ();
744                         
745                         break;
746                 }
747         }
748 }
749
750 SelectionRect *
751 TimeAxisView::get_selection_rect (uint32_t id)
752 {
753         SelectionRect *rect;
754
755         /* check to see if we already have a visible rect for this particular selection ID */
756
757         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
758                 if ((*i)->id == id) {
759                         return (*i);
760                 }
761         }
762
763         /* ditto for the free rect list */
764
765         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
766                 if ((*i)->id == id) {
767                         SelectionRect* ret = (*i);
768                         free_selection_rects.erase (i);
769                         return ret;
770                 }
771         }
772
773         /* no existing matching rect, so go get a new one from the free list, or create one if there are none */
774         
775         if (free_selection_rects.empty()) {
776
777                 rect = new SelectionRect;
778
779                 rect->rect = new SimpleRect (*selection_group);
780                 rect->rect->property_x1() = 0.0;
781                 rect->rect->property_y1() = 0.0;
782                 rect->rect->property_x2() = 0.0;
783                 rect->rect->property_y2() = 0.0;
784                 rect->rect->property_fill_color_rgba() = color_map[cSelectionRectFill];
785                 rect->rect->property_outline_color_rgba() = color_map[cSelectionRectOutline];
786                 
787                 rect->start_trim = new SimpleRect (*selection_group);
788                 rect->start_trim->property_x1() = 0.0;
789                 rect->start_trim->property_x2() = 0.0;
790                 rect->start_trim->property_fill_color_rgba() = color_map[cSelectionStartFill];
791                 rect->start_trim->property_outline_color_rgba() = color_map[cSelectionStartOutline];
792                 
793                 rect->end_trim = new SimpleRect (*selection_group);
794                 rect->end_trim->property_x1() = 0.0;
795                 rect->end_trim->property_x2() = 0.0;
796                 rect->end_trim->property_fill_color_rgba() = color_map[cSelectionEndFill];
797                 rect->end_trim->property_outline_color_rgba() = color_map[cSelectionEndOutline];
798
799                 free_selection_rects.push_front (rect);
800
801                 rect->rect->signal_event().connect (bind (mem_fun (editor, &PublicEditor::canvas_selection_rect_event), rect->rect, rect));
802                 rect->start_trim->signal_event().connect (bind (mem_fun (editor, &PublicEditor::canvas_selection_start_trim_event), rect->rect, rect));
803                 rect->end_trim->signal_event().connect (bind (mem_fun (editor, &PublicEditor::canvas_selection_end_trim_event), rect->rect, rect));
804         } 
805
806         rect = free_selection_rects.front();
807         rect->id = id;
808         free_selection_rects.pop_front();
809         return rect;
810 }
811
812 bool
813 TimeAxisView::is_child (TimeAxisView* tav)
814 {
815         return find (children.begin(), children.end(), tav) != children.end();
816 }
817
818 void
819 TimeAxisView::add_child (TimeAxisView* child)
820 {
821         children.push_back (child);
822 }
823
824 void
825 TimeAxisView::remove_child (TimeAxisView* child)
826 {
827         vector<TimeAxisView*>::iterator i;
828
829         if ((i = find (children.begin(), children.end(), child)) != children.end()) {
830                 children.erase (i);
831         }
832 }
833
834 void
835 TimeAxisView::get_selectables (nframes_t start, nframes_t end, double top, double bot, list<Selectable*>& result)
836 {
837         return;
838 }
839
840 void
841 TimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
842 {
843         return;
844 }
845
846 bool
847 TimeAxisView::touched (double top, double bot)
848 {
849         /* remember: this is X Window - coordinate space starts in upper left and moves down.
850            y_position is the "origin" or "top" of the track.
851          */
852
853         double mybot = y_position + height;
854         
855         return ((y_position <= bot && y_position >= top) || 
856                 ((mybot <= bot) && (top < mybot)) || 
857                 (mybot >= bot && y_position < top));
858 }               
859
860 void
861 TimeAxisView::set_parent (TimeAxisView& p)
862 {
863         parent = &p;
864 }
865
866 bool
867 TimeAxisView::has_state () const
868 {
869         return _has_state;
870 }
871
872 TimeAxisView*
873 TimeAxisView::get_parent_with_state ()
874 {
875         if (parent == 0) {
876                 return 0;
877         }
878
879         if (parent->has_state()) {
880                 return parent;
881         } 
882
883         return parent->get_parent_with_state ();
884 }               
885
886 void
887 TimeAxisView::set_state (const XMLNode& node)
888 {
889         const XMLProperty *prop;
890
891         if ((prop = node.property ("track_height")) != 0) {
892
893                 if (prop->value() == "largest") {
894                         set_height (Largest);
895                 } else if (prop->value() == "large") {
896                         set_height (Large);
897                 } else if (prop->value() == "larger") {
898                         set_height (Larger);
899                 } else if (prop->value() == "normal") {
900                         set_height (Normal);
901                 } else if (prop->value() == "smaller") {
902                         set_height (Smaller);
903                 } else if (prop->value() == "small") {
904                         set_height (Small);
905                 } else {
906                         error << string_compose(_("unknown track height name \"%1\" in XML GUI information"), prop->value()) << endmsg;
907                         set_height (Normal);
908                 }
909
910         } else {
911                 set_height (Normal);
912         }
913 }
914
915 void
916 TimeAxisView::reset_height()
917 {
918         set_height_pixels (height);
919
920         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
921                 (*i)->set_height_pixels ((TrackHeight)(*i)->height);
922         }
923 }
924         
925 uint32_t
926 TimeAxisView::height_to_pixels (TrackHeight h)
927 {
928         switch (h) {
929         case Largest:
930                 return hLargest;
931         case Large:
932                 return hLarge;
933         case Larger:
934                 return hLarger;
935         case Normal:
936                 return hNormal;
937         case Smaller:
938                 return hSmaller;
939         case Small:
940                 return hSmall;
941         }
942         
943         // what is wrong with gcc ?
944         
945         return hNormal;
946 }
947                         
948 void
949 TimeAxisView::compute_controls_size_info ()
950 {
951         Gtk::Window window (Gtk::WINDOW_TOPLEVEL);
952         Gtk::Table two_row_table (2, 8);
953         Gtk::Table one_row_table (1, 8);
954         Button* buttons[5];
955         const int border_width = 2;
956         const int extra_height = (2 * border_width) + 2; // 2 pixels for the controls frame
957
958         window.add (one_row_table);
959
960         one_row_table.set_border_width (border_width);
961         one_row_table.set_row_spacings (0);
962         one_row_table.set_col_spacings (0);
963         one_row_table.set_homogeneous (true);
964
965         two_row_table.set_border_width (border_width);
966         two_row_table.set_row_spacings (0);
967         two_row_table.set_col_spacings (0);
968         two_row_table.set_homogeneous (true);
969
970         for (int i = 0; i < 5; ++i) {
971                 buttons[i] = manage (new Button (X_("f")));
972                 buttons[i]->set_name ("TrackMuteButton");
973         }
974
975         one_row_table.attach (*buttons[0], 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
976         
977         one_row_table.show_all ();
978         Gtk::Requisition req(one_row_table.size_request ());
979
980
981         // height required to show 1 row of buttons
982
983         hSmaller = req.height + extra_height;
984
985         window.remove ();
986         window.add (two_row_table);
987
988         two_row_table.attach (*buttons[1], 5, 6, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
989         two_row_table.attach (*buttons[2], 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
990         two_row_table.attach (*buttons[3], 7, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
991         two_row_table.attach (*buttons[4], 8, 9, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
992
993         two_row_table.show_all ();
994         req = two_row_table.size_request ();
995
996         // height required to show all normal buttons 
997
998         hNormal = req.height + extra_height;
999
1000         // these heights are all just larger than normal. no more 
1001         // elements are visible (yet).
1002
1003         hLarger = hNormal + 50;
1004         hLarge = hNormal + 150;
1005         hLargest = hNormal + 250;
1006
1007         // height required to show track name
1008
1009         hSmall = 27;
1010 }
1011
1012 void
1013 TimeAxisView::show_name_label ()
1014 {
1015         if (!(name_packing & NameLabelPacked)) {
1016                 name_hbox.pack_start (name_label, true, true);
1017                 name_packing = NamePackingBits (name_packing | NameLabelPacked);
1018                 name_hbox.show ();
1019                 name_label.show ();
1020         }
1021 }
1022
1023 void
1024 TimeAxisView::show_name_entry ()
1025 {
1026         if (!(name_packing & NameEntryPacked)) {
1027                 name_hbox.pack_start (name_entry, true, true);
1028                 name_packing = NamePackingBits (name_packing | NameEntryPacked);
1029                 name_hbox.show ();
1030                 name_entry.show ();
1031         }
1032 }
1033
1034 void
1035 TimeAxisView::hide_name_label ()
1036 {
1037         if (name_packing & NameLabelPacked) {
1038                 name_hbox.remove (name_label);
1039                 name_packing = NamePackingBits (name_packing & ~NameLabelPacked);
1040         }
1041 }
1042
1043 void
1044 TimeAxisView::hide_name_entry ()
1045 {
1046         if (name_packing & NameEntryPacked) {
1047                 name_hbox.remove (name_entry);
1048                 name_packing = NamePackingBits (name_packing & ~NameEntryPacked);
1049         }
1050 }
1051
1052 void
1053 TimeAxisView::color_handler (ColorID id, uint32_t val)
1054 {
1055         switch (id) {
1056         case cSelectionRectFill:
1057                 break;
1058         case cSelectionRectOutline:
1059                 break;
1060         case cSelectionStartFill:
1061                 break;
1062         case cSelectionStartOutline:
1063                 break;
1064         case cSelectionEndFill:
1065                 break;
1066         case cSelectionEndOutline:
1067                 break;
1068         default:
1069                 break;
1070         }
1071 }
1072
1073 TimeAxisView*
1074 TimeAxisView::covers_y_position (double y)
1075 {
1076         if (hidden()) {
1077                 return 0;
1078         }
1079
1080         if (y_position <= y && y < (y_position + height)) {
1081                 return this;
1082         }
1083
1084         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1085                 TimeAxisView* tv;
1086
1087                 if ((tv = (*i)->covers_y_position (y)) != 0) {
1088                         return tv;
1089                 }
1090         }
1091
1092         return 0;
1093 }