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