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