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