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