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