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