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