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