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