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