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