Fix DSP load sorting with inactive plugins
[ardour.git] / gtk2_ardour / automation_streamview.cc
1 /*
2  * Copyright (C) 2007-2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <cmath>
24 #include <list>
25 #include <utility>
26
27 #include <gtkmm.h>
28
29 #include "gtkmm2ext/gtk_ui.h"
30
31 #include "pbd/compose.h"
32 #include "canvas/debug.h"
33
34 #include "ardour/midi_region.h"
35 #include "ardour/midi_source.h"
36
37 #include "automation_region_view.h"
38 #include "automation_streamview.h"
39 #include "automation_time_axis.h"
40 #include "gui_thread.h"
41 #include "public_editor.h"
42 #include "region_selection.h"
43 #include "region_view.h"
44 #include "rgb_macros.h"
45 #include "selection.h"
46 #include "ui_config.h"
47
48 #include "pbd/i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace ARDOUR_UI_UTILS;
53 using namespace PBD;
54 using namespace Editing;
55
56 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
57         : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
58                       tv.canvas_display())
59         , _automation_view(tv)
60         , _pending_automation_state (Off)
61 {
62         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group auto %1", tv.name()));
63         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle auto %1", tv.name()));
64
65         color_handler ();
66
67         UIConfiguration::instance().ColorsChanged.connect(sigc::mem_fun(*this, &AutomationStreamView::color_handler));
68 }
69
70 AutomationStreamView::~AutomationStreamView ()
71 {
72 }
73
74
75 RegionView*
76 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wait_for_data, bool /*recording*/)
77 {
78         if (!region) {
79                 return 0;
80         }
81
82         if (wait_for_data) {
83                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
84                 if (mr) {
85                         Source::Lock lock(mr->midi_source()->mutex());
86                         mr->midi_source()->load_model(lock);
87                 }
88         }
89
90         const boost::shared_ptr<AutomationControl> control = boost::dynamic_pointer_cast<AutomationControl> (
91                 region->control (_automation_view.parameter(), true)
92                 );
93
94         boost::shared_ptr<AutomationList> list;
95         if (control) {
96                 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
97                 if (control->list() && !list) {
98                         error << _("unable to display automation region for control without list") << endmsg;
99                         return 0;
100                 }
101         }
102
103         AutomationRegionView *region_view;
104         std::list<RegionView *>::iterator i;
105
106         for (i = region_views.begin(); i != region_views.end(); ++i) {
107                 if ((*i)->region() == region) {
108
109                         /* great. we already have an AutomationRegionView for this Region. use it again. */
110                         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
111
112                         if (arv->line()) {
113                                 arv->line()->set_list (list);
114                         }
115                         (*i)->set_valid (true);
116                         (*i)->enable_display (wait_for_data);
117                         display_region(arv);
118
119                         return 0;
120                 }
121         }
122
123         region_view = new AutomationRegionView (
124                 _canvas_group, _automation_view, region,
125                 _automation_view.parameter (), list,
126                 _samples_per_pixel, region_color
127                 );
128
129         region_view->init (false);
130         region_views.push_front (region_view);
131
132         /* follow global waveform setting */
133
134         if (wait_for_data) {
135                 region_view->enable_display(true);
136                 // region_view->midi_region()->midi_source(0)->load_model();
137         }
138
139         display_region (region_view);
140
141         /* catch regionview going away */
142         region->DropReferences.connect (*this, invalidator (*this), boost::bind (&AutomationStreamView::remove_region_view, this, boost::weak_ptr<Region>(region)), gui_context());
143
144         /* setup automation state for this region */
145         boost::shared_ptr<AutomationLine> line = region_view->line ();
146         if (line && line->the_list()) {
147                 line->the_list()->set_automation_state (automation_state ());
148         }
149
150         RegionViewAdded (region_view);
151
152         return region_view;
153 }
154
155 void
156 AutomationStreamView::display_region(AutomationRegionView* region_view)
157 {
158         region_view->line().reset();
159 }
160
161 void
162 AutomationStreamView::set_automation_state (AutoState state)
163 {
164         /* Setting the automation state for this view sets the state of all regions' lists to the same thing */
165
166         if (region_views.empty()) {
167                 _pending_automation_state = state;
168         } else {
169                 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
170
171                 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
172                         if ((*i)->the_list()) {
173                                 (*i)->the_list()->set_automation_state (state);
174                         }
175                 }
176         }
177 }
178
179 void
180 AutomationStreamView::redisplay_track ()
181 {
182         // Flag region views as invalid and disable drawing
183         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
184                 (*i)->set_valid (false);
185                 (*i)->enable_display(false);
186         }
187
188         // Add and display region views, and flag them as valid
189         if (_trackview.is_track()) {
190                 _trackview.track()->playlist()->foreach_region (
191                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
192                         );
193         }
194
195         // Stack regions by layer, and remove invalid regions
196         layer_regions();
197 }
198
199
200 void
201 AutomationStreamView::setup_rec_box ()
202 {
203 }
204
205 void
206 AutomationStreamView::color_handler ()
207 {
208         if (_trackview.is_midi_track()) {
209                 canvas_rect->set_fill_color (UIConfiguration::instance().color_mod ("midi track base", "midi track base"));
210         } else {
211                 canvas_rect->set_fill_color (UIConfiguration::instance().color ("midi bus base"));
212         }
213 }
214
215 AutoState
216 AutomationStreamView::automation_state () const
217 {
218         if (region_views.empty()) {
219                 return _pending_automation_state;
220         }
221
222         boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
223         if (!line || !line->the_list()) {
224                 return Off;
225         }
226
227         return line->the_list()->automation_state ();
228 }
229
230 bool
231 AutomationStreamView::has_automation () const
232 {
233         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
234
235         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
236                 if ((*i)->npoints() > 0) {
237                         return true;
238                 }
239         }
240
241         return false;
242 }
243
244 /** Our parent AutomationTimeAxisView calls this when the user requests a particular
245  *  InterpolationStyle; tell the AutomationLists in our regions.
246  */
247 void
248 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
249 {
250         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
251
252         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
253                 (*i)->the_list()->set_interpolation (s);
254         }
255 }
256
257 AutomationList::InterpolationStyle
258 AutomationStreamView::interpolation () const
259 {
260         if (region_views.empty()) {
261                 return AutomationList::Linear;
262         }
263
264         AutomationRegionView* v = dynamic_cast<AutomationRegionView*> (region_views.front());
265         if (v) {
266                 return v->line()->the_list()->interpolation ();
267         }
268         return AutomationList::Linear;
269 }
270
271 /** Clear all automation displayed in this view */
272 void
273 AutomationStreamView::clear ()
274 {
275         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
276
277         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
278                 (*i)->clear ();
279         }
280 }
281
282 /** @param start Start position in session samples.
283  *  @param end End position in session samples.
284  *  @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
285  *  @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
286  *  NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
287  *  confusing.
288  */
289 void
290 AutomationStreamView::get_selectables (samplepos_t start, samplepos_t end, double botfrac, double topfrac, list<Selectable*>& results, bool /*within*/)
291 {
292         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
293                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
294                 if (arv) {
295                         arv->line()->get_selectables (start, end, botfrac, topfrac, results);
296                 }
297         }
298 }
299
300 void
301 AutomationStreamView::set_selected_points (PointSelection& ps)
302 {
303         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
304
305         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
306                 (*i)->set_selected_points (ps);
307         }
308 }
309
310 list<boost::shared_ptr<AutomationLine> >
311 AutomationStreamView::get_lines () const
312 {
313         list<boost::shared_ptr<AutomationLine> > lines;
314
315         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
316                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
317                 if (arv) {
318                         lines.push_back (arv->line());
319                 }
320         }
321
322         return lines;
323 }
324
325 bool
326 AutomationStreamView::paste (samplepos_t                               pos,
327                              unsigned                                  paste_count,
328                              float                                     times,
329                              boost::shared_ptr<ARDOUR::AutomationList> alist)
330 {
331         /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
332
333         if (region_views.empty()) {
334                 return false;
335         }
336
337         region_views.sort (RegionView::PositionOrder());
338
339         list<RegionView*>::const_iterator prev = region_views.begin ();
340
341         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
342                 if ((*i)->region()->position() > pos) {
343                         break;
344                 }
345                 prev = i;
346         }
347
348         boost::shared_ptr<Region> r = (*prev)->region ();
349
350         /* If *prev doesn't cover pos, it's no good */
351         if (r->position() > pos || ((r->position() + r->length()) < pos)) {
352                 return false;
353         }
354
355         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);
356         return arv ? arv->paste(pos, paste_count, times, alist) : false;
357 }