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