fix math bug with numthreads computation
[ardour2.git] / gtk2_ardour / time_axis_view.cc
blob978ce6debba905a3eb99df60f7f7f7f4bebbcd9a
1 /*
2 Copyright (C) 2000 Paul Davis
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.
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.
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.
20 #include <cstdlib>
21 #include <cmath>
22 #include <algorithm>
23 #include <string>
24 #include <list>
26 #include <libgnomecanvasmm.h>
27 #include <libgnomecanvasmm/canvas.h>
28 #include <libgnomecanvasmm/item.h>
30 #include "pbd/error.h"
31 #include "pbd/convert.h"
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/selector.h>
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"
42 #include "ardour_ui.h"
43 #include "gui_thread.h"
44 #include "public_editor.h"
45 #include "time_axis_view.h"
46 #include "region_view.h"
47 #include "ghostregion.h"
48 #include "simplerect.h"
49 #include "simpleline.h"
50 #include "selection.h"
51 #include "keyboard.h"
52 #include "rgb_macros.h"
53 #include "utils.h"
54 #include "streamview.h"
56 #include "i18n.h"
58 using namespace std;
59 using namespace Gtk;
60 using namespace Gdk;
61 using namespace ARDOUR;
62 using namespace PBD;
63 using namespace Editing;
64 using namespace ArdourCanvas;
65 using Gtkmm2ext::Keyboard;
67 const double trim_handle_size = 6.0; /* pixels */
68 uint32_t TimeAxisView::extra_height;
69 uint32_t TimeAxisView::smaller_height;
70 int const TimeAxisView::_max_order = 512;
71 PBD::Signal1<void,TimeAxisView*> TimeAxisView::CatchDeletion;
73 TimeAxisView::TimeAxisView (ARDOUR::Session* sess, PublicEditor& ed, TimeAxisView* rent, Canvas& /*canvas*/)
74 : AxisView (sess),
75 controls_table (2, 8),
76 _y_position (0),
77 _editor (ed),
78 _order (0)
80 if (extra_height == 0) {
81 compute_heights ();
84 _canvas_background = new Group (*ed.get_background_group (), 0.0, 0.0);
85 _canvas_display = new Group (*ed.get_trackview_group (), 0.0, 0.0);
86 _canvas_display->hide(); // reveal as needed
88 selection_group = new Group (*_canvas_display);
89 selection_group->hide();
91 _ghost_group = new Group (*_canvas_display);
92 _ghost_group->lower_to_bottom();
93 _ghost_group->show();
95 control_parent = 0;
96 display_menu = 0;
97 _hidden = false;
98 in_destructor = false;
99 height = 0;
100 _effective_height = 0;
101 parent = rent;
102 _has_state = false;
103 last_name_entry_key_press_event = 0;
104 name_packing = NamePackingBits (0);
105 _resize_drag_start = -1;
108 Create the standard LHS Controls
109 We create the top-level container and name add the name label here,
110 subclasses can add to the layout as required
113 name_entry.set_name ("EditorTrackNameDisplay");
114 name_entry.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_button_release));
115 name_entry.signal_button_press_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_button_press));
116 name_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_key_release));
117 name_entry.signal_activate().connect (sigc::mem_fun(*this, &TimeAxisView::name_entry_activated));
118 name_entry.signal_focus_in_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_focus_in));
119 name_entry.signal_focus_out_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_focus_out));
120 Gtkmm2ext::set_size_request_to_display_given_text (name_entry, N_("gTortnam"), 10, 10); // just represents a short name
122 name_label.set_name ("TrackLabel");
123 name_label.set_alignment (0.0, 0.5);
125 /* typically, either name_label OR name_entry are visible,
126 but not both. its up to derived classes to show/hide them as they
127 wish.
130 name_hbox.show ();
132 controls_table.set_size_request (200);
133 controls_table.set_border_width (2);
134 controls_table.set_row_spacings (0);
135 controls_table.set_col_spacings (0);
136 controls_table.set_homogeneous (true);
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 ();
142 resizer.set_size_request (10, 6);
143 resizer.set_name ("ResizeHandle");
144 resizer.signal_expose_event().connect (sigc::mem_fun (*this, &TimeAxisView::resizer_expose));
145 resizer.signal_button_press_event().connect (sigc::mem_fun (*this, &TimeAxisView::resizer_button_press));
146 resizer.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::resizer_button_release));
147 resizer.signal_motion_notify_event().connect (sigc::mem_fun (*this, &TimeAxisView::resizer_motion));
149 resizer.set_events (Gdk::BUTTON_PRESS_MASK|
150 Gdk::BUTTON_RELEASE_MASK|
151 Gdk::POINTER_MOTION_MASK|
152 Gdk::SCROLL_MASK);
154 resizer_box.pack_start (resizer, false, false);
155 resizer.show ();
156 resizer_box.show();
158 HSeparator* separator = manage (new HSeparator());
160 controls_vbox.pack_start (controls_table, false, false);
161 controls_vbox.pack_end (resizer_box, false, false);
162 controls_vbox.show ();
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);
169 controls_ebox.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_button_release));
170 controls_ebox.signal_scroll_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_scroll), true);
172 controls_hbox.pack_start (controls_ebox, false, false);
173 controls_hbox.show ();
175 time_axis_vbox.pack_start (controls_hbox, true, true);
176 time_axis_vbox.pack_end (*separator, false, false);
177 time_axis_vbox.show();
179 ColorsChanged.connect (sigc::mem_fun (*this, &TimeAxisView::color_handler));
181 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&TimeAxisView::erase_ghost, this, _1), gui_context());
184 TimeAxisView::~TimeAxisView()
186 in_destructor = true;
188 for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
189 delete *i;
192 for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
193 delete (*i)->rect;
194 delete (*i)->start_trim;
195 delete (*i)->end_trim;
199 for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
200 delete (*i)->rect;
201 delete (*i)->start_trim;
202 delete (*i)->end_trim;
205 for (list<SimpleLine*>::iterator i = feature_lines.begin(); i != feature_lines.end(); ++i) {
206 delete (*i);
209 delete selection_group;
210 selection_group = 0;
212 delete _canvas_background;
213 _canvas_background = 0;
215 delete _canvas_display;
216 _canvas_display = 0;
218 delete display_menu;
219 display_menu = 0;
222 /** Display this TimeAxisView as the nth component of the parent box, at y.
224 * @param y y position.
225 * @param nth index for this TimeAxisView, increased if this view has children.
226 * @param parent parent component.
227 * @return height of this TimeAxisView.
229 guint32
230 TimeAxisView::show_at (double y, int& nth, VBox *parent)
232 if (control_parent) {
233 control_parent->reorder_child (time_axis_vbox, nth);
234 } else {
235 control_parent = parent;
236 parent->pack_start (time_axis_vbox, false, false);
237 parent->reorder_child (time_axis_vbox, nth);
240 _order = nth;
242 if (_y_position != y) {
243 _canvas_display->property_y () = y;
244 _canvas_background->property_y () = y;
245 /* silly canvas */
246 _canvas_display->move (0.0, 0.0);
247 _canvas_background->move (0.0, 0.0);
248 _y_position = y;
252 _canvas_background->raise_to_top ();
253 _canvas_display->raise_to_top ();
255 if (_marked_for_display) {
256 time_axis_vbox.show ();
257 controls_ebox.show ();
258 _canvas_background->show ();
261 _hidden = false;
263 _effective_height = current_height ();
265 /* now show children */
267 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
268 if (canvas_item_visible ((*i)->_canvas_display)) {
269 ++nth;
270 _effective_height += (*i)->show_at (y + _effective_height, nth, parent);
274 return _effective_height;
277 void
278 TimeAxisView::clip_to_viewport ()
280 if (_marked_for_display) {
281 if (_y_position + _effective_height < _editor.get_trackview_group_vertical_offset () || _y_position > _editor.get_trackview_group_vertical_offset () + _canvas_display->get_canvas()->get_height()) {
282 _canvas_background->hide ();
283 _canvas_display->hide ();
284 return;
286 _canvas_background->show ();
287 _canvas_display->show ();
289 return;
292 bool
293 TimeAxisView::controls_ebox_scroll (GdkEventScroll* ev)
295 if (Keyboard::some_magic_widget_has_focus()) {
296 return false;
299 switch (ev->direction) {
300 case GDK_SCROLL_UP:
301 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
302 step_height (true);
303 return true;
304 } else if (Keyboard::no_modifiers_active (ev->state)) {
305 _editor.scroll_tracks_up_line();
306 return true;
308 break;
310 case GDK_SCROLL_DOWN:
311 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
312 step_height (false);
313 return true;
314 } else if (Keyboard::no_modifiers_active (ev->state)) {
315 _editor.scroll_tracks_down_line();
316 return true;
318 break;
320 default:
321 /* no handling for left/right, yet */
322 break;
325 return false;
328 bool
329 TimeAxisView::controls_ebox_button_release (GdkEventButton* ev)
331 switch (ev->button) {
332 case 1:
333 selection_click (ev);
334 break;
336 case 3:
337 popup_display_menu (ev->time);
338 break;
341 return true;
344 void
345 TimeAxisView::selection_click (GdkEventButton* ev)
347 Selection::Operation op = ArdourKeyboard::selection_type (ev->state);
348 _editor.set_selected_track (*this, op, false);
351 void
352 TimeAxisView::hide ()
354 if (_hidden) {
355 return;
358 _canvas_display->hide ();
359 _canvas_background->hide ();
361 if (control_parent) {
362 control_parent->remove (time_axis_vbox);
363 control_parent = 0;
366 _y_position = -1;
367 _hidden = true;
369 /* now hide children */
371 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
372 (*i)->hide ();
375 /* if its hidden, it cannot be selected */
377 _editor.get_selection().remove (this);
379 Hiding ();
382 void
383 TimeAxisView::step_height (bool bigger)
385 static const uint32_t step = 20;
387 if (bigger) {
388 set_height (height + step);
389 } else {
390 if (height > step) {
391 set_height (std::max (height - step, preset_height (HeightSmall)));
392 } else if (height != preset_height (HeightSmall)) {
393 set_height (HeightSmall);
398 void
399 TimeAxisView::set_heights (uint32_t h)
401 TrackSelection& ts (_editor.get_selection().tracks);
403 for (TrackSelection::iterator i = ts.begin(); i != ts.end(); ++i) {
404 (*i)->set_height (h);
408 void
409 TimeAxisView::set_height (Height h)
411 set_height (preset_height (h));
414 void
415 TimeAxisView::set_height(uint32_t h)
417 time_axis_vbox.property_height_request () = h;
418 height = h;
420 for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
421 (*i)->set_height ();
424 if (canvas_item_visible (selection_group)) {
425 /* resize the selection rect */
426 show_selection (_editor.get_selection().time);
429 reshow_feature_lines ();
432 bool
433 TimeAxisView::name_entry_key_release (GdkEventKey* ev)
435 TrackViewList::iterator i;
437 switch (ev->keyval) {
438 case GDK_Escape:
439 name_entry.select_region (0,0);
440 controls_ebox.grab_focus ();
441 name_entry_changed ();
442 return true;
444 /* Shift+Tab Keys Pressed. Note that for Shift+Tab, GDK actually
445 * generates a different ev->keyval, rather than setting
446 * ev->state.
448 case GDK_ISO_Left_Tab:
449 case GDK_Tab:
451 name_entry_changed ();
452 TrackViewList const & allviews = _editor.get_track_views ();
453 TrackViewList::const_iterator i = find (allviews.begin(), allviews.end(), this);
454 if (ev->keyval == GDK_Tab) {
455 if (i != allviews.end()) {
456 do {
457 if (++i == allviews.end()) {
458 return true;
460 } while((*i)->hidden());
462 } else {
463 if (i != allviews.begin()) {
464 do {
465 if (i == allviews.begin()) {
466 return true;
468 --i;
469 } while ((*i)->hidden());
474 /* resize to show editable name display */
476 if ((*i)->current_height() <= preset_height (HeightSmaller)) {
477 (*i)->set_height (HeightSmaller);
480 (*i)->name_entry.grab_focus();
482 return true;
484 case GDK_Up:
485 case GDK_Down:
486 name_entry_changed ();
487 return true;
489 default:
490 break;
493 #ifdef TIMEOUT_NAME_EDIT
494 /* adapt the timeout to reflect the user's typing speed */
496 guint32 name_entry_timeout;
498 if (last_name_entry_key_press_event) {
499 /* timeout is 1/2 second or 5 times their current inter-char typing speed */
500 name_entry_timeout = std::max (500U, (5 * (ev->time - last_name_entry_key_press_event)));
501 } else {
502 /* start with a 1 second timeout */
503 name_entry_timeout = 1000;
506 last_name_entry_key_press_event = ev->time;
508 /* wait 1 seconds and if no more keys are pressed, act as if they pressed enter */
510 name_entry_key_timeout.disconnect();
511 name_entry_key_timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_key_timed_out), name_entry_timeout);
512 #endif
514 return false;
517 bool
518 TimeAxisView::name_entry_focus_in (GdkEventFocus*)
520 name_entry.select_region (0, -1);
521 name_entry.set_name ("EditorActiveTrackNameDisplay");
522 return false;
525 bool
526 TimeAxisView::name_entry_focus_out (GdkEventFocus*)
528 /* clean up */
530 last_name_entry_key_press_event = 0;
531 name_entry_key_timeout.disconnect ();
532 name_entry.set_name ("EditorTrackNameDisplay");
533 name_entry.select_region (0,0);
535 /* do the real stuff */
537 name_entry_changed ();
539 return false;
542 bool
543 TimeAxisView::name_entry_key_timed_out ()
545 name_entry_activated();
546 return false;
549 void
550 TimeAxisView::name_entry_activated ()
552 controls_ebox.grab_focus();
555 void
556 TimeAxisView::name_entry_changed ()
560 bool
561 TimeAxisView::name_entry_button_press (GdkEventButton *ev)
563 if (ev->button == 3) {
564 return true;
566 return false;
569 bool
570 TimeAxisView::name_entry_button_release (GdkEventButton *ev)
572 if (ev->button == 3) {
573 popup_display_menu (ev->time);
574 return true;
576 return false;
579 void
580 TimeAxisView::conditionally_add_to_selection ()
582 Selection& s (_editor.get_selection ());
584 if (!s.selected (this)) {
585 _editor.set_selected_track (*this, Selection::Set);
589 void
590 TimeAxisView::popup_display_menu (guint32 when)
592 if (display_menu == 0) {
593 build_display_menu ();
596 conditionally_add_to_selection ();
597 display_menu->popup (1, when);
600 void
601 TimeAxisView::set_selected (bool yn)
603 if (yn == _selected) {
604 return;
607 Selectable::set_selected (yn);
609 if (_selected) {
610 controls_ebox.set_name (controls_base_selected_name);
611 time_axis_vbox.set_name (controls_base_selected_name);
612 controls_vbox.set_name (controls_base_selected_name);
613 } else {
614 controls_ebox.set_name (controls_base_unselected_name);
615 time_axis_vbox.set_name (controls_base_unselected_name);
616 controls_vbox.set_name (controls_base_unselected_name);
617 hide_selection ();
619 /* children will be set for the yn=true case. but when deselecting
620 the editor only has a list of top-level trackviews, so we
621 have to do this here.
624 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
625 (*i)->set_selected (false);
629 resizer.queue_draw ();
632 void
633 TimeAxisView::build_display_menu ()
635 using namespace Menu_Helpers;
637 delete display_menu;
639 display_menu = new Menu;
640 display_menu->set_name ("ArdourContextMenu");
642 // Just let implementing classes define what goes into the manu
645 void
646 TimeAxisView::set_samples_per_unit (double spu)
648 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
649 (*i)->set_samples_per_unit (spu);
652 AnalysisFeatureList::const_iterator i;
653 list<ArdourCanvas::SimpleLine*>::iterator l;
655 for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
656 (*l)->property_x1() = _editor.frame_to_pixel (*i);
657 (*l)->property_x2() = _editor.frame_to_pixel (*i);
661 void
662 TimeAxisView::show_timestretch (nframes_t start, nframes_t end)
664 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
665 (*i)->show_timestretch (start, end);
669 void
670 TimeAxisView::hide_timestretch ()
672 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
673 (*i)->hide_timestretch ();
677 void
678 TimeAxisView::show_selection (TimeSelection& ts)
680 double x1;
681 double x2;
682 double y2;
683 SelectionRect *rect;
685 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
686 (*i)->show_selection (ts);
689 if (canvas_item_visible (selection_group)) {
690 while (!used_selection_rects.empty()) {
691 free_selection_rects.push_front (used_selection_rects.front());
692 used_selection_rects.pop_front();
693 free_selection_rects.front()->rect->hide();
694 free_selection_rects.front()->start_trim->hide();
695 free_selection_rects.front()->end_trim->hide();
697 selection_group->hide();
700 selection_group->show();
701 selection_group->raise_to_top();
703 for (list<AudioRange>::iterator i = ts.begin(); i != ts.end(); ++i) {
704 nframes_t start, end, cnt;
706 start = (*i).start;
707 end = (*i).end;
708 cnt = end - start + 1;
710 rect = get_selection_rect ((*i).id);
712 x1 = _editor.frame_to_unit (start);
713 x2 = _editor.frame_to_unit (start + cnt - 1);
714 y2 = current_height();
716 rect->rect->property_x1() = x1;
717 rect->rect->property_y1() = 1.0;
718 rect->rect->property_x2() = x2;
719 rect->rect->property_y2() = y2;
721 // trim boxes are at the top for selections
723 if (x2 > x1) {
724 rect->start_trim->property_x1() = x1;
725 rect->start_trim->property_y1() = 1.0;
726 rect->start_trim->property_x2() = x1 + trim_handle_size;
727 rect->start_trim->property_y2() = 1.0 + trim_handle_size;
729 rect->end_trim->property_x1() = x2 - trim_handle_size;
730 rect->end_trim->property_y1() = 1.0;
731 rect->end_trim->property_x2() = x2;
732 rect->end_trim->property_y2() = 1.0 + trim_handle_size;
734 rect->start_trim->show();
735 rect->end_trim->show();
736 } else {
737 rect->start_trim->hide();
738 rect->end_trim->hide();
741 rect->rect->show ();
742 used_selection_rects.push_back (rect);
746 void
747 TimeAxisView::reshow_selection (TimeSelection& ts)
749 show_selection (ts);
751 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
752 (*i)->show_selection (ts);
756 void
757 TimeAxisView::hide_selection ()
759 if (canvas_item_visible (selection_group)) {
760 while (!used_selection_rects.empty()) {
761 free_selection_rects.push_front (used_selection_rects.front());
762 used_selection_rects.pop_front();
763 free_selection_rects.front()->rect->hide();
764 free_selection_rects.front()->start_trim->hide();
765 free_selection_rects.front()->end_trim->hide();
767 selection_group->hide();
770 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
771 (*i)->hide_selection ();
775 void
776 TimeAxisView::order_selection_trims (ArdourCanvas::Item *item, bool put_start_on_top)
778 /* find the selection rect this is for. we have the item corresponding to one
779 of the trim handles.
782 for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
783 if ((*i)->start_trim == item || (*i)->end_trim == item) {
785 /* make one trim handle be "above" the other so that if they overlap,
786 the top one is the one last used.
789 (*i)->rect->raise_to_top ();
790 (put_start_on_top ? (*i)->start_trim : (*i)->end_trim)->raise_to_top ();
791 (put_start_on_top ? (*i)->end_trim : (*i)->start_trim)->raise_to_top ();
793 break;
798 SelectionRect *
799 TimeAxisView::get_selection_rect (uint32_t id)
801 SelectionRect *rect;
803 /* check to see if we already have a visible rect for this particular selection ID */
805 for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
806 if ((*i)->id == id) {
807 return (*i);
811 /* ditto for the free rect list */
813 for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
814 if ((*i)->id == id) {
815 SelectionRect* ret = (*i);
816 free_selection_rects.erase (i);
817 return ret;
821 /* no existing matching rect, so go get a new one from the free list, or create one if there are none */
823 if (free_selection_rects.empty()) {
825 rect = new SelectionRect;
827 rect->rect = new SimpleRect (*selection_group);
828 rect->rect->property_x1() = 0.0;
829 rect->rect->property_y1() = 0.0;
830 rect->rect->property_x2() = 0.0;
831 rect->rect->property_y2() = 0.0;
832 rect->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
833 rect->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
835 rect->start_trim = new SimpleRect (*selection_group);
836 rect->start_trim->property_x1() = 0.0;
837 rect->start_trim->property_x2() = 0.0;
838 rect->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
839 rect->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
841 rect->end_trim = new SimpleRect (*selection_group);
842 rect->end_trim->property_x1() = 0.0;
843 rect->end_trim->property_x2() = 0.0;
844 rect->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
845 rect->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
847 free_selection_rects.push_front (rect);
849 rect->rect->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_rect_event), rect->rect, rect));
850 rect->start_trim->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_start_trim_event), rect->rect, rect));
851 rect->end_trim->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_end_trim_event), rect->rect, rect));
854 rect = free_selection_rects.front();
855 rect->id = id;
856 free_selection_rects.pop_front();
857 return rect;
860 struct null_deleter { void operator()(void const *) const {} };
862 bool
863 TimeAxisView::is_child (TimeAxisView* tav)
865 return find (children.begin(), children.end(), boost::shared_ptr<TimeAxisView>(tav, null_deleter())) != children.end();
868 void
869 TimeAxisView::add_child (boost::shared_ptr<TimeAxisView> child)
871 children.push_back (child);
874 void
875 TimeAxisView::remove_child (boost::shared_ptr<TimeAxisView> child)
877 Children::iterator i;
879 if ((i = find (children.begin(), children.end(), child)) != children.end()) {
880 children.erase (i);
884 void
885 TimeAxisView::get_selectables (nframes_t /*start*/, nframes_t /*end*/, double /*top*/, double /*bot*/, list<Selectable*>& /*result*/)
887 return;
890 void
891 TimeAxisView::get_inverted_selectables (Selection& /*sel*/, list<Selectable*>& /*result*/)
893 return;
896 void
897 TimeAxisView::add_ghost (RegionView* rv)
899 GhostRegion* gr = rv->add_ghost (*this);
901 if(gr) {
902 ghosts.push_back(gr);
906 void
907 TimeAxisView::remove_ghost (RegionView* rv)
909 rv->remove_ghost_in (*this);
912 void
913 TimeAxisView::erase_ghost (GhostRegion* gr)
915 if (in_destructor) {
916 return;
919 for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
920 if ((*i) == gr) {
921 ghosts.erase (i);
922 break;
927 bool
928 TimeAxisView::touched (double top, double bot)
930 /* remember: this is X Window - coordinate space starts in upper left and moves down.
931 y_position is the "origin" or "top" of the track.
934 double mybot = _y_position + current_height();
936 return ((_y_position <= bot && _y_position >= top) ||
937 ((mybot <= bot) && (top < mybot)) ||
938 (mybot >= bot && _y_position < top));
941 void
942 TimeAxisView::set_parent (TimeAxisView& p)
944 parent = &p;
947 bool
948 TimeAxisView::has_state () const
950 return _has_state;
953 TimeAxisView*
954 TimeAxisView::get_parent_with_state ()
956 if (parent == 0) {
957 return 0;
960 if (parent->has_state()) {
961 return parent;
964 return parent->get_parent_with_state ();
968 XMLNode&
969 TimeAxisView::get_state ()
971 /* XXX: is this method used? */
973 XMLNode* node = new XMLNode ("TAV-" + name());
974 char buf[32];
976 snprintf (buf, sizeof(buf), "%u", height);
977 node->add_property ("height", buf);
978 node->add_property ("marked-for-display", (_marked_for_display ? "1" : "0"));
979 return *node;
983 TimeAxisView::set_state (const XMLNode& node, int /*version*/)
985 const XMLProperty *prop;
987 /* XXX: I think this might be vestigial */
988 if ((prop = node.property ("marked-for-display")) != 0) {
989 _marked_for_display = (prop->value() == "1");
992 if ((prop = node.property ("shown-editor")) != 0) {
993 _marked_for_display = string_is_affirmative (prop->value ());
996 if ((prop = node.property ("track-height")) != 0) {
998 if (prop->value() == "largest") {
999 set_height (HeightLargest);
1000 } else if (prop->value() == "large") {
1001 set_height (HeightLarge);
1002 } else if (prop->value() == "larger") {
1003 set_height (HeightLarger);
1004 } else if (prop->value() == "normal") {
1005 set_height (HeightNormal);
1006 } else if (prop->value() == "smaller") {
1007 set_height (HeightSmaller);
1008 } else if (prop->value() == "small") {
1009 set_height (HeightSmall);
1010 } else {
1011 error << string_compose(_("unknown track height name \"%1\" in XML GUI information"), prop->value()) << endmsg;
1012 set_height (HeightNormal);
1015 } else if ((prop = node.property ("height")) != 0) {
1017 set_height (atoi (prop->value()));
1019 } else {
1021 set_height (HeightNormal);
1024 return 0;
1027 void
1028 TimeAxisView::reset_height()
1030 set_height (height);
1032 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1033 (*i)->set_height ((*i)->height);
1037 void
1038 TimeAxisView::compute_heights ()
1040 Gtk::Window window (Gtk::WINDOW_TOPLEVEL);
1041 Gtk::Table two_row_table (2, 8);
1042 Gtk::Table one_row_table (1, 8);
1043 Button* buttons[5];
1044 const int border_width = 2;
1045 extra_height = (2 * border_width)
1046 //+ 2 // 2 pixels for the hseparator between TimeAxisView control areas
1047 + 10; // resizer button (3 x 2 pixel elements + 2 x 2 pixel gaps)
1049 window.add (one_row_table);
1051 one_row_table.set_border_width (border_width);
1052 one_row_table.set_row_spacings (0);
1053 one_row_table.set_col_spacings (0);
1054 one_row_table.set_homogeneous (true);
1056 two_row_table.set_border_width (border_width);
1057 two_row_table.set_row_spacings (0);
1058 two_row_table.set_col_spacings (0);
1059 two_row_table.set_homogeneous (true);
1061 for (int i = 0; i < 5; ++i) {
1062 buttons[i] = manage (new Button (X_("f")));
1063 buttons[i]->set_name ("TrackMuteButton");
1066 one_row_table.attach (*buttons[0], 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
1068 one_row_table.show_all ();
1069 Gtk::Requisition req(one_row_table.size_request ());
1071 // height required to show 1 row of buttons
1073 smaller_height = req.height + extra_height;
1076 void
1077 TimeAxisView::show_name_label ()
1079 if (!(name_packing & NameLabelPacked)) {
1080 name_hbox.pack_start (name_label, true, true);
1081 name_packing = NamePackingBits (name_packing | NameLabelPacked);
1082 name_hbox.show ();
1083 name_label.show ();
1087 void
1088 TimeAxisView::show_name_entry ()
1090 if (!(name_packing & NameEntryPacked)) {
1091 name_hbox.pack_start (name_entry, true, true);
1092 name_packing = NamePackingBits (name_packing | NameEntryPacked);
1093 name_hbox.show ();
1094 name_entry.show ();
1098 void
1099 TimeAxisView::hide_name_label ()
1101 if (name_packing & NameLabelPacked) {
1102 name_hbox.remove (name_label);
1103 name_packing = NamePackingBits (name_packing & ~NameLabelPacked);
1107 void
1108 TimeAxisView::hide_name_entry ()
1110 if (name_packing & NameEntryPacked) {
1111 name_hbox.remove (name_entry);
1112 name_packing = NamePackingBits (name_packing & ~NameEntryPacked);
1116 void
1117 TimeAxisView::color_handler ()
1119 for (list<GhostRegion*>::iterator i=ghosts.begin(); i != ghosts.end(); i++ ) {
1120 (*i)->set_colors();
1123 for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
1125 (*i)->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
1126 (*i)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1128 (*i)->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1129 (*i)->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1131 (*i)->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1132 (*i)->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1135 for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
1137 (*i)->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
1138 (*i)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1140 (*i)->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1141 (*i)->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1143 (*i)->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1144 (*i)->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1148 /** @return Pair: TimeAxisView, layer index.
1149 * TimeAxisView is non-0 if this object covers y, or one of its children does.
1150 * If the covering object is a child axis, then the child is returned.
1151 * TimeAxisView is 0 otherwise.
1152 * Layer index is the layer number if the TimeAxisView is valid and is in stacked
1153 * region display mode, otherwise 0.
1155 std::pair<TimeAxisView*, layer_t>
1156 TimeAxisView::covers_y_position (double y)
1158 if (hidden()) {
1159 return std::make_pair ( (TimeAxisView *) 0, 0);
1162 if (_y_position <= y && y < (_y_position + height)) {
1164 /* work out the layer index if appropriate */
1165 layer_t l = 0;
1166 if (layer_display () == Stacked && view ()) {
1167 /* compute layer */
1168 l = layer_t ((_y_position + height - y) / (view()->child_height ()));
1169 /* clamp to max layers to be on the safe side; sometimes the above calculation
1170 returns a too-high value */
1171 if (l >= view()->layers ()) {
1172 l = view()->layers() - 1;
1176 return std::make_pair (this, l);
1179 for (Children::const_iterator i = children.begin(); i != children.end(); ++i) {
1181 std::pair<TimeAxisView*, int> const r = (*i)->covers_y_position (y);
1182 if (r.first) {
1183 return r;
1187 return std::make_pair ( (TimeAxisView *) 0, 0);
1190 void
1191 TimeAxisView::show_feature_lines (const AnalysisFeatureList& pos)
1193 analysis_features = pos;
1194 reshow_feature_lines ();
1198 void
1199 TimeAxisView::hide_feature_lines ()
1201 list<ArdourCanvas::SimpleLine*>::iterator l;
1203 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1204 (*l)->hide();
1208 void
1209 TimeAxisView::reshow_feature_lines ()
1211 while (feature_lines.size()< analysis_features.size()) {
1212 ArdourCanvas::SimpleLine* l = new ArdourCanvas::SimpleLine (*_canvas_display);
1213 l->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1214 feature_lines.push_back (l);
1217 while (feature_lines.size() > analysis_features.size()) {
1218 ArdourCanvas::SimpleLine *line = feature_lines.back();
1219 feature_lines.pop_back ();
1220 delete line;
1223 AnalysisFeatureList::const_iterator i;
1224 list<ArdourCanvas::SimpleLine*>::iterator l;
1226 for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1227 (*l)->property_x1() = _editor.frame_to_pixel (*i);
1228 (*l)->property_x2() = _editor.frame_to_pixel (*i);
1229 (*l)->property_y1() = 0;
1230 (*l)->property_y2() = current_height();
1231 (*l)->show ();
1235 bool
1236 TimeAxisView::resizer_button_press (GdkEventButton* event)
1238 _resize_drag_start = event->y_root;
1239 return true;
1242 bool
1243 TimeAxisView::resizer_button_release (GdkEventButton*)
1245 _resize_drag_start = -1;
1246 return true;
1249 void
1250 TimeAxisView::idle_resize (uint32_t h)
1252 set_height (h);
1255 bool
1256 TimeAxisView::resizer_motion (GdkEventMotion* ev)
1258 if (_resize_drag_start >= 0) {
1259 int32_t const delta = (int32_t) floor (ev->y_root - _resize_drag_start);
1260 _editor.add_to_idle_resize (this, delta);
1261 _resize_drag_start = ev->y_root;
1264 return true;
1267 bool
1268 TimeAxisView::resizer_expose (GdkEventExpose* event)
1270 int w, h, x, y, d;
1271 Glib::RefPtr<Gdk::Window> win (resizer.get_window());
1272 Glib::RefPtr<Gdk::GC> dark (resizer.get_style()->get_fg_gc (STATE_NORMAL));
1273 Glib::RefPtr<Gdk::GC> light (resizer.get_style()->get_bg_gc (STATE_NORMAL));
1275 win->draw_rectangle (controls_ebox.get_style()->get_bg_gc(STATE_NORMAL),
1276 true,
1277 event->area.x,
1278 event->area.y,
1279 event->area.width,
1280 event->area.height);
1282 win->get_geometry (x, y, w, h, d);
1284 /* handle/line #1 */
1286 win->draw_line (dark, 0, 0, w - 2, 0);
1287 win->draw_point (dark, 0, 1);
1288 win->draw_line (light, 1, 1, w - 1, 1);
1289 win->draw_point (light, w - 1, 0);
1291 /* handle/line #2 */
1293 win->draw_line (dark, 0, 4, w - 2, 4);
1294 win->draw_point (dark, 0, 5);
1295 win->draw_line (light, 1, 5, w - 1, 5);
1296 win->draw_point (light, w - 1, 4);
1298 /* use vertical resize mouse cursor */
1299 win->set_cursor(Gdk::Cursor(Gdk::SB_V_DOUBLE_ARROW));
1301 return true;
1304 bool
1305 TimeAxisView::set_visibility (bool yn)
1307 if (yn != marked_for_display()) {
1308 if (yn) {
1309 set_marked_for_display (true);
1310 canvas_display()->show();
1311 } else {
1312 set_marked_for_display (false);
1313 hide ();
1315 return true; // things changed
1318 return false;
1321 uint32_t
1322 TimeAxisView::preset_height (Height h)
1324 switch (h) {
1325 case HeightLargest:
1326 return extra_height + 48 + 250;
1327 case HeightLarger:
1328 return extra_height + 48 + 150;
1329 case HeightLarge:
1330 return extra_height + 48 + 50;
1331 case HeightNormal:
1332 return extra_height + 48;
1333 case HeightSmall:
1334 return 27;
1335 case HeightSmaller:
1336 return smaller_height;
1339 /* NOTREACHED */
1340 return 0;