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