2 Copyright (C) 2001-2007 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.
25 #include <gtkmm2ext/gtk_ui.h>
27 #include "ardour/midi_region.h"
28 #include "ardour/midi_source.h"
29 #include "ardour/region_factory.h"
31 #include "automation_streamview.h"
32 #include "region_view.h"
33 #include "automation_region_view.h"
34 #include "automation_time_axis.h"
35 #include "canvas-simplerect.h"
36 #include "region_selection.h"
37 #include "selection.h"
38 #include "public_editor.h"
39 #include "ardour_ui.h"
40 #include "rgb_macros.h"
41 #include "gui_thread.h"
43 #include "simplerect.h"
44 #include "simpleline.h"
47 using namespace ARDOUR;
49 using namespace Editing;
51 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
52 : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
53 new ArdourCanvas::Group(*tv.canvas_background()),
54 new ArdourCanvas::Group(*tv.canvas_display()))
55 , _controller(tv.controller())
56 , _automation_view(tv)
57 , _pending_automation_state (Off)
59 //canvas_rect->property_fill_color_rgba() = stream_base_color;
60 canvas_rect->property_outline_color_rgba() = RGBA_BLACK;
63 AutomationStreamView::~AutomationStreamView ()
69 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wfd, bool /*recording*/)
72 cerr << "No region" << endl;
77 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
79 mr->midi_source()->load_model();
83 const boost::shared_ptr<AutomationControl> control = boost::dynamic_pointer_cast<AutomationControl> (
84 region->control (_controller->controllable()->parameter(), true)
87 boost::shared_ptr<AutomationList> list;
89 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
90 assert(!control->list() || list);
93 AutomationRegionView *region_view;
94 std::list<RegionView *>::iterator i;
96 for (i = region_views.begin(); i != region_views.end(); ++i) {
97 if ((*i)->region() == region) {
99 /* great. we already have an AutomationRegionView for this Region. use it again. */
100 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
103 arv->line()->set_list (list);
104 (*i)->set_valid (true);
105 (*i)->enable_display(wfd);
112 region_view = new AutomationRegionView (_canvas_group, _automation_view, region,
113 _controller->controllable()->parameter(), list,
114 _samples_per_unit, region_color);
116 region_view->init (region_color, false);
117 region_views.push_front (region_view);
119 /* follow global waveform setting */
122 region_view->enable_display(true);
123 //region_view->midi_region()->midi_source(0)->load_model();
126 display_region(region_view);
128 /* catch regionview going away */
129 region->DropReferences.connect (*this, invalidator (*this), boost::bind (&AutomationStreamView::remove_region_view, this, boost::weak_ptr<Region>(region)), gui_context());
131 /* setup automation state for this region */
132 boost::shared_ptr<AutomationLine> line = region_view->line ();
133 if (line && line->the_list()) {
134 line->the_list()->set_automation_state (automation_state ());
137 RegionViewAdded (region_view);
143 AutomationStreamView::display_region(AutomationRegionView* region_view)
145 region_view->line().reset();
149 AutomationStreamView::set_automation_state (AutoState state)
151 /* Setting the automation state for this view sets the state of all regions' lists to the same thing */
153 if (region_views.empty()) {
154 _pending_automation_state = state;
156 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
158 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
159 if ((*i)->the_list()) {
160 (*i)->the_list()->set_automation_state (state);
167 AutomationStreamView::redisplay_track ()
169 list<RegionView *>::iterator i, tmp;
171 // Flag region views as invalid and disable drawing
172 for (i = region_views.begin(); i != region_views.end(); ++i) {
173 (*i)->set_valid (false);
174 (*i)->enable_display(false);
177 // Add and display region views, and flag them as valid
178 if (_trackview.is_track()) {
179 _trackview.track()->playlist()->foreach_region (
180 sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
184 // Stack regions by layer, and remove invalid regions
190 AutomationStreamView::setup_rec_box ()
195 AutomationStreamView::color_handler ()
197 /*if (_trackview.is_midi_track()) {
198 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
201 if (!_trackview.is_midi_track()) {
202 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
207 AutomationStreamView::automation_state () const
209 if (region_views.empty()) {
210 return _pending_automation_state;
213 boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
214 if (!line || !line->the_list()) {
218 return line->the_list()->automation_state ();
222 AutomationStreamView::has_automation () const
224 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
226 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
227 if ((*i)->npoints() > 0) {
235 /** Our parent AutomationTimeAxisView calls this when the user requests a particular
236 * InterpolationStyle; tell the AutomationLists in our regions.
239 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
241 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
243 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
244 (*i)->the_list()->set_interpolation (s);
248 AutomationList::InterpolationStyle
249 AutomationStreamView::interpolation () const
251 if (region_views.empty()) {
252 return AutomationList::Linear;
255 AutomationRegionView* v = dynamic_cast<AutomationRegionView*> (region_views.front());
258 return v->line()->the_list()->interpolation ();
261 /** Clear all automation displayed in this view */
263 AutomationStreamView::clear ()
265 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
267 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
272 /** @param start Start position in session frames.
273 * @param end End position in session frames.
274 * @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
275 * @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
276 * NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
280 AutomationStreamView::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
282 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
283 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
285 arv->line()->get_selectables (start, end, botfrac, topfrac, results);
290 AutomationStreamView::set_selected_points (PointSelection& ps)
292 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
294 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
295 (*i)->set_selected_points (ps);
299 list<boost::shared_ptr<AutomationLine> >
300 AutomationStreamView::get_lines () const
302 list<boost::shared_ptr<AutomationLine> > lines;
304 for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
305 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
307 lines.push_back (arv->line());
313 struct RegionPositionSorter {
314 bool operator() (RegionView* a, RegionView* b) {
315 return a->region()->position() < b->region()->position();
320 /** @param pos Position, in session frames.
321 * @return AutomationLine to paste to for that position, or 0 if there is none appropriate.
323 boost::shared_ptr<AutomationLine>
324 AutomationStreamView::paste_line (framepos_t pos)
326 /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
328 if (region_views.empty()) {
329 return boost::shared_ptr<AutomationLine> ();
332 region_views.sort (RegionPositionSorter ());
334 list<RegionView*>::const_iterator prev = region_views.begin ();
336 for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
337 if ((*i)->region()->position() > pos) {
343 boost::shared_ptr<Region> r = (*prev)->region ();
345 /* If *prev doesn't cover pos, it's no good */
346 if (r->position() > pos || ((r->position() + r->length()) < pos)) {
347 return boost::shared_ptr<AutomationLine> ();
350 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);